
    9i"                        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  SSKJrJrJrJrJr  SSKrSSKJs  Jr  SSKJr  SSKJrJr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)  SSK*J+r+J,r,J-r-J.r.  SSK/J0r0J1r1  SSK2J3r3  SSK4J5r5  SSK6J7r7  SSK8J9r9  SSK:J;r;  SSK<J=r=J>r>J?r?J@r@JArAJBrBJCrC  SSKDJErEJFrF  SSKGJHrHJIrI  SSKJJKrKJLrL  SSKMJNrN  SSKOJPrP  \(       a  SSKQJRrR  SrS\0" \TS5      rU\0" \TS5      rVS\W4S jrXS\W4S jrYS\\R                     S\\R                     4S  jr[S!\S\S"\4   4S# jr\ " S$ S%5      r] " S& S'5      r^ " S( S)5      r_\^" 5       r`/ S*Qra\N" \\\R                  \/5      rc\R                  " 5       qeS+\fS\4S, jrg " S- S.5      rhS/qiS/qjS/qkS/qlSqm\R                    SDS0\S"\4   S1\WS2\WS\
S3   4S4 jj5       ro\R                  S\
S3   4S5 j5       rpSES6 jrqS7\\R                     S8\\L   S9\\L   S:\LS;\\L   S<\\L   S=\LS\r\R                     4S> jrsS?\\W   S@\R                  SA\\\R                        SB\R                  S\r\\R                        4
SC jrtg)Fa  
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)	GeneratorSequence)AnyCallableOptionalTYPE_CHECKINGUnion)enable_python_dispatcher)call_accumulate_gradcall_backward	call_hookFakeCompiledAutogradEngineunwrap_maybe_dynamic_intGetItemSourceLocalSource)countersget_chromium_event_loggerlazy_format_graph_codeset_locals_to_steal)AutogradLazyBackwardCompileInfo%CachedAutogradLazyBackwardCompileInfo)compile_contextCompileContext	CompileIdSource)getArtifactLoggertrace_structuredclone_preserve_strides)FakeTensorMode)
FakeTensor)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)FloatLikeTypeIntLikeType)
OrderedSet)CapturedTraceback)Proxya  You can turn off compiled autograd by either:
1. Moving the unsupported autograd call outside of the torch.compile'd region.
2. Wrapping the unsupported autograd call in the torch._dynamo.compiled_autograd._disable() context manager.
3. Setting torch._dynamo.config.compiled_autograd=False for the torch.compile call containing the unsupported autograd call.
4. Setting torch._dynamo.config.compiled_autograd=False at the start of the program.compiled_autogradcompiled_autograd_verbosereturnc                  h    [         R                  R                  R                  R	                  S5      $ )Nr8   )torch_logging	_internal	log_stateis_artifact_enabled     _/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabledrC   T   s(    >>##--AA# rA   c                  ^    [         R                  R                  R                  R                  $ N)r;   	_inductorconfigtriton
cudagraphsr@   rA   rB   snapshot_cudagraph_enabledrJ   Z   s    ??!!((333rA   xc                 "    U b  [        U 5      $ U $ rE   r!   )rK   s    rB   maybe_clonerM   ^   s    }%a((HrA   CompiledFunction.c                 z   [        U R                  [        5      (       a  U R                  R                  $ [        U R                  [        5      (       aL  [
        R                  R                  R                  5          U R                  R                  5       sS S S 5        $ [        S5      e! , (       d  f       g = f)NzEUnexpected Lazy Backward Compilation Info Type. Please file an issue.)
isinstance_lazy_backward_infor   	bw_moduler   r;   _subclassesfake_tensorunset_fake_temporarilybw_module_fnAssertionError)rN   s    rB   extract_bw_modulerX   d   s    ,,.M   33===	,,.S
 
 **AAC#77DDF DC S
 	
 DCs   =B,,
B:c                       \ rS rSrS\SS4S jrS\R                  R                  SS4S jr	S\
\R                     SS4S	 jrS
\
\R                     SS4S jrSrg)
NaNChecker   accumulate_gradr9   Nc                 :    Xl         / U l        0 U l        / U l        g rE   )r\   params_indicesparams_to_checkoutput_names)selfr\   s     rB   __init__NaNChecker.__init__   s     .)+8:')rA   graphc                 z   [        [        UR                  5      5      nUR                  S[        S9nUR                  SS9S   R
                  S   nU R                  [        U5      :X  a  U R                  U(       + :X  d   eU H  nUR
                  S   nUR                  [        R                  :X  a4  UR
                  S   UL a"  [        UR
                  S   [        5      (       d   eU R                  R                  UR
                  S   5        M     U Vs/ s H  oUR                  PM     snU l        g s  snf )Ncall_functionoptargetoutputrh   r      )nextiternodes
find_nodesr   argsr\   boolri   operatorgetitemrP   intr^   appendnamer`   )ra   rd   inputs_nodeacc_grad_nodesoutput_nodesnode
param_nodes          rB   prep_with_graphNaNChecker.prep_with_graph   s&   4,-))'; * 
 ''8'4Q7<<Q?##t(
 
""<'78	9 9 #D1J
 !!X%5%55OOA&+5zq13778 &&zq'9: # 4@@<4YY<@@s   D8inputsc                    U R                   (       d  g U R                   H]  nX   R                  nUb4  [        R                  " U5      R                  5       (       a   SU S35       eX   U R                  SU S3'   M_     g )Nz9Compiled autograd running under anomaly mode with inputs[zD] already having NaN gradient. This is not supported. {TURN_OFF_MSG}zinputs[])r\   r^   gradr;   isnananyr_   )ra   r   idxr   s       rB   prep_with_inputsNaNChecker.prep_with_inputs   s    ## &&C;##D ;;t,0022 OPSu UQ Q2
 6<[D  73%q!12 'rA   outc                    U R                   (       a  U(       a   e/ nU R                  R                  5        HZ  u  p4UR                  c   e[        R
                  " UR                  5      R                  5       (       d  MI  UR                  U5        M\     U(       a  [        SSR                  U5       S35      eg / n[        U5       HN  u  pg[        R
                  " U5      R                  5       (       d  M0  UR                  U R                  U   5        MP     U(       a  [        SSR                  U5       S35      eg )Nz9Compiled Autograd returned NaN gradients for parameters: ,.z;Compiled Autograd returned NaN gradients for output nodes: )r\   r_   itemsr   r;   r   r   rv   RuntimeErrorjoin	enumerater`   )ra   r   
nan_params
inputs_strparam	nan_gradsir   s           rB   checkNaNChecker.check   s   N7$&J%)%9%9%?%?%A!
zz---;;uzz*..00%%j1 &B
 "OPSPXPXYcPdOeefg   $&I$S>;;t$((**$$T%6%6q%9: * "QRURZRZ[dReQffgh  rA   )r\   r`   r^   r_   )__name__
__module____qualname____firstlineno__rr   rb   r;   fxGraphr}   tupleTensorr   r   __static_attributes__r@   rA   rB   rZ   rZ      sn    * * *AUXX^^ A A0AuU\\': At A u||,  rA   rZ   c            
       X    \ rS rSrSS jrS\S\S\4   S\S	\S\4
S
 jr	S\S\4S jr
Srg)OpNamespace   r9   Nc                 "    [        5       U l        g rE   )r   custom_function_name_counterra   s    rB   rb   OpNamespace.__init__   s    :A))rA   rw   fn.is_custom_functionis_traceablec                   ^ 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                  S[        S[        S[        4U4S jj5       n[        XU5        U$ )NCppNoderl   rq   kwargsr9   c                     > T" U 0 UD6$ rE   r@   )rq   r   results     rB   run_non_traceable_cpp_in_eager7OpNamespace.add.<locals>.run_non_traceable_cpp_in_eager   s    t.v..rA   )	r   hasattrOpsetattrr;   _dynamoallow_in_graphdisabler   )ra   rw   r   r   r   countr   r   s          @rB   addOpNamespace.add   s     t#D55d;E--d3q83VE7#D4&&&&D01D < <V DE  ]]""/c /S /S / #/ D >?rA   c                     [        X5      $ rE   )getattr)ra   rw   s     rB   getOpNamespace.get   s    t""rA   )r   r9   N)r   r   r   r   rb   strr   r   rr   r   r   r   r@   rA   rB   r   r      sY    D S#X !	
  
6# # #rA   r   c                   \    \ rS rSrS\S\S\4   S\SS4S jrS	\S
\S\4S jr	S\4S jr
Srg)r      rw   r   .r   r9   Nc                 6    X l         X0l        Xl        SU l        g )Nz#torch._dynamo.compiled_autograd.ops)r   r   r   r   )ra   rw   r   r   s       rB   rb   Op.__init__   s     "4?rA   rq   r   c                 &    U R                   " U0 UD6$ rE   )r   )ra   rq   r   s      rB   __call__Op.__call__   s    ww'''rA   c                 :    U R                   S-   U R                  -   $ )Nr   )r   r   r   s    rB   __repr__Op.__repr__   s    $t}}44rA   )r   r   r   r   )r   r   r   r   r   r   r   rr   rb   r   r   r   r@   rA   rB   r   r      s[    @@%c3h/@EI@	@(c (S (S (5# 5rA   r   )r   sizesscalarshookspacked_datacompiled_autograd_idc           
      <    [        [        [        U S S S95      5      $ )N)r   frame_idframe_compile_id)r   r   r   )r   s    rB   make_compile_contextr     s(    %9!%	
 rA   c                      \ rS rSrS\S\4   SS4S jrS\R                  S\	\
   S\4S	 jr\S
\S\S\4S j5       rS\\R                     S\\   S\\\\4      S\\\\\4         S\S\S\\\\R                     \\   \\   4   4S jrS\\   SS4S jrS\\   S\\R                     S\\R                     S\S\S\	\   S\\   4S jrS\\   S\\   S\\R                     S\S\R:                  R<                  R>                  S\	\   S\\	\R                     S4   4S jr S\\   S \\   S!\\   S"\S#\\   S$\\   S%\S\\R                     4S& jr!S'\\   S(\R                  S)\\   S*\R                  S\\R                     4
S+ jr"S\R                  4S, jr#S-\S.\S\4   S/\S0\S\4
S1 jr$S-\S2\\   S3\S4\\   S\\R                     4
S5 jr%S.\S\4   S3\S4\\   S\\R                     4S6 jr&S7\S8\\   S3\S4\\   S\\R                     4
S9 jr'S:\S;\S\R                  4S< jr(S=\R                  S>\R                  S?\SS4S@ jr)SA\S\4   S3\SB\S\RT                  RV                  4SC jr,SD\SE\S\R                  4SF jr-S\\R                     SD\SG\S\\R                     4SH jr.S\\R                     SD\SG\S\\R                     4SI jr/S\\   SD\S\\R                     4SJ jr0S8\\R                     S\\R                     SD\S\\R                     4SK jr1SL\R                  SD\S\\R                     4SM jr2SN\RT                  Rf                  S\\   4SO jr4SP\S\4SQ jr5SlSR jr6S\7\   4SS jr8ST\S\94SU jr:S8\S\\S\4   \4   4SV jr;\S3\\   S\\RT                  Rx                     4SW j5       r=\SP\RT                  Rx                  S\4SX j5       r>SlSY jr?SlSZ jr@SlS[ jrASlS\ jrBSlS] jrCSlS^ jrDSlS_ jrES`\S\4Sa jrF SmSb\\   Sc\S\	\\\\4         S\\   4Sd jjrGSe\S\H4Sf jrISg\Sh\Si\	\R:                  R                     SS4Sj jrKSkrLg)nAutogradCompilerInstancei  compiler_fn.r9   Nc                 .   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)r   
contextlib	ExitStackstackcloser/   r   r#   fake_tensor_moder,   	fx_tracerr+   
proxy_modehooks_proxy)ra   r   s     rB   rb   !AutogradCompilerInstance.__init__  sr    &))+
ZZ%%
! .#'"&nn!

 )*0L,0rA   rK   sourcec                 v    [        U[        R                  5      (       d   eU R                  R	                  XS9$ )N)r   )rP   r;   r   r   from_tensor)ra   rK   r   s      rB   	wrap_fake"AutogradCompilerInstance.wrap_fake,  s3    !U\\****$$000BBrA   rw   r   c                 ,    [        [        U 5      U5      $ rE   r   )rw   r   s     rB   r   AutogradCompilerInstance.source0  s    [.44rA   r   r   r   originsr\   
