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   Z/home/ubuntu/veenaModal/venv/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