
    rh                        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Zddlmc 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& dd
l'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 ddl2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z;m<Z< ddl=m>Z>m?Z? ddl@mAZA ddlBmCZC erddlDmEZE dZF e(eGd      ZH e(eGd      ZId ZJd ZKd ZLd ZM G d d      ZN G d d      ZO G d  d!      ZP eO       ZQg d"ZR eAeeej                  eg      ZT ej                         aVd# ZW G d$ d%      ZXd&aYd&aZd&a[d&a\da]ej                  d-d'e_fd(       Z`ej                  d)        Zad.d*Zbd+ Zcd, Zdy)/a  
Provides functionality for compiling PyTorch's autograd (automatic differentiation) system.

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

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

Compiled autograd can significantly improve backward pass performance by removing
Python overhead and enabling additional optimizations. It works by capturing
backward computations into an FX graph that can be compiled and optimized,
while maintaining the same semantics as eager mode autograd.
    N)Counterdefaultdict)OptionalTYPE_CHECKINGUnion)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)getArtifactLoggertrace_structuredclone_preserve_strides)FakeTensorMode)GraphModule)BackwardState)	decomposedisable_autocast_cachedisable_proxy_modes_tracingfetch_object_proxyProxyTorchDispatchModePythonKeyTracertrack_tensor_tree)
DimDynamicShapeEnv)preserve_node_metaset_stack_trace)
OrderedSet)CapturedTraceback)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_verbosec                  h    t         j                  j                  j                  j	                  d      S )Nr0   )torch_logging	_internal	log_stateis_artifact_enabled     r/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabledr:   R   s(    >>##--AA# r8   c                  ^    t         j                  j                  j                  j                  S N)r2   	_inductorconfigtriton
cudagraphsr7   r8   r9   snapshot_cudagraph_enabledrA   X   s    ??!!((333r8   c                      | t        |       S | S r<   r   )xs    r9   maybe_clonerD   \   s    }%a((Hr8   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   r2   _subclassesfake_tensorunset_fake_temporarilybw_module_fnAssertionError)CompiledFunctions    r9   extract_bw_modulerO   b   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fdZdej                  j                  fdZde	ej                     fdZde	ej                     fdZy	)

NaNCheckeraccumulate_gradc                 <    || _         g | _        i | _        g | _        y r<   )rR   params_indicesparams_to_checkoutput_names)selfrR   s     r9   __init__zNaNChecker.__init__~   s"    .)+8:')r8   graphc                 d   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                  k(  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r]   r      )nextiternodes
find_nodesr   argsrR   boolr^   operatorgetitemrF   intrT   appendnamerV   )rW   rY   inputs_nodeacc_grad_nodesoutput_nodesnode
param_nodes          r9   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-inputsc                     | j                   sy | j                  D ]W  }||   j                  }|.t        j                  |      j                         rJ d| d       ||   | j                  d| d<   Y y )Nz9Compiled autograd running under anomaly mode with inputs[zD] already having NaN gradient. This is not supported. {TURN_OFF_MSG}zinputs[])rR   rT   gradr2   isnananyrU   )rW   rs   idxrv   s       r9   prep_with_inputszNaNChecker.prep_with_inputs   s    ## && 	AC#;##D ;;t,002 OPSu UQ Q2
 6<C[D  73%q!12	Ar8   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: )rR   rU   itemsrv   r2   rw   rx   rk   RuntimeErrorjoin	enumeraterV   )rW   r{   
nan_params
inputs_strparam	nan_gradsirv   s           r9   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  r8   N)__name__
__module____qualname__rg   rX   r2   fxGraphrr   tupleTensorrz   r   r7   r8   r9   rQ   rQ   }   sP    * *AUXX^^ A0AuU\\': A u||, r8   rQ   c                       e Zd Zd Zd Zd Zy)OpNamespacec                 "    t               | _        y r<   )r   custom_function_name_counterrW   s    r9   rX   zOpNamespace.__init__   s    :A))r8   c                 ^   |r1d|z   }| j                   |   }| j                   |xx   dz  cc<   | | }t        | |      rJ t        |||      |r,t        | |t        j
                  j                               |S t        j
                  j                  fd       }t        | ||       |S )NCppNodera   c                       | i |S r<   r7   )rf   kwargsresults     r9   run_non_traceable_cpp_in_eagerz7OpNamespace.add.<locals>.run_non_traceable_cpp_in_eager   s    t.v..r8   )r   hasattrOpsetattrr2   _dynamoallow_in_graphdisable)rW   rl   fnis_custom_functionis_traceablecountr   r   s          @r9   addzOpNamespace.add   s    t#D55d;E--d3q83VE7#D4&&&D"01D$ < <V DE  ]]""/ #/ D$ >?r8   c                     t        | |      S r<   )getattr)rW   rl   s     r9   getzOpNamespace.get   s    tT""r8   N)r   r   r   rX   r   r   r7   r8   r9   r   r      s    D*#r8   r   c                       e Zd Zd Zd Zd Zy)r   c                 <    || _         || _        || _        d| _        y )Nz#torch._dynamo.compiled_autograd.ops)r   r   r   r   )rW   rl   r   r   s       r9   rX   zOp.__init__   s    "4?r8   c                 &     | j                   |i |S r<   )r   )rW   rf   r   s      r9   __call__zOp.__call__   s    tww'''r8   c                 :    | j                   dz   | j                  z   S )Nr~   )r   r   r   s    r9   __repr__zOp.__repr__   s    $t}}44r8   N)r   r   r   rX   r   r   r7   r8   r9   r   r      s    @(5r8   r   )rs   sizesscalarshookspacked_datac           	      B    t        t        t        | d d                   S )N)compiled_autograd_idframe_idframe_compile_idr   )r   s    r9   make_compile_contextr     s(    %9!%	
 r8   c                      e Zd Zd=dZd Zedefd       Zdee	j                     dee   deeeef      d	eeeeef         d
edefdZdee   fdZd Zdede	j(                  j*                  j,                  dee   fdZd Zd Zd Zd Zd Zd Zd Zd Z d Z!d Z"d Z#defd Z$dee	j                     d!edefd"Z%d# Z&d$ Z'd% Z(dee   fd&Z)d' Z*d( Z+d) Z,d* Z-d+ Z.ed,        Z/ed-        Z0d. Z1d/ Z2d0 Z3d1 Z4d2 Z5d3 Z6d4 Z7d5 Z8	 d>d	eeeeef         fd6Z9d7efd8Z:d9ed:ed;ee	j(                  jv                     fd<Z<y)?AutogradCompilerInstancereturnNc                 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)compiler_fn
contextlib	ExitStackstackcloser)   r   r   fake_tensor_moder&   	fx_tracerr%   
proxy_modehooks_proxy)rW   r   s     r9   rX   z!AutogradCompilerInstance.__init__  st    &))+
ZZ%%
! .#'"&nn!

 )*0L,0r8   c                 t    t        |t        j                        sJ | j                  j	                  ||      S )N)source)rF   r2   r   r   from_tensor)rW   rC   r   s      r9   	wrap_fakez"AutogradCompilerInstance.wrap_fake"  s2    !U\\***$$0060BBr8   c                 ,    t        t        |       |      S r<   r   )rl   ry   s     r9   r   zAutogradCompilerInstance.source&  s    [.44r8   rs   r   r   originsrR   
check_nansc                 
    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                      |\  }}	}
|d	   }	 tI        |      D ]*  \  }} jK                  | jM                  d
|            ||<   , 	  jW                  |||       tI        |      D cg c]@  \  }} jX                  j[                  | jM                  d|      t\        j^                        B }}}ta        tc        |            D cg c]  } j:                  |    }}tI        |      D ]J  \  }} j(                  je                  dtf        ||   fi       ||<   ||    j6                  |jh                  <   L  jW                  |||	      }tI        |      D ]  \  }} jM                  d|      }tk        |t
              r/ jX                  j[                  ||t\        j^                        ||<   Wtk        |tl              rL jX                  jo                   jX                  jq                  ||t\        j^                        ||      ||<   ts        dtS        |              jW                  | j<                  |
       tI        |      D ]+  \  }} j<                  |    j6                  |jh                  <   -  jB                  jE                  tu        i               jB                  jE                   jv                          jB                  jE                   jx                          jB                  jE                  t{                       jv                  jX                  J  jv                  jX                  } jB                  jE                  t"        j,                  j|                  j~                  j                  |             t        t        j                               |||fS # tN        $ r$}tQ        dtS        |       dtT               |d }~ww xY wc c}}w c c}w )Nr/   capturesra   graph_idTlog_pt2_compile_event)
tracer_clsc              3   Z   K   | ]"  }j                   j                  d |di        $ yw)placeholderr7   N)r   create_proxy).0rl   rW   s     r9   	<genexpr>z9AutogradCompilerInstance.begin_capture.<locals>.<genexpr>J  s.      
 NN''tRD
s   (+r   rs   zFound tensor of type z,, which is not supported by FakeTensorMode. r   r[   r   )r   dynamic_dim)hintr   zUnexpected scalar type: )Dr   rb   COMPILE_COUNTERidr   rj   aot_id_counterr   r   	__enter__rQ   nan_checkertimetime_nsstart_time_nsr   log_event_startr2   nnModuler   rootr   r   r&   rY   tensor_attrssymnode_proxy_lookup_graph_placeholderssizes_proxyscalars_proxyr   packed_data_proxyr   enter_contextr*   r   r   r   	ExceptionNotImplementedErrortypeTURN_OFF_MSGbind_objects_to_proxiesr   $create_unspecified_symint_and_symbolr(   DYNAMICrangelenr   r   rp   rF   floatcreate_symfloatnodecreate_unspecified_symbolrM   r!   r   r   r"   experimentalsymbolic_shapes_suppress_guardsstrr   current_compile_id)rW   rs   r   r   r   rR   r   
args_proxyinputs_originssizes_originsscalars_originsrC   ry   evalr   proxiessymintr   symvalenvs   `                    r9   begin_capturez&AutogradCompilerInstance.begin_capture*  s    	$%j1Q61'.9#.>3DGG<&&(:D:o6$!\\^!#33!"&	 	4 	
 $hhoo/$xx~~~I&(#$&!
+
	
" 	

  !3!569@6 1I	#F+ LQ"nnQHc0JKsL 	$$VZH &e,
 S NN??GS)""
 
 16c%j0AB14##A&BB"5) 	@IAv44(	GAJ 6=QZD%%fkk2	@ ..ug}M!'* 	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	
 	
C  	%'Qy0\]i\jk	
 Cs%   "8T3 <AU#U)3	U <UU compile_reasonsc                 2    sJ t        dd fd       y )Nartifactc                      dddS )N!compiled_autograd_compile_reasonsjsonrl   encodingr7   r7   r8   r9   <lambda>z>AutogradCompilerInstance.log_compile_reasons.<locals>.<lambda>  s    ;"! r8   c                       S r<   r7   )r  s   r9   r  z>AutogradCompilerInstance.log_compile_reasons.<locals>.<lambda>  s     r8   metadata_fn
