
    9j                        d Z ddlZddlZddlZddlZddlZddlmZmZm	Z	 ddlm
Z
mZ ddlmZmZ ddlZddlmZ ddlmZ ddlmZ d	d
lmZmZ d	dlmZ d	dlmZ d	dlmZm Z  d	dl!m"Z"m#Z# d	dl$m%Z%m&Z& ddl'm(Z( ddl)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/m0Z0m1Z1 ddl2m3Z3 erddl4m5Z5 ddl6m7Z7  ejp                  e9      Z: G d de(      Z; G d de3      Z< G d de<      Z= G d de;      Z> G d de;      Z? G d  d!e;      Z@ G d" d#e;      ZA G d$ d%e;      ZB G d& d'e;      ZC G d( d)e;      ZD G d* d+e;      ZE G d, d-e;      ZF G d. d/e;      ZG G d0 d1e;      ZH G d2 d3e;      ZI G d4 d5e;      ZJ G d6 d7e;      ZK G d8 d9e;      ZL G d: d;e;      ZM G d< d=e;      ZN G d> d?e;      ZO G d@ dAe;      ZP G dB dCe;      ZQ G dD dEe;      ZR G dF dGe;      ZS G dH dIe;      ZT G dJ dKe;      ZU G dL dMe(      ZV G dN dOe(      ZWy)Pa)  
This file contains a collection of context manager classes used by Dynamo for tracking
and managing various PyTorch runtime states during graph compilation. These context
managers handle different aspects of PyTorch's execution environment, including:

- Autograd states (grad mode, inference mode)
- CUDA streams and events
- Profiling contexts
- Deterministic algorithms
- Forward/backward AD modes
- SDPA (Scaled Dot Product Attention) kernels
- FSDP (Fully Sharded Data Parallel) states
- AMP (Automatic Mixed Precision) autocast states

The context managers ensure proper state transitions during graph compilation by
tracking enter/exit points and managing cleanup operations. They help maintain
consistency between eager execution and compiled graph behavior by capturing and
restoring state changes.
    N)CallableSequenceSized)AbstractContextManager	ExitStack)AnyTYPE_CHECKING)config)Guard)warning_once   )graph_break_hints	variables)create_call_function)unimplemented)GuardBuilderinstall_guard)
AttrSourceGlobalStateSource)_get_error_on_graph_break_set_error_on_graph_break   )VariableTracker)NestedUserFunctionVariableSkipFunctionVariableUserFunctionVariableUserMethodVariable!WrappedNestedUserFunctionVariableWrappedSkipFunctionVariableWrappedUserFunctionVariableWrappedUserMethodVariable)UserDefinedObjectVariable)	PyCodegen)InstructionTranslatorc            	       "    e Zd Zddddhej                  Z	 ddededz  deddf fdZd	d
defdZ	 dd	d
dedef   dz  ddfdZ	d	d
dedefdZ
ddZddZdefdZdefdZd	d
dee   deeef   defdZdefdZdefdZddZddZ xZS )ContextWrappingVariablecm_objtarget_valuesinitial_valuesstateNkwargsreturnc                 @    t        |   di | || _        || _        y N )super__init__r(   r)   selfr(   r)   r+   	__class__s       c/media/conek/DATA/Code/OCR/venv/lib/python3.12/site-packages/torch/_dynamo/variables/ctx_manager.pyr1   z ContextWrappingVariable.__init__G   s%     	"6"*,    txr$   c                     t        | d      r| j                  || j                         | j                  |       t        j
                  j                  d       S N
_call_func)hasattrr:   r(   set_cleanup_hookr   ConstantVariablecreater3   r7   s     r5   enterzContextWrappingVariable.enterN   sE    4&OOB 2 23b!))0066r6   fn.c                 r     |d fd}| _         j                  j                   j                         y )Nc                  X    t         d      r j                   j                         y y r9   )r;   r:   r)   r?   s   r5   rA   z4ContextWrappingVariable.set_cleanup_hook.<locals>.fnY   s&    4.OOB(;(;< /r6   r,   N
cleanup_fnoutputadd_cleanup_hookcleanupr3   r7   rA   s   `` r5   r<   z(ContextWrappingVariable.set_cleanup_hookT   s.     := 68
		""4<<0r6   argsc                 `    | j                          t        j                  j                  d       S N)cleanup_assertr   r=   r>   r3   r7   rK   s      r5   exitzContextWrappingVariable.exit`   s'     	))0066r6   c                      |t        |j                  j                  | j                               | j	                                      y rM   )r   r7   import_sourcemodule_namefn_namer3   codegens     r5   reconstruct_typez(ContextWrappingVariable.reconstruct_typef   s2    wzz//0@0@0BCT\\^T	
r6   c                      j                   fd        j                  }|sd}j                  |D cg c]  }j                  |       c}       j                  t	        t        |      d             y c c}w )Nc                  &    j                         S rM   )rW   )rV   r3   s   r5   <lambda>z5ContextWrappingVariable.reconstruct.<locals>.<lambda>l   s    d&;&;G&D r6   r/   F)add_push_nullr(   extend_outputcreate_load_constr   len)r3   rV   r(   vals   ``  r5   reconstructz#ContextWrappingVariable.reconstructk   sj    DE**MW#w88=WX23}3EuMN Xs   A;c                     t        d      )Nzmodule_name called on baseNotImplementedErrorr3   s    r5   rS   z#ContextWrappingVariable.module_names   s    !">??r6   c                     t        d      )Nzfn_name called on baserb   rd   s    r5   rT   zContextWrappingVariable.fn_namev   s    !":;;r6   c                    t        |      dk(  sJ t        |d   t        t        t        t
        f      sJ t        |d   t              rt        |d   |       S t        |d   t              rt        |d   |       S t        |d   t              rt        |d   |       S t        |d   t
              rt        |d   |       S t        d      )Nr   r   zUnexpected arg type)r^   
isinstancer   r   r   r   r   r   r!   r    AssertionErrorr3   r7   rK   r+   s       r5   call_functionz%ContextWrappingVariable.call_functiony   s     4yA~~G*$"$	
 	
 
 d1g9:4T!WdCCQ!56.tAw==Q!34,T!Wd;;Q!56.tAw== !677r6   c                      yNTr/   rd   s    r5   supports_graph_breaksz-ContextWrappingVariable.supports_graph_breaks       r6   c                      yrl   r/   rd   s    r5   exit_on_graph_breakz+ContextWrappingVariable.exit_on_graph_break   rn   r6   c                 L    | j                   | j                          d | _         y y rM   )rF   rd   s    r5   rI   zContextWrappingVariable.cleanup   s"    ??&OO"DO 'r6   c                 J    | j                   sJ d       | j                          y )Nzmultiple exits?)rF   rI   rd   s    r5   rN   z&ContextWrappingVariable.cleanup_assert   s    1 11r6   rM   rV   r#   r,   NrD   )__name__
