
    sh,                     l   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J	r	  SSK
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  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&  SSK'J(r(  \(       a  SSK)J*r*  \RV                   " S S5      5       r, " S S\ 5      r- " S S\(5      r. " S S\-5      r/ " S S\-5      r0 " S S\-5      r1 " S S\-5      r2 " S S \-5      r3 " S! S"\-5      r4 " S# S$\-5      r5 " S% S&\-5      r6 " S' S(\-5      r7 " S) S*\-5      r8 " S+ S,\-5      r9 " S- S.\-5      r: " S/ S0\-5      r; " S1 S2\-5      r< " S3 S4\-5      r= " S5 S6\-5      r> " S7 S8\-5      r? " S9 S:\-5      r@ " S; S<\-5      rA " S= S>\-5      rB " S? S@\-5      rC " SA SB\ 5      rD " SC SD\ 5      rE " SE SF\ 5      rFg)Ga)  
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OptionalTYPE_CHECKINGUnion)Guard   )graph_break_hints	variables)create_call_functioncreate_instructioncreate_setup_with)get_interface_for_device)unimplemented_v2)GuardBuilderinstall_guard)
AttrSourceGlobalStateSource   )VariableTracker)NestedUserFunctionVariableUserFunctionVariableUserMethodVariableWrappedUserFunctionVariableWrappedUserMethodVariable)UserDefinedObjectVariable)InstructionTranslatorc                   v    \ rS rSr% SrSr\\   \S'   Sr	\\
R                  R                     \S'   S rS rSrg)	ContextManagerState9   z
Mutating `self` in VariableTracker is not allowed because we copy
them.  This is a mutable container pointed to by context managers
that won't get copied, so it is safe to mutate.
N
cleanup_fnproxyc                 N    U R                   b  U R                  5         S U l         g g Nr    selfs    w/Users/tiagomarins/Projetos/claudeai/copy_bank/venv/lib/python3.13/site-packages/torch/_dynamo/variables/ctx_manager.pycleanupContextManagerState.cleanupD   s"    ??&OO"DO '    c                 T    U R                   (       d   S5       eU R                  5         g )Nzmultiple exits?)r    r(   r%   s    r'   cleanup_assert"ContextManagerState.cleanup_assertI   s    1 11r*   r$   )__name__
__module____qualname____firstlineno____doc__r    r   r   __annotations__r!   torchfxProxyr(   r,   __static_attributes__ r*   r'   r   r   9   s9     &*J")&*E8EHHNN#*#
r*   r   c                      ^  \ rS rSrSSSS1\R
                  kr SSS. SU 4S jjjjrS	 rSSS
 jjrSS jr	S r
S rS rS r        SS jrS rS rSrU =r$ )ContextWrappingVariableN   cm_objtarget_valuesinitial_valuesstateN)r?   c                r   > [         TU ]  " S0 UD6  Xl        X l        Uc  [	        5       U l        g UU l        g Nr8   )super__init__r=   r>   r   r?   )r&   r=   r>   r?   kwargs	__class__s        r'   rC    ContextWrappingVariable.__init__W   s6     	"6"*,.3m(*

r*   c                     U R                  XR                  5        U R                  U5        [        R                  R                  S 5      $ r#   )
_call_funcr=   set_cleanup_hookr
   ConstantVariablecreater&   txs     r'   enterContextWrappingVariable.enter_   s9    ../b!))0066r*   c                    ^ ^ Uc  U U4S jnUT R                   l        TR                  R                  T R                   R                  5        g )Nc                  >   > T R                  TT R                  5        g r#   )rH   r>   rL   s   r'   fn4ContextWrappingVariable.set_cleanup_hook.<locals>.fng   s    D$7$78r*   )r?   r    outputadd_cleanup_hookr(   )r&   rM   rR   s   `` r'   rI   (ContextWrappingVariable.set_cleanup_hookd   s7    :9 !#


		""4::#5#56r*   c                 t    U R                   R                  5         [        R                  R	                  S 5      $ r#   )r?   r,   r
   rJ   rK   r&   rM   argss      r'   exitContextWrappingVariable.exitm   s)    

!!#))0066r*   c                     U" [        UR                  R                  U R                  5       5      U R	                  5       5      5        g r#   )r   rM   import_sourcemodule_namefn_namer&   codegens     r'   reconstruct_type(ContextWrappingVariable.reconstruct_typeq   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      $ r#   )rb   )ra   r&   s   r'   <lambda>5ContextWrappingVariable.reconstruct.<locals>.<lambda>w   s    d&;&;G&Dr*   r8   F)add_push_nullr=   extend_outputcreate_load_constr   len)r&   ra   r=   vals   ``  r'   reconstruct#ContextWrappingVariable.reconstructv   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NotImplementedErrorr%   s    r'   r^   #ContextWrappingVariable.module_name~   s    !">??r*   c                     [        S5      e)Nzfn_name called on baserp   r%   s    r'   r_   ContextWrappingVariable.fn_name   s    !":;;r*   c                 p   [        U5      S:X  d   e[        US   [        5      (       a  [        US   R	                  5       5      US'   [        US   [
        [        45      (       d   e[        US   [
        5      (       a  [        US   U 5      $ [        US   [        5      (       a  [        US   U 5      $ g Nr   r   )rk   
isinstancer   r   get_functionr   r   r   r&   rM   rY   rD   s       r'   call_function%ContextWrappingVariable.call_function   s     4yA~~d1g9::*47+?+?+ABDG$q'$68L#MNNNNd1g122,T!Wd;;d1g344.tAw== 5r*   c                     gNTr8   r%   s    r'   supports_graph_breaks-ContextWrappingVariable.supports_graph_breaks       r*   c                     gr}   r8   r%   s    r'   exit_on_graph_break+ContextWrappingVariable.exit_on_graph_break   r   r*   )r>   r?   r=   r#   returnNrM   r   rM   r   rY   list[VariableTracker]rD   dict[str, VariableTracker]r   r   )r.   r/   r0   r1   r   _nonvar_fieldsrC   rN   rI   rZ   rb   rm   r^   r_   rz   r~   r   r7   __classcell__rE   s   @r'   r:   r:   N   s    	
 
	'	'N -1G;?G	G G7
77

O@<>#> &> -	>
 
>" r*   r:   c                   T   ^  \ rS rSrS
U 4S jjrS rS rS rSS jrS r	S r
S	rU =r$ )GenericContextWrappingVariable   c                 V   > Uc   e[         TU ]  " SUUR                  S.UD6  Xl        g )N)value
value_typer8   )rB   rC   rE   r<   )r&   r<   rD   rE   s      r'   rC   'GenericContextWrappingVariable.__init__   s>    !!! 	
''	
 	

 r*   c                 .    U R                   R                  $ r#   )r<   r/   r%   s    r'   r^   *GenericContextWrappingVariable.module_name   s    {{%%%r*   c                 @    [        U R                  5      R                  $ r#   )typer<   r.   r%   s    r'   r_   &GenericContextWrappingVariable.fn_name   s    DKK )))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__rz   )r&   rM   r   s      r'   rN   $GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r*   c                    U R                   c  S O[        U R                   S5      n[        R                  " U R                  R
                  R                  U US9R                  X0 5      nUR                  R                  5         U$ )N__exit__r   )
r   r   r
   r   r<   r   r   rz   active_generic_context_managerspop)r&   rM   rY   r   xs        r'   rZ   #GenericContextWrappingVariable.exit   sp    ,*T[[*2U((KK  ))
 -"
%	 	

 	**..0r*   c                     g)NFr8   r%   s    r'   r~   4GenericContextWrappingVariable.supports_graph_breaks   s    r*   c                     gr}   r8   r%   s    r'   r   2GenericContextWrappingVariable.exit_on_graph_break   r   r*   )r<   r   r   )r.   r/   r0   r1   rC   r^   r_   rN   rZ   r~   r   r7   r   r   s   @r'   r   r      s+    &*$ r*   r   c                   <    \ rS rSrSr\SS j5       rS rSS jrSr	g)	)GradInplaceRequiresGradCtxManagerVariable   z#represents torch grad requries gradc                     [        SUS S.UD6$ Nr=   r>   r8   )r   rM   r=   rD   s      r'   rK   0GradInplaceRequiresGradCtxManagerVariable.create   s$    8 
