U
    zh                     @   s8  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 ddlmZmZmZ ddlmZ ddlmZmZ dd	lmZmZ dd
lmZmZ ddlmZ ddl m!Z!m"Z"m#Z#m$Z$m%Z% 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(Z+G dd de(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 d0d1 d1e(Z8G d2d3 d3e(Z9G d4d5 d5eZ:G d6d7 d7eZ;G d8d9 d9eZ<dS ):    N)CallableDictListOptional)Guard   )	variables)create_call_functioncreate_instructioncreate_setup_with)get_interface_for_device)unimplementedUnsupported)GuardBuilderinstall_guard)
AttrSourceGlobalStateSource   )VariableTracker)NestedUserFunctionVariableUserFunctionVariableUserMethodVariableWrappedUserFunctionVariableWrappedUserMethodVariablec                   @   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 )	ContextMangerStatez
    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 k	r|    d | _ d S N)r   self r    U/var/www/html/venv/lib/python3.8/site-packages/torch/_dynamo/variables/ctx_manager.pycleanup*   s    
zContextMangerState.cleanupc                 C   s   | j std|   d S )Nzmultiple exits?)r   AssertionErrorr"   r   r    r    r!   cleanup_assert/   s    z!ContextMangerState.cleanup_assert)__name__
__module____qualname____doc__r   r   r   __annotations__r   torchZfxZProxyr"   r$   r    r    r    r!   r      s
   
r   c                       s   e Zd ZddddhejZddd fddZd	d
 ZdddZdd Zdd Z	dd Z
dd Zdd ZddddddZ  ZS )ContextWrappingVariablecm_objtarget_valuesinitial_valuesstateN)r/   c                   s2   t  jf | || _|| _|d kr(t n|| _d S r   )super__init__r-   r.   r   r/   )r   r-   r.   r/   kwargs	__class__r    r!   r1   =   s    z ContextWrappingVariable.__init__c                 C   s$   |  || j | | tjd S r   )
_call_funcr-   set_cleanup_hookr   ConstantVariablecreater   txr    r    r!   enterC   s    
zContextWrappingVariable.enterc                    s2   |d kr fdd}| j _j j j d S )Nc                      s      j d S r   )r5   r.   r    r9   r    r!   fnK   s    z4ContextWrappingVariable.set_cleanup_hook.<locals>.fn)r/   r   outputZadd_cleanup_hookr"   )r   r:   r<   r    r9   r!   r6   H   s    z(ContextWrappingVariable.set_cleanup_hookc                 G   s   | j   tjd S r   )r/   r$   r   r7   r8   r   r:   argsr    r    r!   exitQ   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_typeU   s    z(ContextWrappingVariable.reconstruct_typec                    s`   t jdkr td |   | j}|s0d}  fdd|D   tt|d d S )N      	PUSH_NULLr    c                    s   g | ]}  |qS r    Zcreate_load_const.0valrD   r    r!   
<listcomp>a   s     z7ContextWrappingVariable.reconstruct.<locals>.<listcomp>F)	sysversion_infoappend_outputr
   rE   r-   extend_outputr	   len)r   rD   r-   r    rN   r!   reconstructZ   s    

z#ContextWrappingVariable.reconstructc                 C   s   t dd S )Nzmodule_name called on baseNotImplementedErrorr   r    r    r!   rA   d   s    z#ContextWrappingVariable.module_namec                 C   s   t dd S )Nzfn_name called on baserV   r   r    r    r!   rB   g   s    zContextWrappingVariable.fn_nameList[VariableTracker]Dict[str, VariableTracker]r   r?   r2   returnc                 C   s   t |dkstt|d tr2t|d  |d< t|d ttfsHtt|d trdt|d | S t|d trt|d | S d S Nr   r   )	rT   r#   
isinstancer   r   Zget_functionr   r   r   r   r:   r?   r2   r    r    r!   call_functionj   s    z%ContextWrappingVariable.call_function)N)N)r%   r&   r'   r   _nonvar_fieldsr1   r;   r6   r@   rE   rU   rA   rB   r_   __classcell__r    r    r3   r!   r+   4   s&   
	
 r+   c                       s4   e Zd Zd	dd fddZdd Zdd Z  ZS )