__module____qualname__r   _nonvar_fieldsr   r1   r@   r   r<   rP   rW   r`   strrS   rT   r   dictrj   boolrm   rp   rI   rN   __classcell__r4   s   @r5   r&   r&   >   s:   	
 
	'	'N @D- -25*-OR-	-7/ 7O 7 LP
1)
1/7S/AD/H
1	
17)72A7	7

O@S @< <8#8 '8 S/)*	8
 
88t T #
r6   r&   c                        e Zd Zdee   deddf fdZdefdZdefdZdd	de	fd
Z
dd	de	de	fdZdefdZdefdZ xZS )GenericContextWrappingVariabler'   r+   r,   Nc                 T    |J t        |   d||j                  d| || _        y )N)value
value_typer/   )r0   r1   r4   r'   )r3   r'   r+   r4   s      r5   r1   z'GenericContextWrappingVariable.__init__   s@    !!! 	
''	
 	

 r6   c                 .    | j                   j                  S rM   )r'   ru   rd   s    r5   rS   z*GenericContextWrappingVariable.module_name   s    {{%%%r6   c                 @    t        | j                        j                  S rM   )typer'   rt   rd   s    r5   rT   z&GenericContextWrappingVariable.fn_name   s    DKK )))r6   r7   r$   c                     | j                   d nt        | j                   d      }t        j                  | j                  j
                  j                  | |      j                  |g i       S )N	__enter__source)r   r   r   r   r'   r   __func__rj   )r3   r7   r   s      r5   r@   z$GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r6   rK   c                 "   | j                   d nt        | j                   d      }t        j                  | j                  j
                  j                  | |      j                  |t        |      i       }|j                  j                          |S )N__exit__r   )r   r   r   r   r'   r   r   rj   listactive_generic_context_managerspop)r3   r7   rK   r   xs        r5   rP   z#GenericContextWrappingVariable.exit   sx     ,*T[[*2U((KK  ))
 -DJ
+	 	

 	**..0r6   c                      yNFr/   rd   s    r5   rm   z4GenericContextWrappingVariable.supports_graph_breaks   s    r6   c                      yrl   r/   rd   s    r5   rp   z2GenericContextWrappingVariable.exit_on_graph_break   rn   r6   )rt   ru   rv   r   r   r1   rx   rS   rT   r   r@   rP   rz   rm   rp   r{   r|   s   @r5   r~   r~      s    5c: c d &S &* *$/ $O $
)
2A
	
t T r6   r~   c                   T    e Zd ZdededdfdZdddefdZddd	edefd
Zde	defdZ
y)$RepararametrizeModuleContextVariablectx_manager_vtmodr,   Nc                      || _         || _        y rM   )cm_vtr   )r3   r   r   s      r5   r1   z-RepararametrizeModuleContextVariable.__init__   s    #
r6   r7   r$   c                 B   t         j                  j                  j                  j	                  |      5  | j
                  j                  |d      j                         | _        | j
                  j                  |d      j                         | _	        |j                  j                  j                  | j                         |j                  j                  j                  | j                         | j                  j                  |      cd d d        S # 1 sw Y   y xY w)N_parameters_buffers)torch_dynamor   higher_order_ops dynamo_allow_side_effects_in_hopr   var_getattrrealizeold_parameters_varold_buffer_varrG   side_effectsignore_mutations_onr   r@   r?   s     r5   r@   z*RepararametrizeModuleContextVariable.enter   s     ]]$$55VV
 	( '+hh&:&:2}&M&U&U&WD#"&(("6"6r:"F"N"N"PDII""66t7N7NOII""66t7J7JK::##B'	( 	( 	(s   CDDrK   c                    t         j                  j                  j                  j	                  |      5   | j
                  j                  |g| }|j                  j                  j                  | j                         |j                  j                  j                  | j                         |cd d d        S # 1 sw Y   y xY wrM   )r   r   r   r   r   r   rP   rG   r   stop_ignoring_mutations_onr   r   )r3   r7   rK   r   s       r5   rP   z)RepararametrizeModuleContextVariable.exit   s    
 ]]$$55VV
 	  

*T*AII""==d>Q>QRII""==d>U>UV	 	 	s   A=B;;Cnamec                 .    t        | j                  |      S rM   )getattrr   )r3   r   s     r5   __getattr__z0RepararametrizeModuleContextVariable.__getattr__   s    tzz4((r6   )rt   ru   rv   r&   r   r1   r   r@   rP   rx   r   r/   r6   r5   r   r      sa    '> S T 

(/ 
(O 
()2A	) ) )r6   r   c            	       V    e Zd ZdZedddededd fd       ZdddefdZddd	edefd
Z	y))GradInplaceRequiresGradCtxManagerVariablez#represents torch grad requires gradr7   r$   r(   r+   r,   c                     t        d|d d|S Nr(   r)   r/   )r   r7   r(   r+   s      r5   r>   z0GradInplaceRequiresGradCtxManagerVariable.create   s&     9 
'
 
 	
r6   c                      j                   \  }t        j                  j                  j	                          _        t        j                  j                  j                  |        j                  | fd       |j                  j                  dt        j                  j                  j                  |fi        _
        t        j                  j                  d       S )Nc                  j    t         j                  j                  j                   j                        S rM   )r   _C
_functorch!set_inplace_requires_grad_allowed
prev_staterd   s   r5   rZ   zAGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>
  s#    EHH''II r6   rj   )r(   r   r   r   !get_inplace_requires_grad_allowedr   r   r<   rG   create_nodeproxyr   r=   r>   )r3   r7   enableds   `  r5   r@   z/GradInplaceRequiresGradCtxManagerVariable.enter  s    &&	((--OOQ==gF	
 YY**HHAAJ	

 ))0066r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S Nrj   )rI   rG   r   r   r   r   r   r   r   r=   r>   rO   s      r5   rP   z.GradInplaceRequiresGradCtxManagerVariable.exit  s[     	
		HHAA__		
 ))0066r6   N
rt   ru   rv   __doc__staticmethodr   r>   r   r@   rP   r/   r6   r5   r   r      sh    -
#
47
CF
	4
 
7/ 7O 7$
7)
72A
7	
7r6   r   c            	       V    e Zd ZdZedddededd fd       ZdddefdZddd	edefd
Z	y)0TemporarilyPopInterpreterStackCtxManagerVariablezJrepresents torch._functorch.pyfunction.temporarily_pop_interpreter_stack()r7   r$   r(   r+   r,   c                     t        d|d d|S r   )r   r   s      r5   r>   z7TemporarilyPopInterpreterStackCtxManagerVariable.create&  s'     @ 
'
 
 	
r6   c                 P    t         j                  j                  j                          _         j                  | fd       |j                  j                  dt         j                  j                  j                  di        _        t        j                  j                  d       S )Nc                  j    t         j                  j                  j                   j                        S rM   )r   r   r   push_dynamic_layer_stacksavedrd   s   r5   rZ   zHTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>4  s     EHH''@@L r6   rj   r/   )r   r   r   pop_dynamic_layer_stackr   r<   rG   r   r   r   r=   r>   r?   s   ` r5   r@   z6TemporarilyPopInterpreterStackCtxManagerVariable.enter0  s~    XX((@@B
L	
 YY**HH77	

 ))0066r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S r   )rI   rG   r   r   r   r   r   r   r   r=   r>   rO   s      r5   rP   z5TemporarilyPopInterpreterStackCtxManagerVariable.exit>  sZ     	
		HH88ZZM		
 ))0066r6   Nr   r/   r6   r5   r   r   #  sh    T
#
47
CF
	;
 
7/ 7O 7
7)
72A
7	
7r6   r   c                       e Zd ZdZ e e       ej                        Ze	ddde
dd fd       ZdddefdZdddedefd	Zy
)%JvpIncrementNestingCtxManagerVariablez5represents torch.func.jvp increment/decrement nestingr7   r$   r+   r,   c                 "    t        dd d d|}|S r   )r   r7   r+   vars      r5   r>   z,JvpIncrementNestingCtxManagerVariable.createU  s*     4 

 

 
r6   c                 l   t        | j                         t        j                  j                  j                         }| j                  |d        |j                  j                  dt        j                  j                  j                  di       | _        t        j                  j                  ||      S )Nc                  R    t         j                  j                  j                         S rM   )r   r   eager_transformsexit_jvp_nestingr/   r6   r5   rZ   z=JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>d  s    ((99JJL r6   rj   r/   )r   _guards_singletonr   r   r   enter_jvp_nestingr<   rG   r   r   _jvp_increment_nestingr   r   r   build)r3   r7   	jvp_levels      r5   r@   z+JvpIncrementNestingCtxManagerVariable.enter`  s    d,,-$$55GGI	L	
 YY**HH66	

 ((..r9==r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S Nrj   r/   )
rI   rG   r   r   r   r   _jvp_decrement_nestingr   r=   r>   rO   s      r5   rP   z*JvpIncrementNestingCtxManagerVariable.exitn  sQ     	
		UXX00GGR	
 ))0066r6   Nrt   ru   rv   r   r   r   r   FUNCTORCH_STACK_MATCHr   r   r   r>   r   r@   rP   r/   r6   r5   r   r   K  sy    ? /1<3U3UV#/2	0 >/ >O >7)72A7	7r6   r   c            	       b    e Zd ZdZedddededd fd       ZdddefdZddd	edefd
