
    rh                     N   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 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      Z- G d de(      Z. G d de-      Z/ G d de-      Z0 G d de-      Z1 G d de-      Z2 G d de-      Z3 G d  d!e-      Z4 G d" d#e-      Z5 G d$ d%e-      Z6 G d& d'e-      Z7 G d( d)e-      Z8 G d* d+e-      Z9 G d, d-e-      Z: G d. d/e-      Z; G d0 d1e-      Z< G d2 d3e-      Z= G d4 d5e-      Z> G d6 d7e-      Z? G d8 d9e-      Z@ G d: d;e-      ZA G d< d=e-      ZB G d> d?e-      ZC G d@ dAe      ZD G dB dCe      ZE G dD dEe-      ZF G dF dGe      ZGy)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                        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       v/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/variables/ctx_manager.pyr*   z ContextWrappingVariable.__init__E   s#    "6"*,    c                     | j                  || j                         | j                  |       t        j                  j                  d       S N)
_call_funcr#   set_cleanup_hookr   ConstantVariablecreater,   txs     r/   enterzContextWrappingVariable.enterJ   s;    D../b!))0066r0   c                 p     | fd}| _         j                  j                   j                         y )Nc                  >     j                   j                         y r2   )r3   r$   r7   s   r/   fnz4ContextWrappingVariable.set_cleanup_hook.<locals>.fnR   s    D$7$78r0   
cleanup_fnoutputadd_cleanup_hookcleanupr,   r8   r<   s   `` r/   r4   z(ContextWrappingVariable.set_cleanup_hookO   s,    :9 
		""4<<0r0   c                 `    | j                          t        j                  j                  d       S r2   )cleanup_assertr   r5   r6   r,   r8   argss      r/   exitzContextWrappingVariable.exitX   s%    ))0066r0   c                      |t        |j                  j                  | j                               | j	                                      y r2   )r   r8   import_sourcemodule_namefn_namer,   codegens     r/   reconstruct_typez(ContextWrappingVariable.reconstruct_type\   s2    wzz//0@0@0BCT\\^T	
r0   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 r2   )rN   )rM   r,   s   r/   <lambda>z5ContextWrappingVariable.reconstruct.<locals>.<lambda>b   s    d&;&;G&D r0   r(   F)add_push_nullr#   extend_outputcreate_load_constr	   len)r,   rM   r#   vals   ``  r/   reconstructz#ContextWrappingVariable.reconstructa   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    r/   rJ   z#ContextWrappingVariable.module_namei   s    !">??r0   c                     t        d      )Nzfn_name called on baserY   r[   s    r/   rK   zContextWrappingVariable.fn_namel   s    !":;;r0   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   )
rU   
isinstancer   r   r   r   r   r   r   r   r,   r8   rF   r-   s       r/   call_functionz%ContextWrappingVariable.call_functiono   s     4yA~~G*$"$	
 	
 
 d1g9:4T!WdCCd1g34.tAw==d1g12,T!Wd;;d1g34.tAw== 5r0   c                      yNTr(   r[   s    r/   supports_graph_breaksz-ContextWrappingVariable.supports_graph_breaks       r0   c                      yrc   r(   r[   s    r/   exit_on_graph_breakz+ContextWrappingVariable.exit_on_graph_break   re   r0   c                 L    | j                   | j                          d | _         y y r2   )r>   r[   s    r/   rA   zContextWrappingVariable.cleanup   s"    ??&OO"DO 'r0   c                 J    | j                   sJ d       | j                          y )Nzmultiple exits?)r>   rA   r[   s    r/   rD   z&ContextWrappingVariable.cleanup_assert   s    1 11r0   r2   returnNr8   r   rM   r   r8   r   rF   list[VariableTracker]r-   dict[str, VariableTracker]rk   r   )__name__
__module____qualname__r   _nonvar_fieldsr*   r9   r4   rG   rN   rW   rJ   rK   ra   rd   rg   rA   rD   __classcell__r.   s   @r/   r!   r!   <   s    	
 
	'	'N-
7
17

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

 r0   c                 .    | j                   j                  S r2   )r"   rr   r[   s    r/   rJ   z*GenericContextWrappingVariable.module_name   s    {{%%%r0   c                 @    t        | j                        j                  S r2   )typer"   rq   r[   s    r/   rK   z&GenericContextWrappingVariable.fn_name   s    DKK )))r0   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__ra   )r,   r8   r   s      r/   r9   z$GenericContextWrappingVariable.enter   sY    ,*T[[+2V++KK!!**
 -B
#		$r0   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   ra   active_generic_context_managerspop)r,   r8   rF   r   xs        r/   rG   z#GenericContextWrappingVariable.exit   sr    ,*T[[*2U((KK  ))
 -D"
%	 	

 	**..0r0   c                      yNFr(   r[   s    r/   rd   z4GenericContextWrappingVariable.supports_graph_breaks   s    r0   c                      yrc   r(   r[   s    r/   rg   z2GenericContextWrappingVariable.exit_on_graph_break   re   r0   rj   rl   )rq   rr   rs   r*   rJ   rK   r9   rG   rd   rg   ru   rv   s   @r/   rx   rx      s&    &*$r0   rx   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   r8   r#   r-   s      r/   r6   z0GradInplaceRequiresGradCtxManagerVariable.create   s$    8 
'
 
 	
r0   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 r2   )torch_C
_functorch!set_inplace_requires_grad_allowed
prev_stater[   s   r/   rQ   zAGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>   s#    EHH''II r0   ra   )r#   r   r   r   !get_inplace_requires_grad_allowedr   r   r4   r?   create_nodeproxyr   r5   r6   )r,   r8   enableds   `  r/   r9   z/GradInplaceRequiresGradCtxManagerVariable.enter   s    &&	((--OOQ==gF	
 YY**HHAAJ	

 ))0066r0   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S Nra   )rA   r?   r   r   r   r   r   r   r   r5   r6   rE   s      r/   rG   z.GradInplaceRequiresGradCtxManagerVariable.exit   sY    
		HHAA__		
 ))0066r0   Nrl   rq   rr   rs   __doc__staticmethodr6   r9   rG   r(   r0   r/   r   r      s!    -
 
7$7r0   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      r/   r6   z7TemporarilyPopInterpreterStackCtxManagerVariable.create   s$    ? 
'
 
 	
r0   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 r2   )r   r   r   push_dynamic_layer_stacksavedr[   s   r/   rQ   zHTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>   s     EHH''@@L r0   ra   r(   )r   r   r   pop_dynamic_layer_stackr   r4   r?   r   r   r   r5   r6   r7   s   ` r/   r9   z6TemporarilyPopInterpreterStackCtxManagerVariable.enter   s~    XX((@@B
L	
 YY**HH77	

 ))0066r0   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        t        j                  j                  d       S r   )rA   r?   r   r   r   r   r   r   r   r5   r6   rE   s      r/   rG   z5TemporarilyPopInterpreterStackCtxManagerVariable.exit  sX    
		HH88ZZM		
 ))0066r0   Nrl   r   r(   r0   r/   r   r      s!    T
 
77r0   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   r8   r-   vars      r/   r6   z,JvpIncrementNestingCtxManagerVariable.create  s(    3 

 

 
r0   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 r2   )r   r   eager_transformsexit_jvp_nestingr(   r0   r/   rQ   z=JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>*  s    ((99JJL r0   ra   r(   )r   _guards_singletonr   r   r   enter_jvp_nestingr4   r?   r   r   _jvp_increment_nestingr   r   r5   r6   )r,   r8   	jvp_levels      r/   r9   z+JvpIncrementNestingCtxManagerVariable.enter&  s    d,,-$$55GGI	L	
 YY**HH66	

 ))00;;r0   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S Nra   r(   )
rA   r?   r   r   r   r   _jvp_decrement_nestingr   r5   r6   rE   s      r/   rG   z*JvpIncrementNestingCtxManagerVariable.exit4  sO    
		UXX00GGR	
 ))0066r0   Nrl   rq   rr   rs   r   r   r   r   FUNCTORCH_STACK_MATCHr   r   r6   r9   rG   r(   r0   r/   r   r     s:    ? /1<3U3UV <7r0   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      r/   r6   z&SetFwdGradEnabledContextManager.create?  s$    . 
'
 
 	
r0   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 r2   )r   r   _set_fwd_grad_enabledr   r[   s   r/   rQ   z7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>M  s    EHH224??C r0   ra   )r#   r   r   _is_fwd_grad_enabledr   r   r4   r?   r   r   r   r5   r6   )r,   r8   modes   `  r/   r9   z%SetFwdGradEnabledContextManager.enterG  s    ##((779&&t,C	
 YY**HH**G	

 ))0066r0   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rA   r?   r   r   r   r   r   r   r5   r6   rE   s      r/   rG   z$SetFwdGradEnabledContextManager.exitW  sS    
		HH**__		
 ))0066r0   Nrl   r   r(   r0   r/   r   r   <  s!    a
 
7 7r0   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   )r8   r-   s     r/   r6   zDualLevelContextManager.createg  s$    & 

 
 	