GenericContextWrappingVariableN)r,   c                   s.   |d k	st t jf ||d| || _d S Nr-   r.   )r#   r0   r1   r,   )r   r-   r.   r,   r2   r3   r    r!   r1   z   s     z'GenericContextWrappingVariable.__init__c              
   C   s   | j d krd n
t| j d}z*tj| jjjt| j|d|g i W S  t	k
r| } zt
d| j d|d W 5 d }~X Y nX d S )N	__enter__sourceUnsupported context manager z's __enter__ functionZfrom_exc)rg   r   r   r   r,   re   __func__UserDefinedObjectVariabler_   r   r   )r   r:   rg   er    r    r!   r;      s     
  z$GenericContextWrappingVariable.enterc              
   G   s   | j d krd n
t| j d}zJtj| jjjt| j|d|tj	
d tj	
d tj	
d gi }W n8 tk
r } ztd| j d|d W 5 d }~X Y nX | jd8  _|S )N__exit__rf   rh   z's __exit__ functionri   r   )rg   r   r   r   r,   rm   rj   rk   r_   r7   r8   r   r   Zgeneric_context_manager_depth)r   r:   r?   rg   xrl   r    r    r!   r@      s*    



z#GenericContextWrappingVariable.exit)N)r%   r&   r'   r1   r;   r@   ra   r    r    r3   r!   rb   y   s   rb   c                   @   s,   e Zd ZdZedd Zdd Zdd ZdS )	)GradInplaceRequiresGradCtxManagerVariablez#represents torch grad requries gradc                 K   s   t f |d d|S rc   )ro   r:   r-   r2   r    r    r!   r8      s    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                      s   t jj jS r   )r*   _C
_functorch!set_inplace_requires_grad_allowed
prev_stater    r   r    r!   <lambda>   s   zAGradInplaceRequiresGradCtxManagerVariable.enter.<locals>.<lambda>r_   )r-   r*   rq   rr   Z!get_inplace_requires_grad_allowedrt   rs   r6   r=   create_noder/   r   r   r7   r8   )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 Nr_   )r/   r"   r=   rv   r*   rq   rr   rs   rt   r   r7   r8   r>   r    r    r!   r@      s    
z.GradInplaceRequiresGradCtxManagerVariable.exitNr%   r&   r'   r(   staticmethodr8   r;   r@   r    r    r    r!   ro      s
   
ro   c                   @   s:   e Zd ZdZee ejZe	dd Z
dd Zdd ZdS )	%JvpIncrementNestingCtxManagerVariablez5represents torch.func.jvp increment/decrement nestingc                 K   s   t f d d d|}|S rc   )r{   r:   r2   varr    r    r!   r8      s    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   s   t jj S r   )r*   rr   eager_transformsZexit_jvp_nestingr    r    r    r!   ru          z=JvpIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>r_   r    )r   _guards_singletonr*   rr   r~   Zenter_jvp_nestingr6   r=   rv   rq   Z_jvp_increment_nestingr/   r   r   r7   r8   )r   r:   Z	jvp_levelr    r    r!   r;      s    
 z+JvpIncrementNestingCtxManagerVariable.enterc                 G   s.   | j   |jdtjjjdi  tj	
d S Nr_   r    )r/   r"   r=   rv   r*   rq   rr   Z_jvp_decrement_nestingr   r7   r8   r>   r    r    r!   r@      s    
   z*JvpIncrementNestingCtxManagerVariable.exitNr%   r&   r'   r(   r   r   r   ZFUNCTORCH_STACK_MATCHr   rz   r8   r;   r@   r    r    r    r!   r{      s   
