
    pi                        d Z ddlZddlZddlZddlmZmZ ddlZddl	m
Z
 ddlmZmZ ddlmZmZmZ ddlmZ dd	lmZ dd
lmZmZ ddlmZmZ ddlmZmZ ddlm Z  ddl!m"Z"m#Z#m$Z$m%Z%m&Z&m'Z'm(Z(m)Z) ddl*m+Z+ erddl,m-Z- ddl.m/Z/  G d de       Z0 G d de+      Z1 G d de1      Z2 G d de0      Z3 G d de0      Z4 G d de0      Z5 G d d e0      Z6 G d! d"e0      Z7 G d# d$e0      Z8 G d% d&e0      Z9 G d' d(e0      Z: G d) d*e0      Z; G d+ d,e0      Z< G d- d.e0      Z= G d/ d0e0      Z> G d1 d2e0      Z? G d3 d4e0      Z@ G d5 d6e0      ZA G d7 d8e0      ZB G d9 d:e0      ZC G d; d<e0      ZD G d= d>e0      ZE G d? d@e0      ZF G dA dBe0      ZG G dC dDe       ZH G dE dFe       ZI G dG dHe0      ZJ G dI dJe0      ZK G dK dLe       ZLy)Ma)  
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)_get_error_on_graph_break_set_error_on_graph_break   )VariableTracker)NestedUserFunctionVariableSkipFunctionVariableUserFunctionVariableUserMethodVariable!WrappedNestedUserFunctionVariableWrappedSkipFunctionVariableWrappedUserFunctionVariableWrappedUserMethodVariable)UserDefinedObjectVariable)	PyCodegenInstructionTranslatorc                        e Zd Zddddhej                  Zdd fdZd ZdddZddZdd	Z	dd
Z
d Zd Z	 	 	 	 	 	 	 	 ddZd Zd Zd Zd Z xZS )ContextWrappingVariablecm_objtarget_valuesinitial_valuesstatec                 @    t        |   di | || _        || _        y N )super__init__r%   r&   selfr%   r&   kwargs	__class__s       e/opt/services/ai/voice_agent/venv/lib/python3.12/site-packages/torch/_dynamo/variables/ctx_manager.pyr,   z ContextWrappingVariable.__init__F   s#    "6"*,    c                     | j                  || j                         | j                  |       t        j                  j                  d       S N)
_call_funcr%   set_cleanup_hookr   ConstantVariablecreater.   txs     r1   enterzContextWrappingVariable.enterK   s;    D../b!))0066r2   c                 p     | fd}| _         j                  j                   j                         y )Nc                  >     j                   j                         y r4   )r5   r&   r9   s   r1   fnz4ContextWrappingVariable.set_cleanup_hook.<locals>.fnS   s    D$7$78r2   
cleanup_fnoutputadd_cleanup_hookcleanupr.   r:   r>   s   `` r1   r6   z(ContextWrappingVariable.set_cleanup_hookP   s,    :9 
		""4<<0r2   c                 `    | j                          t        j                  j                  d       S r4   )cleanup_assertr   r7   r8   r.   r:   argss      r1   exitzContextWrappingVariable.exitY   s%    ))0066r2   c                      |t        |j                  j                  | j                               | j	                                      y r4   )r   r:   import_sourcemodule_namefn_namer.   codegens     r1   reconstruct_typez(ContextWrappingVariable.reconstruct_type]   s2    wzz//0@0@0BCT\\^T	
r2   c                      j                   fd        j                  }|sd}j                  |D cg c]  }j                  |       c}       j                  t	        t        |      d             y c c}w )Nc                  &    j                         S r4   )rP   )rO   r.   s   r1   <lambda>z5ContextWrappingVariable.reconstruct.<locals>.<lambda>c   s    d&;&;G&D r2   r*   F)add_push_nullr%   extend_outputcreate_load_constr	   len)r.   rO   r%   vals   ``  r1   reconstructz#ContextWrappingVariable.reconstructb   sj    DE**MW#w88=WX23}3EuMN Xs   A;c                     t        d      )Nzmodule_name called on baseNotImplementedErrorr.   s    r1   rL   z#ContextWrappingVariable.module_namej   s    !">??r2   c                     t        d      )Nzfn_name called on baser[   r]   s    r1   rM   zContextWrappingVariable.fn_namem   s    !":;;r2   c                 ~   t        |      dk(  sJ t        |d   t        t        t        t
        f      sJ t        |d   t              rt        |d   |       S t        |d   t              rt        |d   |       S t        |d   t              rt        |d   |       S t        |d   t
              rt        |d   |       S y Nr   r   )
rW   
isinstancer   r   r   r   r   r   r   r   r.   r:   rH   r/   s       r1   call_functionz%ContextWrappingVariable.call_functionp   s     4yA~~G*$"$	
 	
 
 d1g9:4T!WdCCd1g34.tAw==d1g12,T!Wd;;d1g34.tAw== 5r2   c                      yNTr*   r]   s    r1   supports_graph_breaksz-ContextWrappingVariable.supports_graph_breaks       r2   c                      yre   r*   r]   s    r1   exit_on_graph_breakz+ContextWrappingVariable.exit_on_graph_break   rg   r2   c                 L    | j                   | j                          d | _         y y r4   )r@   r]   s    r1   rC   zContextWrappingVariable.cleanup   s"    ??&OO"DO 'r2   c                 J    | j                   sJ d       | j                          y )Nzmultiple exits?)r@   rC   r]   s    r1   rF   z&ContextWrappingVariable.cleanup_assert   s    1 11r2   r4   returnNr:   r!   rO   r   r:   r!   rH   list[VariableTracker]r/   dict[str, VariableTracker]rm   r   )__name__
__module____qualname__r   _nonvar_fieldsr,   r;   r6   rI   rP   rY   rL   rM   rc   rf   ri   rC   rF   __classcell__r0   s   @r1   r#   r#   =   s    	
 
	'	'N-
7
17

O@<>#> &> -	>
 
>:#
r2   r#   c                   F     e Zd Zd fdZd Zd Zd Zd	dZd Zd Z	 xZ
S )
GenericContextWrappingVariablec                 T    |J t        |   d||j                  d| || _        y )N)value
value_typer*   )r+   r,   r0   r$   )r.   r$   r/   r0   s      r1   r,   z'GenericContextWrappingVariable.__init__   s@    !!! 	
''	
 	

 r2   c                 .    | j                   j                  S r4   )r$   rt   r]   s    r1   rL   z*GenericContextWrappingVariable.module_name   s    {{%%%r2   c                 @    t        | j                        j                  S r4   )typer$   rs   r]   s    r1   rM   z&GenericContextWrappingVariable.fn_name   s    DKK )))r2   c                     | j                   d nt        | j                   d      }t        j                  | j                  j
                  j                  | |      j                  |g i       S )N	__enter__source)r   r   r   r   r$   r   __func__rc   )r.   r:   r   s      r1   r;   z$GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r2   c                    | j                   d nt        | j                   d      }t        j                  | j                  j
                  j                  | |      j                  ||i       }|j                  j                          |S )N__exit__r   )
r   r   r   r   r$   r   r   rc   active_generic_context_managerspop)r.   r:   rH   r   xs        r1   rI   z#GenericContextWrappingVariable.exit   sr    ,*T[[*2U((KK  ))
 -D"
%	 	

 	**..0r2   c                      yNFr*   r]   s    r1   rf   z4GenericContextWrappingVariable.supports_graph_breaks   s    r2   c                      yre   r*   r]   s    r1   ri   z2GenericContextWrappingVariable.exit_on_graph_break   rg   r2   rl   rn   )rs   rt   ru   r,   rL   rM   r;   rI   rf   ri   rw   rx   s   @r1   rz   rz      s&    &*$r2   rz   c                   (    e Zd Zd ZddZddZd Zy)$RepararametrizeModuleContextVariablec                      || _         || _        y r4   )cm_vtmod)r.   ctx_manager_vtr   s      r1   r,   z-RepararametrizeModuleContextVariable.__init__   s    #
r2   c                    | j                   j                  |d      j                         | _        | j                   j                  |d      j                         | _        |j
                  j                  j                  | j                         |j
                  j                  j                  | j                         | j                  j                  |      S )N_parameters_buffers)
r   var_getattrrealizeold_parameters_varold_buffer_varrA   side_effectsignore_mutations_onr   r;   r9   s     r1   r;   z*RepararametrizeModuleContextVariable.enter   s     #'(("6"6r="I"Q"Q"S"hh222zBJJL
		2243J3JK
		2243F3FGzz##r2   c                      | j                   j                  |g| }|j                  j                  j	                  | j
                         |j                  j                  j	                  | j                         |S r4   )r   rI   rA   r   stop_ignoring_mutations_onr   r   )r.   r:   rH   r   s       r1   rI   z)RepararametrizeModuleContextVariable.exit   s\    DJJOOB&&
		99$:M:MN
		99$:Q:QRr2   c                 .    t        | j                  |      S r4   )getattrr   )r.   names     r1   __getattr__z0RepararametrizeModuleContextVariable.__getattr__   s    tzz4((r2   Nrn   )rs   rt   ru   r,   r;   rI   r   r*   r2   r1   r   r      s    
$)r2   r   c                   0    e Zd ZdZedd       Zd ZddZy))GradInplaceRequiresGradCtxManagerVariablez#represents torch grad requires gradc                     t        d|d d|S Nr%   r&   r*   )r   r:   r%   r/   s      r1   r8   z0GradInplaceRequiresGradCtxManagerVariable.create   s$    8 
'
 
 	
r2   c                      j                   \  }t        j                  j                  j	                          _        t        j                  j                  j                  |        j                  | fd       |j                  j                  dt        j                  j                  j                  |fi        _
        t        j                  j                  d       S )Nc                  j    t         j                  j                  j                   j                        S r4   )torch_C
_functorch!set_inplace_requires_grad_allowed
prev_stater]   s   r1   rS   zAGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>   s#    EHH''II r2   rc   )r%   r   r   r   !get_inplace_requires_grad_allowedr   r   r6   rA   create_nodeproxyr   r7   r8   )r.   r:   enableds   `  r1   r;   z/GradInplaceRequiresGradCtxManagerVariable.enter   s    &&	((--OOQ==gF	
 YY**HHAAJ	

 ))0066r2   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S Nrc   )rC   rA   r   r   r   r   r   r   r   r7   r8   rG   s      r1   rI   z.GradInplaceRequiresGradCtxManagerVariable.exit  sY    
		HHAA__		
 ))0066r2   Nrn   rs   rt   ru   __doc__staticmethodr8   r;   rI   r*   r2   r1   r   r      s!    -
 