payload_fn)r   )rW   r  s    `r9   log_compile_reasonsz,AutogradCompilerInstance.log_compile_reasons  s"      /	
r8   c                     j                         D cg c]  } j                  |       }}j                  }	t        |	      |	j                  |	j
                  |	j                  ~	t        j                         r(j                  D ]  }
|
j                  st        d       t        j                  j                  fd       } j                  j                  d|||g|i       d | j                   |    fd} |       } fd}t        j"                  j$                  j&                  j)                  ||      }t+        j,                   j                  |      }|S c c}w )Nz@torch.compile does not currently support higher order gradients.c                 v    t        j                  j                  j                  j                  | |g| }|S r<   )r2   
_functorch_aot_autogradruntime_wrappers_backward_prologue_functional)ctx_saved_tensorsctx_symints	flat_argsr{   maybe_subclass_metadatametadatas       r9   call_aot_bwd_prologuezOAutogradCompilerInstance.proxy_call_aot_backward.<locals>.call_aot_bwd_prologue  sB    ""00AA__!'	
 C Jr8   r[   kindr^   rf   r   c            	         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<   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-                  |      }t/        j(                  j0                  |t3        |             j(                  j5                  d|d	i       } |	|
j                        |_        ||
<   x|
j                  d
k(  r|
j*                  t         j6                  j8                  j:                  j<                  k(  r3t         j6                  j8                  j>                  j<                  |
_        j(                  j                   jA                  |
fd      } |	|
j                        |_        ||
<   <|
j                  dk(  r|
j*                  }j(                  j-                  |      }t/        j(                  j0                  |t3        |             j(                  j                   jA                  |
fd      }||_        ||
<   tC        d       |J d }|D cg c]/  }t        |t         j"                  j&                        r |       n|1 }}jE                  ||       |S c c}w c c}w c c}w c c}w )Nc                 Z    d}| j                   D ]  }|j                  dk(  r|dz  } |S  |S )Nr   r   ra   )rd   r]   )rY   num_argsrp   s      r9   
num_inputszkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.num_inputs  sA    !KK Dww-/ A   r8   r   _ra   c                     d d|  S )Naotr,  r7   )	node_namededuped_aot_ids    r9   make_uniquezlAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.make_unique  s    ^,Ai[99r8   r   r_   get_attrr7   r[   c                     |    S r<   r7   nvalue_remaps    r9   r  ziAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>4      A r8   call_modulec                     |    S r<   r7   r4  s    r9   r  ziAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>=  r7  r8   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#   r2   zerosr7   r8   r9   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)#rY   r   rj   _get_compiled_autograd_symintsr   symintsto_proxyrk   r   r   rd   r]   rp   rl   rf   rF   r2   r   Noder.   r   r^   get_fresh_qualnamer   r   r   create_nodeopsatenviewdefaultreshape	node_copyrM   r   )r+  r*  r   	pall_argsr@  r  psymintsargs_idxpoutputsr1  rp   phr5  rl   qualnamer   r>  ooutputsr0  r6  aot_idrH   ctxpbackward_statepgradsrW   s                      @@r9   copy_paste_aot_backward_graphzWAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph  s     ")//2H#(Ct8S4T)T#Uq	I  88:Gw<3s{{#333329:Qa(:H:(0InG%*  1 HK7;H ![N""6*Ad&9&9&&A%B"CC'1,': "-- *?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 '''8
 JRDE:a8a?G  (((;Nk ;> Xs   Q9QAQ4Qc                 *    t         j                  j                   fd       }t        j                  j
                  |      }j                  j                  d|t        |      i       }j                         }j                  |g|g       |S )Nc                  *    j                  |       S )N)
is_runtime)creation_fn)unwrapped_argsrZ  subclass_metas    r9   make_subclasszkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructor.<locals>.make_subclassU  s    $00J0WWr8   r[   r&  )r2   r   r   pytreetree_maprA  r   r   r   allocate_dummyr   )r]  rZ  r\  r^  punwrapped_argspoutputr_   rW   s   ``     r9   proxy_subclass_constructorzTAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructorT  s    ]]))X *X %oodmm^LOnn11$$?+	 2 G ((*F((&G9=Mr8   )make_subclass_override)r?  rA  _forward_clsrO   r$  r#  _aot_idr2   is_grad_enabledoutput_inforequires_gradr   r   r   r   r   r   r  r  r  _backward_epilogue_functionalr_  r`  )rW   pinputspsaved_tensorssaved_tensorspctxrT  maybe_backward_state_idxr  rL  rN   output_alias_infor%  rW  rR  rd  resultspresultsrS  rH   r#  r$  rU  rV  s   `    `           @@@@@@r9   proxy_call_aot_backwardz0AutogradCompilerInstance.proxy_call_aot_backward  s   ( /2.P.P.RSDMM!$SS ++%&67	#,,"2"J"J!))  "%-%9%9 !$22&Z  
	%	%	 