check_nansc                 >  ^  [         S   S==   S-  ss'   [        [        5      T l        [	        [
        5      T l        [        T R                  5      T l        T R                  R                  5         U(       a  [        U5      OS T l        [        R                  " 5       T l        [        5       R!                  ST R                  ST R                  0SS9  ["        R$                  R'                  5       T R(                  l        ["        R,                  R/                  [0        S9T R(                  l        0 T R(                  l        0 T l        U 4S j[8         5       u  nT l        T l        T l        T l         T RB                  RE                  [G        5       5        Uu  pn
T RB                  RE                  [I        5       5        US	   n [K        U5       H)  u  pT RM                  UT RO                  S
U5      5      X'   M+     T RY                  XU5        [K        U5       VVs/ s H@  u  pT RZ                  R]                  UT RO                  SU5      [^        R`                  5      PMB     nnn[c        [e        U5      5       Vs/ s H  nT R:                  U   PM     nn[K        U5       HK  u  nnT R(                  Rg                  S[h        UU   40 5      UU'   UU   T R6                  URj                  '   MM     T RY                  UUU	5      n[K        U5       H  u  pT RO                  SU5      n[m        U[
        5      (       a/  T RZ                  R]                  UU[^        R`                  5      X<'   M[  [m        U[n        5      (       aF  T RZ                  Rq                  T RZ                  Rs                  UU[^        R`                  S9UUS9X<'   M  [u        S[U        U5      5      e   T RY                  UT R<                  U
5        [K        U5       H,  u  nnT R<                  U   T R6                  URj                  '   M.     T RB                  RE                  [w        0 5      5        T RB                  RE                  T Rx                  5        T RB                  RE                  T Rz                  5        T RB                  RE                  [}        5       5        T Rx                  RZ                  c   eT Rx                  RZ                  nT RB                  RE                  ["        R,                  R~                  R                  R                  U5      5        [        [        R                  " 5       5      UUU4$ ! [P         a$  n[S        S[U        U5       S[V         35      UeS nAff = fs  snnf s  snf )Nr7   capturesrl   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)placeholderr@   N)r   create_proxy).0rw   ra   s     rB   	<genexpr>9AutogradCompilerInstance.begin_capture.<locals>.<genexpr>T  s0      
+ NN''tRDD+s   +.r   r   zFound tensor of type z,, which is not supported by FakeTensorMode. r   rf   r   )r   dynamic_dim)hintr   zUnexpected scalar type: )Er   rm   COMPILE_COUNTERidr   ru   aot_id_counterr   r   	__enter__rZ   nan_checkertimetime_nsstart_time_nsr   log_event_startr;   nnModuler   rootr   r   r,   rd   tensor_attrssymnode_proxy_lookup_graph_placeholderssizes_proxyscalars_proxyr   packed_data_proxyr   enter_contextr0   r   r   r   r   	ExceptionNotImplementedErrortypeTURN_OFF_MSGbind_objects_to_proxiesr   $create_unspecified_symint_and_symbolr.   DYNAMICrangelenr   r   r{   rP   floatcreate_symfloatnodecreate_unspecified_symbolrW   r'   r   r   r(   experimentalsymbolic_shapes_suppress_guardsr   r   current_compile_id)ra   r   r   r   r   r\   r   
args_proxyinputs_originssizes_originsscalars_originsrK   r   eval	sym_sizesr   proxiessymintr   symvalenvs   `                     rB   begin_capture&AutogradCompilerInstance.begin_capture4  s    	$%j1Q61'.9#.>3DGG<&&(:D:o6$!\\^!#33!"&	 	4 	
 $hhoo/$xx~~~I&(#$&!
+
	
" 	

  !3!569@6 	

  !9!;< 1I	#F+"nnQHc0JK , 	$$VH &e,
 - NN??GS)""
 - 	 
 16c)n0EF0E14##A&0EF"9-IAv44(	GAJ 6=QZD%%fkk2 . ..y'=Q!'*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	
 	
C  	%'Qy0\]i\jk	
 Gs%   8U# AV=V#
V-VV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rw   encodingr@   r@   rA   rB   <lambda>>AutogradCompilerInstance.log_compile_reasons.<locals>.<lambda>  s    ;"!rA   c                     > T $ rE   r@   )r+  s   rB   r3  r4    s    rA   metadata_fn
payload_fn)r    )ra   r+  s    `rB   log_compile_reasons,AutogradCompilerInstance.log_compile_reasons  s"      /	
rA   pinputspsaved_tensorssaved_tensorspctxctxmaybe_backward_state_idxc           	        ^ ^^^^^^^ TR                  5        Vs/ s H  nT R                  U5      PM     nnTR                  n	[        U	5      mU	R                  mU	R
                  mU	R                  mA	[        R                  " 5       (       a/  TR                   H  n
U
R                  (       d  M  [        S5      e   [        R                  R                  S[        [        R                     S[        [            S[        ["           S["        4UU4S j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S[*        [        R                     4UUUUUU 4S	 jjnU" 5       nS
["        S[,        S[        ["           S[        R                  4U 4S jjn[        R.                  R0                  R2                  R5                  TTUUS9n[6        R8                  " T R                  U5      nU$ s  snf )Nz@torch.compile does not currently support higher order gradients.ctx_saved_tensorsctx_symints	flat_argsr9   c                 x   > [         R                  R                  R                  R                  " U UTT/UQ76 nU$ rE   )r;   
_functorch_aot_autogradruntime_wrappers_backward_prologue_functional)rB  rC  rD  r   maybe_subclass_metadatametadatas       rB   call_aot_bwd_prologueOAutogradCompilerInstance.proxy_call_aot_backward.<locals>.call_aot_bwd_prologue  sD     ""00AA__!'	
 C JrA   rf   kindri   rq   r   c            	      	  >^^ S[         R                  R                  S[        4S jn U " T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5      mTR                  T   (       a  TSTR                  T    3-  mTR                  T==   S-  ss'   S[        S[        4U4S jjn	TR                  R                   GH  n
U
R                  S	:X  a1  X7   R                  nU	" U
R                   5      Ul        UTU
'   US-  nME  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[1        TR*                  R2                  U[5        TU5      5        TR*                  R7                  SUS0 5      nU	" U
R                   5      Ul        UTU
'   GM  U
R                  S:X  a  U
R,                  [         R8                  R:                  R<                  R>                  :X  a3  [         R8                  R:                  R@                  R>                  U
l        TR*                  R                  RC                  U
U4S j5      nU	" U
R                   5      Ul        UTU
'   GMN  U
R                  S:X  a  U
R,                  nTR*                  R/                  U5      n[1        TR*                  R2                  U[5        TU5      5        TR*                  R                  RC                  U
U4S j5      nXl        UTU
'   GM  [E        S5      e   Uc   eS[         RF                  4S jnU Vs/ s H5  n[%        U[         R                  R(                  5      (       a  U" 5       OUPM7     nnTRI                  UU5        U$ s  snf s  snf s  snf s  snf )Nrd   r9   c                 `    SnU R                    H  nUR                  S:X  a  US-  nM    U$    U$ )Nr   r   rl   )ro   rh   )rd   num_argsr{   s      rB   
num_inputskAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.num_inputs   s<    !KKDww-/ A  (  rA   r   _rl   	node_namec                    > ST SU  3$ )NaotrU  r@   )rV  deduped_aot_ids    rB   make_uniquelAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.make_unique&  s    ^,Ai[99rA   r   rj   get_attrr@   rf   c                    > TU    $ rE   r@   nvalue_remaps    rB   r3  iAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>F  	    ArA   call_modulec                    > TU    $ rE   r@   r^  s    rB   r3  ra  O  rb  rA   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;   zerosr@   rA   rB   dummyfAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.dummyZ  s(    02 ;;q!Q37 322s   /
=)%r;   r   r   ru   rd   r  _get_compiled_autograd_symintsr  symintsto_proxyrv   r   r   ro   rh   r{   rw   rq   rP   Noder6   r   ri   get_fresh_qualnamer   r  r   create_nodeopsatenviewdefaultreshape	node_copyrW   r   r  )rS  rR  r   	pall_argsrl  r"  psymintsargs_idxpoutputsrZ  r{   phr_  rw   qualnamer   ri  ooutputsrY  r`  aot_idrR   r?  pbackward_statepgradsra   s                      @@rB   copy_paste_aot_backward_graphWAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph  s%    %((..  S   ")//2H#(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 ![N""6*Ad&9&9&&A%B"CC'1,':s :s : "--77m+",11B)$))4BG(*K%MHWW(tyy>Q...
 "&1	  ".A &a77 {1~t~~F ".	   H WW
*;;D#~~@@FHDNN//79d;ST!^^77
HbRTUF"-dii"8FK(.K%WW/{{eiinn&9&9&A&AA ',iinn&<&<&D&D!^^11;;6F #.dii"8FK(.K%WW-;;D#~~@@FHDNN//79d;ST!^^11;;6F %-M(.K%()=>>U .X '''85<< 8
 JRIQA:a88a?   (((;Nk ;> Xs   R=SA"S+<Ssubclass_meta
is_runtimeunwrapped_argsc                 D  >^ ^ [         R                  R                  S[        S[        4UU 4S j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$ )Nr  r9   c                  $   > TR                  U TS9$ )N)r  )creation_fn)r  r  r  s    rB   make_subclasskAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructor.<locals>.make_subclassi  s    $00J0WWrA   rf   rN  )r;   r   r   r   pytreetree_maprm  r   r   r   allocate_dummyr  )r  r  r  r  punwrapped_argspoutputrj   ra   s   ``     rB   proxy_subclass_constructorTAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructorf  s     ]]))Xs Xs X *X %oodmm^LOnn11$$?+	 2 G ((*F((&G9=MrA   )make_subclass_override)rk  rm  _forward_clsrX   rK  rJ  _aot_idr;   is_grad_enabledoutput_inforequires_gradr   r   r   r   r   r3   r   r   r   r   listrr   rF  rG  rH  _backward_epilogue_functionalr  r  )ra   r;  r<  r=  r>  r?  r@  r"  rx  rN   output_alias_inforL  r  r~  r  resultspresultsr  rR   rJ  rK  r  r  s   `    `           @@@@@@rB   proxy_call_aot_backward0AutogradCompilerInstance.proxy_call_aot_backward  s   ( /2.P.P.RS.RDMM!$.RS ++%&67	#,,"2"J"J!))  ""%-%9%9!$222&Z  &: 
	%	%	'5	!+.	 !	 		 
&	 ,, ( 
  - 	
 #/"../GHOc	tELL/A c	 c	J 01		,0	BJ3-	\\	( ""00AA__##=	 ` 
 ??4==':e Ts   Houtput_metadatasbackward_idxc                 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)Nr  rf   rN  )sizedtypelayoutdevice)r   rm  r   r  r  r   r   r   r)   r   rv   r;   emptyr  r   )ra   r   r  r=  r  r?  r@  r>  r;  r<  r%  grad_insr   output_metadatar  r  r  r  s                     rB   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
base_sizesbase_stridesbase_storage_offset
view_sizesview_stridesview_storage_offsetc           	          UU R                  U5      U R                  U5      U R                  U5      U R                  U5      U R                  U5      U R                  U5      4nU R                  [        US /S-  5      $ )N   )rm  
proxy_callcopy_slices_prologue)	ra   r   r  r  r  r  r  r  rq   s	            rB   call_copy_slices_prologue2AutogradCompilerInstance.call_copy_slices_prologue  st     MM*%MM,'MM-.MM*%MM,'MM-.
 3TD6A:FFrA   needs_input_gradr   res
grad_slicec                 N    U R                  [        XX44S /[        U5      -  5      $ rE   )r  copy_slices_epiloguer  )ra   r  r   r  r  s        rB   call_copy_slices_epilogue2AutogradCompilerInstance.call_copy_slices_epilogue  s0      s7FS)**
 	
rA   c                 |    [        5          [        R                  " SS/5      sS S S 5        $ ! , (       d  f       g = f)Nr   i[rg  r   s    rB   r  'AutogradCompilerInstance.allocate_dummy  s$    (*;;9~. +**s   -
;fn_namer   r   r   c                 .    [         R                  XX45      $ )zBinds ops.fn_name = fn)rq  r   )ra   r  r   r   r   s        rB   bind_function&AutogradCompilerInstance.bind_function  s     www$6EErA   gradsrq   r  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)rq  r   r  )ra   r  r  rq   r  rh   s         rB   apply_functional)AutogradCompilerInstance.apply_functional  s)     WWWr>D>?CCrA   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      $ rE   rm  )r"  ra   s    rB   r3  5AutogradCompilerInstance.proxy_call.<locals>.<lambda>  s    t}}Q/?rA   rf   rq   r   )	r  tree_flattenr  r   r   r  r  r  r  )
ra   r   rq   r  rD  rU  
proxy_args	proxy_outr   r   s
   `         rB   r  #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.
rU  r~  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_outputsrf   r  )	rq  r   r  r  rm  r   r   r  r  )ra   rU  r~  rq   r  rh   r  new_proxy_outputss           rB   r  )AutogradCompilerInstance.validate_outputs  s|     WW'(__T]]W4Dt4DE
 NN77R 8 
 ?#s7|333$$W@rA   old_varnew_varc                     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$ Nrf   r  )rm  r   r   r;   r   r  r  )ra   r  r  old_var_proxynew_var_proxyr  r   s          rB   
accumulate#AutogradCompilerInstance.accumulate  sq    g.g.NN//UYYm-KTV 0 
	 $$&$$fX	{;rA   variabler   has_post_hooksc                     U R                   R                  S[        U R                  U5      U R                  U5      U40 S9  g r  )r   r   r   rm  )ra   r  r   r  s       rB   r\   (AutogradCompilerInstance.accumulate_grad  sF     	## h'd#
  	$ 		
rA   hookr   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 Nrf   )r   r   r   rm  )ra   r  rq   r   rK   s        rB   proxy_call_hook(AutogradCompilerInstance.proxy_call_hook$  sQ     ~~**,01Dq--"D1 
 	

 2s   Ahook_iddata_idc                     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  r  r  )ra   r  r  r  dataproxyr   s          rB   r  $AutogradCompilerInstance.unpack_hook1  s{    +++(%%g.$$# % 

 !!#$$cUUG4
rA   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)   rM   r  )ra   r   r  r   r  r  s         rB   r  (AutogradCompilerInstance.tensor_pre_hook>  s     +++($$I' % 

 )*#FI.FI((&)ug> +  +* s   &A,,
A;c           	      R   U R                   R                  S[        R                  R                  R
                  R                  X R                  X   5      40 5      n[        5          [        X   5      X'   U R                  X   /U/5        S S S 5        U$ ! , (       d  f       U$ = fr  )r   r   r;   _Cr   r7   call_cpp_tensor_pre_hooksrm  r)   rM   r  )ra   r   r  r   r  s        rB   cpp_tensor_pre_hook,AutogradCompilerInstance.cpp_tensor_pre_hookM  s     ++HH..HHmmFI./	
 )*#FI.FI((&)ug> +  +* s   '&B
B&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  )ra   r   r  r  r%  rK   s         rB   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  )ra   r~  r   r  r  r%  rK   s          rB   r  "AutogradCompilerInstance.post_hookh  s     +++(&&!	 ' 
 )*/67w!{1~wG7((: +  8 +* s   A7 A2A72A77
Binputc                 ,   [        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                  XT/5        S S S 5        U$ ! , (       d  f       W$ = f)Npost_acc_grad_hookr  )rP   r;   r   r   r  r)   rM   r  )ra   r  r  r  r  r  s         rB   r  +AutogradCompilerInstance.post_acc_grad_hookx  s     %....+++($$* % 

 )*u%&C((g6 + 
 +* 
s   B
Brd   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   r   rl   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))primsrr  N)rP   ri   r;   _ops
OpOverload	namespacer   r   r   users     rB   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)r  ro   ri   userskeysr  r	  r   metar  r  r  allvaluesverbose_logdebugr  )ra   rd   to_movehas_cuda_inputsro   r   inputs_usersfirst_getitem_idxlast_getitem_idxr   r{   is_cpu	is_scalar
node_userss                 rB   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#:		%  )
 ''	rA   r{   c                 V   [        U[        R                  R                  5      =(       a    UR                  S:H  =(       ai    UR
                  [        R                  R                  R                  R                  [        R                  R                  R                  R                  4;   $ r  )rP   r;   r   rn  rh   ri   rq  rr  sym_sizeru   	sym_numelrt  )ra   r{   s     rB   is_sym_node$AutogradCompilerInstance.is_sym_node  sn    tUXX]]+ O?*O		''++UYY^^-E-E-M-MNO	
rA   c                 t  ^ [        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S[        R                  R                  S[        4U4S j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   rk   rl   r{   r9   c                 z   > U T;   d$  U R                   S:X  a  U R                  [        ;   a  gU R                  5       $ )Nrf   T)rh   ri   _impure_targets	is_impure)r{   unpack_nodess    rB   r(  /AutogradCompilerInstance.dce.<locals>.is_impure  s2    |#?*t{{o/M>>##rA   zDCE removed %d nodes)r4   r   r   rd   rp   updater  r  r  r	  r;   r   rn  rr   ro   eliminate_dead_coder  r  )ra   r   r{   r(  beforeafterr)  s         @rB   dceAutogradCompilerInstance.dce  s    
 3=, !5!5!@!@M!@!RSGA

 12 TC+,q0000	$EHHMM 	$d 	$ T^^))//000;DNN((../0&.ArA   c                 P   / n/ n[        U R                  R                  R                  5      n[	        U5        [	        U5      nUR
                  S:X  d   eUR                  R                  5        HX  nUR                  [        R                  :X  d   eUR                  (       a  UR                  U5        MG  UR                  U5        MZ     [        5       nU H  n[        UR                  [        5      (       d   eUR                  S   U:X  d   e[        UR                  S   [         5      (       d   e[#        U5      nUR%                  UR                  S   5        UR                  S   U4Ul        M     U H(  n	U R                  R                  R'                  U	5        M*     U$ )Nr   r   rl   )rn   r   rd   ro   rm   rw   r  r  ri   rs   rt   rv   setrP   rq   r   ru   r  r   
erase_node)
ra   
used_sizesunused_sizesit
sizes_nodegetitem_nodeused_sizes_idxusednext_size_idxunuseds
             rB   remove_unused_sizes,AutogradCompilerInstance.remove_unused_sizes  s_   
 $..&&,,-R"X
')))&,,113L&&(*:*::::!!!!,/ ##L1 4 $'5Ddii////99Q<:---diilC0000/Mtyy|,1}5DI  #FNN  ++F3 # rA   r   c                 l    [        U R                  R                  U R                  R                  U5      $ rE   )r%   r   r  rd   )ra   r   s     rB   create_graph_module,AutogradCompilerInstance.create_graph_module  s%    4>>..0D0DbIIrA   c                 L  ^ ^^^ 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.                  (       a/  T R.                  R1                  T R                   R                  5        T R3                  5       mT R5                  S	T R6                   35      m[9        TS
/5        [;        STSSSS9n[<        R?                  SU5        [@        RC                  SU5        [        SU4S jS9  S[D        S[F        4   S
[F        S[F        S[F        S[F        S[F        S[H        [F        [F        4   4UU U4S jjn[K        5       RM                  S[N        RP                  " 5       ST R6                  0T RR                  SS9  T RT                  RW                  S S S 5        UT RY                  T5      4$ )Nrf   r@   rj   )tensor_metaexample_valuer#  r-  c                      SSS.$ )N&compiled_autograd_graph_pre_reorderingstringr1  r@   r@   rA   rB   r3  6AutogradCompilerInstance.end_capture.<locals>.<lambda>  s    @$!rA   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  rd   r   print_readabler   s   rB   r3  rH  !  sG    {##$$"477)=9  n%n0	 1rA   r6  rJ  r   zCompiled autograd graphT)include_deviceinclude_stridecoloredz%scompiled_autograd_graphc                  "   > T R                  SS9$ )NFrL  )rN  )rd   s   rB   r3  rH  L  s    u333GrA   )r8  compiled_fn.r   r   r   packed_inputsr9   c           	        >  Sq TR                  (       a  TR                  R                  U5        / n[        U5       Ho  u  pxUT;   d  M  US:  aK  UR	                  [
        R                  " SU5      5        [
        R                  R                  US   S5        M^  UR	                  U5        Mq     T H$  n	X   R                  5       R                  SS9X'   M&     [        5          [        TR                  5         U " XX4U5      n
TR                  (       a  TR                  R                  U
5        U
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)NTr   r  rl   )non_blockingF)in_compiled_autograd_regionr   r   r   rv   r;   r  r   maybe_mark_dynamic
pin_memoryr  _disabler   r   r   )rT  r   r   r   r   rU  filtered_sizesr   integerr   r   runtime_inputs_to_movera   r9  s              rB   runtime_wrapper=AutogradCompilerInstance.end_capture.<locals>.runtime_wrapperO  s6   4.2+##$$55f=!#$-e$4LCn,"Q;*11%++a2IJ!MM<<^B=OQRS*11': %5 0A &	 4 4 6 ; ; ; NFI 0 Z!5dgg!>%C ''((..s3 "?!>ZZ /4+ "?!>ZZZ /4+e+sI   AE6 BE6 %E";8E3	E"<	E6 
E	E"	E6 "
E0,E6 6E:r7   r   r   )-r   r   r   _exec_final_callbacks_stubr   r   rp  
create_argrm  rJ   r  rd   ro   r  r    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@  r   r   r   compiled_autograd_loginfor  r  r   r   r   r   log_event_endr   r  r  r   __exit__r   )	ra   r~  r{   fieldlazy_graph_coder_  rd   r^  r9  s	   `     @@@rB   end_capture$AutogradCompilerInstance.end_capture  s   ##&AA		
 	

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

$	4!#s(+$	4$	4 $	4 	$	4
 $	4 $	4 38_$	4 $	4L 	"#11LLN!"& 	2 	
 	%%dD$7 0 0 777rA   c                     U  Vs/ s H-  n[        U5      [        R                  R                  L d  M+  UPM/     nnU$ s  snf rE   )r  r;   r   rn  )rq   r_  ro   s      rB   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   rf   r   TF)rh   ri   rs   rt   rq   )r{   s    rB   is_placeholder'AutogradCompilerInstance.is_placeholder  sF    77m#GG&x///		!=0rA   c                    U R                   R                  R                  S[        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.
rf   rg   r   rl   N)r   rd   rp   r   rq   ri   rs   rt   maxprevrv  rv   )ra   r{   r|   	grad_noder8  args         rB   rf  6AutogradCompilerInstance.reorder_accumulate_grad_nodes  s     NN((33'; 4 
D %)IIaL$))A,	L8#3#33((--a0	z-.C))#D,?,?,D,D

4 +JJ|,
rA   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.
rf   rg   r  Nr  )	r   rd   rp   r   r   r   minr  prepend)ra   r{   
first_users      rB   rc  0AutogradCompilerInstance.delay_unpack_hook_nodes  sd     NN((33y 4 
D {{{D1]BTZZJt$
rA   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.
rf   rg   r  Nr  r   rl   )
r   rd   rp   r   r   r   rq   rz  rv  rv   )ra   r{   r8  
input_nodes       rB   rd  6AutogradCompilerInstance.reorder_tensor_pre_hook_nodes  s     NN((33y 4 
D {{{D15FF99Q<L1J*43F3Fz3R3R!!,/##D)
rA   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.
rf   rg   r  Nr  r   rl   )r   rd   rp   r   r   r   rq   rs  rh   ri   rs   rt   rv   zipremovery  rz  rv  )ra   r{   r8  input_nodes	to_remove	to_append
hook_blockr_  abr|  s              rB   re  @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
rA   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.
rf   rg   r  Nr  r   c              3      #    U  H8  nUR                   S :H  =(       a    UR                  [        R                  :H  v   M:     g7f)rf   N)rh   ri   rs   rt   r  s     rB   r   QAutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager.<locals>.<genexpr>   s7      !D ?*Nt{{h>N>N/NN!s   A A)r   rd   rp   r   r   r   rv   reversedrq   r  r  r  r  r  rm   rn   r  )ra   	pre_hooksr{   hook_getitem_noder  registered_nodert   s          rB   rg  >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  %# (rA   c                 0   / 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       H-  nUR                  S:X  d  M  UR                  [        :X  d  M+  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.
rf   rg   r  Nr  r   rl   z8post_acc_grad_hook must have corresponding acc grad node)r   rd   rp   r   r   r   rv   r  rq   r  r  r  rh   ri   r   )ra   post_acc_grad_hooksr{   r8  r|   acc_grad_noder_  s          rB   rh  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44?*qxx;O/O$%M 3
 !, J,
   .%# 2rA   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                   ["        :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.
rf   rg   r  Nr  r   rl      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)rf   r  Nr  )rh   ri   r   r   r   )r   r  r{   s     rB   r   CAutogradCompilerInstance.reorder_post_hook_nodes.<locals>.<genexpr>J  sI      - =?2 KK94 KKOOK>+M D =s   AA	Ar  )r   rd   rp   r   r   r   rv   r  rq   r  extendr  r  r  ry  rh   ri   r   rz  rv  )ra   
post_hooksr8  rz   r  input_nodes_and_usersr  r|  r|   post_acc_grad_hook_noder_  r{   s              @rB   ri  0AutogradCompilerInstance.reorder_post_hook_nodes0  s    
NN((33y 4 
D {{{D1[@d#
 Z(D99Q<L99Q<L))A,K< 1$$&!!((k):;)
%,, - $Z%5%5%:%:%< =-  * +,Cvv(SZZ;O-O XXa[
*.'j..3356A/HH	1HHLLd;?SS23/ 7 +6+22<@ ''-$))#D,?,?,D,D

<(##D)Q )rA   tc                 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rE   r  )r   rK   ra   s     rB   r   4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>o  s     51aq))1s   !)rP   r  rm  r   r;   SymIntSymFloatr  r{   r   r*   r   r   r  proxy_tensor_ProxyTensorr  )ra   r  rK   r  s   `   rB   rm  !AutogradCompilerInstance.to_proxyi  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objectsr%  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)
rP   r;   r   r6   r  r  set_node_originrv   r-   r   )ra   r  r%  r   bound_proxiesr   nodecall_indexrV  s           rB   r  0AutogradCompilerInstance.bind_objects_to_proxiesy  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$ r  )r   r&   r-   r   )ra   r  r  bw_states       rB   bind_backward_state,AutogradCompilerInstance.bind_backward_state  s@    +++  ' ?(DPrA   rV  r  pyobjc                 *   SnUbA  UR                   n[        US5      (       a$  UR                  c  [        S5      eUR                  nU U SU S3n[
        R                  " 5       R                  5       S   nUR                  SU5      n[        U5        g )N r  zThis compiled backward function was saved by AOTAutogradCache, which does not support
                    compiled autograd. Please turn off AOTAutogradCache using `TORCHINDUCTOR_AUTOGRAD_CACHE=0`.z (NodeCall )r  z:raw_stack_trace = CapturedTraceback.extract().format()[-1])
r  r   rQ   r   r  r5   extractformatreplacer1   )	ra   rV  r  r  maybe_aot_idforward_clsnew_coderaw_stack_tracenew_stack_traces	            rB   r  (AutogradCompilerInstance.set_node_origin  s     ,,K{I..22:&s   +22[k.9IK+335<<>rB)11H(
 	(rA   )r   r   r   r   r   r   r   r   r   r  r   r  r   r
  r   r  r  r   rE   )Mr   r   r   r   r   r   rb   r;   r   r	   r   r$   r   staticmethodr   r   r   r  ru   r   r  r   rr   r3   r2   r)  r9  r   r  autogradfunctionBackwardCFunctionr  r  r  r  r  r  r  r  r  r\   r   r6   r  r  r  r  r  r  r  r   r  r#  r/  r2  r=  r%   r@  rp  rn  rs  rv  rf  rc  rd  re  rg  rh  ri  rm  r  r&   r  Functionr  r   r@   rA   rB   r   r     sR   1HS#X$6 14 1C5<< C&1A Cj C 5S 5s 5} 5 5w
U\\"w
 Cyw
 eCJ'(	w

 d5c?+,w
 w
 w
 
sD&[(94;NN	Ow
r
c
 

F#F !.F  -	F
 F F #+3-F 
#FP11 #3-1  -	1
 1 ^^$$661 #+3-1 
x%s*	+1fGG SMG sm	G
 !G SMG smG !G 
%,,	G*
"4.
 
 c]	

 LL
 
%,,	
/ /
FF S#XF !	F
 F 
F	D	D }	D 		D
 "#	D 
%,,		D38$,/BJ3-	%,,	'}47JRSV-	%,,	#   

,1LL
JN
	

S#X&
/2
>A
	
3   5<<(36;>	ell	5<<(36;>	ell	x} s tELL?Q ELL)3;ELL3ITW	ell	 \\,/	ell	*/ehhnn /c /b
 
 
B, SX  DJc Jk J|83 |85#s(1CS1H+I |8| HSM d588==.A  
 UXX]] t  -*%*("+H!5F"&H7*r"# "# "( 48	#  $uS#X/0	
 
#,  )) ) //0	)
 
)rA   r   Fr   dynamicignore_active_disable_ctx)NNNc              #   P  #    U(       d  [         (       a  S v   g 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 [        R                  R                  5       (       a  SSKJn  [        R                  R                  R                  R!                  ["        R$                  " [&        U 5      U5      u  nn[)        5       (       a7  [        R                  R                  R                  R+                  [,        5        Sq[0        n[0        S-  q [        R2                  R5                  S5         S v   S S S 5        U(       d  Sq[        R                  R                  R                  R!                  XV5        [0        S-  q[0        U:X  d   S5       eg ! Sqf = f! , (       d  f       Nl= f! U(       d  Sq[        R                  R                  R                  R!                  XV5        [0        S-  q[0        U:X  d   S5       ef = f7f)	Nr   )
eval_frameforce_eagerTF)cudagraph_treesrl   zINested Compiled Autograd Contexts must return before their parent context)active_disable_ctxr  rr   torch._dynamor  _stancestance%compiled_autograd_enabled_force_eagerr;   r  is_availabletorch._inductorr  r  r   r7   set_autograd_compiler	functoolspartialr   rC   set_verbose_loggerr  compiled_autograd_enableddepthr  set_multithreading_enabled)r   r  r  r  r  prior_compilerprior_dynamicprior_depths           rB   _enabler    s    4 %););=D(((,$$5 591>8=5 zz&&((;
   22HH!!":KH' 011  22EEkR(,%KQJE^^>>uE F &05-  22HH" 
+ _+= 9>5* FE &05-  22HH" 
+ _+sQ   AH&F3 CH&0G F:G AH&3F77H&:
GG AH##H&c               #     #    [         R                  R                  R                  R	                  S S5      u  n nSq[        (       d  Sq S v   U (       a  SqSq[         R                  R                  R                  R	                  X5        g ! U (       a  SqSq[         R                  R                  R                  R	                  X5        f = f7f)NFT)r;   r  r   r7   r  r  r  )r  r  s     rB   r[  r[  
  s     
 	**@@uM !&!
(,%"**@@	
 (,%"**@@	
s   AC
B ?CA CC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  rX  r;   r  r   r7   r  r  clear_cache	itertoolsr   r   r@   rA   rB   resetr  !  sz     %****	HH&&<<T5I	HH&&99$?	HH&&224oo'OrA   r   r  r  r  r  r  r  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)r   r  r  r  r  r  r  r   r   offsetr  s              rB   r  r  .  sn     !9D##J=F
LL 6F"":VDJ
 0 0u?V?V 0 WXXrA   r  r   r  r  c                     S /[        U 5      -  n[        [        U 5      5       HA  nX   (       d  M  X%   c  M  US:X  a   X%   nUc   eUR                  U5        XU'   M;  X%   XE'   MC     U$ )Nr   )r  r  r  )r  r   r  r  grad_inputsr   to_copys          rB   r  r  B  s~     26=M9N0NK3'()v~Av&***  )!'A!$ * rA   )TTr   )u__doc__r   r  r  rs   r   collectionsr   r   collections.abcr   r   typingr   r   r	   r
   r   r;   torch.utils._pytreeutils_pytreer  torch._dispatch.pythonr   torch._dynamo.external_utilsr   r   r   r   r   torch._dynamo.sourcer   r   torch._dynamo.utilsr   r   r   r   /torch._functorch._aot_autograd.runtime_wrappersr   r   torch._guardsr   r   r   r   torch._loggingr   r    torch._prims_commonr"   torch._subclassesr#   torch._subclasses.fake_tensorr$   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.tracebackr0   r1   torch.typesr2   r3   torch.utils._ordered_setr4   torch.utils._tracebackr5   torch.fx.proxyr6   r  r   rj  r  rr   rC   rJ   r   rM   rX   rZ   r   r   rq  r	  ra  r'  r   r   ru   r   r   r  r  rX  r  r  contextmanagerr  r[  r  r  r  r  r@   rA   rB   <module>r     s         , / @ @  $ $ ;  <  M L > 6 , 4   ?   G B 2 / 4 $X *(4GH *EF$ 4D 48ELL) hu||.D 
 
c0B 
6G G^ #  #F5 5  m M "==	 //#	s 	s 	O) O)f$ "  ). % $  	  &*I#s(#II  $I  	I IX 
),- 
 
,(YU\\"Y%Y ;'Y %	Y
 %Y ;'Y %Y 
%,,Y(tnLL 
(5<<(	) 	
 
(5<<
 !rA   