7$7r2   r   c                   0    e Zd ZdZedd       Zd ZddZy)0TemporarilyPopInterpreterStackCtxManagerVariablezJrepresents torch._functorch.pyfunction.temporarily_pop_interpreter_stack()c                     t        d|d d|S r   )r   r   s      r1   r8   z7TemporarilyPopInterpreterStackCtxManagerVariable.create  s$    ? 
'
 
 	
r2   c                 P    t         j                  j                  j                          _         j                  | fd       |j                  j                  dt         j                  j                  j                  di        _        t        j                  j                  d       S )Nc                  j    t         j                  j                  j                   j                        S r4   )r   r   r   push_dynamic_layer_stacksavedr]   s   r1   rS   zHTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>  s     EHH''@@L r2   rc   r*   )r   r   r   pop_dynamic_layer_stackr   r6   rA   r   r   r   r7   r8   r9   s   ` r1   r;   z6TemporarilyPopInterpreterStackCtxManagerVariable.enter  s~    XX((@@B
L	
 YY**HH77	

 ))0066r2   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S r   )rC   rA   r   r   r   r   r   r   r   r7   r8   rG   s      r1   rI   z5TemporarilyPopInterpreterStackCtxManagerVariable.exit%  sX    
		HH88ZZM		
 ))0066r2   Nrn   r   r*   r2   r1   r   r     s!    T
 
77r2   r   c                   `    e Zd ZdZ e e       ej                        Ze	dd       Z
d ZddZy)%JvpIncrementNestingCtxManagerVariablez5represents torch.func.jvp increment/decrement nestingc                 "    t        dd d d|}|S r   )r   r:   r/   vars      r1   r8   z,JvpIncrementNestingCtxManagerVariable.create:  s(    3 

 

 
r2   c                 j   t        | j                         t        j                  j                  j                         }| j                  |d        |j                  j                  dt        j                  j                  j                  di       | _        t        j                  j                  |      S )Nc                  R    t         j                  j                  j                         S r4   )r   r   eager_transformsexit_jvp_nestingr*   r2   r1   rS   z=JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>G  s    ((99JJL r2   rc   r*   )r   _guards_singletonr   r   r   enter_jvp_nestingr6   rA   r   r   _jvp_increment_nestingr   r   r7   r8   )r.   r:   	jvp_levels      r1   r;   z+JvpIncrementNestingCtxManagerVariable.enterC  s    d,,-$$55GGI	L	
 YY**HH66	

 ))00;;r2   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S Nrc   r*   )
rC   rA   r   r   r   r   _jvp_decrement_nestingr   r7   r8   rG   s      r1   rI   z*JvpIncrementNestingCtxManagerVariable.exitQ  sO    
		UXX00GGR	
 ))0066r2   Nrn   rs   rt   ru   r   r   r   r   FUNCTORCH_STACK_MATCHr   r   r8   r;   rI   r*   r2   r1   r   r   0  s:    ? /1<3U3UV <7r2   r   c                   0    e Zd ZdZedd       Zd ZddZy)SetFwdGradEnabledContextManagerzWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradc                     t        d|d d|S r   )r   r   s      r1   r8   z&SetFwdGradEnabledContextManager.create\  s$    . 