r{   c                   @   s,   e Zd ZdZedd Zdd Zdd ZdS )	SetFwdGradEnabledContextManagerzWrepresents torch.autograd.forward_ad._set_fwd_grad_enabled() to enable/disable fwd gradc                 K   s   t f |d d|S rc   )r   rp   r    r    r!   r8      s    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*   rq   _set_fwd_grad_enabledrt   r    r   r    r!   ru     r   z7SetFwdGradEnabledContextManager.enter.<locals>.<lambda>r_   )r-   r*   rq   Z_is_fwd_grad_enabledrt   r   r6   r=   rv   r/   r   r   r7   r8   )r   r:   moder    r   r!   r;     s    
z%SetFwdGradEnabledContextManager.enterc                 G   s0   | j   |jdtjj| jfi  tj	
d S rx   )r/   r"   r=   rv   r*   rq   r   rt   r   r7   r8   r>   r    r    r!   r@     s    
z$SetFwdGradEnabledContextManager.exitNry   r    r    r    r!   r      s
   
r   c                   @   s:   e Zd ZdZee ejZe	dd Z
dd Zdd ZdS )	DualLevelContextManagerz;Represents torch.autograd.forward_ad.dual_level ctx managerc                 K   s   t f d d d|S rc   )r   )r:   r2   r    r    r!   r8   %  s    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!   ru   1  r   z/DualLevelContextManager.enter.<locals>.<lambda>r_   r    )r   r   r*   r   r   Zenter_dual_levelr   r6   r=   rv   rq   Z_enter_dual_levelr/   r   r   r7   r8   r9   r    r   r!   r;   -  s    
 
zDualLevelContextManager.enterc                 G   s0   | j   |jdtjj| jfi  tj	
d S rx   )r/   r"   r=   rv   r*   rq   Z_exit_dual_levelr   r   r7   r8   r>   r    r    r!   r@   ;  s    
zDualLevelContextManager.exitN)r%   r&   r'   r(   r   r   r   Z
DUAL_LEVELr   rz   r8   r;   r@   r    r    r    r!   r      s   
r   c                   @   s:   e Zd ZdZee ejZe	dd Z
dd Zdd ZdS )	&GradIncrementNestingCtxManagerVariablez6represents torch.func.grad increment/decrement nestingc                 K   s   t f d d d|}|S rc   )r   r|   r    r    r!   r8   P  s    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   s   t jj S r   )r*   rq   rr   _grad_decrement_nestingr    r    r    r!   ru   \  r   z>GradIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>r_   r    )r   r   r*   rq   rr   Z_grad_increment_nestingr6   r=   rv   r/   r   r   r7   r8   )r   r:   Z
grad_levelr    r    r!   r;   Y  s    
z,GradIncrementNestingCtxManagerVariable.enterc                 G   s.   | j   |jdtjjjdi  tj	
d S r   )r/   r"   r=   rv   r*   rq   rr   r   r   r7   r8   r>   r    r    r!   r@   e  s    
   z+GradIncrementNestingCtxManagerVariable.exitNr   r    r    r    r!   r   F  s   
r   c                       s<   e Zd ZdZedd Z fddZdd Zdd	 Z  Z	S )
CatchWarningsCtxManagerVariablez'Delay a call to warnings.catch_warningsc                 C   s   t |d d dS )N)catch_warnings_argsr-   r.   )r   )r:   r   r    r    r!   r8   p  s
    z&CatchWarningsCtxManagerVariable.createc                    s*   t |tst|t jf | || _d S r   )r]   dictr#   r0   r1   r   )r   r   r2   r3   r    r!   r1   x  s    z(CatchWarningsCtxManagerVariable.__init__c                    sD   dd | j  D }tjf | | | fdd tj  S )Nc                 S   s   i | ]\}}||  qS r    )as_python_constant)rL   kvr    r    r!   
