
    ȅi                        S r SSKrSSKrSSKrSSKrSSKJrJrJr  SSK	J
r
Jr  SSKJrJrJrJr  SSKrSSKJr  SSKJr  SSKJr  S	S
KJrJr  S	SKJrJrJr  S	SK J!r!  S	SK"J#r#J$r$  S	SK%J&r&J'r'  S	SK(J)r)J*r*  SSK+J,r,  SSK-J.r.J/r/J0r0J1r1J2r2J3r3J4r4J5r5  SSK6J7r7  \(       a  SSK8J9r9  SSK:J;r;  \Rx                  " \=5      r> " S S\,5      r? " S S\75      r@ " S S\@5      rA " S S\?5      rB " S S\?5      rC " S  S!\?5      rD " S" S#\?5      rE " S$ S%\?5      rF " S& S'\?5      rG " S( S)\?5      rH " S* S+\?5      rI " S, S-\?5      rJ " S. S/\?5      rK " S0 S1\?5      rL " S2 S3\?5      rM " S4 S5\?5      rN " S6 S7\?5      rO " S8 S9\?5      rP " S: S;\?5      rQ " S< S=\?5      rR " S> S?\?5      rS " S@ SA\?5      rT " SB SC\?5      rU " SD SE\?5      rV " SF SG\?5      rW " SH SI\?5      rX " SJ SK\?5      rY " SL SM\,5      rZ " SN SO\,5      r[g)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OptionalTYPE_CHECKINGUnion)config)Guard)warning_once   )graph_break_hints	variables)create_call_functioncreate_instructioncreate_setup_with)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            	       J  ^  \ rS rSrSSSS1\R
                  kr SS\S\\   S\SS4U 4S	 jjjrS
SS\4S jr	 SS
SS\\
S\4      SS4S jjrS
SS\S\4S jrSS jrSS jrS\4S jrS\4S jrS
SS\\   S\\\4   S\4S jrS\4S jrS\4S jrSS jrSS jrSrU =r$ )ContextWrappingVariableA   cm_objtarget_valuesinitial_valuesstateNkwargsreturnc                 >   > [         TU ]  " S0 UD6  Xl        X l        g N )super__init__r-   r.   selfr-   r.   r0   	__class__s       ]/home/james-whalen/.local/lib/python3.13/site-packages/torch/_dynamo/variables/ctx_manager.pyr6    ContextWrappingVariable.__init__J   s!     	"6"*,    txr(   c                     [        U S5      (       a  U R                  XR                  5        U R                  U5        [        R
                  R                  S 5      $ N
_call_func)hasattrr@   r-   set_cleanup_hookr   ConstantVariablecreater8   r=   s     r:   enterContextWrappingVariable.enterQ   sF    4&&OOB 2 23b!))0066r<   fn.c                 x   ^ ^ Uc	  SU U4S jjnUT l         TR                  R                  T R                  5        g )Nc                  b   > [        T S5      (       a  T R                  TT R                  5        g g r?   )rA   r@   r.   rE   s   r:   rH   4ContextWrappingVariable.set_cleanup_hook.<locals>.fn\   s)    4..OOB(;(;< /r<   r1   N
cleanup_fnoutputadd_cleanup_hookcleanupr8   r=   rH   s   `` r:   rB   (ContextWrappingVariable.set_cleanup_hookW   s3     := = 9;
		""4<<0r<   argsc                 `    U R                  5         [        R                  R                  S 5      $ N)cleanup_assertr   rC   rD   r8   r=   rT   s      r:   exitContextWrappingVariable.exitc   s'     	))0066r<   c                     U" [        UR                  R                  U R                  5       5      U R	                  5       5      5        g rV   )r   r=   import_sourcemodule_namefn_namer8   codegens     r:   reconstruct_type(ContextWrappingVariable.reconstruct_typei   s2    wzz//0@0@0BCT\\^T	
r<   c                   ^ ^ TR                  UU 4S j5        T R                  nU(       d  SnTR                  U Vs/ s H  nTR                  U5      PM     sn5        TR                  [	        [        U5      S5      5        g s  snf )Nc                  &   > TR                  T 5      $ rV   )ra   )r`   r8   s   r:   <lambda>5ContextWrappingVariable.reconstruct.<locals>.<lambda>o   s    d&;&;G&Dr<   r4   F)add_push_nullr-   extend_outputcreate_load_constr   len)r8   r`   r-   vals   ``  r:   reconstruct#ContextWrappingVariable.reconstructn   sn    DE**MW#w88=WX23}3EuMN Xs   Bc                     [        S5      e)Nzmodule_name called on baseNotImplementedErrorr8   s    r:   r]   #ContextWrappingVariable.module_namev   s    !">??r<   c                     [        S5      e)Nzfn_name called on basero   rq   s    r:   r^   ContextWrappingVariable.fn_namey   s    !":;;r<   c                    [        U5      S:X  d   e[        US   [        [        [        [
        45      (       d   e[        US   [        5      (       a  [        US   U 5      $ [        US   [        5      (       a  [        US   U 5      $ [        US   [        5      (       a  [        US   U 5      $ [        US   [
        5      (       a  [        US   U 5      $ [        S5      e)Nr   r   zUnexpected arg type)rj   
isinstancer   r   r!   r    r"   r#   r%   r$   AssertionErrorr8   r=   rT   r0   s       r:   call_function%ContextWrappingVariable.call_function|   s     4yA~~G*$"$	
 
 	
 
 d1g9::4T!WdCCQ!566.tAw==Q!344,T!Wd;;Q!566.tAw== !677r<   c                     gNTr4   rq   s    r:   supports_graph_breaks-ContextWrappingVariable.supports_graph_breaks       r<   c                     gr|   r4   rq   s    r:   exit_on_graph_break+ContextWrappingVariable.exit_on_graph_break   r   r<   c                 N    U R                   b  U R                  5         S U l         g g rV   )rN   rq   s    r:   rQ   ContextWrappingVariable.cleanup   s"    ??&OO"DO 'r<   c                 T    U R                   (       d   S5       eU R                  5         g )Nzmultiple exits?)rN   rQ   rq   s    r:   rW   &ContextWrappingVariable.cleanup_assert   s    1 11r<   )rN   r.   r-   rV   r`   r'   r1   NrL   )__name__
__module____qualname____firstlineno__r   _nonvar_fieldsr   r	   r6   rF   r   rB   rY   ra   rl   strr]   r^   r   dictry   boolr}   r   rQ   rW   __static_attributes____classcell__r9   s   @r:   r*   r*   A   sD   	
 
	'	'N CG- -2:3--RU-	- -7/ 7O 7 OS
1)
1/7c8J/K
1	
17)72A7	7

O@S @< <8#8 '8 S/)*	8
 
88t T #
 r<   r*   c                      ^  \ rS rSrS\\   S\SS4U 4S jjrS\4S jrS\4S jr	S	S
S\
4S jrS	S
S\
S\
4S jrS\4S jrS\4S jrSrU =r$ )GenericContextWrappingVariable   r,   r0   r1   Nc                 V   > Uc   e[         TU ]  " SUUR                  S.UD6  Xl        g )N)value
value_typer4   )r5   r6   r9   r,   )r8   r,   r0   r9   s      r:   r6   'GenericContextWrappingVariable.__init__   s>    !!! 	
''	
 	

 r<   c                 .    U R                   R                  $ rV   )r,   r   rq   s    r:   r]   *GenericContextWrappingVariable.module_name   s    {{%%%r<   c                 @    [        U R                  5      R                  $ rV   )typer,   r   rq   s    r:   r^   &GenericContextWrappingVariable.fn_name   s    DKK )))r<   r=   r(   c                     U R                   c  S O[        U R                   S5      n[        R                  " U R                  R
                  R                  U US9R                  U/ 0 5      $ )N	__enter__source)r   r   r   r!   r,   r   __func__ry   )r8   r=   r   s      r:   rF   $GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r<   rT   c                     U R                   c  S O[        U R                   S5      n[        R                  " U R                  R
                  R                  U US9R                  U[        U5      0 5      nUR                  R                  5         U$ )N__exit__r   )r   r   r   r!   r,   r   r   ry   listactive_generic_context_managerspop)r8   r=   rT   r   xs        r:   rY   #GenericContextWrappingVariable.exit   sx     ,*T[[*2U((KK  ))
 -DJ
