o
    [Û·i¹
  ã                   @   sF   d dl Z d dlmZmZ de jde jfdd„Zdadd„ Zd	d
„ ZdS )é    N)Ú	decompileÚDecompilationErrorÚcodeÚnew_codec              
   C   s²   dd l }|j dd¡}dd l}| |j¡rWz;zt|ƒ}| d¡ | |¡ W n ty@ } z| dt	|ƒ¡ W Y d }~nd }~ww W | d¡ d S W | d¡ d S | d¡ w d S )Nr   ztorch._dynamo.convert_frameÚbytecodezpossible source code:zDecompilation fails due to: %sziIf you find the decompiled code is wrong,please submit an issue at https://github.com/thuml/depyf/issues.)
ÚtorchÚ_loggingÚgetArtifactLoggerÚloggingÚisEnabledForÚDEBUGr   Údebugr   Ústr)r   r   r   Úbytecode_logr
   Údecompiled_srcÚe© r   úS/home/ubuntu/vllm_env/lib/python3.10/site-packages/depyf/explain/enhance_logging.pyÚpytorch_bytecode_src_hook   s2   ÿ
€ÿÿÿþÿÿør   c                  C   s&   ddl } tdur
dS | jj t¡adS )aŸ  
    Install the bytecode hook for PyTorch, integrate into PyTorch's logging system.

    Example:

    .. code-block:: python

        import torch
        import depyf
        depyf.install()
        # anything with torch.compile
        @torch.compile
        def f(a, b):
            return a + b
        f(torch.tensor(1), torch.tensor(2))
    
    Turn on bytecode log by ``export TORCH_LOGS="+bytecode"``, and execute the script.
    We will see the decompiled source code in the log:

    .. code-block:: text

        ORIGINAL BYTECODE f test.py line 5 
        7           0 LOAD_FAST                0 (a)
                    2 LOAD_FAST                1 (b)
                    4 BINARY_ADD
                    6 RETURN_VALUE
        
        
        MODIFIED BYTECODE f test.py line 5 
        5           0 LOAD_GLOBAL              0 (__compiled_fn_1)
                    2 LOAD_FAST                0 (a)
                    4 LOAD_FAST                1 (b)
                    6 CALL_FUNCTION            2
                    8 UNPACK_SEQUENCE          1
                    10 RETURN_VALUE
        
        
        possible source code:
        def f(a, b):
            __temp_2, = __compiled_fn_1(a, b)
            return __temp_2
        
        If you find the decompiled code is wrong,please submit an issue at https://github.com/thuml/depyf/issues.
    
    To uninstall the hook, use :func:`depyf.uninstall()`.
    r   N)r   Ú_handleÚ_dynamoÚconvert_frameÚregister_bytecode_hookr   )r   r   r   r   Úinstall   s   /ÿr   c                   C   s   t du rdS t  ¡  da dS )zf
    Uninstall the bytecode hook for PyTorch.
    Should be called after :func:`depyf.install()`.
    N)r   Úremover   r   r   r   Ú	uninstallU   s   r   )	ÚtypesÚdepyf.decompilerr   r   ÚCodeTyper   r   r   r   r   r   r   r   Ú<module>   s    7