
    Vhd                        d Z ddlZddlZddlmZmZmZmZmZm	Z	 ddl
mZmZ ddlZddlmc mZ 	 ddlZ ed      Z ed      Zer ede      d	efd
       Znd	efdZdeeef   d	eeef   fdZdedeej6                     f   deded	ej6                  fdZdeeef   d	eeef   fdZ G d d      Zdej>                  j@                  jB                  de"ej6                     ded	e	ej6                  e#ej6                  df   f   fdZ$ded	e"e   fdZ%dej6                  d	e&fdZ' G d d      Z(deded e)ded	ef
d!Z*d"ed#ededed$e)d%e)d	efd&Z+y# e$ r dZY )w xY w)'a  
This module contains utility functions that are explicitly allowed to be called during
TorchDynamo compilation. These functions are carefully vetted to ensure they work
correctly within the TorchDynamo tracing and compilation process.

Key functionality groups:

- Compilation State:
  Functions for checking compilation state (is_compiling)

- Function Wrapping:
  Utilities for wrapping functions (wrap_inline, wrap_numpy) to work with
  TorchDynamo compilation

- Autograd Hooks:
  Functions and classes for handling autograd hooks and backward passes
  (call_hook, FakeBackwardCFunction, etc.)

- Tensor Operations:
  Utility functions for tensor operations and transformations
    N)AnyCallableOptionalTYPE_CHECKINGTypeVarUnion)
deprecated	ParamSpec_P_Rze`torch._dynamo.external_utils.is_compiling` is deprecated. Use `torch.compiler.is_compiling` instead.)categoryreturnc                  >    t         j                  j                         S Ntorchcompileris_compiling     L/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/external_utils.pyr   r   -   s    
 ~~**,,r   c                  >    t         j                  j                         S )zd
        Indicates whether we are tracing/compiling with torch.compile() or torch.export().
        r   r   r   r   r   r   6   s     ~~**,,r   fnc                      t        j                         dt        j                  dt        j                  dt
        f fd       }|S )zC
    Create an extra frame around fn that is not in skipfiles.
    argskwargsr   c                       | i |S r   r   )r   r   r   s     r   innerzwrap_inline.<locals>.innerD   s    4"6""r   )	functoolswrapsr   r   r   r   )r   r   s   ` r   wrap_inliner!   ?   sD    
 __R#RWW #		 #b # # Lr   hook.r   r   c                 \     | | }||d   S |j                  d      dk(  rt        d      |S )zB
    Used by compiled autograd to handle hook returning None.
    r   	hook_typepost_acc_grad_hookz5Tensor post accumulate grad hooks should return None.)getRuntimeError)r"   r   r   results       r   	call_hookr)   K   s>     4[F~Aw	K	 $8	8RSSMr   fc                      t         s S t        j                         dt        j                  dt        j
                  dt        j                  f 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.
    r   r   r   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   r1   z*wrap_numpy.<locals>.wrap.<locals>.<lambda>f   s    %//!:L r   )pytreetree_map_onlyr   Tensornpndarray)r   r   outr*   s      r   wrapzwrap_numpy.<locals>.wrap`   sR    ++LL-f~
f   ##BJJ0LcRRr   )r7   r   r    r   r   r   r4   PyTree)r*   r:   s   ` r   
wrap_numpyr<   Y   sU     __QSBGG Sryy SV]] S S Kr   c                       e Zd Zdej                  j
                  j                  deej                     ddfdZ	de
defdZy)FakeBackwardCFunctionrealsaved_tensorsr   Nc                      || _         || _        y r   )r?   r@   )selfr?   r@   s      r   __init__zFakeBackwardCFunction.__init__l   s    
 	*r   namec                     |dk(  r&t        j                  dt               | j                  S t	        | j
                  |      S )Nsaved_variablesz4'saved_variables' is deprecated; use 'saved_tensors')warningswarnDeprecationWarningr@   getattrr?   )rB   rD   s     r   __getattr__z!FakeBackwardCFunction.__getattr__t   s<    $$MMF" %%%tyy$''r   )__name__
__module____qualname__r   autogradfunctionBackwardCFunctionlistr6   rC   strr   rK   r   r   r   r>   r>   k   sO    +nn%%77+ ELL)+ 
	+( ( (r   r>   backward_c_functionr@   c                 ~    t        | |      } |j                  j                  |g| }t        |t              s|f}|S r   )r>   _forward_clsbackward
isinstancetuple)rT   r@   r   fakegradss        r   call_backwardr\      sD    
 !!4mDD&D&&t3d3EeU#Lr   r0   c                 b    t        | t              rt        |       S t        | t              r| S | gS r   )rX   rY   rR   r/   s    r   normalize_as_listr^      s+    !UAw	At	3Jr   c                 >    | j                         j                         S r   )untyped_storagesizer/   s    r   untyped_storage_sizerb      s    ##%%r   c                       e Zd Zedeeg df      deg df   ddfd       Zedeeg df      ddfd       Zedd       Zy)	FakeCompiledAutogradEnginefinal_callbacksNcbr   c                 &    | j                  |       y r   )append)re   rf   s     r   queue_callbackz)FakeCompiledAutogradEngine.queue_callback   s     	r"r   c                     d}|t        |       k  r | |   } |        |dz  }|t        |       k  r | j                          y )Nr      )lenclear)re   irf   s      r   exec_final_callbacksz/FakeCompiledAutogradEngine.exec_final_callbacks   sI    #o&& #BDFA #o&& 	r   c                       y r   r   r   r   r   _exec_final_callbacks_stubz5FakeCompiledAutogradEngine._exec_final_callbacks_stub   s    r   )r   N)	rL   rM   rN   staticmethodrR   r   ri   ro   rq   r   r   r   rd   rd      s    #hr4x01#7?D7I#	# #
  d8BH3E.F  4      r   rd   bw_state	hook_namec                 &     t        | |      |i |S r   rJ   )rs   rt   r   r   s       r   call_hook_from_backward_staterw      s     (78Y'888r   _r(   
hooks_namemodule_namec                b    t        ||      }t        ||      }|D ]  } |||g| }	|	|	} |S r   rv   )
rx   r(   rs   ry   rz   r   modulehooksr"   
new_results
             r   %call_module_hooks_from_backward_stater      sN     X{+FHj)E  &&040
!F  Mr   ),__doc__r   rG   typingr   r   r   r   r   r   typing_extensionsr	   r
   r   torch.utils._pytreeutils_pytreer4   r.   r7   ModuleNotFoundErrorr   r   FutureWarningboolr   r!   r6   r)   r<   r>   rO   rP   rQ   rR   rY   r\   r^   intrb   rd   rS   rw   r   r   r   r   <module>r      s  ,   I I 3  $ $ t_T] o-$ -	-
-$ -	HRV$ 	"b&)9 	
3..
/8;GJ
\\(2r6" xB'7 $( ((00BB%  5<<u||S0112	 c &ELL &S & *999*-99<99	
		 #	/2	@C	RU		a  	Bs   E EE