o
    "i(                     @   s   d dl Z d dlmZ d dlZd dlm  mZ zd dlZ	W n e
y(   dZ	Y nw defddZdd Zdd	 Zd
d ZG dd dZdd ZdejfddZdefddZdedefddZdS )    N)Listreturnc                   C   s
   t j S )a  
    Indicates whether we are tracing/compiling with torch.compile() or torch.export().

    If need to check specifically that TorchDynamo is used, then use
    torch.compiler.is_dynamo_compiling().

    TODO(khabinov): we should deprecate this function and use one of these two:
    * torch.compiler.is_compiling(),
    * torch.compiler.is_dynamo_compiling().
    It will depend on the context where to use what.
    )torchcompileris_compiling r   r   Z/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_dynamo/external_utils.pyr      s   
r   c                    s   t   fdd}|S )zB
    Create an extra frame around fn that is not in skipfiles
    c                     s    | i |S Nr   )argskwargsfnr   r   inner$   s   zwrap_inline.<locals>.inner)	functoolswraps)r   r   r   r   r   wrap_inline   s   r   c                 G   s   | | }|du r|d S |S )zA
    Used by compiled autograd to handle hook returning None
    Nr   r   )hookr
   resultr   r   r   	call_hook+   s   r   c                    s"   t s S t  fdd}|S )zDecorator that turns a function from ``np.ndarray``s to ``np.ndarray``s into a function
    from ``torch.Tensor``s to ``torch.Tensor``s.
    c                     s>   t tjdd | |f\} } | i |}t tjdd |S )Nc                 S   s   |   S r	   )numpyxr   r   r   <lambda>?   s    z*wrap_numpy.<locals>.wrap.<locals>.<lambda>c                 S   s
   t | S r	   )r   	as_tensorr   r   r   r   r   B   s   
 )pytreetree_map_onlyr   Tensornpndarray)r
   r   outfr   r   wrap<   s
   zwrap_numpy.<locals>.wrap)r   r   r   )r!   r"   r   r    r   
wrap_numpy5   s
   r#   c                   @   s2   e Zd Zdejjjdeej fddZ	dd Z
dS )FakeBackwardCFunctionrealsaved_tensorsc                 C   s   || _ || _d S r	   )r%   r&   )selfr%   r&   r   r   r   __init__H   s   
zFakeBackwardCFunction.__init__c                 C   s   t | j|S r	   )getattrr%   )r'   namer   r   r   __getattr__P   s   z!FakeBackwardCFunction.__getattr__N)__name__
__module____qualname__r   autogradfunctionBackwardCFunctionr   r   r(   r+   r   r   r   r   r$   G   s    
r$   c                 G   s4   t | |}|jj|g|R  }t|tur|f}|S r	   )r$   _forward_clsbackwardtypetuple)backward_c_functionr&   r
   fakegradsr   r   r   call_backwardV   s
   
r9   r   c                 C   s   |    S r	   )untyped_storagesizer   r   r   r   untyped_storage_sizea   s   r<   	hook_namec                 O   s   t | ||i |S r	   r)   )bw_stater=   r
   r   r   r   r   call_hook_from_backward_statee   s   r@   
hooks_namemodule_namec          
      G   s@   t ||}t ||}|D ]}|||g|R  }	|	d ur|	}q|S r	   r>   )
_r   r?   rA   rB   r
   modulehooksr   
new_resultr   r   r   %call_module_hooks_from_backward_statei   s   

rG   )r   typingr   r   torch.utils._pytreeutils_pytreer   r   r   ModuleNotFoundErrorboolr   r   r   r#   r$   r9   r   r<   strr@   rG   r   r   r   r   <module>   s,   