'
 
 	
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 R                  l        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ r#   )r4   _C
_functorch!set_inplace_requires_grad_allowed
prev_stater%   s   r'   rf   AGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>   s!    EHH''IIr*   rz   )r=   r4   r   r   !get_inplace_requires_grad_allowedr   r   rI   rT   create_noder?   r!   r
   rJ   rK   )r&   rM   enableds   `  r'   rN   /GradInplaceRequiresGradCtxManagerVariable.enter   s    &&	((--OOQ==gF	
 9900HHAAJ	


 ))0066r*   c                 
   U R                   R                  5         UR                  R                  S[        R
                  R                  R                  U R                  40 5        [        R                  R                  S 5      $ Nrz   )r?   r(   rT   r   r4   r   r   r   r   r
   rJ   rK   rX   s      r'   rZ   .GradInplaceRequiresGradCtxManagerVariable.exit   s`    


		HHAA__		
 ))0066r*   r   Nr   
r.   r/   r0   r1   r2   staticmethodrK   rN   rZ   r7   r8   r*   r'   r   r      s!    -
 
7$7r*   r   c                   <    \ rS rSrSr\SS j5       rS rSS jrSr	g)	0TemporarilyPopInterpreterStackCtxManagerVariable   zJrepresents torch._functorch.pyfunction.temporarily_pop_interpreter_stack()c                     [        SUS S.UD6$ r   )r   r   s      r'   rK   7TemporarilyPopInterpreterStackCtxManagerVariable.create   s$    ? 
'
 
 	
r*   c                 f  ^  [         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 R                  l	        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ r#   )r4   r   r   push_dynamic_layer_stacksavedr%   s   r'   rf   HTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>   s    EHH''@@Lr*   rz   r8   )r4   r   r   pop_dynamic_layer_stackr   rI   rT   r   r?   r!   r
   rJ   rK   rL   s   ` r'   rN   6TemporarilyPopInterpreterStackCtxManagerVariable.enter   s    XX((@@B
L	
 9900HH77	


 ))0066r*   c                    U R                   R                  5         UR                  R                  S[        R
                  R                  R                  U R                   R                  40 5        [        R                  R                  S 5      $ r   )r?   r(   rT   r   r4   r   r   r   r!   r
   rJ   rK   rX   s      r'   rZ   5TemporarilyPopInterpreterStackCtxManagerVariable.exit  sf    


		HH88ZZ		
 ))0066r*   )r   Nr   r   r8   r*   r'   r   r      s!    T
 
77r*   r   c                   l    \ rS rSrSr\" \" 5       \R                  5      r	\
SS j5       rS rSS jrSrg)	%JvpIncrementNestingCtxManagerVariablei  z5represents torch.func.jvp increment/decrement nestingc                 "    [        SS S S.UD6nU$ r   )r   rM   rD   vars      r'   rK   ,JvpIncrementNestingCtxManagerVariable.create  s(    3 

 

 
r*   c                 ~   [        U R                  5        [        R                  R                  R                  5       nU R                  US 5        UR                  R                  S[        R                  R                  R                  S0 5      U R                  l        [        R                  R                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ r#   )r4   r   eager_transformsexit_jvp_nestingr8   r*   r'   rf   =JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>*  s    ((99JJLr*   rz   r8   )r   _guards_singletonr4   r   r   enter_jvp_nestingrI   rT   r   r   _jvp_increment_nestingr?   r!   r
   rJ   rK   )r&   rM   	jvp_levels      r'   rN   +JvpIncrementNestingCtxManagerVariable.enter&  s    d,,-$$55GGI	L	
 9900HH66	


 ))00;;r*   c                     U R                   R                  5         UR                  R                  S[        R
                  R                  R                  S0 5        [        R                  R                  S 5      $ Nrz   r8   )r?   r(   rT   r   r4   r   r   _jvp_decrement_nestingr
   rJ   rK   rX   s      r'   rZ   *JvpIncrementNestingCtxManagerVariable.exit4  sV    


		UXX00GGR	
 ))0066r*   r8   Nr   r.   r/   r0   r1   r2   r   r   r   FUNCTORCH_STACK_MATCHr   r   rK   rN   rZ   r7   r8   r*   r'   r   r     s:    ? /1<3U3UV <7r*   r   c                   <    \ rS rSrSr\SS j5       rS rSS jrSr	g)	SetFwdGradEnabledContextManageri<  zWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradc                     [        SUS S.UD6$ r   )r   r   s      r'   rK   &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 R                  l
        [        R                  R                  S 5      $ )Nc                  V   > [         R                  R                  T R                  5      $ r#   )r4   r   _set_fwd_grad_enabledr   r%   s   r'   rf   7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>M  s    EHH224??Cr*   rz   )r=   r4   r   _is_fwd_grad_enabledr   r   rI   rT   r   r?   r!   r
   rJ   rK   )r&   rM   modes   `  r'   rN   %SetFwdGradEnabledContextManager.enterG  s    ##((779&&t,C	
 9900HH**G	


 ))0066r*   c                     U R                   R                  5         UR                  R                  S[        R
                  R                  U R                  40 5        [        R                  R                  S 5      $ r   )r?   r(   rT   r   r4   r   r   r   r
   rJ   rK   rX   s      r'   rZ   $SetFwdGradEnabledContextManager.exitW  sZ    


		HH**__		
 ))0066r*   r   Nr   r   r8   r*   r'   r   r   <  s!    a
 
