
    7h                     X   S r SSKrSSKrSSKrSSK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#J$r$J%r%J&r&  SSK'J(r(  \(       a  SSK)J*r*  SSK+J,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. S/\-5      r; " S0 S1\-5      r< " S2 S3\-5      r= " S4 S5\-5      r> " S6 S7\-5      r? " S8 S9\-5      r@ " S: S;\-5      rA " S< S=\-5      rB " S> S?\-5      rC " S@ SA\5      rD " SB SC\5      rE " SD SE\-5      rF " SF SG\5      rGg)Ha)  
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)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SkipFunctionVariableUserFunctionVariableUserMethodVariable!WrappedNestedUserFunctionVariableWrappedSkipFunctionVariableWrappedUserFunctionVariableWrappedUserMethodVariable)UserDefinedObjectVariable)	PyCodegenInstructionTranslatorc                      ^  \ rS rSrSSSS1\R
                  krSSU 4S jjjrS rSSS jjrSS	 jr	SS
 jr
SS jrS rS r        SS jrS rS rS rS rSrU =r$ )ContextWrappingVariable<   cm_objtarget_valuesinitial_valuesstatec                 >   > [         TU ]  " S0 UD6  Xl        X l        g N )super__init__r$   r%   selfr$   r%   kwargs	__class__s       ]/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_dynamo/variables/ctx_manager.pyr+    ContextWrappingVariable.__init__E   s    "6"*,    c                     U R                  XR                  5        U R                  U5        [        R                  R                  S 5      $ N)
_call_funcr$   set_cleanup_hookr   ConstantVariablecreater-   txs     r0   enterContextWrappingVariable.enterJ   s9    ../b!))0066r2   c                 t   ^ ^ Uc  U U4S jnUT l         TR                  R                  T R                  5        g )Nc                  >   > T R                  TT R                  5        g r4   )r5   r%   r9   s   r0   fn4ContextWrappingVariable.set_cleanup_hook.<locals>.fnR   s    D$7$78r2   
cleanup_fnoutputadd_cleanup_hookcleanupr-   r:   r?   s   `` r0   r6   (ContextWrappingVariable.set_cleanup_hookO   s,    :9 
		""4<<0r2   c                 `    U R                  5         [        R                  R                  S 5      $ r4   )cleanup_assertr   r7   r8   r-   r:   argss      r0   exitContextWrappingVariable.exitX   s%    ))0066r2   c                     U" [        UR                  R                  U R                  5       5      U R	                  5       5      5        g r4   )r   r:   import_sourcemodule_namefn_namer-   codegens     r0   reconstruct_type(ContextWrappingVariable.reconstruct_type\   s2    wzz//0@0@0BCT\\^T	
r2   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      $ r4   )rT   )rS   r-   s   r0   <lambda>5ContextWrappingVariable.reconstruct.<locals>.<lambda>b   s    d&;&;G&Dr2   r)   F)add_push_nullr$   extend_outputcreate_load_constr	   len)r-   rS   r$   vals   ``  r0   reconstruct#ContextWrappingVariable.reconstructa   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r-   s    r0   rP   #ContextWrappingVariable.module_namei   s    !">??r2   c                     [        S5      e)Nzfn_name called on baserb   rd   s    r0   rQ   ContextWrappingVariable.fn_namel   s    !":;;r2   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      $ g Nr   r   )
r]   
isinstancer   r   r   r   r   r   r   r   r-   r:   rK   r.   s       r0   call_function%ContextWrappingVariable.call_functiono   s     4yA~~G*$"$	
 
 	
 
 d1g9::4T!WdCCd1g344.tAw==d1g122,T!Wd;;d1g344.tAw== 5r2   c                     gNTr)   rd   s    r0   supports_graph_breaks-ContextWrappingVariable.supports_graph_breaks       r2   c                     gro   r)   rd   s    r0   exit_on_graph_break+ContextWrappingVariable.exit_on_graph_break   rr   r2   c                 N    U R                   b  U R                  5         S U l         g g r4   )rB   rd   s    r0   rE   ContextWrappingVariable.cleanup   s"    ??&OO"DO 'r2   c                 T    U R                   (       d   S5       eU R                  5         g )Nzmultiple exits?)rB   rE   rd   s    r0   rI   &ContextWrappingVariable.cleanup_assert   s    1 11r2   )rB   r%   r$   r4   returnNr:   r   rS   r   r:   r   rK   list[VariableTracker]r.   dict[str, VariableTracker]r{   r   )__name__
__module____qualname____firstlineno__r   _nonvar_fieldsr+   r;   r6   rL   rT   r_   rP   rQ   rl   rp   rt   rE   rI   __static_attributes____classcell__r/   s   @r0   r!   r!   <   s    	
 
	'	'N- -
7
17

O@<>#> &> -	>
 
>:#
 r2   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_typer)   )r*   r+   r/   r#   )r-   r#   r.   r/   s      r0   r+   'GenericContextWrappingVariable.__init__   s>    !!! 	
''	
 	

 r2   c                 .    U R                   R                  $ r4   )r#   r   rd   s    r0   rP   *GenericContextWrappingVariable.module_name   s    {{%%%r2   c                 @    [        U R                  5      R                  $ r4   )typer#   r   rd   s    r0   rQ   &GenericContextWrappingVariable.fn_name   s    DKK )))r2   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__rl   )r-   r:   r   s      r0   r;   $GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r2   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   rl   active_generic_context_managerspop)r-   r:   rK   r   xs        r0   rL   #GenericContextWrappingVariable.exit   sp    ,*T[[*2U((KK  ))
 -"
%	 	

 	**..0r2   c                     gNFr)   rd   s    r0   rp   4GenericContextWrappingVariable.supports_graph_breaks   s    r2   c                     gro   r)   rd   s    r0   rt   2GenericContextWrappingVariable.exit_on_graph_break   rr   r2   )r#   rz   r|   )r   r   r   r   r+   rP   rQ   r;   rL   rp   rt   r   r   r   s   @r0   r   r      s+    &*$ r2   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 requires gradc                     [        SUS S.UD6$ Nr$   r%   r)   )r   r:   r$   r.   s      r0   r8   0GradInplaceRequiresGradCtxManagerVariable.create   s$    8 
'
 
 	
r2   c                   ^  T R                   u  n[        R                  R                  R	                  5       T l        [        R                  R                  R                  U5        T R                  UU 4S j5        UR                  R                  S[        R                  R                  R                  U40 5      T l
        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ r4   )torch_C
_functorch!set_inplace_requires_grad_allowed
prev_staterd   s   r0   rX   AGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>   s!    EHH''IIr2   rl   )r$   r   r   r   !get_inplace_requires_grad_allowedr   r   r6   rC   create_nodeproxyr   r7   r8   )r-   r:   enableds   `  r0   r;   /GradInplaceRequiresGradCtxManagerVariable.enter   s    &&	((--OOQ==gF	
 YY**HHAAJ	

 ))0066r2   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  R                  S 5      $ Nrl   )rE   rC   r   r   r   r   r   r   r   r7   r8   rJ   s      r0   rL   .GradInplaceRequiresGradCtxManagerVariable.exit   sY    
		HHAA__		
 ))0066r2   r   r   Nr|   
