
    Vh$                     <   U d dl mZ d dlmZmZmZ d dlmZ d dlZd dl	m
c mZ d dlmZ d dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZmZ  G d de      Zeej8                  j(                  ej8                  j:                  f   Z eej>                  j@                  jB                  jD                  ejF                  eejF                  i      Z$de%d<   dedefdZ&defdZ' G d de      Z( e(       Z)defdZ*de+fdZ,dee   fdZ-dej\                  fdZ/e)ja                  ejb                        dej\                  dej8                  j:                  de2edf   de3e4ef   de2ej\                  df   f
d       Z5e)ja                  e      dej\                  dej8                  j:                  de2edf   de3e4ef   de2ej\                  df   f
d       Z6e)ja                  e      dej\                  dej8                  j:                  de2edf   de3e4ef   de2ej\                  df   f
d        Z7e)jq                  ejr                         e)jq                  ejt                         dejv                  fd!Z<d"e+d#e3eej\                  f   dede2edf   de3e4ef   defd$Z=y)%    )Enum)AnyOptionalUnion)WeakKeyDictionaryN)DispatchKey)call_torchbind)FakeScriptObject)HigherOrderOperator)FakeTensorMode)disable_proxy_modes_tracingProxyTorchDispatchModetrack_tensor_treec                       e Zd ZdZy)_EffectTypeOrderedN)__name__
__module____qualname__ORDERED     O/home/dcms/DCMS/lib/python3.12/site-packages/torch/_higher_order_ops/effects.pyr   r      s    Gr   r   z&WeakKeyDictionary[OpType, _EffectType]SIDE_EFFECTSopeffectc           	         t        | t        j                  j                  t        j                  j                  f      rt        |       rJ | t        v r(t        |    |k7  rt        dt        |     d|  d| d      |t        | <   y )NzAlready registered effect type z to op z-, trying to register a different effect type .)
isinstancetorch_ops
OpOverloadr   has_aliasingr   RuntimeError)r   r   s     r   _register_effectful_opr%   #   s    
UZZ""EJJ$B$BC2  
\l2.&8-l2.>-?wrd K::@D
 	
 Lr   c                 @    | t         vrt        d|  d      t         | = y )NzOp z is not registered as effectful)r   r$   )r   s    r   _deregister_effectful_opr'   /   s'    	S$CDEERr   c            
       ^     e Zd ZdZd	 fdZdedeedf   dee	ef   deedf   f fdZ
 xZS )
WithEffectsa]  
    with_effects(token, op, args, kwargs) -> (new_token, op_results)

    This HOP helps ensure ordering between side effectful ops like prints or ops
    using torchbind objects. This is needed to ensure a traced graph from
    AOTAutograd is functional so that future optimization passes do not reorder
    these operators. This is done through threading "effect tokens" through the
    graph to enforce data dependence between side effectful ops.

    The tokens are basically dummy values (torch.tensor([])). We create a token
    per "effect type", which are enumerated in the _EffectType enum.
    returnc                 $    t         |   d       y )Nwith_effects)super__init__)self	__class__s    r   r.   zWithEffects.__init__D   s    (r   r   args.kwargsc                    t        |t        j                  j                  t        j                  j                  f      sJ t        |      rJ d       t        |||      sJ t        |t              sJ t        | $  ||g|i |S )Nz"Ops with aliasing is not supported)
r   r    r!   r   r"   r#   has_effectsdictr-   __call__)r/   tokenr   r1   r2   r0   s        r   r6   zWithEffects.__call__G   s     "uzz==uzz?T?TUVVV#I%II#2tV,,,&$'''wr;D;F;;r   )r*   N)r   r   r   __doc__r.   OpTypetupler   r5   strr6   __classcell__)r0   s   @r   r)   r)   6   sW    )< < S#X	<
 sCx.< 
sCx< <r   r)   c                     t        | t        j                  j                        r| t        vS | j
                  j                  D ]  }|j                   y | j
                  j                  D ]  }|j                   y y)NTF)	r   r    r!   r   r   _schema	arguments
alias_inforeturns)r   args     r   r#   r#   X   st    "ejj445%%zz## >>% zz!! >>% r   r*   c                 $   t         j                  j                  j                  j                  h}| |v ryt        | t         j                  j                  t         j                  j                  f      xr t        |        xr t        | ||      d uS )NF)r    opsprofiler_record_function_exit_RecordFunctionr   r!   r   r"   r#   get_effect_key)r   r1   r2   	_skip_opss       r   r4   r4   f   sz    ##99IIJI	Y 	2

66

8M8MNO 	9R  	92tV,D8r   c                     | t         v r	t         |    S |D ]K  }t        |t        j                  t        f      s$t
        j                  t         | <   t
        j                  c S  y N)r   r   r    ScriptObjectr
   r   r   )r   r1   r2   rB   s       r   rH   rH   s   s]    	\B 'cE..0@AB  +22L&&&' r   c                  ,    t        j                  g       S rK   )r    tensorr   r   r   new_token_tensorrO      s    <<r   r7   r1   .r2   c                 Z     ||i |}t               }t        |t              r|g|S ||fS rK   )rO   r   r:   )r7   r   r1   r2   out	new_tokens         r   with_effects_denserS      s>     d
