
    a,j                        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jX        dz  dejX        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jb                    acd+eddefd,Ze G d- d.          Zfd/agd/ahd/aid/ajdakejl        	 	 dEd1e
d"ef         d2eUd3eUded4         fd5            Zmejl        ded4         fd6            ZndFd7Zod8eejX                 d9eeJ         d:eeJ         d;eJd<eeJ         d=eeJ         d>eJdepejX                 fd?Zqd@eeU         dAejX        dBeejX        dz           dCejX        depejX        dz           f
dDZrdS )Ga  
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                  T    t           j        j        j                            d          S )Nr6   )torch_logging	_internal	log_stateis_artifact_enabled     _/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabledrA   T   s%    >#-AA#  r?   c                  8    t           j        j        j        j        S N)r9   	_inductorconfigtriton
cudagraphsr>   r?   r@   snapshot_cudagraph_enabledrH   Z   s    ?!(33r?   xc                 (    | t          |           S | S rC   r   )rI   s    r@   maybe_clonerK   ^   s    }%a(((Hr?   CompiledFunction.c                 L   t          | j        t                    r| j        j        S t          | j        t                    rVt
          j        j                                        5  | j        	                                cd d d            S # 1 swxY w Y   d S t          d          )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==	,.S
 
 
 *AACC 	G 	G#7DDFF	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G 	G S
 
 	
s   $B

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dS )
NaNCheckeraccumulate_gradr7   Nc                 >    || _         g | _        i | _        g | _        d S rC   )rY   params_indicesparams_to_checkoutput_names)selfrY   s     r@   __init__zNaNChecker.__init__   s(    .)+8:')r?   graphc                 2   t          t          |j                            }|                    dt                    }|                    d          d         j        d         }| j        t          |          k    r| j        | k    sJ |D ]x}|j        d         }|j        t          j
        u r/|j        d         |u r t          |j        d         t                    sJ | j                            |j        d                    yd |D             | _        d S )Ncall_functionoptargetoutputrd   r      c                     g | ]	}|j         
S r>   )name).0nodes     r@   
<listcomp>z.NaNChecker.prep_with_graph.<locals>.<listcomp>   s    @@@4TY@@@r?   )nextiternodes
find_nodesr   argsrY   boolre   operatorgetitemrN   intr[   appendr]   )r^   r`   inputs_nodeacc_grad_nodesoutput_nodesrl   
param_nodes          r@   prep_with_graphzNaNChecker.prep_with_graph   s7   4,,--))'; * 
 
 ''8'44Q7<Q?#t(
 (
 
 
"<'78889 # 
	; 
	;D1J
 !X%555OA&+55zq1377 658 &&zq'9::::@@<@@@r?   inputs.c                     | j         sd S | j        D ]`}||         j        }|;t          j        |                                          rJ d| dt                       ||         | j        d| d<   ad S )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^   r}   idxr   s       r@   prep_with_inputszNaNChecker.prep_with_inputs   s    # 	F & 	A 	AC#;#D ;t,,0022  RPS R RCOR R 2
 6<C[D !13!1!1!122	A 	Ar?   outc                 N   | j         r|rJ g }| j                                        D ]N\  }}|j        J t	          j        |j                                                  r|                    |           O|r&t          dd	                    |           d          d S g }t          |          D ]K\  }}t	          j        |                                          r |                    | j        |                    L|r&t          dd	                    |           d          d S )Nz9Compiled Autograd returned NaN gradients for parameters: ,.z;Compiled Autograd returned NaN gradients for output nodes: )rY   r\   itemsr   r9   r   r   rw   RuntimeErrorjoin	enumerater]   )r^   r   
nan_params
inputs_strparam	nan_gradsir   s           r@   checkzNaNChecker.check   s`    	NN7$&J%)%9%?%?%A%A 2 2!
Ez---;uz**..00 2%%j111 "gPSPXPXYcPdPdggg    $&I$S>> ; ;4;t$$((** ;$$T%6q%9::: "hRURZRZ[dReRehhh   r?   )__name__
__module____qualname__rs   r_   r9   fxGraphr|   tupleTensorr   r   r>   r?   r@   rX   rX      s        * * * * * *AUX^ A A A A A0AuU\3->'? AD A A A A u|S01 d      r?   rX   c            
       R    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	dS )OpNamespacer7   Nc                 ,    t                      | _        d S rC   )r   custom_function_name_counterr^   s    r@   r_   zOpNamespace.__init__   s    :A)))))r?   rj   fn.is_custom_functionis_traceablec                    |r-d|z   }| j         |         }| j         |xx         dz  cc<   | | }t          | |          rJ t          |||          |r/t          | |t          j                                                 nCt          j        j        dt          dt          dt          ffd            }t          | ||           |S )NCppNoderh   rr   kwargsr7   c                       | i |S rC   r>   )rr   r   results     r@   run_non_traceable_cpp_in_eagerz7OpNamespace.add.<locals>.run_non_traceable_cpp_in_eager   s    vt.v...r?   )	r   hasattrOpsetattrr9   _dynamoallow_in_graphdisabler   )r^   rj   r   r   r   countr   r   s          @r@   addzOpNamespace.add   s     	$t#D5d;E-d333q8333#E##D4&&&&&D"011 
	@D$ < <V D DEEEE ]"/c /S /S / / / / / #"/ D$ >???r?   c                 "    t          | |          S rC   )getattr)r^   rj   s     r@   getzOpNamespace.get   s    tT"""r?   r7   N)
r   r   r   r_   strr   r   rs   r   r   r>   r?   r@   r   r      s        D D D D S#X !	
  
   8# # # # # # # #r?   r   c                   V    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	dS )r   rj   r   .r   r7   Nc                 >    || _         || _        || _        d| _        d S )Nz#torch._dynamo.compiled_autograd.ops)r   r   r   r   )r^   rj   r   r   s       r@   r_   zOp.__init__   s%     "4?r?   rr   r   c                      | j         |i |S rC   )r   )r^   rr   r   s      r@   __call__zOp.__call__   s    tw''''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   rs   r_   r   r   r>   r?   r@   r   r      s        @@%c3h/@EI@	@ @ @ @(c (S (S ( ( ( (5# 5 5 5 5 5 5r?   r   )r}   sizesscalarshookspacked_datacompiled_autograd_idc           	      Z    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j        j        j        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j&        j'        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j&        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j&        j8                 fdW            Z9edPej&        j8        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j        jF        dz  ddfdjZGdS )mAutogradCompilerInstancecompiler_fn.r7   Nc                 ,   || _         t          j                    | _        | j        j        | _        t                      | _        t          dd| j                  | _        t                      | _
        t          | j
        d          | _        d | _        d S )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__  s    &)++
Z%
! .#'"&n!
 !
 !

 )**0LL)-r?   rI   sourcec                 r    t          |t          j                  sJ | j                            ||          S )N)r   )rN   r9   r   r   from_tensor)r^   rI   r   s      r@   	wrap_fakez"AutogradCompilerInstance.wrap_fake-  s6    !U\*****$0060BBBr?   rj   r   c                 <    t          t          |           |          S rC   r   )rj   r   s     r@   r   zAutogradCompilerInstance.source1  s    [..444r?   r}   r   r   originsrY   
check_nansc                 
    t           d         dxx         dz  cc<   t          t                     _        t	          t
                     _        t           j                   _         j        	                                 |rt          |          nd  _        t          j                     _        t                                          d j        d j        id           t"          j                                         j        _        t"          j                            t0                     j        _        i  j        _        i  _         fdt8          D             \  } _         _         _         _          j!        "                    tG                                 |\  }}	}
 j!        "                    tI                                 |d	         }	 tK          |          D ]2\  }} &                    | '                    d
|                    ||<   3n:# tP          $ r-}tS          dtU          |           dtV                     |d }~ww xY w ,                    |||            fdtK          |          D             } fdt[          t]          |                    D             }tK          |          D ][\  }} j        /                    dt`          ||         fi           ||<   tc          |t
                    s||          j        |j2        <   \ ,                    |||	          }tK          |          D ]\  }} '                    d|          }tc          |t
                    r* j3        4                    ||tj          j6                  ||<   Ztc          |tn                    rF j3        8                     j3        9                    ||tj          j6                  ||          ||<   tu          dtU          |                     ,                    | j        |
           tK          |          D ]\  }} j        |          j        |j2        <     j!        "                    tw          i                       j!        "                     j<                    j!        "                     j=                    j!        "                    t}                                  j<        j3        J  j<        j3        } j!        "                    t"          j        j?        j@        A                    |                     t          t          jD                              |||fS )Nr5   capturesrh   graph_idTlog_pt2_compile_event)
