
    N jS                        S 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  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,J-r-J.r.J/r/J0r0J1r1J2r2  SSK3J4r4  \(       a  SSK5J6r6  SSK7J8r8  \Rr                  " \:5      r; " S S\)5      r< " S S\45      r= " S S\=5      r> " S S\<5      r? " S S\<5      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 " S0 S1\<5      rI " S2 S3\<5      rJ " S4 S5\<5      rK " S6 S7\<5      rL " S8 S9\<5      rM " S: S;\<5      rN " S< S=\<5      rO " S> S?\<5      rP " S@ SA\<5      rQ " SB SC\<5      rR " SD SE\<5      rS " SF SG\<5      rT " SH SI\<5      rU " SJ SK\)5      rV " SL SM\)5      rWg)Na)  
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)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$ )ContextWrappingVariable<   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,   r.   	__class__s       t/root/GenerationalWealth/GenerationalWealth/venv/lib/python3.13/site-packages/torch/_dynamo/variables/ctx_manager.pyr4    ContextWrappingVariable.__init__E   s!     	"6"*,    txr&   c                     [        U S5      (       a  U R                  XR                  5        U R                  U5        [        R
                  $ N
_call_func)hasattrr>   r+   set_cleanup_hookr   CONSTANT_VARIABLE_NONEr6   r;   s     r8   enterContextWrappingVariable.enterL   s;    4&&OOB 2 23b!///r:   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=   )r?   r>   r,   rB   s   r8   rE   4ContextWrappingVariable.set_cleanup_hook.<locals>.fnW   s)    4..OOB(;(;< /r:   r/   N
cleanup_fnoutputadd_cleanup_hookcleanupr6   r;   rE   s   `` r8   r@   (ContextWrappingVariable.set_cleanup_hookR   s3     := = 9;
		""4<<0r:   argsc                 B    U R                  5         [        R                  $ N)cleanup_assertr   rA   r6   r;   rQ   s      r8   exitContextWrappingVariable.exit^   s     	///r:   c                     U" [        UR                  R                  U R                  5       5      U R	                  5       5      5        g rS   )r   r;   import_sourcemodule_namefn_namer6   codegens     r8   reconstruct_type(ContextWrappingVariable.reconstruct_typed   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      $ rS   )r^   )r]   r6   s   r8   <lambda>5ContextWrappingVariable.reconstruct.<locals>.<lambda>j   s    d&;&;G&Dr:   r2   F)add_push_nullr+   extend_outputcreate_load_constr   len)r6   r]   r+   vals   ``  r8   reconstruct#ContextWrappingVariable.reconstructi   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r6   s    r8   rZ   #ContextWrappingVariable.module_nameq   s    !">??r:   c                     [        S5      e)Nzfn_name called on baserl   rn   s    r8   r[   ContextWrappingVariable.fn_namet   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)rg   
isinstancer   r   r   r   r    r!   r#   r"   AssertionErrorr6   r;   rQ   r.   s       r8   call_function%ContextWrappingVariable.call_functionw   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NTr2   rn   s    r8   supports_graph_breaks-ContextWrappingVariable.supports_graph_breaks       r:   c                     gry   r2   rn   s    r8   exit_on_graph_break+ContextWrappingVariable.exit_on_graph_break   r|   r:   c                 N    U R                   b  U R                  5         S U l         g g rS   )rK   rn   s    r8   rN   ContextWrappingVariable.cleanup   s"    ??&OO"DO 'r:   c                 T    U R                   (       d   S5       eU R                  5         g )Nzmultiple exits?)rK   rN   rn   s    r8   rT   &ContextWrappingVariable.cleanup_assert   s    1 11r:   )rK   r,   r+   rS   r]   r%   r/   NrI   )__name__
__module____qualname____firstlineno__r   _nonvar_fieldsr   r	   r4   rC   r   r@   rV   r^   ri   strrZ   r[   r   dictrv   boolrz   r~   rN   rT   __static_attributes____classcell__r7   s   @r8   r(   r(   <   sD   	
 
	'	'N CG- -2:3--RU-	- -0/ 0O 0 OS
1)
1/7c8J/K
1	
10)02A0	0

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*   r.   r/   Nc                 V   > Uc   e[         TU ]  " SUUR                  S.UD6  Xl        g )N)value
value_typer2   )r3   r4   r7   r*   )r6   r*   r.   r7   s      r8   r4   'GenericContextWrappingVariable.__init__   s>    !!! 	
''	
 	

 r:   c                 .    U R                   R                  $ rS   )r*   r   rn   s    r8   rZ   *GenericContextWrappingVariable.module_name   s    {{%%%r:   c                 @    [        U R                  5      R                  $ rS   )typer*   r   rn   s    r8   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__rv   )r6   r;   r   s      r8   rC   $GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r:   rQ   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   rv   listactive_generic_context_managerspop)r6   r;   rQ   r   xs        r8   rV   #GenericContextWrappingVariable.exit   sx     ,*T[[*2U((KK  ))
 -DJ
+	 	

 	**..0r:   c                     gNFr2   rn   s    r8   rz   4GenericContextWrappingVariable.supports_graph_breaks   s    r:   c                     gry   r2   rn   s    r8   r~   2GenericContextWrappingVariable.exit_on_graph_break   r|   r:   )r*   )r   r   r   r   r   r   r4   r   rZ   r[   r   rC   rV   r   rz   r~   r   r   r   s   @r8   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modr/   Nc                     Xl         X l        g rS   )cm_vtr   )r6   r   r   s      r8   r4   -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_varrL   side_effectsignore_mutations_onr   rC   rB   s     r8   rC   *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#rQ   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rS   )r   r   r   r   r   r   rV   rL   r   stop_ignoring_mutations_onr   r   )r6   r;   rQ   r   s       r8   rV   )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      $ rS   )getattrr   )r6   r   s     r8   __getattr__0RepararametrizeModuleContextVariable.__getattr__   s    tzz4((r:   )r   r   r   r   )r   r   r   r   r(   r   r4   r   rC   rV   r   r   r   r2   r:   r8   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+   r.   r/   c                     [        SUS S.UD6$ Nr+   r,   r2   )r   r;   r+   r.   s      r8   create0GradInplaceRequiresGradCtxManagerVariable.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                  $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ rS   )r   _C