Z	de
fdZy)SetFwdGradEnabledContextManagerzWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradr7   r$   r(   r+   r,   c                     t        d|d d|S r   )r   r   s      r5   r>   z&SetFwdGradEnabledContextManager.create{  s&     / 
'
 
 	
r6   c                      j                   \  }t        j                  j                          _        t        j                  j                  |        j                  | fd       |j                  j                  dt        j                  j
                  |fi        _	        t        j                  j                  d       S )Nc                  V    t         j                  j                   j                        S rM   )r   r   _set_fwd_grad_enabledr   rd   s   r5   rZ   z7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>  s    EHH224??C r6   rj   )r(   r   r   _is_fwd_grad_enabledr   r   r<   rG   r   r   r   r=   r>   )r3   r7   modes   `  r5   r@   z%SetFwdGradEnabledContextManager.enter  s    ##((779&&t,C	
 YY**HH**G	

 ))0066r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rI   rG   r   r   r   r   r   r   r=   r>   rO   s      r5   rP   z$SetFwdGradEnabledContextManager.exit  sU     	
		HH**__		
 ))0066r6   c                 J    t         j                  j                  j                  S rM   )r   autograd
forward_adr   rd   s    r5   python_typez+SetFwdGradEnabledContextManager.python_type  s    ~~((>>>r6   N)rt   ru   rv   r   r   r   r>   r   r@   rP   r   r   r/   r6   r5   r   r   x  st    a
#
47
CF
	*
 
7/ 7O 7 
7)
72A
7	
7?T ?r6   r   c                       e Zd ZdZ e e       ej                        Ze	ddde
dd fd       ZdddefdZdddedefd	Zdefd
Zy)DualLevelContextManagerz;Represents torch.autograd.forward_ad.dual_level ctx managerr7   r$   r+   r,   c                     t        dd d d|S r   )r   )r7   r+   s     r5   r>   zDualLevelContextManager.create  s$    & 

 
 	
r6   c                 |    t         j                         t        j                  j                  j                          _         j                  | fd       |j                  j                  dt        j                  j                  di        _        t        j                  j                  | j                        S )Nc                  l    t         j                  j                  j                   j                        S )N)level)r   r   r   exit_dual_level	new_levelrd   s   r5   rZ   z/DualLevelContextManager.enter.<locals>.<lambda>  s#    11AAAW r6   rj   r/   )r   r   r   r   r   enter_dual_levelr   r<   rG   r   r   _enter_dual_levelr   r   r   r   r?   s   ` r5   r@   zDualLevelContextManager.enter  s    d,,-22CCEW	
 YY**HH&&	

 ((..r4>>BBr6   rK   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rI   rG   r   r   r   _exit_dual_levelr   r   r=   r>   rO   s      r5   rP   zDualLevelContextManager.exit  sU     	
		HH%%^^		
 ))0066r6   c                 J    t         j                  j                  j                  S rM   )r   r   r   
dual_levelrd   s    r5   r   z#DualLevelContextManager.python_type  s    ~~((333r6   N)rt   ru   rv   r   r   r   r   
DUAL_LEVELr   r   r   r>   r   r@   rP   r   r   r/   r6   r5   r   r     s    E/1<3J3JK
* 
c 
>W 
 
C/ CO C
7)
72A
7	
74T 4r6   r   c                       e Zd ZdZ e e       ej                        Ze	ddde
dd fd       ZdddefdZdddedefd	Zy
)&GradIncrementNestingCtxManagerVariablez6represents torch.func.grad increment/decrement nestingr7   r$   r+   r,   c                 "    t        dd d d|}|S r   )r   r   s      r5   r>   z-GradIncrementNestingCtxManagerVariable.create  s*     5 

 

 
r6   c                 l   t        | j                         t        j                  j                  j                         }| j                  |d        |j                  j                  dt        j                  j                  j
                  di       | _	        t        j                  j                  ||      S )Nc                  R    t         j                  j                  j                         S rM   )r   r   r   _grad_decrement_nestingr/   r6   r5   rZ   z>GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*W r6   rj   r/   )r   r   r   r   r   _grad_increment_nestingr<   rG   r   r   r   r   r   )r3   r7   
grad_levels      r5   r@   z,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WXYY**HH77	

 ((..r:>>r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S r   )
rI   rG   r   r   r   r   r   r   r=   r>   rO   s      r5   rP   z+GradIncrementNestingCtxManagerVariable.exit  sQ     	
		UXX00HH"b	
 ))0066r6   Nr   r/   r6   r5   r   r     sy    @ /1<3U3UV#/2	1 
?/ 
?O 
?7)72A7	7r6   r   c                        e Zd ZdZedddeeef   dd fd       Z	 	 ddeeef   de	dz  d	e	dz  d
e	ddf
 fdZ
dddefdZddZdefdZ xZS )CatchWarningsCtxManagerVariablez'Delay a call to warnings.catch_warningsr7   r$   catch_warnings_argsr,   c                     t        |d d       S )N)r  r(   r)   )r  )r7   r  s     r5   r>   z&CatchWarningsCtxManagerVariable.create  s     / 3
 	
r6   Nr(   r)   r+   c                 f    t        |t              sJ |       t        |   d||d| || _        y r   )rg   ry   r0   r1   r  )r3   r  r(   r)   r+   r4   s        r5   r1   z(CatchWarningsCtxManagerVariable.__init__  sE     -t4I6II4 	
'	
JP	
 $7 r6   c                 6   | j                   j                         D ci c]  \  }}||j                          }}}t        j                  di || j                  |fd       t        j                  j                  |j                               S c c}}w )Nc                  *     j                  d d d       S rM   )r   )ctx_vals   r5   rZ   z7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*L r6   r/   )
r  itemsas_python_constantwarningscatch_warningsr<   r   r   r   r   )r3   r7   kvr+   r
  s        @r5   r@   z%CatchWarningsCtxManagerVariable.enter  s    262J2J2P2P2R
*.!QAq##%%
 
 ))3F3b"LM((..r73D3D3FGG
s   Bc                    j                  fd       j                  | j                  j                                t	        | j                  j                               }j                  j                  t        |      |d             y )Nc                  (     j                  dd      S )Nr  r  )load_import_from)rV   s   r5   rZ   z=CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    G,,Z9IJ r6   F)	r[   foreachr  valuestuplekeysr\   create_call_function_kwr^   )r3   rV   r  s    ` r5   r`   z+CatchWarningsCtxManagerVariable.reconstruct  sk    J	
 	00779:T--2245g==c$iuUVr6   c                 "    t         j                  S rM   )r  r  rd   s    r5   r   z+CatchWarningsCtxManagerVariable.python_type%  s    &&&r6   NNrs   )rt   ru   rv   r   r   ry   rx   r   r>   r   r1   r@   r`   r   r   r{   r|   s   @r5   r  r    s    1
#
:>sO?S:T
	*
 
 %)%)	7!#"677 Tz7 d
	7
 7 
7H/ HO HW'T 'r6   r  c            	           e Zd ZdZ e e       ej                        Ze	ddde
e   dedd fd       ZdddefdZddd	edefd
Zy)&VmapIncrementNestingCtxManagerVariablez1represents torch VMap increment/decrement nestingr7   r$   r(   r+   r,   c                 "    t        d|d d|}|S r   )r  )r7   r(   r+   r   s       r5   r>   z-VmapIncrementNestingCtxManagerVariable.create3  s*     5 
'
 

 
r6   c                 <   t        | j                         | j                  \  }}t        |t        j
                        r|j                  }n|j                         }|j                         }t        j                  j                  j                  ||      }| j                  |d        |j                  j                  dt        j                  j                  j                  |j!                         |fi       | _        t        j$                  j'                  ||      S )Nc                  R    t         j                  j                  j                         S rM   )r   r   r   _vmap_decrement_nestingr/   r6   r5   rZ   z>VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>K  r   r6   rj   )r   r   r(   rg   r   SymNodeVariablesym_numr  r   r   r   _vmap_increment_nestingr<   rG   create_proxypredispatchas_proxyr   r   r   )r3   r7   
batch_size
randomnessbatch_size_value
vmap_levels         r5   r@   z,VmapIncrementNestingCtxManagerVariable.enter@  s    d,,-!%!3!3
Jj)";";<)11)<<>224
XX((@@j

 	b"WXYY++((@@  "J/	

 ((..r:>>r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S r   )
rI   rG   r   r   r   r%  r   r   r=   r>   rO   s      r5   rP   z+VmapIncrementNestingCtxManagerVariable.exitT  sV     	
		((@@		
 ))0066r6   N)rt   ru   rv   r   r   r   r   r   r   r   r   r   r   r>   r@   rP   r/   r6   r5   r  r  )  s    ; /1<3U3UV
#
0
 
 
2	
 
?/ ?O ?(
7)
72A
7	
7r6   r  c                   ,    e Zd ZdZ e e       ej                        Ze		 dddde
dede
dd f
d       Z	 	 dd
e
dee   d	z  dede
dd	f
 fdZdddefdZdddedefdZdddee   deeef   def fdZddde
dd	fdZdefdZdefdZdefdZ xZS )GradModeVariablez6represents torch.{no_grad,enable_grad,set_grad_mode}()r7   r$   target_valueinitializedr+   r,   c                     t        d|gt        j                         gd|}|r|j                  | |j                         |S r   )r-  r   is_grad_enabledr:   r(   )r7   r.  r/  r+   r   s        r5   r>   zGradModeVariable.createf  sO      
'.!1134
 

 NN2s001