7 7r*   r   c                   l    \ rS rSrSr\" \" 5       \R                  5      r	\
SS j5       rS rSS jrSrg)	DualLevelContextManagerib  z;Represents torch.autograd.forward_ad.dual_level ctx managerc                     [        SS S S.UD6$ r   )r   )rM   rD   s     r'   rK   DualLevelContextManager.createg  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 R                  l        [        R                  R!                  T R                  5      $ )Nc                  f   > [         R                  R                  R                  T R                  S9$ )N)level)r4   autograd
forward_adexit_dual_level	new_levelr%   s   r'   rf   /DualLevelContextManager.enter.<locals>.<lambda>s  s!    11AAAWr*   rz   r8   )r   r   r4   r   r   enter_dual_levelr   rI   rT   r   r   _enter_dual_levelr?   r!   r
   rJ   rK   rL   s   ` r'   rN   DualLevelContextManager.entero  s    d,,-22CCEW	
 9900HH&&	


 ))00@@r*   c                     U R                   R                  5         UR                  R                  S[        R
                  R                  U R                  40 5        [        R                  R                  S 5      $ r   )r?   r(   rT   r   r4   r   _exit_dual_levelr   r
   rJ   rK   rX   s      r'   rZ   DualLevelContextManager.exit}  sZ    


		HH%%^^		
 ))0066r*   )r   Nr   )r.   r/   r0   r1   r2   r   r   r   
DUAL_LEVELr   r   rK   rN   rZ   r7   r8   r*   r'   r   r   b  s9    E/1<3J3JK
 
A7r*   r   c                   l    \ rS rSrSr\" \" 5       \R                  5      r	\
SS j5       rS rSS jrSrg)	&GradIncrementNestingCtxManagerVariablei  z6represents torch.func.grad increment/decrement nestingc                 "    [        SS S S.UD6nU$ r   )r  r   s      r'   rK   -GradIncrementNestingCtxManagerVariable.create  s(    4 

 

 
r*   c                 ~   [        U R                  5        [        R                  R                  R                  5       nU R                  US 5        UR                  R                  S[        R                  R                  R
                  S0 5      U R                  l
        [        R                  R                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ r#   )r4   r   r   _grad_decrement_nestingr8   r*   r'   rf   >GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*Wr*   rz   r8   )r   r   r4   r   r   _grad_increment_nestingrI   rT   r   r?   r!   r
   rJ   rK   )r&   rM   
grad_levels      r'   rN   ,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WX9900HH77	


 ))00<<r*   c                     U R                   R                  5         UR                  R                  S[        R
                  R                  R                  S0 5        [        R                  R                  S 5      $ r   )r?   r(   rT   r   r4   r   r   r  r
   rJ   rK   rX   s      r'   rZ   +GradIncrementNestingCtxManagerVariable.exit  V    


		UXX00HH"b	
 ))0066r*   r8   Nr   r   r8   r*   r'   r  r    s:    @ /1<3U3UV 
=7r*   r  c                   P   ^  \ rS rSrSr\SS j5       rS	U 4S jjrS rS r	Sr
U =r$ )
CatchWarningsCtxManagerVariablei  z'Delay a call to warnings.catch_warningsc                     [        US S S9$ )N)catch_warnings_argsr=   r>   )r  )rM   r  s     r'   rK   &CatchWarningsCtxManagerVariable.create  s    . 3
 	
r*   c                 j   > [        U[        5      (       d   U5       e[        TU ]  " S0 UD6  Xl        g rA   )rw   dictrB   rC   r  )r&   r  rD   rE   s      r'   rC   (CatchWarningsCtxManagerVariable.__init__  s3    -t44I6II4"6"#6 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      $ r#   )r   )ctx_vals   r'   rf   7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*Lr*   r8   )
r  itemsas_python_constantwarningscatch_warningsrI   r
   rJ   rK   r   )r&   rM   kvrD   r  s        @r'   rN   %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      $ )Nr#  r$  )load_import_from)cgs   r'   rf   =CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    !4!4ZAQ!Rr*   F)	rh   foreachr  valuestuplekeysri   create_call_function_kwrk   )r&   r+  r0  s    ` r'   rm   +CatchWarningsCtxManagerVariable.reconstruct  sg    
RS


4++2245T--2245
33CItUKLr*   )r  r   r   )r.   r/   r0   r1   r2   r   rK   rC   rN   rm   r7   r   r   s   @r'   r  r    s/    1
 