<dictcomp>~  s     z9CatchWarningsCtxManagerVariable.enter.<locals>.<dictcomp>c                      s     d d d S r   )rm   r    Zctx_valr    r!   ru     r   z7CatchWarningsCtxManagerVariable.enter.<locals>.<lambda>)	r   itemswarningscatch_warningsr6   r   r7   r8   re   )r   r:   r2   r    r   r!   r;   }  s    z%CatchWarningsCtxManagerVariable.enterc                 C   sF   | dd || j  t| j }||t||d d S )Nr   r   T)	Zload_import_fromZforeachr   valuestuplekeysrS   Zcreate_call_function_kwrT   )r   Zcgr   r    r    r!   rU     s    z+CatchWarningsCtxManagerVariable.reconstruct)
r%   r&   r'   r(   rz   r8   r1   r;   rU   ra   r    r    r3   r!   r   m  s   
r   c                   @   s:   e Zd ZdZee ejZe	dd Z
dd Zdd ZdS )	&VmapIncrementNestingCtxManagerVariablez1represents torch VMap increment/decrement nestingc                 K   s   t f |d d|}|S rc   )r   )r:   r-   r2   r}   r    r    r!   r8     s    z-VmapIncrementNestingCtxManagerVariable.createc                 C   s`   t | j | j\}}tjj||}| |dd  |j	dtjjj||fi | j
_tj|S )Nc                   S   s   t jj S r   )r*   rq   rr   _vmap_decrement_nestingr    r    r    r!   ru     r   z>VmapIncrementNestingCtxManagerVariable.enter.<locals>.<lambda>r_   )r   r   r-   r*   rq   rr   Z_vmap_increment_nestingr6   r=   rv   r/   r   r   r7   r8   )r   r:   Z
batch_sizeZ
randomnessZ
vmap_levelr    r    r!   r;     s    

z,VmapIncrementNestingCtxManagerVariable.enterc                 G   s.   | j   |jdtjjjdi  tj	
d S r   )r/   r"   r=   rv   r*   rq   rr   r   r   r7   r8   r>   r    r    r!   r@     s    
   z+VmapIncrementNestingCtxManagerVariable.exitNr   r    r    r    r!   r     s   
r   c                       sz   e Zd ZdZee ejZe	dddZ
d fdd	Zd	d
 Zdd Zddd fddZdd Zdd Zdd Z  ZS )GradModeVariablez6represents torch.{no_grad,enable_grad,set_grad_mode}()Fc                 K   s2   t f |gt gd|}|r.|| |j |S rc   )r   r*   is_grad_enabledr5   r-   )r:   target_valueinitializedr2   r}   r    r    r!   r8     s    zGradModeVariable.createNTc                    s&   t  jf ||d| t| j d S rc   r0   r1   r   r   )r   r-   r.   r   r2   r3   r    r!   r1     s     zGradModeVariable.__init__c                 C   s   |  || j tjd S r   )r5   r-   r   r7   r8   r9   r    r    r!   r;     s    zGradModeVariable.enterc                 G   s   |  || j tjd S r   )r5   r.   r   r7   r8   r>   r    r    r!   r@     s    zGradModeVariable.exitrX   rY   )r?   r2   c                    s   |  || j t |||S r   )r5   r.   r0   r_   r^   r3   r    r!   r_     s    zGradModeVariable.call_functionc                 C   sL   t |dkst|d }t |krH|jdtjj|fi  tj| d S Nr   r   r_   )rT   r#   r*   r   r=   rv   rq   Z_set_grad_enabledr   r:   r   valuer    r    r!   r5     s       zGradModeVariable._call_funcc                 C   s   dS Nr*   r    r   r    r    r!   rA     s    zGradModeVariable.module_namec                 C   s   dS )NZset_grad_enabledr    r   r    r    r!   rB     s    zGradModeVariable.fn_name)F)NT)r%   r&   r'   r(   r   r   r   Z	GRAD_MODEr   rz   r8   r1   r;   r@   r_   r5   rA   rB   ra   r    r    r3   r!   r     s   
 
r   c                       sJ   e Zd Zedd Zd fdd	Zdd Zdd	 Zd
d Zdd Z	  Z
S )InferenceModeVariablec                 K   s   t |gfdt i|}|S )Nr.   )r   r*   is_inference_mode_enabledr:   r   r2   r}   r    r    r!   r8     s    zInferenceModeVariable.createNc                    s2   |d krt  }t jf ||d| || _d S rc   )r*   r   r0   r1   r-   r   r-   r.   r2   r3   r    r!   r1     s     zInferenceModeVariable.__init__c                 G   s,   | j   |jdtjjj| j jfi  d S rx   )	r/   r$   r=   rv   r*   r   	grad_mode_exit_inference_moder   r>   r    r    r!   r@     s    
zInferenceModeVariable.exitc                    sH   t jjj| j  | | fdd |jdt jjj| ji | j_	d S )Nc                      s   t jj S r   )r*   r   r   r   r    ctxr    r!   ru     r   z-InferenceModeVariable.enter.<locals>.<lambda>r_   )
r*   r   r   Z_enter_inference_moder-   r6   r=   rv   r/   r   r9   r    r   r!   r;     s     
zInferenceModeVariable.enterc                 C   s   dS r   r    r   r    r    r!   rA     s    z!InferenceModeVariable.module_namec                 C   s   dS )NZinference_moder    r   r    r    r!   rB     s    zInferenceModeVariable.fn_name)Nr%   r&   r'   rz   r8   r1   r@   r;   rA   rB   ra   r    r    r3   r!   r     s   
	 	r   c                       sL   e Zd ZdZee ejZe	dd Z
d fdd	Zdd Zd	d
 Z  ZS )TorchFunctionDisableVariablez>represents whether torch function overrides are enabled or notc                 K   s8   t f dg| jjgd|}|| dg ||  |S )NFrd   )r   r=   Ztorch_function_enabledr5   r6   r|   r    r    r!   r8      s    
z#TorchFunctionDisableVariable.createNc                    s&   t  jf ||d| t| j d S rc   r   r   r3   r    r!   r1   ,  s     z%TorchFunctionDisableVariable.__init__c                 C   s   t jd S r   r   r7   r8   r9   r    r    r!   r;   2  s    z"TorchFunctionDisableVariable.enterc                 C   s$   t |dkst|j|d  d S r\   )rT   r#   r=   Zset_torch_function_state)r   r:   r   r    r    r!   r5   5  s    z'TorchFunctionDisableVariable._call_func)N)r%   r&   r'   r(   r   r   r   ZTORCH_FUNCTION_STATEr   rz   r8   r1   r;   r5   ra   r    r    r3   r!   r     s   
r   c                       s\   e Zd ZdZee ejZe	dd Z
d fdd	Zdd Zd	d
 Zdd Zdd Z  ZS )DeterministicAlgorithmsVariablezVrepresents torch.{are_deterministic_algorithms_enabled,use_deterministic_algorithms}()c                 K   s8   t f |gt gd|}|| |g ||  |S rc   )r   r*   Z$are_deterministic_algorithms_enabledr5   r6   r   r    r    r!   r8   A  s    
z&DeterministicAlgorithmsVariable.createNc                    s&   t  jf ||d| t| j d S rc   r   r   r3   r    r!   r1   L  s     z(DeterministicAlgorithmsVariable.__init__c                 C   s   t jd S r   r   r9   r    r    r!   r;   R  s    z%DeterministicAlgorithmsVariable.enterc                 C   sB   t |dkst|d }|jdtjj|fi f tj| d S r   )rT   r#   r=   rv   r*   rq   Z_set_deterministic_algorithmsr   r    r    r!   r5   U  s       z*DeterministicAlgorithmsVariable._call_funcc                 C   s   dS r   r    r   r    r    r!   rA   ]  s    z+DeterministicAlgorithmsVariable.module_namec                 C   s   dS )NZuse_deterministic_algorithmsr    r   r    r    r!   rB   `  s    z'DeterministicAlgorithmsVariable.fn_name)N)r%   r&   r'   r(   r   r   r   ZDETERMINISTIC_ALGORITHMSr   rz   r8   r1   r;   r5   rA   rB   ra   r    r    r3   r!   r   :  s    

r   c                       sN   e Zd ZdZedd Zd fdd	Zdd Zd	d
 Zdd Z	dd Z
  ZS )!DisabledSavedTensorsHooksVariablez;represents torch.autograd.graph.disable_saved_tensors_hook.c                 K   s<   t f |gtjj gd|}|| |g ||  |S rc   )r   r*   rq   	_autogradZ/_saved_tensors_hooks_get_disabled_error_messager5   r6   r   r    r    r!   r8   g  s    