r0   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   r/   rQ   z/DualLevelContextManager.enter.<locals>.<lambda>s  s#    11AAAW r0   ra   r(   )r   r   r   r   r   enter_dual_levelr   r4   r?   r   r   _enter_dual_levelr   r   r5   r6   r7   s   ` r/   r9   zDualLevelContextManager.entero  s    d,,-22CCEW	
 YY**HH&&	

 ))00@@r0   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rA   r?   r   r   r   _exit_dual_levelr   r   r5   r6   rE   s      r/   rG   zDualLevelContextManager.exit}  sS    
		HH%%^^		
 ))0066r0   Nrl   )rq   rr   rs   r   r   r   r   
DUAL_LEVELr   r   r6   r9   rG   r(   r0   r/   r   r   b  s9    E/1<3J3JK
 
A7r0   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      r/   r6   z-GradIncrementNestingCtxManagerVariable.create  s(    4 

 

 
r0   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 r2   )r   r   r   _grad_decrement_nestingr(   r0   r/   rQ   z>GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>      %((*=*=*U*U*W r0   ra   r(   )r   r   r   r   r   _grad_increment_nestingr4   r?   r   r   r   r5   r6   )r,   r8   
grad_levels      r/   r9   z,GradIncrementNestingCtxManagerVariable.enter  s    d,,-XX((@@B
b"WXYY**HH77	

 ))00<<r0   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S r   )
rA   r?   r   r   r   r   r   r   r5   r6   rE   s      r/   rG   z+GradIncrementNestingCtxManagerVariable.exit  O    
		UXX00HH"b	
 ))0066r0   Nrl   r   r(   r0   r/   r   r     s:    @ /1<3U3UV 
=7r0   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   )r8   r   s     r/   r6   z&CatchWarningsCtxManagerVariable.create  s    . 3
 	