+	 	

 	**..0r<   c                     gNFr4   rq   s    r:   r}   4GenericContextWrappingVariable.supports_graph_breaks   s    r<   c                     gr|   r4   rq   s    r:   r   2GenericContextWrappingVariable.exit_on_graph_break   r   r<   )r,   )r   r   r   r   r   r   r6   r   r]   r^   r   rF   rY   r   r}   r   r   r   r   s   @r:   r   r      s    5c: c d &S &* *$/ $O $
)
2A
	
t T  r<   r   c                   d    \ rS rSrS\S\SS4S jrSSS\4S	 jrSSS
\S\4S jr	S\
S\4S jrSrg)$RepararametrizeModuleContextVariable   ctx_manager_vtmodr1   Nc                     Xl         X l        g rV   )cm_vtr   )r8   r   r   s      r:   r6   -RepararametrizeModuleContextVariable.__init__   s    #
r<   r=   r(   c                 L   [         R                  R                  R                  R	                  U5         U R
                  R                  US5      R                  5       U l        U R
                  R                  US5      R                  5       U l	        UR                  R                  R                  U R                  5        UR                  R                  R                  U R                  5        U R                  R                  U5      sS S S 5        $ ! , (       d  f       g = f)N_parameters_buffers)torch_dynamor   higher_order_ops dynamo_allow_side_effects_in_hopr   var_getattrrealizeold_parameters_varold_buffer_varrO   side_effectsignore_mutations_onr   rF   rE   s     r:   rF   *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#rT   c                    [         R                  R                  R                  R	                  U5         U R
                  R                  " U/UQ76 nUR                  R                  R                  U R                  5        UR                  R                  R                  U R                  5        UsS S S 5        $ ! , (       d  f       g = frV   )r   r   r   r   r   r   rY   rO   r   stop_ignoring_mutations_onr   r   )r8   r=   rT   r   s       r:   rY   )RepararametrizeModuleContextVariable.exit   s    
 ]]$$55VV
 

*T*AII""==d>Q>QRII""==d>U>UV
 
 
s   A=B;;
C	namec                 .    [        U R                  U5      $ rV   )getattrr   )r8   r   s     r:   __getattr__0RepararametrizeModuleContextVariable.__getattr__   s    tzz4((r<   )r   r   r   r   )r   r   r   r   r*   r   r6   r   rF   rY   r   r   r   r4   r<   r:   r   r      sa    '> S T 

(/ 
(O 
()2A	) ) )r<   r   c            	       d    \ rS rSrSr\SSS\S\SS 4S j5       rSSS\4S	 jr	SSS
\S\4S jr
Srg))GradInplaceRequiresGradCtxManagerVariable   z#represents torch grad requires gradr=   r(   r-   r0   r1   c                     [        SUS S.UD6$ Nr-   r.   r4   )r   r=   r-   r0   s      r:   rD   0GradInplaceRequiresGradCtxManagerVariable.create   s&     9 
'
 
 	
r<   c                   ^  T R                   u  n[        R                  R                  R	                  5       T l        [        R                  R                  R                  U5        T R                  UU 4S j5        UR                  R                  S[        R                  R                  R                  U40 5      T l
        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ rV   )r   _C
_functorch!set_inplace_requires_grad_allowed
prev_staterq   s   r:   re   AGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>  s!    EHH''IIr<   ry   )r-   r   r   r   !get_inplace_requires_grad_allowedr   r   rB   rO   create_nodeproxyr   rC   rD   )r8   r=   enableds   `  r:   rF   /GradInplaceRequiresGradCtxManagerVariable.enter  s    &&	((--OOQ==gF	
 YY**HHAAJ	

 ))0066r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  R                  S 5      $ Nry   )rQ   rO   r   r   r   r   r   r   r   rC   rD   rX   s      r:   rY   .GradInplaceRequiresGradCtxManagerVariable.exit  s[     	
		HHAA__		
 ))0066r<   r   r   Nr   r   r   r   __doc__staticmethodr   rD   r   rF   rY   r   r4   r<   r:   r   r      sh    -
#
47
CF
	4
 
7/ 7O 7$
7)
72A
7	
7r<   r   c            	       d    \ rS rSrSr\SSS\S\SS 4S j5       rSSS\4S	 jr	SSS
\S\4S jr
Srg)0TemporarilyPopInterpreterStackCtxManagerVariablei&  zJrepresents torch._functorch.pyfunction.temporarily_pop_interpreter_stack()r=   r(   r-   r0   r1   c                     [        SUS S.UD6$ r   )r   r   s      r:   rD   7TemporarilyPopInterpreterStackCtxManagerVariable.create)  s'     @ 
'
 
 	
r<   c                 R  ^  [         R                  R                  R                  5       T l        T R                  UU 4S j5        UR                  R                  S[         R                  R                  R                  S0 5      T l        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ rV   )r   r   r   push_dynamic_layer_stacksavedrq   s   r:   re   HTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>7  s    EHH''@@Lr<   ry   r4   )r   r   r   pop_dynamic_layer_stackr   rB   rO   r   r   r   rC   rD   rE   s   ` r:   rF   6TemporarilyPopInterpreterStackCtxManagerVariable.enter3  s~    XX((@@B
L	
 YY**HH77	

 ))0066r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  R                  S 5      $ r   )rQ   rO   r   r   r   r   r   r   r   rC   rD   rX   s      r:   rY   5TemporarilyPopInterpreterStackCtxManagerVariable.exitA  sZ     	
		HH88ZZM		
 ))0066r<   )r   r   Nr   r4   r<   r:   r   r   &  sh    T
#
47
CF
	;
 
7/ 7O 7
7)
72A
7	
7r<   r   c                       \ rS rSrSr\" \" 5       \R                  5      r	\
SSS\SS 4S j5       rSSS\4S jrSSS	\S\4S
 jrSrg)%JvpIncrementNestingCtxManagerVariableiN  z5represents torch.func.jvp increment/decrement nestingr=   r(   r0   r1   c                 "    [        SS S S.UD6nU$ r   )r   r=   r0   vars      r:   rD   ,JvpIncrementNestingCtxManagerVariable.createX  s*     4 

 

 
r<   c                 j   [        U R                  5        [        R                  R                  R                  5       nU R                  US 5        UR                  R                  S[        R                  R                  R                  S0 5      U l        [        R                  R                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ rV   )r   r   eager_transformsexit_jvp_nestingr4   r<   r:   re   =JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>g  s    ((99JJLr<   ry   r4   )r   _guards_singletonr   r   r   enter_jvp_nestingrB   rO   r   r   _jvp_increment_nestingr   r   rC   rD   )r8   r=   	jvp_levels      r:   rF   +JvpIncrementNestingCtxManagerVariable.enterc  s    d,,-$$55GGI	L	
 YY**HH66	

 ))00;;r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R                  S 5      $ Nry   r4   )
rQ   rO   r   r   r   r   _jvp_decrement_nestingr   rC   rD   rX   s      r:   rY   *JvpIncrementNestingCtxManagerVariable.exitq  sQ     	
		UXX00GGR	
 ))0066r<   r   Nr   r   r   r   r   r   r   r   FUNCTORCH_STACK_MATCHr  r   r   rD   r   rF   rY   r   r4   r<   r:   r   r   N  sy    ? /1<3U3UV#/2	0 </ <O <7)72A7	7r<   r   c            	       d    \ rS rSrSr\SSS\S\SS 4S j5       rSSS\4S	 jr	SSS