_functorch!set_inplace_requires_grad_allowed
prev_statern   s   r8   rb   AGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>  s!    EHH''IIr:   rv   )r+   r   r   r   !get_inplace_requires_grad_allowedr   r   r@   rL   create_nodeproxyr   rA   )r6   r;   enableds   `  r8   rC   /GradInplaceRequiresGradCtxManagerVariable.enter  s    &&	((--OOQ==gF	
 YY**HHAAJ	

 ///r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  $ Nrv   )
rN   rL   r   r   r   r   r   r   r   rA   rU   s      r8   rV   .GradInplaceRequiresGradCtxManagerVariable.exit  sP     	
		HHAA__		
 ///r:   r   r   Nr   r   r   r   __doc__staticmethodr   r   r   rC   rV   r   r2   r:   r8   r   r      sh    -
#
47
CF
	4
 
0/ 0O 0$
0)
02A
0	
0r:   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+   r.   r/   c                     [        SUS S.UD6$ r   )r   r   s      r8   r   7TemporarilyPopInterpreterStackCtxManagerVariable.create$  s'     @ 
'
 
 	
r:   c                 4  ^  [         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                  $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ rS   )r   r   r   push_dynamic_layer_stacksavedrn   s   r8   rb   HTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>2  s    EHH''@@Lr:   rv   r2   )r   r   r   pop_dynamic_layer_stackr   r@   rL   r   r   r   rA   rB   s   ` r8   rC   6TemporarilyPopInterpreterStackCtxManagerVariable.enter.  ss    XX((@@B
L	
 YY**HH77	

 ///r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  $ r   )
rN   rL   r   r   r   r   r   r   r   rA   rU   s      r8   rV   5TemporarilyPopInterpreterStackCtxManagerVariable.exit<  sO     	
		HH88ZZM		
 ///r:   )r   r   Nr   r2   r:   r8   r   r   !  sh    T
#
47
CF
	;
 
0/ 0O 0
0)
02A
0	
0r:   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)%JvpIncrementNestingCtxManagerVariableiI  z5represents torch.func.jvp increment/decrement nestingr;   r&   r.   r/   c                 "    [        SS S S.UD6nU$ r   )r   r;   r.   vars      r8   r   ,JvpIncrementNestingCtxManagerVariable.createS  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       $ rS   )r   r   eager_transformsexit_jvp_nestingr2   r:   r8   rb   =JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>b  s    ((99JJLr:   rv   r2   )r   _guards_singletonr   r   r   enter_jvp_nestingr@   rL   r   r   _jvp_increment_nestingr   r   ConstantVariabler   )r6   r;   	jvp_levels      r8   rC   +JvpIncrementNestingCtxManagerVariable.enter^  s    d,,-$$55GGI	L	
 YY**HH66	

 ))00;;r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  $ Nrv   r2   )	rN   rL   r   r   r   r   _jvp_decrement_nestingr   rA   rU   s      r8   rV   *JvpIncrementNestingCtxManagerVariable.exitl  sF     	
		UXX00GGR	
 ///r:   r   Nr   r   r   r   r   r   r   r   FUNCTORCH_STACK_MATCHr   r   r   r   r   rC   rV   r   r2   r:   r8   r   r   I  sy    ? /1<3U3UV#/2	0 </ <O <0)02A0	0r:   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)SetFwdGradEnabledContextManageriv  zWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradr;   r&   r+   r.   r/   c                     [        SUS S.UD6$ r   )r  r   s      r8   r   &SetFwdGradEnabledContextManager.createy  s&     / 
