
    9j                        d Z ddlZddlZddlZddlZddlZddlmZmZ ddl	m
Z
mZmZ ddlmZmZ ddlZddlmc mZ ddlmZ ddlmZmZmZmZmZ ddlmZmZ dd	l m!Z!m"Z"m#Z#m$Z$ dd
l%m&Z&m'Z' ddl(m)Z)m*Z*m+Z+m,Z, ddl-m.Z.m/Z/ ddl0m1Z1 ddl2m3Z3 ddl4m5Z5 ddl6m7Z7 ddl8m9Z9 ddl:m;Z;m<Z<m=Z=m>Z>m?Z?m@Z@mAZA ddlBmCZCmDZD ddlEmFZFmGZG ddlHmIZImJZJ ddlKmLZL ddlMmNZN erddlOmPZP dZQ e.eRd      ZS e.eRd      ZTdeUfdZVdeUfdZWdej                  dz  dej                  dz  fd ZYd!ede
d"ef   fd#ZZ G d$ d%      Z[ G d& d'      Z\ G d( d)      Z] e\       Z^g d*Z_ eLeeej                  eg      Za ej                         acd+eddefd,Ze G d- d.      Zfd/agd/ahd/aid/ajdakej                  	 	 dDd0e
d"ef   d1eUd2eUded3   fd4       Zmej                  ded3   fd5       ZndEd6Zod7eej                     d8eeJ   d9eeJ   d:eJd;eeJ   d<eeJ   d=eJdepej                     fd>Zqd?eeU   d@ej                  dAeej                  dz     dBej                  depej                  dz     f
dCZry)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)Callable	GeneratorSequence)AnyTYPE_CHECKING)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    t         j                  j                  j                  j	                  d      S )Nr6   )torch_logging	_internal	log_stateis_artifact_enabled     _/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabledrA   T   s(    >>##--AA# r?   c                  ^    t         j                  j                  j                  j                  S N)r9   	_inductorconfigtriton
cudagraphsr>   r?   r@   snapshot_cudagraph_enabledrH   Z   s    ??!!((333r?   xc                      | t        |       S | S rC   r   )rI   s    r@   maybe_clonerK   ^   s    }%a((Hr?   CompiledFunction.c                 \   t        | j                  t              r| j                  j                  S t        | j                  t              rLt
        j                  j                  j                         5  | j                  j                         cd d d        S t        d      # 1 sw Y   y xY w)NzEUnexpected Lazy Backward Compilation Info Type. Please file an issue.)
isinstance_lazy_backward_infor   	bw_moduler   r9   _subclassesfake_tensorunset_fake_temporarilybw_module_fnAssertionError)rL   s    r@   extract_bw_modulerV   d   s    ,,.M  33===	,,.S
 **AAC 	G#77DDF	G 	G S
 	
	G 	Gs   3B""B+c                       e Zd ZdeddfdZdej                  j                  ddfdZde	ej                  df   ddfd	Zd
e	ej                  df   ddfdZy)
NaNCheckeraccumulate_gradr7   Nc                 <    || _         g | _        i | _        g | _        y rC   )rY   params_indicesparams_to_checkoutput_names)selfrY   s     r@   __init__zNaNChecker.__init__   s"    .)+8:')r?   graphc                 b   t        t        |j                              }|j                  dt              }|j                  d      d   j
                  d   }| j                  t        |      k(  r| j                  | k(  sJ |D ]  }|j
                  d   }|j                  t        j                  u r.|j
                  d   |u rt        |j
                  d   t              sJ | j                  j                  |j
                  d           |D cg c]  }|j                   c}| _        y c c}w )Ncall_functionoptargetoutputrd   r      )nextiternodes