7
FM Mr*   r  c                   l    \ rS rSrSr\" \" 5       \R                  5      r	\
SS j5       rS rSS jrSrg)	&VmapIncrementNestingCtxManagerVariablei  z1represents torch VMap increment/decrement nestingc                 "    [        SUS S.UD6nU$ r   )r4  )rM   r=   rD   r   s       r'   rK   -VmapIncrementNestingCtxManagerVariable.create  s(    4 
'
 

 
r*   c                    [        U R                  5        U R                  u  p#[        U[        R
                  5      (       a'  UR                  nUR                  5       R                  nO UR                  5       n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                  XS40 5      U R"                  l        [        R&                  R)                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ r#   )r4   r   r   _vmap_decrement_nestingr8   r*   r'   rf   >VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>  r  r*   rz   )r   r   r=   rw   r
   SymNodeVariablesym_numas_proxynoder"  r4   r   r   _vmap_increment_nestingrI   rT   r   r?   r!   rJ   rK   )r&   rM   
batch_size
randomnessbatch_size_valuebatch_size_node
vmap_levels          r'   rN   ,VmapIncrementNestingCtxManagerVariable.enter  s    d,,-!%!3!3
j)";";<<)11(11388O)<<>(;;=O224
XX((@@

 	b"WX9900HH77)	


 ))00<<r*   c                     U R                   R                  5         UR                  R                  S[        R
                  R                  R                  S0 5        [        R                  R                  S 5      $ r   )r?   r(   rT   r   r4   r   r   r9  r
   rJ   rK   rX   s      r'   rZ   +VmapIncrementNestingCtxManagerVariable.exit  r  r*   r8   Nr   r   r8   r*   r'   r4  r4    s:    ; /1<3U3UV =,7r*   r4  c                      ^  \ rS rSrSr\" \" 5       \R                  5      r	\
SSS jj5       r S SU 4S jjjrS rSS jr      SU 4S jjrSS jrS	 rS
 rSrU =r$ )GradModeVariablei  z6represents torch.{no_grad,enable_grad,set_grad_mode}()c                     [        SU/[        R                  " 5       /S.UD6nU(       a  UR                  XR                  5        U$ r   )rI  r4   is_grad_enabledrH   r=   )rM   target_valueinitializedrD   r   s        r'   rK   GradModeVariable.create  sK     
'.!1134
 

 NN2001
r*   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   rB   rC   r   r   )r&   r=   r>   rM  rD   rE   s        r'   rC   GradModeVariable.__init__  s2     	 	
'	
JP	
 	d,,-r*   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r#   rH   r=   r
   rJ   rK   rL   s     r'   rN   GradModeVariable.enter  +    ../))0066r*   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r#   rH   r>   r
   rJ   rK   rX   s      r'   rZ   GradModeVariable.exit  +    //0))0066r*   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ r#   rH   r>   rB   rz   r&   rM   rY   rD   rE   s       r'   rz   GradModeVariable.call_function  )     	//0w$Rv66r*   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   rz   )rk   r4   rK  rT   r   r   _set_grad_enabledr&   rM   r.  r   s       r'   rH   GradModeVariable._call_func(  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r*   c                     gNr4   r8   r%   s    r'   r^   GradModeVariable.module_name2      r*   c                     g)Nset_grad_enabledr8   r%   s    r'   r_   GradModeVariable.fn_name5  s    !r*   r8   )Fr   r}   r   rM   r   rY   r   rD   r   )r.   r/   r0   r1   r2   r   r   r   	GRAD_MODEr   r   rK   rC   rN   rZ   rz   rH   r^   r_   r7   r   r   s   @r'   rI  rI    s    @/1<3I3IJ  ?C.	. .777#7 &7 -	7." "r*   rI  c                   d   ^  \ rS rSr\S	S j5       r S
 SU 4S jjjrS	S jrS rS r	S r
SrU =r$ )InferenceModeVariablei9  c                 J    [        U/4S[        R                  " 5       0UD6nU$ )Nr>   )rn  r4   is_inference_mode_enabledrM   rL  rD   r   s       r'   rK   InferenceModeVariable.create:  s1    #N
+0+J+J+L
PV
 
r*   c                 f   > Uc  [         R                  " 5       n[        TU ]  " SXS.UD6  Xl        g r   )r4   rp  rB   rC   r=   r&   r=   r>   rD   rE   s       r'   rC   InferenceModeVariable.__init__A  s>     !"<<>N 	
'	
JP	
 +r*   c                     U R                   R                  5         UR                  R                  S[        R
                  R                  R                  U R                   R                  40 5        g r   )	r?   r,   rT   r   r4   r   	grad_mode_exit_inference_moder!   rX   s      r'   rZ   InferenceModeVariable.exitO  sM    

!!#
		NN$$99ZZ		
r*   c                 L  ^ [         R                  R                  R                  " U R                  6 mU R                  UU4S j5        UR                  R                  S[         R                  R                  R                  / U R                  Q70 5      U R                  l	        g )Nc                  V   > [         R                  R                  R                  T 5      $ r#   )r4   r   rw  rx  ctxs   r'   rf   -InferenceModeVariable.enter.<locals>.<lambda>[  s    00EEcJr*   rz   )
r4   r   rw  _enter_inference_moder=   rI   rT   r   r?   r!   r&   rM   r}  s     @r'   rN   InferenceModeVariable.enterX  s{    nn&&<<d>P>PQJ	
 9900NN$$::"d  "	


r*   c                     gre  r8   r%   s    r'   r^   !InferenceModeVariable.module_named  rg  r*   c                     g)Ninference_moder8   r%   s    r'   r_   InferenceModeVariable.fn_nameg  s    r*   r=   r   r#   r   r.   r/   r0   r1   r   rK   rC   rZ   rN   r^   r_   r7   r   r   s   @r'   rn  rn  9  sG      +
 
+ +


   r*   rn  c                   h   ^  \ rS rSrSr\S
S j5       r S SU 4S jjjrS
S jrS r	S r
S rS	rU =r$ )CUDADeviceVariableik  zrepresents torch.cuda.devicec                 \    [        S[        R                  R                  USS9/S S.UD6nU$ )NT)optionalr   r8   )r  r4   cuda_get_device_index)rM   devicerD   r   s       r'   rK   CUDADeviceVariable.createn  s?      
 ::777NO
 

 
r*   c                 6   > [         TU ]  " SXS.UD6  Xl        g r   rB   rC   r=   rt  s       r'   rC   CUDADeviceVariable.__init__w  s*     	 	
'	
JP	
 +r*   c                 
   U R                   R                  5         UR                  R                  S[        R
                  R                  U R                   R                  40 5        [        R                  R                  S5      $ )Nrz   F)r?   r,   rT   r   r4   r  _maybe_exchange_devicer!   r
   rJ   rK   rX   s      r'   rZ   CUDADeviceVariable.exit  s`    

!!#
		JJ--ZZ		
 ))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 R                  l        g )Nc                  B   > [         R                  R                  T 5      $ r#   )r4   r  r  )prev_idxs   r'   rf   *CUDADeviceVariable.enter.<locals>.<lambda>  s    %***K*KH*Ur*   rz   )	r4   r  _exchange_devicer=   rI   rT   r   r?   r!   )r&   rM   r  s     @r'   rN   CUDADeviceVariable.enter  sj    ::..0B0BCb"UV9900JJ''"d  "	


r*   c                     g)Nz
torch.cudar8   r%   s    r'   r^   CUDADeviceVariable.module_name      r*   c                     g)Nr  r8   r%   s    r'   r_   CUDADeviceVariable.fn_name  s    r*   r  r   r#   r   )r.   r/   r0   r1   r2   r   rK   rC   rZ   rN   r^   r_   r7   r   r   s   @r'   r  r  k  sJ    &  	+
 
	+ 	+8
 r*   r  c                      ^  \ rS rSrSr\" \" 5       \R                  5      r	\
SS j5       rS	S
U 4S jjjrS rSS jrSrU =r$ )TorchFunctionDisableVariablei  z>represents whether torch function overrides are enabled or notc                     [        SS/U R                  R                  /S.UD6nUR                  U S/5        UR	                  U 5        U$ )NFr   r8   )r  rT   torch_function_enabledrH   rI   r   s      r'   rK   #TorchFunctionDisableVariable.create  sU    * 
 'II<<=
 
 	rE7#R 