'
 
 	
r:   c                 h  ^  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                  $ )Nc                  V   > [         R                  R                  T R                  5      $ rS   )r   r   _set_fwd_grad_enabledr   rn   s   r8   rb   7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>  s    EHH224??Cr:   rv   )r+   r   r   _is_fwd_grad_enabledr   r  r@   rL   r   r   r   rA   )r6   r;   modes   `  r8   rC   %SetFwdGradEnabledContextManager.enter  s    ##((779&&t,C	
 YY**HH**G	

 ///r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  $ r   )	rN   rL   r   r   r   r  r   r   rA   rU   s      r8   rV   $SetFwdGradEnabledContextManager.exit  sJ     	
		HH**__		
 ///r:   r   Nr   r2   r:   r8   r  r  v  sh    a
#
47
CF
	*
 
0/ 0O 0 
0)
02A
0	
0r:   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&   r.   r/   c                     [        SS S S.UD6$ r   )r  )r;   r.   s     r8   r   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_levelrn   s   r8   rb   /DualLevelContextManager.enter.<locals>.<lambda>  s!    11AAAWr:   rv   r2   )r   r   r   r!  r"  enter_dual_levelr$  r@   rL   r   r   _enter_dual_levelr   r   r  r   rB   s   ` r8   rC   DualLevelContextManager.enter  s    d,,-22CCEW	
 YY**HH&&	

 ))00@@r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  $ r   )	rN   rL   r   r   r   _exit_dual_levelr$  r   rA   rU   s      r8   rV   DualLevelContextManager.exit  sJ     	
		HH%%^^		
 ///r:   )r$  r   N)r   r   r   r   r   r   r   r   
DUAL_LEVELr   r   r   r   r   rC   rV   r   r2   r:   r8   r  r    sy    E/1<3J3JK
* 
c 
>W 
 
A/ AO A
0)
02A
0	
0r:   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&   r.   r/   c                 "    [        SS S S.UD6nU$ r   )r.  r   s      r8   r   -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       $ rS   )r   r   r   _grad_decrement_nestingr2   r:   r8   rb   >GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*Wr:   rv   r2   )r   r   r   r   r   _grad_increment_nestingr@   rL   r   r   r   r  r   )r6   r;   
grad_levels      r8   rC   ,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WXYY**HH77	

 ))00<<r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  $ r  )	rN   rL   r   r   r   r   r3  r   rA   rU   s      r8   rV   +GradIncrementNestingCtxManagerVariable.exit  sF     	
		UXX00HH"b	
 ///r:   r
  Nr  r2   r:   r8   r.  r.    sy    @ /1<3U3UV#/2	1 
=/ 
=O 
=0)02A0	0r:   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_argsr/   c                     [        US S S9$ )N)r=  r+   r,   )r<  )r;   r=  s     r8   r   &CatchWarningsCtxManagerVariable.create  s     / 3
 	