'
 
 	
r2   c                      j                   \  }t        j                  j                          _        t        j                  j                  |        j                  | fd       |j                  j                  dt        j                  j
                  |fi        _	        t        j                  j                  d       S )Nc                  V    t         j                  j                   j                        S r4   )r   r   _set_fwd_grad_enabledr   r]   s   r1   rS   z7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>j  s    EHH224??C r2   rc   )r%   r   r   _is_fwd_grad_enabledr   r   r6   rA   r   r   r   r7   r8   )r.   r:   modes   `  r1   r;   z%SetFwdGradEnabledContextManager.enterd  s    ##((779&&t,C	
 YY**HH**G	

 ))0066r2   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rC   rA   r   r   r   r   r   r   r7   r8   rG   s      r1   rI   z$SetFwdGradEnabledContextManager.exitt  sS    
		HH**__		
 ))0066r2   Nrn   r   r*   r2   r1   r   r   Y  s!    a
 
7 7r2   r   c                   `    e Zd ZdZ e e       ej                        Ze	dd       Z
d ZddZy)DualLevelContextManagerz;Represents torch.autograd.forward_ad.dual_level ctx managerc                     t        dd d d|S r   )r   )r:   r/   s     r1   r8   zDualLevelContextManager.create  s$    & 

 
 	
r2   c                 z    t         j                         t        j                  j                  j                          _         j                  | fd       |j                  j                  dt        j                  j                  di        _        t        j                  j                   j                        S )Nc                  l    t         j                  j                  j                   j                        S )N)level)r   autograd
forward_adexit_dual_level	new_levelr]   s   r1   rS   z/DualLevelContextManager.enter.<locals>.<lambda>  s#    11AAAW r2   rc   r*   )r   r   r   r   r   enter_dual_levelr   r6   rA   r   r   _enter_dual_levelr   r   r7   r8   r9   s   ` r1   r;   zDualLevelContextManager.enter  s    d,,-22CCEW	
 YY**HH&&	

 ))00@@r2   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rC   rA   r   r   r   _exit_dual_levelr   r   r7   r8   rG   s      r1   rI   zDualLevelContextManager.exit  sS    
		HH%%^^		
 ))0066r2   Nrn   )rs   rt   ru   r   r   r   r   
DUAL_LEVELr   r   r8   r;   rI   r*   r2   r1   r   r     s9    E/1<3J3JK
 
A7r2   r   c                   `    e Zd ZdZ e e       ej                        Ze	dd       Z
d ZddZy)&GradIncrementNestingCtxManagerVariablez6represents torch.func.grad increment/decrement nestingc                 "    t        dd d d|}|S r   )r   r   s      r1   r8   z-GradIncrementNestingCtxManagerVariable.create  s(    4 

 

 
r2   c                 j   t        | j                         t        j                  j                  j                         }| j                  |d        |j                  j                  dt        j                  j                  j
                  di       | _	        t        j                  j                  |      S )Nc                  R    t         j                  j                  j                         S r4   )r   r   r   _grad_decrement_nestingr*   r2   r1   rS   z>GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*W r2   rc   r*   )r   r   r   r   r   _grad_increment_nestingr6   rA   r   r   r   r7   r8   )r.   r:   
grad_levels      r1   r;   z,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WXYY**HH77	

 ))00<<r2   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S r   )
rC   rA   r   r   r   r   r   r   r7   r8   rG   s      r1   rI   z+GradIncrementNestingCtxManagerVariable.exit  sO    
		UXX00HH"b	
 ))0066r2   Nrn   r   r*   r2   r1   r   r     s:    @ /1<3U3UV 
=7r2   r   c                   B     e Zd ZdZedd       Zd fdZd Zd Z xZ	S )CatchWarningsCtxManagerVariablez'Delay a call to warnings.catch_warningsc                     t        |d d       S )N)catch_warnings_argsr%   r&   )r   )r:   r   s     r1   r8   z&CatchWarningsCtxManagerVariable.create  s    . 3
 	
r2   c                 `    t        |t              sJ |       t        |   di | || _        y r)   )ra   dictr+   r,   r   )r.   r   r/   r0   s      r1   r,   z(CatchWarningsCtxManagerVariable.__init__  s2    -t4I6II4"6"#6 r2   c                 4   | j                   j                         D ci c]  \  }}||j                          }}}t        j                  di || j                  |fd       t        j                  j                  j                               S c c}}w )Nc                  *     j                  d d d       S r4   )r   )ctx_vals   r1   rS   z7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*L r2   r*   )
r   itemsas_python_constantwarningscatch_warningsr6   r   r7   r8   r   )r.   r:   kvr/   r   s        @r1   r;   z%CatchWarningsCtxManagerVariable.enter  s    262J2J2P2P2R
*.!QAq##%%
 
 ))3F3b"LM))001B1B1DEE
s   Bc                    j                  fd       j                  | j                  j                                t	        | j                  j                               }j                  j                  t        |      |d             y )Nc                  (     j                  dd      S )Nr  r  )load_import_from)cgs   r1   rS   z=CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    !4!4ZAQ!R r2   F)	rT   foreachr   valuestuplekeysrU   create_call_function_kwrW   )r.   r  r  s    ` r1   rY   z+CatchWarningsCtxManagerVariable.reconstruct  sg    
RS


4++2245T--2245
33CItUKLr2   rn   rl   )
rs   rt   ru   r   r   r8   r,   r;   rY   rw   rx   s   @r1   r   r     s)    1
 
7
FMr2   r   c                   `    e Zd ZdZ e e       ej                        Ze	dd       Z
d ZddZy)&VmapIncrementNestingCtxManagerVariablez1represents torch VMap increment/decrement nestingc                 "    t        d|d d|}|S r   )r  )r:   r%   r/   r   s       r1   r8   z-VmapIncrementNestingCtxManagerVariable.create  s(    4 
'
 

 
r2   c                 r   t        | j                         | j                  \  }}t        |t        j
                        r'|j                  }|j                         j                  }n |j                         }|j                         }|j                         }t        j                  j                  j                  ||      }| j                  |d        |j                  j!                  dt        j                  j"                  j                  ||fi       | _        t        j&                  j)                  |      S )Nc                  R    t         j                  j                  j                         S r4   )r   r   r   _vmap_decrement_nestingr*   r2   r1   rS   z>VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>  r   r2   rc   )r   r   r%   ra   r   SymNodeVariablesym_numas_proxynoder   r   r   r   _vmap_increment_nestingr6   rA   r   predispatchr   r7   r8   )r.   r:   
batch_size
randomnessbatch_size_valuebatch_size_node
vmap_levels          r1   r;   z,VmapIncrementNestingCtxManagerVariable.enter  s    d,,-!%!3!3
Jj)";";<)11(11388O)<<>(;;=O224
XX((@@j

 	b"WXYY**((@@j)	

 ))00<<r2   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S r   )
rC   rA   r   r   r   r  r  r   r7   r8   rG   s      r1   rI   z+VmapIncrementNestingCtxManagerVariable.exit  sT    
		((@@		
 ))0066r2   Nrn   r   r*   r2   r1   r  r    s:    ; /1<3U3UV =,7r2   r  c                        e Zd ZdZ e e       ej                        Ze	d
dd       Z
	 d	 d fdZd ZddZ	 	 	 	 	 	 d fdZddZd Zd	 Z xZS )GradModeVariablez6represents torch.{no_grad,enable_grad,set_grad_mode}()c                     t        d|gt        j                         gd|}|r|j                  | |j                         |S r   )r!  r   is_grad_enabledr5   r%   )r:   target_valueinitializedr/   r   s        r1   r8   zGradModeVariable.create$  sM     
'.!1134
 

 NN2s001