r6   Nr(   r)   c                 T    t        |   d||d| t        | j                         y r   )r0   r1   r   r   )r3   r(   r)   r/  r+   r4   s        r5   r1   zGradModeVariable.__init__v  s4     	 	
'	
JP	
 	d,,-r6   c                 x    | j                  || j                         t        j                  j	                  d       S rM   r:   r(   r   r=   r>   r?   s     r5   r@   zGradModeVariable.enter  -    D../))0066r6   rK   c                 x    | j                  || j                         t        j                  j	                  d       S rM   r:   r)   r   r=   r>   rO   s      r5   rP   zGradModeVariable.exit  /     	D//0))0066r6   c                 ^    | j                  || j                         t        |   |||      S rM   r:   r)   r0   rj   r3   r7   rK   r+   r4   s       r5   rj   zGradModeVariable.call_function  s-     	D//0w$Rv66r6   r  c                 
   t        |      dk(  sJ |d   }t        j                         |k7  rW|j                  j	                  dt        j
                  j                  |fi        t        j
                  j                  |       y y )Nr   r   rj   )r^   r   r1  rG   r   r   _set_grad_enabledr3   r7   r  r   s       r5   r:   zGradModeVariable._call_func  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r6   c                      yNr   r/   rd   s    r5   rS   zGradModeVariable.module_name      r6   c                      y)Nset_grad_enabledr/   rd   s    r5   rT   zGradModeVariable.fn_name  s    !r6   c                 "    t         j                  S rM   )r   rC  rd   s    r5   r   zGradModeVariable.python_type  s    %%%r6   Frl   )rt   ru   rv   r   r   r   r   	GRAD_MODEr   r   r   rz   r>   r   r1   r   r@   rP   ry   rx   rj   r:   rS   rT   r   r   r{   r|   s   @r5   r-  r-  a  sR   @/1<3I3IJ "#  	
 
 $ 15 	
.
. !-
. 	
.
 
. 

.7/ 7O 77)72A7	77#7 '7 S/)*	7
 
7.4 .c .d .S " "&T &r6   r-  c            	            e Zd Zedddededd fd       Z	 dded	edz  deddf fd
ZdddedefdZ	dddefdZ
defdZdefdZdefdZ xZS )InferenceModeVariabler7   r$   r.  r+   r,   c                 H    t        |gfdt        j                         i|}|S )Nr)   )rH  r   is_inference_mode_enabledr7   r.  r+   r   s       r5   r>   zInferenceModeVariable.create  s3     $N
+0+J+J+L
PV
 
r6   Nr(   r)   c                 V    |t        j                         }t        |   d||d| y r   )r   rJ  r0   r1   r2   s       r5   r1   zInferenceModeVariable.__init__  s8     !"<<>N 	
'	
JP	
r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S r   )rN   rG   r   r   r   	grad_mode_exit_inference_moder   r   r=   r>   rO   s      r5   rP   zInferenceModeVariable.exit  s]     	
		NN$$99ZZM		
 ))0066r6   c                 T   dt         j                  j                  j                  rE| j                  d   r6dt        j
                         t         j                  j                  d       n0t        j                  j                  j                  | j                   dfd}| j                  ||       |j                  j                  dt         j                  j                  j                  g | j                  i       | _        t        j                   j#                  d       S )NFr   Tc                      r t         j                  j                         y t         j                  j                  j                          y rM   )r   r   r=  r   rN  rO  )ctx disabled_inference_mode_forciblypriors   r5   cleanup_hookz1InferenceModeVariable.enter.<locals>.cleanup_hook  s0    /**51((==cBr6   rj   rD   )r   r   r
   "fake_tensor_disable_inference_moder(   r1  r   r=  r   rN  _enter_inference_moder<   rG   r   r   r   r=   r>   )r3   r7   rU  rR  rS  rT  s      @@@r5   r@   zInferenceModeVariable.enter  s    +0(MM  CC""1%
 04,))+EHH&&u-..**@@$BTBTUC	C 	b,/YY**NN$$::"d  "	

 ))0066r6   c                      yr@  r/   rd   s    r5   rS   z!InferenceModeVariable.module_name  rA  r6   c                      y)Ninference_moder/   rd   s    r5   rT   zInferenceModeVariable.fn_name  s    r6   c                 "    t         j                  S rM   )r   rZ  rd   s    r5   r   z!InferenceModeVariable.python_type  s    ###r6   rM   )rt   ru   rv   r   r   r>   rz   r1   r   rP   r@   rx   rS   rT   r   r   r{   r|   s   @r5   rH  rH    s    #36BE	   '+

 t
 	

 


7)
72A
7	
77/ 7O 7<S    $T $r6   rH  c            	            e Zd ZdZedddededd fd       Z	 dd	ed
edz  deddf fdZdddedefdZ	dddefdZ
defdZdefdZdefdZ xZS )CUDADeviceVariablezrepresents torch.cuda.devicer7   r$   devicer+   r,   c                 b    t        dt        j                  j                  |d      gd d|}|S )NT)optionalr   r/   )r]  r   cuda_get_device_index)r7   r^  r+   r   s       r5   r>   zCUDADeviceVariable.create  sA     ! 
 ::777NO
 

 
r6   Nr(   r)   c                 *    t        |   d||d| y r   r0   r1   r2   s       r5   r1   zCUDADeviceVariable.__init__  $     	 	
'	
JP	
r6   rK   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d      S )Nrj   F)
rN   rG   r   r   ra  _maybe_exchange_devicer   r   r=   r>   rO   s      r5   rP   zCUDADeviceVariable.exit  sW     	
		JJ--ZZM		
 ))0077r6   c                 H   t        j                  j                  | j                   | j	                  |fd       |j
                  j                  dt         j                  j                  g | j                  i       | _        t        j                  j                  d       S )Nc                  B    t         j                  j                         S rM   )r   ra  rg  )prev_idxs   r5   rZ   z*CUDADeviceVariable.enter.<locals>.<lambda>  s    %***K*KH*U r6   rj   )r   ra  _exchange_devicer(   r<   rG   r   r   r   r=   r>   )r3   r7   rj  s     @r5   r@   zCUDADeviceVariable.enter  s~    ::..0B0BCb"UVYY**JJ''"d  "	

 ))0066r6   c                      y)Nz
torch.cudar/   rd   s    r5   rS   zCUDADeviceVariable.module_name#      r6   c                      y)Nr^  r/   rd   s    r5   rT   zCUDADeviceVariable.fn_name&  s    r6   c                 6    t         j                  j                  S rM   )r   ra  r^  rd   s    r5   r   zCUDADeviceVariable.python_type)  s    zz   r6   rM   )rt   ru   rv   r   r   r   r>   r1   r   rP   r@   rx   rS   rT   r   r   r{   r|   s   @r5   r]  r]    s    &#-0<?	  &*

 d

 	

 


8)
82A
8	
8	7/ 	7O 	7S  !T !r6   r]  c                        e Zd ZdZ e e       ej                        Ze	ddde
dd fd       Z	 	 dddded	edz  d
ede
ddf fdZ	 ddddede
f   dz  ddfdZdddeddfdZdefdZdefdZdefdZ xZS )TorchFunctionDisableVariablez>represents whether torch function overrides are enabled or notr7   r$   r+   r,   c                 $    t        | fg g d|}|S )Nr   )rq  r   s      r5   r>   z#TorchFunctionDisableVariable.create2  s-     +

 	
 
r6   Nr(   r)   only_subclassc                    t        |      dk(  sJ |t        |      dk(  sJ || _        |j                  j                  | _        |j                  j
                  | _        t        |    d||d| t        | j                         y )Nr   r   r/   )r^   rs  symbolic_torch_function_statetorch_function_subclass_enabled'initial_torch_function_subclass_enabledtorch_function_mode_enabled#initial_torch_function_mode_enabledr0   r1   r   r   )r3   r7   r(   r)   rs  r+   r4   s         r5   r1   z%TorchFunctionDisableVariable.__init__>  s     =!Q&&&)c..AQ.FFF*,,LL 	4 ,,HH 	0 	 	