r0   c                 `    t        |t              sJ |       t        |   di | || _        y r'   )r_   dictr)   r*   r   )r,   r   r-   r.   s      r/   r*   z(CatchWarningsCtxManagerVariable.__init__  s2    -t4I6II4"6"#6 r0   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 r2   )r   )ctx_vals   r/   rQ   z7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>  s    '*:*:4t*L r0   r(   )
r   itemsas_python_constantwarningscatch_warningsr4   r   r5   r6   r   )r,   r8   kvr-   r   s        @r/   r9   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   r/   rQ   z=CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>  s    !4!4ZAQ!R r0   F)	rR   foreachr   valuestuplekeysrS   create_call_function_kwrU   )r,   r   r   s    ` r/   rW   z+CatchWarningsCtxManagerVariable.reconstruct  sg    
RS


4++2245T--2245
33CItUKLr0   rl   rj   )
rq   rr   rs   r   r   r6   r*   r9   rW   ru   rv   s   @r/   r   r     s)    1
 
7
FMr0   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   )r8   r#   r-   r   s       r/   r6   z-VmapIncrementNestingCtxManagerVariable.create  s(    4 
'
 

 
r0   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 r2   )r   r   r   _vmap_decrement_nestingr(   r0   r/   rQ   z>VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>  r   r0   ra   )r   r   r#   r_   r   SymNodeVariablesym_numas_proxynoder   r   r   r   _vmap_increment_nestingr4   r?   r   r   r5   r6   )r,   r8   
batch_size
randomnessbatch_size_valuebatch_size_node
vmap_levels          r/   r9   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**HH77j)	

 ))00<<r0   c                     | j                          |j                  j                  dt        j                  j
                  j                  di        t        j                  j                  d       S r   )
rA   r?   r   r   r   r   r   r   r5   r6   rE   s      r/   rG   z+VmapIncrementNestingCtxManagerVariable.exit  r   r0   Nrl   r   r(   r0   r/   r   r     s:    ; /1<3U3UV =,7r0   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_enabledr3   r#   )r8   target_valueinitializedr-   r   s        r/   r6   zGradModeVariable.create  sM     
'.!1134
 

 NN2s001
r0   c                 T    t        |   d||d| t        | j                         y r   r)   r*   r   r   )r,   r#   r$   r  r-   r.   s        r/   r*   zGradModeVariable.__init__  s4     	 	
'	
JP	
 	d,,-r0   c                 x    | j                  || j                         t        j                  j	                  d       S r2   r3   r#   r   r5   r6   r7   s     r/   r9   zGradModeVariable.enter  -    D../))0066r0   c                 x    | j                  || j                         t        j                  j	                  d       S r2   r3   r$   r   r5   r6   rE   s      r/   rG   zGradModeVariable.exit  -    D//0))0066r0   c                 ^    | j                  || j                         t        |   |||      S r2   r3   r$   r)   ra   r,   r8   rF   r-   r.   s       r/   ra   zGradModeVariable.call_function  -     	D//0w$Rv66r0   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   ra   )rU   r   r  r?   r   r   _set_grad_enabledr,   r8   r   rz   s       r/   r3   zGradModeVariable._call_func(  sn    6{aq	  "e+II!!!;!;eXr HH&&u-	 ,r0   c                      yNr   r(   r[   s    r/   rJ   zGradModeVariable.module_name2      r0   c                      y)Nset_grad_enabledr(   r[   s    r/   rK   zGradModeVariable.fn_name5  s    !r0   Frl   rc   rj   r8   r   rF   ro   r-   rp   )rq   rr   rs   r   r   r   r   	GRAD_MODEr   r   r6   r*   r9   rG   ra   r3   rJ   rK   ru   rv   s   @r/   r
  r
    s|    @/1<3I3IJ  ?C.	.777#7 &7 -	7."r0   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r8   r  r-   r   s       r/   r6   zInferenceModeVariable.create:  s1    #N
