o
    Zh                     @   s  d Z ddlZddlZddlZddlZddlmZmZ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& ddl'm(Z( erxddl)m*Z* ej+G dd dZ,G dd de Z-G dd de(Z.G dd de-Z/G dd de-Z0G dd de-Z1G dd de-Z2G dd  d e-Z3G d!d" d"e-Z4G d#d$ d$e-Z5G d%d& d&e-Z6G d'd( d(e-Z7G d)d* d*e-Z8G d+d, d,e-Z9G d-d. d.e-Z:G d/d0 d0e-Z;G d1d2 d2e-Z<G d3d4 d4e-Z=G d5d6 d6e-Z>G d7d8 d8e-Z?G d9d: d:e-Z@G d;d< d<e-ZAG d=d> d>e-ZBG d?d@ d@e-ZCG dAdB dBe ZDG dCdD dDe ZEG dEdF dFe ZFdS )Ga)  
This file contains a collection of context manager classes used by Dynamo for tracking
and managing various PyTorch runtime states during graph compilation. These context
managers handle different aspects of PyTorch's execution environment, including:

- Autograd states (grad mode, inference mode)
- CUDA streams and events
- Profiling contexts
- Deterministic algorithms
- Forward/backward AD modes
- SDPA (Scaled Dot Product Attention) kernels
- FSDP (Fully Sharded Data Parallel) states
- AMP (Automatic Mixed Precision) autocast states

The context managers ensure proper state transitions during graph compilation by
tracking enter/exit points and managing cleanup operations. They help maintain
consistency between eager execution and compiled graph behavior by capturing and
restoring state changes.
    N)CallableOptionalTYPE_CHECKINGUnion)Guard   )graph_break_hints	variables)create_call_functioncreate_instructioncreate_setup_with)get_interface_for_device)unimplemented_v2)GuardBuilderinstall_guard)
AttrSourceGlobalStateSource   )VariableTracker)NestedUserFunctionVariableUserFunctionVariableUserMethodVariableWrappedUserFunctionVariableWrappedUserMethodVariable)UserDefinedObjectVariable)InstructionTranslatorc                   @   sF   e Zd ZU dZdZee ed< dZee	j
j ed< dd Zdd ZdS )	ContextManagerStatez
    Mutating `self` in VariableTracker is not allowed because we copy
    them.  This is a mutable container pointed to by context managers
    that won't get copied, so it is safe to mutate.
    N
cleanup_fnproxyc                 C   s    | j d ur|    d | _ d S d S N)r   self r"   R/var/www/auris/lib/python3.10/site-packages/torch/_dynamo/variables/ctx_manager.pycleanupD   s   

zContextManagerState.cleanupc                 C   s   | j sJ d|   d S )Nzmultiple exits?)r   r$   r    r"   r"   r#   cleanup_assertI      z"ContextManagerState.cleanup_assert)__name__
__module____qualname____doc__r   r   r   __annotations__r   torchZfxZProxyr$   r%   r"   r"   r"   r#   r   9   s   
 r   c                       s   e Zd ZddddhejZ	d%dd	d& fdd	Zd
d Zd%d'ddZd'ddZdd Z	dd Z
dd Zdd Z								d(dd Zd!d" Zd#d$ Z  ZS ))ContextWrappingVariablecm_objtarget_valuesinitial_valuesstateN)r1   returnc                   s<   t  jdi | || _|| _|d u rt | _d S || _d S Nr"   )super__init__r/   r0   r   r1   )r!   r/   r0   r1   kwargs	__class__r"   r#   r5   W   s   z ContextWrappingVariable.__init__c                 C   s$   |  || j | | tjd S r   )
_call_funcr/   set_cleanup_hookr	   ConstantVariablecreater!   txr"   r"   r#   enter_   s   
zContextWrappingVariable.enterr>   r   c                    s2   |d u r fdd}| j _j j j d S )Nc                      s      j d S r   )r9   r0   r"   r=   r"   r#   fng   s   z4ContextWrappingVariable.set_cleanup_hook.<locals>.fn)r1   r   outputZadd_cleanup_hookr$   )r!   r>   r@   r"   r=   r#   r:   d   s   z(ContextWrappingVariable.set_cleanup_hookc                 G   s   | j   tjd S r   )r1   r%   r	   r;   r<   r!   r>   argsr"   r"   r#   exitm   s   
zContextWrappingVariable.exitc                 C   s"   |t |j|  |   d S r   )r   r>   Zimport_sourcemodule_namefn_namer!   codegenr"   r"   r#   reconstruct_typeq   s   z(ContextWrappingVariable.reconstruct_typec                    sR      fdd j}|sd}  fdd|D   tt|d d S )Nc                      s
     S r   )rI   r"   rH   r!   r"   r#   <lambda>w   s   
 z5ContextWrappingVariable.reconstruct.<locals>.<lambda>r"   c                       g | ]}  |qS r"   Zcreate_load_const.0valrH   r"   r#   
<listcomp>{       z7ContextWrappingVariable.reconstruct.<locals>.<listcomp>F)add_push_nullr/   extend_outputr
   len)r!   rH   r/   r"   rJ   r#   reconstructv   s   z#ContextWrappingVariable.reconstructc                 C      t d)Nzmodule_name called on baseNotImplementedErrorr    r"   r"   r#   rE   ~      z#ContextWrappingVariable.module_namec                 C   rX   )Nzfn_name called on baserY   r    r"   r"   r#   rF      r[   zContextWrappingVariable.fn_namerC   list[VariableTracker]r6   dict[str, VariableTracker]r   c                 C   s   t |dksJ t|d trt|d  |d< t|d ttfs$J t|d tr2t|d | S t|d tr@t|d | S d S Nr   r   )rV   