r2   c                 T    t        |   d||d| t        | j                         y r   r+   r,   r   r   )r.   r%   r&   r%  r/   r0   s        r1   r,   zGradModeVariable.__init__/  s4     	 	
'	
JP	
 	d,,-r2   c                 x    | j                  || j                         t        j                  j	                  d       S r4   r5   r%   r   r7   r8   r9   s     r1   r;   zGradModeVariable.enter7  -    D../))0066r2   c                 x    | j                  || j                         t        j                  j	                  d       S r4   r5   r&   r   r7   r8   rG   s      r1   rI   zGradModeVariable.exit;  -    D//0))0066r2   c                 ^    | j                  || j                         t        |   |||      S r4   r5   r&   r+   rc   r.   r:   rH   r/   r0   s       r1   rc   zGradModeVariable.call_function?  -     	D//0w$Rv66r2   c                 
   t        |      dk(  sJ |d   }t        j                         |k7  rW|j                  j	                  dt        j
                  j                  |fi        t        j
                  j                  |       y y Nr   r   rc   )rW   r   r#  rA   r   r   _set_grad_enabledr.   r:   r
  r|   s       r1   r5   zGradModeVariable._call_funcH  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r2   c                      yNr   r*   r]   s    r1   rL   zGradModeVariable.module_nameR      r2   c                      y)Nset_grad_enabledr*   r]   s    r1   rM   zGradModeVariable.fn_nameU  s    !r2   Frn   re   rl   r:   r!   rH   rq   r/   rr   )rs   rt   ru   r   r   r   r   	GRAD_MODEr   r   r8   r,   r;   rI   rc   r5   rL   rM   rw   rx   s   @r1   r!  r!    s|    @/1<3I3IJ  ?C.	.777#7 &7 -	7."r2   r!  c                   R     e Zd Zedd       Z	 d	 d	 fdZddZd Zd Zd Z	 xZ
S )
InferenceModeVariablec                 H    t        |gfdt        j                         i|}|S )Nr&   )r?  r   is_inference_mode_enabledr:   r$  r/   r   s       r1   r8   zInferenceModeVariable.createZ  s1    #N
+0+J+J+L
PV
 
r2   c                 d    |t        j                         }t        |   d||d| || _        y r   )r   rA  r+   r,   r%   r-   s       r1   r,   zInferenceModeVariable.__init__a  sB     !"<<>N 	
'	
JP	
 +r2   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        y r   )rF   rA   r   r   r   	grad_mode_exit_inference_moder   rG   s      r1   rI   zInferenceModeVariable.exito  sB    
		NN$$99ZZM		