r:   Nr+   r,   r.   c                 n   > [        U[        5      (       d   U5       e[        TU ]  " SX#S.UD6  Xl        g r   )rs   r   r3   r4   r=  )r6   r=  r+   r,   r.   r7   s        r8   r4   (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      $ rS   )r   )ctx_vals   r8   rb   7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*Lr:   r2   )
r=  itemsas_python_constantwarningscatch_warningsr@   r   r  r   r   )r6   r;   kvr.   rD  s        @r8   rC   %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      $ )NrH  rI  )load_import_from)r]   s   r8   rb   =CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    G,,Z9IJr:   F)	rd   foreachr=  valuestuplekeysre   create_call_function_kwrg   )r6   r]   rT  s    ` r8   ri   +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   r   r	   r   r4   rC   ri   r   r   r   s   @r8   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+   r.   r/   c                 "    [        SUS S.UD6nU$ r   )rY  )r;   r+   r.   r   s       r8   r   -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       $ rS   )r   r   r   _vmap_decrement_nestingr2   r:   r8   rb   >VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>@  r5  r:   rv   )r   r   r+   rs   r   SymNodeVariablesym_numrG  r   r   r   _vmap_increment_nestingr@   rL   create_proxypredispatchas_proxyr   r  r   )r6   r;   
batch_size
randomnessbatch_size_value
vmap_levels         r8   rC   ,VmapIncrementNestingCtxManagerVariable.enter5  s    d,,-!%!3!3
j)";";<<)11)<<>224
XX((@@

 	b"WXYY++((@@  "J/	

 ))00<<r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  $ r  )	rN   rL   r   r   r   rd  r^  r   rA   rU   s      r8   rV   +VmapIncrementNestingCtxManagerVariable.exitI  sK     	
		((@@		
 ///r:   r
  N)r   r   r   r   r   r   r   r   r  r   r   r   r   r   r   rC   rV   r   r2   r:   r8   rY  rY    s    ; /1<3U3UV
#
0
 
 
2	
 
=/ =O =(
0)
02A
0	
0r:   rY  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$ )GradModeVariableiV  z6represents torch.{no_grad,enable_grad,set_grad_mode}()r;   r&   target_valueinitializedr.   r/   c                     [        SU/[        R                  " 5       /S.UD6nU(       a  UR                  XR                  5        U$ r   )rn  r   is_grad_enabledr>   r+   )r;   ro  rp  r.   r   s        r8   r   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   )r3   r4   r   r   )r6   r+   r,   rp  r.   r7   s        r8   r4   GradModeVariable.__init__k  s2     	 	
'	
JP	
 	d,,-r:   c                 X    U R                  XR                  5        [        R                  $ rS   r>   r+   r   rA   rB   s     r8   rC   GradModeVariable.enterw       ..////r:   rQ   c                 X    U R                  XR                  5        [        R                  $ rS   r>   r,   r   rA   rU   s      r8   rV   GradModeVariable.exit{  "     	//0///r:   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ rS   r>   r,   r3   rv   r6   r;   rQ   r.   r7   s       r8   rv   GradModeVariable.call_function  s)     	//0w$Rv66r:   rR  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   rv   )rg   r   rr  rL   r   r   _set_grad_enabledr6   r;   rR  r   s       r8   r>   GradModeVariable._call_func  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r:   c                     gNr   r2   rn   s    r8   rZ   GradModeVariable.module_name      r:   c                     g)Nset_grad_enabledr2   rn   s    r8   r[   GradModeVariable.fn_name  s    !r:   r2   Fry   )r   r   r   r   r   r   r   r   	GRAD_MODEr   r   r   r   r   r	   r   r4   r   rC   rV   r   r   rv   r>   rZ   r[   r   r   r   s   @r8   rn  rn  V  sP   @/1<3I3IJ "#  	
 
 $ 48 	
.
. !$0
. 	
.
 
. 

. 
.0/ 0O 00)02A0	07#7 '7 S/)*	7
 
7.4 .c .d .S " " "r:   rn  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&   ro  r.   r/   c                 J    [        U/4S[        R                  " 5       0UD6nU$ )Nr,   )r  r   is_inference_mode_enabledr;   ro  r.   r   s       r8   r   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  r3   r4   r5   s       r8   r4   InferenceModeVariable.__init__  s6     !"<<>N 	
'	
JP	
r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  $ r   )
rT   rL   r   r   r!  	grad_mode_exit_inference_moder   r   rA   rU   s      r8   rV   InferenceModeVariable.exit  sR     	
		NN$$99ZZM		
 ///r:   c                 P  ^^^ 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                   $ )NFr   Tc                     > T(       a   [         R                  R                  T5        g [         R                  R                  R                  T 5        g rS   )r   r   r  r!  r  r  )ctx disabled_inference_mode_forciblypriors   r8   cleanup_hook1InferenceModeVariable.enter.<locals>.cleanup_hook  s0    /**51((==cBr:   rv   rI   )r   r   r   "fake_tensor_disable_inference_moder+   rr  r   r  r!  r  _enter_inference_moder@   rL   r   r   r   rA   )r6   r;   r  r  r  r  s      @@@r8   rC   InferenceModeVariable.enter  s    +0(MM  CC""1%
 04,))+EHH&&u-..**@@$BTBTUC	C 	C 	b/YY**NN$$::"d  "	

 ///r:   c                     gr  r2   rn   s    r8   rZ   !InferenceModeVariable.module_name  r  r:   c                     g)Ninference_moder2   rn   s    r8   r[   InferenceModeVariable.fn_name  s    r:   r
  rS   )r   r   r   r   r   r   r   r	   r   r4   r   rV   rC   r   rZ   r[   r   r   r   s   @r8   r  r    s    #36BE	   *.

 !
 	

 

 

0)
02A
0	
00/ 0O 0<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&   devicer.   r/   c                 \    [        S[        R                  R                  USS9/S S.UD6nU$ )NT)optionalr   r2   )r  r   cuda_get_device_index)r;   r  r.   r   s       r8   r   CUDADeviceVariable.create  sA     ! 
 ::777NO
 

 
r:   Nr+   r,   c                 *   > [         TU ]  " SXS.UD6  g r   r3   r4   r5   s       r8   r4   CUDADeviceVariable.__init__  "     	 	
'	
JP	
r:   rQ   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S5      $ )Nrv   F)
rT   rL   r   r   r  _maybe_exchange_devicer   r   r  r   rU   s      r8   rV   CUDADeviceVariable.exit  sW     	
		JJ--ZZM		
 ))0077r:   c                 .  ^ [         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                  $ )Nc                  B   > [         R                  R                  T 5      $ rS   )r   r  r  )prev_idxs   r8   rb   *CUDADeviceVariable.enter.<locals>.<lambda>	  s    %***K*KH*Ur:   rv   )
r   r  _exchange_devicer+   r@   rL   r   r   r   rA   )r6   r;   r  s     @r8   rC   CUDADeviceVariable.enter  ss    ::..0B0BCb"UVYY**JJ''"d  "	

 ///r:   c                     g)Nz
torch.cudar2   rn   s    r8   rZ   CUDADeviceVariable.module_name      r:   c                     g)Nr  r2   rn   s    r8   r[   CUDADeviceVariable.fn_name  s    r:   r
  rS   )r   r   r   r   r   r   r   r   r	   r4   r   rV   rC   r   rZ   r[   r   r   r   s   @r8   r  r    s    &#-0<?	  )-

 !
 	

 

 

8)
82A
8	
8	0/ 	0O 	0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&   r.   r/   c                 $    [        U 4/ / S.UD6nU$ )Nr   )r  r   s      r8   r   #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   r2   )rg   r  symbolic_torch_function_statetorch_function_subclass_enabled'initial_torch_function_subclass_enabledtorch_function_mode_enabled#initial_torch_function_mode_enabledr3   r4   r   r   )r6   r;   r+   r,   r  r.   r7   s         r8   r4   %TorchFunctionDisableVariable.__init__*  s     =!Q&&&)c..AQ.FFF*,,LL 	4 ,,HH 	0 	 	
'	
JP	
 	d,,-r:   rE   .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 rS   )r  r  r  r  r  rB   s   r8   rE   9TorchFunctionDisableVariable.set_cleanup_hook.<locals>.fnH  sA    @@ 00P ))DD 44P *r:   rI   rJ   rO   s   `` r8   r@   -TorchFunctionDisableVariable.set_cleanup_hookA  s3    
 :  
		""4<<0r:   rR  c                     [        U5      S:X  d   eSUR                  l        U R                  (       d  SUR                  l        g g )Nr   F)rg   r  r  r  r  r6   r;   rR  s      r8   r>   'TorchFunctionDisableVariable._call_funcT  s>    6{aKP((H!!KPB,,H "r:   c                     g)Nztorch._Cr2   rn   s    r8   rZ   (TorchFunctionDisableVariable.module_nameZ      r:   c                 (    U R                   (       a  gg)NDisableTorchFunctionSubclassDisableTorchFunction)r  rn   s    r8   r[   $TorchFunctionDisableVariable.fn_name]  s    1%r:   )rK   r  r  r  ry   rS   )r   r   r   r   r   r   r   r   TORCH_FUNCTION_STATEr   r   r   r   r   r	   r   r4   r   r@   r>   r   rZ   r[   r   r   r   s   @r8   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\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$ )!DisabledSavedTensorsHooksVariableic  z;represents torch.autograd.graph.disable_saved_tensors_hook.r;   r&   ro  r.   r/   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>   r@   r  s       r8   r   (DisabledSavedTensorsHooksVariable.createf  s`     0 