isinstancer   r   Zget_functionr   r   r   r!   r>   rC   r6   r"   r"   r#   call_function   s   z%ContextWrappingVariable.call_functionc                 C      dS NTr"   r    r"   r"   r#   supports_graph_breaks      z-ContextWrappingVariable.supports_graph_breaksc                 C   rb   rc   r"   r    r"   r"   r#   exit_on_graph_break   re   z+ContextWrappingVariable.exit_on_graph_breakr   r2   Nr>   r   r>   r   rC   r\   r6   r]   r2   r   )r'   r(   r)   r   _nonvar_fieldsr5   r?   r:   rD   rI   rW   rE   rF   ra   rd   rf   __classcell__r"   r"   r7   r#   r-   N   s@    	
	
r-   c                       sP   e Zd Zd fddZdd Zdd Zd	d
 ZdddZdd Zdd Z	  Z
S )GenericContextWrappingVariabler2   Nc                    s0   |d usJ t  jd||jd| || _d S )N)valueZ
value_typer"   )r4   r5   r8   r.   )r!   r.   r6   r7   r"   r#   r5      s   
z'GenericContextWrappingVariable.__init__c                 C   s   | j jS r   )r.   r(   r    r"   r"   r#   rE      r[   z*GenericContextWrappingVariable.module_namec                 C   s   t | jjS r   )typer.   r'   r    r"   r"   r#   rF         z&GenericContextWrappingVariable.fn_namec                 C   s:   | j d u rd nt| j d}tj| jjj| |d|g i S )N	__enter__source)rr   r   r	   r   r.   rp   __func__ra   )r!   r>   rr   r"   r"   r#   r?      s   
z$GenericContextWrappingVariable.enterr>   r   c                 G   sH   | j d u rd nt| j d}tj| jjj| |d||i }|j	  |S )N__exit__rq   )
rr   r   r	   r   r.   rt   rs   ra   Zactive_generic_context_managerspop)r!   r>   rC   rr   xr"   r"   r#   rD      s   

z#GenericContextWrappingVariable.exitc                 C   rb   )NFr"   r    r"   r"   r#   rd      re   z4GenericContextWrappingVariable.supports_graph_breaksc                 C   rb   rc   r"   r    r"   r"   r#   rf      re   z2GenericContextWrappingVariable.exit_on_graph_breakrg   rh   )r'   r(   r)   r5   rE   rF   r?   rD   rd   rf   rk   r"   r"   r7   r#   rl      s    	

rl   c                   @   0   e Zd ZdZedddZdd Zddd	Zd
S ))GradInplaceRequiresGradCtxManagerVariablez#represents torch grad requries gradr>   r   c                 K      t d|d d|S Nr/   r0   r"   )rx   r>   r/   r6   r"   r"   r#   r<         z0GradInplaceRequiresGradCtxManagerVariable.createc                    sb    j \}tjj  _tjj|  | fdd |j	dtjjj|fi  j
_tjd S )Nc                         t jj jS r   )r,   _C
_functorch!set_inplace_requires_grad_allowed
prev_stater"   r    r"   r#   rK      s    zAGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>ra   )r/   r,   r   r   Z!get_inplace_requires_grad_allowedr   r   r:   rA   create_noder1   r   r	   r;   r<   )r!   r>   enabledr"   r    r#   r?      s   
z/GradInplaceRequiresGradCtxManagerVariable.enterc                 G   s2   | j   |jdtjjj| jfi  t	j
d S Nra   )r1   r$   rA   r   r,   r   r   r   r   r	   r;   r<   rB   r"   r"   r#   rD      s   
z.GradInplaceRequiresGradCtxManagerVariable.exitNrh   r'   r(   r)   r*   staticmethodr<   r?   rD   r"   r"   r"   r#   rx      s    rx   c                   @   rw   )0TemporarilyPopInterpreterStackCtxManagerVariablezJrepresents torch._functorch.pyfunction.temporarily_pop_interpreter_stack()r>   r   c                 K   ry   rz   )r   r|   r"   r"   r#   r<      r}   z7TemporarilyPopInterpreterStackCtxManagerVariable.createc                    sJ   t jj  _ | fdd |jdt jjjdi  j_	t
jd S )Nc                      r~   r   )r,   r   r   push_dynamic_layer_stacksavedr"   r    r"   r#   rK          zHTemporarilyPopInterpreterStackCtxManagerVariable.enter.<locals>.<lambda>ra   r"   )r,   r   r   Zpop_dynamic_layer_stackr   r:   rA   r   r1   r   r	   r;   r<   r=   r"   r    r#   r?      s   
z6TemporarilyPopInterpreterStackCtxManagerVariable.enterc                 G   s4   | j   |jdtjjj| j jfi  t	j
d S r   )r1   r$   rA   r   r,   r   r   r   r   r	   r;   r<   rB   r"   r"   r#   rD     s   
z5TemporarilyPopInterpreterStackCtxManagerVariable.exitNrh   r   r"   r"   r"   r#   r      s    r   c                   @   >   e Zd ZdZee ejZe	dddZ
dd Zddd	Zd
S )%JvpIncrementNestingCtxManagerVariablez5represents torch.func.jvp increment/decrement nestingr>   r   c                 K      t dd d d|}|S rz   )r   r>   r6   varr"   r"   r#   r<        z,JvpIncrementNestingCtxManagerVariable.createc                 C   sN   t | j tjj }| |dd  |jdtj	jj
di | j_tj|S )Nc                   S      t jj S r   )r,   r   eager_transformsZexit_jvp_nestingr"   r"   r"   r#   rK   *      z=JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>ra   r"   )r   _guards_singletonr,   r   r   Zenter_jvp_nestingr:   rA   r   r   Z_jvp_increment_nestingr1   r   r	   r;   r<   )r!   r>   Z	jvp_levelr"   r"   r#   r?   &  s   
z+JvpIncrementNestingCtxManagerVariable.enterc                 G   .   | j   |jdtjjjdi  tj	
d S Nra   r"   )r1   r$   rA   r   r,   r   r   Z_jvp_decrement_nestingr	   r;   r<   rB   r"   r"   r#   rD   4  
   