z(DisabledSavedTensorsHooksVariable.createNc                    s   t  jf ||d| d S rc   r0   r1   r   r3   r    r!   r1   t  s     z*DisabledSavedTensorsHooksVariable.__init__c                 C   s   t jd S r   r   r9   r    r    r!   r;   y  s    z'DisabledSavedTensorsHooksVariable.enterc                 C   sr   t |dkst|d }|d k	rJ|jdtjjj|fi  tjj| n$|jdtjjjdi  tjj  d S )Nr   r   r_   r    )	rT   r#   r=   rv   r*   rq   r   Z_saved_tensors_hooks_disableZ_saved_tensors_hooks_enabler   r    r    r!   r5   |  s"       z,DisabledSavedTensorsHooksVariable._call_funcc                 C   s   dS )Nztorch.autograd.graphr    r   r    r    r!   rA     s    z-DisabledSavedTensorsHooksVariable.module_namec                 C   s   dS )NZdisable_saved_tensors_hooksr    r   r    r    r!   rB     s    z)DisabledSavedTensorsHooksVariable.fn_name)N)r%   r&   r'   r(   rz   r8   r1   r;   r5   rA   rB   ra   r    r    r3   r!   r   d  s   
r   c                       sJ   e Zd Zedd Zd fdd	Z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ks$tt| j	||}|
  g }|  dD ]j}|dkr| t jjjt jjjfkr| t jjjkrdnd}n