r   r   r   r   __doc__staticmethodr8   r;   rL   r   r)   r2   r0   r   r      s!    -
 
7$7r2   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      r0   r8   7TemporarilyPopInterpreterStackCtxManagerVariable.create   s$    ? 
'
 
 	
r2   c                 R  ^  [         R                  R                  R                  5       T l        T R                  UU 4S j5        UR                  R                  S[         R                  R                  R                  S0 5      T l        [        R                  R                  S 5      $ )Nc                  j   > [         R                  R                  R                  T R                  5      $ r4   )r   r   r   push_dynamic_layer_stacksavedrd   s   r0   rX   HTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>   s    EHH''@@Lr2   rl   r)   )r   r   r   pop_dynamic_layer_stackr   r6   rC   r   r   r   r7   r8   r9   s   ` r0   r;   6TemporarilyPopInterpreterStackCtxManagerVariable.enter   s~    XX((@@B
L	
 YY**HH77	

 ))0066r2   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        [        R                  R                  S 5      $ r   )rE   rC   r   r   r   r   r   r   r   r7   r8   rJ   s      r0   rL   5TemporarilyPopInterpreterStackCtxManagerVariable.exit  sX    
		HH88ZZM		
 ))0066r2   )r   r   Nr|   r   r)   r2   r0   r   r      s!    T
 
77r2   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   r:   r.   vars      r0   r8   ,JvpIncrementNestingCtxManagerVariable.create  s(    3 

 

 
r2   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       $ r4   )r   r   eager_transformsexit_jvp_nestingr)   r2   r0   rX   =JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>*  s    ((99JJLr2   rl   r)   )r   _guards_singletonr   r   r   enter_jvp_nestingr6   rC   r   r   _jvp_increment_nestingr   r   r7   r8   )r-   r:   	jvp_levels      r0   r;   +JvpIncrementNestingCtxManagerVariable.enter&  s    d,,-$$55GGI	L	
 YY**HH66	

 ))00;;r2   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R                  S 5      $ Nrl   r)   )
rE   rC   r   r   r   r   _jvp_decrement_nestingr   r7   r8   rJ   s      r0   rL   *JvpIncrementNestingCtxManagerVariable.exit4  sO    
		UXX00GGR	
 ))0066r2   r   Nr|   r   r   r   r   r   r   r   r   FUNCTORCH_STACK_MATCHr   r   r8   r;   rL   r   r)   r2   r0   r   r     s:    ? /1<3U3UV <7r2   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      r0   r8   &SetFwdGradEnabledContextManager.create?  s$    . 
'
 
 	
r2   c                   ^  T R                   u  n[        R                  R                  5       T l        [        R                  R                  U5        T R                  UU 4S j5        UR                  R                  S[        R                  R
                  U40 5      T l	        [        R                  R                  S 5      $ )Nc                  V   > [         R                  R                  T R                  5      $ r4   )r   r   _set_fwd_grad_enabledr   rd   s   r0   rX   7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>M  s    EHH224??Cr2   rl   )r$   r   r   _is_fwd_grad_enabledr   r   r6   rC   r   r   r   r7   r8   )r-   r:   modes   `  r0   r;   %SetFwdGradEnabledContextManager.enterG  s    ##((779&&t,C	
 YY**HH**G	

 ))0066r2   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S 5      $ r   )
rE   rC   r   r   r   r   r   r   r7   r8   rJ   s      r0   rL   $SetFwdGradEnabledContextManager.exitW  sS    
		HH**__		
 ))0066r2   r   Nr|   r   r)   r2   r0   r   r   <  s!    a
 
7 7r2   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   )r:   r.   s     r0   r8   DualLevelContextManager.createg  s$    & 

 
 	