z*JvpIncrementNestingCtxManagerVariable.exitNrh   r'   r(   r)   r*   r   r   r   ZFUNCTORCH_STACK_MATCHr   r   r<   r?   rD   r"   r"   r"   r#   r     s    r   c                   @   rw   )SetFwdGradEnabledContextManagerzWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradr>   r   c                 K   ry   rz   )r   r|   r"   r"   r#   r<   ?  r}   z&SetFwdGradEnabledContextManager.createc                    s\    j \}tj  _tj|  | fdd |jdtjj|fi  j	_
tjd S )Nc                      s   t j jS r   )r,   r   _set_fwd_grad_enabledr   r"   r    r"   r#   rK   M      z7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>ra   )r/   r,   r   Z_is_fwd_grad_enabledr   r   r:   rA   r   r1   r   r	   r;   r<   )r!   r>   moder"   r    r#   r?   G  s   
z%SetFwdGradEnabledContextManager.enterc                 G   0   | j   |jdtjj| jfi  tj	
d S r   )r1   r$   rA   r   r,   r   r   r   r	   r;   r<   rB   r"   r"   r#   rD   W     
z$SetFwdGradEnabledContextManager.exitNrh   r   r"   r"   r"   r#   r   <  s    r   c                   @   r   )DualLevelContextManagerz;Represents torch.autograd.forward_ad.dual_level ctx managerr>   r   c                 K   s   t dd d d|S rz   )r   )r>   r6   r"   r"   r#   r<   g  r}   zDualLevelContextManager.createc                    sT   t  j tjj  _ | fdd |j	dtj
jdi  j_tj jS )Nc                      s   t jjj jdS )N)level)r,   autograd
forward_adZexit_dual_level	new_levelr"   r    r"   r#   rK   s      z/DualLevelContextManager.enter.<locals>.<lambda>ra   r"   )r   r   r,   r   r   Zenter_dual_levelr   r:   rA   r   r   Z_enter_dual_levelr1   r   r	   r;   r<   r=   r"   r    r#   r?   o  s   
zDualLevelContextManager.enterc                 G   r   r   )r1   r$   rA   r   r,   r   Z_exit_dual_levelr   r	   r;   r<   rB   r"   r"   r#   rD   }  r   zDualLevelContextManager.exitNrh   )r'   r(   r)   r*   r   r   r   Z
DUAL_LEVELr   r   r<   r?   rD   r"   r"   r"   r#   r   b  s    r   c                   @   r   )&GradIncrementNestingCtxManagerVariablez6represents torch.func.grad increment/decrement nestingr>   r   c                 K   r   rz   )r   r   r"   r"   r#   r<     r   z-GradIncrementNestingCtxManagerVariable.createc                 C   sN   t | j tjj }| |dd  |jdtjjjdi | j	_
tj|S )Nc                   S   r   r   )r,   r   r   _grad_decrement_nestingr"   r"   r"   r#   rK     r   z>GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>ra   r"   )r   r   r,   r   r   Z_grad_increment_nestingr:   rA   r   r1   r   r	   r;   r<   )r!   r>   Z
grad_levelr"   r"   r#   r?     s   
z,GradIncrementNestingCtxManagerVariable.enterc                 G   r   r   )r1   r$   rA   r   r,   r   r   r   r	   r;   r<   rB   r"   r"   r#   rD     r   z+GradIncrementNestingCtxManagerVariable.exitNrh   r   r"   r"   r"   r#   r     s    r   c                       s@   e Zd ZdZedddZd fdd	Zd
d Zdd Z  Z	S )CatchWarningsCtxManagerVariablez'Delay a call to warnings.catch_warningsr>   r   c                 C   s   t |d d dS )N)catch_warnings_argsr/   r0   )r   )r>   r   r"   r"   r#   r<     s
   z&CatchWarningsCtxManagerVariable.creater2   Nc                    s.   t |ts	J |t jdi | || _d S r3   )r_   dictr4   r5   r   )r!   r   r6   r7   r"   r#   r5     s   
z(CatchWarningsCtxManagerVariable.__init__c                    sH   dd | j  D }tjdi | | | fdd tj  S )Nc                 S   s   i | ]	\}}||  qS r"   )as_python_constant)rO   kvr"   r"   r#   
<dictcomp>  s    z9CatchWarningsCtxManagerVariable.enter.<locals>.<dictcomp>c                      s     d d d S r   )rt   r"   Zctx_valr"   r#   rK     r   z7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>r"   )	r   itemswarningscatch_warningsr:   r	   r;   r<   rp   )r!   r>   r6   r"   r   r#   r?     s   z%CatchWarningsCtxManagerVariable.enterc                    sL      fdd  | j  t| j }  t||d d S )Nc                      s     ddS )Nr   r   )Zload_import_fromr"   cgr"   r#   rK     r   z=CatchWarningsCtxManagerVariable.reconstruct.<locals>.<lambda>F)	rT   Zforeachr   valuestuplekeysrU   Zcreate_call_function_kwrV   )r!   r   r   r"   r   r#   rW     s   z+CatchWarningsCtxManagerVariable.reconstructrh   rg   )
r'   r(   r)   r*   r   r<   r5   r?   rW   rk   r"   r"   r7   r#   r     s    r   c                   @   r   )&VmapIncrementNestingCtxManagerVariablez1represents torch VMap increment/decrement nestingr>   r   c                 K   s   t d|d d|}|S rz   )r   )r>   r/   r6   r   r"   r"   r#   r<     r   z-VmapIncrementNestingCtxManagerVariable.createc                 C   s   t | j | j\}}t|tjr|j}| j}n|	 }|	 }|	 }t
jj||}| |dd  |jdt
jjj||fi | j_tj|S )Nc                   S   r   r   )r,   r   r   _vmap_decrement_nestingr"   r"   r"   r#   rK     r   z>VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>ra   )r   r   r/   r_   r	   SymNodeVariableZsym_numas_proxynoder   r,   r   r   Z_vmap_increment_nestingr:   rA   r   r1   r   r;   r<   )r!   r>   Z
batch_sizeZ
randomnessZbatch_size_valueZbatch_size_nodeZ
vmap_levelr"   r"   r#   r?     s&   