\S\4S jr
Srg)SetFwdGradEnabledContextManageri{  zWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradr=   r(   r-   r0   r1   c                     [        SUS S.UD6$ r   )r  r   s      r:   rD   &SetFwdGradEnabledContextManager.create~  s&     / 
'
 
 	
r<   c                   ^  T R                   u  n[        R                  R                  5       T l        [        R                  R                  U5        T R                  UU 4S j5        UR                  R                  S[        R                  R
                  U40 5      T l	        [        R                  R                  S 5      $ )Nc                  V   > [         R                  R                  T R                  5      $ rV   )r   r   _set_fwd_grad_enabledr   rq   s   r:   re   7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>  s    EHH224??Cr<   ry   )r-   r   r   _is_fwd_grad_enabledr   r  rB   rO   r   r   r   rC   rD   )r8   r=   modes   `  r:   rF   %SetFwdGradEnabledContextManager.enter  s    ##((779&&t,C	
 YY**HH**G	

 ))0066r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S 5      $ r   )
rQ   rO   r   r   r   r  r   r   rC   rD   rX   s      r:   rY   $SetFwdGradEnabledContextManager.exit  sU     	
		HH**__		
 ))0066r<   r   Nr   r4   r<   r:   r  r  {  sh    a
#
47
CF
	*
 
7/ 7O 7 
7)
72A
7	
7r<   r  c                       \ rS rSrSr\" \" 5       \R                  5      r	\
SSS\SS 4S j5       rSSS\4S jrSSS	\S\4S
 jrSrg)DualLevelContextManageri  z;Represents torch.autograd.forward_ad.dual_level ctx managerr=   r(   r0   r1   c                     [        SS S S.UD6$ r   )r  )r=   r0   s     r:   rD   DualLevelContextManager.create  s$    & 

 
 	
r<   c                 |  ^  [        T R                  5        [        R                  R                  R                  5       T l        T R                  UU 4S j5        UR                  R                  S[        R                  R                  S0 5      T l        [        R                  R                  T R                  5      $ )Nc                  f   > [         R                  R                  R                  T R                  S9$ )N)level)r   autograd
forward_adexit_dual_level	new_levelrq   s   r:   re   /DualLevelContextManager.enter.<locals>.<lambda>  s!    11AAAWr<   ry   r4   )r   r  r   r"  r#  enter_dual_levelr%  rB   rO   r   r   _enter_dual_levelr   r   rC   rD   rE   s   ` r:   rF   DualLevelContextManager.enter  s    d,,-22CCEW	
 YY**HH&&	

 ))00@@r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S 5      $ r   )
rQ   rO   r   r   r   _exit_dual_levelr%  r   rC   rD   rX   s      r:   rY   DualLevelContextManager.exit  sU     	
		HH%%^^		
 ))0066r<   )r%  r   N)r   r   r   r   r   r   r   r   
DUAL_LEVELr  r   r   rD   r   rF   rY   r   r4   r<   r:   r  r    sy    E/1<3J3JK
* 
c 
>W 
 
A/ AO A
7)
72A
7	
7r<   r  c                       \ rS rSrSr\" \" 5       \R                  5      r	\
SSS\SS 4S j5       rSSS\4S jrSSS	\S\4S
 jrSrg)&GradIncrementNestingCtxManagerVariablei  z6represents torch.func.grad increment/decrement nestingr=   r(   r0   r1   c                 "    [        SS S S.UD6nU$ r   )r/  r   s      r:   rD   -GradIncrementNestingCtxManagerVariable.create  s*     5 

 

 
r<   c                 j   [        U R                  5        [        R                  R                  R                  5       nU R                  US 5        UR                  R                  S[        R                  R                  R
                  S0 5      U l	        [        R                  R                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ rV   )r   r   r   _grad_decrement_nestingr4   r<   r:   re   >GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*Wr<   ry   r4   )r   r  r   r   r   _grad_increment_nestingrB   rO   r   r   r   rC   rD   )r8   r=   
grad_levels      r:   rF   ,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WXYY**HH77	

 ))00<<r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R                  S 5      $ r  )
rQ   rO   r   r   r   r   r4  r   rC   rD   rX   s      r:   rY   +GradIncrementNestingCtxManagerVariable.exit  sQ     	
		UXX00HH"b	
 ))0066r<   r  Nr  r4   r<   r:   r/  r/    sy    @ /1<3U3UV#/2	1 
=/ 
=O 
=7)72A7	7r<   r/  c                      ^  \ rS rSrSr\SSS\\\4   SS 4S j5       r	  SS\\\4   S	\
\   S
\
\   S\SS4
U 4S jjjrSSS\4S jrSS jrSrU =r$ )CatchWarningsCtxManagerVariablei  z'Delay a call to warnings.catch_warningsr=   r(   catch_warnings_argsr1   c                     [        US S S9$ )N)r>  r-   r.   )r=  )r=   r>  s     r:   rD   &CatchWarningsCtxManagerVariable.create  s     / 3
 	
r<   Nr-   r.   r0   c                 n   > [        U[        5      (       d   U5       e[        TU ]  " SX#S.UD6  Xl        g r   )rv   r   r5   r6   r>  )r8   r>  r-   r.   r0   r9   s        r:   r6   (CatchWarningsCtxManagerVariable.__init__  sD     -t44I6II4 	
'	
JP	
 $7 r<   c                 :  ^ U R                   R                  5        VVs0 s H  u  p#X#R                  5       _M     nnn[        R                  " S0 UD6mU R                  UU4S j5        [        R                  R                  TR                  5       5      $ s  snnf )Nc                  *   > T R                  S S S 5      $ rV   )r   )ctx_vals   r:   re   7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*Lr<   r4   )
r>  itemsas_python_constantwarningscatch_warningsrB   r   rC   rD   r   )r8   r=   kvr0   rE  s        @r:   rF   %CatchWarningsCtxManagerVariable.enter  s    262J2J2P2P2R
2R$!A##%%2R 	 
 ))3F3b"LM))001B1B1DEE
s   Bc                   ^ TR                  U4S j5        TR                  U R                  R                  5       5        [	        U R                  R                  5       5      nTR                  TR                  [        U5      US5      5        g )Nc                  (   > T R                  SS5      $ )NrI  rJ  )load_import_from)r`   s   r:   re   =CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    G,,Z9IJr<   F)	rg   foreachr>  valuestuplekeysrh   create_call_function_kwrj   )r8   r`   rU  s    ` r:   rl   +CatchWarningsCtxManagerVariable.reconstruct  sk    J	
 	00779:T--2245g==c$iuUVr<   )r>  NNr   )r   r   r   r   r   r   r   r   r   rD   r	   r   r6   rF   rl   r   r   r   s   @r:   r=  r=    s    1
#
:>sO?S:T
	*
 
 (,(,	7!#"677  }7 !	7
 7 
7 7F/ FO FW Wr<   r=  c            	           \ rS rSrSr\" \" 5       \R                  5      r	\
SSS\\   S\SS 4S j5       rSSS\4S	 jrSSS
\S\4S jrSrg)&VmapIncrementNestingCtxManagerVariablei#  z1represents torch VMap increment/decrement nestingr=   r(   r-   r0   r1   c                 "    [        SUS S.UD6nU$ r   )rZ  )r=   r-   r0   r   s       r:   rD   -VmapIncrementNestingCtxManagerVariable.create-  s*     5 
'
 

 
r<   c                 @   [        U R                  5        U R                  u  p#[        U[        R
                  5      (       a  UR                  nOUR                  5       nUR                  5       n[        R                  R                  R                  XC5      nU R                  US 5        UR                  R                  S[        R                  R                  R                  UR!                  5       U40 5      U l        [        R$                  R'                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ rV   )r   r   r   _vmap_decrement_nestingr4   r<   r:   re   >VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>E  r6  r<   ry   )r   r  r-   rv   r   SymNodeVariablesym_numrH  r   r   r   _vmap_increment_nestingrB   rO   create_proxypredispatchas_proxyr   rC   rD   )r8   r=   
batch_size
randomnessbatch_size_value
vmap_levels         r:   rF   ,VmapIncrementNestingCtxManagerVariable.enter:  s    d,,-!%!3!3
j)";";<<)11)<<>224
XX((@@

 	b"WXYY++((@@  "J/	

 ))00<<r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R                  S 5      $ r  )
rQ   rO   r   r   r   re  r_  r   rC   rD   rX   s      r:   rY   +VmapIncrementNestingCtxManagerVariable.exitN  sV     	
		((@@		
 ))0066r<   r  N)r   r   r   r   r   r   r   r   r  r  r   r   r   r   rD   rF   rY   r   r4   r<   r:   rZ  rZ  #  s    ; /1<3U3UV
#
0
 
 
2	
 
=/ =O =(
7)
72A
7	
7r<   rZ  c                   @  ^  \ rS rSrSr\" \" 5       \R                  5      r	\
 SSSS\S\S\SS 4
S	 jj5       r  SS\S\\\      S\S\SS
4
U 4S jjjrSSS\4S jrSSS\S\4S jrSSS\\   S\\\4   S\4U 4S jjrSSS\SS
4S jrS\4S jrS\4S jrSrU =r$ )GradModeVariablei[  z6represents torch.{no_grad,enable_grad,set_grad_mode}()r=   r(   target_valueinitializedr0   r1   c                     [        SU/[        R                  " 5       /S.UD6nU(       a  UR                  XR                  5        U$ r   )ro  r   is_grad_enabledr@   r-   )r=   rp  rq  r0   r   s        r:   rD   GradModeVariable.create`  sM      
'.!1134
 

 NN2001