find_nodesr   argsrY   boolre   operatorgetitemrN   intr[   appendnamer]   )r^   r`   inputs_nodeacc_grad_nodesoutput_nodesnode
param_nodes          r@   prep_with_graphz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8 &&zq'9:
	; 4@@4TYY@@s   D,inputs.c                     | j                   sy | j                  D ]]  }||   j                  }|4t        j                  |      j                         rJ d| dt                ||   | j                  d| d<   _ y )Nz9Compiled autograd running under anomaly mode with inputs[z6] already having NaN gradient. This is not supported. zinputs[])rY   r[   gradr9   isnananyTURN_OFF_MSGr\   )r^   rz   idxr}   s       r@   prep_with_inputszNaNChecker.prep_with_inputs   s    ## && 	AC#;##D ;;t,002 OPSu UCCO.R2
 6<C[D  73%q!12	Ar?   outc                 6   | j                   r|rJ g }| j                  j                         D ]R  \  }}|j                  J t	        j
                  |j                        j                         sB|j                  |       T |rt        ddj                  |       d      y g }t        |      D ]G  \  }}t	        j
                  |      j                         s*|j                  | j                  |          I |rt        ddj                  |       d      y )Nz9Compiled Autograd returned NaN gradients for parameters: ,.z;Compiled Autograd returned NaN gradients for output nodes: )rY   r\   itemsr}   r9   r~   r   rr   RuntimeErrorjoin	enumerater]   )r^   r   
nan_params
inputs_strparam	nan_gradsir}   s           r@   checkzNaNChecker.check   s!   N7$&J%)%9%9%?%?%A 2!
Ezz---;;uzz*..0%%j12
 "OPSPXPXYcPdOeefg   $&I$S> ;4;;t$((*$$T%6%6q%9:; "QRURZRZ[dReQffgh  r?   )__name__
__module____qualname__rn   r_   r9   fxGraphry   tupleTensorr   r   r>   r?   r@   rX   rX      sx    * * *AUXX^^ A A0AuU\\3->'? AD A u||S01 d r?   rX   c            
       J    e Zd ZddZdededef   dededef
d	Zdedefd
Z	y)OpNamespacer7   Nc                 "    t               | _        y rC   )r   custom_function_name_counterr^   s    r@   r_   zOpNamespace.__init__   s    :A))r?   rs   fn.is_custom_functionis_traceablec                    |r1d|z   }| j                   |   }| j                   |xx   dz  cc<   | | }t        | |      rJ t        |||      |r,t        | |t        j
                  j                               |S t        j
                  j                  dt        dt        dt        ffd       }t        | ||       |S )NCppNoderh   rm   kwargsr7   c                       | i |S rC   r>   )rm   r   results     r@   run_non_traceable_cpp_in_eagerz7OpNamespace.add.<locals>.run_non_traceable_cpp_in_eager   s    t.v..r?   )	r   hasattrOpsetattrr9   _dynamoallow_in_graphdisabler   )r^   rs   r   r   r   countr   r   s          @r@   addz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$ >?r?   c                     t        | |      S rC   )getattr)r^   rs   s     r@   getzOpNamespace.get   s    tT""r?   r7   N)
r   r   r   r_   strr   r   rn   r   r   r>   r?   r@   r   r      sY    D S#X !	
  
8# # #r?   r   c                   N    e Zd Zdededef   deddfdZded	edefd
ZdefdZ	y)r   rs   r   .r   r7   Nc                 <    || _         || _        || _        d| _        y )Nz#torch._dynamo.compiled_autograd.ops)r   r   r   r   )r^   rs   r   r   s       r@   r_   zOp.__init__   s!     "4?r?   rm   r   c                 &     | j                   |i |S rC   )r   )r^   rm   r   s      r@   __call__zOp.__call__   s    tww'''r?   c                 :    | j                   dz   | j                  z   S )Nr   )r   r   r   s    r@   __repr__zOp.__repr__   s    $t}}44r?   )
r   r   r   r   r   r   rn   r_   r   r   r>   r?   r@   r   r      s[    @@%c3h/@EI@	@(c (S (S (5# 5r?   r   )rz   sizesscalarshookspacked_datacompiled_autograd_idc           	      B    t        t        t        | d d                   S )N)r   frame_idframe_compile_id)r   r   r   )r   s    r@   make_compile_contextr     s(    %9!%	
 r?   c                   |   e Zd Zdedef   ddfdZdej                  dedz  de	fdZ
ed	ed
edefd       Zdeej                     dee   deeez     deeeeef         dededeeeej                     ee   ee   f   fdZdee   ddfdZdee   deej                     deej                     dedededz  dee   dee   fdZdee   dee   deej                     dedej2                  j4                  j6                  dedz  dee   deej                  dz  df   fdZdee   d ee   d!ee   d"ed#ee   d$ee   d%edeej                     fd&Zd'ee   d(ej                  d)ee   d*ej                  deej                     f
d+Zdej                  fd,Zd-ed.edef   d/ed0edef
d1Z d-ed2ee   d3ed4ee   deej                     f
d5Z!d.edef   d3ed4ee   deej                     fd6Z"d7ed8ee   d3ed4ee   deej                     f
d9Z#d:ed;edej                  fd<Z$d=ej                  d>ej                  d?eddfd@Z%dAedef   d3edBedejL                  jN                  fdCZ(dDedEedej                  fdFZ)deej                     dDedGedeej                     fdHZ*deej                     dDedGedeej                     fdIZ+dee   dDedeej                     fdJZ,d8eej                     deej                     dDedeej                     fdKZ-dLej                  dDedeej                     fdMZ.dNejL                  j^                  dee   fdOZ0dPedefdQZ1dkdRZ2de3e   fdSZ4dTede5fdUZ6d8edeedef   ef   fdVZ7ed3ee   deejL                  jp                     fdW       Z9edPejL                  jp                  defdX       Z:dkdYZ;dkdZZ<dkd[Z=dkd\Z>dkd]Z?dkd^Z@dkd_ZAd`edefdaZB	 dldbee   dcedeeeef      dz  dee   fddZCdeedeDfdfZEdgedhediej2                  j                  dz  ddfdjZGy)mAutogradCompilerInstancecompiler_fn.r7   Nc                 4   || _         t        j                         | _        | j                  j                  | _        t               | _        t        dd| j                        | _        t               | _
        t        | j                  d      | _        d | _        y )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)r^   r   s     r@   r_   z!AutogradCompilerInstance.__init__  st    &))+
ZZ%%
! .#'"&nn!

 )*0L)-r?   rI   sourcec                 t    t        |t        j                        sJ | j                  j	                  ||      S )N)r   )rN   r9   r   r   from_tensor)r^   rI   r   s      r@   	wrap_fakez"AutogradCompilerInstance.wrap_fake-  s2    !U\\***$$0060BBr?   rs   r   c                 ,    t        t        |       |      S rC   r   )rs   r   s     r@   r   zAutogradCompilerInstance.source1  s    [.44r?   rz   r   r   originsrY   
check_nansc                 D    t         d   dxx   dz  cc<   t        t               _        t	        t
               _        t         j                         _         j                  j                          |rt        |      nd  _        t        j                          _        t               j!                  d j                  d j                  id       t"        j$                  j'                          j(                  _        t"        j,                  j/                  t0               j(                  _        i  j(                  _        i  _         fdt8        D        \  } _         _         _         _          jB                  jE                  tG                      |\  }}	}
 jB                  jE                  tI                      |d	   }	 tK        |      D ]*  \  }} jM                  | jO                  d
|            ||<   , 	  jY                  |||       tK        |      D cg c]@  \  }} jZ                  j]                  | jO                  d|      t^        j`                        B }}}tc        te        |            D cg c]  } j:                  |    }}tK        |      D ][  \  }} j(                  jg                  dth        ||   fi       ||<   tk        |t
              r@||    j6                  |jl                  <   ]  jY                  |||	      }tK        |      D ]  \  }} jO                  d|      }tk        |t
              r/ jZ                  j]                  ||t^        j`                        ||<   Wtk        |tn              rL jZ                  jq                   jZ                  js                  ||t^        j`                        ||      ||<   tu        dtU        |              jY                  | j<                  |
       tK        |      D ]+  \  }} j<                  |    j6                  |jl                  <   -  jB                  jE                  tw        i               jB                  jE                   jx                          jB                  jE                   jz                          jB                  jE                  t}                       jx                  jZ                  J  jx                  jZ                  } jB                  jE                  t"        j,                  j~                  j                  j                  |             t        t        j                               |||fS # tP        $ r$}tS        dtU        |       dtV               |d }~ww xY wc c}}w c c}w )Nr5   capturesrh   graph_idTlog_pt2_compile_event)
tracer_clsc              3   Z   K   | ]"  }j                   j                  d |di        $ yw)placeholderr>   N)r   create_proxy).0rs   r^   s     r@   	<genexpr>z9AutogradCompilerInstance.begin_capture.<locals>.<genexpr>U  s.      
 NN''tRD
s   (+r   rz   zFound tensor of type z,, which is not supported by FakeTensorMode. r   rb   r   )r   dynamic_dim)hintr   zUnexpected scalar type: )Er   ri   COMPILE_COUNTERidr   rq   aot_id_counterr   r   	__enter__rX   nan_checkertimetime_nsstart_time_nsr   log_event_startr9   nnModuler   rootr   r   r*   r`   tensor_attrssymnode_proxy_lookup_graph_placeholderssizes_proxyscalars_proxyr   packed_data_proxyr   enter_contextr.   r
   r   r   r   	ExceptionNotImplementedErrortyper   bind_objects_to_proxiesr   $create_unspecified_symint_and_symbolr,   DYNAMICrangelenr   r   rN   rw   floatcreate_symfloatnodecreate_unspecified_symbolrU   r%   r   r   r&   experimentalsymbolic_shapes_suppress_guardsr   r   current_compile_id)r^   rz   r   r   r   rY   r   