z,VmapIncrementNestingCtxManagerVariable.enterc                 G   r   r   )r1   r$   rA   r   r,   r   r   r   r	   r;   r<   rB   r"   r"   r#   rD     r   z+VmapIncrementNestingCtxManagerVariable.exitNrh   r   r"   r"   r"   r#   r     s    r   c                       s   e Zd ZdZee ejZe	ddddZ
	d	d fd
dZdd ZdddZ						d  fddZdddZdd Zdd Z  ZS )!GradModeVariablez6represents torch.{no_grad,enable_grad,set_grad_mode}()Fr>   r   c                 K   s2   t d|gt gd|}|r|| |j |S rz   )r   r,   is_grad_enabledr9   r/   )r>   target_valueinitializedr6   r   r"   r"   r#   r<     s   zGradModeVariable.createNTr2   c                    s&   t  jd||d| t| j d S rz   r4   r5   r   r   )r!   r/   r0   r   r6   r7   r"   r#   r5     s   zGradModeVariable.__init__c                 C      |  || j tjd S r   r9   r/   r	   r;   r<   r=   r"   r"   r#   r?     r&   zGradModeVariable.enterc                 G   r   r   r9   r0   r	   r;   r<   rB   r"   r"   r#   rD     r&   zGradModeVariable.exitrC   r\   r6   r]   c                       |  || j t |||S r   r9   r0   r4   ra   r`   r7   r"   r#   ra        zGradModeVariable.call_functionc                 C   sP   t |dksJ |d }t |kr&|jdtjj|fi  tj| d S d S Nr   r   ra   )rV   r,   r   rA   r   r   Z_set_grad_enabledr!   r>   r   rm   r"   r"   r#   r9   (  s   zGradModeVariable._call_funcc                 C   rb   Nr,   r"   r    r"   r"   r#   rE   2  re   zGradModeVariable.module_namec                 C   rb   )NZset_grad_enabledr"   r    r"   r"   r#   rF   5  re   zGradModeVariable.fn_name)Frh   rc   rg   r>   r   rC   r\   r6   r]   )r'   r(   r)   r*   r   r   r   Z	GRAD_MODEr   r   r<   r5   r?   rD   ra   r9   rE   rF   rk   r"   r"   r7   r#   r     s*    

	
r   c                       sT   e Zd ZedddZ	d	d fddZdd	d
Zdd Zdd Zdd Z	  Z
S )InferenceModeVariabler>   r   c                 K   s   t |gfdt i|}|S )Nr0   )r   r,   is_inference_mode_enabledr>   r   r6   r   r"   r"   r#   r<   :  s   zInferenceModeVariable.createNr2   c                    s2   |d u rt  }t jd||d| || _d S rz   )r,   r   r4   r5   r/   r!   r/   r0   r6   r7   r"   r#   r5   A  s   
zInferenceModeVariable.__init__c                 G   s,   | j   |jdtjjj| j jfi  d S r   )	r1   r%   rA   r   r,   r   	grad_mode_exit_inference_moder   rB   r"   r"   r#   rD   O  s   
zInferenceModeVariable.exitc                    sL   t jjj| j  | | fdd |jdt jjjg | jR i | j_	d S )Nc                      s   t jj S r   )r,   r   r   r   r"   ctxr"   r#   rK   [  r   z-InferenceModeVariable.enter.<locals>.<lambda>ra   )
r,   r   r   Z_enter_inference_moder/   r:   rA   r   r1   r   r=   r"   r   r#   r?   X  s   
zInferenceModeVariable.enterc                 C   rb   r   r"   r    r"   r"   r#   rE   d  re   z!InferenceModeVariable.module_namec                 C   rb   )NZinference_moder"   r    r"   r"   r#   rF   g  re   zInferenceModeVariable.fn_namerh   r   rg   r'   r(   r)   r   r<   r5   rD   r?   rE   rF   rk   r"   r"   r7   r#   r   9  s    	
	r   c                       sX   e Zd ZdZedddZ	d	d fdd	Zdd
dZdd Zdd Z	dd Z
  ZS )CUDADeviceVariablezrepresents torch.cuda.devicer>   r   c                 K   s&   t dtjj|ddgd d|}|S )NT)optionalr{   r"   )r   r,   cudaZ_get_device_index)r>   devicer6   r   r"   r"   r#   r<   n  s   zCUDADeviceVariable.createNr2   c                    "   t  jd||d| || _d S rz   r4   r5   r/   r   r7   r"   r#   r5   w  s   
zCUDADeviceVariable.__init__c                 G   s2   | j   |jdtjj| j jfi  tj	
dS )Nra   F)r1   r%   rA   r   r,   r   _maybe_exchange_devicer   r	   r;   r<   rB   r"   r"   r#   rD     s   
zCUDADeviceVariable.exitc                    H   t jj| j  | | fdd |jdt jjg | jR i | j_d S )Nc                         t j S r   )r,   r   r   r"   Zprev_idxr"   r#   rK     r   z*CUDADeviceVariable.enter.<locals>.<lambda>ra   )	r,   r   Z_exchange_devicer/   r:   rA   r   r1   r   r=   r"   r   r#   r?     s   
zCUDADeviceVariable.enterc                 C   rb   )Nz
torch.cudar"   r    r"   r"   r#   rE     re   zCUDADeviceVariable.module_namec                 C   rb   )Nr   r"   r    r"   r"   r#   rF     re   zCUDADeviceVariable.fn_namerh   r   rg   )r'   r(   r)   r*   r   r<   r5   rD   r?   rE   rF   rk   r"   r"   r7   r#   r   k  s    