f
C "I#u C  sr   c                 V    | 5  t        ||g|i |}|cd d d        S # 1 sw Y   y xY wrK   )rS   )moder7   r   r1   r2   results         r   with_effects_fakerW      s5     
 #E2???  s   (c                    t               5  t        ||g|i |}d d d        | j                  j                  |      }t	        j
                  | j                  j                  |      }t	        j
                  | j                  j                  |      }ddlm}	  |	|       | j                  j                  dt        ||g||      }
t        |
d | j                        }|S # 1 sw Y   xY w)Nr   )has_side_effectcall_functionconstanttracer)
r   r,   r]   unwrap_proxypytreetree_maptorch.fx.noderY   create_proxyr   )rU   r7   r   r1   r2   rQ   proxy_token
proxy_argsproxy_kwargsrY   	out_proxyrV   s               r   with_effects_proxyrg      s     
%	& 75"6t6v67 ++**51K!9!94@J??4;;#;#;VDL- B((	b&:&	I sIT[[QFM)7 7s   C##C,c                     t        | t        j                  j                        r| j                  S | t
        k(  rt        |d   |d         j                  S t        d|        )Nr      zUnable to get schema for op )	r   r    r!   r"   r>   r	   getattrschemar$   )r   r1   s     r   _get_schemarl      sV    "ejj++,zz	~	tAwQ(///9">??r   allow_token_discoverytokensc                 H   t        |||      }|J ||vr| sJ d| d|        t        j                  j                  t        j                  j                  j
                        }|`|j                  }ddlm}m	}	  |       5  t               }
ddd       |j                  j                  dddi d	      } |	
|d|
       |
||<   nt               ||<   ||   }ddlm}  |       }|j                  |g      d   }|j                  |      }|j                  |      }|j                         5  t!        ||g|i |^}}ddd       t#        ||      }t%        |j&                        dk(  r
d   J d}nQt%        |j&                        dk(  rt%              dk(  sJ |d   }n#t%              t%        |j&                        k(  sJ |j)                        }t+        |t        j,                        sJ |||<   |j)                  |      S # 1 sw Y   xY w# 1 sw Y   xY w)a,  
    Args:
        allow_token_discovery: Whether or not we are discovering tokens. If this
        is true, we will create a token for every side effect type seen that
        does not have a token assigned yet.  If this is false, the tokens
        should've all been created ahead of time, so we will error if there is
        no token mapping to every effect type.

        tokens: Map of effect type to tokens. This is to chain operators of the
        same effects together so that they do not get reordered in later
        optimization passes.
    Nz"Could not find a token for effect z which came from the function r   )r   r   placeholdertangents_tokenr   )namer[   )PythonFunctionalizeAPIri   )rH   r    _C_get_dispatch_mode_TorchDispatchModeKeyPROXYr]   "torch.fx.experimental.proxy_tensorr   r   rO   rb   #torch._subclasses.functional_tensorrs   unwrap_tensorsredispatch_to_nextr,   rl   lenrA   wrap_tensorsr   Tensor)rm   rn   r   r1   r2   keyproxy_tensor_moder]   r   r   token_tensortoken_proxyr7   rs   ctxunwrapped_tokenunwrapped_argsunwrapped_kwargsrR   unwrapped_outsrk   wrapped_tokens                         r   handle_effectsr      sQ   . T6
*C??
&!	X/u4RSURVW	X!!HH77HH**00
 ( '--F
 -. 2/12 ,22??/R>N @ K lK$vV&F3K*,F3K3KEJ
 
"C((%1!4O''-N))&1				! 
'3R(
"0(
4D(
$^

 ^,F
6>>aa (((	V^^		!>"a''''*>"c&..&9999 $$Y/MmU\\222F3KN++U2 2*
 
s   H.HHH!)>enumr   typingr   r   r   weakrefr   r    torch.utils._pytreeutils_pytreer_   torch._Cr   !torch._higher_order_ops.torchbindr	   "torch._library.fake_class_registryr
   
torch._opsr   torch._subclasses.fake_tensorr   rx   r   r   r   r   r!   r"   r9   rD   aten_printdefaultr   r   __annotations__r%   r'   r)   r,   r#   boolr4   rH   r~   rO   py_implCompositeExplicitAutogradr:   r5   r;   rS   rW   rg   fallthroughAutogradCPUAutogradCUDAFunctionSchemarl   r   r   r   r   <module>r      s,    ' ' %  $ $   < ? * 8 $  
uzz--uzz/D/DD	E :K		%%{':':++:6 	v 	{ 	 <% <> }V 
T 
(= %,,  k;;<
<<



 c?
 38n	

 5<<
 =
 n%	<<	 	

	 c?		
 38n	 5<<	 &	 ,-<< 	

 c?	
 38n 5<< .<   00 1   11 2@U11 @T,T,ell*+T, 	T, S/	T,
 cNT, 	T,r   