o
    )i                     @   s   d dl Z d dl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Zd dlmZ d dlmZmZmZ d dlmZ eeZG dd	 d	ZdS )
    N)abstractmethod)contextmanager)CodeType)CallableOptional)CompilationLevelCUDAGraphModeget_current_vllm_config)init_loggerc                   @   sd   e Zd ZdZ		ddee defddZdd	 Ze	d
d Z
dedefddZedefddZdS )'TorchCompileWrapperWithCustomDispatchera  
    A wrapper class for torch.compile, with a custom dispatch logic.
    Subclasses should:
    1. Implement the forward method
    2. Implement the dispatch logic in the __call__ method
        It can use `self.compiled_codes` to access the compiled bytecode,
        and `with self.dispatch_to_code(index):` to dispatch to
        the compiled code.
    3. Implement the `__init__` method to determine how to call
        `torch.compile` over the forward method.
    Nr   compiled_callablecompilation_levelc                 C   s   t  }|| _|d u r+|j|}d }t|tr |dkr t  jj}tj| j	t
j||d}|| _| jj	j| _g | _tjj| j |tjk| _d S )NZinductor)Z	fullgraphbackendoptions)r	   vllm_configcompilation_configZinit_backend
isinstancestrZinductor_compile_configtorchcompileforwardenvsZ"VLLM_TEST_DYNAMO_FULLGRAPH_CAPTUREr   	__class____code__original_code_objectcompiled_codesZ_dynamoZconvert_frameZregister_bytecode_hookbytecode_hookr   ZDYNAMO_ONCEZuse_custom_dispatcher)selfr   r   r   r   r    r   d/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/vllm/compilation/wrapper.py__init__"   s*   z0TorchCompileWrapperWithCustomDispatcher.__init__c                 O   s   | j |i |S )zImplement the dispatch logic here, beyond the torch.compile level.
        NOTE: this function can have additional arguments beyond the forward
         method, for directly dispatching to the compiled code.
        )r   r   argskwargsr   r   r   __call__C   s   z0TorchCompileWrapperWithCustomDispatcher.__call__c                 O   s   d S )Nr   r!   r   r   r   r   J   s   z/TorchCompileWrapperWithCustomDispatcher.forwardold_codenew_codec                 C   s  || j urdS t }|r0|jr0|j}|jj}|jjtj	j
d }|dkr+|dkr+n|r0|js|jd }|j|ks<J |jd | urEdS | j| | jjj}t|tr|dkr| jjj}tj	|d| d	}tj	|sz+d
dl}	|	|}
t|d}||
 W d   n1 sw   Y  td| W n	 ty   Y nw | jjjtj krd|j!v rd
dl}	|	|}
d|
 }t"|dS dS )z8Hook to save the compiled bytecode for direct execution.N_compilezconvert_frame.pyframer    Zrank_ztransformed_code.pyr   wz#Dynamo transformed code saved to %supdateaB  Assigning / modifying buffers of nn.Module during forward pass is not allowed when using cudagraph inside the compiler because it will cause silent errors. Please use eager mode or fix the code. The following code contains clues about which buffer is being modified (please search for the usage of the function `update`):
)#r   sys	_getframef_backf_codeco_nameco_filenamesplitospathsepf_localsr   appendr   r   Zdebug_dump_pathr   r   Zparallel_configrankjoinexistsdepyfZ	decompileopenwriteloggerdebug	ExceptionZcudagraph_moder   NONEco_namesRuntimeError)r   r%   r&   r)   Z	code_name	file_nameZdebug_dump_dirr9   Zdecompiled_filer<   srcfmsgr   r   r   r   N   sX   








z5TorchCompileWrapperWithCustomDispatcher.bytecode_hookindexc                 c   s(    | j | | jj_dV  | j| jj_dS )a  Context manager to dispatch to the compiled code.
        Why does this work? Because Dynamo guarantees that the compiled
        bytecode has exactly the same arguments, cell variables, and free
        variables as the original code. Therefore we can directly switch
        the code object in the function and call it.

        See https://dev-discuss.pytorch.org/t/what-is-the-relationship-requirement-among-original-bytecode-transformed-bytecode-and-bytecode-returned-by-hooks-in-dynamo/1693/7 for more details.
        N)r   r   r   r   r   )r   rI   r   r   r   dispatch_to_code~   s   
z8TorchCompileWrapperWithCustomDispatcher.dispatch_to_code)Nr   )__name__
__module____qualname____doc__r   r   intr    r$   r   r   r   r   r   rJ   r   r   r   r   r      s    
!
0r   )r4   r-   abcr   
contextlibr   typesr   typingr   r   r   Z	vllm.envsr   Zvllm.configr   r   r	   Zvllm.loggerr
   rK   r?   r   r   r   r   r   <module>   s   