r   c                       sR   e Zd ZdZee ejZe	dddZ
dd fdd	Zd
d ZdddZ  ZS )TorchFunctionDisableVariablez>represents whether torch function overrides are enabled or notr>   r   c                 K   s8   t ddg| jjgd|}|| dg ||  |S )NFr{   r"   )r   rA   Ztorch_function_enabledr9   r:   r   r"   r"   r#   r<     s   
z#TorchFunctionDisableVariable.createNr2   c                    &   t  jd||d| t| j d S rz   r   r   r7   r"   r#   r5        z%TorchFunctionDisableVariable.__init__c                 C      t jd S r   r	   r;   r<   r=   r"   r"   r#   r?     ro   z"TorchFunctionDisableVariable.enterc                 C   s<   t |dksJ |d |j_|d |j_|j|d  d S r^   )rV   Zsymbolic_torch_function_stateZtorch_function_subclass_enabledZtorch_function_mode_enabledrA   Zset_torch_function_state)r!   r>   r   r"   r"   r#   r9     s   z'TorchFunctionDisableVariable._call_funcrh   r   rg   )r'   r(   r)   r*   r   r   r   ZTORCH_FUNCTION_STATEr   r   r<   r5   r?   r9   rk   r"   r"   r7   r#   r     s    r   c                       sb   e Zd ZdZee ejZe	dddZ
dd fdd	Zd
d ZdddZdd Zdd Z  ZS )DeterministicAlgorithmsVariablezVrepresents torch.{are_deterministic_algorithms_enabled,use_deterministic_algorithms}()r>   r   c                 K   s8   t d|gt gd|}|| |g ||  |S rz   )r   r,   Z$are_deterministic_algorithms_enabledr9   r:   r   r"   r"   r#   r<     s   
z&DeterministicAlgorithmsVariable.createNr2   c                    r   rz   r   r   r7   r"   r#   r5     r   z(DeterministicAlgorithmsVariable.__init__c                 C   r   r   r   r=   r"   r"   r#   r?     ro   z%DeterministicAlgorithmsVariable.enterc                 C   sB   t |dksJ |d }|jdtjj|fi f tj| d S r   )rV   rA   r   r,   r   Z_set_deterministic_algorithmsr   r"   r"   r#   r9     s   z*DeterministicAlgorithmsVariable._call_funcc                 C   rb   r   r"   r    r"   r"   r#   rE     re   z+DeterministicAlgorithmsVariable.module_namec                 C   rb   )NZuse_deterministic_algorithmsr"   r    r"   r"   r#   rF     re   z'DeterministicAlgorithmsVariable.fn_namerh   r   rg   )r'   r(   r)   r*   r   r   r   ZDETERMINISTIC_ALGORITHMSr   r   r<   r5   r?   r9   rE   rF   rk   r"   r"   r7   r#   r     s    


r   c                       sT   e Zd ZdZedddZdd fdd	Zd
d ZdddZdd Z	dd Z
  ZS )!DisabledSavedTensorsHooksVariablez;represents torch.autograd.graph.disable_saved_tensors_hook.r>   r   c                 K   s<   t d|gtjj gd|}|| |g ||  |S rz   )r   r,   r   	_autogradZ/_saved_tensors_hooks_get_disabled_error_messager9   r:   r   r"   r"   r#   r<     s   

z(DisabledSavedTensorsHooksVariable.createNr2   c                       t  jd||d| d S rz   r4   r5   r   r7   r"   r#   r5     s
   
z*DisabledSavedTensorsHooksVariable.__init__c                 C   r   r   r   r=   r"   r"   r#   r?     ro   z'DisabledSavedTensorsHooksVariable.enterc                 C   st   t |dksJ |d }|d ur&|jdtjjj|fi  tjj| d S |jdtjjjdi  tjj  d S )Nr   r   ra   r"   )rV   rA   r   r,   r   r   Z_saved_tensors_hooks_disableZ_saved_tensors_hooks_enabler   r"   r"   r#   r9     s   z,DisabledSavedTensorsHooksVariable._call_funcc                 C   rb   )Nztorch.autograd.graphr"   r    r"   r"   r#   rE     re   z-DisabledSavedTensorsHooksVariable.module_namec                 C   rb   )NZdisable_saved_tensors_hooksr"   r    r"   r"   r#   rF     re   z)DisabledSavedTensorsHooksVariable.fn_namerh   r   rg   )r'   r(   r)   r*   r   r<   r5   r?   r9   rE   rF   rk   r"   r"   r7   r#   r     s    
r   c                       sN   e Zd Zedd Zdd fddZdd	d
Zdd Zdd Zdd Z	  Z
S )AutocastModeVariablec                 C   s   | t jjjt jjjt jjjfv sJ t| j|i |}|	  g }|
  dD ]5}|dkrG| t jjjt jjjfv rG| t jjju rDdnd}n|j| }t|trY||  q)|| q)t|fdd i|}|S )N)device_typeZdtyper   Zcache_enabledr   r   cpur0   )r,   ampZautocast_modeautocastr   r   inspect	signaturebindapply_defaultsclear	argumentsr_   r   appendr   r   )funcrC   r6   Z