+0+J+J+L
PV
 
r0   c                 d    |t        j                         }t        |   d||d| || _        y r   )r   r*  r)   r*   r#   r+   s       r/   r*   zInferenceModeVariable.__init__A  sB     !"<<>N 	
'	
JP	
 +r0   c                     | j                          |j                  j                  dt        j                  j
                  j                  | j                  fi        y r   )rD   r?   r   r   r   	grad_mode_exit_inference_moder   rE   s      r/   rG   zInferenceModeVariable.exitO  sB    
		NN$$99ZZM		
r0   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 r2   )r   r   r  r   r.  r/  )ctx disabled_inference_mode_forciblypriors   r/   cleanup_hookz1InferenceModeVariable.enter.<locals>.cleanup_hookg  s0    /**51((==cBr0   ra   )r   _dynamoconfig"fake_tensor_disable_inference_moder#   r  r   r  r   r.  _enter_inference_moder4   r?   r   r   )r,   r8   r5  r2  r3  r4  s      @@@r/   r9   zInferenceModeVariable.enterX  s    +0(MM  CC""1%
 04,))+EHH&&u-..**@@$BTBTUC	C 	b,/YY**NN$$::"d  "	

r0   c                      yr   r(   r[   s    r/   rJ   z!InferenceModeVariable.module_nameu  r!  r0   c                      y)Ninference_moder(   r[   s    r/   rK   zInferenceModeVariable.fn_namex  s    r0   rl   r2   rj   rq   rr   rs   r   r6   r*   rG   r9   rJ   rK   ru   rv   s   @r/   r(  r(  9  s=      +
 
+

: r0   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(   )r?  r   cuda_get_device_index)r8   devicer-   r   s       r/   r6   zCUDADeviceVariable.create  s?      
 ::777NO
 

 
r0   c                 8    t        |   d||d| || _        y r   r)   r*   r#   r+   s       r/   r*   zCUDADeviceVariable.__init__  s.     	 	
'	
JP	
 +r0   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d      S )Nra   F)
rD   r?   r   r   rB  _maybe_exchange_devicer   r   r5   r6   rE   s      r/   rG   zCUDADeviceVariable.exit  sU    
		JJ--ZZM		
 ))0077r0   c                    t        j                  j                  | j                   | j	                  |fd       |j
                  j                  dt         j                  j                  g | j                  i       | _        y )Nc                  B    t         j                  j                         S r2   )r   rB  rH  )prev_idxs   r/   rQ   z*CUDADeviceVariable.enter.<locals>.<lambda>  s    %***K*KH*U r0   ra   )r   rB  _exchange_devicer#   r4   r?   r   r   )r,   r8   rK  s     @r/   r9   zCUDADeviceVariable.enter  se    ::..0B0BCb"UVYY**JJ''"d  "	

r0   c                      y)Nz
torch.cudar(   r[   s    r/   rJ   zCUDADeviceVariable.module_name      r0   c                      y)NrD  r(   r[   s    r/   rK   zCUDADeviceVariable.fn_name  s    r0   rl   r2   rj   )rq   rr   rs   r   r   r6   r*   rG   r9   rJ   rK   ru   rv   s   @r/   r?  r?  |  s@    &  	+
 
	+8
r0   r?  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   )rQ  r   s      r/   r6   z#TorchFunctionDisableVariable.create  s(    * 

 

 
r0   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(   )rU   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$   rV  r-   r   r8   r.   s          r/   r*   z%TorchFunctionDisableVariable.__init__  s     =!Q&&&>"a'''<-"--/*,,LL 	4 ,,HH 	0 	 	
'	
JP	
 	d,,-r0   c                 p     | fd}| _         j                  j                   j                         y )Nc                       j                   j                  _         j                  s j                   j                  _        y y r2   )rY  rW  rX  rV  rZ  r7   s   r/   r<   z9TorchFunctionDisableVariable.set_cleanup_hook.<locals>.fn  sA    @@ 00P ))DD 44P *r0   r=   rB   s   `` r/   r4   z-TorchFunctionDisableVariable.set_cleanup_hook  s,    : 
		""4<<0r0   c                     t        |      dk(  sJ d|j                  _        | j                  sd|j                  _        y y )Nr   F)rU   rW  rX  rV  rZ  )r,   r8   r   s      r/   r3   z'TorchFunctionDisableVariable._call_func  s>    6{aKP((H!!KPB,,H "r0   c                      y)Nztorch._Cr(   r[   s    r/   rJ   z(TorchFunctionDisableVariable.module_name      r0   c                     | j                   ryy)NDisableTorchFunctionSubclassDisableTorchFunction)rV  r[   s    r/   rK   z$TorchFunctionDisableVariable.fn_name  s    1%r0   rl   rc   rj   r2   )rq   rr   rs   r   r   r   r   TORCH_FUNCTION_STATEr   r   r6   r*   r4   r3   rJ   rK   ru   rv   s   @r/   rQ  rQ    sX    H/1<3T3TU  AE.	.*1Q&r0   rQ  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   )rf  r   $are_deterministic_algorithms_enabledr3   r4   r+  s       r/   r6   z&DeterministicAlgorithmsVariable.create  sT    - 
'.!FFHI
 

 	rL>*R 
r0   c                 T    t        |   d||d| t        | j                         y r   r  r+   s       r/   r*   z(DeterministicAlgorithmsVariable.__init__  s2     	
'	
JP	
 	d,,-r0   c                 @    t         j                  j                  d       S r2   r   r5   r6   r7   s     r/   r9   z%DeterministicAlgorithmsVariable.enter      ))0066r0   c                     t        |      dk(  sJ |d   }|j                  j                  dt        j                  j
                  |fi       f t        j                  j                  |       y r  )rU   r?   r   r   r   _set_deterministic_algorithmsr  s       r/   r3   z*DeterministicAlgorithmsVariable._call_func
  s^    6{aq	II!!!G!G%SU	

 	..u5r0   c                      yr   r(   r[   s    r/   rJ   z+DeterministicAlgorithmsVariable.module_name  r!  r0   c                      y)Nuse_deterministic_algorithmsr(   r[   s    r/   rK   z'DeterministicAlgorithmsVariable.fn_name  s    -r0   rl   r2   rj   )rq   rr   rs   r   r   r   r   DETERMINISTIC_ALGORITHMSr   r   r6   r*   r9   r3   rJ   rK   ru   rv   s   @r/   rf  rf    sL    `\BB  .76.r0   rf  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   )rt  r   r   	_autograd/_saved_tensors_hooks_get_disabled_error_messager3   r4   r+  s       r/   r6   z(DisabledSavedTensorsHooksVariable.create  s`    / 
'.""RRT

 
 	rL>*R 
r0   c                 *    t        |   d||d| y r   r)   r*   r+   s       r/   r*   z*DisabledSavedTensorsHooksVariable.__init__+  s"     	
'	
JP	
r0   c                 @    t         j                  j                  d       S r2   rk  r7   s     r/   r9   z'DisabledSavedTensorsHooksVariable.enter0  rl  r0   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   ra   r(   )rU   r?   r   r   r   rv  _saved_tensors_hooks_disable_saved_tensors_hooks_enabler  s       r/   r3   z,DisabledSavedTensorsHooksVariable._call_func3  s    6{aq	 II!!""??	 HH;;EB II!!!3!3!O!OQSUW HH::<r0   c                      y)Nztorch.autograd.graphr(   r[   s    r/   rJ   z-DisabledSavedTensorsHooksVariable.module_nameH  s    %r0   c                      y)Ndisable_saved_tensors_hooksr(   r[   s    r/   rK   z)DisabledSavedTensorsHooksVariable.fn_nameK  s    ,r0   rl   r2   rj   )rq   rr   rs   r   r   r6   r*   r9   r3   rJ   rK   ru   rv   s   @r/   rt  rt    s1    E
 


7=*&-r0   rt  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  rB  cpur$   )r   ampautocast_modeautocastrB  r  inspect	signaturebindapply_defaultsclear	argumentsr_   r   appendr   r  )funcrF   r-   
bound_argsr#   keyargr   s           r/   r6   zAutocastModeVariable.createP  sI   II##,,JJNN##IIMM""
 
 	
 
 2W&&t,114B6B
!!#G 	*Cm#

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

(?(? ?fU **3/#/$$S%;%;%=>$$S)	* #=PPP
r0   c                 8    t        |   d||d| || _        y r   rF  r+   s       r/   r*   zAutocastModeVariable.__init__p  s,     	
'	
JP	
 +r0   c                     | j                          |j                  j                  dt        j                  j
                  | j                  fi        t        j                  j                  d       S r   )
rD   r?   r   r   r  _exit_autocastr   r   r5   r6   rE   s      r/   rG   zAutocastModeVariable.exitv  sR    
		UYY55

}b	
 ))0066r0   c                    t        j                  j                  | j                   | j	                  |fd       |j
                  j                  dt         j                  j                  g | j                  i       | _        y )Nc                  B    t         j                  j                         S r2   )r   r  r  )r2  s   r/   rQ   z,AutocastModeVariable.enter.<locals>.<lambda>  s    %))*B*B3*G r0   ra   )r   r  _enter_autocastr#   r4   r?   r   r   )r,   r8   r2  s     @r/   r9   zAutocastModeVariable.enter}  sd    ii''););<b"GHYY**UYY668N$:L:L8NPR

r0   c                      y)Nztorch.amp.autocast_moder(   r[   s    r/   rJ   z AutocastModeVariable.module_name  s    (r0   c                      y)Nr  r(   r[   s    r/   rK   zAutocastModeVariable.fn_name  r`  r0   r2   rj   rl   r=  rv   s   @r/   r  r  O  s.     >+7
)r0   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(   ry  )r,   r#   r-   r.   s      r/   r*   zNullContextVariable.__init__  s    ?}??r0   c                 @    t         j                  j                  d       S r2   rk  r7   s     r/   r9   zNullContextVariable.enter  rl  r0   c                 @    t         j                  j                  d       S r2   rk  rE   s      r/   rG   zNullContextVariable.exit  rl  r0   c                      yN
contextlibr(   r[   s    r/   rJ   zNullContextVariable.module_name  rN  r0   c                      yNnullcontextr(   r[   s    r/   rK   zNullContextVariable.fn_name      r0   r2   rj   rl   )
rq   rr   rs   r   r*   r9   rG   rJ   rK   ru   rv   s   @r/   r  r    s"    @77r0   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  ry  )r,   r-   r.   s     r/   r*   z ProfilerContextVariable.__init__  s    6t6v6r0   c                     | S r2   r(   r7   s     r/   r9   zProfilerContextVariable.enter  s    r0   c                 @    t         j                  j                  d       S r2   rk  rE   s      r/   rG   zProfilerContextVariable.exit  rl  r0   c                      yr  r(   r[   s    r/   rJ   z#ProfilerContextVariable.module_name  rN  r0   c                      yr  r(   r[   s    r/   rK   zProfilerContextVariable.fn_name  r  r0   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     r/   rW   z#ProfilerContextVariable.reconstruct  s*    HIr"..		
r0   rj   rl   )rq   rr   rs   r   r*   r9   rG   rJ   rK   rW   ru   rv   s   @r/   r  r    s&    77
r0   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_clsra   r2   )r#   r$   rD  r(   )	builderr  r   rD  current_streamStreamVariabler?   create_proxyr  )r8   r  r-   r  current_stream_methodr  s         r/   r6   zStreamContextVariable.create  s    . 8!

. 	 +II""%		
 % 
'.*+&&
 	
 	
r0   c                     t        |   d||d| || _        t        | j                        j                  | _        t        | j                        j
                  | _        y r   )r)   r*   rD  r   
set_stream_set_stream_by_idset_stream_id)r,   r#   rD  r$   r-   r.   s        r/   r*   zStreamContextVariable.__init__  sX     	
'	
JP	
 24;;?JJ5dkkBTTr0   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   ra   c                  T     j                   j                  d   j                        S )Nr   )r  r$   rz   r[   s   r/   rQ   z-StreamContextVariable.enter.<locals>.<lambda>  s!    $//$:M:Ma:P:V:V*W r0   )r#   r   r?   r  r  rz   r  	stream_iddevice_indexr  r4   )r,   r8   streams   `  r/   r9   zStreamContextVariable.enter  s    a ))+7II""##A&//13	 ''*00FII""""!!6#6#68J8JK	 	**1-334b"WXr0   c                     |j                   j                  d| j                  | j                  d   j	                         fi        | j                          y )Nra   r   )r?   r  r  r$   r   rD   rE   s      r/   rG   zStreamContextVariable.exit  sI    
		OO  #,,.0		
 	r0   rl   r2   rj   )	rq   rr   rs   r   r6   r*   r9   rG   ru   rv   s   @r/   r  r    s&    
 
.UY*r0   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)r_   r   TensorVariableTupleVariablevar_getattrr   r  )r8   tensorsr   versionss       r/   _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 r2   )r  r  )r  r8   s    r/   rQ   z<PreserveVersionContextVariable.constructor.<locals>.<lambda>  s    :VVG r0   )r   LambdaVariable)r8   s   `r/   constructorz*PreserveVersionContextVariable.constructor  s    ''
 	
r0   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_versionsr_   r   r  r  r5   r   )r,   r  r  r-   r.   s       r/   r*   z'PreserveVersionContextVariable.__init__  s    /40"6"*dllI$<$<=$22DLL>BDL!;!;Y=V=V W
 "+!8!8$:L:L9M!ND
r0   c                      y r2   r(   r7   s     r/   r9   z$PreserveVersionContextVariable.enter*  s    r0   c                     ddl m} t        j                  |      j	                  || j
                  | j                  gi       S )Nr   )_unsafe_set_version_counter)tensor_version_opr  r   TorchInGraphFunctionVariablera   r  r  )r,   r8   rF   r  s       r/   rG   z#PreserveVersionContextVariable.exit-  s9    C55'

-T\\4+=+=>
C	Dr0   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  rL   s     r/   rW   z*PreserveVersionContextVariable.reconstruct4  s-    bIU"..
	
r0   rj   rl   rm   )rq   rr   rs   r   r   r  r  r*   r9   rG   rW   ru   rv   s   @r/   r  r    sH     
A 
A 
 
OD
r0   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  rz   _training_state)r8   r  r  r-   r   s        r/   r6   z-FSDPParamGroupUseTrainingStateVariable.createE  s<    4 
+'.+11AAB
 	
 
r0   c                 b    t        |   d||d| || _        t        | j                         y r   )r)   r*   r  r   r   )r,   r  r#   r$   r-   r.   s        r/   r*   z/FSDPParamGroupUseTrainingStateVariable.__init__O  s<     	 	
'	
JP	
  /d,,-r0   c                 x    | j                  || j                         t        j                  j	                  d       S r2   r  r7   s     r/   r9   z,FSDPParamGroupUseTrainingStateVariable.enterX  r  r0   c                 x    | j                  || j                         t        j                  j	                  d       S r2   r  rE   s      r/   rG   z+FSDPParamGroupUseTrainingStateVariable.exit\  r  r0   c                 ^    | j                  || j                         t        |   |||      S r2   r  r  s       r/   ra   z4FSDPParamGroupUseTrainingStateVariable.call_function`  r  r0   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  )	rU   r  rz   r  call_methodr   r5   r6   EnumVariabler  s       r/   r3   z1FSDPParamGroupUseTrainingStateVariable._call_funci  s    6{aq	%%55>  ,,..556GH**51  :?D  &&6 ?r0   c                      y)NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr(   r[   s    r/   rJ   z2FSDPParamGroupUseTrainingStateVariable.module_namex  s    Ur0   c                      y)Nuse_training_stater(   r[   s    r/   rK   z.FSDPParamGroupUseTrainingStateVariable.fn_name{      #r0   rl   r2   rj   r%  )rq   rr   rs   r   r   r   FSDP_TRAINING_STATEr   r   r6   r*   r9   rG   ra   r3   rJ   rK   ru   rv   s   @r/   r  r  B  sz    /1<3S3ST  >B.	.777#7 &7 -	7?V$r0   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(   )r_   r   nn	attention
SDPBackendr  )r8   backendsr  r-   r   s        r/   r6   zSDPAKernelVariable.create  sL    h 2 2 = => zH  
"%
 	
 
r0   Nr#   r  rk   c                 8    t        |   d||d| || _        y r   )r)   r*   r  )r,   r#   r$   r  r-   r.   s        r/   r*   zSDPAKernelVariable.__init__  s.     	 	
'	
JP	
 )r0   c           	          |D cg c]M  }| j                   j                  dt        j                  j                  j
                  |j                  fi       O }}|S c c}w r   )r?   r   r   r  r  _backend_from_stringname)r8   r  backendnodess       r/   _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   r/   rQ   z*SDPAKernelVariable.enter.<locals>.<lambda>  s2    EHH&&33""1B1B 4  r0   r
  ra   )r   r  r  _cur_sdpa_kernel_backendsr  r  r4   r  r#   r  r?   r   boolr   r5   r6   )r,   r8   r  s   `  r/   r9   zSDPAKernelVariable.enter  s    "XX//II++ J 
 		
 	''T->-> 	( 	
 %%b$*<*<=
		HH++$t(()*		
 ))0066r0   c                 B   | j                          | j                  || j                        }|j                  j	                  dt
        j                  j                  j                  |t        | j                        fi        t        j                  j                  d       S r   )rD   r  r  r?   r   r   r  r  r  r  r  r   r5   r6   )r,   r8   rF   r  s       r/   rG   zSDPAKernelVariable.exit  s}    %%b$*<*<=
		HH++$t(()*		
 ))0066r0   c                      y)Nztorch.nn.attentionr(   r[   s    r/   rJ   zSDPAKernelVariable.module_name  r  r0   c                      y)N_sdpa_kernel_variadicr(   r[   s    r/   rK   zSDPAKernelVariable.fn_name  s    &r0   r$  rl   r   )rq   rr   rs   r   r   r6   listr   r  r  r  r  r*   r  r9   rG   rJ   rK   ru   rv   s   @r/   r  r    s~    3	 	 "	
)EHH..99:
) 	
) 

)  7,	7$
'r0   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  metarD  r~   r)   r*   r   rz   )r,   r   rz   rD  r-   r.   s        r/   r*   zStreamVariable.__init__  s    EJJOO!C::???3u<<<||  FKK/ 	
<	
/ 	"6"

r0   c                 "    t         j                  S r2   )r   Streamr[   s    r/   python_typezStreamVariable.python_type  s    ||r0   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_clsr8   r   record_eventr   )hasattrrz   utilsr  r  r  r  r?   r  r   r5   EventVariablerU   r_   r  r6   NotImplementedr)   r  )
r,   r8   r  rF   r-   r  r  r  otherr.   s
            r/   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::r0   c                     | j                   S r2   r   r[   s    r/   r   zStreamVariable.as_proxy      zzr0   c                     | j                   rJ d| j                   }|j                  j                  j	                  || j
                        }|j                  |j                  |d             y )N_stream_Tadd)r   rD  r8   r?   install_global_by_idrz   append_outputcreate_load_globalr,   rM   prefixr  s       r/   rW   zStreamVariable.reconstruct  s`     ;; DKK=)zz  55fdjjIg8848HIr0   rj   rF   ro   r-   rp   rk   r   rm   )	rq   rr   rs   r*   r  r  r   rW   ru   rv   s   @r/   r  r    s<    	+; &	+;
 -+; 
+;ZJr0   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   rz   )r,   r   rz   r-   r.   s       r/   r*   zEventVariable.__init__!  sQ    EJJOO!C::???3u<<<"6"

r0   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   )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  r?   r  r   r5   r~   rz   rr   rs   r   r  r   r  )r,   r8   r  rF   r-   r  r  method_names           r/   r  zEventVariable.call_method(  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r0   c                     | j                   S r2   r+  r[   s    r/   r   zEventVariable.as_proxyM  r,  r0   c                     | j                   rJ d}|j                  j                  j                  || j                        }|j                  |j                  |d             y )N_eventTr/  )r   r8   r?   r1  rz   r2  r3  r4  s       r/   rW   zEventVariable.reconstructP  sS     ;;zz  55fdjjIg8848HIr0   rj   r6  rm   )rq   rr   rs   r*   r  r   rW   ru   rv   s   @r/   r'  r'     s7    # &	#
 -# 
#JJr0   r'  c                   F     e Zd ZdZd fdZd Zd	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   r6  r7  __getattr__)r,   r#   r-   r  _r.   s        r/   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r0   c                     | j                  |       | j                  || j                         t        j                  j                  d       S r2   )r4   r3   r#   r   r5   r6   r7   s     r/   r9   zDynamoConfigPatchVariable.enterg  s;    b!D../))0066r0   c                 x    | j                  || j                         t        j                  j	                  d       S r2   r  rE   s      r/   rG   zDynamoConfigPatchVariable.exitm  r  r0   c                     t        |      dk(  sJ |d   }|D ]/  \  }}t        j                  j                  j	                  ||       1 y r^   )rU   r   r6  r7  r  )r,   r8   r   rz   r  rV   s         r/   r3   z$DynamoConfigPatchVariable._call_funcq  sJ    6{aq	 	7HCMM  ,,S#6	7r0   c                      y)Nztorch._dynamor(   r[   s    r/   rJ   z%DynamoConfigPatchVariable.module_name|  s    r0   c                      y)Npatch_dynamo_configr(   r[   s    r/   rK   z!DynamoConfigPatchVariable.fn_name  s    $r0   rj   rl   )rq   rr   rs   r   r*   r9   rG   r3   rJ   rK   ru   rv   s   @r/   rB  rB  Z  s%    6D777%r0   rB  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targetr2  rk   Nc                 p    t        |   di | t        |t        t        f      sJ || _        || _        y r'   )r)   r*   r_   r!   rx   r2  rN  )r,   r2  rN  r-   r.   s       r/   r*   z!WithExitFunctionVariable.__init__  sC     	"6")+IJ
 	
 
 r0   c                 D    |rJ  | j                   j                  |g| S r2   )r2  rG   r`   s       r/   ra   z&WithExitFunctionVariable.call_function  s&     ztxx}}R'$''r0   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)rR     SWAPr   )r  FPOP_TOP)r2  rN   r8   r?   partial_convertsysversion_infor2  r
   rS   r#   rT   r	   rU   r   rN  )r,   rM   rV   s      r/   rW   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rn   rm   )rq   rr   rs   r   rt   r   r!   rx   r*   ra   rW   ru   rv   s   @r/   rM  rM    so    		'	'N
*,JJK
 
(#( &( -	(
 
(Ar0   rM  )Hr   r  rY  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!   rx   r   r   r   r   r   r   r   r   r
  r(  r?  rQ  rf  rt  r  r  r  r  r  r  r  r  r'  rB  rM  r(   r0   r/   <module>rh     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r0   