
    ɯwgc                         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
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y# e$ r dZ
Y Nw xY w)    N)Listreturnc                  >    t         j                  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     a/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_dynamo/external_utils.pyr   r      s     >>&&((r
   c                 B     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    4"6""r
   )	functoolswraps)r   r   s   ` r   wrap_inliner   !   s'    
 __R# # Lr
   c                 D     | | }||d   S |d   dk(  rt        d      |S )zA
    Used by compiled autograd to handle hook returning None
    r   	hook_typepost_acc_grad_hookz5Tensor post accumulate grad hooks should return None.)RuntimeError)hookr   r   results       r   	call_hookr   -   s:     4[F~Aw		 4	4RSSMr
   c                 R     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                 "    | j                         S r   )numpyxs    r   <lambda>z*wrap_numpy.<locals>.wrap.<locals>.<lambda>C   s    AGGI r
   c                 ,    t        j                  |       S r   )r   	as_tensorr!   s    r   r#   z*wrap_numpy.<locals>.wrap.<locals>.<lambda>F   s    %//!:L r
   )pytreetree_map_onlyr   Tensornpndarray)r   r   outfs      r   wrapzwrap_numpy.<locals>.wrap@   sR    ++LL-f~
f   ##BJJ0LcRRr
   )r)   r   r   )r,   r-   s   ` r   
wrap_numpyr.   9   s1     __QS S Kr
   c                   |    e Zd Zdej                  j
                  j                  deej                     ddfdZ	d Z
y)FakeBackwardCFunctionrealsaved_tensorsr   Nc                      || _         || _        y r   )r1   r2   )selfr1   r2   s      r   __init__zFakeBackwardCFunction.__init__L   s    
 	*r
   c                     |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   s>    $$MMF" %%% tyy$''r
   )__name__
__module____qualname__r   autogradfunctionBackwardCFunctionr   r(   r5   r=   r	   r
   r   r0   r0   K   sA    +nn%%77+ ELL)+ 
	+	(r
   r0   c                     t        | |      } |j                  j                  |g| }t        |      t        ur|f}|S r   )r0   _forward_clsbackwardtypetuple)backward_c_functionr2   r   fakegradss        r   call_backwardrL   a   sF     !4mDD&D&&t3d3E E{%Lr
   r"   c                 >    | j                         j                         S r   )untyped_storagesizer!   s    r   untyped_storage_sizerP   l   s    ##%%r
   c                   <    e Zd Zed        Zed        Zed        Zy)FakeCompiledAutogradEnginec                 &    | j                  |       y r   )append)final_callbackscbs     r   queue_callbackz)FakeCompiledAutogradEngine.queue_callbackq   s    r"r
   c                     d}|t        |       k  r | |   } |        |dz  }|t        |       k  r | j                          y )Nr      )lenclear)rU   irV   s      r   exec_final_callbacksz/FakeCompiledAutogradEngine.exec_final_callbacksu   sI    #o&& #BDFA #o&& 	r
   c                       y 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   s<    # #      r
   rR   	hook_namec                 &     t        | |      |i |S r   r;   )bw_statera   r   r   s       r   call_hook_from_backward_statere      s    '78Y'888r
   
hooks_namemodule_namec                b    t        ||      }t        ||      }|D ]  } |||g| }	|	|	} |S r   rc   )
_r   rd   rf   rg   r   modulehooksr   
new_results
             r   %call_module_hooks_from_backward_staterm      sN     X{+FHj)E  &&04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        $ $
)d )		$( (,&ELL & &9c 9	,/	>A	s  	Bs   A& &A0/A0