bound_argsr/   keyargr   r"   r"   r#   r<     s*   
	


zAutocastModeVariable.createNr2   c                    r   rz   r   r   r7   r"   r#   r5   ?  s   
zAutocastModeVariable.__init__r>   r   c                 G   s*   | j   |jdtjj| j jfi  d S r   )r1   r%   rA   r   r,   r   _exit_autocastr   rB   r"   r"   r#   rD   E  s   
zAutocastModeVariable.exitc                    r   )Nc                      r   r   )r,   r   r   r"   r   r"   r#   rK   M  r   z,AutocastModeVariable.enter.<locals>.<lambda>ra   )	r,   r   Z_enter_autocastr/   r:   rA   r   r1   r   r=   r"   r   r#   r?   K  s
   zAutocastModeVariable.enterc                 C   rb   )Nztorch.amp.autocast_moder"   r    r"   r"   r#   rE   R  re   z AutocastModeVariable.module_namec                 C   rb   )Nr   r"   r    r"   r"   r#   rF   U  re   zAutocastModeVariable.fn_namer   rg   rh   r   r"   r"   r7   r#   r     s    

r   c                       sF   e Zd ZdZdd fddZdd Zdd
dZdd Zdd Z  Z	S )NullContextVariablez>
    This class represents Python contextlib.nullcontext.
    Nr2   c                    s   t  jdd|i| d S Nr/   r"   r   )r!   r/   r6   r7   r"   r#   r5   ^     zNullContextVariable.__init__c                 C   r   r   r   r=   r"   r"   r#   r?   a  ro   zNullContextVariable.enterr>   r   c                 G   r   r   r   rB   r"   r"   r#   rD   d  ro   zNullContextVariable.exitc                 C   rb   N
contextlibr"   r    r"   r"   r#   rE   g  re   zNullContextVariable.module_namec                 C   rb   Nnullcontextr"   r    r"   r"   r#   rF   j  re   zNullContextVariable.fn_namer   rg   rh   )
r'   r(   r)   r*   r5   r?   rD   rE   rF   rk   r"   r"   r7   r#   r   Y  s    
r   c                       sL   e Zd ZdZd fddZdd Zdd
dZdd Zdd Zdd Z	  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.
    r2   Nc                    s   t  jddd i| d S r   r   )r!   r6   r7   r"   r#   r5   w  r   z ProfilerContextVariable.__init__c                 C   s   | S r   r"   r=   r"   r"   r#   r?   z  re   zProfilerContextVariable.enterr>   r   c                 G   r   r   r   rB   r"   r"   r#   rD   }  ro   zProfilerContextVariable.exitc                 C   rb   r   r"   r    r"   r"   r#   rE     re   z#ProfilerContextVariable.module_namec                 C   rb   r   r"   r    r"   r"   r#   rF     re   zProfilerContextVariable.fn_namec                 C      t dt| dg tjd d S )Nz2torch.profiler object escaped from compiled regionzXDynamo doesn't support compiling a region that returns a torch.profiler context manager.Zgb_typecontextZexplanationhintsr   strr   SUPPORTABLE)r!   r   r"   r"   r#   rW     s   
z#ProfilerContextVariable.reconstructrg   rh   )r'   r(   r)   r*   r5   r?   rD   rE   rF   rW   rk   r"   r"   r7   r#   r   n  s    
r   c                       s@   e Zd ZedddZdd fddZd	d
 ZdddZ  ZS )StreamContextVariabler>   r   c              	   K   sN   ddl m} t|jj}|t| | jd|di }td|g|g|jd|S )Nr   wrap_fx_proxy_clsra   r   )r/   r0   r   r"   )	builderr
  r   r   current_streamStreamVariablerA   create_proxyr  )r>   r   r6   r
  Zcurrent_stream_methodr  r"   r"   r#   r<     s.   
zStreamContextVariable.createNr2   c                    s>   t  jd||d| || _t| jj| _t| jj| _d S rz   )r4   r5   r   r   
set_streamZ_set_stream_by_idset_stream_id)r!   r/   r   r0   r6   r7   r"   r#   r5     s   zStreamContextVariable.__init__c                    s    j d  d ur|jd j j d  fi  n j d j}|jd j|j|j|j	fi    j d j  
| fdd d S )Nr   ra   c                      s      jd jS )Nr   )r  r0   rm   r"   r    r"   r#   rK     r   z-StreamContextVariable.enter.<locals>.<lambda>)r/   r   rA   r  r  rm   r  Z	stream_idZdevice_indexr   r:   )r!   r>   streamr"   r    r#   r?     s    zStreamContextVariable.enterc                 G   s.   |j d| j| jd  fi  | j  d S )Nra   r   )rA   r  r  r0   r   r1   r%   rB   r"   r"   r#   rD     s   zStreamContextVariable.exitrh   r   rg   )	r'   r(   r)   r   r<   r5   r?   rD   rk   r"   r"   r7   r#   r    s    r  c                       sT   e Zd ZdZedd Zedd Zd fdd	Zd
d ZdddZ	dd Z
  ZS )PreserveVersionContextVariablez?
    Wraps torch.autograd._unsafe_preserve_version_counter
    c                    sX   t |tjrt fdd|fD }t|g}nt fdd|jD }t||S )Nc                       g | ]}|  d qS _versionZvar_getattrrO   rv   r>   r"   r#   rR         zNPreserveVersionContextVariable._create_lambda_from_tensors.<locals>.<listcomp>c                    r  r  r  r  r  r"   r#   rR     r  )r_   r	   TensorVariableTupleVariabler   r  )r>   tensorsversionsr"   r  r#   _create_lambda_from_tensors  s   