r*   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   rP  rt  s       r'   rC   %TorchFunctionDisableVariable.__init__  0     	
'	
JP	
 	d,,-r*   c                 @    [         R                  R                  S 5      $ r#   r
   rJ   rK   rL   s     r'   rN   "TorchFunctionDisableVariable.enter      ))0066r*   c                     [        U5      S:X  d   eUS   UR                  l        US   UR                  l        UR                  R                  US   5        g rv   )rk   symbolic_torch_function_statetorch_function_subclass_enabledtorch_function_mode_enabledrT   set_torch_function_state)r&   rM   r.  s      r'   rH   'TorchFunctionDisableVariable._call_func  sO    6{aKQRS9((HGMay((D
		**6!95r*   r8   r   r#   r   )r.   r/   r0   r1   r2   r   r   r   TORCH_FUNCTION_STATEr   r   rK   rC   rN   rH   r7   r   r   s   @r'   r  r    sH    H/1<3T3TU	 	. .76 6r*   r  c                      ^  \ rS rSrSr\" \" 5       \R                  5      r	\
S
S j5       rSSU 4S jjjrS rS
S jrS rS rS	rU =r$ )DeterministicAlgorithmsVariablei  zVrepresents torch.{are_deterministic_algorithms_enabled,use_deterministic_algorithms}()c                     [        SU/[        R                  " 5       /S.UD6nUR                  X/5        UR	                  U 5        U$ r   )r  r4   $are_deterministic_algorithms_enabledrH   rI   rq  s       r'   rK   &DeterministicAlgorithmsVariable.create  sR    - 
'.!FFHI
 

 	r>*R 
r*   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   rP  rt  s       r'   rC   (DeterministicAlgorithmsVariable.__init__  r  r*   c                 @    [         R                  R                  S 5      $ r#   r  rL   s     r'   rN   %DeterministicAlgorithmsVariable.enter  r  r*   c                     [        U5      S:X  d   eUS   nUR                  R                  S[        R                  R
                  U40 5      4  [        R                  R                  U5        g r`  )rk   rT   r   r4   r   _set_deterministic_algorithmsrb  s       r'   rH   *DeterministicAlgorithmsVariable._call_func  s^    6{aq	II!!!G!G%SU	

 	..u5r*   c                     gre  r8   r%   s    r'   r^   +DeterministicAlgorithmsVariable.module_name  rg  r*   c                     g)Nuse_deterministic_algorithmsr8   r%   s    r'   r_   'DeterministicAlgorithmsVariable.fn_name  s    -r*   r8   r   r#   r   )r.   r/   r0   r1   r2   r   r   r   DETERMINISTIC_ALGORITHMSr   r   rK   rC   rN   rH   r^   r_   r7   r   r   s   @r'   r  r    sV    `\BB  . .76. .r*   r  c                   d   ^  \ rS rSrSr\S
S j5       rSSU 4S jjjrS rS
S jr	S r
S rS	rU =r$ )!DisabledSavedTensorsHooksVariablei  z;represents torch.autograd.graph.disable_saved_tensors_hook.c                     [        SU/[        R                  R                  R	                  5       /S.UD6nUR                  X/5        UR                  U 5        U$ r   )r  r4   r   	_autograd/_saved_tensors_hooks_get_disabled_error_messagerH   rI   rq  s       r'   rK   (DisabledSavedTensorsHooksVariable.create  s^    / 
'.""RRT

 
 	r>*R 
r*   c                 *   > [         TU ]  " SXS.UD6  g r   rB   rC   rt  s       r'   rC   *DisabledSavedTensorsHooksVariable.__init__  s      	
'	
JP	
r*   c                 @    [         R                  R                  S 5      $ r#   r  rL   s     r'   rN   'DisabledSavedTensorsHooksVariable.enter  r  r*   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   rz   r8   )rk   rT   r   r4   r   r  _saved_tensors_hooks_disable_saved_tensors_hooks_enablerb  s       r'   rH   ,DisabledSavedTensorsHooksVariable._call_func  s    6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r*   c                     g)Nztorch.autograd.graphr8   r%   s    r'   r^   -DisabledSavedTensorsHooksVariable.module_name  s    %r*   c                     g)Ndisable_saved_tensors_hooksr8   r%   s    r'   r_   )DisabledSavedTensorsHooksVariable.fn_name  s    ,r*   r8   r   r#   r   )r.   r/   r0   r1   r2   r   rK   rC   rN   rH   r^   r_   r7   r   r   s   @r'   r  r    s;    E
 

 

7=*&- -r*   r  c                   \   ^  \ rS rSr\S 5       rS	S
U 4S jjjrSS jrS rS r	S r
SrU =r$ )AutocastModeVariablei  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>   )r4   ampautocast_modeautocastr  r  inspect	signaturebindapply_defaultsclear	argumentsrw   r   appendr"  r  )funcrY   rD   
bound_argsr=   keyargr   s           r'   rK   AutocastModeVariable.create  sF   II##,,JJNN##IIMM""
 
 	
 
 &&t,114B6B
!!#GCm#

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

(?(? ?fU **3/#//$$S%;%;%=>$$S) H #=PPP
r*   c                 6   > [         TU ]  " SXS.UD6  Xl        g r   r  rt  s       r'   rC   AutocastModeVariable.__init__?  s(     	
'	
JP	
 +r*   c                 
   U R                   R                  5         UR                  R                  S[        R
                  R                  U R                   R                  40 5        [        R                  R                  S 5      $ r   )r?   r,   rT   r   r4   r  _exit_autocastr!   r
   rJ   rK   rX   s      r'   rZ   AutocastModeVariable.exitE  s]    

!!#
		UYY55

8H8H7JB	
 ))0066r*   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 R                  l        g )Nc                  B   > [         R                  R                  T 5      $ r#   )r4   r  r  r|  s   r'   rf   ,AutocastModeVariable.enter.<locals>.<lambda>N  s    %))*B*B3*Gr*   rz   )	r4   r  _enter_autocastr=   rI   rT   r   r?   r!   r  s     @r'   rN   AutocastModeVariable.enterL  si    ii''););<b"GH9900UYY668N$:L:L8NPR


r*   c                     g)Nztorch.amp.autocast_moder8   r%   s    r'   r^    AutocastModeVariable.module_nameS  s    (r*   c                     g)Nr  r8   r%   s    r'   r_   AutocastModeVariable.fn_nameV  s    r*   r  r#   r   r   r  r   s   @r'   r  r    s8     >+ +7
) r*   r  c                   P   ^  \ rS rSrSrS	S
U 4S jjjrS rSS jrS rS r	Sr
U =r$ )NullContextVariableiZ  z6
This class represents Python contextlib.nullcontext.
c                 *   > [         TU ]  " SSU0UD6  g Nr=   r8   r  )r&   r=   rD   rE   s      r'   rC   NullContextVariable.__init___  s    ?}??r*   c                 @    [         R                  R                  S 5      $ r#   r  rL   s     r'   rN   NullContextVariable.enterb  r  r*   c                 @    [         R                  R                  S 5      $ r#   r  rX   s      r'   rZ   NullContextVariable.exite  r  r*   c                     gN
contextlibr8   r%   s    r'   r^   NullContextVariable.module_nameh  r  r*   c                     gNnullcontextr8   r%   s    r'   r_   NullContextVariable.fn_namek      r*   r8   r#   r   r   )r.   r/   r0   r1   r2   rC   rN   rZ   r^   r_   r7   r   r   s   @r'   r   r   Z  s-    @ @77 r*   r   c                   R   ^  \ rS rSrSrS
U 4S jjrS rSS jrS rS r	S r
S	rU =r$ )ProfilerContextVariableio  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.
c                 *   > [         TU ]  " SSS 0UD6  g r  r  )r&   rD   rE   s     r'   rC    ProfilerContextVariable.__init__x  s    6t6v6r*   c                     U $ r#   r8   rL   s     r'   rN   ProfilerContextVariable.enter{  s    r*   c                 @    [         R                  R                  S 5      $ r#   r  rX   s      r'   rZ   ProfilerContextVariable.exit~  r  r*   c                     gr	  r8   r%   s    r'   r^   #ProfilerContextVariable.module_name  r  r*   c                     gr  r8   r%   s    r'   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   strr	   SUPPORTABLE)r&   r+  s     r'   rm   #ProfilerContextVariable.reconstruct  s*    HIr"..		
r*   r8   r   r   )r.   r/   r0   r1   r2   rC   rN   rZ   r^   r_   rm   r7   r   r   s   @r'   r  r  o  s+    77
 
