
    IЦiB                     f   S SK r S SKrS SKrS SKJrJrJrJrJrJ	r	J
r
  S SKrS SKJrJrJr  S SKJrJr  S SKJrJrJr  S SKJrJr  S SKJr  S SKJr  S S	KJr  S S
K J!r!  S SK"J#r#J$r$J%r%J&r&J'r'J(r(J)r)  S SK*J+r+J,r,  S SK-J.r.J/r/  S SK0J1r1  S SK2J3r3  \	(       a  S SK4J5r5  \" \6S5      r7\" \6S5      r8S r9S r:S r;/ SQr<\1" \\\Rz                  \R|                  R~                  R                  R                  /5      rB " S S5      rCSqDSqESqF\ R                  SS j5       rH\ R                  S 5       rISS jrJg)    N)AnyDictListOptionalTupleTYPE_CHECKINGUnion)call_backward	call_hookFakeCompiledAutogradEngineGetItemSourceLocalSource)counterslazy_format_graph_codeset_locals_to_steal)getArtifactLoggertrace_structuredclone_preserve_strides)FakeTensorMode)GraphModule)BackwardState)	decomposedisable_autocast_cachedisable_proxy_modes_tracingfetch_object_proxyProxyTorchDispatchModePythonKeyTracertrack_tensor_tree)
DimDynamicShapeEnv)preserve_node_metaset_stack_trace)
OrderedSet)CapturedTraceback)Proxycompiled_autogradcompiled_autograd_verbosec                  h    [         R                  R                  R                  R	                  S5      $ )Nr)   )torch_logging	_internal	log_stateis_artifact_enabled     ^/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabledr3   +   s(    >>##--AA# r1   c                  ^    [         R                  R                  R                  R                  $ N)r+   	_inductorconfigtriton
cudagraphsr0   r1   r2   snapshot_cudagraph_enabledr:   1   s    ??!!((333r1   c                 "    U b  [        U 5      $ U $ r5   r   )xs    r2   maybe_cloner=   5   s    }%a((Hr1   )inputssizesscalarshooksc                      \ rS rSrS*S jrS r\S\4S j5       rS\	\
R                     S\	\   S	\	\\\4      S
\	\	\\\4         4S jrS\4S jrS rS\4S jrS rS rS rS\	\   4S jrS rS rS rS r\S 5       r\S 5       rS rS r S r!S r"S r#S  r$S! r% S+S
\&\	\\\4         4S" jjr'S#\4S$ jr(S%\S&\S'\&\
RR                  RT                     4S( jr+S)r,g),AutogradCompilerInstanceF   returnNc                 .   Xl         [        R                  " 5       U l        U R                  R                  U l        [        5       U l        [        SSU R                  S9U l        [        5       U l
        [        U R                  S5      U l        S U l        g )NT)allow_fallback_kernelsallow_non_fake_inputs	shape_envsymbolic)compiler_fn
contextlib	ExitStackstackcloser"   rI   r   fake_tensor_moder   	fx_tracerr   
proxy_modehooks_proxy)selfrK   s     r2   __init__!AutogradCompilerInstance.__init__G   sr    &))+
ZZ%%
! .#'"&nn!

 )*0L,0r1   c                 v    [        U[        R                  5      (       d   eU R                  R	                  XS9$ )N)source)
