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G dd dZdS )    N)abstractmethod)contextmanager)CodeType)CallableListc                   @   s\   e Zd ZdZdd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.
    Tcompiled_callableuse_custom_dispatcherc                 C   s2   || _ | jjj| _g | _tjj	| j
 || _d S N)r   	__class__forward__code__original_code_objectcompiled_codestorch_dynamoconvert_frameregister_bytecode_hookbytecode_hookr	   )selfr   r	    r   H/home/ubuntu/vllm_env/lib/python3.10/site-packages/depyf/optimization.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__    s   z0TorchCompileWrapperWithCustomDispatcher.__call__c                 O   s   d S r
   r   r   r   r   r   r   '   s   z/TorchCompileWrapperWithCustomDispatcher.forwardold_codenew_codec                 C   s   || j urdS t }	 |j}|jj}|jjtj	j
d }|dkr'|dkr'nq|jd }|j|ks4J |jd | ur=dS | j| dS )z8Hook to save the compiled bytecode for direct execution.NT_compilezconvert_frame.pyframer   )r   sys	_getframef_backf_codeco_nameco_filenamesplitospathsepf_localsr   append)r   r   r   r!   	code_name	file_namer   r   r   r   +   s   

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   r0   r   r   r   dispatch_to_code>   s   
z8TorchCompileWrapperWithCustomDispatcher.dispatch_to_codeN)T)__name__
__module____qualname____doc__r   boolr   r   r   r   r   r   r   intr1   r   r   r   r   r      s    
r   )r)   r"   abcr   
contextlibr   typesr   typingr   r   r   r   r   r   r   r   <module>   s    