'	
JP	
 	d,,-r6   rA   .c                 r     |d fd}| _         j                  j                   j                         y )Nc                       j                   j                  _         j                  s j                  j                  _        y y rM   )rw  ru  rv  rs  ry  rx  r?   s   r5   rA   z9TorchFunctionDisableVariable.set_cleanup_hook.<locals>.fn\  sA    @@ 00P ))@@ 44P *r6   rD   rE   rJ   s   `` r5   r<   z-TorchFunctionDisableVariable.set_cleanup_hookU  s.    
 : 
		""4<<0r6   r  c                     t        |      dk(  sJ d|j                  _        | j                  sd|j                  _        y y )Nr   F)r^   ru  rv  rs  rx  r3   r7   r  s      r5   r:   z'TorchFunctionDisableVariable._call_funch  s>    6{aKP((H!!KPB,,H "r6   c                      y)Nztorch._Cr/   rd   s    r5   rS   z(TorchFunctionDisableVariable.module_namen      r6   c                     | j                   ryy)NDisableTorchFunctionSubclassDisableTorchFunction)rs  rd   s    r5   rT   z$TorchFunctionDisableVariable.fn_nameq  s    1%r6   c                     | j                   rt        j                  j                  S t        j                  j                  S rM   )rs  r   r   r  r  rd   s    r5   r   z(TorchFunctionDisableVariable.python_typev  s+    88888xx,,,r6   rl   rM   )rt   ru   rv   r   r   r   r   TORCH_FUNCTION_STATEr   r   r   r>   r   rz   r1   r   r<   r:   rx   rS   rT   r   r   r{   r|   s   @r5   rq  rq  -  s	   H/1<3T3TU	#	/2		'	 	 (,".#. . 	.
 . . 
.4 )-1#1 S#X%1 
	1&Q4 Qe Q QS & &
-T -r6   rq  c            	            e Zd ZdZedddedz  dedd fd       Z	 dd	eedz     d
eedz     dz  deddf fdZ	ddde
fdZdddeedz     ddfdZdefdZdefdZdefdZ xZS )!DisabledSavedTensorsHooksVariablez;represents torch.autograd.graph.disable_saved_tensors_hook.r7   r$   r.  Nr+   r,   c                     t        d|gt        j                  j                  j	                         gd|}|j                  | |g       |j                  |        |S r   )r  r   r   	_autograd/_saved_tensors_hooks_get_disabled_error_messager:   r<   rK  s       r5   r>   z(DisabledSavedTensorsHooksVariable.create  sb     0 
'.""RRT

 
 	rL>*R 
r6   r(   r)   c                 *    t        |   d||d| y r   rd  r2   s       r5   r1   z*DisabledSavedTensorsHooksVariable.__init__  re  r6   c                 @    t         j                  j                  d       S rM   r   r=   r>   r?   s     r5   r@   z'DisabledSavedTensorsHooksVariable.enter      ))0066r6   r  c                    t        |      dk(  sJ |d   }|k|j                  j                  dt        j                  j
                  j                  |fi        t        j                  j
                  j                  |       y |j                  j                  dt        j                  j
                  j                  di        t        j                  j
                  j                          y )Nr   r   rj   r/   )r^   rG   r   r   r   r  _saved_tensors_hooks_disable_saved_tensors_hooks_enabler>  s       r5   r:   z,DisabledSavedTensorsHooksVariable._call_func  s     6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r6   c                      y)Nztorch.autograd.graphr/   rd   s    r5   rS   z-DisabledSavedTensorsHooksVariable.module_name      %r6   c                      y)Ndisable_saved_tensors_hooksr/   rd   s    r5   rT   z)DisabledSavedTensorsHooksVariable.fn_name  s    ,r6   c                 "    t         j                  S rM   
contextlib_GeneratorContextManagerrd   s    r5   r   z-DisabledSavedTensorsHooksVariable.python_type      222r6   rM   )rt   ru   rv   r   r   rx   r   r>   r   r1   r   r@   r:   rS   rT   r   r   r{   r|   s   @r5   r  r  |  s    E#36:IL	, " 7;
d
+
 !t,t3
 	

 

7/ 7O 7=)=3;C$J3G=	=.&S &- -3T 3r6   r  c            	            e Zd Zedej
                  j                  j                  dee	   de
ee	f   dd fd       Z	 ddee	   de	dz  de	ddf fd	Zd
ddedefdZd
ddefdZdefdZdefdZdefdZ xZS )AutocastModeVariablefuncrK   r+   r,   c                 0   | t         j                  j                  j                  t         j                  j                  j                  t         j
                  j                  j                  fv sJ  t        j                  |       j                  |i |}|j                          g }|j                          dD ]  }|dk(  ru| t         j                  j                  j                  t         j
                  j                  j                  fv r+| t         j                  j                  j                  u rdnd}n|j                  |   }t        |t              r |j                  |j                                |j                  |        t!        |fdd i|}|S )N)device_typedtyper   cache_enabledr  ra  cpur)   )r   ampautocast_modeautocastra  r  inspect	signaturebindapply_defaultsclear	argumentsrg   r   appendr  r  )r  rK   r+   
bound_argsr(   keyargr   s           r5   r>   zAutocastModeVariable.create  sK    II##,,JJNN##IIMM""
 
 	
 
 2W&&t,114B6B
!!#G 	*Cm#

''		&&1 )
 !%

(?(? ?fU **3/#/$$S%;%;%=>$$S)	* #=PPP
r6   Nr(   r)   c                 *    t        |   d||d| y r   rd  r2   s       r5   r1   zAutocastModeVariable.__init__  re  r6   r7   r$   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rN   rG   r   r   r  _exit_autocastr   r   r=   r>   rO   s      r5   rP   zAutocastModeVariable.exit  sT     	
		UYY55

}b	
 ))0066r6   c                 H   t        j                  j                  | j                   | j	                  |fd       |j
                  j                  dt         j                  j                  g | j                  i       | _        t        j                  j                  d       S )Nc                  B    t         j                  j                         S rM   )r   r  r  )rR  s   r5   rZ   z,AutocastModeVariable.enter.<locals>.<lambda>  s    %))*B*B3*G r6   rj   )r   r  _enter_autocastr(   r<   rG   r   r   r   r=   r>   )r3   r7   rR  s     @r5   r@   zAutocastModeVariable.enter  s}    ii''););<b"GHYY**UYY668N$:L:L8NPR

 ))0066r6   c                      y)Nztorch.amp.autocast_moder/   rd   s    r5   rS   z AutocastModeVariable.module_name  s    (r6   c                      y)Nr  r/   rd   s    r5   rT   zAutocastModeVariable.fn_name   r  r6   c                 J    t         j                  j                  j                  S rM   )r   r  r  r  rd   s    r5   r   z AutocastModeVariable.python_type  s    yy&&///r6   rM   )rt   ru   rv   r   r   r  r  r  r   r   ry   rx   r>   r1   r   rP   r@   rS   rT   r   r   r{   r|   s   @r5   r  r    s    "ii%%.."sm" S#X" 
 	" "N &*
}
 d

 	

 

7)72A7	77/ 7O 7)S ) 0T 0r6   r  c                        e Zd ZdZddedz  deddf fdZdddefd	Zddd
edefdZde	fdZ
de	fdZdefdZ xZS )NullContextVariablez>
    This class represents Python contextlib.nullcontext.
    Nr(   r+   r,   c                 (    t        |   dd|i| y Nr(   r/   rd  )r3   r(   r+   r4   s      r5   r1   zNullContextVariable.__init__  s    ?}??r6   r7   r$   c                 t    t         j                  j                  d       }| j                  r| j                  S |S rM   )r   r=   r>   r(   )r3   r7   nones      r5   r@   zNullContextVariable.enter  s1    ))006%)%7%7t!!ATAr6   rK   c                 @    t         j                  j                  d       S rM   r  rO   s      r5   rP   zNullContextVariable.exit       ))0066r6   c                      yNr  r/   rd   s    r5   rS   zNullContextVariable.module_name  rm  r6   c                      yNnullcontextr/   rd   s    r5   rT   zNullContextVariable.fn_name      r6   c                 "    t         j                  S rM   )r  r  rd   s    r5   r   zNullContextVariable.python_type  s    %%%r6   rM   )rt   ru   rv   r   r   r1   r   r@   rP   rx   rS   rT   r   r   r{   r|   s   @r5   r  r    s    @cDj @3 @4 @B/ BO B7)72A7	7
S  &T &r6   r  c                   |     e Zd ZdZdeddf fdZdefdZdddefd	Z	ddd
edefdZ
defdZdefdZddZ xZS )ProfilerContextVariablea~  
    This class represents a set of torch profiler context objects, where Dynamo
    ignores all the side-effects in the __init__, __enter__ and __exit__ methods
    by treating the object mostly as a `contextlib.nullcontext`, except for edge
    cases like the `__enter__` method which returns the object itself rather
    than `None`, per implementation of the torch objects.
    r+   r,   Nc                 (    t        |   ddd i| y r  rd  )r3   r+   r4   s     r5   r1   z ProfilerContextVariable.__init__+  s    6t6v6r6   c                 6    t         j                  j                  S rM   )r   profilerprofilerd   s    r5   r   z#ProfilerContextVariable.python_type.  s    ~~%%%r6   r7   r$   c                     | S rM   r/   r?   s     r5   r@   zProfilerContextVariable.enter1  s    r6   rK   c                 @    t         j                  j                  d       S rM   r  rO   s      r5   rP   zProfilerContextVariable.exit4  r  r6   c                      yr  r/   rd   s    r5   rS   z#ProfilerContextVariable.module_name9  rm  r6   c                      yr  r/   rd   s    r5   rT   zProfilerContextVariable.fn_name<  r  r6   c                 T    t        dt        |       dg t        j                         y )Nz2torch.profiler object escaped from compiled regionzXDynamo doesn't support compiling a region that returns a torch.profiler context manager.gb_typecontextexplanationhintsr   rx   r   SUPPORTABLErU   s     r5   r`   z#ProfilerContextVariable.reconstruct?  s*    HIr"..		