tracer_clsc              3   R   K   | ]!}j                             d |di           V  "dS )placeholderr>   N)r   create_proxy)rk   rj   r^   s     r@   	<genexpr>z9AutogradCompilerInstance.begin_capture.<locals>.<genexpr>U  sM       
 
 N''tRDD
 
 
 
 
 
r?   r   r}   zFound tensor of type z,, which is not supported by FakeTensorMode. c           	          g | ]?\  }}j                             |                    d |          t          j                  @S )r   )r   $create_unspecified_symint_and_symbolr   r,   DYNAMIC)rk   r   valr^   s      r@   rm   z:AutogradCompilerInstance.begin_capture.<locals>.<listcomp>m  s[     
 
 
 S N??GS))" 
 
 
r?   c                 *    g | ]}j         |         S r>   )sizes_proxy)rk   r   r^   s     r@   rm   z:AutogradCompilerInstance.begin_capture.<locals>.<listcomp>y  s!    FFF14#A&FFFr?   rb   r   )r   dynamic_dim)hintr   zUnexpected scalar type: )Er   rn   COMPILE_COUNTERidr   rv   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_placeholdersr   scalars_proxyr   packed_data_proxyr   enter_contextr.   r
   r   r   r   	ExceptionNotImplementedErrortyper   bind_objects_to_proxiesrangelenr   r   rN   rl   r   r   r,   r   floatcreate_symfloatnodecreate_unspecified_symbolrU   r%   r   r   r&   experimentalsymbolic_shapes_suppress_guardsr   r   current_compile_id)r^   r}   r   r   r   rY   r   