r<   Nr-   r.   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   r5   r6   r   r  )r8   r-   r.   rq  r0   r9   s        r:   r6   GradModeVariable.__init__p  s2     	 	
'	
JP	
 	d,,-r<   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ rV   r@   r-   r   rC   rD   rE   s     r:   rF   GradModeVariable.enter|  +    ../))0066r<   rT   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ rV   r@   r.   r   rC   rD   rX   s      r:   rY   GradModeVariable.exit  -     	//0))0066r<   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ rV   r@   r.   r5   ry   r8   r=   rT   r0   r9   s       r:   ry   GradModeVariable.call_function  s)     	//0w$Rv66r<   rS  c                    [        U5      S:X  d   eUS   n[        R                  " 5       U:w  aW  UR                  R	                  S[        R
                  R                  U40 5        [        R
                  R                  U5        g g Nr   r   ry   )rj   r   rs  rO   r   r   _set_grad_enabledr8   r=   rS  r   s       r:   r@   GradModeVariable._call_func  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r<   c                     gNr   r4   rq   s    r:   r]   GradModeVariable.module_name      r<   c                     g)Nset_grad_enabledr4   rq   s    r:   r^   GradModeVariable.fn_name  s    !r<   r4   Fr|   )r   r   r   r   r   r   r   r   	GRAD_MODEr  r   r   r   rD   r	   r   r6   r   rF   rY   r   r   ry   r@   r]   r^   r   r   r   s   @r:   ro  ro  [  sP   @/1<3I3IJ "#  	
 
 $ 48 	
.
. !$0
. 	
.
 
. 

. 
.7/ 7O 77)72A7	77#7 '7 S/)*	7
 
7.4 .c .d .S " " "r<   ro  c            	          ^  \ rS rSr\SSS\S\SS 4S j5       r SS	\S
\\   S\SS4U 4S jjjr	SSS\
S\
4S jrSSS\
4S jrS\4S jrS\4S jrSrU =r$ )InferenceModeVariablei  r=   r(   rp  r0   r1   c                 J    [        U/4S[        R                  " 5       0UD6nU$ )Nr.   )r  r   is_inference_mode_enabledr=   rp  r0   r   s       r:   rD   InferenceModeVariable.create  s3     $N
+0+J+J+L
PV
 
r<   Nr-   r.   c                 Z   > Uc  [         R                  " 5       n[        TU ]  " SXS.UD6  g r   )r   r  r5   r6   r7   s       r:   r6   InferenceModeVariable.__init__  s6     !"<<>N 	
'	
JP	
r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  R                  S 5      $ r   )rW   rO   r   r   r"  	grad_mode_exit_inference_moder   r   rC   rD   rX   s      r:   rY   InferenceModeVariable.exit  s]     	
		NN$$99ZZM		
 ))0066r<   c                 n  ^^^ Sm[         R                  R                  R                  (       aK  U R                  S   (       a7  Sm[         R
                  " 5       m[         R                  R                  S5        O1[         R                  R                  R                  " U R                  6 mSUUU4S jjnU R                  X5        UR                  R                  S[         R                  R                  R                  / U R                  Q70 5      U l        [        R                   R#                  S 5      $ )NFr   Tc                     > T(       a   [         R                  R                  T5        g [         R                  R                  R                  T 5        g rV   )r   r   r  r"  r  r  )ctx disabled_inference_mode_forciblypriors   r:   cleanup_hook1InferenceModeVariable.enter.<locals>.cleanup_hook  s0    /**51((==cBr<   ry   rL   )r   r   r   "fake_tensor_disable_inference_moder-   rs  r   r  r"  r  _enter_inference_moderB   rO   r   r   r   rC   rD   )r8   r=   r  r  r  r  s      @@@r:   rF   InferenceModeVariable.enter  s    +0(MM  CC""1%
 04,))+EHH&&u-..**@@$BTBTUC	C 	C 	b/YY**NN$$::"d  "	

 ))0066r<   c                     gr  r4   rq   s    r:   r]   !InferenceModeVariable.module_name  r  r<   c                     g)Ninference_moder4   rq   s    r:   r^   InferenceModeVariable.fn_name  s    r<   r  rV   )r   r   r   r   r   r   rD   r	   r   r6   r   rY   rF   r   r]   r^   r   r   r   s   @r:   r  r    s    #36BE	   *.

 !
 	

 

 

7)
72A
7	
77/ 7O 7<S      r<   r  c            	          ^  \ rS rSrSr\SSS\S\SS 4S j5       r SS
\S\\   S\SS	4U 4S jjjr	SSS\
S\
4S jrSSS\
4S jrS\4S jrS\4S jrSrU =r$ )CUDADeviceVariablei  zrepresents torch.cuda.devicer=   r(   devicer0   r1   c                 \    [        S[        R                  R                  USS9/S S.UD6nU$ )NT)optionalr   r4   )r  r   cuda_get_device_index)r=   r  r0   r   s       r:   rD   CUDADeviceVariable.create  sA     ! 
 ::777NO
 

 
r<   Nr-   r.   c                 *   > [         TU ]  " SXS.UD6  g r   r5   r6   r7   s       r:   r6   CUDADeviceVariable.__init__  "     	 	
'	
JP	
r<   rT   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S5      $ )Nry   F)
rW   rO   r   r   r  _maybe_exchange_devicer   r   rC   rD   rX   s      r:   rY   CUDADeviceVariable.exit   sW     	
		JJ--ZZM		
 ))0077r<   c                 L  ^ [         R                  R                  " U R                  6 mU R	                  UU4S j5        UR
                  R                  S[         R                  R                  / U R                  Q70 5      U l        [        R                  R                  S 5      $ )Nc                  B   > [         R                  R                  T 5      $ rV   )r   r  r  )prev_idxs   r:   re   *CUDADeviceVariable.enter.<locals>.<lambda>  s    %***K*KH*Ur<   ry   )r   r  _exchange_devicer-   rB   rO   r   r   r   rC   rD   )r8   r=   r  s     @r:   rF   CUDADeviceVariable.enter  s~    ::..0B0BCb"UVYY**JJ''"d  "	

 ))0066r<   c                     g)Nz
torch.cudar4   rq   s    r:   r]   CUDADeviceVariable.module_name      r<   c                     g)Nr  r4   rq   s    r:   r^   CUDADeviceVariable.fn_name  s    r<   r  rV   )r   r   r   r   r   r   r   rD   r	   r6   r   rY   rF   r   r]   r^   r   r   r   s   @r:   r  r    s    &#-0<?	  )-

 !
 	

 

 

8)
82A
8	
8	7/ 	7O 	7S   r<   r  c                     ^  \ rS rSrSr\" \" 5       \R                  5      r	\
SSS\SS 4S j5       r  SSSS	\S
\\   S\S\SS4U 4S jjjr SSSS\S\4   S-  SS4S jjrSSS\SS4S jrS\4S jrS\4S jrSrU =r$ )TorchFunctionDisableVariablei  z>represents whether torch function overrides are enabled or notr=   r(   r0   r1   c                 $    [        U 4/ / S.UD6nU$ )Nr   )r  r   s      r:   rD   #TorchFunctionDisableVariable.create#  s-     +

 	
 