&	 ,, ( 
  - 	
 #/"../GHOc	 c	J 01	$ ""00AA__##=	 ` 
 ??4==':Y Ts   E6backward_idxrT  rp  c           
      x   | 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)Nrg  r[   r&  )sizedtypelayoutdevice)r   rA  r   rf  rt  r   r   r	   r#   r   rk   r2   emptyr   r   )rW   rs   output_metadatasrn  ru  rT  rp  ro  rl  rm  r  grad_insry   output_metadatary  rz  rx  rw  s                     r9   proxy_call_backwardz,AutogradCompilerInstance.proxy_call_backwardo  sf    +++---'}53##Y/22(G nn11$$" 
  2 	G """(* 	<57H(12B(C $_"*gcl.BOOD).=+tKKTvfU ((7;	< X	< Xs   #A0D&&D9c           	          || j                  |      | j                  |      | j                  |      | j                  |      | j                  |      | j                  |      f}| j                  t        |d gdz        S )N   )rA  
proxy_callcopy_slices_prologue)	rW   rs   
base_sizesbase_stridesbase_storage_offset
view_sizesview_stridesview_storage_offsetrf   s	            r9   call_copy_slices_prologuez2AutogradCompilerInstance.call_copy_slices_prologue  st     MM*%MM,'MM-.MM*%MM,'MM-.
 3TD6A:FFr8   c                 R    | j                  t        ||||fd gt        |      z        S r<   )r  copy_slices_epiloguer   )rW   needs_input_gradr   res
grad_slices        r9   call_copy_slices_epiloguez2AutogradCompilerInstance.call_copy_slices_epilogue  s2     vsJ7FS)**
 	
r8   c                 p    t               5  t        j                  ddg      cd d d        S # 1 sw Y   y xY w)Nr   i[r<  r   s    r9   ra  z'AutogradCompilerInstance.allocate_dummy  s-    (* 	/;;9~.	/ 	/ 	/s   ,5c                 2    t         j                  ||||      S )zBinds ops.fn_name = fn)rE  r   )rW   fn_namer   r   r   s        r9   bind_functionz&AutogradCompilerInstance.bind_function  s    www$6EEr8   c                 Z    t         j                  |      }| j                  ||g||      S )z:Proxies a call to ops.fn_name(grads, *args) into the graph)rE  r   r  )rW   r  gradsrf   r~  r]   s         r9   apply_functionalz)AutogradCompilerInstance.apply_functional  s)    WWWrE>D>?CCr8   c                 `    t        j                  |      \  }}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 r<   rA  )r  rW   s    r9   r  z5AutogradCompilerInstance.proxy_call.<locals>.<lambda>  s    t}}Q/? r8   r[   rf   r   )	r_  tree_flattenr`  r   r   ra  r   r   r   )
rW   r   rf   r~  r"  r,  
proxy_args	proxy_outr   r   s
   `         r9   r  z#AutogradCompilerInstance.proxy_call  s    **40	1__%?F
NN//Rj 0 
	 2AAA$%%'AA$$VE#f+DV-Wqil-WX B-Ws   B&B+
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_outputsr[   r  )	rE  r   r_  r`  rA  r   r   r   r   )rW   r,  rR  rf   r~  r]   r  new_proxy_outputss           r9   r  z)AutogradCompilerInstance.validate_outputs  s    WW'(__T]]W4Dt4DE
 NN77Rj 8 
 ?#s7|333$$W.?@r8   c                     | j                  |      }| j                  |      }| j                  j                  dt        j                  ||fi       }| j                         }| j                  |g|g       |S Nr[   r  )rA  r   r   r2   r   ra  r   )rW   old_varnew_varold_var_proxynew_var_proxyr  r   s          r9   
accumulatez#AutogradCompilerInstance.accumulate  ss    g.g.NN//UYYm]-KTV 0 
	 $$&$$fX	{;r8   c                     | j                   j                  dt        | j                  |      | j                  |      |fi        y r  )r   r   r   rA  )rW   variablerv   has_post_hookss       r9   rR   z(AutogradCompilerInstance.accumulate_grad  sD    ## h'd#
  	$ 		
r8   c                     | j                   j                  dt        |g|D cg c]  }| j                  |       c}|      S c c}w Nr[   )r   r   r
   rA  )rW   hookrf   r   rC   s        r9   proxy_call_hookz(AutogradCompilerInstance.proxy_call_hook  sM    ~~**,01q$--"1 
 	

 2s   Ac                     | j                   J | j                   |   }| j                  |   }| j                  ||d      }| j                         }| j	                  |g|g       |S )Nunpack_hook	hook_type)r   r   r  ra  r   )rW   hook_iddata_idr  dataproxyr{   s          r9   r  z$AutogradCompilerInstance.unpack_hook  s{    +++(%%g.$$# % 

 !!#$$cUUG4
r8   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#   rD   r   )rW   rs   r  r   r  r  s         r9   r  z(AutogradCompilerInstance.tensor_pre_hook  s    +++($$1I' % 

 )* 	?#F1I.F1I((&)ug>	? 	? s   )A22A<r  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   r2   _Cr   r/   call_cpp_tensor_pre_hooksrA  r#   rD   r   )rW   rs   r  r   r  s        r9   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  )rW   rs   r  r  r  rC   s         r9   r  z!AutogradCompilerInstance.pre_hook)  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  )rW   rR  rs   r  r  r  rC   s          r9   r  z"AutogradCompilerInstance.post_hook6  s    +++(&&!	 ' 
 )* 	;/67!{1~7G7((':	;  8	; s   A7A2A72A77Bc                    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  )rF   r2   r   r   r  r#   rD   r   )rW   inputr  r  r  s        r9   r  z+AutogradCompilerInstance.post_acc_grad_hookD  s    %...+++($$* % 

 )* 	9 '(E((8	9 	9 s    BB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   rs   ra   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))primsrF  N)rF   r^   r2   _ops
OpOverload	namespacer   r   r   users     r9   r   zDAutogradCompilerInstance.move_graph_nodes_to_cuda.<locals>.<genexpr>n  sw      
  #4;;

0E0EF G KK115FF
 #4;;3 ? $ > >>
s   BB	zMoving node %s from cpu to cuda)listrd   r^   userskeysr   r   r   metarz  r   rw  allvaluesverbose_logdebugr  )rW   rY   to_movehas_cuda_inputsrd   rs   inputs_usersfirst_getitem_idxlast_getitem_idxr   rp   is_cpu	is_scalar
node_userss                 r9   move_graph_nodes_to_cudaz1AutogradCompilerInstance.move_graph_nodes_to_cudaW  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#:		% ;
 ''	r8   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  )rF   r2   r   rB  r]   r^   rE  rF  sym_sizerj   	sym_numelrH  )rW   rp   s     r9   is_sym_nodez$AutogradCompilerInstance.is_sym_node  sn    tUXX]]+ O?*O		''++UYY^^-E-E-M-MNO	
r8   c                 2   t               t        | j                  j                  j	                  d            D ].  \  }}j                  |j                  j                                0 t        t              dz
  k(  sJ fd}t        | j                  j                  j                        }| j                  j                  j                  |       t        | j                  j                  j                        }t        j                  d||z
         y )Nr   r`   ra   c                 p    | v s!| j                   dk(  r| j                  t        v ry| j                         S )Nr[   T)r]   r^   _impure_targets	is_impure)rp   unpack_nodess    r9   r  z/AutogradCompilerInstance.dce.<locals>.is_impure  s2    |#?*t{{o/M>>##r8   zDCE removed %d nodes)r,   r   r   rY   re   updater  r  r   r   rd   eliminate_dead_coder  r  )rW   r   rp   r  beforeafterr  s         @r9   dcezAutogradCompilerInstance.dce  s    
 3=, !5!5!@!@M!@!RS 	3GAt

 12	3C+,q0000	$ T^^))//000;DNN((../0&5.Ar8   c                    g }g }t        | j                  j                  j                        }t	        |       t	        |      }|j
                  dk(  sJ |j                  j                         D ]P  }|j                  t        j                  k(  sJ |j                  r|j                  |       @|j                  |       R 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   ra   )rc   r   rY   rd   rb   rl   r  r  r^   rh   ri   rk   setrF   rf   r   rj   r   r   
erase_node)
rW   
used_sizesunused_sizesit
sizes_nodegetitem_nodeused_sizes_idxusednext_size_idxunuseds
             r9   remove_unused_sizesz,AutogradCompilerInstance.remove_unused_sizes  sh   
 $..&&,,-R"X
')))&,,113 	2L&&(*:*::::!!!!,/ ##L1	2 $'5 	6Ddii///99Q<:---diilC000/Mtyy|,1}5DI	6 # 	4FNN  ++F3	4 r8   c                 l    t        | j                  j                  | j                  j                  |      S r<   )r   r   r   rY   )rW   r   s     r9   create_graph_modulez,AutogradCompilerInstance.create_graph_module  s%    4>>..0D0DbIIr8   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        fd}tE               jG                  dtI        jJ                         d j6                  i jL                  d        jN                  jQ                  d d d        | jS                        fS )Nr[   r7   r_   )tensor_metaexample_valuer  r  c                      dddS )N&compiled_autograd_graph_pre_reorderingstringr  r7   r7   r8   r9   r  z6AutogradCompilerInstance.end_capture.<locals>.<lambda>  s    @$! r8   c                      t         j                  j                   j                  j                  d j                   d      j                  d      S )NCompiledAutogradPreReorderingFprint_output)r   r   r   rY   r   print_readabler   s   r9   r  z6AutogradCompilerInstance.end_capture.<locals>.<lambda>  sG    {##$$"477)=9  n%n0	 r8   r  r  rs   zCompiled autograd graphT)include_deviceinclude_stridecoloredz%scompiled_autograd_graphc                  (     j                  d      S )NFr  )r
  )rY   s   r9   r  z6AutogradCompilerInstance.end_capture.<locals>.<lambda>  s    u333G r8   )r  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  ra   )non_blockingF)in_compiled_autograd_regionr   rz   r   rk   r2   r{  r   maybe_mark_dynamic
pin_memoryr  _disabler   r   r   )compiled_fnrs   r   r   r   packed_inputsfiltered_sizesry   integerr   r{   runtime_inputs_to_moverW   r  s              r9   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%r/   r   r   )*r   r   r   _exec_final_callbacks_stubr   r   rD  
create_argrA  rA   r  rY   rd   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   rr   r  r  r   r   r   compiled_autograd_loginfor  r  r   log_event_endr   r   r   r   __exit__r   )	rW   rR  rp   fieldlazy_graph_coder  rY   r  r  s	   `     @@@r9   end_capturez$AutogradCompilerInstance.end_capture  st   ##&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> 	"#11LLN!"& 	2 	
 	%%dD$7 0 0 777r8   c                 z    | D cg c]*  }t        |      t        j                  j                  u s)|, }}|S c c}w r<   )r   r2   r   rB  )rf   r5  rd   s      r9   get_all_nodesz&AutogradCompilerInstance.get_all_nodesB  s4     !=qDGuxx}}$<== >s   *88c                     | j                   dk(  sH| j                   dk(  r:| j                  t        j                  k(  r| j                  d   j                   dk(  ryy)Nr   r[   r   TF)r]   r^   rh   ri   rf   )rp   s    r9   is_placeholderz'AutogradCompilerInstance.is_placeholderH  sF    77m#GG&x///		!=0r8   c                    | j                   j                  j                  dt              D ]  }|j                  d   |j                  d   }}d}|j
                  t        j                  k(  r|}|j                  d   }t        ||g      }||j                  usm| 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.
        r[   r\   r   ra   N)r   rY   re   r   rf   r^   rh   ri   maxprevr/  rk   )rW   rp   rq   	grad_noder  args         r9   r!  z6AutogradCompilerInstance.reorder_accumulate_grad_nodesR  s     NN((33'; 4 
 	-D %)IIaL$))A,	JL8#3#33((--a0	z9-.C$))#D,?,?,D

4 +JJ|,	-r8   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.
        r[   r\   r  Nr  )	r   rY   re   r
   r   r   minr  prepend)rW   rp   
first_users      r9   r  z0AutogradCompilerInstance.delay_unpack_hook_nodesg  si     NN((33y 4 
 	%D {{{D1]BTZZJt$	%r8   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.
        r[   r\   r  Nr  r   ra   )
r   rY   re   r
   r   r   rf   r2  r/  rk   )rW   rp   r  
input_nodes       r9   r  z6AutogradCompilerInstance.reorder_tensor_pre_hook_nodest  s     NN((33y 4 
 	*D {{{D15FF99Q<L1J*43F3Fz3R!!,/##D)	*r8   c                    | j                   j                  j                  dt              D ]Y  }|j                  j                  dd      dk7  r$|j                  d   }| j                  |j                  d         }g }g }|g}|D ]p  }|j                  dk(  s|j                  t        j                  k(  s1|j                  |j                  d          |j                  |       |j                  |       r t        ||      D ]'  \  }}	|j                  |       |j                  |	       ) t        |      }
|
|j                   us| j#                  |
      r1|
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.
        r[   r\   r  Nr  r   ra   )r   rY   re   r
   r   r   rf   r-  r]   r^   rh   ri   rk   zipremover1  r2  r/  )rW   rp   r  input_nodes	to_remove	to_append
hook_blockr5  abr4  s              r9   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	+r8   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.
        r[   r\   r  Nr  r   c              3   x   K   | ]2  }|j                   d k(  xr |j                  t        j                  k(   4 yw)r[   N)r]   r^   rh   ri   r  s     r9   r   zQAutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager.<locals>.<genexpr>  s8       ?*Nt{{h>N>N/NNs   8:)r   rY   re   r
   r   r   rk   reversedrf   r  r  r  r   r  rb   rc   r7  )rW   	pre_hooksrp   hook_getitem_noder  registered_noderi   s          r9   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#	5r8   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        k(  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.
        r[   r\   r  Nr  r   ra   z8post_acc_grad_hook must have corresponding acc grad node)r   rY   re   r
   r   r   rk   rF  rf   r  r  r  r]   r^   r   )rW   post_acc_grad_hooksrp   r  rq   acc_grad_noder5  s          r9   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,
   .%#	&r8   c           	      R   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"        k(  r|j                  d   }d}	t        |j                  j                               D ]H  }
|
j                  dk(  s|
j                   t        k(  s'|
j                  j                  dd      d
k(  sG|
}	J |	$|	j                  |       |j                         p|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.
        r[   r\   r  Nr  r   ra      c              3      K   | ]G  }|j                   d k(  r2|j                  t        k(  rj                  j	                  dd      dk(  s| I yw)r[   r  Nr  )r]   r^   r
   r   r   )r   r  rp   s     r9   r   zCAutogradCompilerInstance.reorder_post_hook_nodes.<locals>.<genexpr>  sH      -?2 KK94 KKOOK>+M -s   AAr  )r   rY   re   r
   r   r   rk   rF  rf   r   extendr  r  r  r1  r]   r^   r   r2  r/  )rW   
post_hooksr  ro   r>  input_nodes_and_usersr:  r4  rq   post_acc_grad_hook_noder5  rp   s              @r9   r$  z0AutogradCompilerInstance.reorder_post_hook_nodes  s    
NN((33y 4 
 	$D {{{D1[@d#	$ Z( (	*D99Q<L99Q<L))A,K< 1$$&!!((k):;) 	
%,, - $Z%5%5%:%:%< =- 	 +,Cvv(SZZ;O-O XXa[
*.'j..3356 4A/HH	1HHLLd;?SS23/4 +6+22<@ ''-$))#D,?,?,D

<(##D)Q(	*r8   c                 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r<   r  )r   rC   rW   s     r9   r   z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>2  s     5aq)5s   )rF   r  rA  r   r2   SymIntSymFloatr   rp   r   r$   r   r   r   proxy_tensor_ProxyTensorr  )rW   trC   rX  s   `   r9   rA  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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)
rF   r2   r   r.   r   r   set_node_originrk   r'   r   )rW   objectsr  r   bound_proxiesr   nodecall_indexr/  s           r9   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 r\  )r   r    r'   r   )rW   rc  r  bw_states       r9   bind_backward_statez,AutogradCompilerInstance.bind_backward_stateO  sB    +++  ' ?(EDPr8   r/  rb  pyobjc                    d}|;|j                   }t        |d      r#|j                  t        d      |j                  }| | d| d}t        j                         j                         d   }|j                  d|      }t        |       y )N rg  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])
rf  r   rG   r   rg  r-   extractformatreplacer+   )	rW   r/  rb  rg  maybe_aot_idforward_clsnew_coderaw_stack_tracenew_stack_traces	            r9   r_  z(AutogradCompilerInstance.set_node_originV  s     ,,K{I.22:&s   +22[k.9IK+335<<>rB)11H(
 	(r8   r   Nr<   )=r   r   r   rX   r   staticmethodr   r   r  r2   r   rj   r   r   r   r   rg   r  r  rt  autogradfunctionBackwardCFunctionr   r  r  r  ra  r  r  r  r  r  rR   r  r  r  r  r  r  r  r  r  r  r  r  r+  r-  r/  r!  r  r  r   r"  r#  r$  rA  r   rf  Functionr_  r7   r8   r9   r   r     sI   1C 5] 5 5s
U\\"s
 Cys
 eCJ'(	s

 d5c?+,s
 s
 s
j
c
@D1
 1 ^^$$661 #+3-1fG*
/
FD
		

	
# 
$u||*< 
s 
s 
&/c /b
B, DJu8n  
  -*%*("+H!5F"&H7*r"" LP)1$uS#X2G)H& )) ) //0	)r8   r   Fdynamicc              #     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 dd l}|j                  j                  j                  j                  t        j                  t        |       |      \  }}t!               r3|j                  j                  j                  j#                  t$               dat(        }t(        dz  a	 |j*                  j-                  d      5  d  d d d        |sda|j                  j                  j                  j                  ||       t(        dz  at(        |k(  sJ d       y # daw xY w# 1 sw Y   ^xY w# |sda|j                  j                  j                  j                  ||       t(        dz  at(        |k(  sJ d       w xY ww)Nr   )
eval_frameforce_eagerTFra   zINested Compiled Autograd Contexts must return before their parent context)active_disable_ctxr   rg   torch._dynamor{  _stancestance%compiled_autograd_enabled_force_eagertorch._inductor.cudagraph_treesr  r   r/   set_autograd_compiler	functoolspartialr   r:   set_verbose_loggerr  compiled_autograd_enableddepthru  set_multithreading_enabled)r   ry  ignore_active_disable_ctxr{  r2   prior_compilerprior_dynamicprior_depths           r9   _enabler  }  s    , %);=D(((,$$5 591>8=5 3
   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E" B G,E5 E)E5 AG"E&&G)E2.E5 5AG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)r2   r  r   r/   r  r  r}  )r  r  s     r9   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  r2   r  r   r/   r  r  clear_cache	itertoolsr   r   r7   r8   r9   resetr    sw     %***	HH&&<<T5I	HH&&99$?	HH&&224oo'Or8   c                     | d   }|j                  ||      }|J |j                  |       ||z
  }	|j                  |||	      }
||
|
j                  t        j
                        gS )Nr   )memory_format)new_empty_stridedcopy_
as_stridedcloner2   contiguous_format)rs   r  r  r  r  r  r  rv   r   offsetr  s              r9   r  r    sw     !9D##J=F
LL #66F"":|VDJJ
 0 0u?V?V 0 WXXr8   c                     d gt        |       z  }t        t        |             D ]5  }| |   s	||   |dk(  r|j                  ||          |||<   .||   ||<   7 |S )Nr   )r   r   r  )r  r   r  r  grad_inputsr   s         r9   r  r    sy    &3/00K3'() (A1v~Av  Q(!'A!$QA( r8   )TTrs  )e__doc__r   r  r  rh   r   collectionsr   r   typingr   r   r   r2   torch.utils._pytreeutils_pytreer_  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   torch._loggingr   r   torch._prims_commonr   torch._subclassesr   torch.fxr   %torch.fx.experimental._backward_stater    "torch.fx.experimental.proxy_tensorr!   r"   r#   r$   r%   r&   r'   %torch.fx.experimental.symbolic_shapesr(   r)   torch.fx.tracebackr*   r+   torch.utils._ordered_setr,   torch.utils._tracebackr-   torch.fx.proxyr.   r   r   r%  r  r:   rA   rD   rO   rQ   r   r   rE  r   r  r  r   r   r   r   r  r  r  r}  r  contextmanagerrg   r  r  r  r  r  r7   r8   r9   <module>r     s         , 1 1  $ $  <  E D > 6 ,   ?   G B / 4 $X *(4GH *EF4
6G G^# #:5 5 m M "==	 ")//#	Y) Y)z" "  ). % $  	 D$ D DN 
 
,(Y(r8   