args_proxyinputs_originssizes_originsscalars_originsrI   r   e	sym_sizesproxiesr   symintr   r   symvalenvs   `                     r@   begin_capturez&AutogradCompilerInstance.begin_capture5  sl    	$%j111Q6111''.9#.>.>3DG<<&&(((:DN:o666$!\^^!##33!"&	 	4 	
 	
 	
 $hoo//$x~~~II&(#$&!
 
 
 
+
 
 
	
" 	
  !3!5!56669@6 	
  !9!;!;<<< 1I	#F++ L LQ"nnQHc0J0JKKsL 	 	 	%kQkk]ikk 	 	$$VZHHH
 
 
 
 &e,,
 
 
	 GFFFc)nn0E0EFFF"9-- 	D 	DIAv44(	 GAJ fc** D9@)&+6..y'=QQ!'** 	L 	LHC[[C00F#s## L#~RR&   
 C'' L#~AAN<<%$.$6 =  
 !  B     %%?cKKK$$Wd.@/RRR"7++ 	K 	KIAv595G5JD%fk22 	
  2///
  !6777
  111
  !7!9!9:::$.:::#-
  H!1BB3GG	
 	
 	

 13344	
 	
s   AH 
I!(I		Icompile_reasonsc                 <    sJ t          dd fd           d S )Nartifactc                      dddS )N!compiled_autograd_compile_reasonsjsonrj   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  sL       /...	
 	
 	
 	
 	
 	
r?   pinputspsaved_tensorssaved_tensorspctxctxmaybe_backward_state_idxopaque_object_indicesc                      fd                                 D             } fd|D             }	j        }
t          |
          |
j        |
j        |
j        ~
t          j                    r j        D ]}|j	        rt          d          t          j        j        dt          t          j                 dt          t                   dt          t                    dt          t                    dt           f
fd	            } j                            d
||||	g|R 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        j        j                            ||          }t5          j         j        |          }|S )Nc                 :    g | ]}                     |          S r>   to_proxyrk   r  r^   s     r@   rm   zDAutogradCompilerInstance.proxy_call_aot_backward.<locals>.<listcomp>  s%    SSSDMM!$$SSSr?   c                 *    g | ]}j         |         S r>   )r   )rk   r   r^   s     r@   rm   zDAutogradCompilerInstance.proxy_call_aot_backward.<locals>.<listcomp>  s1     
 
 
 S!
 
 
r?   z@torch.compile does not currently support higher order gradients.ctx_saved_tensorsctx_symintsctx_opaque_objs	flat_argsr7   c                 R    t          j        j        j        j        | ||g|R  }|S rC   )r9   
_functorch_aot_autogradruntime_wrappers_backward_prologue_functional)r3  r4  r5  r6  r   maybe_subclass_metadatametadatas        r@   call_aot_bwd_prologuezOAutogradCompilerInstance.proxy_call_aot_backward.<locals>.call_aot_bwd_prologue  sE     "0A_!'   C Jr?   rb   kindre   rr   r   c            	      @   dt           j        j        dt          fd}  | j                  }fdt          |t          d u          z
            D             }                                }t          |          t          j                  k    sJ fd|D             }||d t          |          <   |	                               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    r-||         j        }	 ||j                  |	_        |	|<   |dz  };|j        dk    r5t          |j                  dk    sJ fd|j        d         D             }{|j        dk    r|j        }
j                            |
          }t)          j        j        |t-          |
                     j                            d|di           } ||j                  |_        ||<   	|j        dk    r|j        t           j        j        j        j        u r t           j        j        j        j        |_        j        j                            |fd          } ||j                  |_        ||<   |j        dk    r{|j        }
j                            |
          }t)          j        j        |t-          |
                     j        j                            |fd          }||_        ||<   t=          d          |J dt           j        fdfd|D             }                     ||           |S )Nr`   r7   c                 @    d}| j         D ]}|j        dk    r|dz  } |S )Nr   r   rh   )rp   rd   )r`   num_argsrl   s      r@   
num_inputszkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.num_inputs  s9    !K  Dw-// A r?   c                      g | ]
}|         S r>   r>   )rk   r   pgradss     r@   rm   zkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<listcomp>  s*       q	  r?   c                 :    g | ]}                     |          S r>   r/  r1  s     r@   rm   zkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<listcomp>   s%    :::Qa((:::r?   r   _rh   	node_namec                     d d|  S )NaotrH  r>   )rI  deduped_aot_ids    r@   make_uniquezlAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.make_unique4  s    9^99i999r?   r   rf   c                     g | ]N}t          |t          j        j                  r+t          j                            |         j                  n|OS r>   )rN   r9   r   Noder4   r   )rk   nr^   value_remaps     r@   rm   zkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<listcomp>@  s\            &a77{1~t~FFF     r?   get_attrr>   rb   c                     |          S rC   r>   rP  rQ  s    r@   r   ziAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>T      A r?   call_modulec                     |          S rC   r>   rT  s    r@   r   ziAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<lambda>]  rU  r?   zshouldn't get herec                      t                      5  t          j        ddddd          cd d d            S # 1 swxY w Y   d S )Nr   {   r'   r9   zerosr>   r?   r@   dummyzfAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.dummyh  s    022 8 8 ;q!Q3778 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8s   488c                 f    g | ]-}t          |t          j        j                  r
             n|.S r>   )rN   r9   r   r4   )rk   or\  s     r@   rm   zkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph.<locals>.<listcomp>l  sC       DE:a88?a  r?   )!r9   r   r   rv   r`   r  _get_compiled_autograd_symintsr  symintsrw   r   r   rp   rd   rl   rj   rr   re   r   get_fresh_qualnamer   r   r   create_nodeopsatenviewdefaultreshape	node_copyrU   r   r  )rD  rC  	pall_argsr`  psymintsargs_idxpoutputsrM  rl   phrj   qualnamer   outputsrL  r\  rQ  aot_idrP   r*  pbackward_staterF  r^   s                 @@@r@   copy_paste_aot_backward_graphzWAutogradCompilerInstance.proxy_call_aot_backward.<locals>.copy_paste_aot_backward_graph  sV    %(.  S         "z)/22H   #(Ct8S4T4T)T#U#U  I 88::Gw<<3s{#3#33333::::':::H(0InGn%*  111 HK48H ![[N"6* D"Cd&9&&A"C"CC'''1,''':s :s : : : : : : "- *? *?7m++"8,1B)k$)44BG(*K%MHHW((ty>>Q....          "&1	     HH W
**;D#~@@FFHDN/79d;S;STTT!^77
HbRTUUF"-+di"8"8FK(.K%%W//{ein&9&AAA ',in&<&D!^1;;6666 F #.+di"8"8FK(.K%%W--;D#~@@FFHDN/79d;S;STTT!^1;;6666 F %-FM(.K%%()=>>>'''85< 8 8 8 8   IQ  G (((;;;Nr?   subclass_meta
is_runtimeunwrapped_argsc                 F    t           j        j        dt          dt          f fd            }t	          j        j        |          }j                            d|t          |          i           }
                                }                    |g|g           |S )Nru  r7   c                  2                         |           S )N)rt  )creation_fn)ru  rt  rs  s    r@   make_subclasszkAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructor.<locals>.make_subclassw  s    $00J0WWWr?   rb   r?  )r9   r   r   r   pytreetree_mapr0  r   r   r   allocate_dummyr  )rs  rt  ru  ry  punwrapped_argspoutputrf   r^   s   ``     r@   proxy_subclass_constructorzTAutogradCompilerInstance.proxy_call_aot_backward.<locals>.proxy_subclass_constructort  s     ])Xs Xs X X X X X X *)X %odm^LLOn11$$?++ 2  G ((**F((&G9===Mr?   )make_subclass_override)r_  _forward_clsrV   r=  r<  _aot_idr9   is_grad_enabledoutput_inforequires_gradr   r   r   r   r   r1   r   r   r   r   listrs   r8  r9  r:  _backward_epilogue_functionalrz  r{  r0  )r^   r&  r'  r(  r)  r*  r+  r,  rj  popaque_objectsrL   output_alias_infor>  rr  ro  r  resultspresultsrp  rP   r<  r=  rq  rF  s   `    `            @@@@@@r@   proxy_call_aot_backwardz0AutogradCompilerInstance.proxy_call_aot_backward  s   * TSSSc.P.P.R.RSSS
 
 
 
,
 
 
 +%&677	#,"2"J!) "" 	%-%9  !$2 &Z  
 
	%	'5	!+.	 &c]	 !		
 	 	 	 	 	 	 
&	%	  ,, ( 	   - 
 
 #/"./GHOd	tEL/A d	 d	 d	 d	 d	 d	 d	 d	 d	 d	 d	L 0/11		,0	BJ3-	\	 	 	 	 	 	* "0A__##=	 ` 
 
 ?4='::r?   output_metadatasbackward_idxc           
         | j         J | j         |         }|                     |          }	|                     |          }
