
    [Th                        S r SSKrSSKrSSKrSSKrSSKrSSKJrJr  SSK	J
r
JrJrJr  SSKrSSKJs  J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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/J0r0J1r1J2r2J3r3J4r4J5r5  SSK6J7r7J8r8  SSK9J:r:J;r;  SSK<J=r=  SSK>J?r?  \(       a  SSK@JArA  \$" \BS5      rC\$" \BS5      rDS rES rFS rG " S S5      rH " S S5      rI\H" 5       rJ/ SQrK\=" \\\R                  \R                  R                  R                  R                  /5      rP\R                  " 5       qRS rS " S S 5      rTS!qUS!qVS!qW\R                  S'S" j5       rY\R                  S# 5       rZS(S$ jr[S% r\S& r]g))a  
Provides functionality for compiling PyTorch's autograd (automatic differentiation) system.

This module implements compiled autograd, which traces and optimizes backward pass
computations at runtime. The key components are:

- AutogradCompilerInstance: Traces and compiles autograd graphs using FX
- Context managers (_enable/_disable): Control when compiled autograd is active
- Utility functions: Support graph manipulation, tensor operations, and hooks

Compiled autograd can significantly improve backward pass performance by removing
Python overhead and enabling additional optimizations. It works by capturing
backward computations into an FX graph that can be compiled and optimized,
while maintaining the same semantics as eager mode autograd.
    N)Counterdefaultdict)AnyOptionalTYPE_CHECKINGUnion)call_backward	call_hookFakeCompiledAutogradEngineGetItemSourceLocalSource)countersget_chromium_event_loggerlazy_format_graph_codeset_locals_to_stealcompile_contextCompileContext	CompileId)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     W/var/www/auris/envauris/lib/python3.13/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabledr7   F   s(    >>##--AA# r5   c                  ^    [         R                  R                  R                  R                  $ N)r/   	_inductorconfigtriton
cudagraphsr4   r5   r6   snapshot_cudagraph_enabledr>   L   s    ??!!((333r5   c                 "    U b  [        U 5      $ U $ r9   r   )xs    r6   maybe_clonerA   P   s    }%a((Hr5   c                   &    \ rS rSrS rS rS rSrg)OpNamespace[   c                 "    [        5       U l        g r9   )r   custom_function_name_counterselfs    r6   __init__OpNamespace.__init__\   s    :A))r5   c                 v  ^ U(       a1  SU-   nU R                   U   nU R                   U==   S-  ss'   U U 3n[        X5      (       a   e[        XU5      mU(       a+  [        X[        R
                  R                  T5      5        U$ [        R
                  R                  U4S j5       n[        XU5        U$ )NCppNode   c                     > T" U 0 UD6$ r9   r4   )argskwargsresults     r6   run_non_traceable_cpp_in_eager7OpNamespace.add.<locals>.run_non_traceable_cpp_in_eagerm   s    t.v..r5   )rF   hasattrOpsetattrr/   _dynamoallow_in_graphdisable)rH   namefnis_custom_functionis_traceablecountrR   rQ   s          @r6   addOpNamespace.add_   s    t#D55d;E--d3q83VE7#D4&&&&D01D < <V DE  ]]""/ #/ D >?r5   c                     [        X5      $ r9   )getattr)rH   rZ   s     r6   getOpNamespace.gett   s    t""r5   )rF   N)__name__
__module____qualname____firstlineno__rI   r_   rc   __static_attributes__r4   r5   r6   rC   rC   [   s    D*#r5   rC   c                   &    \ rS rSrS rS rS rSrg)rU   x   c                 6    X l         X0l        Xl        SU l        g )Nz#torch._dynamo.compiled_autograd.ops)r[   r\   re   rf   )rH   rZ   r[   r\   s       r6   rI   Op.__init__y   s    "4?r5   c                 &    U R                   " U0 UD6$ r9   )r[   )rH   rO   rP   s      r6   __call__Op.__call__   s    ww'''r5   c                 :    U R                   S-   U R                  -   $ )N.)rf   re   rG   s    r6   __repr__Op.__repr__   s    $t}}44r5   )rf   re   r[   r\   N)re   rf   rg   rh   rI   ro   rs   ri   r4   r5   r6   rU   rU   x   s    @(5r5   rU   )inputssizesscalarshookspacked_datac           
      <    [        [        [        U S S S95      5      $ )N)compiled_autograd_idframe_idframe_compile_idr   )r{   s    r6   make_compile_contextr~      s(    %9!%	
 r5   c                   h   \ 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\S\
R(                  R*                  R,                  S\\   4S jrS rS rS rS rS rS rS rS r S 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' r,\S( 5       r-\S) 5       r.S* r/S+ r0S, r1S- r2S. r3S/ r4S0 r5S1 r6 S;S
\\	\\\4         4S2 jjr7S3\4S4 jr8S5\S6\S7\\
R(                  Rr                     4S8 jr:S9r;g)<AutogradCompilerInstance   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&   r   r   fake_tensor_moder#   	fx_tracerr"   
proxy_modehooks_proxy)rH   r   s     r6   rI   !AutogradCompilerInstance.__init__   sr    &))+
ZZ%%
! .#'"&nn!

 )*0L,0r5   c                 v    [        U[        R                  5      (       d   eU R                  R	                  XS9$ )N)source)
isinstancer/   Tensorr   from_tensor)rH   r@   r   s      r6   	wrap_fake"AutogradCompilerInstance.wrap_fake   s3    !U\\****$$000BBr5   c                 ,    [        [        U 5      U5      $ r9   r   )rZ   idxs     r6   r   AutogradCompilerInstance.source   s    [.44r5   ru   rv   rw   originsc                 	  ^  [         S   S==   S-  ss'   [        [        5      T l        [	        T R                  5      T l        T R
                  R                  5         [        R                  " 5       T l	        [        5       R                  ST R                  ST R                  0SS9  S T l        0 T l        [        R                  R!                  5       T R"                  l        [        R&                  R)                  [*        S9T R"                  l        0 T R"                  l        0 T l        U 4S j[2         5       u  nT l        T l        T l        T l        T R<                  R?                  [A        5       5        Uu  pgn[C        U5       V	V
s/ s H'  u  pT RE                  U
T RG                  S	U	5      5      PM)     nn	n
T RI                  XU5        [C        U5       V	Vs/ s H@  u  pT RJ                  RM                  UT RG                  S
U	5      [N        RP                  5      PMB     nn	nT RI                  UT R4                  U5      n[C        U5       H   u  pX   T R0                  URR                  '   M"     [C        U5       H  u  pT RG                  SU	5      n[U        U[V        5      (       a/  T RJ                  RM                  UU[N        RP                  5      X9'   M[  [U        U[X        5      (       aF  T RJ                  R[                  T RJ                  R]                  UU[N        RP                  S9UUS9X9'   M  [_        S[a        U5      5      e   T RI                  UT R6                  U5        [C        U5       H,  u  nnT R6                  U   T R0                  URR                  '   M.     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                  RJ                  c   eT Rd                  RJ                  nT R<                  R?                  [        R&                  Rj                  Rl                  Ro                  U5      5        [q        [r        Rt                  " 5       5      UUU4$ s  sn
n	f s  snn	f )Nr,   capturesrM   graph_idTlog_pt2_compile_event)
tracer_clsc              3   `   >#    U  H#  nTR                   R                  S US0 5      v   M%     g7f)placeholderr4   N)r   create_proxy).0rZ   rH   s     r6   	<genexpr>9AutogradCompilerInstance.begin_capture.<locals>.<genexpr>   s0      
+ NN''tRDD+s   +.ru   rv   rw   )r   dynamic_dim)hintr   zUnexpected scalar type: );r   nextCOMPILE_COUNTERidr~   r   	__enter__timetime_nsstart_time_nsr   log_event_startaot_graph_cls_nameaot_graph_infosr/   nnModuler   rootfxGraphr#   graphtensor_attrssymnode_proxy_lookup_graph_placeholderssizes_proxyscalars_proxyr   packed_data_proxyr   enter_contextr'   	enumerater   r   bind_objects_to_proxiesr   $create_unspecified_symint_and_symbolr%   DYNAMICnoder   intfloatcreate_symfloatnodecreate_unspecified_symbolAssertionErrortyper   r   r   r   experimentalsymbolic_shapes_suppress_guardsstrr   current_compile_id)rH   ru   rv   rw   r   
args_proxyinputs_originssizes_originsscalars_originsr   r@   valproxiesisymintr   symvalenvs   `                 r6   begin_capture&AutogradCompilerInstance.begin_capture   s    	$%j1Q61'3DGG<&&(!\\^!#33!"&	 	4 	
 26:<#hhoo/$xx~~~I&(#$&!
+
	
" 	

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

  2/

  !6!67

  1

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

  HH!!11BB3G	
 1134	
 	
m

s   .SAScompile_reasonsc                 8   ^ T(       d   e[        SS U4S jS9  g )Nartifactc                      SSS.$ )N!compiled_autograd_compile_reasonsjsonrZ   encodingr4   r4   r5   r6   <lambda>>AutogradCompilerInstance.log_compile_reasons.<locals>.<lambda>#  s    ;"!r5   c                     > T $ r9   r4   )r   s   r6   r   r   '  s    r5   metadata_fn
payload_fn)r   )rH   r   s    `r6   log_compile_reasons,AutogradCompilerInstance.log_compile_reasons  s"      /	
r5   c                 L  ^ ^^^^^ TR                  5        Vs/ s H  nT R                  U5      PM     nnTR                  n	U	R                  mU	R                  mA	[
        R                  R                  UU4S j5       n
T R                  R                  SU
UU/UQ70 S9mS mUb  T R                  U   mUUUU 4S jnU" 5       nU 4S jn[
        R                  R                  R                  R                  TTUUS9n[        R                   " T R                  U5      nU$ s  snf )Nc                 x   > [         R                  R                  R                  R                  " U UTT/UQ76 nU$ r9   )r/   
_functorch_aot_autogradruntime_wrappers_backward_prologue_functional)ctx_saved_tensorsctx_symints	flat_argsoutmaybe_subclass_metadatametadatas       r6   call_aot_bwd_prologueOAutogradCompilerInstance.proxy_call_aot_backward.<locals>.call_aot_bwd_prologueF  sB    ""00AA__!'	
 C Jr5   call_functionkindtargetrO   rP   c            	        >^ S n U " TR                   R                  5      n[        U[        TS L5      -
  5       Vs/ s H  nTU   PM
     nnTR	                  5       n[        U5      [        TR                  5      :X  d   eU Vs/ s H  nTR                  U5      PM     nnXcS [        U5      & Tb  UR                  T5        Sn0 mS nTR                   R                  R                   GH  n	U	R                  S:X  a  X7   R                  TU	'   US-  nM,  U	R                  S:X  a  [        U	R                  5      S:X  d   eU	R                  S    V
s/ s H[  n
[        U
[        R                  R                   5      (       a-  [        R                  R#                  TU
   TR$                  5      OU
PM]     nn
M  U	R                  S:X  a  U	R&                  nTR$                  R)                  U5      n[+        TR$                  R,                  U[/        TR                   U5      5        TR$                  R1                  SUS0 5      nUTU	'   GM`  U	R                  S:X  a2  TR$                  R                  R3                  U	U4S	 j5      nUTU	'   GM  [5        S
5      e   Uc   eS nU Vs/ s H5  n[        U[        R                  R"                  5      (       a  U" 5       OUPM7     nnTR7                  UU5        U$ s  snf s  snf s  sn
f s  snf )Nc                 `    SnU R                    H  nUR                  S:X  a  US-  nM    U$    U$ )Nr   r   rM   )nodesop)r   num_argsr   s      r6   
num_inputskAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.num_inputsb  s<    !KKDww-/ A  (  r5   r   r   rM   outputget_attrr4   r   c                    > TU    $ r9   r4   )nvalue_remaps    r6   r   iAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>  s	    Ar5   zshouldn't get herec                      [        5          [        R                  " SSSSS5      sS S S 5        $ ! , (       d  f       g = f)Nr   {   r    r/   zerosr4   r5   r6   dummyfAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.dummy  s(    02 ;;q!Q37 322s   /
=)
_bw_moduler   ranger   _get_compiled_autograd_symintslensymintsto_proxyappendr  r  r   rO   r   r/   r   Noder+   r   r   get_fresh_qualnamerV   r   rb   create_node	node_copyr   r   )r  r  r   	pall_argsr  epsymintsargs_idxpoutputsr   r
  rZ   qualnamerQ   r  ooutputsr  ctxpbackward_statepgradsrH   s                    @r6   copy_paste_aot_backward_graphWAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_grapha  s     "#.."6"67H#(Ct8S4T)T#U#Uaq	#U   88:Gw<3s{{#333329:'Qa('H:(0nG%*  1 HK7;H,,2277m+(1(;(@(@K%MHWW(tyy>Q...
 "&1	  ".A &a77 {1~t~~F ".	   H WW
*;;D#~~@@FH++Xws~~t7T "^^77
HbRTUF(.K%WW/!^^11;;6F )/K%()=>>5 36 '''8
 JRIQA:a88a?   (((;Nq ;" :s   K7K<A"L%<Lc                 (  >^ ^ [         R                  R                  UU 4S j5       n[        R                  " TR
                  U5      nTR                  R                  SU[        U5      0 S9nTR                  5       nTR                  U/U/5        U$ )Nc                  $   > TR                  U TS9$ )N)
is_runtime)creation_fn)unwrapped_argsr-  subclass_metas    r6   make_subclasskAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructor.<locals>.make_subclass  s    $00J0WWr5   r   r   )r/   rW   rX   pytreetree_mapr  r   r   tupleallocate_dummyr   )r0  r-  r/  r1  punwrapped_argspoutputr  rH   s   ``     r6   proxy_subclass_constructorTAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructor  s    ]]))X *X %oodmm^LOnn11$$?+	 2 G ((*F((&G9=Mr5   )make_subclass_override)r  r  _forward_clsr   r   r/   rW   rX   r   r   r   r   r   r   _backward_epilogue_functionalr3  r4  )rH   pinputspsaved_tensorssaved_tensorspctxr&  maybe_backward_state_idxr  r   CompiledFunctionr   r)  r%  r9  resultspresultsr   r   r'  r(  s   `    `          @@@@r6   proxy_call_aot_backward0AutogradCompilerInstance.proxy_call_aot_backward*  s>   ( /2.P.P.RS.RDMM!$.RS ++#,,"2"J"J		%	%	 
&	 ,, ( 
  - 	
 #/"../GHOF	 F	P 01	$ ""00AA__##=	 ` 
 ??4==':M Ts   D!backward_idxr&  rB  c                 r   U R                   c   eU R                   U   nU R                  U5      nU R                  U5      n	[        UR                  S5      (       a  U R	                  UU	UUUU5      n
O%U R
                  R                  S[        UU	/UQ70 S9n
U
c   e[        5          / n[        U5       HK  u  pUb  X   c  UR                  S 5        M   Uu  pnnUR                  [        R                  " UUXS95        MM     U R                  X5        S S S 5        [        W5      $ ! , (       d  f       N= f)N_aot_idr   r   )sizedtypelayoutdevice)r   r  rT   r<  rF  r   r   r	   r    r   r  r/   emptyr   r5  )rH   ru   output_metadatasr@  rH  r&  rB  rA  r>  r?  r   grad_insr   output_metadatarM  rN  rL  rK  s                     r6   proxy_call_backward,AutogradCompilerInstance.proxy_call_backward  sM    +++---'}53##Y//22(G nn11$$" 
  2 	G """(*57H(12B(C$"*gl.BOOD).=+tKKTvU )D ((; + X +*s   'A.D((
D6c                     UUR                  5       UR                  5       UR                  5       UR                  5       UR                  5       UR                  5       4nU R                  [        US /S-  5      $ )N   )rv   stridesstorage_offset
proxy_callcopy_slices_prologue)rH   ru   baseviewrO   s        r6   call_copy_slices_prologue2AutogradCompilerInstance.call_copy_slices_prologue  sd    JJLLLN!JJLLLN!
 3TD6A:FFr5   c                 N    U R                  [        XX44S /[        U5      -  5      $ r9   )rY  copy_slices_epiloguer  )rH   needs_input_gradrQ   res
grad_slices        r6   call_copy_slices_epilogue2AutogradCompilerInstance.call_copy_slices_epilogue  s.     s7FS)**
 	
r5   c                 |    [        5          [        R                  " SS/5      sS S S 5        $ ! , (       d  f       g = f)Nr   i[r  rG   s    r6   r6  'AutogradCompilerInstance.allocate_dummy  s$    (*;;9~. +**s   -
;c                 .    [         R                  XX45      $ )zBinds ops.fn_name = fn)opsr_   )rH   fn_namer[   r\   r]   s        r6   bind_function&AutogradCompilerInstance.bind_function  s    www$6EEr5   c                 X    [         R                  U5      nU R                  XR/UQ7U5      $ )z:Proxies a call to ops.fn_name(grads, *args) into the graph)ri  rc   rY  )rH   rj  gradsrO   rR  r  s         r6   apply_functional)AutogradCompilerInstance.apply_functional  s'    WWWr>D>?CCr5   c                 f  ^  [         R                  " U5      u  pE[         R                  " U 4S jU5      nT R                  R	                  SX0 S9nU Vs/ s H  nT R                  5       PM     nnT R                  U[        [        U5      5       V	s/ s H  oU	   PM	     sn	5        U$ s  snf s  sn	f )z*Proxies a call to fn(*args) into the graphc                 &   > TR                  U 5      $ r9   r  )r  rH   s    r6   r   5AutogradCompilerInstance.proxy_call.<locals>.<lambda>  s    t}}Q/?r5   r   rO   rP   )	r3  tree_flattenr4  r   r   r6  r   r  r  )
rH   r[   rO   rR  r   _
proxy_args	proxy_outrQ   r   s
   `         r6   rY  #AutogradCompilerInstance.proxy_call  s    **40	__%?F
NN//R 0 
	 2AAA$%%'A$$VE#f+DV-WDVqlDV-WX B-Ws   B)B.
c                    [         R                  S5      n[        R                  " U R                  U/UQ75      nU R
                  R                  SXV0 S9n[        U5      [        U5      :X  d   eU R                  X'5        U$ )zEProxies a call to ops.validate_outputs(outputs, *args) into the graphvalidate_outputsr   ru  )	ri  rc   r3  r4  r  r   r   r  r   )rH   rw  r%  rO   rR  r  rx  new_proxy_outputss           r6   r|  )AutogradCompilerInstance.validate_outputs%  sz    WW'(__T]]W4Dt4DE
 NN77R 8 
 ?#s7|333$$W@r5   c                     U R                  U5      nU R                  U5      nU R                  R                  S[        R                  X440 S9nU R                  5       nU R                  U/U/5        U$ )Nr   ru  )r  r   r   r/   r_   r6  r   )rH   old_varnew_varold_var_proxynew_var_proxyry  rQ   s          r6   
accumulate#AutogradCompilerInstance.accumulate0  sq    g.g.NN//UYYm-KTV 0 
	 $$&$$fX	{;r5   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   )r   r   r
   r  )rH   hookrO   rP   r@   s        r6   proxy_call_hook(AutogradCompilerInstance.proxy_call_hook:  sO    ~~**,01Dq--"D1 
 	

 2s   Ac                     U R                   c   eU R                   U   nU R                  U   nU R                  UUSS9nU R                  5       nU R	                  U/U/5        U$ )Nunpack_hook	hook_type)r   r   r  r6  r   )rH   hook_iddata_idr  dataproxyr   s          r6   r  $AutogradCompilerInstance.unpack_hookE  s{    +++(%%g.$$# % 

 !!#$$cUUG4
r5   r   c                     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_hookr  r   r  r    rA   r   )rH   ru   r  r   r  r  s         r6   r  (AutogradCompilerInstance.tensor_pre_hookR  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  )rH   ru   r  r  r   r@   s         r6   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  )rH   r%  ru   r  r  r   r@   s          r6   r  "AutogradCompilerInstance.post_hookl  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  )r   r/   r   r   r  r    rA   r   )rH   inputr  r  r  s        r6   r  +AutogradCompilerInstance.post_acc_grad_hookz  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(       ap  UR                  5        HC  n
[        R                  SU
5        U
R                  S   R!                  5       U
R                  S'   ME     [        UR	                  5       5      $ / $ )NFr   ru   rM   r   cudaTcpuc              3   D  #    U  H  n[        UR                  [        R                  R                  5      =(       a    UR                  R
                  S ;   =(       d<    [        UR                  [        5      =(       a    UR                  R                  (       + v   M     g7f))primsatenN)r   r   r/   _ops
OpOverload	namespacerU   r\   r   users     r6   r   DAutogradCompilerInstance.move_graph_nodes_to_cuda.<locals>.<genexpr>  sx      
 !+ #4;;

0E0EF G KK115FF
 #4;;3 ? $ > >> !+s   BB zMoving node %s from cpu to cuda)listr  r   userskeysr  r   r   metarN  r   rK  allvaluesverbose_logdebugr  )rH   r   to_movehas_cuda_inputsr  ru   inputs_usersfirst_getitem_idxlast_getitem_idxr   r   is_cpu	is_scalar
node_userss                 r6   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- /4 (!!"CTJ#'99U#3#8#8#:		%  )
 ''	r5   c                 V   [        U[        R                  R                  5      =(       a    UR                  S:H  =(       ai    UR
                  [        R                  R                  R                  R                  [        R                  R                  R                  R                  4;   $ r  )r   r/   r   r  r  r   ri  r  sym_sizer   	sym_numeldefault)rH   r   s     r6   is_sym_node$AutogradCompilerInstance.is_sym_node  sn    tUXX]]+ O?*O		''++UYY^^-E-E-M-MNO	
r5   c                 0  ^ [        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                  5      nU R                  R                  R                  U5        [        U R                  R                  R                  5      n[        R                  SXE-
  5        g )Nr   )r  rM   c                    > U T;   =(       dU    U R                   S:H  =(       d?    U R                   S:H  =(       d)    U R                   S:H  =(       a    U R                  [        ;   $ )Nr   r  r   )r  r   _impure_targets)r   unpack_nodess    r6   	is_impure/AutogradCompilerInstance.dce.<locals>.is_impure  sV    $ S77m+S77h&S GG.Q4;;/3Q	r5   zDCE removed %d nodes)r)   r   r   r   
find_nodesupdater  r  r  r   r  eliminate_dead_coder  r  )rH   r   r   r  beforeafterr  s         @r6   dceAutogradCompilerInstance.dce  s    
 3=, !5!5!@!@M!@!RSGA

 12 TC+,q0000	 T^^))//000;DNN((../0&.Ar5   c                 l    [        U R                  R                  U R                  R                  U5      $ r9   )r   r   r   r   )rH   r   s     r6   create_graph_module,AutogradCompilerInstance.create_graph_module  s%    4>>..0D0DbIIr5   c           	      D  ^ ^^ T R                   R                  S[        R                  S0 5        T R                  R                  5         T R                   R                  SST R                   R                  T R                  U5      5      40 5        / m[        5       (       a%  T R                  T R                   R                  5      mT R                   R                  R                   H*  nS H!  nX2R                  ;   d  M  UR                  U	 M#     M,     [        SS U 4S jS9  T R                  5         T R!                  5         T R#                  5         T R%                  5         T R'                  5         T R)                  5         T R+                  5         T R-                  5         T R/                  5         T R1                  S	T R2                   35      m[5        TS
/5        [7        STSSSS9n[8        R;                  SU5        [<        R?                  SU5        [        SU4S jS9  UU 4S jn[A        5       RC                  S[D        RF                  " 5       ST R2                  0T RH                  SS9  T RJ                  RM                  S S S 5        UT RO                  T5      4$ )Nr   r4   r  )tensor_metaexample_valuer   r   c                      SSS.$ )N&compiled_autograd_graph_pre_reorderingstringr   r4   r4   r5   r6   r   6AutogradCompilerInstance.end_capture.<locals>.<lambda>  s    @$!r5   c                     > [        T R                  R                  T R                  R                  ST R                   S35      R                  SS9$ )NCompiledAutogradPreReorderingFprint_output)r   r   r   r   r   print_readablerG   s   r6   r   r     sG    {##$$"477)=9  n%n0	 1r5   r   r  ru   zCompiled autograd graphT)include_deviceinclude_stridecoloredz%scompiled_autograd_graphc                  "   > T R                  SS9$ )NFr  )r  )r   s   r6   r   r  '  s    u333Gr5   )r   c           	      F  >  Sq T H$  nX   R                  5       R                  SS9X'   M&     [        5          [	        TR
                  5         U " XX4U5      sS S S 5        sS S S 5        Sq $ ! , (       d  f       O= fS S S 5        O! , (       d  f       O= fSq g ! Sq f = f)NT)non_blockingF)in_compiled_autograd_region
pin_memoryr  _disabler~   r   )	compiled_fnru   rv   rw   rx   packed_inputsr   runtime_inputs_to_moverH   s	          r6   runtime_wrapper=AutogradCompilerInstance.end_capture.<locals>.runtime_wrapper*  s    4.2+/A &	 4 4 6 ; ; ; NFI 0 Z!5dgg!>&vgmT "?!>ZZ /4+ "?!>ZZZ /4+e+s@   6B B
A.	B"	B .
A<	8B?	B 
BB B r,   r   r   )(r   r   r   _exec_final_callbacks_stubr   r   r  
create_argr  r>   r  r   r  r  r   rename_aot_dispatcher_nodesdelay_unpack_hook_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   r   r   compiled_autograd_loginfor  r  r   log_event_endr   r   r   r   __exit__r   )rH   r%  r   fieldlazy_graph_coder  r   r  s   `     @@r6   end_capture$AutogradCompilerInstance.end_capture  sA   ##&AA		
 	

""^^&&t}}W'=>@		
 -/%''%)%B%B4>>CWCW%X"
 NN((..D@II%		%( A /
 	1	
 	((*$$&**,446**,224--/$$& 	
((+;DGG9)EFEH:.0%
 	""49$0%G	


	4 	"#11LLN!"& 	2 	
 	%%dD$7 0 0 777r5   c           	         U R                   c  gS[        R                  R                  R                  S[        R                  R                  R                  4S jn[        [        5      nU R                  R                  5        GH  u  p4US   nUS   nSnUS   R                  nX&   (       a  S	X&    3nX&==   S
-  ss'   [        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 U S	U
R$                   3Ul        ['        U
R(                  5       H+  u  pSU 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Ma  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  $ )Nre   rZ   zaten::reshapeoriginal_aten)
r   rT   re   rZ   r  rc   r  r   r  all_input_nodes)r  r  target_matchs      r6   
is_similarHAutogradCompilerInstance.rename_aot_dispatcher_nodes.<locals>.is_similarH  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	r5   ca_node_start_idxaot_id aot_gmrw  rM   r   r  zIFailed to match %s%s (NodeCall %s) nodes with AOT backward graph %s nodes)r   r/   r   r   r  r   r   r   itemsr   iterr  r   r  StopIterationr   r  r  rZ   r   r
  r  r  )rH   r  aot_id_counternodecall_indexr  r  r  aot_id_postfix	aot_graphaot_itaot_nodeca_itrw  ca_noder   inps                   r6   r  4AutogradCompilerInstance.rename_aot_dispatcher_nodes@  s   
 ""*	588==-- 	EHHMM4F4F 	8 *5S)9$($8$8$>$>$@ N $%8 9(^FNX,,I%#$^%;$<!="a'" )//*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 ,+%(0@(--#QGL"+H,D,D"E $'vh~.>az!J  // #F
  $F|H"5kG% kkX-'**2H2H-2H-C %A  ! H ! !!_++" s,   (+J#BJ5.C$J5#
J21J25-K'&K'c                     U  Vs/ s H-  n[        U5      [        R                  R                  L d  M+  UPM/     nnU$ s  snf r9   )r   r/   r   r  )rO   r
  r  s      r6   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)Nr   r   r   TF)r  r   operatorgetitemrO   )r   s    r6   is_placeholder'AutogradCompilerInstance.is_placeholder  sF    77m#GG&x///		!=0r5   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   rM   N)r   r   r  r/   ri  inductoraccumulate_grad_r  rO   r   r#  r$  maxprevr%  r  )rH   r   
param_node	grad_nodegetitem_nodeargs         r6   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|,
r5   c                     U R                   R                  R                  S[        S9 HK  nUR                  R                  SS5      S:w  a  M%  [        UR                  5      nUR                  U5        MM     g)z`
We can delay unpack hooks until they are needed, even later than in the eager autograd engine.
r   r(  r  Nr  )	r   r   r  r
   rP   rc   minr  prepend)rH   r   
first_users      r6   r  0AutogradCompilerInstance.delay_unpack_hook_nodes  sd     NN((33y 4 
D {{{D1]BTZZJt$
r5   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   r(  r  Nr  r   rM   )
r   r   r  r
   rP   rc   rO   r,  r%  r  )rH   r   r/  
input_nodes       r6   r  6AutogradCompilerInstance.reorder_tensor_pre_hook_nodes  s     NN((33y 4 
D {{{D15FF99Q<L1J*43F3Fz3R3R!!,/##D)
r5   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   r(  r  Nr  r   rM   )r   r   r  r
   rP   rc   rO   r   r  r   r#  r$  r  zipremover+  r,  r%  )rH   r   r/  input_nodes	to_remove	to_append
hook_blockr
  abr0  s              r6   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
r5   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   r(  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     r6   r   QAutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager.<locals>.<genexpr>#  s7      !D ?*Nt{{h>N>N/NN!s   A A)r   r   r  r
   rP   rc   r  reversedrO   r  r  r  r  r  r   r  r4  )rH   	pre_hooksr   hook_getitem_noder  registered_noder$  s          r6   r  >AutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager  s%    	NN((33y 4 
D {{{D1Z?T"
 Y'D $		!*+E5zQ  !     #4a(;(;(=#>?Oii/''(9:''-$G#++G4  %# (r5   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   r(  r  Nr  r   rM   z8post_acc_grad_hook must have corresponding acc grad node)r   r   r  r
   rP   rc   r  rG  rO   r  r  r  r  r   r/   ri  r)  r*  r  )rH   post_acc_grad_hooksr   r/  r-  acc_grad_noder
  s          r6   r  9AutogradCompilerInstance.reorder_post_acc_grad_hook_nodes/  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,
   .%) 2r5   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   r(  r  Nr  r   rM      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
   rP   rc   )r   r  r   s     r6   r   CAutogradCompilerInstance.reorder_post_hook_nodes.<locals>.<genexpr>p  sI      - =?2 KK94 KKOOK>+M D =s   AA	Ar  )r   r   r  r
   rP   rc   r  rG  rO   r  extendr  r  r  r+  r  r   r/   ri  r)  r*  r  r,  r%  )rH   
post_hooksr/  output_nodesr=  input_nodes_and_usersr8  r0  r-  post_acc_grad_hook_noder
  r   s              @r6   r  0AutogradCompilerInstance.reorder_post_hook_nodesV  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 )r5   c                 l  ^  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
                  [        R                  45      (       a  T R                  UR                     $ [        U[        R                  5      (       d  U$ [        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r9   rs  )r   r@   rH   s     r6   r   4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>  s     51aq))1s   !)r   r  r  r5  r/   SymIntSymFloatr   r   r   r!   r   r   r   proxy_tensor_ProxyTensorr  )rH   tr@   r_  s   `   r6   r  !AutogradCompilerInstance.to_proxy  s    9a./0aDMM!$a00a51555a%,,788,,QVV44!U\\**H)$..!<,(=(=(J(J(W(WXXXX!!! 1s   D1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  U$ s  snf N)constanttracer)
r   r/   r   r+   r  r  set_node_originr  r$   r   )rH   objectsr   r   bound_proxiesr   r  	node_names           r6   r   0AutogradCompilerInstance.bind_objects_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$ rd  )r   r   r$   r   )rH   rl  r  bw_states       r6   bind_backward_state,AutogradCompilerInstance.bind_backward_state  s@    +++  ' ?(DPr5   rj  r  pyobjc                    SnUb  UR                   n[        US5      (       ax  UR                  c  [        S5      e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 )	Nr  rJ  zThis compiled backward function was saved by AOTAutogradCache, which does not support
                    compiled autograd. Please turn off AOTAutogradCache using `TORCHINDUCTOR_AUTOGRAD_CACHE=0`.)r  r  r  z (NodeCall )r  z:raw_stack_trace = CapturedTraceback.extract().format()[-1])r<  rT   _lazy_backward_infoRuntimeErrorr   rJ  r  r   r   r  	bw_moduler   r*   extractformatreplacer(   )	rH   rj  r  rq  maybe_aot_idforward_clsnew_coderaw_stack_tracenew_stack_traces	            r6   rg  (AutogradCompilerInstance.set_node_origin  s     ,,K{I..22:&s  +4'*22),T^^-A-A-G-G)H*)==GG8$$^4  [k.9IK+335<<>rB)11H(
 	(r5   )r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Nr9   )<re   rf   rg   rh   rI   r   staticmethodr   r   r  r/   r   r   r   r   r5  r   r   r   rF  autogradfunctionBackwardCFunctionr   rS  r]  rd  r6  rk  ro  rY  r|  r  r  r  r  r  r  r  r  r  r  r  r  r  r   r%  r  r  r  r  r  r  r  r  r   ro  Functionrg  ri   r4   r5   r6   r   r      s   1C 5] 5 5a
U\\"a
 Cya
 eCJ'(	a

 d5c?+,a
F
c
Zx1
 1 ^^$$661 #+3-1f
G
/
FD
			
# &/c /b
B.J^8@`D  
  -*%*("+H!5F%&N:*x"" LP)1$uS#X2G)H& )) ) //0	)r5   r   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_CrW   r,   set_autograd_compiler	functoolspartialr   r7   set_verbose_loggerr  compiled_autograd_enabledr  set_multithreading_enabled)r   dynamicr  r/   prior_compilerprior_dynamics         r6   _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  rW   r,   r  r  )r  r  s     r6   r  r    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        [        R                  R                  R
                  R                  5         [        R                  " 5       qg )NF)r  r  r/   r  rW   r,   r  r  clear_cache	itertoolsr^   r   r4   r5   r6   resetr  #  sz     %****	HH&&<<T5I	HH&&99$?	HH&&224oo'Or5   c                     U S   nUR                  X5      nUc   eUR                  U5        Xc-
  n	UR                  XEU	5      n
XU
R                  [        R
                  S9/$ )Nr   )memory_format)new_empty_stridedcopy_
as_stridedcloner/   contiguous_format)ru   
base_sizesbase_stridesbase_storage_offset
view_sizesview_stridesview_storage_offsetgradrQ   offsetrc  s              r6   rZ  rZ  0  sn     !9D##J=F
LL 6F"":VDJ
 0 0u?V?V 0 WXXr5   c                     S /[        U 5      -  n[        [        U 5      5       H:  nX   (       d  M  X%   c  M  US:X  a  UR                  X%   5        XU'   M4  X%   XE'   M<     U$ )Nr   )r  r  r  )ra  rQ   rb  rc  grad_inputsr   s         r6   r`  r`  D  sk    &3/00K3'()v~Av  (!'A!$ * r5   )Fr  )^__doc__r   r  r  r#  r   collectionsr   r   typingr   r   r   r   r/   torch.utils._pytreeutils_pytreer3  torch._dynamo.external_utilsr	   r
   r   torch._dynamo.sourcer   r   torch._dynamo.utilsr   r   r   r   torch._guardsr   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+   re   r  r  r7   r>   rA   rC   rU   ri  r   r  r)  r*  r  r  r^   r   r~   r   r  r  r  contextmanagerr  r  r  rZ  r`  r4   r5   r6   <module>r     s         , 6 6  $ $ 
 <  E D > 6 ,   ?   G B / 4 $ *(4GH *EF4# #:5 5 m M "==		++33	 //#	w) w)v! "  ). % $  & &R 
 
$(Y(r5   