z:PreserveVersionContextVariable._create_lambda_from_tensorsc                    s   t  fddS )Nc                    s   t  | S r   )r  r  )r  r  r"   r#   rK     s    z<PreserveVersionContextVariable.constructor.<locals>.<lambda>)r	   ZLambdaVariabler  r"   r  r#   constructor  s   
z*PreserveVersionContextVariable.constructorr2   Nc                    st   | dd  t jdi | || _|| _t| jtjr$t| jg| _t| jtj	tj
fr8t| jg| _d S d S r   )
setdefaultr4   r5   r  prev_versionsr_   r	   r  r  r;   r   )r!   r  r!  r6   r7   r"   r#   r5     s   z'PreserveVersionContextVariable.__init__c                 C   s   d S r   r"   r=   r"   r"   r#   r?     re   z$PreserveVersionContextVariable.enterr>   r   c                 G   s(   ddl m} t||| j| jgi S )Nr   )_unsafe_set_version_counter)Ztensor_version_opr"  r	   ZTorchInGraphFunctionVariablera   r  r!  )r!   r>   rC   r"  r"   r"   r#   rD     s   z#PreserveVersionContextVariable.exitc                 C   r  )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  rG   r"   r"   r#   rW     s   
z*PreserveVersionContextVariable.reconstructrg   rh   )r'   r(   r)   r*   r   r  r  r5   r?   rD   rW   rk   r"   r"   r7   r#   r    s    


r  c                       s   e Zd Zee ejZedddZ		d	d fddZ
d	d
 ZdddZ						d fddZdddZdd Zdd Z  ZS )&FSDPParamGroupUseTrainingStateVariabler>   r   c                 K   s"   t d||g|jjgd|}|S )N)param_group_varr/   r0   r"   )r#  rm   _training_state)r>   r$  r   r6   r   r"   r"   r#   r<     s   z-FSDPParamGroupUseTrainingStateVariable.createNr2   c                    s,   t  jd||d| || _t| j d S rz   )r4   r5   r$  r   r   )r!   r$  r/   r0   r6   r7   r"   r#   r5     s   z/FSDPParamGroupUseTrainingStateVariable.__init__c                 C   r   r   r   r=   r"   r"   r#   r?   &  r&   z,FSDPParamGroupUseTrainingStateVariable.enterc                 G   r   r   r   rB   r"   r"   r#   rD   *  r&   z+FSDPParamGroupUseTrainingStateVariable.exitrC   r\   r6   r]   c                    r   r   r   r`   r7   r"   r#   ra   .  r   z4FSDPParamGroupUseTrainingStateVariable.call_functionc                 C   s\   t |dksJ |d }| jjj|kr,| j|dtjdt|fi  || jj_d S d S )Nr   r   __setattr__r%  )	rV   r$  rm   r%  call_methodr	   r;   r<   ZEnumVariabler   r"   r"   r#   r9   7  s   
	z1FSDPParamGroupUseTrainingStateVariable._call_funcc                 C   rb   )NzDtorch.distributed.fsdp._fully_shard._fsdp_param_group.FSDPParamGroupr"   r    r"   r"   r#   rE   F  re   z2FSDPParamGroupUseTrainingStateVariable.module_namec                 C   rb   )NZuse_training_stater"   r    r"   r"   r#   rF   I  re   z.FSDPParamGroupUseTrainingStateVariable.fn_namerh   r   rg   r   )r'   r(   r)   r   r   r   ZFSDP_TRAINING_STATEr   r   r<   r5   r?   rD   ra   r9   rE   rF   rk   r"   r"   r7   r#   r#    s(    
	

	r#  c                       st   e Zd ZdZedddZ	ddeejj	j
 ddf fd	d
Zedd Zdd ZdddZdd Zdd Z  ZS )SDPAKernelVariablez)represents torch.nn.attention.sdpa_kernelr>   r   c                 K   s.   t |tjjjr|g}td|d d|}|S rz   )r_   r,   nn	attention
SDPBackendr(  )r>   backendsr6   r   r"   r"   r#   r<   P  s   zSDPAKernelVariable.createNr/   r2   c                    r   rz   r   r   r7   r"   r#   r5   [  s
   
zSDPAKernelVariable.__init__c                    s    fdd|D }|S )Nc                    s(   g | ]} j d tjjj|jfi qS )ra   )rA   r   r,   r)  r*  Z_backend_from_stringname)rO   backendr  r"   r#   rR   h  s    z9SDPAKernelVariable._backends_to_nodes.<locals>.<listcomp>r"   )r>   r,  nodesr"   r  r#   _backends_to_nodese  s   
	z%SDPAKernelVariable._backends_to_nodesc                    sf   t jj  _ | fdd t jj j  | j}|j	
dt jjj|fi  tjd S )Nc                      r~   r   )r,   r)  r*  _sdpa_kernelprev_backendsr"   r    r"   r#   rK   v  r   z*SDPAKernelVariable.enter.<locals>.<lambda>ra   )r,   r)  r*  Z_cur_sdpa_kernel_backendsr2  r:   r1  r/   r0  rA   r   r	   r;   r<   )r!   r>   r   r"   r    r#   r?   s  s   zSDPAKernelVariable.enterc                 G   s>   | j   | || j}|jdtjjj	|fi  t
jd S r   )r1   r%   r0  r2  rA   r   r,   r)  r*  r1  r	   r;   r<   )r!   r>   rC   r   r"   r"   r#   rD     s   
zSDPAKernelVariable.exitc                 C   rb   )Nztorch.nn.attentionr"   r    r"   r"   r#   rE     re   zSDPAKernelVariable.module_namec                 C   rb   )NZ_sdpa_kernel_variadicr"   r    r"   r"   r#   rF     re   zSDPAKernelVariable.fn_namerh   r   )r'   r(   r)   r*   r   r<   listr,   r)  r*  r+  r5   r0  r?   rD   rE   rF   rk   r"   r"   r7   r#   r(  M  s     