r2   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_levelrd   s   r0   rX   /DualLevelContextManager.enter.<locals>.<lambda>s  s!    11AAAWr2   rl   r)   )r   r   r   r   r   enter_dual_levelr   r6   rC   r   r   _enter_dual_levelr   r   r7   r8   r9   s   ` r0   r;   DualLevelContextManager.entero  s    d,,-22CCEW	
 YY**HH&&	

 ))00@@r2   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S 5      $ r   )
rE   rC   r   r   r   _exit_dual_levelr   r   r7   r8   rJ   s      r0   rL   DualLevelContextManager.exit}  sS    
		HH%%^^		
 ))0066r2   )r   r   Nr|   )r   r   r   r   r   r   r   r   
DUAL_LEVELr   r   r8   r;   rL   r   r)   r2   r0   r   r   b  s9    E/1<3J3JK
 
A7r2   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      r0   r8   -GradIncrementNestingCtxManagerVariable.create  s(    4 

 

 
r2   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       $ r4   )r   r   r   _grad_decrement_nestingr)   r2   r0   rX   >GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*Wr2   rl   r)   )r   r   r   r   r   _grad_increment_nestingr6   rC   r   r   r   r7   r8   )r-   r:   
grad_levels      r0   r;   ,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WXYY**HH77	

 ))00<<r2   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R                  S 5      $ r   )
rE   rC   r   r   r   r   r  r   r7   r8   rJ   s      r0   rL   +GradIncrementNestingCtxManagerVariable.exit  O    
		UXX00HH"b	
 ))0066r2   r   Nr|   r   r)   r2   r0   r  r    s:    @ /1<3U3UV 
=7r2   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  )r:   r  s     r0   r8   &CatchWarningsCtxManagerVariable.create  s    . 3
 	
r2   c                 j   > [        U[        5      (       d   U5       e[        TU ]  " S0 UD6  Xl        g r(   )rj   dictr*   r+   r  )r-   r  r.   r/   s      r0   r+   (CatchWarningsCtxManagerVariable.__init__  s3    -t44I6II4"6"#6 r2   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      $ r4   )r   )ctx_vals   r0   rX   7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*Lr2   r)   )
r  itemsas_python_constantwarningscatch_warningsr6   r   r7   r8   r   )r-   r:   kvr.   r   s        @r0   r;   %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   r0   rX   =CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    !4!4ZAQ!Rr2   F)	rZ   foreachr  valuestuplekeysr[   create_call_function_kwr]   )r-   r,  r1  s    ` r0   r_   +CatchWarningsCtxManagerVariable.reconstruct  sg    
RS


4++2245T--2245
33CItUKLr2   )r  r|   rz   )r   r   r   r   r   r   r8   r+   r;   r_   r   r   r   s   @r0   r  r    s/    1
 
7
FM Mr2   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   )r5  )r:   r$   r.   r   s       r0   r8   -VmapIncrementNestingCtxManagerVariable.create  s(    4 
'
 

 
r2   c                 v   [        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 l        [        R$                  R'                  U5      $ )Nc                  R    [         R                  R                  R                  5       $ r4   )r   r   r   _vmap_decrement_nestingr)   r2   r0   rX   >VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>  r  r2   rl   )r   r   r$   rj   r   SymNodeVariablesym_numas_proxynoder#  r   r   r   _vmap_increment_nestingr6   rC   r   r   r7   r8   )r-   r:   
batch_size
randomnessbatch_size_valuebatch_size_node
vmap_levels          r0   r;   ,VmapIncrementNestingCtxManagerVariable.enter  s    d,,-!%!3!3
j)";";<<)11(11388O)<<>(;;=O224
XX((@@

 	b"WXYY**HH77)	

 ))00<<r2   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  S0 5        [        R                  R                  S 5      $ r   )
rE   rC   r   r   r   r   r:  r   r7   r8   rJ   s      r0   rL   +VmapIncrementNestingCtxManagerVariable.exit  r  r2   r   Nr|   r   r)   r2   r0   r5  r5    s:    ; /1<3U3UV =,7r2   r5  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   )rJ  r   is_grad_enabledr5   r$   )r:   target_valueinitializedr.   r   s        r0   r8   GradModeVariable.create  sK     
'.!1134
 

 NN2001
r2   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   r*   r+   r   r   )r-   r$   r%   rN  r.   r/   s        r0   r+   GradModeVariable.__init__  s2     	 	
'	
JP	
 	d,,-r2   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r4   r5   r$   r   r7   r8   r9   s     r0   r;   GradModeVariable.enter  +    ../))0066r2   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r4   r5   r%   r   r7   r8   rJ   s      r0   rL   GradModeVariable.exit  +    //0))0066r2   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ r4   r5   r%   r*   rl   r-   r:   rK   r.   r/   s       r0   rl   GradModeVariable.call_function  )     	//0w$Rv66r2   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   rl   )r]   r   rL  rC   r   r   _set_grad_enabledr-   r:   r/  r   s       r0   r5   GradModeVariable._call_func(  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r2   c                     gNr   r)   rd   s    r0   rP   GradModeVariable.module_name2      r2   c                     g)Nset_grad_enabledr)   rd   s    r0   rQ   GradModeVariable.fn_name5  s    !r2   r)   Fr|   ro   rz   r:   r   rK   r   r.   r   )r   r   r   r   r   r   r   r   	GRAD_MODEr   r   r8   r+   r;   rL   rl   r5   rP   rQ   r   r   r   s   @r0   rJ  rJ    s    @/1<3I3IJ  ?C.	. .777#7 &7 -	7." "r2   rJ  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%   )rp  r   is_inference_mode_enabledr:   rM  r.   r   s       r0   r8   InferenceModeVariable.create:  s1    #N