r<   Nr-   r.   only_subclassc                   > [        U5      S:X  d   eUb  [        U5      S:X  d   eX@l        UR                  R                  U l        UR                  R
                  U l        [        TU ]   " SX#S.UD6  [        U R                  5        g )Nr   r   r4   )rj   r  symbolic_torch_function_statetorch_function_subclass_enabled'initial_torch_function_subclass_enabledtorch_function_mode_enabled#initial_torch_function_mode_enabledr5   r6   r   r  )r8   r=   r-   r.   r  r0   r9   s         r:   r6   %TorchFunctionDisableVariable.__init__/  s     =!Q&&&)c..AQ.FFF*,,LL 	4 ,,HH 	0 	 	
'	
JP	
 	d,,-r<   rH   .c                 x   ^ ^ Uc	  SU U4S jjnUT l         TR                  R                  T R                  5        g )Nc                     > T R                   TR                  l        T R                  (       d  T R                   TR                  l        g g rV   )r  r  r  r  r  rE   s   r:   rH   9TorchFunctionDisableVariable.set_cleanup_hook.<locals>.fnM  sA    @@ 00P ))DD 44P *r<   rL   rM   rR   s   `` r:   rB   -TorchFunctionDisableVariable.set_cleanup_hookF  s3    
 :  
		""4<<0r<   rS  c                     [        U5      S:X  d   eSUR                  l        U R                  (       d  SUR                  l        g g )Nr   F)rj   r  r  r  r  r8   r=   rS  s      r:   r@   'TorchFunctionDisableVariable._call_funcY  s>    6{aKP((H!!KPB,,H "r<   c                     g)Nztorch._Cr4   rq   s    r:   r]   (TorchFunctionDisableVariable.module_name_      r<   c                 (    U R                   (       a  gg)NDisableTorchFunctionSubclassDisableTorchFunction)r  rq   s    r:   r^   $TorchFunctionDisableVariable.fn_nameb  s    1%r<   )rN   r  r  r  r|   rV   )r   r   r   r   r   r   r   r   TORCH_FUNCTION_STATEr  r   r   rD   r   r	   r   r6   r   rB   r@   r   r]   r^   r   r   r   s   @r:   r  r    s   H/1<3T3TU	#	/2		'	 	 +/".#. . !	.
 . . 
