o
    á)i+  ã                   @   sÔ   d dl mZ d dlmZ d dlmZ d dlmZ e ¡ r(ddl	m
Z
 ddlmZ e ¡ r4ddlmZmZ dd	lm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 eeƒZ G dd„ deƒZ!dS )é    )Úfx)Ú
VllmConfig)Úinit_logger)Úcurrent_platformé   )Ú
FusionPass)ÚAttnFusionPass)ÚAllReduceFusionPassÚAsyncTPPass)ÚActivationQuantFusionPass)ÚFixFunctionalizationPass)ÚCustomGraphPassÚInductorPassÚget_pass_context)ÚNoOpEliminationPass)ÚSequenceParallelismPass)ÚVllmInductorPassc                   @   sL   e Zd ZdZdd„ Zdejfdd„Zdefdd	„Z	d
e
fdd„Zd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.
    c                 C   s
   g | _ d S ©N)Úpasses)Úself© r   úi/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/compilation/pass_manager.pyÚ__init__*   s   
zPostGradPassManager.__init__Úgraphc                 C   s4   t ƒ j}| jD ]}| |¡r||ƒ q|  |¡ d S r   )r   Zruntime_shaper   Zis_applicable_for_shapeÚfix_functionalization)r   r   ÚshapeÚpass_r   r   r   Ú__call__-   s   

€zPostGradPassManager.__call__Úconfigc                 C   sÖ   |j j| _| jjr|  jt|ƒg7  _| jjr/|  jt|ƒg7  _| jjr/|  jt|ƒg7  _| jj	rH|  jt
 |¡g7  _|  jt|ƒg7  _| jjrV|  jt|ƒg7  _| jjrd|  jt|ƒg7  _t|ƒ| _d S r   )Zcompilation_configÚpass_configZenable_noopr   r   Zenable_sequence_parallelismr   Zenable_async_tpr
   Zenable_fusionr   Úinstancer   Zenable_attn_fusionr   Zenable_fi_allreduce_fusionr	   r   r   )r   r   r   r   r   Ú	configure6   s   
zPostGradPassManager.configurer   c                 C   s   t |tƒsJ ‚| j |¡ d S r   )Ú
isinstancer   r   Úappend)r   r   r   r   r   ÚaddJ   s   zPostGradPassManager.addc                 C   sL   | j  ¡ g dœ}| jD ]}|d  | ¡ ¡ q|d  | j ¡ ¡ t |¡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.
        )r    r   r   )r    Úuuidr   r$   r   r   Z	hash_dict)r   Ústater   r   r   r   r&   N   s
   

zPostGradPassManager.uuidN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   ZGraphr   r   r"   r   r%   r&   r   r   r   r   r      s    	r   N)"Ztorchr   Zvllm.configr   Zvllm.loggerr   Zvllm.platformsr   Zis_cuda_alikeZfusionr   Zfusion_attnr   Zis_cudaZcollective_fusionr	   r
   Zactivation_quant_fusionr   r   r   Zinductor_passr   r   r   Znoop_eliminationr   Zsequence_parallelismr   Zvllm_inductor_passr   r(   Úloggerr   r   r   r   r   Ú<module>   s"   