r6   rs   )rt   ru   rv   r   r   r1   r   r   r   r@   rP   rx   rS   rT   r`   r{   r|   s   @r5   r  r  "  su    7 7 7&T &/ O 7)72A7	7
S  
r6   r  c                        e Zd ZdZdhej
                  Zededee	   dddedd f
d	       Z
	 	 ddeded
z  dedd
f fdZdefdZddde	fdZddde	de	fdZdefdZdefdZd fdZ xZS )%ProfilerRecordFunctionContextVariableac  
    This class represents torch profiler context objects.

    For record_function: emits torch.ops.profiler._record_function_enter_new
    to the graph on enter, and torch.ops.profiler._record_function_exit on exit.
    But if emit_profiler_ops=False, behaves like nullcontext.

    For profile: behaves like nullcontext, ignoring all side-effects.
    emit_profiler_opsr  record_argsrecord_kwargszdict[str, VariableTracker]r+   r,   c                    d }t         j                  r|r|d   j                         n<|j                  dt        j
                  j                  d            j                         }d }t        |      dkD  r|d   j                         }nd|v r|d   j                         }||g}nt        t        d|        t        d|d d|S )	Nr   r   unknownr   rK   z+Profiler record function %s will be ignoredr   r/   )r
    capture_profiler_record_functionr  getr   r=   r>   r^   r   logr  )r  r  r  r+   r(   r   record_args_consts          r5   r>   z,ProfilerRecordFunctionContextVariable.createZ  s     22
  A113ZZI66==iH$$&  !%;!#$/N$E$E$G!6!$*6N$E$E$G!!#45MKTR4 
'
 
 	
r6   Nr(   r)   c                 *    t        |   d||d| y r   rd  r2   s       r5   r1   z.ProfilerRecordFunctionContextVariable.__init__z  re  r6   c                 J    t         j                  j                  j                  S rM   )r   r   r  record_functionrd   s    r5   r   z1ProfilerRecordFunctionContextVariable.python_type  s    ~~&&666r6   r7   r$   c                     t         j                  rV| j                  \  }}|j                  j	                  dt
        j                  j                  j                  ||fi       | _	        | S r   )
r
   r  r(   rG   r   r   opsr  _record_function_enter_newr   )r3   r7   r   rK   s       r5   r@   z+ProfilerRecordFunctionContextVariable.enter  sW    22++JD$..		""==t	DJ r6   rK   c                 
   t         j                  rU|j                  j                  dt        j
                  j                  j                  j                  | j                  fi        t        j                  j                  d       S r   )r
   r  rG   r   r   r  r  _record_function_exit_RecordFunctionr   r   r=   r>   rO   s      r5   rP   z*ProfilerRecordFunctionContextVariable.exit  s`     22II!!		""88HH	 ))0066r6   c                 *    t         j                  rdS dS )Nztorch.autograd.profilerr  r
   r  rd   s    r5   rS   z1ProfilerRecordFunctionContextVariable.module_name  s!     66 &	
 	
r6   c                 *    t         j                  rdS dS )Nr  r  r  rd   s    r5   rT   z-ProfilerRecordFunctionContextVariable.fn_name  s!     66 	
 	
r6   c                     t         j                  r)t        dt        |       dg t        j
                         y t        |   |       y )Nz,record_function escaped from compiled regionzADynamo doesn't support graph break inside record_function region.r  )r
   r  r   rx   r   r  r0   rW   )r3   rV   r4   s     r5   rW   z6ProfilerRecordFunctionContextVariable.reconstruct_type  sE     22FD	_&22	 G$W-r6   r  rs   )rt   ru   rv   r   r&   rw   r   r   r   r   r>   r1   r   r   r@   rP   rx   rS   rT   rW   r{   r|   s   @r5   r  r  J  s     		 	/	/N
 

o.
 4
 	

 
1
 
B "%)

 d

 	

 

7T 7
/ 
O 
7)72A7	7
S 

 
. .r6   r  c                        e Zd ZdZedddedd fd       Zedddefd       Zdeded	edd
f fdZ	de
fdZdddefdZdddedefdZddZ xZS )PreserveVersionContextVariablez?
    Wraps torch.autograd._unsafe_preserve_version_counter
    r7   r$   tensorsr,   c           	         |j                         rKt        j                  |fD cg c]  }|j                  | d       c}      }t        j                  |g      }n[t	        |t        j                        sJ t        j                  |j
                  D cg c]  }|j                  | d       c}      }|}t        ||      S c c}w c c}w )N_version)	is_tensorr   TupleVariabler   rg   r  r  )r7   r  r   versionstensors_tuples        r5   _create_lambda_from_tensorsz:PreserveVersionContextVariable._create_lambda_from_tensors  s    
  ..9@	B1r:.BH &33WI>Mgy'>'>??? ..8?F1r:.FH $M-mXFF C Gs   CCc                 4     t        j                   fd      S )Nc                 0    t         j                  |       S rM   )r  r  )r  r7   s    r5   rZ   z<PreserveVersionContextVariable.constructor.<locals>.<lambda>  s    :VVG r6   )r   LambdaVariable)r7   s   `r5   constructorz*PreserveVersionContextVariable.constructor  s    ''
 	
r6   prev_versionsr+   Nc                 b   |j                  dd        t        |   di | || _        || _        | j                  j                         r%t        j                  | j                  g      | _        | j                  j                         r&t        j                  | j                  g      | _        y y r  )	
setdefaultr0   r1   r  r  r  r   r  is_symnode_like)r3   r  r  r+   r4   s       r5   r1   z'PreserveVersionContextVariable.__init__  s     	/40"6"*<<!!#$22DLL>BDL--/!*!8!8$:L:L9M!ND 0r6   c                 J    t         j                  j                  j                  S rM   )r   r   rN   _unsafe_preserve_version_counterrd   s    r5   r   z*PreserveVersionContextVariable.python_type  s    ~~''HHHr6   c                 @    t         j                  j                  d       S rM   r  r?   s     r5   r@   z$PreserveVersionContextVariable.enter  r  r6   rK   c                     ddl m} t        j                  |      j	                  || j
                  | j                  gi       S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   TorchInGraphFunctionVariablerj   r  r  )r3   r7   rK   r  s       r5   rP   z#PreserveVersionContextVariable.exit  s<     	D55'

-T\\4+=+=>
C	Dr6   c                 T    t        dt        |       dg t        j                         y )NzLtorch.autograd._unsafe_preserve_version_counter escaped from compiled regionzyDynamo doesn't support compiling a region that returns a torch.autograd._unsafe_preserve_version_counter context manager.r  r  rU   s     r5   r`   z*PreserveVersionContextVariable.reconstruct  s-    bIU"..
	