. .4 )-1#1 S#X%1 
	1&Q4 Qe Q QS & & &r<   r  c            	          ^  \ rS rSrSr\" \" 5       \R                  5      r	\
SSS\S\SS 4S j5       r SS
\\   S\\\      S\SS	4U 4S jjjrSSS\4S jrSSS\\   SS	4S jrS\4S jrS\4S jrSrU =r$ )DeterministicAlgorithmsVariableih  zVrepresents torch.{are_deterministic_algorithms_enabled,use_deterministic_algorithms}()r=   r(   rp  r0   r1   c                     [        SU/[        R                  " 5       /S.UD6nUR                  X/5        UR	                  U 5        U$ r   )r  r   $are_deterministic_algorithms_enabledr@   rB   r  s       r:   rD   &DeterministicAlgorithmsVariable.createp  sT     . 
'.!FFHI
 

 	r>*R 
r<   Nr-   r.   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   rv  r7   s       r:   r6   (DeterministicAlgorithmsVariable.__init__}  s2     	 	
'	
JP	
 	d,,-r<   c                 @    [         R                  R                  S 5      $ rV   r   rC   rD   rE   s     r:   rF   %DeterministicAlgorithmsVariable.enter      ))0066r<   rS  c                     [        U5      S:X  d   eUS   nUR                  R                  S[        R                  R
                  U40 5        [        R                  R                  U5        g r  )rj   rO   r   r   r   _set_deterministic_algorithmsr  s       r:   r@   *DeterministicAlgorithmsVariable._call_func  sX    6{aq	
		UXXCCeXr	
 	..u5r<   c                     gr  r4   rq   s    r:   r]   +DeterministicAlgorithmsVariable.module_name  r  r<   c                     g)Nuse_deterministic_algorithmsr4   rq   s    r:   r^   'DeterministicAlgorithmsVariable.fn_name  s    -r<   r4   rV   )r   r   r   r   r   r   r   r   DETERMINISTIC_ALGORITHMSr  r   r   r   rD   r   r	   r6   r   rF   r@   r   r]   r^   r   r   r   s   @r:   r  r  h  s    `--
 
#
37
CF
	*
 
 48	.~	. !$0	. 		.
 
	. 	.7/ 7O 764 6htn 6QU 6S . . .r<   r  c            	          ^  \ rS rSrSr\SSS\\   S\SS 4S j5       r	 SS
\
\\      S\\
\\         S\SS	4U 4S jjjrSSS\4S jrSSS\
\\      SS	4S jrS\4S jrS\4S jrSrU =r$ )!DisabledSavedTensorsHooksVariablei  z;represents torch.autograd.graph.disable_saved_tensors_hook.r=   r(   rp  r0   r1   c                     [        SU/[        R                  R                  R	                  5       /S.UD6nUR                  X/5        UR                  U 5        U$ r   )r  r   r   	_autograd/_saved_tensors_hooks_get_disabled_error_messager@   rB   r  s       r:   rD   (DisabledSavedTensorsHooksVariable.create  s`     0 
'.""RRT

 
 	r>*R 
r<   Nr-   r.   c                 *   > [         TU ]  " SXS.UD6  g r   r  r7   s       r:   r6   *DisabledSavedTensorsHooksVariable.__init__  r  r<   c                 @    [         R                  R                  S 5      $ rV   r  rE   s     r:   rF   'DisabledSavedTensorsHooksVariable.enter  r  r<   rS  c                    [        U5      S:X  d   eUS   nUbk  UR                  R                  S[        R                  R
                  R                  U40 5        [        R                  R
                  R                  U5        g UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R
                  R                  5         g )Nr   r   ry   r4   )rj   rO   r   r   r   r  _saved_tensors_hooks_disable_saved_tensors_hooks_enabler  s       r:   r@   ,DisabledSavedTensorsHooksVariable._call_func  s     6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r<   c                     g)Nztorch.autograd.graphr4   rq   s    r:   r]   -DisabledSavedTensorsHooksVariable.module_name      %r<   c                     g)Ndisable_saved_tensors_hooksr4   rq   s    r:   r^   )DisabledSavedTensorsHooksVariable.fn_name  s    ,r<   r4   rV   )r   r   r   r   r   r   r	   r   r   rD   r   r6   r   rF   r@   r]   r^   r   r   r   s   @r:   r  r    s    E#3;C=LO	, " =A
.
 !(3-!89
 	

 

 
7/ 7O 7=)=3;HSM3J=	=.&S &- - -r<   r  c            	         ^  \ rS rSr\S\R                  R                  R                  S\	\
   S\\\
4   SS 4S j5       r SS\	\
   S	\\
   S\
SS4U 4S
 jjjrSSS\S\4S jrSSS\4S jrS\4S jrS\4S jrSrU =r$ )AutocastModeVariablei  funcrT   r0   r1   c                 N   U [         R                  R                  R                  [         R                  R                  R                  [         R
                  R                  R                  4;   d   e[        R                  " U 5      R                  " U0 UD6nUR                  5         / nUR                  5         S H  nUS:X  ax  U [         R                  R                  R                  [         R
                  R                  R                  4;   a,  U [         R                  R                  R                  L a  SOSnOUR                  U   n[        U[        5      (       a!  UR                  UR                  5       5        M  UR                  U5        M     [!        U4SS 0UD6nU$ )N)device_typedtyper   cache_enabledr  r  cpur.   )r   ampautocast_modeautocastr  r  inspect	signaturebindapply_defaultsclear	argumentsrv   r   appendrH  r  )r  rT   r0   
bound_argsr-   keyargr   s           r:   rD   AutocastModeVariable.create  sH    II##,,JJNN##IIMM""
 
 	
 
 &&t,114B6B
!!#GCm#

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

(?(? ?fU **3/#//$$S%;%;%=>$$S) H #=PPP
r<   Nr-   r.   c                 *   > [         TU ]  " SXS.UD6  g r   r  r7   s       r:   r6   AutocastModeVariable.__init__  r  r<   r=   r(   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S 5      $ r   )
rW   rO   r   r   r  _exit_autocastr   r   rC   rD   rX   s      r:   rY   AutocastModeVariable.exit  sT     	
		UYY55

}b	
 ))0066r<   c                 L  ^ [         R                  R                  " U R                  6 mU R	                  UU4S j5        UR
                  R                  S[         R                  R                  / U R                  Q70 5      U l        [        R                  R                  S 5      $ )Nc                  B   > [         R                  R                  T 5      $ rV   )r   r  r#  r  s   r:   re   ,AutocastModeVariable.enter.<locals>.<lambda>  s    %))*B*B3*Gr<   ry   )r   r  _enter_autocastr-   rB   rO   r   r   r   rC   rD   )r8   r=   r  s     @r:   rF   AutocastModeVariable.enter  s}    ii''););<b"GHYY**UYY668N$:L:L8NPR

 ))0066r<   c                     g)Nztorch.amp.autocast_moder4   rq   s    r:   r]    AutocastModeVariable.module_name  s    (r<   c                     g)Nr  r4   rq   s    r:   r^   AutocastModeVariable.fn_name  r  r<   r  rV   )r   r   r   r   r   r   r  r  r  r   r   r   r   rD   r	   r6   r   rY   rF   r]   r^   r   r   r   s   @r:   r  r    s    !ii%%..!sm! S#X! 
 	! !L )-
}
 !
 	

 

 
7)72A7	77/ 7O 7)S )  r<   r  c                      ^  \ rS rSrSrSS\\   S\SS4U 4S jjjrSS	S\4S
 jr	SS	S\S\4S jr
S\4S jrS\4S jrSrU =r$ )NullContextVariablei  z6
This class represents Python contextlib.nullcontext.
Nr-   r0   r1   c                 *   > [         TU ]  " SSU0UD6  g Nr-   r4   r  )r8   r-   r0   r9   s      r:   r6   NullContextVariable.__init__#  s    ?}??r<   r=   r(   c                 ~    [         R                  R                  S 5      nU R                  (       a  U R                  $ U$ rV   )r   rC   rD   r-   )r8   r=   nones      r:   rF   NullContextVariable.enter&  s1    ))006%)%7%7t!!ATAr<   rT   c                 @    [         R                  R                  S 5      $ rV   r  rX   s      r:   rY   NullContextVariable.exit*       ))0066r<   c                     gN
contextlibr4   rq   s    r:   r]   NullContextVariable.module_name/  r  r<   c                     gNnullcontextr4   rq   s    r:   r^   NullContextVariable.fn_name2      r<   r4   rV   )r   r   r   r   r   r	   r   r6   r   rF   rY   r   r]   r^   r   r   r   s   @r:   r0  r0    s    @hsm @c @d @ @B/ BO B7)72A7	7
S   r<   r0  c                      ^  \ rS rSrSrS\SS4U 4S jjrSSS\4S	 jrSSS
\S\4S jr	S\
4S jrS\
4S jrSS jrSrU =r$ )ProfilerContextVariablei6  af  
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.
r0   r1   Nc                 *   > [         TU ]  " SSS 0UD6  g r2  r  )r8   r0   r9   s     r:   r6    ProfilerContextVariable.__init__?  s    6t6v6r<   r=   r(   c                     U $ rV   r4   rE   s     r:   rF   ProfilerContextVariable.enterB  s    r<   rT   c                 @    [         R                  R                  S 5      $ rV   r  rX   s      r:   rY   ProfilerContextVariable.exitE  r9  r<   c                     gr;  r4   rq   s    r:   r]   #ProfilerContextVariable.module_nameJ  r  r<   c                     gr?  r4   rq   s    r:   r^   ProfilerContextVariable.fn_nameM  rB  r<   c                 N    [        S[        U 5      S/ [        R                  QS9  g )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   r   r   SUPPORTABLEr_   s     r:   rl   #ProfilerContextVariable.reconstructP  s*    HIr"..		
r<   r4   r   )r   r   r   r   r   r   r6   r   rF   rY   r   r]   r^   rl   r   r   r   s   @r:   rD  rD  6  sn    7 7 7/ O 7)72A7	7
S  
 
r<   rD  c                      ^  \ rS rSrSrS1\R                  kr\S\S\	\
   SSS\S	S 4
S
 j5       r  SS\S\\   S\S	S4U 4S jjjrSSS	\
4S jrSSS\
S	\
4S jrS	\4S jrS	\4S jrSU 4S jjrSrU =r$ )%ProfilerRecordFunctionContextVariablei[  aK  
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]r0   r1   c                    S n[         R                  (       a  U(       a  US   R                  5       O3UR                  S[        R
                  " S5      5      R                  5       nS n[        U5      S:  a  US   R                  5       nOSU;   a  US   R                  5       nXV/nO[        [        SU 5        [        SUS S.UD6$ )	Nr   r   unknownr   rT   z+Profiler record function %s will be ignoredr   r4   )
r    capture_profiler_record_functionrH  getr   rC   rj   r   logrY  )r  r[  r\  r0   r-   r   record_args_consts          r:   rD   ,ProfilerRecordFunctionContextVariable.createk  s     22
  A113ZZI66yA$$&  !%;!#$/N$E$E$G!6!$*6N$E$E$G!!5MKTR4 
'
 
 	
r<   Nr-   r.   c                 *   > [         TU ]  " SXS.UD6  g r   r  r7   s       r:   r6   .ProfilerRecordFunctionContextVariable.__init__  r  r<   r=   r(   c                     [         R                  (       aT  U R                  u  p#UR                  R	                  S[
        R                  R                  R                  X#40 5      U l	        U $ r   )
r   r_  r-   rO   r   r   opsprofiler_record_function_enter_newr   )r8   r=   r   rT   s       r:   rF   +ProfilerRecordFunctionContextVariable.enter  sS    22++JD..		""==	DJ r<   rT   c                    [         R                  (       aU  UR                  R                  S[        R
                  R                  R                  R                  U R                  40 5        [        R                  R                  S 5      $ r   )r   r_  rO   r   r   rg  rh  _record_function_exit_RecordFunctionr   r   rC   rD   rX   s      r:   rY   *ProfilerRecordFunctionContextVariable.exit  s`     22II!!		""88HH	 ))0066r<   c                 4    [         R                  (       a  S$ S$ )Nztorch.autograd.profilerr<  r   r_  rq   s    r:   r]   1ProfilerRecordFunctionContextVariable.module_name  s!     66 &	
 	
r<   c                 4    [         R                  (       a  S$ S$ )Nrecord_functionr@  rp  rq   s    r:   r^   -ProfilerRecordFunctionContextVariable.fn_name  s!     66 	
 	
r<   c                    > [         R                  (       a&  [        S[        U 5      S/ [        R
                  QS9  g [        TU ]  U5        g )Nz,record_function escaped from compiled regionzADynamo doesn't support graph break inside record_function region.rP  )r   r_  r   r   r   rV  r5   ra   )r8   r`   r9   s     r:   ra   6ProfilerRecordFunctionContextVariable.reconstruct_type  sE     22FD	_&22	 G$W-r<   r  rX  r   )r   r   r   r   r   r*   r   r   r   r   r   rD   r	   r6   rF   rY   r   r]   r^   ra   r   r   r   s   @r:   rY  rY  [  s     		 	/	/N
 

o.
 4
 	

 
1
 
B "(,

 !
 	

 

 

/ 
O 
7)72A7	7
S 

 
. .r<   rY  c                      ^  \ rS rSrSr\SSS\SS 4S j5       r\SSS\4S j5       rS\S	\S
\	SS4U 4S jjr
SSS\4S jrSSS\S\4S jrSS jrSrU =r$ )PreserveVersionContextVariablei  z7
Wraps torch.autograd._unsafe_preserve_version_counter
r=   r(   tensorsr1   c           	         UR                  5       (       aO  [        R                  " U4 Vs/ s H  o"R                  U S5      PM     sn5      n[        R                  " U/5      nOc[	        U[        R                  5      (       d   e[        R                  " UR
                   Vs/ s H  o"R                  U S5      PM     sn5      nUn[        XC5      $ s  snf s  snf )N_version)	is_tensorr   TupleVariabler   rv   rG  rx  )r=   ry  r   versionstensors_tuples        r:   _create_lambda_from_tensors:PreserveVersionContextVariable._create_lambda_from_tensors  s    
  ..9@	B	1r:.	BH &33WI>Mgy'>'>???? ..8?F1r:.FH $M-mFF C Gs   C$Cc                 8   ^  [         R                  " U 4S j5      $ )Nc                 0   > [         R                  TU 5      $ rV   )rx  r  )ry  r=   s    r:   re   <PreserveVersionContextVariable.constructor.<locals>.<lambda>  s    :VVGr<   )r   LambdaVariable)r=   s   `r:   constructor*PreserveVersionContextVariable.constructor  s    ''
 	
r<   prev_versionsr0   Nc                 x  > UR                  SS 5        [        TU ]  " S0 UD6  Xl        X l        U R                  R                  5       (       a&  [        R                  " U R                  /5      U l        U R                  R                  5       (       a'  [        R                  " U R                  /5      U l        g g r2  )	
setdefaultr5   r6   ry  r  r|  r   r}  is_symnode_like)r8   ry  r  r0   r9   s       r:   r6   'PreserveVersionContextVariable.__init__  s     	/40"6"*<<!!##$22DLL>BDL--//!*!8!8$:L:L9M!ND 0r<   c                 @    [         R                  R                  S 5      $ rV   r  rE   s     r:   rF   $PreserveVersionContextVariable.enter  r  r<   rT   c                     SSK Jn  [        R                  " U5      R	                  XR
                  U R                  /0 5      $ )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   TorchInGraphFunctionVariablery   ry  r  )r8   r=   rT   r  s       r:   rY   #PreserveVersionContextVariable.exit   s:     	D55'

-\\4+=+=>
C	Dr<   c                 N    [        S[        U 5      S/ [        R                  QS9  g )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.rP  rU  r_   s     r:   rl   *PreserveVersionContextVariable.reconstruct	  s-    bIU"..
	
r<   )r  ry  r   )r   r   r   r   r   r   r   r  r  r   r6   rF   rY   rl   r   r   r   s   @r:   rx  rx    s     G#G G 
*G G" 
/ 
O 
 
O O 'O 	O
 
O 7/ 7O 7D)D2AD	D
 
r<   rx  c                   @  ^  \ rS rSr\" \" 5       \R                  5      r\	SSS\
S\
S\
SS 4
S j5       r SS\
S
\\
   S\\\
      S\
SS	4
U 4S jjjrSSS\4S jrSSS\S\4S jrSSS\\   S\\\4   S\4U 4S jjrSSS\\
   SS	4S jrS\4S jrS\4S jrSrU =r$ )&FSDPParamGroupUseTrainingStateVariablei  r=   r(   param_group_varrp  r0   r1   c                 P    [        SUU/UR                  R                  /S.UD6nU$ )N)r  r-   r.   r4   )r  r   _training_state)r=   r  rp  r0   r   s        r:   rD   -FSDPParamGroupUseTrainingStateVariable.create  s>     5 
+'.+11AAB
 	
 
r<   Nr-   r.   c                 `   > [         TU ]  " SX#S.UD6  Xl        [        U R                  5        g r   )r5   r6   r  r   r  )r8   r  r-   r.   r0   r9   s        r:   r6   /FSDPParamGroupUseTrainingStateVariable.__init__)  s8     	 	
'	
JP	
  /d,,-r<   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ rV   ry  rE   s     r:   rF   ,FSDPParamGroupUseTrainingStateVariable.enter6  r{  r<   rT   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ rV   r}  rX   s      r:   rY   +FSDPParamGroupUseTrainingStateVariable.exit:  r  r<   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ rV   r  r  s       r:   ry   4FSDPParamGroupUseTrainingStateVariable.call_function@  s)     	//0w$Rv66r<   rS  c                 P   [        U5      S:X  d   eUS   nU R                  R                  R                  U:w  al  U R                  R	                  US[
        R                  R                  S5      [
        R                  " U5      40 5        X0R                  R                  l        g g )Nr   r   __setattr__r  )	rj   r  r   r  call_methodr   rC   rD   EnumVariabler  s       r:   r@   1FSDPParamGroupUseTrainingStateVariable._call_funcJ  s    6{aq	%%55>  ,,..556GH**51  :?  &&6 ?r<   c                     g)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr4   rq   s    r:   r]   2FSDPParamGroupUseTrainingStateVariable.module_nameY  s    Ur<   c                     g)Nuse_training_stater4   rq   s    r:   r^   .FSDPParamGroupUseTrainingStateVariable.fn_name\      #r<   )r  rV   )r   r   r   r   r   r   r   FSDP_TRAINING_STATEr  r   r   rD   r   r	   r6   r   rF   rY   r   r   ry   r@   r]   r^   r   r   r   s   @r:   r  r    sP   /1<3S3ST#  	
 
2 $ 37	..  }. !#/	.
 . 
. .7/ 7O 77)72A7	77#7 '7 S/)*	7
 
7?4 ?hsm ?PT ?VS V$ $ $r<   r  c                   ,  ^  \ rS rSrSr\ SSSS\S\S\SS 4
S	 jj5       r  SS\	\
R                  R                  R                     S\S\S\SS
4
U 4S jjjr\SSS\	\   S\	\   4S j5       rSSS\4S jrSSS\S\4S jrS\4S jrS\4S jrSrU =r$ )SDPAKernelVariablei`  z)represents torch.nn.attention.sdpa_kernelr=   r(   backendsset_priorityr0   r1   c                     [        U[        R                  R                  R                  5      (       a  U/n[        SUS US.UD6nU$ )N)r-   r.   r  r4   )rv   r   nn	attention
SDPBackendr  )r=   r  r  r0   r   s        r:   rD   SDPAKernelVariable.createc  sQ     h 2 2 = =>> zH  
"%
 	
 
r<   Nr-   r.   c                 6   > [         TU ]  " SXS.UD6  X0l        g r   )r5   r6   r  )r8   r-   r.   r  r0   r9   s        r:   r6   SDPAKernelVariable.__init__t  s*     	 	
'	
JP	
 )r<   c           	          U Vs/ s HN  nU R                   R                  S[        R                  R                  R
                  UR                  40 5      PMP     nnU$ s  snf r   )rO   r   r   r  r  _backend_from_stringr   )r=   r  backendnodess       r:   _backends_to_nodes%SDPAKernelVariable._backends_to_nodes  se     $
 $ II!!""77	 $ 	 
 
s   AAc                 4  ^  [         R                  R                  R                  T R                  S9T l        T R                  UU 4S j5        [         R                  R                  R                  T R                  T R                  S9  T R                  UT R                  5      nUR                  R                  S[         R                  R                  R                  U[        T R                  5      40 5        [        R                  R                  S 5      $ )N)with_priorityc                  |   > [         R                  R                  R                  T R                  T R
                  S9$ )Nr  )r   r  r  _sdpa_kernelprev_backendsr  rq   s   r:   re   *SDPAKernelVariable.enter.<locals>.<lambda>  s0    EHH&&33""1B1B 4 r<   r  ry   )r   r  r  _cur_sdpa_kernel_backendsr  r  rB   r  r-   r  rO   r   r   r   rC   rD   )r8   r=   r  s   `  r:   rF   SDPAKernelVariable.enter  s    "XX//II++ J 
 		
 	''T->-> 	( 	
 %%b$*<*<=
		HH++$t(()*		
 ))0066r<   rT   c                 @   U R                  5         U R                  XR                  5      nUR                  R	                  S[
        R                  R                  R                  U[        U R                  5      40 5        [        R                  R                  S 5      $ r   )rW   r  r  rO   r   r   r  r  r  r   r  r   rC   rD   )r8   r=   rT   r  s       r:   rY   SDPAKernelVariable.exit  s}     	%%b*<*<=
		HH++$t(()*		
 ))0066r<   c                     g)Nztorch.nn.attentionr4   rq   s    r:   r]   SDPAKernelVariable.module_name  r  r<   c                     g)N_sdpa_kernel_variadicr4   rq   s    r:   r^   SDPAKernelVariable.fn_name  s    &r<   )r  r  r  r   )r   r   r   r   r   r   r   r   rD   r   r   r  r  r  r6   r  r   rF   rY   r   r]   r^   r   r   r   s   @r:   r  r  `  s&   3 ##  	
 
 & #"	
)EHH..99:
) 
) 	
)
 
) 

) 
) #s) 
c  7/ 7O 7,7)72A7	7$S $
' ' 'r<   r  c            	          ^  \ rS rSrSr SS\S\S\SS4U 4S jjjrS	S
S\S\4S jrS\	4S jr
S\	4S jrSS jrSrU =r$ )FxTracebackAnnotateVariablei  a  
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.   r0   r1   c                 *   > [         TU ]  " SXS.UD6  g r   r  r7   s       r:   r6   $FxTracebackAnnotateVariable.__init__  s"     	 	
'	
JP	
r<   r=   r(   rT   c                 t  ^ [        5       mTR                  [        R                  R                  R                  U R                  5      5        TR                  [        R                  R                  R                  5       5        U R                  UU4S j5        [        R                  R                  S 5      $ )Nc                  $   > T R                  5       $ rV   )close)stacks   r:   re   3FxTracebackAnnotateVariable.enter.<locals>.<lambda>  s    %++-r<   )r   enter_contextr   fx	tracebackannotater-   preserve_node_metarB   r   rC   rD   )r8   r=   rT   r  s      @r:   rF   !FxTracebackAnnotateVariable.enter  s~     EHH..778J8JKLEHH..AACDb"78))0066r<   c                     g)Nztorch.fx.tracebackr4   rq   s    r:   r]   'FxTracebackAnnotateVariable.module_name  r  r<   c                     g)Nr  r4   rq   s    r:   r^   #FxTracebackAnnotateVariable.fn_name  r  r<   c                 N    [        S[        U 5      S/ [        R                  QS9  g )Nz8torch.fx.traceback.annotate escaped from compiled regionzBDynamo doesn't support graph break on torch.fx.traceback.annotate.rP  rU  r_   s     r:   ra   ,FxTracebackAnnotateVariable.reconstruct_type  s*    NI\"..		
r<   r4   rV   r   )r   r   r   r   r   r   r6   r   rF   r   r]   r^   ra   r   r   r   s   @r:   r  r    sv     9=
 