r*   r  c                   T   ^  \ rS rSr\SS j5       rSS	U 4S jjjrS rSS jrSr	U =r
$ )
StreamContextVariablei  c           
          SSK Jn  [        UR                  5      R                  nU" [
        U U R                  R                  SUS0 5      5      n[        SU/U/UR                  S.UD6$ )Nr   wrap_fx_proxy_clsrz   r#   )r=   r>   r  r8   )	builderr+  r   r  current_streamStreamVariablerT   create_proxyr(  )rM   rL  rD   r+  current_stream_methodr-  s         r'   rK   StreamContextVariable.create  s    . 8!

. 	 +II""%		
 % 
'.*+&&
 	
 	
r*   c                    > [         TU ]  " SXS.UD6  X l        [        U R                  5      R                  U l        [        U R                  5      R
                  U l        g r   )rB   rC   r  r   
set_stream_set_stream_by_idset_stream_id)r&   r=   r  r>   rD   rE   s        r'   rC   StreamContextVariable.__init__  sT     	
'	
JP	
 24;;?JJ5dkkBTTr*   c                   ^  T R                   S   R                  5       bE  UR                  R                  ST R                  T R                   S   R                  5       40 5        ObT R                   S   R
                  nUR                  R                  ST R                  UR                  UR                  UR                  40 5        T R	                  T R                   S   R
                  5        T R                  UU 4S j5        g )Nr   rz   c                  T   > T R                  T R                  S   R                  5      $ )Nr   )r3  r>   r   r%   s   r'   rf   -StreamContextVariable.enter.<locals>.<lambda>  s    $//$:M:Ma:P:V:V*Wr*   )r=   r=  rT   r/  r3  r   r5  	stream_iddevice_indexr  rI   )r&   rM   streams   `  r'   rN   StreamContextVariable.enter  s    a ))+7II""##A&//13	 ''*00FII""""!!6#6#68J8JK	 	**1-334b"WXr*   c                     UR                   R                  SU R                  U R                  S   R	                  5       40 5        U R
                  R                  5         g )Nrz   r   )rT   r/  r3  r>   r=  r?   r,   rX   s      r'   rZ   StreamContextVariable.exit  sM    
		OO  #,,.0		
 	

!!#r*   )r  r3  r5  r   r#   r   )r.   r/   r0   r1   r   rK   rC   rN   rZ   r7   r   r   s   @r'   r(  r(    s1    
 