+0+J+J+L
PV
 
r2   c                 f   > Uc  [         R                  " 5       n[        TU ]  " SXS.UD6  Xl        g r   )r   rr  r*   r+   r$   r,   s       r0   r+   InferenceModeVariable.__init__A  s>     !"<<>N 	
'	
JP	
 +r2   c                     U R                  5         UR                  R                  S[        R                  R
                  R                  U R                  40 5        g r   )rI   rC   r   r   r   	grad_mode_exit_inference_moder   rJ   s      r0   rL   InferenceModeVariable.exitO  sB    
		NN$$99ZZM		
r2   c                 .  ^^^ 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UUU4S jnU R                  X5        UR                  R                  S[         R                  R                  R                  / U R                  Q70 5      U l        g )NFr   Tc                     > T(       a   [         R                  R                  T5        g [         R                  R                  R                  T 5        g r4   )r   r   rb  r   rx  ry  )ctx disabled_inference_mode_forciblypriors   r0   cleanup_hook1InferenceModeVariable.enter.<locals>.cleanup_hookg  s0    /**51((==cBr2   rl   )r   _dynamoconfig"fake_tensor_disable_inference_moder$   rL  r   rb  r   rx  _enter_inference_moder6   rC   r   r   )r-   r:   r  r}  r~  r  s      @@@r0   r;   InferenceModeVariable.enterX  s    +0(MM  CC""1%
 04,))+EHH&&u-..**@@$BTBTUC	C 	b/YY**NN$$::"d  "	

r2   c                     grf  r)   rd   s    r0   rP   !InferenceModeVariable.module_nameu  rh  r2   c                     g)Ninference_moder)   rd   s    r0   rQ   InferenceModeVariable.fn_namex  s    r2   r   r$   r|   r4   rz   r   r   r   r   r   r8   r+   rL   r;   rP   rQ   r   r   r   s   @r0   rp  rp  9  sG      +
 
+ +

:   r2   rp  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$ )CUDADeviceVariablei|  zrepresents torch.cuda.devicec                 \    [        S[        R                  R                  USS9/S S.UD6nU$ )NT)optionalr   r)   )r  r   cuda_get_device_index)r:   devicer.   r   s       r0   r8   CUDADeviceVariable.create  s?      
 ::777NO
 

 
r2   c                 6   > [         TU ]  " SXS.UD6  Xl        g r   r*   r+   r$   r,   s       r0   r+   CUDADeviceVariable.__init__  s*     	 	
'	
JP	
 +r2   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S5      $ )Nrl   F)
rI   rC   r   r   r  _maybe_exchange_devicer   r   r7   r8   rJ   s      r0   rL   CUDADeviceVariable.exit  sU    
		JJ--ZZM		
 ))0077r2   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        g )Nc                  B   > [         R                  R                  T 5      $ r4   )r   r  r  )prev_idxs   r0   rX   *CUDADeviceVariable.enter.<locals>.<lambda>  s    %***K*KH*Ur2   rl   )r   r  _exchange_devicer$   r6   rC   r   r   )r-   r:   r  s     @r0   r;   CUDADeviceVariable.enter  se    ::..0B0BCb"UVYY**JJ''"d  "	

r2   c                     g)Nz
torch.cudar)   rd   s    r0   rP   CUDADeviceVariable.module_name      r2   c                     g)Nr  r)   rd   s    r0   rQ   CUDADeviceVariable.fn_name  s    r2   r  r|   r4   rz   )r   r   r   r   r   r   r8   r+   rL   r;   rP   rQ   r   r   r   s   @r0   r  r  |  sJ    &  	+
 
	+ 	+8
 r2   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S
S jjrS
S jrS rS rS	rU =r$ )TorchFunctionDisableVariablei  z>represents whether torch function overrides are enabled or notc                 "    [        S/ / S.UD6nU$ r   )r  r   s      r0   r8   #TorchFunctionDisableVariable.create  s(    * 

 

 
r2   c                 >  > [        U5      S:X  d   e[        U5      S:X  d   eSSKJn  UR                  " 5       nX0l        UR
                  R                  U l        UR
                  R                  U l	        [        TU ],  " SXS.UD6  [        U R                  5        g )Nr   r   r   r   r)   )r]   symbolic_convertr   
current_txonly_subclasssymbolic_torch_function_statetorch_function_subclass_enabled'initial_torch_function_subclass_enabledtorch_function_mode_enabled#initial_torch_function_mode_enabledr*   r+   r   r   )r-   r$   r%   r  r.   r   r:   r/   s          r0   r+   %TorchFunctionDisableVariable.__init__  s     =!Q&&&>"a'''<"--/*,,LL 	4 ,,HH 	0 	 	
'	
JP	
 	d,,-r2   c                 t   ^ ^ Uc  U U4S 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 r4   )r  r  r  r  r  r9   s   r0   r?   9TorchFunctionDisableVariable.set_cleanup_hook.<locals>.fn  sA    @@ 00P ))DD 44P *r2   rA   rF   s   `` r0   r6   -TorchFunctionDisableVariable.set_cleanup_hook  s,    : 
		""4<<0r2   c                     [        U5      S:X  d   eSUR                  l        U R                  (       d  SUR                  l        g g )Nr   F)r]   r  r  r  r  )r-   r:   r/  s      r0   r5   'TorchFunctionDisableVariable._call_func  s>    6{aKP((H!!KPB,,H "r2   c                     g)Nztorch._Cr)   rd   s    r0   rP   (TorchFunctionDisableVariable.module_name      r2   c                 (    U R                   (       a  gg)NDisableTorchFunctionSubclassDisableTorchFunction)r  rd   s    r0   rQ   $TorchFunctionDisableVariable.fn_name  s    1%r2   )rB   r  r  r  r|   ro   rz   r4   )r   r   r   r   r   r   r   r   TORCH_FUNCTION_STATEr   r   r8   r+   r6   r5   rP   rQ   r   r   r   s   @r0   r  r    sb    H/1<3T3TU  AE.	. .*1Q& &r2   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  r   $are_deterministic_algorithms_enabledr5   r6   rs  s       r0   r8   &DeterministicAlgorithmsVariable.create  sR    - 
'.!FFHI
 

 	r>*R 
r2   c                 T   > [         TU ]  " SXS.UD6  [        U R                  5        g r   rQ  r,   s       r0   r+   (DeterministicAlgorithmsVariable.__init__  s0     	
'	
JP	
 	d,,-r2   c                 @    [         R                  R                  S 5      $ r4   r   r7   r8   r9   s     r0   r;   %DeterministicAlgorithmsVariable.enter      ))0066r2   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 ra  )r]   rC   r   r   r   _set_deterministic_algorithmsrc  s       r0   r5   *DeterministicAlgorithmsVariable._call_func
  s^    6{aq	II!!!G!G%SU	

 	..u5r2   c                     grf  r)   rd   s    r0   rP   +DeterministicAlgorithmsVariable.module_name  rh  r2   c                     g)Nuse_deterministic_algorithmsr)   rd   s    r0   rQ   'DeterministicAlgorithmsVariable.fn_name  s    -r2   r)   r|   r4   rz   )r   r   r   r   r   r   r   r   DETERMINISTIC_ALGORITHMSr   r   r8   r+   r;   r5   rP   rQ   r   r   r   s   @r0   r  r    sV    `\BB  . .76. .r2   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  r   r   	_autograd/_saved_tensors_hooks_get_disabled_error_messager5   r6   rs  s       r0   r8   (DisabledSavedTensorsHooksVariable.create  s^    / 
'.""RRT

 
 	r>*R 
r2   c                 *   > [         TU ]  " SXS.UD6  g r   r*   r+   r,   s       r0   r+   *DisabledSavedTensorsHooksVariable.__init__+  s      	
'	
JP	
r2   c                 @    [         R                  R                  S 5      $ r4   r  r9   s     r0   r;   'DisabledSavedTensorsHooksVariable.enter0  r  r2   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   rl   r)   )r]   rC   r   r   r   r  _saved_tensors_hooks_disable_saved_tensors_hooks_enablerc  s       r0   r5   ,DisabledSavedTensorsHooksVariable._call_func3  s    6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r2   c                     g)Nztorch.autograd.graphr)   rd   s    r0   rP   -DisabledSavedTensorsHooksVariable.module_nameH  s    %r2   c                     g)Ndisable_saved_tensors_hooksr)   rd   s    r0   rQ   )DisabledSavedTensorsHooksVariable.fn_nameK  s    ,r2   r)   r|   r4   rz   )r   r   r   r   r   r   r8   r+   r;   r5   rP   rQ   r   r   r   s   @r0   r  r    s;    E
 

 

7=*&- -r2   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$ )AutocastModeVariableiO  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	argumentsrj   r   appendr#  r  )funcrK   r.   
bound_argsr$   keyargr   s           r0   r8   AutocastModeVariable.createP  sF   II##,,JJNN##IIMM""
 
 	
 
 &&t,114B6B
!!#GCm#

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

(?(? ?fU **3/#//$$S%;%;%=>$$S) H #=PPP
r2   c                 6   > [         TU ]  " SXS.UD6  Xl        g r   r  r,   s       r0   r+   AutocastModeVariable.__init__p  s(     	
'	
JP	
 +r2   c                     U R                  5         UR                  R                  S[        R                  R
                  U R                  40 5        [        R                  R                  S 5      $ r   )
rI   rC   r   r   r  _exit_autocastr   r   r7   r8   rJ   s      r0   rL   AutocastModeVariable.exitv  sR    
		UYY55

}b	
 ))0066r2   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        g )Nc                  B   > [         R                  R                  T 5      $ r4   )r   r  r  )r}  s   r0   rX   ,AutocastModeVariable.enter.<locals>.<lambda>  s    %))*B*B3*Gr2   rl   )r   r  _enter_autocastr$   r6   rC   r   r   )r-   r:   r}  s     @r0   r;   AutocastModeVariable.enter}  sd    ii''););<b"GHYY**UYY668N$:L:L8NPR

r2   c                     g)Nztorch.amp.autocast_moder)   rd   s    r0   rP    AutocastModeVariable.module_name  s    (r2   c                     g)Nr  r)   rd   s    r0   rQ   AutocastModeVariable.fn_name  r  r2   r  r4   rz   r|   r  r   s   @r0   r  r  O  s8     >+ +7
) r2   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$ )NullContextVariablei  z6
This class represents Python contextlib.nullcontext.
c                 *   > [         TU ]  " SSU0UD6  g Nr$   r)   r  )r-   r$   r.   r/   s      r0   r+   NullContextVariable.__init__  s    ?}??r2   c                 @    [         R                  R                  S 5      $ r4   r  r9   s     r0   r;   NullContextVariable.enter  r  r2   c                 @    [         R                  R                  S 5      $ r4   r  rJ   s      r0   rL   NullContextVariable.exit  r  r2   c                     gN
contextlibr)   rd   s    r0   rP   NullContextVariable.module_name  r  r2   c                     gNnullcontextr)   rd   s    r0   rQ   NullContextVariable.fn_name      r2   r)   r4   rz   r|   )r   r   r   r   r   r+   r;   rL   rP   rQ   r   r   r   s   @r0   r  r    s-    @ @77 r2   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$ )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.
c                 *   > [         TU ]  " SSS 0UD6  g r  r  )r-   r.   r/   s     r0   r+    ProfilerContextVariable.__init__  s    6t6v6r2   c                     U $ r4   r)   r9   s     r0   r;   ProfilerContextVariable.enter  s    r2   c                 @    [         R                  R                  S 5      $ r4   r  rJ   s      r0   rL   ProfilerContextVariable.exit  r  r2   c                     gr  r)   rd   s    r0   rP   #ProfilerContextVariable.module_name  r  r2   c                     gr  r)   rd   s    r0   rQ   ProfilerContextVariable.fn_name  r   r2   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     r0   r_   #ProfilerContextVariable.reconstruct  s*    HIr"..		
r2   r)   rz   r|   )r   r   r   r   r   r+   r;   rL   rP   rQ   r_   r   r   r   s   @r0   r"  r"    s+    77
 
r2   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_clsrl   r4   )r$   r%   r  r)   )	builderr;  r   r  current_streamStreamVariablerC   create_proxyr8  )r:   rM  r.   r;  current_stream_methodr=  s         r0   r8   StreamContextVariable.create  s    . 8!

. 	 +II""%		
 % 
'.*+&&
 	
 	
r2   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   )r*   r+   r  r   
set_stream_set_stream_by_idset_stream_id)r-   r$   r  r%   r.   r/   s        r0   r+   StreamContextVariable.__init__  sT     	
'	
JP	
 24;;?JJ5dkkBTTr2   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   rl   c                  T   > T R                  T R                  S   R                  5      $ )Nr   )rC  r%   r   rd   s   r0   rX   -StreamContextVariable.enter.<locals>.<lambda>  s    $//$:M:Ma:P:V:V*Wr2   )r$   r>  rC   r?  rC  r   rE  	stream_iddevice_indexr  r6   )r-   r:   streams   `  r0   r;   StreamContextVariable.enter  s    a ))+7II""##A&//13	 ''*00FII""""!!6#6#68J8JK	 	**1-334b"WXr2   c                     UR                   R                  SU R                  U R                  S   R	                  5       40 5        U R                  5         g )Nrl   r   )rC   r?  rC  r%   r>  rI   rJ   s      r0   rL   StreamContextVariable.exit  sI    
		OO  #,,.0		
 	r2   )r  rC  rE  r|   r4   rz   )r   r   r   r   r   r8   r+   r;   rL   r   r   r   s   @r0   r8  r8    s1    
 
.U UY* r2   r8  c                   j   ^  \ 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S j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)rj   r   TensorVariableTupleVariablevar_getattrr"  rQ  )r:   tensorsr   versionss       r0   _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      $ r4   )rQ  rY  )rW  r:   s    r0   rX   <PreserveVersionContextVariable.constructor.<locals>.<lambda>  s    :VVGr2   )r   LambdaVariable)r:   s   `r0   constructor*PreserveVersionContextVariable.constructor  s    ''
 	
r2   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  )
setdefaultr*   r+   rW  prev_versionsrj   r   rT  rU  r7   r<  )r-   rW  rc  r.   r/   s       r0   r+   'PreserveVersionContextVariable.__init__  s    /40"6"*dllI$<$<==$22DLL>BDL!;!;Y=V=V W
 
 "+!8!8$:L:L9M!ND
r2   c                     g r4   r)   r9   s     r0   r;   $PreserveVersionContextVariable.enter*  s    r2   c                     SSK Jn  [        R                  " U5      R	                  XR
                  U R                  /0 5      $ )Nr   )_unsafe_set_version_counter)tensor_version_oprh  r   TorchInGraphFunctionVariablerl   rW  rc  )r-   r:   rK   rh  s       r0   rL   #PreserveVersionContextVariable.exit-  s7    C55'

-\\4+=+=>
C	Dr2   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.  r3  rR   s     r0   r_   *PreserveVersionContextVariable.reconstruct4  s-    bIU"..
	
r2   )rc  rW  rz   r|   r}   )r   r   r   r   r   r   rY  r_  r+   r;   rL   r_   r   r   r   s   @r0   rQ  rQ    sM     
A 
A 
 
OD
 
r2   rQ  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$ )&FSDPParamGroupUseTrainingStateVariableiB  c                 P    [        SUU/UR                  R                  /S.UD6nU$ )N)param_group_varr$   r%   r)   )ro  r   _training_state)r:   rq  rM  r.   r   s        r0   r8   -FSDPParamGroupUseTrainingStateVariable.createE  s<    4 
+'.+11AAB
 	
 
r2   c                 `   > [         TU ]  " SX#S.UD6  Xl        [        U R                  5        g r   )r*   r+   rq  r   r   )r-   rq  r$   r%   r.   r/   s        r0   r+   /FSDPParamGroupUseTrainingStateVariable.__init__O  s8     	 	
'	
JP	
  /d,,-r2   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r4   rT  r9   s     r0   r;   ,FSDPParamGroupUseTrainingStateVariable.enterX  rV  r2   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r4   rX  rJ   s      r0   rL   +FSDPParamGroupUseTrainingStateVariable.exit\  rZ  r2   c                 Z   > U R                  XR                  5        [        TU ]  XU5      $ r4   r\  r]  s       r0   rl   4FSDPParamGroupUseTrainingStateVariable.call_function`  r_  r2   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__rr  )	r]   rq  r   rr  call_methodr   r7   r8   EnumVariablerc  s       r0   r5   1FSDPParamGroupUseTrainingStateVariable._call_funci  s    6{aq	%%55>  ,,..556GH**51  :?  &&6 ?r2   c                     g)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr)   rd   s    r0   rP   2FSDPParamGroupUseTrainingStateVariable.module_namex  s    Ur2   c                     g)Nuse_training_stater)   rd   s    r0   rQ   .FSDPParamGroupUseTrainingStateVariable.fn_name{      #r2   )rq  r|   r4   rz   rm  )r   r   r   r   r   r   r   FSDP_TRAINING_STATEr   r   r8   r+   r;   rL   rl   r5   rP   rQ   r   r   r   s   @r0   ro  ro  B  s    /1<3S3ST  >B.	. .777#7 &7 -	7?V$ $r2   ro  c                      ^  \ rS rSrSr\SSS jj5       r  SS\\R                  R                  R                     S\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$ )SDPAKernelVariablei  z)represents torch.nn.attention.sdpa_kernelc                     [        U[        R                  R                  R                  5      (       a  U/n[        SUS US.UD6nU$ )N)r$   r%   set_priorityr)   )rj   r   nn	attention
SDPBackendr  )r:   backendsr  r.   r   s        r0   r8   SDPAKernelVariable.create  sO    h 2 2 = =>> zH  
"%
 	
 
r2   Nr$   r  r{   c                 6   > [         TU ]  " SXS.UD6  X0l        g r   )r*   r+   r  )r-   r$   r%   r  r.   r/   s        r0   r+   SDPAKernelVariable.__init__  s*     	 	
'	
JP	
 )r2   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   )rC   r   r   r  r  _backend_from_stringname)r:   r  backendnodess       r0   _backends_to_nodes%SDPAKernelVariable._backends_to_nodes  se     $
 $ II!!""77	 $ 	 
 
s   AAc                 4  ^  [         R                  R                  R                  T R                  S9T l        T R                  UU 4S j5        [         R                  R                  R                  T R                  T R                  S9  T R                  UT R                  5      nUR                  R                  S[         R                  R                  R                  U[        T R                  5      40 5        [        R                  R                  S 5      $ )N)with_priorityc                  |   > [         R                  R                  R                  T R                  T R
                  S9$ )Nr  )r   r  r  _sdpa_kernelprev_backendsr  rd   s   r0   rX   *SDPAKernelVariable.enter.<locals>.<lambda>  s0    EHH&&33""1B1B 4 r2   r  rl   )r   r  r  _cur_sdpa_kernel_backendsr  r  r6   r  r$   r  rC   r   boolr   r7   r8   )r-   r:   r  s   `  r0   r;   SDPAKernelVariable.enter  s    "XX//II++ J 
 		
 	''T->-> 	( 	
 %%b$*<*<=
		HH++$t(()*		
 ))0066r2   c                 @   U R                  5         U R                  XR                  5      nUR                  R	                  S[
        R                  R                  R                  U[        U R                  5      40 5        [        R                  R                  S 5      $ r   )rI   r  r  rC   r   r   r  r  r  r  r  r   r7   r8   )r-   r:   rK   r  s       r0   rL   SDPAKernelVariable.exit  s{    %%b*<*<=
		HH++$t(()*		
 ))0066r2   c                     g)Nztorch.nn.attentionr)   rd   s    r0   rP   SDPAKernelVariable.module_name  r  r2   c                     g)N_sdpa_kernel_variadicr)   rd   s    r0   rQ   SDPAKernelVariable.fn_name  s    &r2   )r  r  rl  r|   r   )r   r   r   r   r   r   r8   listr   r  r  r  r  r+   r  r;   rL   rP   rQ   r   r   r   s   @r0   r  r    s    3	 	 "	
)EHH..99:
) 	
) 

) 
)  7,	7$
' 'r2   r  c                   ^   ^  \ rS rSrSU 4S jjrS r      S	U 4S jjrS rS
S j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 devicer)   )r?  metar  r   r*   r+   r   r   )r-   r   r   r  r.   r/   s        r0   r+   StreamVariable.__init__  sz    EJJOO!C::???3u<<<||  FKK/ 	
<	
/ 	"6"

r2   c                 "    [         R                  $ r4   )r   Streamrd   s    r0   python_typeStreamVariable.python_type  s    ||r2   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_eventr~  query
target_clsr:   r   record_eventr   )hasattrr   utilsr  r  r<  r;  rC   r?  r   r7   EventVariabler]   rj   r>  r8   NotImplementedr*   r~  )
r-   r:   r  rK   r.   r  r  r;  otherr/   s
            r0   r~  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::r2   c                     U R                   $ r4   r   rd   s    r0   r>  StreamVariable.as_proxy      zzr2   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  r:   rC   install_global_by_idr   append_outputcreate_load_globalr-   rS   prefixr  s       r0   r_   StreamVariable.reconstruct  s^     ;; DKK=)zz  55fjjIg8848HIr2   )r  r   r   rz   rK   r   r.   r   r{   r   r}   )r   r   r   r   r+   r  r~  r>  r_   r   r   r   s   @r0   r>  r>    sB    	+; &	+;
 -+; 
+;ZJ Jr2   r>  c                   R   ^  \ rS rSrSU 4S jjr      SS jrS rS	S j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  r)   )r?  r  r*   r+   r   r   )r-   r   r   r.   r/   s       r0   r+   EventVariable.__init__!  sM    EJJOO!C::???3u<<<"6"

r2   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$ [        U R                  5      R                   S	[        U R                  5      R                   S	U 3n[        S
[        U5      SU S3/ [        R                  QS9  g )Nr   )r  r   r:  )waitrecordr  r~  r  r  .zUnsupported event methodz#Dynamo doesn't support tracing the zC method. We currently support wait, record, synchronize, and query.r.  )r  r  r<  r;  rC   r?  r   r7   r   r   r   r   r   r4  r   r5  )r-   r:   r  rK   r.   r  r;  method_names           r0   r~  EventVariable.call_method(  s    	..44II""t&7v&N --d33W_$$55ii,,!4*;TFTM6*R  

#../qdjj1A1N1N0OqQUPVW  2D	A+ OM N&22r2   c                     U R                   $ r4   r   rd   s    r0   r>  EventVariable.as_proxyM  r  r2   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   r:   rC   r  r   r  r  r  s       r0   r_   EventVariable.reconstructP  sQ     ;;zz  55fjjIg8848HIr2   )r   r   rz   r  r}   )
r   r   r   r   r+   r~  r>  r_   r   r   r   s   @r0   r  r     s=    # &	#
 -# 
#JJ Jr2   r  c                   V   ^  \ rS rSrSrS
U 4S jjrS rSS jrSS jrS r	S r
S	rU =r$ )DynamoConfigPatchVariableiZ  z,represents torch._dynamo.patch_dynamo_configc                 B  > [        UR                  5       5      n[        TU ]  " SU4S S.UD6  0 U l        U H;  u  p4[
        R                  R                  R                  U5      U R                  U'   M=     [        U R                  R                  5       5      4U l        g r   )	r0  r"  r*   r+   r%   r   r  r  __getattr__)r-   r$   r.   r  _r/   s        r0   r+   "DynamoConfigPatchVariable.__init___  s    m1134W'7WPVW #FC',}}';';'G'G'LD$ $$T%8%8%>%>%@ACr2   c                     U R                  U5        U R                  XR                  5        [        R                  R                  S 5      $ r4   )r6   r5   r$   r   r7   r8   r9   s     r0   r;   DynamoConfigPatchVariable.enterg  s9    b!../))0066r2   c                 v    U R                  XR                  5        [        R                  R	                  S 5      $ r4   rX  rJ   s      r0   rL   DynamoConfigPatchVariable.exitm  rZ  r2   c                     [        U5      S:X  d   eUS   nU H.  u  pE[        R                  R                  R	                  XE5        M0     g ri   )r]   r   r  r  r}  )r-   r:   r/  r   r  r^   s         r0   r5   $DynamoConfigPatchVariable._call_funcq  sA    6{aq	HCMM  ,,S6 r2   c                     g)Nztorch._dynamor)   rd   s    r0   rP   %DynamoConfigPatchVariable.module_name|  s    r2   c                     g)Npatch_dynamo_configr)   rd   s    r0   rQ   !DynamoConfigPatchVariable.fn_name  s    $r2   )r%   rz   r|   )r   r   r   r   r   r+   r;   rL   r5   rP   rQ   r   r   r   s   @r0   r  r  Z  s*    6D777% %r2   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S j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 r(   )r*   r+   rj   r!   r   r}  r  )r-   r}  r  r.   r/   s       r0   r+   !WithExitFunctionVariable.__init__  sD     	"6")+IJ
 
 	
 
 r2   c                 N    U(       a   eU R                   R                  " U/UQ76 $ r4   )r}  rL   rk   s       r0   rl   &WithExitFunctionVariable.call_function  s$     zxx}}R'$''r2   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}  rT   r:   rC   partial_convertsysversion_infor  r
   r[   r$   r\   r	   r]   r   r  )r-   rS   r^   s      r0   r_   $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   r   r   r   r   r   r   r!   r   r+   rl   r_   r   r   r   s   @r0   r  r    su    		'	'N
*,JJK
 
(#( &( -	(
 
(A Ar2   r  )Hr   r  r  r$  typingr   r   torch._Cr   torch._guardsr    r   r   bytecode_transformationr	   r
   r   device_interfacer   excr   guardsr   r   r   r   r   baser   	functionsr   r   r   r   r   r   r   r   user_definedr   torch._dynamo.codegenr   torch._dynamo.symbolic_convertr   r!   r   r   r   r   r   r   r  r  r5  rJ  rp  r  r  r  r  r  r  r"  r8  rQ  ro  r  r>  r  r  r  r)   r2   r0   <module>r     s  (  
  '   + 
 8 " 0 2 !	 	 	 4 /D]o ]@(%> (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 @ F/0 /d>&#: >&B).&= ).X1-(? 1-h92 9x1 * 
5  
F=3 =@<
%< <
~:$-D :$zP'0 P'fKJ_ KJ\7JO 7Jt&% 7 &%R-A -Ar2   