|j| }t|tr||  qN|| qNt|fdd i|}|S )N)device_typeZdtyperw   Zcache_enabledr   cudacpur.   )r*   ampZautocast_modeautocastr   r   r#   inspect	signaturebindapply_defaultsclear	argumentsr]   r   appendr   r   )funcr?   r2   Z
bound_argsr-   keyargr}   r    r    r!   r8     s*    
	


zAutocastModeVariable.createNc                    s"   t  jf ||d| || _d S rc   )r0   r1   r-   r   r3   r    r!   r1     s     zAutocastModeVariable.__init__c                 G   s*   | j   |jdtjj| j jfi  d S rx   )r/   r$   r=   rv   r*   r   _exit_autocastr   r>   r    r    r!   r@     s    
   zAutocastModeVariable.exitc                    sD   t jj| j  | | fdd |jdt jj| ji | j_d S )Nc                      s   t j S r   )r*   r   r   r    r   r    r!   ru     r   z,AutocastModeVariable.enter.<locals>.<lambda>r_   )	r*   r   Z_enter_autocastr-   r6   r=   rv   r/   r   r9   r    r   r!   r;     s       zAutocastModeVariable.enterc                 C   s   dS )Nztorch.amp.autocast_moder    r   r    r    r!   rA     s    z AutocastModeVariable.module_namec                 C   s   dS )Nr   r    r   r    r    r!   rB     s    zAutocastModeVariable.fn_name)Nr   r    r    r3   r!   r     s   
r   c                       sB   e Zd ZdZd fdd	Zdd Zdd Zd	d
 Zdd Z  Z	S )NullContextVariablez
    This class represents Python contextlib.nullcontext.
    It's used as a placeholder for other context managers that Dynamo doesn't
    support yet, e.g, torch.autograd.profiler.record_function.
    Nc                    s   t  jf d|i| d S Nr-   r   )r   r-   r2   r3   r    r!   r1     s    zNullContextVariable.__init__c                 C   s   t jd S r   r   r9   r    r    r!   r;     s    zNullContextVariable.enterc                 G   s   t jd S r   r   r>   r    r    r!   r@     s    zNullContextVariable.exitc                 C   s   dS )N