r6   rs   )rt   ru   rv   r   r   r   r  r  r   r1   r   r   r@   rP   r`   r{   r|   s   @r5   r  r    s     G#G G 
*G G" 
/ 
O 
 
O O 'O 	O
 
O IT I7/ 7O 7D)D2AD	D
r6   r  c                   "    e Zd Z e e       ej                        Zeddde	de	de	dd f
d       Z
	 dde	d	ee	   d
ee	   dz  de	ddf
 fdZdddefdZdddedefdZdddee   deeef   def fdZdddee	   ddfdZdefdZdefdZ xZS )&FSDPParamGroupUseTrainingStateVariabler7   r$   param_group_varr.  r+   r,   c                 P    t        d||g|j                  j                  gd|}|S )N)r  r(   r)   r/   )r  r   _training_state)r7   r  r.  r+   r   s        r5   r>   z-FSDPParamGroupUseTrainingStateVariable.create  s>     5 
+'.+11AAB
 	
 
r6   Nr(   r)   c                 b    t        |   d||d| || _        t        | j                         y r   )r0   r1   r  r   r   )r3   r  r(   r)   r+   r4   s        r5   r1   z/FSDPParamGroupUseTrainingStateVariable.__init__  s<     	 	
'	
JP	
  /d,,-r6   c                 x    | j                  || j                         t        j                  j	                  d       S rM   r4  r?   s     r5   r@   z,FSDPParamGroupUseTrainingStateVariable.enter+  r5  r6   rK   c                 x    | j                  || j                         t        j                  j	                  d       S rM   r7  rO   s      r5   rP   z+FSDPParamGroupUseTrainingStateVariable.exit/  r8  r6   c                 ^    | j                  || j                         t        |   |||      S rM   r:  r;  s       r5   rj   z4FSDPParamGroupUseTrainingStateVariable.call_function5  s-     	D//0w$Rv66r6   r  c           	      d   t        |      dk(  sJ |d   }| j                  j                  j                  |k7  rx| j                  j	                  |dt
        j                  j                  |d      t
        j                  j                  ||      fi        || j                  j                  _        y y )Nr   r   __setattr__r  )r^   r  r   r  call_methodr   r   r   r>  s       r5   r:   z1FSDPParamGroupUseTrainingStateVariable._call_func?  s    6{aq	%%55>  ,,--33B8IJ--33B>  :?D  &&6 ?r6   c                      y)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr/   rd   s    r5   rS   z2FSDPParamGroupUseTrainingStateVariable.module_nameN  s    Ur6   c                      y)Nuse_training_stater/   rd   s    r5   rT   z.FSDPParamGroupUseTrainingStateVariable.fn_nameQ      #r6   rM   )rt   ru   rv   r   r   r   FSDP_TRAINING_STATEr   r   r   r>   r   r1   r   r@   rP   ry   rx   rj   r:   rS   rT   r{   r|   s   @r5   r  r    sF   /1<3S3ST#  	
 
2 $ 04	..  }. !,	.
 . 
.7/ 7O 77)72A7	77#7 '7 S/)*	7
 
7?4 ?hsm ?PT ?VS V$ $r6   r  c                       e Zd ZdZe	 ddddedededd f
d       Z	 	 dd
ee	j                  j                  j                     dedededd	f
 fdZedddee   dee   fd       ZdddefdZdddedefdZdefdZdefdZdefdZ xZS )SDPAKernelVariablez)represents torch.nn.attention.sdpa_kernelr7   r$   backendsset_priorityr+   r,   c                     t        |t        j                  j                  j                        r|g}t        d|d |d|}|S )N)r(   r)   r  r/   )rg   r   nn	attention
SDPBackendr  )r7   r  r  r+   r   s        r5   r>   zSDPAKernelVariable.createX  sN     h 2 2 = => zH  
"%
 	
 
r6   Nr(   r)   c                 8    t        |   d||d| || _        y r   )r0   r1   r  )r3   r(   r)   r  r+   r4   s        r5   r1   zSDPAKernelVariable.__init__i  s.     	 	
'	
JP	
 )r6   c           	          |D cg c]M  }| j                   j                  dt        j                  j                  j
                  |j                  fi       O }}|S c c}w r   )rG   r   r   r!  r"  _backend_from_stringr   )r7   r  backendnodess       r5   _backends_to_nodesz%SDPAKernelVariable._backends_to_nodesu  s`     $
  II!!""77	
 
 