args_proxyinputs_originssizes_originsscalars_originsrI   r   eval	sym_sizesr   proxiessymintr   symvalenvs   `                     r@   begin_capturez&AutogradCompilerInstance.begin_capture5  s    	$%j1Q61'.9#.>3DGG<&&(:D:o6$!\\^!#33!"&	 	4 	
 $hhoo/$xx~~~I&(#$&!
+
	
" 	

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

  !9!;< 1I	#F+ LQ"nnQHc0JKsL 	$$VZH &e,
 S NN??GS)""
	 
 16c)n0EF14##A&FF"9- 	DIAv44(	GAJ fc*9@))&++6	D ..y'=Q!'* 	LHC[[C0F#s##~~RR&& 
 C'#~~AANN<<%$.$6$6 = 
 !  B   %%?cKK'	L( 	$$Wd.@.@/R"7+ 	KIAv595G5G5JD%%fkk2	K 	

  2/

  !6!67

  1

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

  HH!!11BB3G	

 1134	
 	
G  	%'Qy0\]i\jk	
 Gs%   8U' AV=V'	V0VVcompile_reasonsc                 2    sJ t        dd fd       y )Nartifactc                      dddS )N!compiled_autograd_compile_reasonsjsonrs   encodingr>   r>   r?   r@   <lambda>z>AutogradCompilerInstance.log_compile_reasons.<locals>.<lambda>  s    ;"! r?   c                       S rC   r>   )r  s   r@   r  z>AutogradCompilerInstance.log_compile_reasons.<locals>.<lambda>  s     r?   metadata_fn
payload_fn)r   )r^   r  s    `r@   log_compile_reasonsz,AutogradCompilerInstance.log_compile_reasons  s"      /	
r?   pinputspsaved_tensorssaved_tensorspctxctxmaybe_backward_state_idxopaque_object_indicesc                 H    j                         D cg c]  } j                  |       }	}|D 
cg c]  }
 j                  |
    }}
j                  }t	        |      |j
                  |j                  |j                  ~t        j                         r(j                  D ]  }|j                  st        d       t        j                  j                  dt        t        j                      dt        t"           dt        t$           dt        t$           dt$        f
fd       } j&                  j)                  d|||	|g|i 	      d | j                  |   dt*        t        j                      f fd
} |       }dt$        dt,        dt        t$           dt        j                   f fd}t        j.                  j0                  j2                  j5                  ||      }t7        j8                   j                  |      }|S c c}w c c}
w )Nz@torch.compile does not currently support higher order gradients.ctx_saved_tensorsctx_symintsctx_opaque_objs	flat_argsr7   c                 x    t        j                  j                  j                  j                  | ||g| }|S rC   )r9   
_functorch_aot_autogradruntime_wrappers_backward_prologue_functional)r*  r+  r,  r-  r   maybe_subclass_metadatametadatas        r@   call_aot_bwd_prologuezOAutogradCompilerInstance.proxy_call_aot_backward.<locals>.call_aot_bwd_prologue  sG     ""00AA__!' C Jr?   rb   kindre   rm   r   c            	      D	   dt         j                  j                  dt        fd}  | j                        }t        |t        d u      z
        D cg c]  }|   	 }}j                         }t        |      t        j                        k(  sJ |D cg c]  }j                  |       }}||d t        |       |j                         d}i d }t              j                     rdj                      z  j                  xx   dz  cc<   dt        dt        ffd}	j                  j                  D ]  }
|
j                  d	k(  r1||   j                  } |	|
j                         |_        ||
<   |dz  }D|
j                  d
k(  rt        |
j"                        dk(  sJ |
j"                  d   D cg c]U  }t%        |t         j                  j&                        r-t         j                  j)                  |   j*                        n|W }}|
j                  dk(  r|
j,                  }j*                  j/                  |      }t1        j*                  j2                  |t5        |             j*                  j7                  d|di       } |	|
j                         |_        ||
<   x|
j                  dk(  r|
j,                  t         j8                  j:                  j<                  j>                  u r3t         j8                  j:                  j@                  j>                  |
_        j*                  j                  jC                  |
fd      } |	|
j                         |_        ||
<   ;|
j                  dk(  r|
j,                  }j*                  j/                  |      }t1        j*                  j2                  |t5        |             j*                  j                  jC                  |
fd      }||_        ||
<   tE        d       |J dt         jF                  fd}|D cg c]/  }t%        |t         j                  j(                        r |       n|1 }}jI                  ||       |S c c}w c c}w c c}w c c}w )Nr`   r7   c                 Z    d}| j                   D ]  }|j                  dk(  r|dz  } |S  |S )Nr   r   rh   )rk   rd   )r`   num_argsrw   s      r@   
num_inputszkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.num_inputs  sA    !KK Dww-/ A   r?   r   _rh   	node_namec                     d d|  S )Naotr<  r>   )r=  deduped_aot_ids    r@   make_uniquezlAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.make_unique4  s    ^,Ai[99r?   r   rf   get_attrr>   rb   c                     |    S rC   r>   nvalue_remaps    r@   r  ziAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>T      A r?   call_modulec                     |    S rC   r>   rD  s    r@   r  ziAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>]  rG  r?   zshouldn't get herec                  t    t               5  t        j                  ddddd      cd d d        S # 1 sw Y   y xY w)Nr   {   r'   r9   zerosr>   r?   r@   dummyzfAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.dummyh  s1    02 8 ;;q!Q378 8 8s   .7)%r9   r   r   rq   r`   r   _get_compiled_autograd_symintsr   symintsto_proxyrr   r   r   rk   rd   rw   rs   rm   rN   Noder4   r   re   get_fresh_qualnamer   r   r   create_nodeopsatenviewdefaultreshape	node_copyrU   r   r   )r;  r:  r   	pall_argsrP  r  psymintsargs_idxpoutputsrA  rw   phrE  rs   qualnamer   rN  ooutputsr@  rF  aot_idrP   r&  pbackward_statepgradsr^   s                      @@r@   copy_paste_aot_backward_graphzWAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph  s    %((..  S   ")//2H#(Ct8S4T)T#Uq	I  88:Gw<3s{{#333329:Qa(:H:(0InG%*  1 HK48H ![N""6*Ad&9&9&&A%B"CC'1,':s :s : "-- *?77m+"8,11B)$))4BG(*K%MHWW(tyy>Q...
 "&1	   &a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 %-FM(.K%()=>>U*?X '''85<< 8
 JRDE:a8a?G  (((;Nm ;@ Xs   RRAR4Rsubclass_meta
is_runtimeunwrapped_argsc                 D    t         j                  j                  dt        dt        f fd       }t	        j
                  j                  |      }j                  j                  d|t        |      i       }j                         }j                  |g|g       |S )Nri  r7   c                  *    j                  |       S )N)rh  )creation_fn)ri  rh  rg  s    r@   make_subclasszkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructor.<locals>.make_subclassw  s    $00J0WWr?   rb   r6  )r9   r   r   r   pytreetree_maprQ  r   r   r   allocate_dummyr   )rg  rh  ri  rm  punwrapped_argspoutputrf   r^   s   ``     r@   proxy_subclass_constructorzTAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructort  s     ]]))Xs Xs X *X %oodmm^LOnn11$$?+ 2 G ((*F((&G9=Mr?   )make_subclass_override)rO  rQ  r   _forward_clsrV   r4  r3  _aot_idr9   is_grad_enabledoutput_inforequires_gradr   r   r   r   r   r1   r   r   r   listrn   r/  r0  r1  _backward_epilogue_functionalrn  ro  )r^   r"  r#  r$  r%  r&  r'  r(  r  r\  r   popaque_objectsrL   output_alias_infor5  rf  rb  rs  resultspresultsrc  rP   r3  r4  rd  re  s   `    `              @@@@@@r@   proxy_call_aot_backwardz0AutogradCompilerInstance.proxy_call_aot_backward  s=   * /2.P.P.RSDMM!$SS -
 S!
 
 ++%&67	#,,"2"J"J!))  "%-%9%9 !$22&Z  
	%	%	'5	!+.	 &c]	 !		
 	 
&	  ,, ( 	  - 
 #/"../GHOd	tELL/A d	 d	L 01		,0	BJ3-	\\	* ""00AA__##=	 ` 
 ??4==':{ T