isinstancer+   TensorrP   from_tensor)rT   r<   rX   s      r2   	wrap_fake"AutogradCompilerInstance.wrap_fakeU   s3    !U\\****$$000BBr1   c                 ,    [        [        U 5      U5      $ r5   r   )nameidxs     r2   rX   AutogradCompilerInstance.sourceY   s    [.44r1   r>   r?   r@   originsc                 (  ^  [         S   S==   S-  ss'   S T l        0 T l        [        R                  R                  5       T R                  l        [        R                  R                  [        S9T R                  l        0 T R                  l        U 4S j[         5       u  pVnT l        T R                  R!                  [#        5       5        Uu  pn
[%        U5       VVs/ s H'  u  pT R'                  UT R)                  SU5      5      PM)     nnnT R+                  XU5        [%        U5       VVs/ s H@  u  pT R,                  R/                  UT R)                  SU5      [0        R2                  5      PMB     nnnT R+                  X&U	5        [%        U5       H  u  pT R)                  SU5      n[5        U[6        5      (       a/  T R,                  R/                  UU[0        R2                  5      X;'   M[  [5        U[8        5      (       aF  T R,                  R;                  T R,                  R=                  UU[0        R2                  S	9UUS
9X;'   M  [?        S[A        U5      5      e   T R+                  X7U
5        T R                  R!                  [C        0 5      5        T R                  R!                  T RD                  5        T R                  R!                  T RF                  5        T R                  R!                  [I        5       5        T RD                  R,                  c   eT RD                  R,                  nT R                  R!                  [        R                  RJ                  RL                  RO                  U5      5        XU4$ s  snnf s  snnf )Nr(   captures   )
tracer_clsc              3   `   >#    U  H#  nTR                   R                  S US0 5      v   M%     g7f)placeholderr0   N)rQ   create_proxy).0r_   rT   s     r2   	<genexpr>9AutogradCompilerInstance.begin_capture.<locals>.<genexpr>j   s1      D
+ NN''tRDD+s   +.r>   r?   r@   )rX   dynamic_dim)hintrX   zUnexpected scalar type: )(r   aot_graph_cls_nameaot_graph_infosr+   nnModulerQ   rootfxGraphr   graphtensor_attrs_graph_placeholdersrS   rN   enter_contextr#   	enumerater\   rX   bind_tensors_to_proxiesrI   $create_unspecified_symint_and_symbolr!   DYNAMICrY   intfloatcreate_symfloatnodecreate_unspecified_symbolAssertionErrortyper   rP   rR   r   experimentalsymbolic_shapes_suppress_guards)rT   r>   r?   r@   rb   
args_proxysizes_proxyscalars_proxyinputs_originssizes_originsscalars_originsr`   r<   valrX   envs   `               r2   begin_capture&AutogradCompilerInstance.begin_capture]   s    	$%j1Q6115:<#hhoo/$xx~~~I&(#D
+D
@
0@
 	

  !3!569@6 $F+
+ NN1dkk(C89+ 	 
 	$$VH &e,
 - NN??GS)""
 - 	 
 	$$UG!'*HC[[C0F#s###~~RR&& 
 C''#~~AANN<<%$.$6$6 = 
 !  B   %%?cKK' +( 	$$W_M 	

  2/

  !6!67

  1

  !7!9:$$..:::##--

  HH!!11BB3G	
 g%%e

s   !.N2ANbackward_idxc                    U R                   c   eU R                   U   nU R                  R                  S[        UU R	                  U5      /U R	                  U5      Q70 S9n[        5          / nU HB  nUc  UR                  S 5        M  Uu  ppUR                  [        R                  " XXS95        MD     U R                  Xv5        S S S 5        [        W5      $ ! , (       d  f       N= f)Ncall_function)kindtargetargskwargs)sizedtypelayoutdevice)rS   rQ   ri   r
   to_proxyr   appendr+   emptyr{   tuple)rT   r>   output_metadatassaved_tensorsr   backward_c_functionproxiesgrad_insoutput_metadatar   r   r   r   s                r2   proxy_call_backward,AutogradCompilerInstance.proxy_call_backward   s     +++"..|<..--  #m, v&
  . 	
 )*57H#3"*OOD).=+KKTvU $4 ((; + X +*s   ,AC
C)c                     U R                   R                  S[        U/U Vs/ s H  o@R                  U5      PM     snQ7U5      $ s  snf Nr   )rQ   ri   r   r   )rT   hookr   r   r<   s        r2   proxy_call_hook(AutogradCompilerInstance.proxy_call_hook   sO    ~~**,01Dq--"D1 
 	

 2s   Aic                     U R                   c   eU R                   U   nU R                  UX   SS9n[        5          [        X   5      X'   U R	                  X   /U/5        S S S 5        U$ ! , (       d  f       U$ = f)Ntensor_pre_hook	hook_typerS   r   r   r=   r{   )rT   r>   hook_idr   r   proxys         r2   r   (AutogradCompilerInstance.tensor_pre_hook   s    +++($$I' % 

 )*#FI.FI((&)ug> +  +* s   &A,,
A;c                    U R                   c   eU R                   U   nU R                  UUSS9n[        5          U Vs/ s H  n[        U5      PM     nnU R	                  X5        S S S 5        U$ s  snf ! , (       d  f       U$ = f)Npre_hookr   r   )rT   r>   r   r   r   r<   s         r2   r   !AutogradCompilerInstance.pre_hook   s    +++(&&  ' 

 )*.45fk!nfF5((9 +  6 +* s   A6A1A61A66
Bc                    U R                   c   eU R                   U   nU R                  UUUSS9n[        5          U Vs/ s H  n[        U5      PM     nnU R	                  X5        S S S 5        U$ s  snf ! , (       d  f       U$ = f)N	post_hookr   r   )rT   outputsr>   r   r   r   r<   s          r2   r   "AutogradCompilerInstance.post_hook   s    +++(&&!	 ' 
 )*/67w!{1~wG7((: +  8 +* s   A7 A2A72A77
Bc                 ,   [        U[        R                  5      (       d   eU R                  c   eU R                  U   nU R	                  UUSS9n[        5          [        U5      /nU R                  X/5        S S S 5        U$ ! , (       d  f       U$ = f)Npost_acc_grad_hookr   )rY   r+   rZ   rS   r   r   r=   r{   )rT   inputr   r   r   s        r2   r   +AutogradCompilerInstance.post_acc_grad_hook   s    %....+++($$* % 

 )* '(E((8 +  +* s   B
Bc                    0 nSn[        UR                  5      nUS   R                  S:X  d   eUS   n[        UR                  R	                  5       5      n[        [        5      nXG   US   :X  d   eU[        U5      -   S-
  nXH   US   :X  d   e[        U5       H  u  pU(       d+  U
R                  S   R                  R                  S:X  a  SnM7  U
R                  S   R                  R                  S	:H  n[        U
R                  S   R                  5       5      S:H  nU(       d  M  U(       d  M  [        U
R                  R	                  5       5      n[        S
 U 5       5      (       d  M  XU	'   M     U(       aZ  UR                  5        H-  n
U
R                  S   R                  5       U
R                  S'   M/     [        UR	                  5       5      $ / $ )NFr   r>   re   r   cudaTcpuc              3      #    U  HS  n[        UR                  [        R                  R                  5      =(       a    UR                  R
                  S ;   v   MU     g7f))primsatenN)rY   r   r+   _ops
OpOverload	namespacerj   users     r2   rk   DAutogradCompilerInstance.move_graph_nodes_to_cuda.<locals>.<genexpr>$  sM       !+ t{{EJJ,A,AB C--1BBC *s   AA)listnodesr   userskeyslenrx   rz   metar   r   r   allvaluesr   )rT   rv   to_movehas_cuda_inputsr   r>   inputs_usersfirst_getitem_idxlast_getitem_idxr   nodeis_cpu	is_scalar
node_userss                 r2   move_graph_nodes_to_cuda1AutogradCompilerInstance.move_graph_nodes_to_cuda  s   ,.U[[!Qx(***qFLL--/0 34'<?:::,s</@@1D&,r*:::: .GA"tyy'7'>'>'C'Cv'M"&YYu%,,11U:FDIIe,11349Iv))!$**//"34
  !+   "&AJ /& (#'99U#3#8#8#:		%  ) ''	r1   c                 V   [        U[        R                  R                  5      =(       a    UR                  S:H  =(       ai    UR
                  [        R                  R                  R                  R                  [        R                  R                  R                  R                  4;   $ r   )rY   r+   rt   Nodeopr   opsr   sym_sizer~   	sym_numeldefault)rT   r   s     r2   is_sym_node$AutogradCompilerInstance.is_sym_node7  sn    tUXX]]+ O?*O		''++UYY^^-E-E-M-MNO	
r1   c                 \  ^ [        5       m[        U R                  R                  R	                  SS95       H.  u  pTR                  UR                  R                  5       5        M0     W[        [        5      S-
  :X  d   eU4S jnU R                  R                  R                  U5        g )Nrh   )r   re   c                    > U T;   =(       dU    U R                   S:H  =(       d?    U R                   S:H  =(       d)    U R                   S:H  =(       a    U R                  [        ;   $ )Nrh   outputr   )r   r   _impure_targets)r   unpack_nodess    r2   	is_impure/AutogradCompilerInstance.dce.<locals>.is_impureI  sV    $ S77m+S77h&S GG.Q4;;/3Q	r1   )r%   rz   rQ   rv   
find_nodesupdater   r   r   rx   eliminate_dead_code)rT   r   r   r   r   s       @r2   dceAutogradCompilerInstance.dce?  s    
 3=, !5!5!@!@M!@!RSGA

 12 TC+,q0000	 	00;r1   c           	        ^^ U R                   R                  S[        R                  S0 5        U R                  R                  5         U R                   R                  SSU R                   R                  U R                  U5      5      40 5        U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R!                  5         / m[#        5       (       a%  U R%                  U R                   R&                  5      m[)        U R                   R*                  U R                   R&                  S5      m[-        TS/5        [/        STSSSS9n[0        R3                  S	U5        [4        R7                  S	U5        [9        S
U4S jS9  U4S jnX0R;                  T5      4$ )Nr   r0   r   CompiledAutogradr>   zCompiled autograd graphT)include_deviceinclude_stridecoloredz%scompiled_autograd_graphc                  "   > T R                  SS9$ )NF)print_output)print_readable)rv   s   r2   <lambda>6AutogradCompilerInstance.end_capture.<locals>.<lambda>  s    u333Gr1   )
payload_fnc                    >  Sq T H$  nX   R                  5       R                  SS9X'   M&     [        5          U " XX45      sS S S 5        Sq $ ! , (       d  f       O= f Sq g ! Sq f = f)NT)non_blockingF)in_compiled_autograd_region
pin_memoryr   _disable)compiled_fnr>   r?   r@   rA   r   runtime_inputs_to_moves         r2   runtime_wrapper=AutogradCompilerInstance.end_capture.<locals>.runtime_wrapper  sq    4.2+/A &	 4 4 6 ; ; ; NFI 0 Z&vgE  Z /4+  ZZ /4+e+s"   6A# 	A	A# 
AA# #A')rQ   ri   r   _exec_final_callbacks_stubrN   rO   create_node
create_argr   rename_aot_dispatcher_nodesreorder_tensor_pre_hook_nodes'reorder_pre_hook_nodes_to_schedule_asapreorder_accumulate_grad_nodes%reorder_pre_hook_nodes_to_mimic_eager reorder_post_acc_grad_hook_nodesreorder_post_hook_nodesr   r:   r   rv   r   rs   r   r   compiled_autograd_loginfoverbose_logdebugr   rK   )rT   r   lazy_graph_coder	  rv   r  s       @@r2   end_capture$AutogradCompilerInstance.end_captureS  s   ##&AA		
 	

""^^&&t}}W'=>@		
 	((***,446**,224--/$$& 	
,.%''%)%B%B4>>CWCW%X"NN!5!57I
 	EH:.0%
 	""49$0%G	


	4  0 0 777r1   c           	      r   U R                   c  gS[        R                  R                  R                  S[        R                  R                  R                  4S jnU R
                  R                  5        GH  u  p#US   nUS   nUS   R                  n[        UR                  5      n[        U5      nUc   e UR                  S:w  a  [        U5      nUR                  S:w  a  M   [        U R                  R                  R                  5      n	[        U5       H  n
[        U	5        M     [        U	5      nUR                  S	:w  a7  U" X5      (       d*  [        U	5      nUR                  S	:w  a  U" X5      (       d  M*  UR                  S	:w  a  UR                  S	:w  a  UR                  (       d  [        U	5      nM>  U" X5      (       d  [        eSU S
UR                    3Ul        [#        UR$                  5       H)  u  pSU S
UR                    3UR$                  U   l        M+     [        U5      n[        U	5      nUR                  S	:w  a  UR                  S	:w  a  M  GM  GM  GM  GM     g! [         a     GM
  f = f! [         a(    [&        R)                  SU R                   UUU5         GM?  f = f)z
Renames nodes as they appear in the AOTDispatcher backward graphs, prefixed by AOT id
e.g. AOTDispatcher backward graph X's `sin_Y` -> `aotX_sin_Y`
Ncaaotc                 r   U R                   UR                   :H  nU(       dg  [        U R                   S5      =(       aJ    [        UR                   S5      =(       a-    U R                   R                  UR                   R                  :H  nU(       d  [        U R                   S5      (       a  [        UR                   S5      (       a  UR                   R                  5       S:X  ab  [        UR                  R                  S5      S5      (       a8  U R                   R                  5       UR                  S   R                  5       :H  nU=(       ak    U R                  UR                  :H  =(       aK    U R                  UR                  :H  =(       a+    [        U R                  5      [        UR                  5      :H  $ )N__name__r_   zaten::reshapeoriginal_aten)
r   hasattrr   r_   r   getr   r   r   all_input_nodes)r  r  target_matchs      r2   
is_similarHAutogradCompilerInstance.rename_aot_dispatcher_nodes.<locals>.is_similar  s:   99

2L BIIz2 B

J7B		**cjj.A.AA  !BIIv..CJJ//JJOO%8CHHLL96BB  "yy~~/388O3L3Q3Q3SS  HEESVVOHGGsxx'H **+s33F3F/GG	r1   ca_node_start_idxaot_idaot_gmr   r   _zIFailed to match %s%s (NodeCall %s) nodes with AOT backward graph %s nodes)ro   r+   rt   r   r   rp   itemsrv   iterr   nextr   StopIterationrQ   ranger   r_   rz   r$  r  r  )rT   r&  nodecall_indexr  r(  r)  	aot_graphaot_itaot_nodeca_itr+  ca_noder   inps                 r2   r  4AutogradCompilerInstance.rename_aot_dispatcher_nodes  se   
 ""*	588==-- 	EHHMM4F4F 	6 %)$8$8$>$>$@ N $%8 9(^FX,,I )//*FF|H'''kk_4#F|H kk_4
&T^^1177801AK 2u+ jjH,Z5R5R #5kG jjH,Z5R5R kkX-'**2H"=="&u+ %g88 ,+%((--#AGL"+H,D,D"E<?xq
:S//27 #F  $F|H"5kG! kkX-'**2H2H-2H-; %A ! D ! !!_++" s,   ;+I2)BJC J2
J J-J65J6c                     U  Vs/ s H-  n[        U5      [        R                  R                  L d  M+  UPM/     nnU$ s  snf r5   )r   r+   rt   r   )r   nr   s      r2   get_all_nodes&AutogradCompilerInstance.get_all_nodes  s5     !=DqDGuxx}}$<D= >s   *==c                     U R                   S:X  dK  U R                   S:X  a<  U R                  [        R                  :X  a  U R                  S   R                   S:X  a  gg)Nrh   r   r   TF)r   r   operatorgetitemr   )r   s    r2   is_placeholder'AutogradCompilerInstance.is_placeholder  sF    77m#GG&x///		!=0r1   c                    U R                   R                  R                  S[        R                  R
                  R                  R                  S9 H  nUR                  S   UR                  S   p2SnUR                  [        R                  :X  a  UnUR                  S   n[        X#/5      nXQR                  Ld  Mm  U R                  U5      (       a  M  UR                  U5        Uc  M  UR                  U5        M     g)z
Usage of AOTAutograd causes all the accumulate_grad_ nodes to get pushed to the end of
the graph.  This differs from eager mode, which schedules them as soon as possible. This
pass attempts to reorder the graph to mimic eager behavior.
r   r   r   r   re   N)rQ   rv   r   r+   r   inductoraccumulate_grad_r   r   r   r>  r?  maxprevr@  r   )rT   r   
param_node	grad_nodegetitem_nodeargs         r2   r  6AutogradCompilerInstance.reorder_accumulate_grad_nodes  s     NN((33uyy'9'9'J'J'R'R 4 
D %)IIaL$))A,	L8#3#33((--a0	z-.C))#D,?,?,D,D

4 +JJ|,
r1   c                 v   U R                   R                  R                  S[        S9 H  nUR                  R                  SS5      S:w  a  M%  UR                  S   nUR                  S   nX1R                  Ld  MS  U R                  U5      (       a  Mk  UR                  U5        UR                  U5        M     g)z
Usage of AOTAutograd causes all the tensor_pre_hook nodes to get pushed
to the end of the graph. This differs from eager mode, which schedules
them as soon as possible. This pass attempts to reorder the graph to
mimic eager behavior.
r   rC  r   Nr   r   re   )
rQ   rv   r   r   r   r#  r   rG  r@  r   )rT   r   rJ  
input_nodes       r2   r  6AutogradCompilerInstance.reorder_tensor_pre_hook_nodes  s     NN((33y 4 
D {{{D15FF99Q<L1J*43F3Fz3R3R!!,/##D)
r1   c                 4   U R                   R                  R                  S[        S9 GHk  nUR                  R                  SS5      S:w  a  M&  UR                  S   nU R                  UR                  S   5      n/ n/ nU/nU Hu  nUR                  S:X  d  M  UR                  [        R                  :X  d  M5  UR                  UR                  S   5        UR                  U5        UR                  U5        Mw     [        XE5       H'  u  pUR                  U5        UR                  U	5        M)     [        U5      n
XR                   Ld  GM'  U R#                  U
5      (       a  GM@  U
R                  U5        U H  nUR                  U5        M     GMn     g)aR  
In this function, we schedule the pre hooks as soon as possible. This
does not match eager behavior (schedule pre hook right before its
registered node), but it can make acc grad be scheduled properly when
the pre hooks are registered to them. After reordering acc grad node, we
will reorder the pre hooks again to mimic eager behavior.
r   rC  r   Nr   r   re   )rQ   rv   r   r   r   r#  r   r;  r   r   r>  r?  r   zipremoverF  rG  r@  )rT   r   rJ  input_nodes	to_remove	to_append
hook_blockr:  abrK  s              r2   r  @AutogradCompilerInstance.reorder_pre_hook_nodes_to_schedule_asap*  sQ    NN((33y 4 
D {{{D1Z?99Q<L,,TYYq\:KIIJ 44?*qxx8;K;K/K$$QVVAY/$$Q'%%a(	 !
 I1""1%""1% 2 k"C))#D,?,?,D,D

<(#A ''* $3
r1   c                    / nU R                   R                  R                  S[        S9 H6  nUR                  R                  SS5      S:w  a  M%  UR                  U5        M8     [        U5       H  nUR                  S   n[        UR                  R                  5       5      n[        U5      S:X  a  MF  [        S U 5       5      (       d   e[        [        US   R                  R                  5       5      5      nXRR                  Ld  M  UR!                  U5        UR!                  U5        U H  nUR!                  U5        M     M     g)z
Usage of AOTAutograd causes all the pre_hook nodes to get pushed to the
end of the graph. This differs from eager mode, which schedules them
right before their registered node execution. This pass attempts to
reorder the graph to mimic eager behavior.
r   rC  r   Nr   r   c              3      #    U  H8  nUR                   S :H  =(       a    UR                  [        R                  :H  v   M:     g7f)r   N)r   r   r>  r?  r   s     r2   rk   QAutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager.<locals>.<genexpr>e  s7      !D ?*Nt{{h>N>N/NN!s   A A)rQ   rv   r   r   r   r#  r   reversedr   r   r   r   r   r   r.  r-  prepend)rT   	pre_hooksr   hook_getitem_noder   registered_noder?  s          r2   r  >AutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eagerN  s%    	NN((33y 4 
D {{{D1Z?T"
 Y'D $		!*+E5zQ  !     #4a(;(;(=#>?Oii/''(9:''-$G#++G4  %# (r1   c                    / nU R                   R                  R                  S[        S9 H6  nUR                  R                  SS5      S:w  a  M%  UR                  U5        M8     [        U5       H  nUR                  S   nUR                  S   nSn[        UR                  R                  5       5       HU  nUR                  S:X  d  M  UR                  [        R                  R                   R"                  R$                  :X  d  MS  Un  O   Uc   S5       eUR                  U5        UR                  U5        M     g)	z
Usage of AOTAutograd causes all the post_acc_grad_hook nodes to get
pushed to the end of the graph. This differs from eager mode, which
schedules them as soon as possible. This pass attempts to reorder the
graph to mimic eager behavior.
r   rC  r   Nr   r   re   z8post_acc_grad_hook must have corresponding acc grad node)rQ   rv   r   r   r   r#  r   r]  r   r   r   r   r   r   r+   r   rD  rE  r   )rT   post_acc_grad_hooksr   rJ  rH  acc_grad_noder:  s          r2   r  9AutogradCompilerInstance.reorder_post_acc_grad_hook_nodesq  s    !NN((33y 4 
D {{{D15II&&t,
 01D99Q<L1J !M***//12DDO+EII$6$6$G$G$O$OO$%M 3 )JIJ)   .%) 2r1   c           	        ^ / nU R                   R                  R                  S[        S9 H6  mTR                  R                  SS5      S:w  a  M%  UR                  T5        M8     [        U5       GH  mTR                  S   nTR                  S   nTR                  S   n[        U5      S:  a  MB  / nUR                  [        U5      5        U H?  nUR                  U4S	 j[        UR                  R                  5       5       5       5        MA     [        U5      nUR                  S:X  a  UR                   ["        R$                  R&                  R(                  R*                  :X  a  UR                  S   nSn	[        UR                  R                  5       5       HO  n
U
R                  S:X  d  M  U
R                   [        :X  d  M+  U
R                  R                  SS5      S
:X  d  MM  U
n	MQ     U	b%  U	R                  U5        UR                  T5        GM  UTR,                  Ld  GM  U R/                  U5      (       a  GM  UR                  U5        UR                  T5        GM     g)z
Usage of AOTAutograd causes all the post_hook nodes to get pushed to the
end of the graph. This differs from eager mode, which schedules them as
soon as possible. This pass attempts to reorder the graph to mimic eager
behavior.
r   rC  r   Nr   r   re      c              3      >#    U  HM  nUR                   S :X  a6  UR                  [        :X  a"  TR                  R	                  SS5      S:X  a  MI  Uv   MO     g7f)r   r   Nr   )r   r   r   r   r#  )rj   r   r   s     r2   rk   CAutogradCompilerInstance.reorder_post_hook_nodes.<locals>.<genexpr>  sI      - =?2 KK94 KKOOK>+M D =s   AA	Ar   )rQ   rv   r   r   r   r#  r   r]  r   r   extendr   r   r   rF  r   r   r+   r   rD  rE  r   rG  r@  )rT   
post_hooksrJ  output_nodesrS  input_nodes_and_usersrN  rK  rH  post_acc_grad_hook_noder:  r   s              @r2   r  0AutogradCompilerInstance.reorder_post_hook_nodes  s    
NN((33y 4 
D {{{D1[@d#
 Z(D99Q<L99Q<L))A,K< 1$$&!!((k):;)
%,, - $Z%5%5%:%:%< =-  * +,C/)JJ%))"4"4"E"E"M"MM XXa[
*.'j..3356A/HH	1HHLLd;?SS23/ 7 +6+22<@ ''-$))#D,?,?,D,D

<(##D)W )r1   c                   ^  Uc  g [        U[        5      (       a"  U Vs/ s H  nT R                  U5      PM     sn$ [        U[        5      (       a  [        U 4S jU 5       5      $ [        U[        R
                  5      (       d   e[        T R                  U5      n[        U[        R                  R                  R                  R                  5      (       d   eUR                  $ s  snf )Nc              3   F   >#    U  H  nTR                  U5      v   M     g 7fr5   )r   )rj   r<   rT   s     r2   rk   4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>  s     51aq))1s   !)rY   r   r   r   r+   rZ   r   rQ   rt   r   proxy_tensor_ProxyTensorr   )rT   tr<   rt  s   `   r2   r   !AutogradCompilerInstance.to_proxy  s    9a./0aDMM!$a00a51555!U\\****)$..!<,(=(=(J(J(W(WXXXX!!! 1s   C)c                    [        U[        R                  R                  5      (       a  U(       ae  [	        U5      [	        U5      :X  d   e/ n[        [	        U5      5       H.  nX5   u  pgU R                  XvS 5        UR                  X%   5        M0     UnO&[        [	        U5      5       Vs/ s H  oRU   PM	     nn[	        U5      [	        U5      :X  d   e[        XS U R                  S9  g s  snf N)constanttracer)
rY   r+   rt   r'   r   r0  set_node_originr   r    rQ   )rT   tensorsr   rb   bound_proxiesr   r1  	node_names           r2   r{   0AutogradCompilerInstance.bind_tensors_to_proxies  s     guxx~~..7|s7|333 "s7|,A07
-N((DI!((4 - (/4S\/BC/B!1:/BC7|s7|+++'T$..Q Ds   ,C,indexc                     U R                   c   eU R                   U   n[        5       n[        X2S U R                  S9  U$ ry  )rS   r   r    rQ   )rT   r  r   bw_states       r2   bind_backward_state,AutogradCompilerInstance.bind_backward_state  s@    +++  ' ?(DPr1   r  r1  pyobjc                    SnUb}  UR                   n[        US5      (       a`  Xl        UR                  n[	        U R
                  R                  R                  5      UUR                  R                  S.U R                  U'   U U SU S3n[        R                  " 5       R                  5       S   nUR                  SU5      n[        U5        g )N _aot_id)r(  r)  r*  z (NodeCall )r   z:raw_stack_trace = CapturedTraceback.extract().format()[-1])_forward_clsr"  ro   r  r   rQ   rv   r   _lazy_backward_info	bw_modulerp   r&   extractformatreplacer$   )	rT   r  r1  r  maybe_aot_idforward_clsnew_coderaw_stack_tracenew_stack_traces	            r2   r|  (AutogradCompilerInstance.set_node_origin  s     ,,K{I..*3'*22),T^^-A-A-G-G)H*)==GG8$$^4  [k.9IK+335<<>rB)11H(
 	(r1   )
ro   rp   rO   rK   rP   rQ   rS   rR   rI   rN   rE   Nr5   )-r   
__module____qualname____firstlineno__rU   r\   staticmethodr   rX   r   r+   rZ   r~   r	   r   r   strr   r   r   r   r   r   r   r   r   r   r  r  r;  r@  r  r  r  r  r  r  r   r   r{   r  autogradFunctionr|  __static_attributes__r0   r1   r2   rC   rC   F   s   1C 5] 5 5G&U\\"G& CyG& eCJ'(	G&
 d5c?+,G&R!
 !F	
# &&c &P
<(B8HXt  
  -**("+H!5F%&N:*x" LPR)1$uS#X2G)HR$ )) ) //0	)r1   rC   Fc              #   *  #    U(       a  [        U5      [        L d   eSSKJn  UR                  R
                  S:X  a
  Sq S v   Sqg SS KnUR                  R                  R                  R                  [        R                  " [        U 5      U5      u  nn[        5       (       a3  UR                  R                  R                  R!                  ["        5        Sq UR&                  R)                  S5         S v   S S S 5        U(       d  SqUR                  R                  R                  R                  XE5        g ! Sqf = f! , (       d  f       NN= f! U(       d  SqUR                  R                  R                  R                  XE5        f = f7f)Nr   )
eval_frameforce_eagerTF)r   booltorch._dynamor  _stancestance%compiled_autograd_enabled_force_eagertorch._inductor.cudagraph_trees_C_dynamor(   set_autograd_compiler	functoolspartialrC   r3   set_verbose_loggerr  compiled_autograd_enabledr  set_multithreading_enabled)rK   dynamicr  r+   prior_compilerprior_dynamics         r2   _enabler    sE    G}$$$(  M1 15-	:491 	/
 HH..DD6Dg
	
 ,--HH..AA+N$(!	::5A B ",1)HH..DD- 5:1" BA ",1)HH..DDsN   >FD> BFE 8E=E 9F>EF
EE :FFc               #     #    [         R                  R                  R                  R	                  S S5      u  n nSq S v   U (       a  Sq[         R                  R                  R                  R	                  X5        g ! U (       a  Sq[         R                  R                  R                  R	                  X5        f = f7f)NFT)r+   r  r  r(   r  r  )r  r  s     r2   r  r  I  s     
 	**@@uM !&
(,%**@@	
 (,%**@@	
s   :B?A> =B?>>B<<B?c                      Sq [        (       a   e[        R                  R                  R
                  R                  S S5        [        R                  R                  R
                  R                  S 5        g )NF)r  r  r+   r  r  r(   r  r  r0   r1   r2   resetr  \  sR     %****	HH&&<<T5I	HH&&99$?r1   )Fr  )KrL   r  r>  typingr   r   r   r   r   r   r	   r+   torch._dynamo.external_utilsr
   r   r   torch._dynamo.sourcer   r   torch._dynamo.utilsr   r   r   torch._loggingr   r   torch._prims_commonr   torch._subclassesr   torch.fxr   %torch.fx.experimental._backward_stater   "torch.fx.experimental.proxy_tensorr   r   r   r   r   r   r    %torch.fx.experimental.symbolic_shapesr!   r"   torch.fx.tracebackr#   r$   torch.utils._ordered_setr%   torch.utils._tracebackr&   torch.fx.proxyr'   r   r  r  r3   r:   r=   rx   r  r   rD  rE  r   r   rC   r  r  r  contextmanagerr  r  r  r0   r1   r2   <module>r     s/      I I I  
 < U U > 6 ,   ?   G B / 4 $ *(4GH *EF4 > "==		++33	L) L)` "  ). % $  & &R 
 
$@r1   