r(  c                       sP   e Zd Zd fddZdd Z					
		d fddZdd Zdd Z  ZS )r  r2   Nc                    sf   |d urd|j jv r|j jd |ksJ |jj|jksJ dt jdi | || _|| _|| _d S )Nexample_valuez.stream value is not equal to the passed devicer"   )r   metar   rn   r4   r5   r   rm   )r!   r   rm   r   r6   r7   r"   r#   r5     s   
zStreamVariable.__init__c                 C   s   t jS r   )r,   ZStreamr    r"   r"   r#   python_type     zStreamVariable.python_typerC   r\   r6   r]   r   c           	         s6  t | j|sJ d| ddlm}m} ddlm} |dv r5|jjd|g|| g| |R   t	
d S |dkrP|t	j
||jjd|g|| g| |R  d	S |d
krj|t||jjd|g|| g| |R  d	S ||v rt|dkr|s|d }t|tst	j
tS t	j
|| | j|jS t ||||S )Nzno stream method found named r   )cmp_name_to_op_mappingproxy_args_kwargsr   r	  )Zwait_streamsynchronizeZ
wait_eventr'  queryZ
target_clsr>   r   Zrecord_eventr   )hasattrrm   utilsr8  r9  r  r
  rA   r  r	   r;   EventVariablerV   r_   r  r<   NotImplementedr4   r'  )	r!   r>   r-  rC   r6   r8  r9  r
  otherr7   r"   r#   r'    sL   

zStreamVariable.call_methodc                 C      | j S r   r   r    r"   r"   r#   r     r7  zStreamVariable.as_proxyc                 C   s@   | j rJ d| j }|jj|| j}||j|dd d S )NZ_stream_Tadd)rr   r   r>   rA   install_global_by_idrm   append_outputcreate_load_globalr!   rH   prefixr-  r"   r"   r#   rW     s   
zStreamVariable.reconstructrg   rC   r\   r6   r]   r2   r   )	r'   r(   r)   r5   r6  r'  r   rW   rk   r"   r"   r7   r#   r    s    -r  c                       sD   e Zd Zd fddZ							dd
dZdd Zdd Z  ZS )r?  r2   Nc                    sJ   |d urd|j jv r|j jd |ksJ t jdi | || _|| _d S )Nr4  r"   )r   r5  r4   r5   r   rm   )r!   r   rm   r6   r7   r"   r#   r5     s
   
zEventVariable.__init__rC   r\   r6   r]   r   c                 C   s   ddl m} ddlm} |dv r&|jjd|g|| g| |R   td S |dkrA|tj||jjd|g|| g| |R  dS td	t	|d
| dfg t
jd d S )Nr   )r9  r   r	  )waitrecordr:  r'  r;  r<  z#Unsupported torch.cuda.Event methodz4Dynamo doesn't support tracing the torch.cuda.Event.zC method. We currently support wait, record, synchronize, and query.r  )r>  r9  r  r
  rA   r  r	   r;   r   r  r   r  )r!   r>   r-  rC   r6   r9  r
  r"   r"   r#   r'    s8   


zEventVariable.call_methodc                 C   rB  r   rC  r    r"   r"   r#   r     r7  zEventVariable.as_proxyc                 C   s8   | j rJ d}|jj|| j}||j|dd d S )NZ_eventTrD  )rr   r>   rA   rF  rm   rG  rH  rI  r"   r"   r#   rW     s   
zEventVariable.reconstructrg   rK  )r'   r(   r)   r5   r'  r   rW   rk   r"   r"   r7   r#   r?    s    
$r?  c                       s\   e Zd ZdhejZdeeef ddf fddZ					
				dddZ	dd Z
  ZS )WithExitFunctionVariabletargetr   r2   Nc                    s4   t  jdi | t|ttfsJ || _|| _d S r3   )r4   r5   r_   r-   rl   r   rO  )r!   r   rO  r6   r7   r"   r#   r5   #  s   
z!WithExitFunctionVariable.__init__r>   r   rC   r\   r6   r]   r   c                 C   s   |rJ | j j|g|R  S r   )r   rD   r`   r"   r"   r#   ra   0  s   z&WithExitFunctionVariable.call_functionc                    s   | j    jjjrPtjdkr% td tjdk r% tddd  	 fdd| j j
D   	tt| j j
d	  t| j  td
 d S d S )N)      Z	PUSH_NULL)rP     ZSWAPr   )r   c                    rL   r"   rM   rN   rQ   r"   r#   rR   D  rS   z8WithExitFunctionVariable.reconstruct.<locals>.<listcomp>FPOP_TOP)r   rI   r>   rA   Zpartial_convertsysversion_inforG  r   rU   r/   r
   rV   r   rO  rG   r"   rQ   r#   rW   9  s   


z$WithExitFunctionVariable.reconstructri   )r'   r(   r)   r   rj   r   r-   rl   r5   ra   rW   rk   r"   r"   r7   r#   rN    s(    

	rN  )Gr*   dataclassesr   rT  r   typingr   r   r   r   Ztorch._Cr,   Ztorch._guardsr    r   r	   Zbytecode_transformationr
   r   r   Zdevice_interfacer   excr   Zguardsr   r   rr   r   r   baser   Z	functionsr   r   r   r   r   Zuser_definedr   Ztorch._dynamo.symbolic_convertr   	dataclassr   r-   rl   rx   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r#  r(  r  r?  rN  r"   r"   r"   r#   <module>   s^   N+($)&&'1:22!,4;#@?=IN9