t          |j        d          r|                     |	|
|||||          }n(| j                            dt          ||
g|	R i           }|J t                      5  g }t          |          D ]W\  }}|||         |
                    d            %|\  }}}}|
                    t          j        ||||                     X|                     ||           d d d            n# 1 swxY w Y   t          |          S )Nr  rb   r?  )sizedtypelayoutdevice)r   r0  r   r  r  r   r   r   r'   r   rw   r9   emptyr  r   )r^   r}   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  s    +++---''}553#Y// 	22(% GG n11$$"  
  2 	 	G """(** 	< 	<24H(12B(C(C  $_"*gcl.BOOD))).=+tKTvfUUU    ((7;;;	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< 	< Xs   ,B D88D<?D<
base_sizesbase_stridesbase_storage_offset
view_sizesview_stridesview_storage_offsetc           	      8   ||                      |          |                      |          |                      |          |                      |          |                      |          |                      |          f}|                     t          |d gdz            S )N   )r0  
proxy_callcopy_slices_prologue)	r^   r}   r  r  r  r  r  r  rr   s	            r@   call_copy_slices_prologuez2AutogradCompilerInstance.call_copy_slices_prologue  s     MM*%%MM,''MM-..MM*%%MM,''MM-..
 3TD6A:FFFr?   needs_input_gradr   res
grad_slicec                 d    |                      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  s<      vsJ7FS)***
 
 	
r?   c                 ~    t                      5  t          j        ddg          cd d d            S # 1 swxY w Y   d S )Nr   i[rZ  r   s    r@   r|  z'AutogradCompilerInstance.allocate_dummy  s    (** 	/ 	/;9~..	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/ 	/s   266fn_namer   r   r   c                 <    t                               ||||          S )zBinds ops.fn_name = fn)rc  r   )r^   r  r   r   r   s        r@   bind_functionz&AutogradCompilerInstance.bind_function  s     www$6EEEr?   gradsrr   r  c                 l    t                               |          }|                     ||g|R |          S )z:Proxies a call to ops.fn_name(grads, *args) into the graph)rc  r   r  )r^   r  r  rr   r  rd   s         r@   apply_functionalz)AutogradCompilerInstance.apply_functional  s3     WWWrE>D>>?CCCr?   c           	      
    t          j         fd|          } j                            d||i            fd|D             }                     |fdt          t          |                    D                        |S )z*Proxies a call to fn(*args) into the graphc                 .                         |           S rC   r/  )r  r^   s    r@   r   z5AutogradCompilerInstance.proxy_call.<locals>.<lambda>  s    t}}Q/?/? r?   rb   rr   r   c                 8    g | ]}                                 S r>   )r|  )rk   rH  r^   s     r@   rm   z7AutogradCompilerInstance.proxy_call.<locals>.<listcomp>  s%    AAAA$%%''AAAr?   c                      g | ]
}|         S r>   r>   )rk   r   	proxy_outs     r@   rm   z7AutogradCompilerInstance.proxy_call.<locals>.<listcomp>  s    -W-W-Wqil-W-W-Wr?   )rz  r{  r   r   r  r  r  )r^   r   rr   r  
proxy_argsr   r  s   `     @r@   r  z#AutogradCompilerInstance.proxy_call  s     _%?%?%?%?FF
N//Rj 0 
 
	 BAAAAAA$$V-W-W-W-WE#f++DVDV-W-W-WXXXr?   rH  ro  c                 "   t                               d          }t          j        | j        |g|R           }| j                            d||i           }t          |          t          |          k    sJ |                     ||           |S )zEProxies a call to ops.validate_outputs(outputs, *args) into the graphvalidate_outputsrb   r  )	rc  r   rz  r{  r0  r   r   r  r  )r^   rH  ro  rr   r  rd   r  new_proxy_outputss           r@   r  z)AutogradCompilerInstance.validate_outputs  s     WW'((_T]W4Dt4D4DEE
 N77Rj 8 
 
 ?##s7||3333$$W.?@@@r?   old_varnew_varc                    |                      |          }|                      |          }| j                            dt          j        ||fi           }|                                 }|                     |g|g           |S Nrb   r  )r0  r   r   r9   r   r|  r  )r^   r  r  old_var_proxynew_var_proxyr  r   s          r@   
accumulatez#AutogradCompilerInstance.accumulate  s    g..g..N//UYm]-KTV 0 
 
	 $$&&$$fX	{;;;r?   variabler   has_post_hooksc                     | j                             dt          |                     |          |                     |          |fi            d S r  )r   r   r   r0  )r^   r  r   r  s       r@   rY   z(AutogradCompilerInstance.accumulate_grad&  s^     	## h''d##
  	$ 		
 		
 		
 		
 		
r?   hookr   c                 h      j                             dt          |g fd|D             R |          S )Nrb   c                 :    g | ]}                     |          S r>   r/  rk   rI   r^   s     r@   rm   z<AutogradCompilerInstance.proxy_call_hook.<locals>.<listcomp><  s%    111q$--""111r?   )r   r   r   )r^   r  rr   r   s   `   r@   proxy_call_hookz(AutogradCompilerInstance.proxy_call_hook4  sS     ~**1111D111  
 
 	
r?   hook_iddata_idc                     | j         J | j         |         }| j        |         }|                     ||d          }|                                 }|                     |g|g           |S )Nunpack_hook	hook_type)r   r   r  r|  r  )r^   r  r  r  dataproxyr   s          r@   r  z$AutogradCompilerInstance.unpack_hookA  s    +++(%g.$$# % 
 

 !!##$$cUUG444
r?   r   c                 &   | j         J | j         |         }|                     |||         d          }t                      5  t          ||                   ||<   |                     ||         g|g           d d d            n# 1 swxY w Y   |S )Ntensor_pre_hookr  )r   r  r'   rK   r  )r^   r}   r  r   r  r  s         r@   r  z(AutogradCompilerInstance.tensor_pre_hookN  s     +++($$1I' % 
 

 )** 	? 	?#F1I..F1I((&)ug>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? s   7BB
B
c           	      `   | j                             dt          j        j        j        j        ||                     ||                   fi           }t                      5  t          ||                   ||<   | 
                    ||         g|g           d d d            n# 1 swxY w Y   |S Nrb   )r   r   r9   _Cr   r5   call_cpp_tensor_pre_hooksr0  r'   rK   r  )r^   r}   r  r   r  s        r@   cpp_tensor_pre_hookz,AutogradCompilerInstance.cpp_tensor_pre_hook]  s     ++H.HdmmF1I../	
 
 )** 	? 	?#F1I..F1I((&)ug>>>	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? 	? s    7B##B'*B'c                     | j         J | j         |         }|                     ||d          }t                      5  d |D             }|                     ||           d d d            n# 1 swxY w Y   |S )Npre_hookr  c                 ,    g | ]}t          |          S r>   rK   rk   rI   s     r@   rm   z5AutogradCompilerInstance.pre_hook.<locals>.<listcomp>t  s    555k!nn555r?   r   r  r'   r  )r^   r}   r  r  r  s        r@   r  z!AutogradCompilerInstance.pre_hookk  s    +++(&&  ' 
 

 )** 	: 	:55f555F((999	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: 	: s   #A,,A03A0c                     | j         J | j         |         }|                     |||d          }t                      5  d |D             }|                     ||           d d d            n# 1 swxY w Y   |S )N	post_hookr  c                 ,    g | ]}t          |          S r>   r  r  s     r@   rm   z6AutogradCompilerInstance.post_hook.<locals>.<listcomp>  s    777!{1~~777r?   r  )r^   ro  r}   r  r  r  s         r@   r  z"AutogradCompilerInstance.post_hookx  s     +++(&&!	 ' 
 
 )** 	; 	;77w777G((':::	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; 	; s   #A--A14A1inputc                 4   t          |t          j                  sJ | j        J | j        |         }|                     ||d          }t                      5  t          |          g}|                     ||g           d d d            n# 1 swxY w Y   |S )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 	7u%%&C((ug666	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 	7 
s   (BBBr`   c                    i }d}t          |j                  }|d         j        dk    sJ |d         }t          |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}%|
j        d         j	        j
        d	k    }t          |
j        d                                                   dk    }|rF|rDt          |
j                                                  }t          d
 |D                       r|
||	<   |rz|                                D ]D}
t                              d|
           |
j        d                                         |
j        d<   Et          |                                          S g S )NFr   r}   rh   r   cudaTcpuc              3      K   | ]]}t          |j        t          j        j                  o|j        j        d v p&t          |j        t                    o|j        j         V  ^dS ))primsrd  N)rN   re   r9   _ops
OpOverload	namespacer   r   rk   users     r@   r   zDAutogradCompilerInstance.move_graph_nodes_to_cuda.<locals>.<genexpr>  s       
 
  #4;
0EFF G K15FF
 #4;33 ? $ >>
 
 
 
 
 
r?   zMoving node %s from cpu to cuda)r  rp   re   userskeysr  r   r   metar  r  r  allvaluesverbose_logdebugr  )r^   r`   to_movehas_cuda_inputsrp   r}   inputs_usersfirst_getitem_idxlast_getitem_idxr   rl   is_cpu	is_scalar
node_userss                 r@   move_graph_nodes_to_cudaz1AutogradCompilerInstance.move_graph_nodes_to_cuda  s   ,.U[!!Qx(****qFL--//00 344&'<?::::,s</@/@@1D%&,r*::::: .. 	& 	&GAt" ty'7'>'Cv'M'M"&Yu%,1U:FDIe,1133449I &) &!$*//"3"344
 
 
 !+
 
 
 
 
 & "&GAJ  	((( ; ;!!"CTJJJ#'9U#3#8#8#:#:	%   '''	r?   rl   c                     t          |t          j        j                  oG|j        dk    o<|j        t          j        j        j        j	        t          j        j        j
        j        fv S r  )rN   r9   r   rO  rd   re   rc  rd  sym_sizerv   	sym_numelrf  )r^   rl   s     r@   is_sym_nodez$AutogradCompilerInstance.is_sym_node  sU    tUX]++ O?*O	'+UY^-E-MNO	
r?   c                 N   t                      d }t          | j        j                            d                    D ]1\  }}                    |j                                                   2|t          t                    dz
  k    sJ dt          j        j        dt          ffd}t          | j        j        j                  }| j        j                            |           t          | j        j        j                  }t                               d||z
             d S )Nr   rg   rh   rl   r7   c                 j    | v s| j         dk    r| j        t          v rdS |                                 S )Nrb   T)rd   re   _impure_targets	is_impure)rl   unpack_nodess    r@   r  z/AutogradCompilerInstance.dce.<locals>.is_impure  s<    |##?**t{o/M/Mt>>###r?   zDCE removed %d nodes)r2   r   r   r`   rq   updater  r  r  r   r9   r   rO  rs   rp   eliminate_dead_coder  r  )r^   r   rl   r  beforeafterr  s         @r@   dcezAutogradCompilerInstance.dce  s   
 3=,, !5!@!@M!@!R!RSS 	3 	3GAt
 1 12222C+,,q00000	$EHM 	$d 	$ 	$ 	$ 	$ 	$ 	$ T^)/0000;;;DN(.//0&5.AAAAAr?   c                    g }g }t          | j        j        j                  }t	          |           t	          |          }|j        dk    sJ |j        D ]I}|j        t          j	        u sJ |j        r|
                    |           4|
                    |           Jt                      }|D ]}t          |j        t                    sJ |j        d         |k    sJ t          |j        d         t                    sJ t!          |          }|                    |j        d                    |j        d         |f|_        |D ]!}	| j        j                            |	           "|S )Nr   r   rh   )ro   r   r`   rp   rn   rj   r  re   rt   ru   rw   setrN   rr   r   rv   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"XX
'))))&, 	2 	2L&(*:::::! 2!!,//// ##L1111#&55 	6 	6Ddi/////9Q<:----dilC00000//Mty|,,,1}5DII" 	4 	4FN ++F3333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>.0DbIIIr?   c                 l     j                             dt          j        di             j                                          j                             dd j                                                  |                    fi            g t                      r 
                     j         j                   j         j        j        D ]}dD ]}||j        v r|j        |= t          dd  fd                                                                                                                                                                                                                                                                                            j        r$ j                             j         j                                                                         d	 j                   t9          d
g           t;          dddd          }t<                              d|           t@          !                    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                      &                    dtO          j(                    d j        i j)        d            j*        +                    d d d            | ,                              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                              d          S )NCompiledAutogradPreReorderingFprint_output)r#   r   r   r`   r   print_readabler   s   r@   r   z6AutogradCompilerInstance.end_capture.<locals>.<lambda>2  sE    {#$947999    n%n00	 r?   r"  r#  r}   zCompiled autograd graphT)include_deviceinclude_stridecoloredz%scompiled_autograd_graphc                  0                          d          S )NFr%  )r'  )r`   s   r@   r   z6AutogradCompilerInstance.end_capture.<locals>.<lambda>]  s    u333GG r?   )r$  compiled_fn.r   r   r   packed_inputsr7   c           	         	 da j        rj                            |           g }t          |          D ]s\  }}|v rj|dk    rO|                    t          j        d|                     t
          j                            |d         d           ^|                    |           tD ]3}	||	         	                                
                    d          ||	<   4t                      5  t          j                  5   | |||||          }
j        rj                            |
           |
cd d d            cd d d            da S # 1 swxY w Y   d d d            n# 1 swxY w Y   da d S # da w xY w)NTr   r  rh   )non_blockingF)in_compiled_autograd_regionr   r   r   rw   r9   r  r   maybe_mark_dynamic
pin_memoryr  _disabler   r   r   )r-  r}   r   r   r   r.  filtered_sizesr   integerr   r   runtime_inputs_to_mover^   r  s              r@   runtime_wrapperz=AutogradCompilerInstance.end_capture.<locals>.runtime_wrapper`  sL   4.2+# >$55f===!#$-e$4$4 ; ;LCn,,"Q;;*11%+a2I2IJJJ!M<<^B=OQRSSSS*11':::/ O OA &q	 4 4 6 6 ; ; ; N NF1IIZZ  !5dg!>!>  %+ C ' 4(..s333              /4++                       /4+++e+3333sZ   C,E7 /E(2E6E(E7 E	E(E	E(E7 (E,,E7 /E,0E7 7E;r5   r   r   )-r   r   r   _exec_final_callbacks_stubr   r   rb  
create_argr0  rH   r  r`   rp   r  r   delay_unpack_hook_nodesreorder_tensor_pre_hook_nodes'reorder_pre_hook_nodes_to_schedule_asapreorder_accumulate_grad_nodes%reorder_pre_hook_nodes_to_mimic_eager reorder_post_acc_grad_hook_nodesreorder_post_hook_nodesr  r   r|   r  r  r   r   r   compiled_autograd_loginfor  r  r   r   r   r   log_event_endr   r   r   r   __exit__r   )	r^   ro  rl   fieldlazy_graph_coder8  r`   r7  r  s	   `     @@@r@   end_capturez$AutogradCompilerInstance.end_capture  s   ##&A		
 	
 	
 	
""^&&t}}W'='=>>@		
 	
 	
 -/%'' 	Y%)%B%B4>CW%X%X"
 N(. 	) 	)D@ ) )DI%%	%() 	 1 1 1 1	
 	
 	
 	
 	$$&&&**,,,44666**,,,22444--///$$&&& 	


 	C,,T^-ABBB 1133(()EDG)E)EFFEH:...0%
 
 
 	""4999$000%GGGG	
 	
 	
 	

$	4!#s(+$	4$	4 $	4 	$	4
 $	4 $	4 38_$	4 $	4 $	4 $	4 $	4 $	4 $	4 $	4L 	"##11LNN!"& 	2 	
 	
 	
 	%%dD$777 0 0 7 777r?   c                     d | D             }|S )Nc                 R    g | ]$}t          |          t          j        j        u "|%S r>   )r  r9   r   rO  )rk   rP  s     r@   rm   z:AutogradCompilerInstance.get_all_nodes.<locals>.<listcomp>  s,    ===qDGGux}$<$<$<$<$<r?   r>   )rr   rp   s     r@   get_all_nodesz&AutogradCompilerInstance.get_all_nodes  s     >=D===r?   c                     | j         dk    s4| j         dk    r+| j        t          j        u r| j        d         j         dk    rdS dS )Nr   rb   r   TF)rd   re   rt   ru   rr   )rl   s    r@   is_placeholderz'AutogradCompilerInstance.is_placeholder  sI    7m##G&&x///	!=004ur?   c                    | j         j                            dt                    D ]}|j        d         |j        d         }}d}|j        t          j        u r|}|j        d         }t          ||g          }||j	        urA| 
                    |          s,|                    |           ||                    |           dS )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`   rq   r   rr   re   rt   ru   maxprevrM  rw   )r^   rl   r{   	grad_noder  args         r@   r>  z6AutogradCompilerInstance.reorder_accumulate_grad_nodes  s     N(33'; 4 
 
 	- 	-D %)IaL$)A,	JL8#333((-a0	z9-..C$)##D,?,?,D,D#

4   +JJ|,,,	- 	-r?   c                     | j         j                            dt                    D ]K}|j                            dd          dk    r"t          |j                  }|                    |           LdS )zp
        We can delay unpack hooks until they are needed, even later than in the eager autograd engine.
        rb   rc   r  Nr  )	r   r`   rq   r   r   r   minr  prepend)r^   rl   
first_users      r@   r;  z0AutogradCompilerInstance.delay_unpack_hook_nodes  s     N(33y 4 
 
 	% 	%D {{D11]BBTZJt$$$$	% 	%r?   c                 \   | j         j                            dt                    D ]}|j                            dd          dk    r"|j        d         }|j        d         }||j        ur?|                     |          s*|	                    |           |	                    |           dS )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`   rq   r   r   r   rr   rP  rM  rw   )r^   rl   r  
input_nodes       r@   r<  z6AutogradCompilerInstance.reorder_tensor_pre_hook_nodes  s     N(33y 4 
 
 	* 	*D {{D115FFF9Q<L1J**43F3Fz3R3R*!!,///##D)))	* 	*r?   c                    | j         j                            dt                    D ]`}|j                            dd          dk    r#|j        d         }|                     |j        d                   }g }g }|g}|D ]j}|j        dk    r]|j	        t          j        u rJ|                    |j        d                    |                    |           |                    |           kt          ||          D ]/\  }}	|                    |           |                    |	           0t          |          }
|
|j        urD|                     |
          s/|
                    |           |D ]}|                    |           bdS )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`   rq   r   r   r   rr   rK  rd   re   rt   ru   rw   zipremoverO  rP  rM  )r^   rl   r  input_nodes	to_remove	to_append
hook_blockrP  abrR  s              r@   r=  z@AutogradCompilerInstance.reorder_pre_hook_nodes_to_schedule_asap  s    N(33y 4 
 
 	+ 	+D {{D11Z??9Q<L,,TYq\::KIIJ  ) )4?**qx8;K/K/K$$QVAY///$$Q'''%%a(((Iy11 & &1""1%%%""1%%%%k""C$)##D,?,?,D,D#

<(((# + +A ''****5	+ 	+r?   c                    g }| j         j                            dt                    D ]7}|j                            dd          dk    r"|                    |           8t          |          D ]}|j        d         }t          |j
                                                  }t          |          dk    rIt          d |D                       sJ t          t          |d         j
                                                            }||j        urD|                    |           |                    |           |D ]}|                    |           dS )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   R   K   | ]"}|j         d k    o|j        t          j        u V  #dS )rb   N)rd   re   rt   ru   r  s     r@   r   zQAutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager.<locals>.<genexpr>  sL         ?*Nt{h>N/N     r?   )r   r`   rq   r   r   r   rw   reversedrr   r  r  r  r  r  rn   ro   rU  )r^   	pre_hooksrl   hook_getitem_noder  registered_noderu   s          r@   r?  z>AutogradCompilerInstance.reorder_pre_hook_nodes_to_mimic_eager  s    	N(33y 4 
 
 	# 	#D {{D11Z??T""""Y'' 	5 	5D $	!**++E5zzQ   !        #4a(;(;(=(=#>#>??Odi//''(9:::''---$ 5 5G#++G4444%	5 	5r?   c                    g }| j         j                            dt                    D ]7}|j                            dd          dk    r"|                    |           8t          |          D ]}|j        d         }|j        d         }d}t          |j
                                                  D ]}|j        dk    r|j        t          u r|} n |
