§
    `ƒi™  ã                   ól   — 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¦  «        ZdS )é    N)Úabstractmethod)Úcontextmanager)ÚCodeType)ÚCallableÚListc                   ól   — e Zd ZdZddedefd„Zd„ Zed„ ¦   «         Z	de
d	e
fd
„Zedef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                 ó®   — || _         | 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   s      úf/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/depyf/optimization.pyÚ__init__z0TorchCompileWrapperWithCustomDispatcher.__init__   sL   € Ø!2ˆÔØ$(¤NÔ$:Ô$CˆÔ!Ø.0ˆÔÝŒÔ#×:Ò:¸4Ô;MÑNÔNÐNà+@ˆÔ"Ð"Ð"ó    c                 ó   —  | j         |i |¤ŽS )zØImplement 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Úkwargss      r   Ú__call__z0TorchCompileWrapperWithCustomDispatcher.__call__    s   € ð
 &ˆtÔ% tÐ6¨vÐ6Ð6Ð6r   c                 ó   — d S r   © r   s      r   r   z/TorchCompileWrapperWithCustomDispatcher.forward'   s   € àˆr   Úold_codeÚnew_codec                 óx  — || j         urdS t          j        ¦   «         }	 |j        }|j        j        }|j        j                             t          j	        j
        ¦  «        d         }|dk    r|dk    rnŒU|j        d         }|j        |k    sJ ‚|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_names         r   r   z5TorchCompileWrapperWithCustomDispatcher.bytecode_hook+   sÊ   € à˜4Ô4Ð4Ð4ØˆFÝ”‘”ˆð	Ø”LˆEØœÔ,ˆIØœÔ0×6Ò6µr´w´{ÑCÔCÀBÔGˆIØ˜JÒ&Ð&¨9Ð8JÒ+JÐ+JØð	ð ”˜wÔ'ˆØŒ|˜xÒ'Ð'Ð'Ð'àŒ>˜&Ô!¨Ð-Ð-ØˆFàÔ×"Ò" 8Ñ,Ô,Ð,Ð,Ð,r   Úindexc              #   óv   K  — | 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   r7   s     r   Údispatch_to_codez8TorchCompileWrapperWithCustomDispatcher.dispatch_to_code>   s<   è è € ð +/Ô*=¸eÔ*DˆŒÔÔ'ØˆˆˆØ*.Ô*CˆŒÔÔ'Ð'Ð'r   N)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úboolr   r    r   r   r   r   r   Úintr9   r"   r   r   r	   r	      sÆ   € € € € € ð
ð 
ðAð A¨(ð AÈ4ð Að Að Að Að7ð 7ð 7ð ðð ñ „^ðð- hð -¸(ð -ð -ð -ð -ð& ðD cð Dð Dð Dñ „^ðDð Dð Dr   r	   )r0   r)   Úabcr   Ú
contextlibr   Útypesr   Útypingr   r   r   r	   r"   r   r   ú<module>rD      sª   ðØ 	€	€	€	Ø 
€
€
€
Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %Ø Ð Ð Ð Ð Ð Ø !Ð !Ð !Ð !Ð !Ð !Ð !Ð !à €€€ð?Dð ?Dð ?Dð ?Dð ?Dñ ?Dô ?Dð ?Dð ?Dð ?Dr   