
    -`i                        d dl Z d dlmZ d dlmZmZmZ d dlmZ d dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d d	lmZ d d
lmZ ddlmZ ddlmZ  ej                    rd dlmZmZ  ej                    rddlm Z  ddl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(  ej)                    rddl*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2 ddl3m4Z4  ee5          Z6 ed          Z7 ed          Z8dee7e8f         dee7e8f         fdZ9 G d de0          Z:dS )    N)Callable)Any	ParamSpecTypeVar)fx)envs)rocm_aiter_ops)
VllmConfigset_current_vllm_config)init_logger)current_platform)set_env_var   )PostCleanupPass)VllmInductorPass)RocmAiterRMSNormFusionPass'RocmAiterSiluMulFp8GroupQuantFusionPass)ActivationQuantFusionPass)RMSNormQuantFusionPass)AttnFusionPass)QKNormRoPEFusionPass)SequenceParallelismPass)AllReduceFusionPassAsyncTPPass)FixFunctionalizationPass)CustomGraphPassInductorPassget_pass_context)NoOpEliminationPassPRfnreturnc                      t          j                   dt          j        dt          j        dt
          f fd            }|S )z
    Function decorator that turns on inductor pattern match debug
    for the duration of the call.
    Used to avoid logging builtin Inductor pattern matching.
    argskwargsr#   c                      t           j        x}0t          d|          5   | i |cd d d            S # 1 swxY w Y    | i |S )N!TORCHINDUCTOR_PATTERN_MATCH_DEBUG)r   VLLM_PATTERN_MATCH_DEBUGr   )r%   r&   	debug_valr"   s      q/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/vllm/compilation/pass_manager.pywrapperz)with_pattern_match_debug.<locals>.wrapper8   s    66IC@)LL + +r4*6**+ + + + + + + + + + + + + + + +r4"6"""s   599)	functoolswrapsr    r%   r&   r!   )r"   r,   s   ` r+   with_pattern_match_debugr/   1   sX     _R#qv # #a # # # # # # N    c                   p    e Zd ZdZddZedej        ddfd            Zde	ddfdZ
d	eddfd
ZdefdZdS )PostGradPassManagera$  
    The pass manager for post-grad passes.
    It handles configuration, adding custom passes, and running passes.
    It supports uuid for the Inductor code cache. That includes torch<2.6
    support using pickling (in .inductor_pass.CustomGraphPass).

    The order of the post-grad post-passes is:
    1. passes (constructor parameter)
    2. default passes (NoopEliminationPass, FusionPass)
    3. config["post_grad_custom_post_pass"] (if it exists)
    4. fix_functionalization
    This way, all passes operate on a functionalized graph.
    r#   Nc                     g | _         d S N)passes)selfs    r+   __init__zPostGradPassManager.__init__R   s    *,r0   graphc                    dt           _        t                      j        }| j        D ]T}|                    |          r! ||           t           xj        dz  c_        8t                              d||           U|                     |           t           xj        dz  c_        | 	                    |           d t           _        d S )Nr   r   z!Skipping %s with compile range %s)
r   dump_prefixr   compile_ranger5   is_applicable_for_rangeloggerdebugpost_cleanupfix_functionalization)r6   r8   r;   pass_s       r+   __call__zPostGradPassManager.__call__U   s    '($(**8[ 	X 	XE,,];; Xe ,,1,,,@%WWWW 	%   $$)$$ 	""5)))'+$$$r0   configc                 0   |j         j        | _        t          |d          5  | j        j        r| xj        t          |          gz  c_        | j        j        rH| xj        t          |          gz  c_        | j        j        r| xj        t          |          gz  c_        | j        j
        r| xj        t          |          gz  c_        | j        j        rO| xj        t          |          gz  c_        t          j                    r| xj        t!          |          gz  c_        | j        j        rO| xj        t%          |          gz  c_        t          j                    r| xj        t'          |          gz  c_        | j        j        r| xj        t+          |          gz  c_        | j        j        r| xj        t/          |          gz  c_        t1          |          | _        t5          |          | _        d d d            d S # 1 swxY w Y   d S )NF)check_compile)compilation_configpass_configr   eliminate_noopsr5   r   	enable_spr   fuse_gemm_commsr   fuse_allreduce_rmsr   fuse_norm_quantr   r	   
is_enabledr   fuse_act_quantr   r   fuse_attn_quantr   enable_qk_norm_rope_fusionr   r   r?   r   r@   )r6   rC   s     r+   	configurezPostGradPassManager.configurej   so   !4@ %V5AAA 	J 	J/ = 3F ; ;<<) 9 7 ? ?@@#3 9KKK$7$7#88KK2 = 3F ; ;<</  6v > >??!,.. KK26::$ KK . U 9& A ABB!,.. UKK$KF$S$S#TTKK/ 8v 6 677: > 4V < <== !0 7 7D)A&)I)ID&?	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	J 	Js   GHHHrA   c                 h    t          |t                    sJ | j                            |           d S r4   )
isinstancer   r5   append)r6   rA   s     r+   addzPostGradPassManager.add   s4    %.....5!!!!!r0   c                 n   g }d| j                                         i}| j        D ])}|                    |                                           *|                    | j                                                   t          t                      j                  |d<   ||d<   t          j
        |          S )z
        The PostGradPassManager is set as a custom pass in the Inductor and
        affects compilation caching. Its uuid depends on the UUIDs of all
        dependent passes and the pass config. See InductorPass for more info.
        rG   r;   r5   )rG   compute_hashr5   rT   uuidr@   strr   r;   r   	hash_dict)r6   r5   staterA   s       r+   rX   zPostGradPassManager.uuid   s     !.0@0M0M0O0O P[ 	( 	(EMM%**,,''''d05577888 "%%5%7%7%E!F!Fo h%e,,,r0   )r#   N)__name__
__module____qualname____doc__r7   r/   r   GraphrB   r
   rQ   r   rU   rY   rX    r0   r+   r2   r2   C   s         - - - - ,bh ,4 , , , ,(#J
 #Jt #J #J #J #JJ" "$ " " " "-c - - - - - -r0   r2   );r-   collections.abcr   typingr   r   r   torchr   vllmr   vllm._aiter_opsr	   vllm.configr
   r   vllm.loggerr   vllm.platformsr   vllm.utils.system_utilsr   r?   r   vllm_inductor_passr   rM   "vllm.compilation.rocm_aiter_fusionr   r   is_cuda_alikeactivation_quant_fusionr   fusionr   fusion_attnr   qk_norm_rope_fusionr   sequence_parallelismr   is_cudacollective_fusionr   r   r@   r   inductor_passr   r   r   noop_eliminationr   r\   r=   r    r!   r/   r2   ra   r0   r+   <module>rw      s       $ $ $ $ $ $ * * * * * * * * * *             * * * * * * ; ; ; ; ; ; ; ; # # # # # # + + + + + + / / / / / / ) ) ) ) ) ) 0 0 0 0 0 0>        
 "!## >BBBBBB......++++++999999====== DCCCCCCCC ; ; ; ; ; ;         
 2 1 1 1 1 1	X		IcNNGCLL!Q$ HQTN    $a- a- a- a- a-/ a- a- a- a- a-r0   