J d            |                    |           |                    |           dS )	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`   rq   r   r   r   rw   rd  rr   r  r  r  rd   re   r   )r^   post_acc_grad_hooksrl   r  r{   acc_grad_noderP  s          r@   r@  z9AutogradCompilerInstance.reorder_post_acc_grad_hook_nodes  s@    !N(33y 4 
 
 	- 	-D {{D115III&&t,,,, 011 	& 	&D9Q<L1J !M**//1122  4?**qx;O/O/O$%ME ,,J -,,
   ...%%%%#	& 	&r?   c           	      b   g }| j         j                            dt                    D ]7j                            dd          dk    r"|                               8t          |          D ]j        d         }j        d         }j        d         }t          |          dk    r>g }|
                    t          |                     |D ]G}|
                    fd	t          |j                                                  D                        Ht          |          }|j        dk    r|j        t"          u r|j        d         }d}	t          |j                                                  D ]<}
|
j        dk    r/|
j        t          u r!|
j                            dd          d
k    r|
}	=|	,|	                    |           |                               t|j        ur?|                     |          s*|                    |           |                               dS )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   | ]>}|j         d k    r-|j        t          u rj                            dd          dk    :|V  ?dS )rb   r  Nr  )rd   re   r   r   r   )rk   r  rl   s     r@   r   zCAutogradCompilerInstance.reorder_post_hook_nodes.<locals>.<genexpr>\  sg       - -?22 K944 KOOK>>+MM 
 NMMM- -r?   r  )r   r`   rq   r   r   r   rw   rd  rr   r  extendr  r  r  rO  rd   re   r   rP  rM  )r^   