.U UY*$ $r*   r(  c                   f   ^  \ rS rSrSr\S 5       r\S 5       rS
U 4S jjrS r	SS jr
S rS	rU =r$ )PreserveVersionContextVariablei  z7
Wraps torch.autograd._unsafe_preserve_version_counter
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O@[        R                  " UR
                   Vs/ s H  o"R	                  U S5      PM     sn5      n[        X5      $ s  snf s  snf )N_version)rw   r
   TensorVariableTupleVariablevar_getattrr!  rA  )rM   tensorsr   versionss       r'   _create_lambda_from_tensors:PreserveVersionContextVariable._create_lambda_from_tensors  s    gy7788 ..9@	B	1r:.	BH  --wi8G ..8?F1r:.FH .g@@ C
 Gs   B:B?c                 8   ^  [         R                  " U 4S j5      $ )Nc                 0   > [         R                  TU 5      $ r#   )rA  rI  )rG  rM   s    r'   rf   <PreserveVersionContextVariable.constructor.<locals>.<lambda>  s    :VVGr*   )r
   LambdaVariable)rM   s   `r'   constructor*PreserveVersionContextVariable.constructor  s    ''
 	
r*   c                   > 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                  [        R                  45      (       a'  [        R                  " U R                  /5      U l        g g r  )
setdefaultrB   rC   rG  prev_versionsrw   r
   rD  rE  rJ   r;  )r&   rG  rS  rD   rE   s       r'   rC   'PreserveVersionContextVariable.__init__  s    /40"6"*dllI$<$<==$22DLL>BDL!;!;Y=V=V W
 
 "+!8!8$:L:L9M!ND
r*   c                     g r#   r8   rL   s     r'   rN   $PreserveVersionContextVariable.enter  s    r*   c                     SSK Jn  [        R                  " U5      R	                  XR
                  U R                  /0 5      $ )Nr   )_unsafe_set_version_counter)tensor_version_oprX  r
   TorchInGraphFunctionVariablerz   rG  rS  )r&   rM   rY   rX  s       r'   rZ   #PreserveVersionContextVariable.exit  s7    C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     r'   rm   *PreserveVersionContextVariable.reconstruct  s-    bIU"..
	
r*   )rS  rG  r   r   )r.   r/   r0   r1   r2   r   rI  rO  rC   rN   rZ   rm   r7   r   r   s   @r'   rA  rA    sM     
A 
A 
 
OD
 
r*   rA  c                      ^  \ rS rSr\" \" 5       \R                  5      r\	SS j5       r
 S SU 4S jjjrS rSS jr      SU 4S jjrSS jrS rS	 rS
rU =r$ )&FSDPParamGroupUseTrainingStateVariablei  c                 P    [        SUU/UR                  R                  /S.UD6nU$ )N)param_group_varr=   r>   r8   )r_  r   _training_state)rM   ra  rL  rD   r   s        r'   rK   -FSDPParamGroupUseTrainingStateVariable.create  s<    4 
+'.+11AAB
 	
 
r*   c                 `   > [         TU ]  " SX#S.UD6  Xl        [        U R                  5        g r   )rB   rC   ra  r   r   )r&   ra  r=   r>   rD   rE   s        r'   rC   /FSDPParamGroupUseTrainingStateVariable.__init__  s8     	 	
'	
JP	
  /d,,-r*   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r#   rS  rL   s     r'   rN   ,FSDPParamGroupUseTrainingStateVariable.enter'  rU  r*   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r#   rW  rX   s      r'   rZ   +FSDPParamGroupUseTrainingStateVariable.exit+  rY  r*   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ r#   r[  r\  s       r'   rz   4FSDPParamGroupUseTrainingStateVariable.call_function/  r^  r*   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__rb  )	rk   ra  r   rb  call_methodr
   rJ   rK   EnumVariablerb  s       r'   rH   1FSDPParamGroupUseTrainingStateVariable._call_func8  s    6{aq	%%55>  ,,..556GH**51  :?  &&6 ?r*   c                     g)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr8   r%   s    r'   r^   2FSDPParamGroupUseTrainingStateVariable.module_nameG  s    Ur*   c                     g)Nuse_training_stater8   r%   s    r'   r_   .FSDPParamGroupUseTrainingStateVariable.fn_nameJ      #r*   )ra  r   r#   r   rk  )r.   r/   r0   r1   r   r   r   FSDP_TRAINING_STATEr   r   rK   rC   rN   rZ   rz   rH   r^   r_   r7   r   r   s   @r'   r_  r_    s    /1<3S3ST  >B.	. .777#7 &7 -	7?V$ $r*   r_  c                      ^  \ rS rSrSr\SS j5       r SS\\R                  R                  R                     SS4U 4S jjjr\S 5       rS	 rSS
 jrS rS rSrU =r$ )SDPAKernelVariableiN  z)represents torch.nn.attention.sdpa_kernelc                     [        U[        R                  R                  R                  5      (       a  U/n[        SUS S.UD6nU$ r   )rw   r4   nn	attention
SDPBackendry  )rM   backendsrD   r   s       r'   rK   SDPAKernelVariable.createQ  sL    h 2 2 = =>> zH  
"
 

 
r*   Nr=   r   c                 *   > [         TU ]  " SXS.UD6  g r   r  rt  s       r'   rC   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   )rT   r   r4   r{  r|  _backend_from_stringname)rM   r~  backendnodess       r'   _backends_to_nodes%SDPAKernelVariable._backends_to_nodesf  se     $
 $ II!!""77	 $ 	 
 
s   AAc                   ^  [         R                  R                  R                  5       T l        T R                  UU 4S j5        [         R                  R                  R                  T R                  5        T R                  UT R                  5      nUR                  R                  S[         R                  R                  R                  U40 5        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ r#   )r4   r{  r|  _sdpa_kernelprev_backendsr%   s   r'   rf   *SDPAKernelVariable.enter.<locals>.<lambda>w  s     **778J8JKr*   rz   )r4   r{  r|  _cur_sdpa_kernel_backendsr  rI   r  r=   r  rT   r   r
   rJ   rK   )r&   rM   r  s   `  r'   rN   SDPAKernelVariable.entert  s    "XX//IIKK	
 	''(:(:;%%b$*<*<=
		HH++F		
 ))0066r*   c                 ,   U R                   R                  5         U R                  XR                  5      nUR                  R                  S[        R                  R                  R                  U40 5        [        R                  R                  S 5      $ r   )r?   r,   r  r  rT   r   r4   r{  r|  r  r
   rJ   rK   )r&   rM   rY   r  s       r'   rZ   SDPAKernelVariable.exit  sq    

!!#%%b*<*<=
		HH++F		
 ))0066r*   c                     g)Nztorch.nn.attentionr8   r%   s    r'   r^   SDPAKernelVariable.module_name  rv  r*   c                     g)N_sdpa_kernel_variadicr8   r%   s    r'   r_   SDPAKernelVariable.fn_name  s    &r*   )r  r   r#   )r.   r/   r0   r1   r2   r   rK   listr4   r{  r|  r}  rC   r  rN   rZ   r^   r_   r7   r   r   s   @r'   ry  ry  N  s{    3  
EHH..99:

 

 
  7	7$
' 'r*   ry  c                   Z   ^  \ rS rSrSU 4S jjrS r      S	U 4S jjrS rS rSr	U =r
$ )
r.  i  c                   > Ub9  SUR                   R                  ;   a  UR                   R                  S   U:X  d   eUR                  R                  UR                  :X  d   S5       e[        TU ]  " S0 UD6  Xl        X l        X0l        g )Nexample_valuez.stream value is not equal to the passed devicer8   )r>  metar  r   rB   rC   r!   r   )r&   r!   r   r  rD   rE   s        r'   rC   StreamVariable.__init__  sz    EJJOO!C::???3u<<<||  FKK/ 	
<	
/ 	"6"

r*   c                 "    [         R                  $ r#   )r4   Streamr%   s    r'   python_typeStreamVariable.python_type  s    ||r*   c                 4  > [        U R                  U5      (       d
   SU 35       eSSKJnJn  SSKJn  US;   a?  UR                  R                  " SU/U" U /U-   U5      Q76   [        R                  " S 5      $ US:X  a=  U" [        R                  UUR                  R                  " SU/U" U /U-   U5      Q76 S	9$ US
:X  a3  U" [        UUR                  R                  " SU/U" U /U-   U5      Q76 S	9$ X%;   a  [        U5      S:X  a  U(       dy  US   n[        U[        5      (       d#  [        R                  R                  [         5      $ [        R                  R                  XR   " U R                  UR                  5      5      $ ["        T	U ]I  XX45      $ )Nzno stream method found named r   )cmp_name_to_op_mappingproxy_args_kwargsr   r*  )wait_streamsynchronize
wait_eventrn  query
target_clsrM   r!   record_eventr   )hasattrr   utilsr  r  r,  r+  rT   r/  r
   rJ   EventVariablerk   rw   r.  rK   NotImplementedrB   rn  )
r&   rM   r  rY   rD   r  r  r+  otherrE   s
            r'   rn  StreamVariable.call_method  s    tzz4((P,I$*PP(E.??II""t&7v&N --d33W_$$55ii,,!4*;TFTM6*R  ^#$(ii,,!4*;TFTM6*R  +D	Qv GEe^44 1188HH--44&,TZZE  w"2T::r*   c                     U R                   $ r#   r!   r%   s    r'   r=  StreamVariable.as_proxy      zzr*   c                     U R                   (       a   eSU R                   3nUR                  R                  R	                  X R
                  5      nUR                  UR                  USS95        g )N_stream_Tadd)r   r  rM   rT   install_global_by_idr   append_outputcreate_load_globalr&   ra   prefixr  s       r'   rm   StreamVariable.reconstruct  s^     ;; DKK=)zz  55fjjIg8848HIr*   )r  r!   r   r   rY   r   rD   r   r   r   )r.   r/   r0   r1   rC   r  rn  r=  rm   r7   r   r   s   @r'   r.  r.    sB    	+; &	+;
 -+; 
+;ZJ Jr*   r.  c                   N   ^  \ rS rSrSU 4S jjr      SS jrS rS rSrU =r	$ )	r  i  c                    > Ub9  SUR                   R                  ;   a  UR                   R                  S   U:X  d   e[        TU ]  " S0 UD6  Xl        X l        g )Nr  r8   )r>  r  rB   rC   r!   r   )r&   r!   r   rD   rE   s       r'   rC   EventVariable.__init__  sM    EJJOO!C::???3u<<<"6"

r*   c                    SSK Jn  SSKJn  US;   a?  UR                  R
                  " SU/U" U /U-   U5      Q76   [        R                  " S 5      $ US:X  a=  U" [        R                  UUR                  R
                  " SU/U" U /U-   U5      Q76 S9$ [        S	[        U5      S
U S34/ [        R                  QS9  g )Nr   )r  r   r*  )waitrecordr  rn  r  r  z#Unsupported torch.cuda.Event methodz4Dynamo doesn't support tracing the torch.cuda.Event.zC method. We currently support wait, record, synchronize, and query.r  )r  r  r,  r+  rT   r/  r
   rJ   r   r$  r	   r%  )r&   rM   r  rY   rD   r  r+  s          r'   rn  EventVariable.call_method  s     	..44II""t&7v&N --d33W_$$55ii,,!4*;TFTM6*R  =D	J4& QQ R&22
r*   c                     U R                   $ r#   r  r%   s    r'   r=  EventVariable.as_proxy  r  r*   c                     U R                   (       a   eSnUR                  R                  R                  X R                  5      nUR                  UR                  USS95        g )N_eventTr  )r   rM   rT   r  r   r  r  r  s       r'   rm   EventVariable.reconstruct  sQ     ;;zz  55fjjIg8848HIr*   )r!   r   r   r  )
r.   r/   r0   r1   rC   rn  r=  rm   r7   r   r   s   @r'   r  r    s=    " &	"
 -" 
"HJ Jr*   r  c                   |   ^  \ rS rSrS1\R
                  krS\\\4   SS4U 4S jjr	        S
S jr
S rS	rU =r$ )WithExitFunctionVariablei  targetr}  r   Nc                 x   > [         TU ]  " S0 UD6  [        U[        [        45      (       d   eXl        X l        g rA   )rB   rC   rw   r:   r   r}  r  )r&   r}  r  rD   rE   s       r'   rC   !WithExitFunctionVariable.__init__$  sD     	"6")+IJ
 
 	
 
 r*   c                 N    U(       a   eU R                   R                  " U/UQ76 $ r#   )r}  rZ   ry   s       r'   rz   &WithExitFunctionVariable.call_function1  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}  rb   rM   rT   partial_convertsysversion_infor  r   ri   r=   rj   r   rk   r   r  )r&   ra   rl   s      r'   rm   $WithExitFunctionVariable.reconstruct:  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/   r0   r1   r   r   r   r:   r   rC   rz   rm   r7   r   r   s   @r'   r  r    su    		'	'N
*,JJK
 
(#( &( -	(
 
(A Ar*   r  )Gr2   dataclassesr  r  r#  typingr   r   r   r   torch._Cr4   torch._guardsr    r	   r
   bytecode_transformationr   r   r   device_interfacer   excr   guardsr   r   r   r   r   baser   	functionsr   r   r   r   r   user_definedr   torch._dynamo.symbolic_convertr   	dataclassr   r:   r   r   r   r   r   r   r  r  r4  rI  rn  r  r  r  r  r  r   r  r(  rA  r_  ry  r.  r  r  r8   r*   r'   <module>r     s  (   
  ; ;   + 
 8 " 0 2 !  4 D   (Ko K\(%> (V%70G %7P!77N !7H&7,C &7R#7&= #7L#75 #7L$7-D $7NM&= M>.7-D .7b7". 7"t/ 3 / d/0 /d6#: 6B).&= ).X1-(? 1-h92 9x1 * 
5  
F=$3 =$@<
%< <
~:$-D :$zF'0 F'RKJ_ KJ\6JO 6Jr-A -Ar*   