'.""RRT

 
 	r>*R 
r:   Nr+   r,   c                 *   > [         TU ]  " SXS.UD6  g r   r  r5   s       r8   r4   *DisabledSavedTensorsHooksVariable.__init__u  r  r:   c                 "    [         R                  $ rS   r   rA   rB   s     r8   rC   'DisabledSavedTensorsHooksVariable.enter      ///r:   rR  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   rv   r2   )rg   rL   r   r   r   r  _saved_tensors_hooks_disable_saved_tensors_hooks_enabler  s       r8   r>   ,DisabledSavedTensorsHooksVariable._call_func  s     6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r:   c                     g)Nztorch.autograd.graphr2   rn   s    r8   rZ   -DisabledSavedTensorsHooksVariable.module_name      %r:   c                     g)Ndisable_saved_tensors_hooksr2   rn   s    r8   r[   )DisabledSavedTensorsHooksVariable.fn_name  s    ,r:   r2   rS   )r   r   r   r   r   r   r	   r   r   r   r   r4   r   rC   r>   rZ   r[   r   r   r   s   @r8   r  r  c  s    E#3;C=LO	, " =A
.
 !(3-!89
 	

 

 
0/ 0O 0=)=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  funcrQ   r.   r/   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	argumentsrs   r   appendrG  r  )r  rQ   r.   
bound_argsr+   keyargr   s           r8   r   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  r5   s       r8   r4   AutocastModeVariable.__init__  r  r:   r;   r&   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  $ r   )	rT   rL   r   r   r  _exit_autocastr   r   rA   rU   s      r8   rV   AutocastModeVariable.exit  sI     	
		UYY55

}b	
 ///r:   c                 .  ^ [         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                  $ )Nc                  B   > [         R                  R                  T 5      $ rS   )r   r  r  r  s   r8   rb   ,AutocastModeVariable.enter.<locals>.<lambda>  s    %))*B*B3*Gr:   rv   )
r   r  _enter_autocastr+   r@   rL   r   r   r   rA   )r6   r;   r  s     @r8   rC   AutocastModeVariable.enter  sr    ii''););<b"GHYY**UYY668N$:L:L8NPR

 ///r:   c                     g)Nztorch.amp.autocast_moder2   rn   s    r8   rZ    AutocastModeVariable.module_name  s    (r:   c                     g)Nr  r2   rn   s    r8   r[   AutocastModeVariable.fn_name  r  r:   r
  rS   )r   r   r   r   r   r   r  r  r  r   r   r   r   r   r	   r4   r   rV   rC   rZ   r[   r   r   r   s   @r8   r  r    s    "ii%%.."sm" S#X" 
 	" "N )-
}
 !
 	

 

 