post_hooksr  rz   r\  input_nodes_and_usersrX  rR  r{   post_acc_grad_hook_noderP  rl   s              @r@   rA  z0AutogradCompilerInstance.reorder_post_hook_nodesA  sk    
N(33y 4 
 
 	$ 	$D {{D11[@@d####Z(( )	* )	*D9Q<L9Q<L)A,K<  1$$ %'!!((k):):;;;) 	 	
%,, - - - - $Z%5%:%:%<%< = =- - -     +,,Cv((SZ;O-O-O Xa[
*.'j.335566 4 4A//H	11HLLd;;?SSS23/*6+22<@@@ ''---$)##D,?,?,D,D#

<(((##D)))S)	* )	*r?   tc                     |d S t          |t                    r fd|D             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 )Nc                 :    g | ]}                     |          S r>   r/  r  s     r@   rm   z5AutogradCompilerInstance.to_proxy.<locals>.<listcomp>  s%    000DMM!$$000r?   c              3   B   K   | ]}                     |          V  d S rC   r/  r  s     r@   r   z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>  s/      55aq))555555r?   )rN   r  r   r9   SymIntSymFloatr   rl   r   r(   r   r   r	  proxy_tensor_ProxyTensorr  )r^   rr  rx  s   `  r@   r0  z!AutogradCompilerInstance.to_proxy{  s    94a 	10000a0000a 	655551555555a%,788 	5,QV44!U\** 	H)$.!<<,(=(J(WXXXXX!!r?   objectsr  c                    t          t          j        j                  r|rt	          |          t	          |          k    sJ g }t          t	          |                    D ]?}||         \  }}|                     ||d            |                    |                    @|n(fdt          t	          |                    D             t	          |          t	                    k    sJ t          |d | j	                   S )Nc                      g | ]
}|         S r>   r>   )rk   r   r  s     r@   rm   zDAutogradCompilerInstance.bind_objects_to_proxies.<locals>.<listcomp>  s    CCC!71:CCCr?   constanttracer)
rN   r9   r   r4   r  r  set_node_originrw   r+   r   )r^   rz  r  r   bound_proxiesr   nodecall_indexrI  s     `     r@   r  z0AutogradCompilerInstance.bind_objects_to_proxies  s    gux~.. 
	D 	D7||s7||3333 "s7||,, 5 5A07
-NI((NDIII!((4444'CCCCuS\\/B/BCCC7||s7||++++'7T$.QQQQr?   indexc                 ~    | j         J | j         |         }t                      }t          ||d | j                   |S )Nr}  )r   r$   r+   r   )r^   r  r  bw_states       r@   bind_backward_statez,AutogradCompilerInstance.bind_backward_state  sE    +++ ' ??(EDPPPPr?   rI  r  pyobjc                 *   d}|4|j         }t          |d          r|j        t          d          |j        }| | d| d}t          j                                                    d         }|                    d|          }t          |           d S )N r  zThis compiled backward function was saved by AOTAutogradCache, which does not support
                    compiled autograd. Please turn off AOTAutogradCache using `TORCHINDUCTOR_AUTOGRAD_CACHE=0`.z (NodeCall )r  z:raw_stack_trace = CapturedTraceback.extract().format()[-1])
r  r   rO   r   r  r3   extractformatreplacer/   )	r^   rI  r  r  maybe_aot_idforward_clsnew_coderaw_stack_tracenew_stack_traces	            r@   r  z(AutogradCompilerInstance.set_node_origin  s     ,K{I.. 32:&s    +2KKK.KKK+355<<>>rB)11H(
 
 	(((((r?   r   rC   )Hr   r   r   r   r   r_   r9   r   r   r"   r   staticmethodr   r   r   r  rv   r1   r0   r   rs   r  r%  r   r  autogradfunctionBackwardCFunctionr  r  r  r|  r  r  r  r  r  rY   r   r4   r  r  r  r  r  r  r  r   r  r  r  r  r  r#   r  rH  rO  rK  rM  r>  r;  r<  r=  r?  r@  rA  r0  r  r$   r  Functionr  r>   r?   r@   r   r     s	       .HS#X$6 .4 . . . .C5< C$ C: C C C C 5S 5s 5} 5 5 5 \5y
U\"y
 Cyy
 kM12	y

 d5c?+,y
 y
 y
 
sD&[(94;NN	Oy
 y
 y
 y
v
c
 

 
 
 
R#R !.R  -	R
 R R #&*R  $CyR 
#R R R Rh33 #3-3  -	3
 3 ^$63 #&*3  $Cy3 
u|d"C'	(3 3 3 3jGG SMG sm	G
 !G SMG smG !G 
%,	G G G G*
"4.
 
 c]	

 L
 
%,	
 
 
 
/ / / / /
FF S#XF !	F
 F 
F F F F	D	D }	D 		D
 "#	D 
%,		D 	D 	D 	D
38$
,/
BJ3-
	%,	
 
 
 
'}47JRSV-	%,	   #      

,1L
JN
	
 
 
 

S#X&
/2
>A
	
 
 
 
3      5<(36;>	el	   5<(36;>	el	   x} s tEL?Q    EL)3;EL3ITW	el	    \,/	el	   */ehn /c / / / /b
 
 
 
 
 
B B B B. SX        DJc Jk J J J J|83 |85#s(1CS1H+I |8 |8 |8 |8| HSM d58=.A    \
 UX] t    \- - - -*% % % %* * * *("+ "+ "+ "+H!5 !5 !5 !5F"& "& "& "&H8* 8* 8* 8*t"# "# " " " "( 15	 #  eCHo&-	
 
#   ,     )) ) ~&-	)
 
) ) ) ) ) )r?   r   FTr   dynamicignore_active_disable_ctx)NNNc              #     K   |st           rd V  d S |rt          |          t          u sJ ddlm} |j        j        dk    rda	 d V  dad S # daw xY wt          j	        
                                rddlm} t          j        j        j                            t#          j        t&          |           |          \  }}t)                      r.t          j        j        j                            t,                     dat0          }t0          dz  a	 t          j                            d          5  d V  d d d            n# 1 swxY w Y   |sdat          j        j        j                            ||           t0          dz  at0          |k    s
J d            d S # |sdat          j        j        j                            ||           t0          dz  at0          |k    s
J d            w xY w)	Nr   )
eval_frameforce_eagerTF)cudagraph_treesrh   zINested Compiled Autograd Contexts must return before their parent context)active_disable_ctxr  rs   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 % 0); 0 	)==D((((,,,,,,$55 591>8=5555==== z&&(( <;;;;;;
  2HH!":KHH'  011 S 2EEkRRR(,%KQJE^>>uEE  EEE               & 605- 2HH"M   
+++_ ,++++ & 605- 2HH"M   
+++_ ,++++++s=   A AF ,D=1F =EF EF AG&c               #   N  K   t           j        j        j                            d d          \  } }dat          sda	 d V  | rdadat           j        j        j                            | |           d S # | rdadat           j        j        j                            | |           w xY w)NFT)r9   r  r   r5   r  r  r  )r  r  s     r@   r4  r4    s      
 	*@@uMM !& "!
 	-(,%"*@@M	
 	
 	
 	
 	
  	-(,%"*@@M	
 	
 	
 	
s   A2 22B$c                  8   da t          rJ t          j        j        j                            d d           t          j        j        j                            d            t          j        j        j                                         t          j
                    ad S )NF)r  r1  r9   r  r   r5   r  r  clear_cache	itertoolsr   r   r>   r?   r@   resetr  3  sy     %****	H&<<T5III	H&99$???	H&22444o''OOOr?   r}   r  r  r  r  r  r  c                     | d         }|                     ||          }|J |                    |           ||z
  }	|                    |||	          }
||
|
                    t          j                  gS )Nr   )memory_format)new_empty_stridedcopy_
as_stridedcloner9   contiguous_format)r}   r  r  r  r  r  r  r   r   offsetr  s              r@   r  r  @  s     !9D##J==F
LL #66F"":|VDDJJ
 0 0u?V 0 W WXXr?   r  r   r  r  c                     d gt          |           z  }t          t          |                     D ]K}| |         rA||         |dk    r'||         }|J |                    |           |||<   @||         ||<   L|S )Nr   )r  r  r  )r  r   r  r  grad_inputsr   to_copys          r@   r  r  T  s     /3Vc:J6K6K-KK3'(()) 
( 
(A 		(1v~Avva&***  )))!'A!$QAr?   )TTr   )s__doc__r   r  r  rt   r   collectionsr   r   collections.abcr   r   r   typingr   r	   r9   torch.utils._pytreeutils_pytreerz  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   rB  r  rs   rA   rH   r   rK   rV   rX   r   r   rc  r   r9  r  r   r   rv   r   r   r  r  r1  r  r  contextmanagerr  r4  r  r  r  r  r>   r?   r@   <module>r     s                   , , , , , , , , 9 9 9 9 9 9 9 9 9 9 % % % % % % % %  $ $ $ $ $ $ $ $ $ ; ; ; ; ; ;              < ; ; ; ; ; ; ;                   M L L L L L L L L L L L > > > > > > > > 6 6 6 6 6 6 , , , , , , 4 4 4 4 4 4             ? ? ? ? ? ?                  G F F F F F F F B B B B B B B B 2 2 2 2 2 2 2 2 / / / / / / 4 4 4 4 4 4  %$$$$$$X *)(4GHH *EFF$    4D 4 4 4 45<$& 5<$+>    
 
c0B 
 
 
 
6G G G G G G G G^!# !# !# !# !# !# !# !#H5 5 5 5 5 5 5 5  kmm MLL *"=	  ")/##	s 	s 	 	 	 	`) `) `) `) `) `) `) `)H% "  ). % $  	  &*I I#s(#II  $I  	I I I IX 
),- 
 
 
 
,( ( ( (YU\"Y%Y ;'Y %	Y
 %Y ;'Y %Y 
%,Y Y Y Y(tnL 
%,%	& 	
 
%,
     r?   