25
HK
	
 

7)
72A
7	
7$S $ 
 
r<   r  c                   x   ^  \ rS rSrSrS\\\4   S\SS4U 4S jjrSS	S
\SS4S jr	S\4S jr
S\4S jrSrU =r$ )DynamoConfigPatchVariablei  z,represents torch._dynamo.patch_dynamo_configr-   r0   r1   Nc                   > [        UR                  5       5      n[        TU ]  " SU4S S.UD6  0 nU H0  u  pV[        R
                  R                  R                  U5      XE'   M2     [        UR                  5       5      4U l        g r   )	rT  rG  r5   r6   r   r   r   r   r.   )r8   r-   r0   target_values_tupleinitial_values_dictr  _r9   s          r:   r6   "DynamoConfigPatchVariable.__init__  s    #M$7$7$9: 	
.0	
IO	
 !)FC',}}';';'G'G'L$ *$%8%>%>%@ACr<   r=   r(   rS  c                     [        U5      S:X  d   eUS   nU H.  u  pE[        R                  R                  R	                  XE5        M0     g Nr   r   )rj   r   r   r   r  )r8   r=   rS  r   r  rk   s         r:   r@   $DynamoConfigPatchVariable._call_func  sA    6{aq	HCMM  ,,S6 r<   c                     gNztorch._dynamor4   rq   s    r:   r]   %DynamoConfigPatchVariable.module_name      r<   c                     g)Npatch_dynamo_configr4   rq   s    r:   r^   !DynamoConfigPatchVariable.fn_name  s    $r<   )r.   )r   r   r   r   r   r   r   r   r6   r@   r]   r^   r   r   r   s   @r:   r  r    sd    6Dd38n D D D74 7c 7d 7S % % %r<   r  c                   t   ^  \ rS rSrSrS\S\SS4U 4S jjrSS	S