0)02A0	00/ 0O 0)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+   r.   r/   c                 *   > [         TU ]  " SSU0UD6  g Nr+   r2   r  )r6   r+   r.   r7   s      r8   r4   NullContextVariable.__init__  s    ?}??r:   r;   r&   c                 `    [         R                  nU R                  (       a  U R                  $ U$ rS   )r   rA   r+   )r6   r;   nones      r8   rC   NullContextVariable.enter  s&    //%)%7%7t!!ATAr:   rQ   c                 "    [         R                  $ rS   r  rU   s      r8   rV   NullContextVariable.exit       ///r:   c                     gN
contextlibr2   rn   s    r8   rZ   NullContextVariable.module_name  r  r:   c                     gNnullcontextr2   rn   s    r8   r[   NullContextVariable.fn_name      r:   r2   rS   )r   r   r   r   r   r	   r   r4   r   rC   rV   r   rZ   r[   r   r   r   s   @r8   r  r    s    @hsm @c @d @ @B/ BO B0)02A0	0
S   r:   r  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$ )ProfilerContextVariablei   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.
r.   r/   Nc                 *   > [         TU ]  " SSS 0UD6  g r  r  )r6   r.   r7   s     r8   r4    ProfilerContextVariable.__init__	  s    6t6v6r:   r;   r&   c                     U $ rS   r2   rB   s     r8   rC   ProfilerContextVariable.enter  s    r:   rQ   c                 "    [         R                  $ rS   r  rU   s      r8   rV   ProfilerContextVariable.exit  r$  r:   c                     gr&  r2   rn   s    r8   rZ   #ProfilerContextVariable.module_name  r  r:   c                     gr*  r2   rn   s    r8   r[   ProfilerContextVariable.fn_name  r-  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     r8   ri   #ProfilerContextVariable.reconstruct  s*    HIr"..		
r:   r2   r   )r   r   r   r   r   r   r4   r   rC   rV   r   rZ   r[   ri   r   r   r   s   @r8   r/  r/     sn    7 7 7/ O 0)02A0	0
S  
 