s   AAc                 >    t         j                  j                  j                   j                         _         j                  | fd       t         j                  j                  j                   j                   j                          j                  | j                        }|j                  j                  dt         j                  j                  j                  |t         j                        fi        t        j                  j                  d       S )N)with_priorityc                      t         j                  j                  j                   j                   j
                        S )Nr  )r   r!  r"  _sdpa_kernelprev_backendsr  rd   s   r5   rZ   z*SDPAKernelVariable.enter.<locals>.<lambda>  s2    EHH&&33""1B1B 4  r6   r-  rj   )r   r!  r"  _cur_sdpa_kernel_backendsr  r/  r<   r.  r(   r)  rG   r   rz   r   r=   r>   )r3   r7   r  s   `  r5   r@   zSDPAKernelVariable.enter  s    "XX//II++ J 
 		
 	''T->-> 	( 	
 %%b$*<*<=
		HH++$t(()*		
 ))0066r6   rK   c                 B   | j                          | j                  || j                        }|j                  j	                  dt
        j                  j                  j                  |t        | j                        fi        t        j                  j                  d       S r   )rN   r)  r/  rG   r   r   r!  r"  r.  rz   r  r   r=   r>   )r3   r7   rK   r  s       r5   rP   zSDPAKernelVariable.exit  s     	%%b$*<*<=
		HH++$t(()*		
 ))0066r6   c                      y)Nztorch.nn.attentionr/   rd   s    r5   rS   zSDPAKernelVariable.module_name  r  r6   c                      y)N_sdpa_kernel_variadicr/   rd   s    r5   rT   zSDPAKernelVariable.fn_name  s    &r6   c                 "    t         j                  S rM   r  rd   s    r5   r   zSDPAKernelVariable.python_type  r  r6   rE  r   )rt   ru   rv   r   r   r   rz   r>   r   r   r!  r"  r#  r1   r)  r   r@   rP   rx   rS   rT   r   r   r{   r|   s   @r5   r  r  U  s(   3 ##  	
 
 & #"	
)EHH..99:
) 
) 	
)
 
) 

) #s) 
c  7/ 7O 7,7)72A7	7$S $
' '3T 3r6   r  c            	       x     e Zd ZdZ	 ddedededdf fdZdd	d
edefdZdefdZ	defdZ
defdZddZ xZS )FxTracebackAnnotateVariablea  
    fx.traceback.annotate is a context manager that allows users to annotate the
    fx graph nodes with custom metadata. In the context of Dynamo, we don't have
    to trace the body of the context manager. Instead we want to directly run
    the body of the context manager, so the Dynamo created Fx graphs have the
    right custom metadata. This variable tracker just runs __enter__ and
    __exit__ method (instead of tracing).
    Nr(   r)   r+   r,   c                 *    t        |   d||d| y r   rd  r2   s       r5   r1   z$FxTracebackAnnotateVariable.__init__  s$     	 	
'	
JP	
r6   r7   r$   rK   c                 r   t               j                  t        j                  j                  j                  | j                               j                  t        j                  j                  j                                | j                  |fd       t        j                  j                  d       S )Nc                  $     j                         S rM   )close)stacks   r5   rZ   z3FxTracebackAnnotateVariable.enter.<locals>.<lambda>  s    %++- r6   )r   enter_contextr   fx	tracebackannotater(   preserve_node_metar<   r   r=   r>   )r3   r7   rK   r<  s      @r5   r@   z!FxTracebackAnnotateVariable.enter  s~     EHH..778J8JKLEHH..AACDb"78))0066r6   c                      y)Nztorch.fx.tracebackr/   rd   s    r5   rS   z'FxTracebackAnnotateVariable.module_name  r  r6   c                      y)Nr@  r/   rd   s    r5   rT   z#FxTracebackAnnotateVariable.fn_name  r  r6   c                 "    t         j                  S rM   r  rd   s    r5   r   z'FxTracebackAnnotateVariable.python_type  r  r6   c                 T    t        dt        |       dg t        j                         y )Nz8torch.fx.traceback.annotate escaped from compiled regionzBDynamo doesn't support graph break on torch.fx.traceback.annotate.r  r  rU   s     r5   rW   z,FxTracebackAnnotateVariable.reconstruct_type  s*    NI\"..		
r6   rM   rs   )rt   ru   rv   r   r   r1   r   r@   rx   rS   rT   r   r   rW   r{   r|   s   @r5   r7  r7    sx     9=
 
25
HK
	

7)
72A
7	
7$S $ 3T 3
r6   r7  c                   r     e Zd ZdZdeeef   deddf fdZddd	eddfd
ZdefdZ	defdZ
defdZ xZS )DynamoConfigPatchVariablez,represents torch._dynamo.patch_dynamo_configr(   r+   r,   Nc                    t        |j                               }t        |   d|fd d| i }|D ]1  \  }}t        j
                  j                  j                  |      ||<   3 t        |j                               f| _        y r   )	r  r  r0   r1   r   r   r
   r   r)   )r3   r(   r+   target_values_tupleinitial_values_dictr  _r4   s          r5   r1   z"DynamoConfigPatchVariable.__init__  s    #M$7$7$9: 	
.0	
IO	
 !) 	MFC',}}';';'G'G'L$	M$%8%>%>%@ACr6   r7   r$   r  c                     t        |      dk(  sJ |d   }|D ]/  \  }}t        j                  j                  j	                  ||       1 y Nr   r   )r^   r   r   r
   r  )r3   r7   r  r   r  r_   s         r5   r:   z$DynamoConfigPatchVariable._call_func  sJ    6{aq	 	7HCMM  ,,S#6	7r6   c                      yNztorch._dynamor/   rd   s    r5   rS   z%DynamoConfigPatchVariable.module_name      r6   c                      y)Npatch_dynamo_configr/   rd   s    r5   rT   z!DynamoConfigPatchVariable.fn_name      $r6   c                     ddl m} |S )Nr   )DynamoConfigPatchProxy)torch._dynamo.decoratorsrU  )r3   rU  s     r5   r   z%DynamoConfigPatchVariable.python_type  s    C%%r6   )rt   ru   rv   r   ry   rx   r   r1   r:   rS   rT   r   r   r{   r|   s   @r5   rG  rG    sk    6	Dd38n 	D 	D 	D74 7c 7d 7S % %&T &r6   rG  c                   n     e Zd ZdZdededdf fdZddd	ee   ddfd
Zde	fdZ
de	fdZdefdZ xZS )ErrorOnGraphBreakVariablez-represents torch._dynamo.error_on_graph_breakerror_on_graph_breakr+   r,   Nc                 >    t        |   d|ft               fd| y r   )r0   r1   r   )r3   rY  r+   r4   s      r5   r1   z"ErrorOnGraphBreakVariable.__init__  s.     	
/1579	
 	
r6   r7   r$   r  c                 @    t        |      dk(  sJ t        |d          y rM  )r^   r   r}  s      r5   r:   z$ErrorOnGraphBreakVariable._call_func  s     6{a!&),r6   c                      yrO  r/   rd   s    r5   rS   z%ErrorOnGraphBreakVariable.module_name  rP  r6   c                      y)NrY  r/   rd   s    r5   rT   z!ErrorOnGraphBreakVariable.fn_name  r  r6   c                     ddl m} |S )Nr   )(ErrorOnGraphBreakDecoratorContextManager)rV  r_  )r3   r_  s     r5   r   z%ErrorOnGraphBreakVariable.python_type!  s    U77r6   )rt   ru   rv   r   rz   r   r1   r   r:   rx   rS   rT   r   r   r{   r|   s   @r5   rX  rX    sd    7
T 
S 
T 
-4 -htn -QU -S & &8T 8r6   rX  c                        e Zd ZdZdedz  dedz  deddf fdZd fdZdd	deedf   ddfdZ	de
fdZde
fdZdefdZdefdZdd	dedd
fdZ xZS )CudagraphOverrideVariablez,represents torch._dynamo.override_cudagraphsfwdNbwdr+   r,   c                 .    t        |   d||fd d| y r   rd  )r3   rb  rc  r+   r4   s       r5   r1   z"CudagraphOverrideVariable.__init__*  s)     	
*	
 	
r6   r7   r$   r   c                     |j                   j                  }||j                  |j                  f| _        nd| _        t
        |   |      S )NrM   )rG   cudagraph_annotationrb  rc  r)   r0   r@   )r3   r7   prevr4   s      r5   r@   zCudagraphOverrideVariable.enter1  sD    yy--#'88TXX"6D")Dw}R  r6   r  .c                 &   ddl m} t        |      dk(  r2|d   -d |j                  _        d |j                  j
                  _        y t        |      dk(  sJ |\  }} |||      }||j                  _        ||j                  j
                  _        y )Nr   )_CudagraphAnnotationr   r   )rb  rc  )torch._inductorri  r^   rG   rf  tracing_context)r3   r7   r  ri  rb  rc  
annotations          r5   r:   z$CudagraphOverrideVariable._call_func:  s}    8v;!q	 1-1BII*=ABII%%:v;!###HC-#3?J-7BII*=GBII%%:r6   c                      yrO  r/   rd   s    r5   rS   z%CudagraphOverrideVariable.module_nameH  rP  r6   c                      y)Noverride_cudagraphsr/   rd   s    r5   rT   z!CudagraphOverrideVariable.fn_nameK  rS  r6   c                     ddl m} |S )Nr   )CudagraphOverrideContextManager)rV  rq  )r3   rq  s     r5   r   z%CudagraphOverrideVariable.python_typeN  s    L..r6   c                      yr   r/   rd   s    r5   rp   z-CudagraphOverrideVariable.exit_on_graph_breakS  s     r6   rK   c                 @    t         j                  j                  d       S rM   r  rO   s      r5   rP   zCudagraphOverrideVariable.exitX  s     ))0066r6   )r7   r$   r,   r   )rt   ru   rv   r   rz   r   r1   r@   r  r:   rx   rS   rT   r   r   rp   r   rP   r{   r|   s   @r5   ra  ra  '  s    6
D4K 
dTk 
S 
T 
!H4 HeCHo HRV HS % %/T /
T 
7)72A7	7r6   ra  c                   n     e Zd Zdeez  deddf fdZdefdZddd	e	e
   deee
f   de
fd
ZddZ xZS )WithEnterFunctionVariablerR  r+   r,   Nc                 2    t        |   di | || _        y r.   )r0   r1   rR  )r3   rR  r+   r4   s      r5   r1   z"WithEnterFunctionVariable.__init__b  s    
 	"6"r6   c                 "    t         j                  S rM   types
MethodTyperd   s    r5   r   z%WithEnterFunctionVariable.python_typej      r6   r7   r$   rK   c                 ^    |rJ |rJ |j                  | j                  |j                        S rM   )	enter_ctxrR  current_instructionri   s       r5   rj   z'WithEnterFunctionVariable.call_functionm  s0     xz ||DHHb&<&<==r6   c                 \   	 | j                   j                          d| j                   j                          }t        dt	        | j                         d| ddgt        j                  t        j                         y # t        $ r! t	        t        | j                               }Y qw xY w)N.z;Attempted to reconstruct context manager's __enter__ methodz)Attempted to reconstruct context manager z while tracing `with ...:`zIt is likely there is a graph break while tracing `with ctx:` but outside the actual `ctx.__enter__()` method. `torch.compile` does not expect this to happen.r  )
rR  rS   rT   rc   rx   r   r   r   	DIFFICULT
DYNAMO_BUG)r3   rV   type_strs      r5   r`   z%WithEnterFunctionVariable.reconstructz  s    	+((..014883C3C3E2FGH 	QMCH:MghB #,,	
 #--		
 # 	+4>*H	+s   7B 'B+*B+rs   )rt   ru   rv   r&   r~   r   r1   r   r   r   r   ry   rx   rj   r`   r{   r|   s   @r5   ru  ru  a  su    $'EE  
	 T  >#> '> S/)*	>
 
>
r6   ru  c                        e Zd Zdhej                  Zdeez  dededdf fdZde	fdZ
dd	d
ee   deeef   defdZddZ xZS )WithExitFunctionVariabletargetrR  r+   r,   Nc                 p    t        |   di | t        |t        t        f      sJ || _        || _        y r.   )r0   r1   rg   r&   r~   rR  r  )r3   rR  r  r+   r4   s       r5   r1   z!WithExitFunctionVariable.__init__  sC     	"6")+IJ
 	
 
 r6   c                 "    t         j                  S rM   rx  rd   s    r5   r   z$WithExitFunctionVariable.python_type  r{  r6   r7   r$   rK   c                 D    |rJ  | j                   j                  |g| S rM   )rR  rP   ri   s       r5   rj   z&WithExitFunctionVariable.call_function  s&     ztxx}}R'$''r6   c                 :    | j                   j                  |       y rM   )rR  rW   rU   s     r5   r`   z$WithExitFunctionVariable.reconstruct  s     	!!'*r6   rs   )rt   ru   rv   r   rw   r&   r~   r   r1   r   r   r   ry   rx   rj   r`   r{   r|   s   @r5   r  r    s    		'	'N
$'EE  	
 
 T  (#( '( S/)*	(
 
(+r6   r  )Xr   r  r  loggingry  r  collections.abcr   r   r   r   r   typingr   r	   torch._Cr   torch._dynamor
   torch._guardsr   torch._loggingr    r   r   bytecode_transformationr   excr   guardsr   r   r   r   r   utilsr   r   baser   	functionsr   r   r   r   r   r   r    r!   user_definedr"   torch._dynamo.codegenr#   torch._dynamo.symbolic_convertr$   	getLoggerrt   r  r&   r~   r   r   r   r   r   r   r   r  r  r-  rH  r]  rq  r  r  r  r  r  r  r  r  r7  rG  rX  ra  ru  r  r/   r6   r5   <module>r     s  (      5 5 8 %     ' + :  0 2 H !	 	 	 4 /Dg!do dN*%> *Z")+I ")J)70G )7X%77N %7P*7,C *7Z*?&= *?Z(45 (4V(7-D (7V+'&= +'\57-D 57pE&. E&PH$3 H$V6!0 6!rL-#: L-^=3(? =3@H02 H0V&1 &6%
5 %
Ps.,C s.lI
%< I
XF$-D F$R]30 ]3@.
"9 .
b$& 7 $&N8 7 8477 7 77t)
 )
X#+ #+r6   