\\   SS4S jr	S\
4S jrS\
4S jrSrU =r$ )ErrorOnGraphBreakVariablei  z-represents torch._dynamo.error_on_graph_breakerror_on_graph_breakr0   r1   Nc                 @   > [         TU ]  " SU4[        5       4S.UD6  g r   )r5   r6   r   )r8   r  r0   r9   s      r:   r6   "ErrorOnGraphBreakVariable.__init__  s.     	
/1579	
 	
r<   r=   r(   rS  c                 B    [        U5      S:X  d   e[        US   5        g r  )rj   r   r  s      r:   r@   $ErrorOnGraphBreakVariable._call_func  s     6{a!&),r<   c                     gr  r4   rq   s    r:   r]   %ErrorOnGraphBreakVariable.module_name  r  r<   c                     g)Nr  r4   rq   s    r:   r^   !ErrorOnGraphBreakVariable.fn_name  r  r<   r4   )r   r   r   r   r   r   r   r6   r   r@   r   r]   r^   r   r   r   s   @r:   r  r    s]    7
T 
S 
T 
-4 -htn -QU -S & & &r<   r  c                   v   ^  \ rS rSrS\\\4   S\SS4U 4S jjrSSS	\	\
   S\\\
4   S\
4S
 jrSS jrSrU =r$ )WithEnterFunctionVariablei!  r  r0   r1   Nc                 2   > [         TU ]  " S0 UD6  Xl        g r3   )r5   r6   r  )r8   r  r0   r9   s      r:   r6   "WithEnterFunctionVariable.__init__"  s    
 	"6"r<   r=   r(   rT   c                 r    U(       a   eU(       a   eUR                  U R                  UR                  5      $ rV   )	enter_ctxr  current_instructionrx   s       r:   ry   'WithEnterFunctionVariable.call_function*  s0     xz ||DHHb&<&<==r<   c                 X    U R                   R                  5        SU R                   R                  5        3n[        S[	        U R                   5      SU S3S/[        R                  Q[        R                  QS9  g ! [         a!    [	        [        U R                   5      5      n Nof = f)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.rP  )
r  r]   r^   rp   r   r   r   r   	DIFFICULT
DYNAMO_BUG)r8   r`   type_strs      r:   rl   %WithEnterFunctionVariable.reconstruct7  s    	+((..014883C3C3E2FGH 	QMCH:MghB #,,	
 #--		
 # 	+4>*H	+s   7A> >(B)(B)r'  r   )r   r   r   r   r   r*   r   r   r6   r   r   r   r   ry   rl   r   r   r   s   @r:   r  r  !  st    *,JJK  
	>#> '> S/)*	>
 
>
 
r<   r  c                      ^  \ rS rSrS1\R
                  krS\\\4   S\	S\	SS4U 4S jjr
SS	S
\\   S\\\4   S\4S jrSS jrSrU =r$ )WithExitFunctionVariableiJ  targetr  r0   r1   Nc                 x   > [         TU ]  " S0 UD6  [        U[        [        45      (       d   eXl        X l        g r3   )r5   r6   rv   r*   r   r  r  )r8   r  r  r0   r9   s       r:   r6   !WithExitFunctionVariable.__init__P  sD     	"6")+IJ
 
 	
 
 r<   r=   r(   rT   c                 N    U(       a   eU R                   R                  " U/UQ76 $ rV   )r  rY   rx   s       r:   ry   &WithExitFunctionVariable.call_function]  s$     zxx}}R'$''r<   c                    U R                   R                  U5        UR                  R                  R                  (       Ga  [
        R                  S:  aG  UR                  [        S5      5        [
        R                  S:  a  UR                  [        SSS95        UR                  U R                   R                   Vs/ s H  o!R                  U5      PM     sn5        UR                  [        [        U R                   R                  5      S5      5        UR                  [        U R                  5      5        UR                  [        S5      5        g g s  snf )	N)      	PUSH_NULL)r     SWAPr   )r  FPOP_TOP)r  ra   r=   rO   partial_convertsysversion_infoappend_outputr   rh   r-   ri   r   rj   r   r  )r8   r`   rk   s      r:   rl   $WithExitFunctionVariable.reconstructf  s    	!!'*::,,,7*%%&8&EF##g-))*<V*KL !!;?88;Q;QR;QC**3/;QR !!$S)?)?%@%H !!"3DKK"@A!!"4Y"?@ - Ss    E)r  r  r   )r   r   r   r   r   r   r   r*   r   r   r6   r   r   r   ry   rl   r   r   r   s   @r:   r  r  J  s    		'	'N
*,JJK  	
 
(#( '( S/)*	(
 
(A Ar<   r  )\r   r  loggingr  rI  collections.abcr   r   r   r<  r   r   typingr   r	   r
   r   torch._Cr   torch._dynamor   torch._guardsr   torch._loggingr    r   r   bytecode_transformationr   r   r   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(   	getLoggerr   ra  r*   r   r   r   r   r   r  r  r/  r=  rZ  ro  r  r  r  r  r  r  r0  rD  rY  rx  r  r  r  r  r  r  r  r4   r<   r:   <module>r5     s&  (   
  5 5 8 6 6     ' + 
   0 2 H !	 	 	 4 /D!do dN*%> *Z")+I ")J)70G )7X%77N %7P*7,C *7Z'7&= '7T%75 %7P(7-D (7V(W&= (WV57-D 57pB". B"JE 3 E P30 3lG&#: G&T/.&= /.d:-(? :-zD2 DN1 0"
5 "
Jp.,C p.fF
%< F
RF$-D F$RZ'0 Z'z+
"9 +
\% 7 %B& 7 &*&
 &
R/A /Ar<   