r:   r/  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]r.   r/   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   rQ   z+Profiler record function %s will be ignoredr   r2   )
r    capture_profiler_record_functionrG  getr   r  rg   r   logrD  )r  rF  rG  r.   r+   r   record_args_consts          r8   r   ,ProfilerRecordFunctionContextVariable.create5  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  r5   s       r8   r4   .ProfilerRecordFunctionContextVariable.__init__U  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   rJ  r+   rL   r   r   opsprofiler_record_function_enter_newr   )r6   r;   r   rQ   s       r8   rC   +ProfilerRecordFunctionContextVariable.enter_  sS    22++JD..		""==	DJ r:   rQ   c                     [         R                  (       aU  UR                  R                  S[        R
                  R                  R                  R                  U R                  40 5        [        R                  $ r   )r   rJ  rL   r   r   rR  rS  _record_function_exit_RecordFunctionr   r   rA   rU   s      r8   rV   *ProfilerRecordFunctionContextVariable.exitk  sU     22II!!		""88HH	 ///r:   c                 4    [         R                  (       a  S$ S$ )Nztorch.autograd.profilerr'  r   rJ  rn   s    r8   rZ   1ProfilerRecordFunctionContextVariable.module_namex  s!     66 &	
 	
r:   c                 4    [         R                  (       a  S$ S$ )Nrecord_functionr+  r[  rn   s    r8   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.r;  )r   rJ  r   r   r   rA  r3   r^   )r6   r]   r7   s     r8   r^   6ProfilerRecordFunctionContextVariable.reconstruct_type  sE     22FD	_&22	 G$W-r:   r
  rW  r   )r   r   r   r   r   r(   r   r   r   r   r   r   r	   r4   rC   rV   r   rZ   r[   r^   r   r   r   s   @r8   rD  rD  %  s     		 	/	/N
 

o.
 4
 	

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

 

 

/ 
O 
0)02A0	0
S 

 
. .r:   rD  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&   tensorsr/   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   rs   rF  rc  )r;   rd  r   versionstensors_tuples        r8   _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      $ rS   )rc  rk  )rd  r;   s    r8   rb   <PreserveVersionContextVariable.constructor.<locals>.<lambda>  s    :VVGr:   )r   LambdaVariable)r;   s   `r8   constructor*PreserveVersionContextVariable.constructor  s    ''
 	
r:   prev_versionsr.   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 r  )	
setdefaultr3   r4   rd  rs  rg  r   rh  is_symnode_like)r6   rd  rs  r.   r7   s       r8   r4   'PreserveVersionContextVariable.__init__  s     	/40"6"*<<!!##$22DLL>BDL--//!*!8!8$:L:L9M!ND 0r:   c                 "    [         R                  $ rS   r  rB   s     r8   rC   $PreserveVersionContextVariable.enter  r  r:   rQ   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   TorchInGraphFunctionVariablerv   rd  rs  )r6   r;   rQ   r{  s       r8   rV   #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.r;  r@  r\   s     r8   ri   *PreserveVersionContextVariable.reconstruct  s-    bIU"..
	
r:   )rs  rd  r   )r   r   r   r   r   r   r   rk  rq  r   r4   rC   rV   ri   r   r   r   s   @r8   rc  rc    s     G#G G 
*G G" 
/ 
O 
 
O O 'O 	O
 
O 0/ 0O 0D)D2AD	D
 
r:   rc  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_varro  r.   r/   c                 P    [        SUU/UR                  R                  /S.UD6nU$ )N)r  r+   r,   r2   )r  r   _training_state)r;   r  ro  r.   r   s        r8   r   -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   )r3   r4   r  r   r   )r6   r  r+   r,   r.   r7   s        r8   r4   /FSDPParamGroupUseTrainingStateVariable.__init__  s8     	 	
'	
JP	
  /d,,-r:   c                 X    U R                  XR                  5        [        R                  $ rS   rw  rB   s     r8   rC   ,FSDPParamGroupUseTrainingStateVariable.enter   ry  r:   rQ   c                 X    U R                  XR                  5        [        R                  $ rS   r{  rU   s      r8   rV   +FSDPParamGroupUseTrainingStateVariable.exit  r}  r:   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ rS   r  r  s       r8   rv   4FSDPParamGroupUseTrainingStateVariable.call_function
  s)     	//0w$Rv66r:   rR  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  )	rg   r  r   r  call_methodr   r  r   EnumVariabler  s       r8   r>   1FSDPParamGroupUseTrainingStateVariable._call_func  s    6{aq	%%55>  ,,..556GH**51  :?  &&6 ?r:   c                     g)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr2   rn   s    r8   rZ   2FSDPParamGroupUseTrainingStateVariable.module_name#  s    Ur:   c                     g)Nuse_training_stater2   rn   s    r8   r[   .FSDPParamGroupUseTrainingStateVariable.fn_name&      #r:   )r  rS   )r   r   r   r   r   r   r   FSDP_TRAINING_STATEr   r   r   r   r   r	   r4   r   rC   rV   r   r   rv   r>   rZ   r[   r   r   r   s   @r8   r  r    sP   /1<3S3ST#  	
 
2 $ 37	..  }. !#/	.
 . 
. .0/ 0O 00)02A0	0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_priorityr.   r/   c                     [        U[        R                  R                  R                  5      (       a  U/n[        SUS US.UD6nU$ )N)r+   r,   r  r2   )rs   r   nn	attention
SDPBackendr  )r;   r  r  r.   r   s        r8   r   SDPAKernelVariable.create-  sQ     h 2 2 = =>> zH  
"%
 	
 
r:   Nr+   r,   c                 6   > [         TU ]  " SXS.UD6  X0l        g r   )r3   r4   r  )r6   r+   r,   r  r.   r7   s        r8   r4   SDPAKernelVariable.__init__>  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   )rL   r   r   r  r  _backend_from_stringr   )r;   r  backendnodess       r8   _backends_to_nodes%SDPAKernelVariable._backends_to_nodesJ  se     $
 $ II!!""77	 $ 	 
 
s   AAc                   ^  [         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                  $ )N)with_priorityc                  |   > [         R                  R                  R                  T R                  T R
                  S9$ )Nr  )r   r  r  _sdpa_kernelprev_backendsr  rn   s   r8   rb   *SDPAKernelVariable.enter.<locals>.<lambda>a  s0    EHH&&33""1B1B 4 r:   r  rv   )r   r  r  _cur_sdpa_kernel_backendsr  r  r@   r  r+   r  rL   r   r   r   rA   )r6   r;   r	  s   `  r8   rC   SDPAKernelVariable.enter[  s    "XX//II++ J 
 		
 	''T->-> 	( 	
 %%b$*<*<=
		HH++$t(()*		
 ///r:   rQ   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   )rT   r  r  rL   r   r   r  r  r  r   r  r   rA   )r6   r;   rQ   r	  s       r8   rV   SDPAKernelVariable.exitq  sr     	%%b*<*<=
		HH++$t(()*		
 ///r:   c                     g)Nztorch.nn.attentionr2   rn   s    r8   rZ   SDPAKernelVariable.module_name~  r  r:   c                     g)N_sdpa_kernel_variadicr2   rn   s    r8   r[   SDPAKernelVariable.fn_name  s    &r:   )r  r  r  r   )r   r   r   r   r   r   r   r   r   r   r   r  r  r  r4   r  r   rC   rV   r   rZ   r[   r   r   r   s   @r8   r  r  *  s&   3 ##  	
 
 & #"	
)EHH..99:
) 
) 	
)
 
) 

) 
) #s) 
c  0/ 0O 0,0)02A0	0$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,   r.   r/   c                 *   > [         TU ]  " SXS.UD6  g r   r  r5   s       r8   r4   $FxTracebackAnnotateVariable.__init__  s"     	 	
'	
JP	
r:   r;   r&   rQ   c                 V  ^ [        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                  $ )Nc                  $   > T R                  5       $ rS   )close)stacks   r8   rb   3FxTracebackAnnotateVariable.enter.<locals>.<lambda>  s    %++-r:   )r   enter_contextr   fx	tracebackannotater+   preserve_node_metar@   r   rA   )r6   r;   rQ   r  s      @r8   rC   !FxTracebackAnnotateVariable.enter  ss     EHH..778J8JKLEHH..AACDb"78///r:   c                     g)Nztorch.fx.tracebackr2   rn   s    r8   rZ   'FxTracebackAnnotateVariable.module_name  r  r:   c                     g)Nr  r2   rn   s    r8   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.r;  r@  r\   s     r8   r^   ,FxTracebackAnnotateVariable.reconstruct_type  s*    NI\"..		
r:   r2   rS   r   )r   r   r   r   r   r   r4   r   rC   r   rZ   r[   r^   r   r   r   s   @r8   r  r    sv     9=
 
25
HK
	
 

0)
02A
0	
0$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+   r.   r/   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   )	rS  rF  r3   r4   r   r   r   r   r,   )r6   r+   r.   target_values_tupleinitial_values_dictr  _r7   s          r8   r4   "DynamoConfigPatchVariable.__init__  s    #M$7$7$9: 	
.0	
IO	
 !)FC',}}';';'G'G'L$ *$%8%>%>%@ACr:   r;   r&   rR  c                     [        U5      S:X  d   eUS   nU H.  u  pE[        R                  R                  R	                  XE5        M0     g Nr   r   )rg   r   r   r   r  )r6   r;   rR  r   r  rh   s         r8   r>   $DynamoConfigPatchVariable._call_func  sA    6{aq	HCMM  ,,S6 r:   c                     gNztorch._dynamor2   rn   s    r8   rZ   %DynamoConfigPatchVariable.module_name      r:   c                     g)Npatch_dynamo_configr2   rn   s    r8   r[   !DynamoConfigPatchVariable.fn_name  s    $r:   )r,   )r   r   r   r   r   r   r   r   r4   r>   rZ   r[   r   r   r   s   @r8   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_breakr.   r/   Nc                 @   > [         TU ]  " SU4[        5       4S.UD6  g r   )r3   r4   r   )r6   r  r.   r7   s      r8   r4   "ErrorOnGraphBreakVariable.__init__  s.     	
/1579	
 	
r:   r;   r&   rR  c                 B    [        U5      S:X  d   e[        US   5        g r  )rg   r   r  s      r8   r>   $ErrorOnGraphBreakVariable._call_func  s     6{a!&),r:   c                     gr  r2   rn   s    r8   rZ   %ErrorOnGraphBreakVariable.module_name  r  r:   c                     g)Nr  r2   rn   s    r8   r[   !ErrorOnGraphBreakVariable.fn_name  r  r:   r2   )r   r   r   r   r   r   r   r4   r   r>   r   rZ   r[   r   r   r   s   @r8   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  r.   r/   Nc                 2   > [         TU ]  " S0 UD6  Xl        g r1   )r3   r4   r  )r6   r  r.   r7   s      r8   r4   "WithEnterFunctionVariable.__init__  s    
 	"6"r:   r;   r&   rQ   c                 r    U(       a   eU(       a   eUR                  U R                  UR                  5      $ rS   )	enter_ctxr  current_instructionru   s       r8   rv   '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.r;  )
r  rZ   r[   rm   r   r   r   r   	DIFFICULT
DYNAMO_BUG)r6   r]   type_strs      r8   ri   %WithEnterFunctionVariable.reconstruct  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   r4   r   r   r   r   rv   ri   r   r   r   s   @r8   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$ )WithExitFunctionVariablei  targetr  r.   r/   Nc                 x   > [         TU ]  " S0 UD6  [        U[        [        45      (       d   eXl        X l        g r1   )r3   r4   rs   r(   r   r  r  )r6   r  r  r.   r7   s       r8   r4   !WithExitFunctionVariable.__init__  sD     	"6")+IJ
 
 	
 
 r:   r;   r&   rQ   c                 N    U(       a   eU R                   R                  " U/UQ76 $ rS   )r  rV   ru   s       r8   rv   &WithExitFunctionVariable.call_function(  s$     zxx}}R'$''r:   c                 :    U R                   R                  U5        g rS   )r  r^   r\   s     r8   ri   $WithExitFunctionVariable.reconstruct1  s     	!!'*r:   )r  r  r   )r   r   r   r   r   r   r   r(   r   r   r4   r   r   r   rv   ri   r   r   r   s   @r8   r  r    s    		'	'N
*,JJK  	
 
(#( '( S/)*	(
 
(+ +r:   r  )Xr   r   loggingrH  collections.abcr   r   r   r'  r   r   typingr   r	   r
   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&   	getLoggerr   rL  r(   r   r   r   r   r   r  r  r.  r<  rY  rn  r  r  r  r  r  r  r/  rD  rc  r  r  r  r  r  r  r  r2   r:   r8   <module>r     s
  (    5 5 8 6 6     ' + :  0 2 H !	 	 	 4 /D!do dN*%> *Z")+I ")J)00G )0X%07N %0P*0,C *0Z'0&= '0T%05 %0P(0-D (0V(W&= (WV50-D 50pB". B"JE 3 E P30 3lG&#: G&T:-(? :-zE2 EP1 0"
5 "
Jp.,C p.fF
%< F
RF$-D F$RZ'0 Z'z+
"9 +
\% 7 %D& 7 &*&
 &
R +  +r:   