s
   HHoutput_metadatasbackward_idxc           
      z   | j                   J | j                   |   }| j                  |      }	| j                  |      }
t        |j                  d      r| j	                  |	|
|||||      }n(| j
                  j                  dt        ||
g|	i       }|J t               5  g }t        |      D ]M  \  }}|||   |j                  d        |\  }}}}|j                  t        j                  ||||             O | j                  ||       d d d        t        |      S # 1 sw Y   t              S xY w)Nrv  rb   r6  )sizedtypelayoutdevice)r   rQ  r   ru  r  r   r   r   r'   r   rr   r9   emptyr   r   )r^   rz   r  r$  r  r&  r'  r(  r%  r"  r#  r  grad_insr   output_metadatar  r  r  r  s                      r@   proxy_call_backwardz,AutogradCompilerInstance.proxy_call_backward  si    +++---'}53##Y/22(%G nn11$$" 
  2 	G """(* 	<24H(12B(C $_"*gcl.BOOD).=+tKKTvfU ((7;	< X	< Xs   $A0D''D:
base_sizesbase_stridesbase_storage_offset
view_sizesview_stridesview_storage_offsetc           	          || j                  |      | j                  |      | j                  |      | j                  |      | j                  |      | j                  |      f}| j                  t        |d gdz        S )N   )rQ  
proxy_callcopy_slices_prologue)	r^   rz   r  r  r  r  r  r  rm   s	            r@   call_copy_slices_prologuez2AutogradCompilerInstance.call_copy_slices_prologue  st     MM*%MM,'MM-.MM*%MM,'MM-.
 3TD6A:FFr?   needs_input_gradr   res
grad_slicec                 R    | j                  t        ||||fd gt        |      z        S rC   )r  copy_slices_epiloguer   )r^   r  r   r  r  s        r@   call_copy_slices_epiloguez2AutogradCompilerInstance.call_copy_slices_epilogue  s4      vsJ7FS)**
 	
r?   c                 p    t               5  t        j                  ddg      cd d d        S # 1 sw Y   y xY w)Nr   i[rL  r   s    r@   rp  z'AutogradCompilerInstance.allocate_dummy  s-    (* 	/;;9~.	/ 	/ 	/s   ,5fn_namer   r   r   c                 2    t         j                  ||||      S )zBinds ops.fn_name = fn)rU  r   )r^   r  r   r   r   s        r@   bind_functionz&AutogradCompilerInstance.bind_function  s     www$6EEr?   gradsrm   r  c                 Z    t         j                  |      }| j                  ||g||      S )z:Proxies a call to ops.fn_name(grads, *args) into the graph)rU  r   r  )r^   r  r  rm   r  rd   s         r@   apply_functionalz)AutogradCompilerInstance.apply_functional  s+     WWWrE>D>?CCr?   c                 0    t        j                   fd|      } j                  j                  d||i       }|D cg c]  } j	                          }} j                  |t        t        |            D cg c]  }||   	 c}       |S c c}w c c}w )z*Proxies a call to fn(*args) into the graphc                 &    j                  |       S rC   rQ  )r  r^   s    r@   r  z5AutogradCompilerInstance.proxy_call.<locals>.<lambda>  s    t}}Q/? r?   rb   rm   r   )rn  ro  r   r   rp  r   r   r   )	r^   r   rm   r  
proxy_args	proxy_outr<  r   r   s	   `        r@   r  z#AutogradCompilerInstance.proxy_call  s     __%?F
NN//Rj 0 
	 2AAA$%%'AA$$VE#f+DV-Wqil-WX B-Ws   B9B
r<  rb  c                    t         j                  d      }t        j                  | j                  |g|      }| j
                  j                  d||i       }t        |      t        |      k(  sJ | j                  ||       |S )zEProxies a call to ops.validate_outputs(outputs, *args) into the graphvalidate_outputsrb   r  )	rU  r   rn  ro  rQ  r   r   r   r   )r^   r<  rb  rm   r  rd   r  new_proxy_outputss           r@   r  z)AutogradCompilerInstance.validate_outputs  s     WW'(__T]]W4Dt4DE
 NN77Rj 8 
 ?#s7|333$$W.?@r?   old_varnew_varc                     | j                  |      }| j                  |      }| j                  j                  dt        j                  ||fi       }| j                         }| j                  |g|g       |S Nrb   r  )rQ  r   r   r9   r   rp  r   )r^   r  r  old_var_proxynew_var_proxyr  r   s          r@   
accumulatez#AutogradCompilerInstance.accumulate  ss    g.g.NN//UYYm]-KTV 0 
	 $$&$$fX	{;r?   variabler}   has_post_hooksc                     | j                   j                  dt        | j                  |      | j                  |      |fi        y r  )r   r   r   rQ  )r^   r  r}   r  s       r@   rY   z(AutogradCompilerInstance.accumulate_grad&  sF     	## h'd#
  	$ 		
r?   hookr   c                     | j                   j                  dt        |g|D cg c]  }| j                  |       c}|      S c c}w Nrb   )r   r   r   rQ  )r^   r  rm   r   rI   s        r@   proxy_call_hookz(AutogradCompilerInstance.proxy_call_hook4  sO     ~~**,01q$--"1 
 	

 2s   Ahook_iddata_idc                     | j                   J | j                   |   }| j                  |   }| j                  ||d      }| j                         }| j	                  |g|g       |S )Nunpack_hook	hook_type)r   r   r  rp  r   )r^   r  r  r  dataproxyr   s          r@   r  z$AutogradCompilerInstance.unpack_hookA  s{    +++(%%g.$$# % 

 !!#$$cUUG4
r?   r   c                     | j                   J | j                   |   }| j                  |||   d      }t               5  t        ||         ||<   | j	                  ||   g|g       d d d        |S # 1 sw Y   |S xY w)Ntensor_pre_hookr  r   r  r'   rK   r   )r^   rz   r  r   r  r  s         r@   r  z(AutogradCompilerInstance.tensor_pre_hookN  s     +++($$1I' % 

 )* 	?#F1I.F1I((&)ug>	? 	? s   )A22A<c           	      R   | j                   j                  dt        j                  j                  j
                  j                  || j                  ||         fi       }t               5  t        ||         ||<   | j                  ||   g|g       d d d        |S # 1 sw Y   |S xY wr  )r   r   r9   _Cr   r5   call_cpp_tensor_pre_hooksrQ  r'   rK   r   )r^   rz   r  r   r  s        r@   cpp_tensor_pre_hookz,AutogradCompilerInstance.cpp_tensor_pre_hook]  s     ++HH..HHdmmF1I./	
 )* 	?#F1I.F1I((&)ug>	? 	? s   ))BB&c                    | j                   J | j                   |   }| j                  ||d      }t               5  |D cg c]  }t        |       }}| j	                  ||       d d d        |S c c}w # 1 sw Y   |S xY w)Npre_hookr  r  )r^   rz   r  r  r  rI   s         r@   r  z!AutogradCompilerInstance.pre_hookk  s    +++(&&  ' 

 )* 	:.45k!n5F5((9	:  6	: s   A6A1A61A66B c                    | j                   J | j                   |   }| j                  |||d      }t               5  |D cg c]  }t        |       }}| j	                  ||       d d d        |S c c}w # 1 sw Y   |S xY w)N	post_hookr  r  )r^   rb  rz   r  r  r  rI   s          r@   r  z"AutogradCompilerInstance.post_hookx  s     +++(&&!	 ' 
 )* 	;/67!{1~7G7((':	;  8	; s   A7A2A72A77Binputc                    t        |t        j                        sJ | j                  J | j                  |   }| j	                  ||d      }t               5  t        |      g}| j                  ||g       d d d        |S # 1 sw Y   S xY w)Npost_acc_grad_hookr  )rN   r9   r   r   r  r'   rK   r   )r^   r  r  r  r  r  s         r@   r  z+AutogradCompilerInstance.post_acc_grad_hook  s     %...+++($$* % 

 )* 	7u%&C((ug6	7 
	7 
s    BBr`   c                    i }d}t        |j                        }|d   j                  dk(  sJ |d   }t        |j                  j	                               }t        t              }||   |d   k(  sJ |t        |      z   dz
  }||   |d   k(  sJ t        |      D ]  \  }	}
|s)|
j                  d   j                  j                  dk(  rd}1|
j                  d   j                  j                  d	k(  }t        |
j                  d   j                               dk(  }|s|st        |
j                  j	                               }t        d
 |D              s|
||	<    |rn|j                         D ]B  }
t        j                  d|
       |
j                  d   j!                         |
j                  d<   D t        |j	                               S g S )NFr   rz   rh   r  cudaTcpuc              3     K   | ]  }t        |j                  t        j                  j                        xr |j                  j
                  d v xs3 t        |j                  t              xr |j                  j                     yw))primsrV  N)rN   re   r9   _ops
OpOverload	namespacer   r   r   users     r@   r   zDAutogradCompilerInstance.move_graph_nodes_to_cuda.<locals>.<genexpr>  sw      
  #4;;

0E0EF G KK115FF
 #4;;3 ? $ > >>
s   BB	zMoving node %s from cpu to cuda)rz  rk   re   userskeysr   r   r   metar  r   r  allvaluesverbose_logdebugr  )r^   r`   to_movehas_cuda_inputsrk   rz   inputs_usersfirst_getitem_idxlast_getitem_idxr   rw   is_cpu	is_scalar
node_userss                 r@   move_graph_nodes_to_cudaz1AutogradCompilerInstance.move_graph_nodes_to_cuda  s   ,.U[[!Qx(***qFLL--/0 34&'<?:::,s</@@1D%&,r*:::: . 	&GAt"tyy'7'>'>'C'Cv'M"&YYu%,,11U:FDIIe,11349I)!$**//"34
 
 !+
 
 "&GAJ-	&4 ( ;!!"CTJ#'99U#3#8#8#:		% ;
 ''	r?   rw   c                 @   t        |t        j                  j                        xry |j                  dk(  xrh |j
                  t        j                  j                  j                  j                  t        j                  j                  j                  j                  fv S r  )rN   r9   r   rR  rd   re   rU  rV  sym_sizerq   	sym_numelrX  )r^   rw   s     r@   is_sym_nodez$AutogradCompilerInstance.is_sym_node  sn    tUXX]]+ O?*O		''++UYY^^-E-E-M-MNO	
r?   c                 x   t               d }t        | j                  j                  j	                  d            D ].  \  }}j                  |j                  j                                0 |t        t              dz
  k(  sJ dt        j                  j                  dt        ffd}t        | j                  j                  j                        }| j                  j                  j                  |       t        | j                  j                  j                        }t         j#                  d||z
         y )Nr   rg   rh   rw   r7   c                 p    | v s!| j                   dk(  r| j                  t        v ry| j                         S )Nrb   T)rd   re   _impure_targets	is_impure)rw   unpack_nodess    r@   r  z/AutogradCompilerInstance.dce.<locals>.is_impure  s2    |#?*t{{o/M>>##r?   zDCE removed %d nodes)r2   r   r   r`   rl   updater  r  r   r   r9   r   rR  rn   rk   eliminate_dead_coder  r  )r^   r   rw   r  beforeafterr  s         @r@   dcezAutogradCompilerInstance.dce  s    
 3=, !5!5!@!@M!@!RS 	3GAt

 12	3C+,q0000	$EHHMM 	$d 	$ T^^))//000;DNN((../0&5.Ar?   c                     g }g }t        | j                  j                  j                        }t	        |       t	        |      }|j
                  dk(  sJ |j                  D ]O  }|j                  t        j                  u sJ |j                  r|j                  |       ?|j                  |       Q t               }|D ]  }t        |j                  t              sJ |j                  d   |k(  sJ t        |j                  d   t              sJ t!        |      }|j#                  |j                  d          |j                  d   |f|_         |D ]'  }	| j                  j                  j%                  |	       ) |S )Nr   r   rh   )rj   r   r`   rk   ri   rs   r  re   ro   rp   rr   setrN   rm   r   rq   r   r   
erase_node)
r^   
used_sizesunused_sizesit
sizes_nodegetitem_nodeused_sizes_idxusednext_size_idxunuseds
             r@   remove_unused_sizesz,AutogradCompilerInstance.remove_unused_sizes  s_   
 $..&&,,-R"X
')))&,, 	2L&&(*:*::::!!!!,/ ##L1	2 $'5 	6Ddii///99Q<:---diilC000/Mtyy|,1}5DI	6 # 	4FNN  ++F3	4 r?   r   c                 l    t        | j                  j                  | j                  j                  |      S rC   )r#   r   r   r`   )r^   r   s     r@   create_graph_modulez,AutogradCompilerInstance.create_graph_module  s%    4>>..0D0DbIIr?   c                 :     j                   j                  dt        j                  di         j                  j                           j                   j                  dd j                   j                   j                  |            fi        g t               r% j                   j                   j                         j                   j                  j                  D ]%  }dD ]  }||j                  v s|j                  |=   ' t        dd  fd        j                           j!                           j#                           j%                           j'                           j)                           j+                           j-                           j.                  r/ j.                  j1                   j                   j                          j3                          j5                  d	 j6                         t9        d
g       t;        dddd      }t<        j?                  d|       t@        jC                  d|       t        dfd       dtD        dtF        f   d
tF        dtF        dtF        dtF        dtF        dtH        tF        tF        f   f fd}tK               jM                  dtO        jP                         d j6                  i jR                  d        jT                  jW                  d d d        | jY                        fS )Nrb   r>   rf   )tensor_metaexample_valuer  r  c                      dddS )N&compiled_autograd_graph_pre_reorderingstringr  r>   r>   r?   r@   r  z6AutogradCompilerInstance.end_capture.<locals>.<lambda>.  s    @$! r?   c                      t         j                  j                   j                  j                  d j                   d      j                  d      S )NCompiledAutogradPreReorderingFprint_output)r#   r   r   r`   r   print_readabler   s   r@   r  z6AutogradCompilerInstance.end_capture.<locals>.<lambda>2  sG    {##$$"477)=9  n%n0	 r?   r  r  rz   zCompiled autograd graphT)include_deviceinclude_stridecoloredz%scompiled_autograd_graphc                  (     j                  d      S )NFr  )r  )r`   s   r@   r  z6AutogradCompilerInstance.end_capture.<locals>.<lambda>]  s    u333G r?   )r   compiled_fn.r   r   r   packed_inputsr7   c           	         	 da j                  rj                  j                  |       g }t        |      D ]i  \  }}|v s|dkD  rI|j	                  t        j                  d|             t
        j                  j                  |d   d       Y|j	                  |       k D ](  }	||	   j                         j                  d      ||	<   * t               5  t        j                        5   | |||||      }
j                  rj                  j                  |
       |
cd d d        cd d d        da S # 1 sw Y   nxY wd d d        da y # 1 sw Y   da y xY w# da w xY w)NTr   r  rh   )non_blockingF)in_compiled_autograd_regionr   r   r   rr   r9   r  r   maybe_mark_dynamic
pin_memoryr  _disabler   r   r   )r  rz   r   r   r   r  filtered_sizesr   integerr   r   runtime_inputs_to_mover^   r  s              r@   runtime_wrapperz=AutogradCompilerInstance.end_capture.<locals>.runtime_wrapper`  sf   4.2+##$$55f=!#$-e$4 ;LCn,"Q;*11%++a2IJ!MM<<^B=OQRS*11':; 0 OA &q	 4 4 6 ; ; ; NF1IO Z !5dgg!> %C ''((..s3   /4+   /4+ /4+e+sO   A E! BE! E25D<'	E0	E! <E	EE! EE! E! !E%r5   r   r   )-r   r   r   _exec_final_callbacks_stubr   r   rT  
create_argrQ  rH   r  r`   rk   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   ry   r  r  r   r   r   compiled_autograd_loginfor  r  r   r   r   r   log_event_endr   r   r   r   __exit__r   )	r^   rb  rw   fieldlazy_graph_coder%  r`   r$  r  s	   `     @@@r@   end_capturez$AutogradCompilerInstance.end_capture  s   ##&AA		
 	

""^^&&t}}W'=>@		
 -/%'%)%B%B4>>CWCW%X"
 NN((.. 	)D@ )DII%		%()	)
 	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_$	4L 	"#11LLN!"& 	2 	
 	%%dD$7 0 0 777r?   c                 z    | D cg c]*  }t        |      t        j                  j                  u s)|, }}|S c c}w rC   )r   r9   r   rR  )rm   rE  rk   s      r@   get_all_nodesz&AutogradCompilerInstance.get_all_nodes  s4     !=qDGuxx}}$<== >s   *88c                     | j                   dk(  sG| j                   dk(  r9| j                  t        j                  u r| j                  d   j                   dk(  ryy)Nr   rb   r   TF)rd   re   ro   rp   rm   )rw   s    r@   is_placeholderz'AutogradCompilerInstance.is_placeholder  sF    77m#GG&x///		!=0r?   c                    | j                   j                  j                  dt              D ]  }|j                  d   |j                  d   }}d}|j
                  t        j                  u r|}|j                  d   }t        ||g      }||j                  usl| j                  |      r~|j                  |       ||j                  |        y)a  
        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.
        rb   rc   r   rh   N)r   r`   rl   r   rm   re   ro   rp   maxprevr9  rr   )r^   rw   rx   	grad_noder  args         r@   r+  z6AutogradCompilerInstance.reorder_accumulate_grad_nodes  s     NN((33'; 4 
 	-D %)IIaL$))A,	JL8#3#33((--a0	z9-.C$))#D,?,?,D

4 +JJ|,	-r?   c                     | j                   j                  j                  dt              D ]H  }|j                  j                  dd      dk7  r#t        |j                        }|j                  |       J y)zp
        We can delay unpack hooks until they are needed, even later than in the eager autograd engine.
        rb   rc   r  Nr  )	r   r`   rl   r   r   r   minr  prepend)r^   rw   
first_users      r@   r(  z0AutogradCompilerInstance.delay_unpack_hook_nodes  si     NN((33y 4 
 	%D {{{D1]BTZZJt$	%r?   c                 f   | j                   j                  j                  dt              D ]  }|j                  j                  dd      dk7  r#|j                  d   }|j                  d   }||j                  usP| j                  |      rb|j                  |       |j                  |        y)a  
        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.
        rb   rc   r  Nr  r   rh   )
r   r`   rl   r   r   r   rm   r<  r9  rr   )r^   rw   r  
input_nodes       r@   r)  z6AutogradCompilerInstance.reorder_tensor_pre_hook_nodes  s     NN((33y 4 
 	*D {{{D15FF99Q<L1J*43F3Fz3R!!,/##D)	*r?   c                    | j                   j                  j                  dt              D ]X  }|j                  j                  dd      dk7  r$|j                  d   }| j                  |j                  d         }g }g }|g}|D ]o  }|j                  dk(  s|j                  t        j                  u s0|j                  |j                  d          |j                  |       |j                  |       q t        ||      D ]'  \  }}	|j                  |       |j                  |	       ) t        |      }
|
|j                   us| j#                  |
      r0|
j                  |       |D ]  }|j                  |        [ y)a  
        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.
        rb   rc   r  Nr  r   rh   )r   r`   rl   r   r   r   rm   r7  rd   re   ro   rp   rr   zipremover;  r<  r9  )r^   rw   r  input_nodes	to_remove	to_append
hook_blockrE  abr>  s              r@   r*  z@AutogradCompilerInstance.reorder_pre_hook_nodes_to_schedule_asap  sh    NN((33y 4 
 	+D {{{D1Z?99Q<L,,TYYq\:KIIJ  )44?*qxx8;K;K/K$$QVVAY/$$Q'%%a(	)
 Iy1 &1""1%""1%& k"C$))#D,?,?,D

<(# +A ''*+3	+r?   c                    g }| j                   j                  j                  dt              D ]3  }|j                  j                  dd      dk7  r#|j                  |       5 t        |      D ]  }|j                  d   }t        |j                  j                               }t        |      dk(  rDt        d |D              sJ t        t        |d   j                  j                                     }||j                  us|j!                  |       |j!                  |       |D ]  }|j!                  |         y)a%  
        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.
        rb   rc   r  Nr  r   c              3   v   K   | ]1  }|j                   d k(  xr |j                  t        j                  u  3 yw)rb   N)rd   re   ro   rp   r  s     r@   r   zQAutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager.<locals>.<genexpr>  s8       ?*Nt{{h>N>N/NNs   79)r   r`   rl   r   r   r   rr   reversedrm   rz  r  r  r   r  ri   rj   rA  )r^   	pre_hooksrw   hook_getitem_noder  registered_noderp   s          r@   r,  z>AutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager  s1    	NN((33y 4 
 	#D {{{D1Z?T"	# Y' 	5D $		!*+E5zQ  !    #4a(;(;(=#>?Odii/''(9:''-$ 5G#++G45#	5r?   c                    g }| j                   j                  j                  dt              D ]3  }|j                  j                  dd      dk7  r#|j                  |       5 t        |      D ]  }|j                  d   }|j                  d   }d}t        |j                  j                               D ](  }|j                  dk(  s|j                  t        u s&|} n |J d       |j                  |       |j                  |        y)	a  
        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.
        rb   rc   r  Nr  r   rh   z8post_acc_grad_hook must have corresponding acc grad node)r   r`   rl   r   r   r   rr   rP  rm   rz  r  r  rd   re   r   )r^   post_acc_grad_hooksrw   r  rx   acc_grad_noderE  s          r@   r-  z9AutogradCompilerInstance.reorder_post_acc_grad_hook_nodes  s    !NN((33y 4 
 	-D {{{D15II&&t,	- 01 	&D99Q<L1J !M***//12 44?*qxx;O/O$%M
 !, J,
   .%#	&r?   c           	      N   g }| j                   j                  j                  dt              D ]3  j                  j                  dd      dk7  r#|j                         5 t        |      D ]  j                  d   }j                  d   }j                  d   }t        |      dkD  r@g }|j                  t        |             |D ]=  }|j                  fd	t        |j                  j                               D               ? t        |      }|j                  dk(  r|j                   t"        u r|j                  d   }d}	t        |j                  j                               D ]G  }
|
j                  dk(  s|
j                   t        u s&|
j                  j                  dd      d
k(  sF|
}	I |	$|	j                  |       |j                         n|j$                  us~| j'                  |      r|j                  |       |j                          y)a  
        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.
        rb   rc   r  Nr  r   rh      c              3      K   | ]F  }|j                   d k(  r1|j                  t        u rj                  j	                  dd      dk(  s| H yw)rb   r  Nr  )rd   re   r   r   r   )r   r  rw   s     r@   r   zCAutogradCompilerInstance.reorder_post_hook_nodes.<locals>.<genexpr>\  sH      -?2 KK94 KKOOK>+M -s   AAr  )r   r`   rl   r   r   r   rr   rP  rm   r   extendrz  r  r  r;  rd   re   r   r<  r9  )r^   
post_hooksr  rv   rH  input_nodes_and_usersrD  r>  rx   post_acc_grad_hook_noderE  rw   s              @r@   r.  z0AutogradCompilerInstance.reorder_post_hook_nodesA  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6 4A/HH	1HHLLd;?SS23/4 +6+22<@ ''-$))#D,?,?,D

<(##D)S)	*r?   tc                 0    |y t        |t              r|D cg c]  } j                  |       c}S t        |t              rt         fd|D              S t        |t        j
                  t        j                  f      r j                  |j                     S t        |t        j                        s|S t         j                  |      }t        |t        j                  j                  j                  j                        sJ |j                   S c c}w )Nc              3   @   K   | ]  }j                  |        y wrC   r  )r   rI   r^   s     r@   r   z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>  s     5aq)5s   )rN   rz  rQ  r   r9   SymIntSymFloatr   rw   r   r(   r   r   r  proxy_tensor_ProxyTensorr  )r^   r^  rI   rc  s   `   r@   rQ  z!AutogradCompilerInstance.to_proxy{  s    9a./0DMM!$00a51555a%,,78,,QVV44!U\\*H)$..!<,(=(=(J(J(W(WXXX!!! 1s   Dobjectsr  c                    t        |t        j                  j                        r|rft	        |      t	        |      k(  sJ g }t        t	        |            D ]1  }||   \  }}| j                  ||d        |j                  ||          3 |}n$t        t	        |            D cg c]  }||   	 }}t	        |      t	        |      k(  sJ t        ||d | j                         |S c c}w N)constanttracer)
rN   r9   r   r4   r   r   set_node_originrr   r+   r   )r^   re  r  r   bound_proxiesr   nodecall_indexr=  s           r@   r   z0AutogradCompilerInstance.bind_objects_to_proxies  s     guxx~~.7|s7|333 "s7|, 5A07
-NI((NDI!((45 (/4S\/BC!71:CC7|s7|+++'7T$..Q	 Ds   #C%indexc                     | j                   J | j                   |   }t               }t        ||d | j                         |S rg  )r   r$   r+   r   )r^   rm  r  bw_states       r@   bind_backward_statez,AutogradCompilerInstance.bind_backward_state  sB    +++  ' ?(EDPr?   r=  rl  pyobjc                    d}|;|j                   }t        |d      r#|j                  t        d      |j                  }| | d| d}t        j                         j                         d   }|j                  d|      }t        |       y )N rv  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])
ru  r   rO   r   rv  r3   extractformatreplacer/   )	r^   r=  rl  rq  maybe_aot_idforward_clsnew_coderaw_stack_tracenew_stack_traces	            r@   rj  z(AutogradCompilerInstance.set_node_origin  s     ,,K{I.22:&s   +22[k.9IK+335<<>rB)11H(
 	(r?   r   rC   )Hr   r   r   r   r   r_   r9   r   r   r"   r   staticmethodr   r   r   rz  rq   r1   r0   r   rn   r  r!  r   r  autogradfunctionBackwardCFunctionr  r  r  rp  r  r  r  r  r  rY   r   r4   r  r  r  r  r  r  r  r   r  r  r  r  r  r#   r  r5  rR  r7  r9  r+  r(  r)  r*  r,  r-  r.  rQ  r   r$   rp  Functionrj  r>   r?   r@   r   r     sj   .HS#X$6 .4 .C5<< C$ C: C 5S 5s 5} 5 5y
U\\"y
 Cyy
 kM12	y

 d5c?+,y
 y
 y
 
sD&[(94;NN	Oy
v
c
 

R#R !.R  -	R
 R R #&*R  $CyR 
#Rh33 #3-3  -	3
 3 ^^$$663 #&*3  $Cy3 
u||d"C'	(3j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8*t"# "# "( 15	#  eCHo&-	
 
#,  )) ) ~~&&-	)
 
)r?   r   Fr   dynamicignore_active_disable_ctx)NNNc              #     K   |st         rd  y |rt        |      t        u sJ ddlm} |j
                  j                  dk(  r
da	 d  day t        j                  j                         rddlm} t        j                  j                  j                  j!                  t#        j$                  t&        |       |      \  }}t)               r7t        j                  j                  j                  j+                  t,               dat0        }t0        dz  a	 t        j2                  j5                  d      5  d  d d d        |sdat        j                  j                  j                  j!                  ||       t0        dz  at0        |k(  sJ d       y # daw xY w# 1 sw Y   bxY w# |sdat        j                  j                  j                  j!                  ||       t0        dz  at0        |k(  sJ d       w xY ww)	Nr   )
eval_frameforce_eagerTF)cudagraph_treesrh   zINested Compiled Autograd Contexts must return before their parent context)active_disable_ctxr   rn   torch._dynamor  _stancestance%compiled_autograd_enabled_force_eagerr9   r  is_availabletorch._inductorr  r  r   r5   set_autograd_compiler	functoolspartialr   rA   set_verbose_loggerr  compiled_autograd_enableddepthr~  set_multithreading_enabled)r   r  r  r  r  prior_compilerprior_dynamicprior_depths           r@   _enabler    s    4 %);=D(((,$$5 591>8=5 zz&&(;
   22HH!!":KH' 01  22EEkR(,%KQJE^^>>uE  &05-  22HH"M 
+ _+= 9>5*  &05-  22HH"M 
+ _+sQ   AG;F CG;F% 3F8F%  AG;FG;F"F% %AG88G;c               #     K   t         j                  j                  j                  j	                  d d      \  } }dat        sda	 d  | rdadat         j                  j                  j                  j	                  | |       y # | rdadat         j                  j                  j                  j	                  | |       w xY ww)NFT)r9   r  r   r5   r  r  r  )r  r  s     r@   r!  r!    s     
 	**@@uM !&!
(,%"**@@M	
 (,%"**@@M	
s   ACB 	;C<C  Cc                  r   da t        rJ t        j                  j                  j
                  j                  d d       t        j                  j                  j
                  j                  d        t        j                  j                  j
                  j                          t        j                         ay )NF)r  r  r9   r  r   r5   r  r  clear_cache	itertoolsr   r   r>   r?   r@   resetr  3  sw     %***	HH&&<<T5I	HH&&99$?	HH&&224oo'Or?   rz   r  r  r  r  r  r  c                     | d   }|j                  ||      }|J |j                  |       ||z
  }	|j                  |||	      }
||
|
j                  t        j
                        gS )Nr   )memory_format)new_empty_stridedcopy_
as_stridedcloner9   contiguous_format)rz   r  r  r  r  r  r  r}   r   offsetr  s              r@   r  r  @  sw     !9D##J=F
LL #66F"":|VDJJ
 0 0u?V?V 0 WXXr?   r  r   r  r  c                     d gt        |       z  }t        t        |             D ];  }| |   s	||   |dk(  r ||   }|J |j                  |       |||<   4||   ||<   = |S )Nr   )r   r   r  )r  r   r  r  grad_inputsr   to_copys          r@   r  r  T  s     /3Vc:J6K-KK3'() 
(A1v~Ava&***  )!'A!$QA
( r?   )TTr   )s__doc__r   r  r  ro   r   collectionsr   r   collections.abcr   r   r   typingr   r	   r9   torch.utils._pytreeutils_pytreern  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.tracebackr.   r/   torch.typesr0   r1   torch.utils._ordered_setr2   torch.utils._tracebackr3   torch.fx.proxyr4   r   r   r/  r  rn   rA   rH   r   rK   rV   rX   r   r   rU  r   r&  r  r   r   rq   r   r   r  r  r  r  r  contextmanagerr  r!  r  rz  r  r  r>   r?   r@   <module>r     s         , 9 9 %  $ $ ;  <  M L > 6 , 4   ?   G B 2 / 4 $X *(4GH *EF$ 4D 45<<$& 5<<$+> 
 
c0B 
6G G^!# !#H5 5  m M "==	 ")//#	s 	s 	`) `)H% "  ). % $  	  &*I#s(#II  $I  	I IX 
),- 
 
,(YU\\"Y%Y ;'Y %	Y
 %Y ;'Y %Y 
%,,Y(tnLL 
%,,%	& 	
 
%,,
r?   