contextlibr    r   r    r    r!   rA     s    zNullContextVariable.module_namec                 C   s   dS )Nnullcontextr    r   r    r    r!   rB     s    zNullContextVariable.fn_name)N)
r%   r&   r'   r(   r1   r;   r@   rA   rB   ra   r    r    r3   r!   r     s   r   c                       s:   e Zd Zedd Zd
 fdd	Zdd Zdd	 Z  ZS )StreamContextVariablec              	   K   sN   ddl m} t|jj}|t| | jd|di }tf |g|g|jd|S )Nr   wrap_fx_proxy_clsr_   r   )r-   r.   device)	builderr   r   r   current_streamStreamVariabler=   create_proxyr   )r:   r   r2   r   Zcurrent_stream_methodr   r    r    r!   r8     s*    
zStreamContextVariable.createNc                    s>   t  jf ||d| || _t| jj| _t| jj| _d S rc   )r0   r1   r   r   
set_streamZ_set_stream_by_idset_stream_id)r   r-   r   r.   r2   r3   r    r!   r1     s     zStreamContextVariable.__init__c                    s    j d  d k	r4|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   r_   c                      s      jd jS )Nr   )r   r.   r   r    r   r    r!   ru     r   z-StreamContextVariable.enter.<locals>.<lambda>)r-   as_proxyr=   r   r   r   r   Z	stream_idZdevice_indexr   r6   )r   r:   streamr    r   r!   r;     s     zStreamContextVariable.enterc                 G   s.   |j d| j| jd  fi  | j  d S )Nr_   r   )r=   r   r   r.   r   r/   r$   r>   r    r    r!   r@      s    zStreamContextVariable.exit)N)	r%   r&   r'   rz   r8   r1   r;   r@   ra   r    r    r3   r!   r     s
   
r   c                       sD   e Zd ZdZedd Z fddZdd Zdd	 Zd
d Z	  Z
S )PreserveVersionContextVariablez?
    Wraps torch.autograd._unsafe_preserve_version_counter
    c                    s   t  fddS )Nc                    s   t | |  dS )N_version)r   Zvar_getattr)tensorr:   r    r!   ru   2  s   
z<PreserveVersionContextVariable.constructor.<locals>.<lambda>)r   ZLambdaVariabler   r    r   r!   constructor/  s    
z*PreserveVersionContextVariable.constructorc                    s*   | dd  t jf | || _|| _d S r   )
setdefaultr0   r1   r   prev_version)r   r   r   r2   r3   r    r!   r1   8  s    z'PreserveVersionContextVariable.__init__c                 C   s   d S r   r    r9   r    r    r!   r;   >  s    z$PreserveVersionContextVariable.enterc                 G   s(   ddl m} t||| j| jgi S )Nr   )_unsafe_set_version_counter)Ztensor_version_opr   r   ZTorchInGraphFunctionVariabler_   r   r   )r   r:   r?   r   r    r    r!   r@   A  s     
 z#PreserveVersionContextVariable.exitc                 C   s   t d d S )Nz@torch.autograd._unsafe_preserve_version_counter with graph break)r   rC   r    r    r!   rU   H  s    z*PreserveVersionContextVariable.reconstruct)r%   r&   r'   r(   rz   r   r1   r;   r@   rU   ra   r    r    r3   r!   r   *  s   