r2   c                    dt         j                  j                  j                  rE| j                  d   r6dt        j
                         t         j                  j                  d       n0t        j                  j                  j                  | j                   fd}| j                  ||       |j                  j                  dt         j                  j                  j                  g | j                  i       | _        y )NFr   Tc                      r t         j                  j                         y t         j                  j                  j                          y r4   )r   r   r4  r   rE  rF  )ctx disabled_inference_mode_forciblypriors   r1   cleanup_hookz1InferenceModeVariable.enter.<locals>.cleanup_hook  s0    /**51((==cBr2   rc   )r   _dynamoconfig"fake_tensor_disable_inference_moder%   r#  r   r4  r   rE  _enter_inference_moder6   rA   r   r   )r.   r:   rL  rI  rJ  rK  s      @@@r1   r;   zInferenceModeVariable.enterx  s    +0(MM  CC""1%
 04,))+EHH&&u-..**@@$BTBTUC	C 	b,/YY**NN$$::"d  "	

r2   c                      yr7  r*   r]   s    r1   rL   z!InferenceModeVariable.module_name  r8  r2   c                      y)Ninference_moder*   r]   s    r1   rM   zInferenceModeVariable.fn_name  s    r2   rn   r4   rl   rs   rt   ru   r   r8   r,   rI   r;   rL   rM   rw   rx   s   @r1   r?  r?  Y  s=      +
 
+

: r2   r?  c                   V     e Zd ZdZedd       Z	 d		 d
 fdZddZd Zd Z	d Z
 xZS )CUDADeviceVariablezrepresents torch.cuda.devicec                 b    t        dt        j                  j                  |d      gd d|}|S )NT)optionalr   r*   )rV  r   cuda_get_device_index)r:   devicer/   r   s       r1   r8   zCUDADeviceVariable.create  s?      
 ::777NO
 

 
r2   c                 8    t        |   d||d| || _        y r   r+   r,   r%   r-   s       r1   r,   zCUDADeviceVariable.__init__  s.     	 	
'	
JP	
 +r2   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d      S )Nrc   F)
rF   rA   r   r   rY  _maybe_exchange_devicer   r   r7   r8   rG   s      r1   rI   zCUDADeviceVariable.exit  sU    
		JJ--ZZM		
 ))0077r2   c                    t        j                  j                  | j                   | j	                  |fd       |j
                  j                  dt         j                  j                  g | j                  i       | _        y )Nc                  B    t         j                  j                         S r4   )r   rY  r_  )prev_idxs   r1   rS   z*CUDADeviceVariable.enter.<locals>.<lambda>  s    %***K*KH*U r2   rc   )r   rY  _exchange_devicer%   r6   rA   r   r   )r.   r:   rb  s     @r1   r;   zCUDADeviceVariable.enter  se    ::..0B0BCb"UVYY**JJ''"d  "	

r2   c                      y)Nz
torch.cudar*   r]   s    r1   rL   zCUDADeviceVariable.module_name      r2   c                      y)Nr[  r*   r]   s    r1   rM   zCUDADeviceVariable.fn_name  s    r2   rn   r4   rl   )rs   rt   ru   r   r   r8   r,   rI   r;   rL   rM   rw   rx   s   @r1   rV  rV    s@    &  	+
 
	+8
r2   rV  c                        e Zd ZdZ e e       ej                        Ze	dd       Z
	 d		 d
 fdZdddZddZd Zd Z xZS )TorchFunctionDisableVariablez>represents whether torch function overrides are enabled or notc                 "    t        dg g d|}|S r   )rh  r   s      r1   r8   z#TorchFunctionDisableVariable.create  s(    * 

 

 
r2   c                 <   t        |      dk(  sJ t        |      dk(  sJ ddlm}  |j                         }|| _        |j
                  j                  | _        |j
                  j                  | _	        t        | ,  d||d| t        | j                         y )Nr   r   r    r   r*   )rW   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&   rm  r/   r!   r:   r0   s          r1   r,   z%TorchFunctionDisableVariable.__init__  s     =!Q&&&>"a'''<-"--/*,,LL 	4 ,,HH 	0 	 	
'	
JP	
 	d,,-r2   c                 p     | fd}| _         j                  j                   j                         y )Nc                       j                   j                  _         j                  s j                   j                  _        y y r4   )rp  rn  ro  rm  rq  r9   s   r1   r>   z9TorchFunctionDisableVariable.set_cleanup_hook.<locals>.fn  sA    @@ 00P ))DD 44P *r2   r?   rD   s   `` r1   r6   z-TorchFunctionDisableVariable.set_cleanup_hook  s,    : 
		""4<<0r2   c                     t        |      dk(  sJ d|j                  _        | j                  sd|j                  _        y y )Nr   F)rW   rn  ro  rm  rq  r.   r:   r
  s      r1   r5   z'TorchFunctionDisableVariable._call_func   s>    6{aKP((H!!KPB,,H "r2   c                      y)Nztorch._Cr*   r]   s    r1   rL   z(TorchFunctionDisableVariable.module_name      r2   c                     | j                   ryy)NDisableTorchFunctionSubclassDisableTorchFunction)rm  r]   s    r1   rM   z$TorchFunctionDisableVariable.fn_name	  s    1%r2   rn   re   rl   r4   )rs   rt   ru   r   r   r   r   TORCH_FUNCTION_STATEr   r   r8   r,   r6   r5   rL   rM   rw   rx   s   @r1   rh  rh    sX    H/1<3T3TU  AE.	.*1Q&r2   rh  c                        e Zd ZdZ e e       ej                        Ze	dd       Z
d	d
 fdZd ZddZd Zd Z xZS )DeterministicAlgorithmsVariablezVrepresents torch.{are_deterministic_algorithms_enabled,use_deterministic_algorithms}()c                     t        d|gt        j                         gd|}|j                  | |g       |j	                  |        |S r   )r~  r   $are_deterministic_algorithms_enabledr5   r6   rB  s       r1   r8   z&DeterministicAlgorithmsVariable.create  sT    - 
'.!FFHI
 

 	rL>*R 
r2   c                 T    t        |   d||d| t        | j                         y r   r'  r-   s       r1   r,   z(DeterministicAlgorithmsVariable.__init__!  s2     	
'	
JP	
 	d,,-r2   c                 @    t         j                  j                  d       S r4   r   r7   r8   r9   s     r1   r;   z%DeterministicAlgorithmsVariable.enter'      ))0066r2   c                     t        |      dk(  sJ |d   }|j                  j                  dt        j                  j
                  |fi        t        j                  j                  |       y r3  )rW   rA   r   r   r   _set_deterministic_algorithmsr5  s       r1   r5   z*DeterministicAlgorithmsVariable._call_func*  sX    6{aq	
		UXXCCeXr	
 	..u5r2   c                      yr7  r*   r]   s    r1   rL   z+DeterministicAlgorithmsVariable.module_name2  r8  r2   c                      y)Nuse_deterministic_algorithmsr*   r]   s    r1   rM   z'DeterministicAlgorithmsVariable.fn_name5  s    -r2   rn   r4   rl   )rs   rt   ru   r   r   r   r   DETERMINISTIC_ALGORITHMSr   r   r8   r,   r;   r5   rL   rM   rw   rx   s   @r1   r~  r~    sL    `\BB  .76.r2   r~  c                   R     e Zd ZdZedd       Zd	d
 fdZd ZddZd Z	d Z
 xZS )!DisabledSavedTensorsHooksVariablez;represents torch.autograd.graph.disable_saved_tensors_hook.c                     t        d|gt        j                  j                  j	                         gd|}|j                  | |g       |j                  |        |S r   )r  r   r   	_autograd/_saved_tensors_hooks_get_disabled_error_messager5   r6   rB  s       r1   r8   z(DisabledSavedTensorsHooksVariable.create<  s`    / 
'.""RRT

 
 	rL>*R 
r2   c                 *    t        |   d||d| y r   r+   r,   r-   s       r1   r,   z*DisabledSavedTensorsHooksVariable.__init__I  s"     	
'	
JP	
r2   c                 @    t         j                  j                  d       S r4   r  r9   s     r1   r;   z'DisabledSavedTensorsHooksVariable.enterN  r  r2   c                    t        |      dk(  sJ |d   }|k|j                  j                  dt        j                  j
                  j                  |fi        t        j                  j
                  j                  |       y |j                  j                  dt        j                  j
                  j                  di        t        j                  j
                  j                          y )Nr   r   rc   r*   )rW   rA   r   r   r   r  _saved_tensors_hooks_disable_saved_tensors_hooks_enabler5  s       r1   r5   z,DisabledSavedTensorsHooksVariable._call_funcQ  s    6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r2   c                      y)Nztorch.autograd.graphr*   r]   s    r1   rL   z-DisabledSavedTensorsHooksVariable.module_namef      %r2   c                      y)Ndisable_saved_tensors_hooksr*   r]   s    r1   rM   z)DisabledSavedTensorsHooksVariable.fn_namei  s    ,r2   rn   r4   rl   )rs   rt   ru   r   r   r8   r,   r;   r5   rL   rM   rw   rx   s   @r1   r  r  9  s1    E
 


7=*&-r2   r  c                   L     e Zd Zed        Zdd fdZd	dZd Zd Zd Z	 xZ
S )
AutocastModeVariablec                 0   | t         j                  j                  j                  t         j                  j                  j                  t         j
                  j                  j                  fv sJ  t        j                  |       j                  |i |}|j                          g }|j                          dD ]  }|dk(  ru| t         j                  j                  j                  t         j
                  j                  j                  fv r+| t         j                  j                  j                  u rdnd}n|j                  |   }t        |t              r |j                  |j                                |j                  |        t!        |fdd i|}|S )N)device_typedtyper   cache_enabledr  rY  cpur&   )r   ampautocast_modeautocastrY  r  inspect	signaturebindapply_defaultsclear	argumentsra   r   appendr   r  )funcrH   r/   
bound_argsr%   keyargr   s           r1   r8   zAutocastModeVariable.createn  sI   II##,,JJNN##IIMM""
 
 	
 
 2W&&t,114B6B
!!#G 	*Cm#

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

(?(? ?fU **3/#/$$S%;%;%=>$$S)	* #=PPP
r2   c                 8    t        |   d||d| || _        y r   r]  r-   s       r1   r,   zAutocastModeVariable.__init__  s,     	
'	
JP	
 +r2   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rF   rA   r   r   r  _exit_autocastr   r   r7   r8   rG   s      r1   rI   zAutocastModeVariable.exit  sR    
		UYY55

}b	
 ))0066r2   c                    t        j                  j                  | j                   | j	                  |fd       |j
                  j                  dt         j                  j                  g | j                  i       | _        y )Nc                  B    t         j                  j                         S r4   )r   r  r  )rI  s   r1   rS   z,AutocastModeVariable.enter.<locals>.<lambda>  s    %))*B*B3*G r2   rc   )r   r  _enter_autocastr%   r6   rA   r   r   )r.   r:   rI  s     @r1   r;   zAutocastModeVariable.enter  sd    ii''););<b"GHYY**UYY668N$:L:L8NPR

r2   c                      y)Nztorch.amp.autocast_moder*   r]   s    r1   rL   z AutocastModeVariable.module_name  s    (r2   c                      y)Nr  r*   r]   s    r1   rM   zAutocastModeVariable.fn_name  rx  r2   r4   rl   rn   rT  rx   s   @r1   r  r  m  s.     >+7
)r2   r  c                   @     e Zd ZdZdd fdZd Zd	dZd Zd Z xZ	S )
NullContextVariablez>
    This class represents Python contextlib.nullcontext.
    c                 (    t        |   dd|i| y Nr%   r*   r  )r.   r%   r/   r0   s      r1   r,   zNullContextVariable.__init__  s    ?}??r2   c                 t    t         j                  j                  d       }| j                  r| j                  S |S r4   )r   r7   r8   r%   )r.   r:   nones      r1   r;   zNullContextVariable.enter  s1    ))006%)%7%7t!!ATAr2   c                 @    t         j                  j                  d       S r4   r  rG   s      r1   rI   zNullContextVariable.exit  r  r2   c                      yN
contextlibr*   r]   s    r1   rL   zNullContextVariable.module_name  re  r2   c                      yNnullcontextr*   r]   s    r1   rM   zNullContextVariable.fn_name      r2   r4   rl   rn   )
rs   rt   ru   r   r,   r;   rI   rL   rM   rw   rx   s   @r1   r  r    s#    @B7r2   r  c                   D     e Zd ZdZd fdZd Zd	dZd Zd Zd Z	 xZ
S )
ProfilerContextVariablea~  
    This class represents a set of torch profiler context objects, where Dynamo
    ignores all the side-effects in the __init__, __enter__ and __exit__ methods
    by treating the object mostly as a `contextlib.nullcontext`, except for edge
    cases like the `__enter__` method which returns the object itself rather
    than `None`, per implementation of the torch objects.
    c                 (    t        |   ddd i| y r  r  )r.   r/   r0   s     r1   r,   z ProfilerContextVariable.__init__  s    6t6v6r2   c                     | S r4   r*   r9   s     r1   r;   zProfilerContextVariable.enter  s    r2   c                 @    t         j                  j                  d       S r4   r  rG   s      r1   rI   zProfilerContextVariable.exit  r  r2   c                      yr  r*   r]   s    r1   rL   z#ProfilerContextVariable.module_name  re  r2   c                      yr  r*   r]   s    r1   rM   zProfilerContextVariable.fn_name  r  r2   c                 T    t        dt        |       dg t        j                         y )Nz2torch.profiler object escaped from compiled regionzXDynamo doesn't support compiling a region that returns a torch.profiler context manager.gb_typecontextexplanationhintsr   strr   SUPPORTABLE)r.   r  s     r1   rY   z#ProfilerContextVariable.reconstruct  s*    HIr"..		
r2   rl   rn   )rs   rt   ru   r   r,   r;   rI   rL   rM   rY   rw   rx   s   @r1   r  r    s&    77
r2   r  c                   B     e Zd Zedd       Zdd fdZd ZddZ xZS )StreamContextVariablec           
          ddl m} t        |j                        j                  } |t
        | | j                  j                  d|di             }t        d|g|g|j                  d|S )Nr   wrap_fx_proxy_clsrc   r4   )r%   r&   r[  r*   )	builderr  r   r[  current_streamStreamVariablerA   create_proxyr  )r:   r$  r/   r  current_stream_methodr  s         r1   r8   zStreamContextVariable.create  s    . 8!

. 	 +II""%		
 % 
'.*+&&
 	
 	
r2   c                     t        |   d||d| || _        t        | j                        j                  | _        t        | j                        j
                  | _        y r   )r+   r,   r[  r   
set_stream_set_stream_by_idset_stream_id)r.   r%   r[  r&   r/   r0   s        r1   r,   zStreamContextVariable.__init__  sX     	
'	
JP	
 24;;?JJ5dkkBTTr2   c                      j                   d   j                         E|j                  j                  d j                   j                   d   j                         fi        nb j                   d   j
                  }|j                  j                  d j                  |j                  |j                  |j                  fi         j	                   j                   d   j
                          j                  | fd       y )Nr   rc   c                  T     j                   j                  d   j                        S )Nr   )r  r&   r|   r]   s   r1   rS   z-StreamContextVariable.enter.<locals>.<lambda>  s!    $//$:M:Ma:P:V:V*W r2   )r%   r  rA   r  r  r|   r  	stream_iddevice_indexr  r6   )r.   r:   streams   `  r1   r;   z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                     |j                   j                  d| j                  | j                  d   j	                         fi        | j                          y )Nrc   r   )rA   r  r  r&   r  rF   rG   s      r1   rI   zStreamContextVariable.exit  sI    
		OO  #,,.0		
 	r2   rn   r4   rl   )	rs   rt   ru   r   r8   r,   r;   rI   rw   rx   s   @r1   r  r    s&    
 
.UY*r2   r  c                   Z     e Zd ZdZed        Zed        Zd fdZd Zd	dZ	d
dZ
 xZS )PreserveVersionContextVariablez?
    Wraps torch.autograd._unsafe_preserve_version_counter
    c           	      r   t        |t        j                        rKt        j                  |fD cg c]  }|j	                  | d       c}      }t        j                  |g      }n=t        j                  |j
                  D cg c]  }|j	                  | d       c}      }t        ||      S c c}w c c}w )N_version)ra   r   TensorVariableTupleVariabler   r   r  )r:   tensorsr   versionss       r1   _create_lambda_from_tensorsz:PreserveVersionContextVariable._create_lambda_from_tensors'  s    gy778 ..9@	B1r:.BH  --wi8G ..8?F1r:.FH .gx@@ C
 Gs   B/B4c                 4     t        j                   fd      S )Nc                 0    t         j                  |       S r4   )r  r  )r  r:   s    r1   rS   z<PreserveVersionContextVariable.constructor.<locals>.<lambda>7  s    :VVG r2   )r   LambdaVariable)r:   s   `r1   constructorz*PreserveVersionContextVariable.constructor4  s    ''
 	
r2   c                    |j                  dd        t        |   di | || _        || _        t        | j                  t        j                        r%t        j                  | j                  g      | _        t        | j                  t        j                  t        j                  f      r&t        j                  | j                  g      | _        y y r  )
setdefaultr+   r,   r  prev_versionsra   r   r  r  r7   r  )r.   r  r  r/   r0   s       r1   r,   z'PreserveVersionContextVariable.__init__<  s    /40"6"*dllI$<$<=$22DLL>BDL!;!;Y=V=V W
 "+!8!8$:L:L9M!ND
r2   c                      y r4   r*   r9   s     r1   r;   z$PreserveVersionContextVariable.enterI  s    r2   c                     ddl m} t        j                  |      j	                  || j
                  | j                  gi       S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   TorchInGraphFunctionVariablerc   r  r  )r.   r:   rH   r  s       r1   rI   z#PreserveVersionContextVariable.exitL  s9    C55'

-T\\4+=+=>
C	Dr2   c                 T    t        dt        |       dg t        j                         y )NzLtorch.autograd._unsafe_preserve_version_counter escaped from compiled regionzyDynamo doesn't support compiling a region that returns a torch.autograd._unsafe_preserve_version_counter context manager.r  r  rN   s     r1   rY   z*PreserveVersionContextVariable.reconstructS  s-    bIU"..
	
r2   rl   rn   ro   )rs   rt   ru   r   r   r  r  r,   r;   rI   rY   rw   rx   s   @r1   r  r  "  sH     
A 
A 
 
OD
r2   r  c                        e Zd Z e e       ej                        Zed	d       Z		 d
	 d fdZ
d Zd	dZ	 	 	 	 	 	 d fdZd	dZd Zd Z xZS )&FSDPParamGroupUseTrainingStateVariablec                 P    t        d||g|j                  j                  gd|}|S )N)param_group_varr%   r&   r*   )r   r|   _training_state)r:   r  r$  r/   r   s        r1   r8   z-FSDPParamGroupUseTrainingStateVariable.created  s<    4 
+'.+11AAB
 	
 
r2   c                 b    t        |   d||d| || _        t        | j                         y r   )r+   r,   r  r   r   )r.   r  r%   r&   r/   r0   s        r1   r,   z/FSDPParamGroupUseTrainingStateVariable.__init__n  s<     	 	
'	
JP	
  /d,,-r2   c                 x    | j                  || j                         t        j                  j	                  d       S r4   r)  r9   s     r1   r;   z,FSDPParamGroupUseTrainingStateVariable.enterw  r*  r2   c                 x    | j                  || j                         t        j                  j	                  d       S r4   r,  rG   s      r1   rI   z+FSDPParamGroupUseTrainingStateVariable.exit{  r-  r2   c                 ^    | j                  || j                         t        |   |||      S r4   r/  r0  s       r1   rc   z4FSDPParamGroupUseTrainingStateVariable.call_function  r1  r2   c                 L   t        |      dk(  sJ |d   }| j                  j                  j                  |k7  rl| j                  j	                  |dt
        j                  j                  d      t        j                  |      fi        || j                  j                  _        y y )Nr   r   __setattr__r  )	rW   r  r|   r  call_methodr   r7   r8   EnumVariabler5  s       r1   r5   z1FSDPParamGroupUseTrainingStateVariable._call_func  s    6{aq	%%55>  ,,..556GH**51  :?D  &&6 ?r2   c                      y)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr*   r]   s    r1   rL   z2FSDPParamGroupUseTrainingStateVariable.module_name  s    Ur2   c                      y)Nuse_training_stater*   r]   s    r1   rM   z.FSDPParamGroupUseTrainingStateVariable.fn_name      #r2   rn   r4   rl   r<  )rs   rt   ru   r   r   r   FSDP_TRAINING_STATEr   r   r8   r,   r;   rI   rc   r5   rL   rM   rw   rx   s   @r1   r   r   a  sz    /1<3S3ST  >B.	.777#7 &7 -	7?V$r2   r   c                        e Zd ZdZeddd       Z	 	 ddeej                  j                  j                     deddf fdZed        Zd	 Zdd
Zd Zd Z xZS )SDPAKernelVariablez)represents torch.nn.attention.sdpa_kernelc                     t        |t        j                  j                  j                        r|g}t        d|d |d|}|S )N)r%   r&   set_priorityr*   )ra   r   nn	attention
SDPBackendr  )r:   backendsr  r/   r   s        r1   r8   zSDPAKernelVariable.create  sL    h 2 2 = => zH  
"%
 	
 
r2   Nr%   r  rm   c                 8    t        |   d||d| || _        y r   )r+   r,   r  )r.   r%   r&   r  r/   r0   s        r1   r,   zSDPAKernelVariable.__init__  s.     	 	
'	
JP	
 )r2   c           	          |D cg c]M  }| j                   j                  dt        j                  j                  j
                  |j                  fi       O }}|S c c}w r   )rA   r   r   r  r  _backend_from_stringr   )r:   r  backendnodess       r1   _backends_to_nodesz%SDPAKernelVariable._backends_to_nodes  s`     $
  II!!""77	
 
 
s   AAc                 >    t         j                  j                  j                   j                         _         j                  | fd       t         j                  j                  j                   j                   j                          j                  | j                        }|j                  j                  dt         j                  j                  j                  |t         j                        fi        t        j                  j                  d       S )N)with_priorityc                      t         j                  j                  j                   j                   j
                        S )Nr  )r   r  r  _sdpa_kernelprev_backendsr  r]   s   r1   rS   z*SDPAKernelVariable.enter.<locals>.<lambda>  s2    EHH&&33""1B1B 4  r2   r"  rc   )r   r  r  _cur_sdpa_kernel_backendsr  r$  r6   r#  r%   r  rA   r   boolr   r7   r8   )r.   r:   r  s   `  r1   r;   zSDPAKernelVariable.enter  s    "XX//II++ J 
 		
 	''T->-> 	( 	
 %%b$*<*<=
		HH++$t(()*		
 ))0066r2   c                 B   | j                          | j                  || j                        }|j                  j	                  dt
        j                  j                  j                  |t        | j                        fi        t        j                  j                  d       S r   )rF   r  r$  rA   r   r   r  r  r#  r&  r  r   r7   r8   )r.   r:   rH   r  s       r1   rI   zSDPAKernelVariable.exit  s}    %%b$*<*<=
		HH++$t(()*		
 ))0066r2   c                      y)Nztorch.nn.attentionr*   r]   s    r1   rL   zSDPAKernelVariable.module_name  r  r2   c                      y)N_sdpa_kernel_variadicr*   r]   s    r1   rM   zSDPAKernelVariable.fn_name  s    &r2   r;  rn   r   )rs   rt   ru   r   r   r8   listr   r  r  r  r&  r,   r  r;   rI   rL   rM   rw   rx   s   @r1   r  r    s~    3	 	 "	
)EHH..99:
) 	
) 

)  7,	7$
'r2   r  c                   L     e Zd Zd fdZd Z	 	 	 	 	 	 d fdZd ZddZ xZS )	r  c                    |6d|j                   j                  v r|j                   j                  d   |k(  sJ |j                  j                  |j                  k(  sJ d       t	        |   di | || _        || _        || _        y )Nexample_valuez.stream value is not equal to the passed devicer*   )r  metar[  r   r+   r,   r   r|   )r.   r   r|   r[  r/   r0   s        r1   r,   zStreamVariable.__init__  s    EJJOO!C::???3u<<<||  FKK/ 	
<	
/ 	"6"

r2   c                 "    t         j                  S r4   )r   Streamr]   s    r1   python_typezStreamVariable.python_type  s    ||r2   c                    t        | j                  |      s
J d|        ddlm}m} ddlm} |dv r> |j                  j                  d|g || g|z   |        t        j                  d       S |dk(  r@ |t        j                  | |j                  j                  d|g || g|z   |       	      S |d
k(  r6 |t        | |j                  j                  d|g || g|z   |       	      S ||v rt        |      dk(  rw|su|d   }t        |t              s#t        j                  j                  t               S t        j                  j                   ||   | j                  |j                              S t"        	| I  ||||      S )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|   utilsr4  r5  r  r  rA   r  r   r7   EventVariablerW   ra   r  r8   NotImplementedr+   r
  )
r.   r:   r   rH   r/   r4  r5  r  otherr0   s
            r1   r
  zStreamVariable.call_method   s    tzz4(P,I$*PP(E.??"BII""t&7v&N --d33W_$$55,bii,,!4*;TFTM6*R  ^#$(,bii,,!4*;TFTM6*R  ++D	Qv GEe^4 1188HH--44,&t,TZZE  w"2tT6::r2   c                     | j                   S r4   r   r]   s    r1   r  zStreamVariable.as_proxy-      zzr2   c                     | j                   rJ d| j                   }|j                  j                  j	                  || j
                        }|j                  |j                  |d             y )N_stream_Tadd)r   r[  r:   rA   install_global_by_idr|   append_outputcreate_load_globalr.   rO   prefixr   s       r1   rY   zStreamVariable.reconstruct0  s`     ;; DKK=)zz  55fdjjIg8848HIr2   rl   rH   rq   r/   rr   rm   r   ro   )	rs   rt   ru   r,   r2  r
  r  rY   rw   rx   s   @r1   r  r    s<    	+; &	+;
 -+; 
+;ZJr2   r  c                   B     e Zd Zd fdZ	 	 	 	 	 	 ddZd ZddZ xZS )r?  c                     |6d|j                   j                  v r|j                   j                  d   |k(  sJ t        |   di | || _        || _        y )Nr.  r*   )r  r/  r+   r,   r   r|   )r.   r   r|   r/   r0   s       r1   r,   zEventVariable.__init__@  sQ    EJJOO!C::???3u<<<"6"

r2   c                 
   ddl m} ddlm} |dv r> |j                  j
                  d|g || g|z   |        t        j                  d       S |dk(  r@ |t        j                  | |j                  j
                  d|g || g|z   |             S t        | j                        j                   d	t        | j                        j                   d	| }t        d
t        |      d| dg t        j                         y )Nr   )r5  r   r  )waitrecordr7  r
  r9  r:  .zUnsupported event methodz#Dynamo doesn't support tracing the zC method. We currently support wait, record, synchronize, and query.r  )r>  r5  r  r  rA   r  r   r7   r   r|   rt   ru   r   r  r   r  )r.   r:   r   rH   r/   r5  r  method_names           r1   r
  zEventVariable.call_methodG  s    	..44"BII""t&7v&N --d33W_$$55,bii,,!4*;TFTM6*R  

#../qdjj1A1N1N0OqQUPVW  2D	A+ OM N&22r2   c                     | j                   S r4   rC  r]   s    r1   r  zEventVariable.as_proxyl  rD  r2   c                     | j                   rJ d}|j                  j                  j                  || j                        }|j                  |j                  |d             y )N_eventTrG  )r   r:   rA   rI  r|   rJ  rK  rL  s       r1   rY   zEventVariable.reconstructo  sS     ;;zz  55fdjjIg8848HIr2   rl   rN  ro   )rs   rt   ru   r,   r
  r  rY   rw   rx   s   @r1   r?  r?  ?  s7    # &	#
 -# 
#JJr2   r?  c                   8     e Zd ZdZd fdZddZd Zd Z xZS )DynamoConfigPatchVariablez,represents torch._dynamo.patch_dynamo_configc                 >   t        |j                               }t        |   d|fd d| i | _        |D ];  \  }}t
        j                  j                  j                  |      | j                  |<   = t        | j                  j                               f| _        y r   )	r  r   r+   r,   r&   r   rM  rN  r   )r.   r%   r/   r  _r0   s        r1   r,   z"DynamoConfigPatchVariable.__init__~  s    m1134W'7WPVW # 	MFC',}}';';'G'G'LD$	M$T%8%8%>%>%@ACr2   c                     t        |      dk(  sJ |d   }|D ]/  \  }}t        j                  j                  j	                  ||       1 y r`   )rW   r   rM  rN  r	  )r.   r:   r
  r|   r  rX   s         r1   r5   z$DynamoConfigPatchVariable._call_func  sJ    6{aq	 	7HCMM  ,,S#6	7r2   c                      yNztorch._dynamor*   r]   s    r1   rL   z%DynamoConfigPatchVariable.module_name      r2   c                      y)Npatch_dynamo_configr*   r]   s    r1   rM   z!DynamoConfigPatchVariable.fn_name  s    $r2   rl   rn   	rs   rt   ru   r   r,   r5   rL   rM   rw   rx   s   @r1   rZ  rZ  y  s    6D7%r2   rZ  c                   8     e Zd ZdZd fdZddZd Zd Z xZS )ErrorOnGraphBreakVariablez-represents torch._dynamo.error_on_graph_breakc                 >    t        |   d|ft               fd| y r   )r+   r,   r   )r.   error_on_graph_breakr/   r0   s      r1   r,   z"ErrorOnGraphBreakVariable.__init__  s.     	
/1579	
 	
r2   c                 @    t        |      dk(  sJ t        |d          y r`   )rW   r   rv  s      r1   r5   z$ErrorOnGraphBreakVariable._call_func  s     6{a!&),r2   c                      yr_  r*   r]   s    r1   rL   z%ErrorOnGraphBreakVariable.module_name  r`  r2   c                      y)Nrg  r*   r]   s    r1   rM   z!ErrorOnGraphBreakVariable.fn_name  r  r2   rl   rn   rc  rx   s   @r1   re  re    s    7
-&r2   re  c                   p     e Zd Zdhej                  Zdeeef   ddf fdZ	 	 	 	 	 	 	 	 ddZ	d	dZ
 xZS )
WithExitFunctionVariabletargetrI  rm   Nc                 p    t        |   di | t        |t        t        f      sJ || _        || _        y r)   )r+   r,   ra   r#   rz   rI  rm  )r.   rI  rm  r/   r0   s       r1   r,   z!WithExitFunctionVariable.__init__  sC     	"6")+IJ
 	
 
 r2   c                 D    |rJ  | j                   j                  |g| S r4   )rI  rI   rb   s       r1   rc   z&WithExitFunctionVariable.call_function  s&     ztxx}}R'$''r2   c                    | j                   j                  |       |j                  j                  j                  rt
        j                  dk\  rI|j                  t        d             t
        j                  dk  r|j                  t        dd             |j                  | j                   j                  D cg c]  }|j                  |       c}       |j                  t        t        | j                   j                        d             |j                  t        | j                               |j                  t        d             y y c c}w )	N)      	PUSH_NULL)rq     SWAPr   )r  FPOP_TOP)rI  rP   r:   rA   partial_convertsysversion_inforJ  r
   rU   r%   rV   r	   rW   r   rm  )r.   rO   rX   s      r1   rY   z$WithExitFunctionVariable.reconstruct  s    	!!'*::,,7*%%&8&EF##g-))*<V*KL!!;?88;Q;QRC**3/R !!$S)?)?%@%H !!"3DKK"@A!!"4Y"?@ - Ss   <Erp   ro   )rs   rt   ru   r   rv   r   r#   rz   r,   rc   rY   rw   rx   s   @r1   rl  rl    so    		'	'N
*,JJK
 
(#( &( -	(
 
(Ar2   rl  )Mr   r  rx  r  typingr   r   torch._Cr   torch._guardsr    r   r   bytecode_transformationr	   r
   r   device_interfacer   excr   guardsr   r   r   r   r   r>  r   r   baser   	functionsr   r   r   r   r   r   r   r   user_definedr   torch._dynamo.codegenr   torch._dynamo.symbolic_convertr!   r#   rz   r   r   r   r   r   r   r   r   r  r!  r?  rV  rh  r~  r  r  r  r  r  r  r   r  r  r?  rZ  re  rl  r*   r2   r1   <module>r     s  (  
  '   + 
 8 " 0 2 H !	 	 	 4 /D]o ]@(%> (V)+I )8%70G %7P!77N !7H&7,C &7R#7&= #7L#75 #7L$7-D $7NM&= M>17-D 17h7". 7"t@ 3 @ F/0 /d>&#: >&B'.&= '.T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 %>& 7 &*-A -Ar2   