
    קgc                         d dl Z d dlZd dlmZ d dlZd dlmc mZ 	 d dl	Z
n# e$ r dZ
Y nw xY wdefdZd Zd Zd Z G d d	          Zd
 Zdej        fdZ G d d          ZdefdZdedefdZdS )    N)Listreturnc                  >    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     X/var/www/html/ai-engine/env/lib/python3.11/site-packages/torch/_dynamo/external_utils.pyr   r      s     >&&(((r
   c                 F     t          j                    fd            }|S )zB
    Create an extra frame around fn that is not in skipfiles
    c                       | i |S Nr	   )argskwargsfns     r   innerzwrap_inline.<locals>.inner&   s    r4"6"""r
   )	functoolswraps)r   r   s   ` r   wrap_inliner   !   s:    
 _R# # # # # Lr
   c                 Z     | | }||d         S |d         dk    rt          d          |S )zA
    Used by compiled autograd to handle hook returning None
    Nr   	hook_typepost_acc_grad_hookz5Tensor post accumulate grad hooks should return None.)RuntimeError)hookr   r   results       r   	call_hookr   -   sB     T4[F~Aw		 4	4	4RSSSMr
   c                 X     t           s S t          j                    f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                      t          j        t          j        d | |f          \  } } | i |}t          j        t          j        d |          S )Nc                 *    |                                  S r   )numpyxs    r   <lambda>z*wrap_numpy.<locals>.wrap.<locals>.<lambda>C   s    AGGII r
   c                 *    t          j        |           S r   )r   	as_tensorr!   s    r   r#   z*wrap_numpy.<locals>.wrap.<locals>.<lambda>F   s    %/!:L:L r
   )pytreetree_map_onlyr   Tensornpndarray)r   r   outfs      r   wrapzwrap_numpy.<locals>.wrap@   s[    +L--f~
 
f a   #BJ0L0LcRRRr
   )r)   r   r   )r,   r-   s   ` r   
wrap_numpyr.   9   sL      _QS S S S S Kr
   c                   \    e Zd Zdej        j        j        deej                 ddfdZ	d Z
dS )FakeBackwardCFunctionrealsaved_tensorsr   Nc                 "    || _         || _        d S r   )r1   r2   )selfr1   r2   s      r   __init__zFakeBackwardCFunction.__init__L   s    
 	*r
   c                 z    |dk    r!t          j        dt                     | j        S t	          | j        |          S )Nsaved_variablesz4'saved_variables' is deprecated; use 'saved_tensors')warningswarnDeprecationWarningr2   getattrr1   )r4   names     r   __getattr__z!FakeBackwardCFunction.__getattr__T   sG    $$$MF"   %% ty$'''r
   )__name__
__module____qualname__r   autogradfunctionBackwardCFunctionr   r(   r5   r=   r	   r
   r   r0   r0   K   sb        +n%7+ EL)+ 
	+ + + +	( 	( 	( 	( 	(r
   r0   c                 ~    t          | |          } |j        j        |g|R  }t          |          t          ur|f}|S r   )r0   _forward_clsbackwardtypetuple)backward_c_functionr2   r   fakegradss        r   call_backwardrL   a   sN     !4mDDD&D&t3d333E E{{%Lr
   r"   c                 N    |                                                                  S r   )untyped_storagesizer!   s    r   untyped_storage_sizerP   l   s     ##%%%r
   c                   P    e Zd Zed             Zed             Zed             ZdS )FakeCompiledAutogradEnginec                 0    |                      |           d S r   )append)final_callbackscbs     r   queue_callbackz)FakeCompiledAutogradEngine.queue_callbackq   s    r"""""r
   c                     d}|t          |           k     r*| |         } |             |dz  }|t          |           k     *|                                  d S )Nr      )lenclear)rU   irV   s      r   exec_final_callbacksz/FakeCompiledAutogradEngine.exec_final_callbacksu   sg    #o&&&& #BBDDDFA #o&&&& 	r
   c                      d S r   r	   r	   r
   r   _exec_final_callbacks_stubz5FakeCompiledAutogradEngine._exec_final_callbacks_stub~   s    r
   N)r>   r?   r@   staticmethodrW   r]   r_   r	   r
   r   rR   rR   p   sc        # # \#     \    \  r
   rR   	hook_namec                 .     t          | |          |i |S r   r;   )bw_statera   r   r   s       r   call_hook_from_backward_statere      s!    '78Y''8888r
   
hooks_namemodule_namec                l    t          ||          }t          ||          }|D ]} |||g|R  }	|	|	}|S r   rc   )
_r   rd   rf   rg   r   modulehooksr   
new_results
             r   %call_module_hooks_from_backward_staterm      s[     X{++FHj))E    T&&04000
!FMr
   )r   r8   typingr   r   torch.utils._pytreeutils_pytreer&   r    r)   ModuleNotFoundErrorboolr   r   r   r.   r0   rL   r(   rP   rR   strre   rm   r	   r
   r   <module>ru      s               $ $ $ $ $ $ $ $ $   	BBB)d ) ) ) )	 	 		 	 	  $( ( ( ( ( ( ( (,  &EL & & & &       &9c 9 9 9 9	,/	>A	 	 	 	 	 	s   " ,,