r   c                       s>   e Zd Z fddZddddddZd	d
 Zdd Z  ZS )r   c                    sb   |d k	r(d|j jkr(|j jd |ks(t|jj|jks>tdt jf | || _|| _|| _d S )Nexample_valuez.stream value is not equal to the passed device)	nodemetar#   r   typer0   r1   r   r   )r   r   r   r   r2   r3   r    r!   r1   O  s    zStreamVariable.__init__rX   rY   r   rZ   c                 C   s   t | j|std| |dks0td| ddlm} ddlm} |dkrz|jjd	|f|| g| |  t	
d S |d
kr|t	j
||jjd	|f|| g| | dS |dkr|t||jjd	|f|| g| | dS t| jd | d  d S )Nzno stream method found named )wait_streamsynchronizequeryrecord_event
wait_eventz unsupported stream method r   proxy_args_kwargsr   r   )r   r   r   call_methodr   Z
target_clsr:   r   r   z stream method  unsupported)hasattrr   r#   utilsr   r   r   r=   r   r   r7   EventVariabler   r   r   r:   namer?   r2   r   r   r    r    r!   r   Z  sJ    
 
  zStreamVariable.call_methodc                 C   s   | j S r   r   r   r    r    r!   r     s    zStreamVariable.as_proxyc                 C   sB   | j r
td| j }|jj|| j}||j|ddd d S )NZ_stream_FT)Z	push_nulladd)	rg   r#   r   r:   r=   Zinstall_global_by_idr   rR   Zcreate_load_global)r   rD   prefixr   r    r    r!   rU     s    
zStreamVariable.reconstruct)r%   r&   r'   r1   r   r   rU   ra   r    r    r3   r!   r   N  s   +r   c                       s6   e Zd Z fddZddddddZd	d
 Z  ZS )r   c                    sF   |d k	r(d|j jkr(|j jd |ks(tt jf | || _|| _d S )Nr   )r   r   r#   r0   r1   r   r   )r   r   r   r2   r3   r    r!   r1     s
    zEventVariable.__init__rX   rY   r   rZ   c                 C   s   ddl m} ddlm} |dkrJ|jjd|f|| g| |  td S |dkr~|tj||jjd|f|| g| | dS td	| d
 d S )Nr   r   r   r   )waitrecordr   r   r   r   zevent method r   )	r   r   r   r   r=   r   r   r7   r   r   r    r    r!   r     s,     
 zEventVariable.call_methodc                 C   s   | j S r   r   r   r    r    r!   r     s    zEventVariable.as_proxy)r%   r&   r'   r1   r   r   ra   r    r    r3   r!   r     s   r   c                       sH   e Zd ZdhejZed fddZddddd	d
Zdd Z  Z	S )WithExitFunctionVariabletargetr   c                    s,   t  jf | t|tst|| _|| _d S r   )r0   r1   r]   r+   r#   r   r   )r   r   r   r2   r3   r    r!   r1     s    z!WithExitFunctionVariable.__init__rX   rY   r   rZ   c                 C   s   |rt | jj|f| S r   )r#   r   r@   r^   r    r    r!   r_     s    z&WithExitFunctionVariable.call_functionc                    s   | j    jjjrtjdkr@ td  tddd  	 fdd| j j
D   	tt| j j
d  t| j  td	 d S )
NrF   rI   ZSWAPr   )r   c                    s   g | ]}  |qS r    rJ   rK   rN   r    r!   rO     s     z8WithExitFunctionVariable.reconstruct.<locals>.<listcomp>FPOP_TOP)r   rE   r:   r=   Zpartial_convertrP   rQ   rR   r
   rS   r-   r	   rT   r   r   rC   r    rN   r!   rU     s    

z$WithExitFunctionVariable.reconstruct)
r%   r&   r'   r   r`   r+   r1   r_   rU   ra   r    r    r3   r!   r     s    r   )=dataclassesr   rP   r   typingr   r   r   r   Ztorch._Cr*   Ztorch._guardsr    r   Zbytecode_transformationr	   r
   r   Zdevice_interfacer   excr   r   Zguardsr   r   rg   r   r   baser   Z	functionsr   r   r   r   r   	dataclassr   r+   rb   ro   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r    r    r!   <module>   sJ   	E0()&&'(52*4;@$K&