U
    zh                      @   s\  d dl Z d dlZd dlZd dlZd dl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  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 ddlm Z m!Z!m"Z"m#Z# dd	lm$Z$m%Z%m&Z&m'Z'm(Z( d
dl)m*Z* d
dl+m,Z,m-Z- d
dl.m/Z/m0Z0 G dd de*Z1G dd de*Z2G dd de*Z3G dd de3Z4G dd de*Z5G dd de3Z6G dd de3Z7G dd de*Z8G dd de*Z9G d d! d!e*Z:G d"d# d#e*Z;d$d% Z<G d&d' d'e*Z=ej>G d(d) d)Z?G d*d+ d+e0Z@G d,d- d-e*ZAG d.d/ d/e*ZBG d0d1 d1e*ZCG d2d3 d3e*ZDG d4d5 d5e*ZEG d6d7 d7e*ZFejGd
d8d9d: ZHG d;d< d<e*ZIG d=d> d>e*ZJG d?d@ d@e*ZKG dAdB dBe*ZLG dCdD dDe*ZMG dEdF dFe*ZNG dGdH dHe*ZOG dIdJ dJe*ZPG dKdL dLePZQG dMdN dNePZRG dOdP dPePZSG dQdR dRePZTG dSdT dTePZUejVeTejWeTejXeUiZYdS )U    N)DictList   )config	variables)create_call_functioncreate_instructionunimplemented)GuardBuilderinstall_guard)
AttrSourceGetItemSourceODictGetItemSource
TypeSource)check_unspec_or_constant_argsidentityis_tensor_base_attr_getterproxy_args_kwargsset_example_value   )VariableTracker)NestedUserFunctionVariableUserFunctionVariable)is_standard_setattrUserDefinedObjectVariablec                       sh   e Zd ZdhejZd fdd	Z fddZ fdd	Zed
dddZ	ddd
d fddZ
  ZS )SuperVariablespecializedNFc                    s$   t  jf | || _|| _|| _d S N)super__init__typevarobjvarr   )selfr!   r"   r   kwargs	__class__ N/var/www/html/venv/lib/python3.8/site-packages/torch/_dynamo/variables/misc.pyr    &   s    zSuperVariable.__init__c                    sR   |t t || j | jd k	r>|| j |tdd n|tdd d S )Nr   Tr   )r   ZBuiltinVariabler   r!   r"   extend_outputr   r#   codegenr%   r'   r(   reconstruct3   s    


zSuperVariable.reconstructc           
         s   | j std| jr$t| j |S | j }| j  }| j jrLt| j jnd }t	|t
rj| j j}| j j}d }| j jd k	r|j}||d }t|t|D ],}	t||	 |rttt|d|	|} qqtt||||fS )Nz1-arg super not implementedr   __mro__)r"   AssertionErrorr   getattrr!   as_python_constantpython_typesourcer   
issubclasstypevaluer-   indexrangelenhasattrr   r   r   )
r#   txnameZsearch_typeZtype_to_useZtype_to_use_sourcer2   Z
search_mrostart_indexr6   r%   r'   r(   _resolved_getattr_and_source<   s,    
	

z*SuperVariable._resolved_getattr_and_sourcer   r;   returnc                 C   sV   |  | |\}}tj|s&t| |S |rJt|tj tjj	||dS tj	|S )Nr2   )
r=   r   ConstantVariable
is_literalGetAttrVariabler   
make_guardr   ZCONSTANT_MATCHcreate)r#   r:   r;   r5   r2   r'   r'   r(   var_getattrc   s    	
zSuperVariable.var_getattrList[VariableTracker]Dict[str, VariableTracker]argsr$   r?   c                    s  |  | |\}}|tjkr"ttS |tjjjkr| j}ddl	m
} t|tjrt|j|r|s|s|jj	|dtjd tjd S td nht|tjrtj||d|| jg| |S t|tjrtj|j| j|d|||S |tjjkrzt| jtjrz| jjrzt|dkrzt|dkrz|d   rzdd	l!m"}	 |d # }
|	|t$| jj|
tj| jj%|
S |tjj&tj'fkrt| jtj(r|st|dkst)t*tj(| j+|d
||S t,|rt| jtr| jj-|f||S td|  d S )Nr   )AttributeMutationNewZ__call_nn_module_initTzsuper() nn.Module.__init__r@   r   r   VariableBuilder__setitem__znon-function or method super: ).r=   objectr    LambdaVariabler   torchnnModuler"   side_effectsrK   
isinstancer   r   Zmutable_localoutputZ
store_attrrA   rE   r
   typesFunctionTyper   call_function
MethodTypeUserMethodVariable__func__collectionsOrderedDict__getitem__r2   r8   is_python_constantbuilderrM   r0   r   r5   rN   __setattr__ZCustomizedDictVariabler.   r   call_methodr   Zmethod_setattr_standard)r#   r:   r;   rJ   r$   Zinner_fnr2   r"   rK   rM   keyr%   r'   r(   rc   t   s    



  
     



    zSuperVariable.call_method)NF)__name__
__module____qualname__r   _nonvar_fieldsr    r,   r=   strrF   rc   __classcell__r'   r'   r%   r(   r       s   	'r   c                       s$   e Zd Z fddZdd Z  ZS )ExceptionVariablec                    s   t  jf | || _|| _d S r   )r   r    exc_typerJ   )r#   rl   rJ   r$   r%   r'   r(   r       s    zExceptionVariable.__init__c                 C   s2   | d| jj || j |t| jd d S )NbuiltinsT)load_import_fromrl   re   foreachrJ   rY   r8   r*   r'   r'   r(   r,      s    zExceptionVariable.reconstructre   rf   rg   r    r,   rj   r'   r'   r%   r(   rk      s   rk   c                   @   s   e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nre   rf   rg   __doc__r'   r'   r'   r(   rq      s   rq   c                   @   s   e Zd ZdZdS )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nrr   r'   r'   r'   r(   rt      s   rt   c                   @   s:   e Zd ZdZdd ZeddddZdd	dd
ddZdS )ComptimeVariableza
    This variable is special, it lets you execute arbitrary code at
    Dynamo compile time
    c                 C   s   t dd S )Nzcomptime is special form)NotImplementedErrorr*   r'   r'   r(   r,      s    zComptimeVariable.reconstructr   r>   c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r   r@   )rw   	functionsr   r/   r   r2   )r#   r:   r;   rw   r   r'   r'   r(   rF      s     
zComptimeVariable.var_getattrrG   rH   rI   c                 C   s   ddl m} |rtt|dks$t|d }t|trH| || nvt|tr| }|j	rptd|j
 t||j|j |jrt|jjnd t }||| ntdt| tjd S )Nr   )ComptimeContextr   zOcomptime function must not have free variables, but these variables were free: z"unsupported argument to comptime: )rw   ry   r.   r8   rU   r   Zget_functionr   get_codeclosureco_freevarsrW   rX   	f_globalsfn_namer0   defaultstupleitemsRuntimeErrorr4   r   rA   rE   )r#   r:   rJ   r$   ry   fncodefuncr'   r'   r(   rY      s*    


zComptimeVariable.call_functionN)re   rf   rg   rs   r,   ri   rF   rY   r'   r'   r'   r(   ru      s    ru   c                       s0   e Zd ZdhejZ fddZdd Z  ZS )ClosureVariabler;   c                    s   t  jf | || _d S r   r   r    r;   r#   r;   r$   r%   r'   r(   r      s    zClosureVariable.__init__c                 C   s   | || j d S r   append_outputZcreate_load_closurer;   r*   r'   r'   r(   r,     s    zClosureVariable.reconstructre   rf   rg   rq   rh   r    r,   rj   r'   r'   r%   r(   r     s   r   c                       s0   e Zd ZdhejZ fddZdd Z  ZS )InlinedClosureVariabler;   c                    s   t  jf | || _d S r   r   r   r%   r'   r(   r      s    zInlinedClosureVariable.__init__c                 C   s   | || j d S r   r   r*   r'   r'   r(   r,   !  s    z"InlinedClosureVariable.reconstructr   r'   r'   r%   r(   r     s   r   c                       s   e Zd Z fddZ  ZS )NewCellVariablec                    s   t  jf | d S r   r   r    r#   r$   r%   r'   r(   r    &  s    zNewCellVariable.__init__re   rf   rg   r    rj   r'   r'   r%   r(   r   %  s   r   c                       s   e Zd Z fddZ  ZS )NewGlobalVariablec                    s   t  jf | d S r   r   r   r%   r'   r(   r    +  s    zNewGlobalVariable.__init__r   r'   r'   r%   r(   r   *  s   r   c                       sF   e Zd ZdZedd Zed fddZedd fd	d
Z	  Z
S )InspectSignatureVariablez!represents inspect.signature(...)c                 K   s   |rt d|  t| S )Nzinspect.signature with )r
   r   )callabler$   r'   r'   r(   rE   2  s    zInspectSignatureVariable.create)	inspectedc                    s   t  jf | || _d S r   )r   r    r   )r#   r   r$   r%   r'   r(   r    8  s    z!InspectSignatureVariable.__init__r   r>   c                    s4   |dkr&t jdd | j D tdS t ||S )N
parametersc                 S   s   i | ]}t j|t qS r'   )r   rA   rE   InspectParameterVariable).0r;   r'   r'   r(   
<dictcomp>?  s   
 z8InspectSignatureVariable.var_getattr.<locals>.<dictcomp>)Zuser_cls)r   ConstDictVariabler   Zinspect_parameter_namesdictr   rF   )r#   r:   r;   r%   r'   r(   rF   <  s    z$InspectSignatureVariable.var_getattr)re   rf   rg   rs   staticmethodrE   r   r    ri   rF   rj   r'   r'   r%   r(   r   /  s
   
r   c                   @   s   e Zd ZdZdS )r   z2This is not implemented, if used will graph break.Nrr   r'   r'   r'   r(   r   H  s   r   c                    s    fdd}t |_|S )Nc                     s    j | |S r   )applyrJ   r$   fn_clsr'   r(   trampoline_autograd_applyO  s    zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)!produce_trampoline_autograd_applyZ_origin)r   r   r'   r   r(   r   N  s    r   c                       sT   e Zd ZdZdhejZ fddZdd Zdd Zd	d
 Z	dddddZ
  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr   c                    s   t  jf | || _d S r   )r   r    r   )r#   r   r$   r%   r'   r(   r    ^  s    z!AutogradFunctionVariable.__init__c                    s  d  fdd}t |||f  rZt rZtjrZddlm} ddlm	} | j
j}|| j
j}|r||| j
j| j
j}| j
j}	|	tjjjk	rtd | j
j}
|
tjjjk	rtd d	d
lm} | j}|d krt| j
j| j
j}||| j
j|t|ddd||}| jrVt| jd}t|tj |rVt| jd}t|tj |S | jrpt| jd}nd }| j
j}t ||}|f|}t!|t"j#rt$j%||d||S t!|t"j&rt$j'|j(t$)| j
|d||S td|  d S )NFc                    s8   t | tjr| jdk	rd t | tjr4| r4d d S )NFT)rU   r   TensorVariablerequires_gradNNModuleVariableZis_training)noder   r:   r'   r(   visite  s    

z2AutogradFunctionVariable.call_apply.<locals>.visitr   )#autograd_function_forward_rewritten)_is_setup_context_definedzNYI - User defind vjpzNYI - User defind jvpr   )AutogradFunctionApplyVariabler   )memberr@   forwardsetup_contextz?non-function or method in subclass of torch.autograd.Function: )*r   r   rQ   Zis_grad_enabledr   Zcapture_autograd_functionZ"torch._functorch.autograd_functionr   Ztorch.autograd.functionr   r   r   r   ZvjpautogradFunctionr
   ZjvpZhigher_order_opsr   r2   r   Zimport_sourcerf   re   backwardrY   r   rD   r   ZFUNCTION_MATCHAutogradFunctionContextVariablerE   rU   rW   rX   r   r   rZ   r[   r\   ZUserDefinedClassVariable)r#   r:   rJ   r$   r   r   r   Z
forward_fnZis_setup_ctx_definedZvjp_fnZjvp_fnr   r2   valZfwd_srcZsetup_ctx_srcr   ctxr'   r   r(   
call_applyb  s    	  
  
  
  z#AutogradFunctionVariable.call_applyc                 C   s\   | j j}t| jd| _t|d jtjjj	ks2t
t|tjsBt
tj|| jd|||S )Nr   r   r@   )r   r   r   r2   r4   r5   rQ   _dynamoZexternal_utilsZFakeBackwardCFunctionr.   rU   rW   rX   r   r   rY   )r#   r:   rJ   r$   r   r'   r'   r(   call_backward  s      z&AutogradFunctionVariable.call_backwardc                 C   s
   t | jS r   )r   r   r#   r:   rJ   r$   r'   r'   r(   rY     s    z&AutogradFunctionVariable.call_functionrG   rH   r   c                 C   sb  ddl m} ddlm} |dkrf|| jrVt| j}|||jjd|ft|| dS | 	|||S n|dkr|| 
|||S dd	lm } | jd k	rt| j|nd }	zt| j|}
W n tk
r   d }
Y nX t|
tr(|
| j}|	d k	r||j||	d
|||S ||||||S n6t|
trPtj|
j| |	d
|||S td|  d S )Nr   )is_callable_allowedr   )wrap_fx_proxyr   rY   )r:   proxyr   )trace_rulesr@   zUnsupported method: )r   r   ra   r   r   r   rV   create_proxyr   r   r    r2   r   inspectgetattr_staticAttributeErrorrU   r   __get__lookupZcreate_with_sourcerY   classmethodr   r[   r\   r
   )r#   r:   r;   rJ   r$   r   r   r   r   r2   objr   r'   r'   r(   rc     sj    
	


         z$AutogradFunctionVariable.call_method)re   rf   rg   rs   r   rh   r    r   r   rY   rc   rj   r'   r'   r%   r(   r   V  s   ]
r   c                   @   s&   e Zd ZU ejedZee e	d< dS )SavedTensorBox)default_factorytensorsN)
re   rf   rg   dataclassesfieldlistr   r   r   __annotations__r'   r'   r'   r(   r     s   
r   c                       sj   e Zd ZdZdddhejZd fdd	Zedd	d
Zdd Z	dddd fddZ
 fddZ  ZS )r   zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    r   	inferencesaved_tensorsNFc                    s4   t  jf ||d| || _|| _|| _|| _d S )N)r5   
value_type)r   r    r   r   r   needs_input_grad)r#   r5   r   r   r   r   r   r$   r%   r'   r(   r      s
    
z(AutogradFunctionContextVariable.__init__c                 C   sv   d }|r|st dd |D }| jdtjjjt  i }| jjd tjjjt	j
td|t |di }t|j|j |S )Nc                 s   s    | ]}t |tjo|jV  qd S r   )rU   r   r   r   r   xr'   r'   r(   	<genexpr>*  s   z9AutogradFunctionContextVariable.create.<locals>.<genexpr>rY   T)r   r   r   r   )r   rV   r   rQ   r   functionZFunctionCtxrT   Ztrack_object_new	functoolspartialr   r   r   r   r5   )r:   rJ   r$   r   r   outr'   r'   r(   rE   &  s2       z&AutogradFunctionContextVariable.createc                 C   s   | j d krtd | j S )Nzproxy not set)r   r
   r#   r'   r'   r(   as_proxyA  s    
z(AutogradFunctionContextVariable.as_proxyrG   rH   r   rI   c                    s   |dkrt  ||||S |dkr0td|  | jd krBtd | jsf| jrR|rVt|jj	| | t
| jjdkr~g | j_|D ]}| jj| qtjd S )Nrb   save_for_backwardz"autograd.Function context method: zCsave_for_backward only supported on a newly constructed FunctionCtxr   )r   rc   r
   r   r   r2   r.   rV   rT   Ztrack_save_for_backwardr8   r   appendr   rA   rE   )r#   r:   r;   rJ   r$   argr%   r'   r(   rc   F  s     
z+AutogradFunctionContextVariable.call_methodc                    s    dkrt  fddS  dkr@jd k	r@ttjjS  dkrjd k	r`tjjS j	rddl
m} |tj	djjS t  S )Nr   c                     s     | |S r   )rc   r   r;   r#   r:   r'   r(   <lambda>d      z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r   r   r   rL   )rP   r   r   TupleVariabler   r   r   rA   rE   r2   ra   rM   r   r5   r   rF   r#   r:   r;   rM   r%   r   r(   rF   a  s    
z+AutogradFunctionContextVariable.var_getattr)NFNNN)NN)re   rf   rg   rs   r   rh   r    r   rE   r   rc   rF   rj   r'   r'   r%   r(   r   
  s*   
     	r   c                       s.   e Zd Z fddZddddddZ  ZS )	rP   c                    s   t  jf | || _d S r   )r   r    r   )r#   r   r$   r%   r'   r(   r    u  s    zLambdaVariable.__init__rG   rH   r   rI   c                 C   s   | j ||S r   )r   r   r'   r'   r(   rY   y  s    zLambdaVariable.call_function)re   rf   rg   r    rY   rj   r'   r'   r%   r(   rP   t  s
    rP   c                       s   e Zd ZdhejZ fddZdd Zeej	j
dddZd	d
 Zdd Zdd ZddddddZee eeef ed fddZ  ZS )rC   r;   c                    s:   t  jf | t|tstt|ts*t|| _|| _d S r   )r   r    rU   r   r.   ri   r   r;   )r#   r   r;   r$   r%   r'   r(   r      s
    zGetAttrVariable.__init__c                 C   s   | j j d| j d| j dS N(z, ))r&   re   r   r;   r   r'   r'   r(   __str__  s    zGetAttrVariable.__str__)
base_proxyc                 C   s
   t | |S r   )r/   )r   attrr'   r'   r(   create_getattr_proxy  s    z$GetAttrVariable.create_getattr_proxyc                 C   s   t | j | jS r   )rC   r   r   r   r;   r   r'   r'   r(   r     s    zGetAttrVariable.as_proxyc                 C   sZ   t | jtjst|j| jj}| j|j	kr2tt
|| j}||j	krNtt
||S r   )rU   r   r   r   rv   rV   Zget_submoduleZ
module_keyr;   __dict__r   r   )r#   r:   r;   Zstep1Zstep2r'   r'   r(   const_getattr  s    
zGetAttrVariable.const_getattrc                 C   s    || j  ||| j d S r   )r   r)   Zcreate_load_attrsr;   r*   r'   r'   r(   r,     s    
zGetAttrVariable.reconstructrG   rH   r   rI   c                 C   s   | j || j||S r   )r   rc   r;   r   r'   r'   r(   rY     s    zGetAttrVariable.call_functionc                    s  |dkr| j dkr|s|d  rt| jtjtjfr| j}|d  }|||r`|	||S |dkrt
|dkr||d S td S n|dkr
| j dkr
t
|dkr
|d  r
|s
t| jtjtjfr
| j}|d  }|||r tdS td	S t ||||S )
N)r_   getr   r   r   r   r   __contains__TF)r;   r`   rU   r   r   r   r   r0   Zhas_key_in_generic_dictrF   r8   rA   r   rc   )r#   r:   r;   rJ   r$   r   rd   r%   r'   r(   rc     sT    







zGetAttrVariable.call_method)re   rf   rg   r   rh   r    r   r   rQ   ZfxZProxyr   r   r   r,   rY   r   r   ri   rc   rj   r'   r'   r%   r(   rC     s&    	
rC   c                       sB   e Zd Z fddZdddd fddZd	d
 Zdd Z  ZS )MethodWrapperVariablec                    s   t  jf | || _d S r   )r   r    method_wrapper)r#   r   r$   r%   r'   r(   r      s    zMethodWrapperVariable.__init__rG   rH   r   rI   c                    s`   t | jrLt|d tjrLt|dkr2t|dks6t|d || jjj	S t
 ||| d S )Nr   r   )r   r   rU   r   r   r8   r.   rF   __self__re   r   rY   r   r%   r'   r(   rY     s     z#MethodWrapperVariable.call_functionc                 C   s   dS NTr'   r   r'   r'   r(   r`     s    z(MethodWrapperVariable.is_python_constantc                 C   s   | j S r   )r   r   r'   r'   r(   r0     s    z(MethodWrapperVariable.as_python_constant)re   rf   rg   r    rY   r`   r0   rj   r'   r'   r%   r(   r     s    r   c                       s8   e Zd Z fddZ fddZdd Zdd Z  ZS )	GetSetDescriptorVariablec                    s   t  jf | || _d S r   )r   r    desc)r#   r   r$   r%   r'   r(   r      s    z!GetSetDescriptorVariable.__init__c                    sF   |dkr4| j r4ddlm} ||t| j d| jjS t ||S d S )Nr   r   rL   )r2   ra   rM   r   r   r   r   rF   r   r%   r'   r(   rF     s    z$GetSetDescriptorVariable.var_getattrc                 C   s   dS r   r'   r   r'   r'   r(   r`     s    z+GetSetDescriptorVariable.is_python_constantc                 C   s   | j S r   )r   r   r'   r'   r(   r0     s    z+GetSetDescriptorVariable.as_python_constant)re   rf   rg   r    rF   r`   r0   rj   r'   r'   r%   r(   r     s   
r   c                       sV   e Zd ZddhejZejd fddZdd Zdd	 Z	d
d Z
 fddZ  ZS )PythonModuleVariabler5   is_torchr5   c                    s2   t  jf | || _| jtkp*| jjd| _d S )Nztorch.)r   r    r5   rQ   re   
startswithr   r#   r5   r$   r%   r'   r(   r      s    zPythonModuleVariable.__init__c                 C   s   t jS r   )rW   
ModuleTyper   r'   r'   r(   r1     s    z PythonModuleVariable.python_typec                 C   s   | j S r   r   r   r'   r'   r(   r0     s    z'PythonModuleVariable.as_python_constantc                 C   s   d| j  dS )NzPythonModuleVariable(r   r   r   r'   r'   r(   __repr__  s    zPythonModuleVariable.__repr__c                    s,   | j rt| j|}tj|S t ||S r   )r   r9   r5   r   rA   rE   r   call_hasattrr#   r:   r;   resultr%   r'   r(   r      s    z!PythonModuleVariable.call_hasattr)re   rf   rg   r   rh   rW   r   r    r1   r0   r   r   rj   r'   r'   r%   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 )TypingVariablec                    s   t  jf | || _d S r   r   r    r5   r   r%   r'   r(   r    (  s    zTypingVariable.__init__rG   rH   r   rI   c                 C   s:   |dkr.t |dkr.tj| j|d   S td d S )Nr_   r   r   typing)r8   r   rA   rE   r5   r0   r
   r#   r:   r;   rJ   r$   r'   r'   r(   rc   ,  s
    zTypingVariable.call_methodc                 C   s
   t | jS r   r4   r5   r   r'   r'   r(   r1   9  s    zTypingVariable.python_typec                 C   s   | j S r   r   r   r'   r'   r(   r0   <  s    z!TypingVariable.as_python_constant)re   rf   rg   r    rc   r1   r0   rj   r'   r'   r%   r(   r   '  s   r   )maxsizec                  C   sZ   ddl m}  i }|  D ]<\}}|j D ](\}}t|r*t||d  }r*|||< q*q|S )Nr   )NP_TO_TNP_MODULE)utilsr  r   r   r   r/   )r  Znp_fn_to_tnp_fnZnp_modZtnp_modr~   Ztnp_fnZnp_fnr'   r'   r(   get_np_to_tnp_map@  s    r  c                       s   e Zd ZdZejfZ fddZedd Z	edd Z
dd	d
dddZdd	d
dddZdd Zdd Z fddZ  ZS )NumpyVariablezz
    Wrapper around `numpy.*`. Currently, is able to trace a small subset of numpy functions as well as numpy dtypes.
    c                    s   t  jf | || _d S r   r   r   r%   r'   r(   r    X  s    zNumpyVariable.__init__c                 C   s:   |j d}t|dkr,|d d ddgks0t|| jkS N.r   rQ   _numpy)rf   splitr8   r.   constant_fold_functionsclsr   modr'   r'   r(   can_constant_fold_through\  s    $z'NumpyVariable.can_constant_fold_throughc                 C   s<   |j d}t|dkr,|d d ddgks0tt|d S r  )rf   r
  r8   r.   np_constant_collections_mapr   r  r'   r'   r(    get_constant_collection_for_funcb  s    $z.NumpyVariable.get_constant_collection_for_funcrG   rH   r   rI   c           
   	   C   s  t jstd| j d ddlm} ddlm} t 	| j}|d krXtd| j d | 
| }d k	rz*|| jd	d
 |D dd | D W S  tk
r   t| jj d| d|  Y nX n|jdkrt jrd|j d}|dt j 7 }t| ||j||\}}| |r\t||r\tj|  dd
 |D dd | D S |jjd||ft|| }	|||	S d S )Nznumpy.z()r   )numpy_to_tensor_wrapperr   )NumpyNdarrayVariablezCan't find numpy function zM in torch._numpy.  Please file an issue to request support for this function.c                 S   s   g | ]}|  qS r'   r0   r   r'   r'   r(   
<listcomp>  s     z/NumpyVariable.call_function.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r'   r  r   kvr'   r'   r(   r     s      z/NumpyVariable.call_function.<locals>.<dictcomp>z with non-const args:  ztorch._numpy.randomz
delegate 'z' to NumPy itself via zconfg.use_numpy_random_stream=c                 S   s   g | ]}|  qS r'   r  r   r'   r'   r(   r    s     c                 S   s   i | ]\}}||  qS r'   r  r  r'   r'   r(   r     s      rY   )r   trace_numpyr
   r5   r  r  Ztensorr  r  r   r  r   rv   re   rf   Zuse_numpy_random_streamrg   Z
patch_argsr  r   r   rA   rE   r0   rV   r   r   )
r#   r:   rJ   r$   r  r  r   Zcollection_variable_typmsgr   r'   r'   r(   rY   h  s^    zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpyr	   r   r'   r'   r(   rc     s    zNumpyVariable.call_methodc                 C   s
   t | jS r   r  r   r'   r'   r(   r1     s    zNumpyVariable.python_typec                 C   s   | j S r   r   r   r'   r'   r(   r0     s    z NumpyVariable.as_python_constantc                    s$   t jrt| jtr| jjS t  S r   )r   r  rU   r5   r4   re   r   r   r   r%   r'   r(   r     s    zNumpyVariable.as_proxy)re   rf   rg   rs   tnpZ
issubdtyper  r    r   r  r  rY   rc   r1   r0   r   rj   r'   r'   r%   r(   r  Q  s$   

 A	r  c                       s,   e Zd Z fddZdd Zdd Z  ZS )NullVariablec                    s   t  jf | d S r   r   r   r%   r'   r(   r      s    zNullVariable.__init__c                 C   s   dS )Nr  r'   r   r'   r'   r(   r     s    zNullVariable.__str__c                 C   s$   t jdk rtd |td d S )N      z0cannot reconstruct NullVariable in < Python 3.11	PUSH_NULL)sysversion_infor
   r   r   r*   r'   r'   r(   r,     s    
zNullVariable.reconstruct)re   rf   rg   r    r   r,   rj   r'   r'   r%   r(   r    s   r  c                   @   s   e Zd ZdZdS )DeletedVariablez"Marker used to implement delattr()Nrr   r'   r'   r'   r(   r%    s   r%  c                       sH   e Zd ZdZdhejZedd Z fddZdd Z	d	d
 Z
  ZS )StringFormatVariablez[
    Represents a call to str.format(), we delay calling format until after the graph.
    format_stringc                 C   s^   t dd t|| D rJtj|jdd |D dd | D S | |t	|t
|S )Nc                 s   s   | ]}|  V  qd S r   )r`   r   r'   r'   r(   r     s   z.StringFormatVariable.create.<locals>.<genexpr>c                 S   s   g | ]}|  qS r'   r  )r   r  r'   r'   r(   r    s     z/StringFormatVariable.create.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r'   r  r  r'   r'   r(   r     s      z/StringFormatVariable.create.<locals>.<dictcomp>)all	itertoolschainvaluesr   rA   rE   formatr   r   r   )r  r'  sym_args
sym_kwargsr'   r'   r(   rE     s    zStringFormatVariable.createc                    s2   t  jf | t|tst|| _|| _|| _d S r   )r   r    rU   ri   r.   r'  r-  r.  )r#   r'  r-  r.  r$   r%   r'   r(   r      s
    zStringFormatVariable.__init__c                 C   s&   | j j d| jd| jd| jdS r   )r&   re   r'  r-  r.  r   r'   r'   r(   r     s    zStringFormatVariable.__repr__c                 C   s   t jdkr|td ||| j ||d |t| j	 dd | j
 D }|t| |tddd d S )	Nr  r"  r,  c                 S   s   i | ]\}}t j||qS r'   )r   rA   rE   r  r'   r'   r(   r     s    
 z4StringFormatVariable.reconstruct.<locals>.<dictcomp>CALL_FUNCTION_EXr   )r   )r#  r$  r   r   Zcreate_load_constr'  Zcreate_load_attrr   r   r-  r.  r   r   )r#   r+   r$   r'   r'   r(   r,     s    
z StringFormatVariable.reconstruct)re   rf   rg   rs   r   rh   r   rE   r    r   r,   rj   r'   r'   r%   r(   r&    s   
r&  c                       sN   e Zd ZdZ fddZedd Zdd Zdd	 Zed
dddZ	  Z
S )DebuggingVariablez
    Represents a call to a debugging function like print(), or something
    registered to config.reorderable_logging_functions.
    c                    s   t  jf | || _d S r   r   r   r%   r'   r(   r      s    zDebuggingVariable.__init__c                 C   s(   t | o&t| tjtjfo&| tjjjkS r   )	r   rU   rW   rX   BuiltinFunctionTyperQ   r   r   Zreorderable_logging_functions)r   r'   r'   r(   is_reorderable_logging_function  s
    z1DebuggingVariable.is_reorderable_logging_functionc                 C   sP   |j r
d S | | j||s8td| j d| d| d |j| t|f d S )NzReordering debugging function z with inputs r  z is not yet implemented.)exportcan_reorder_logsr5   r
   Zdebug_localsr   r   r   r'   r'   r(   rY     s    zDebuggingVariable.call_functionc                 C   s   | j |S r   )r2   r,   r*   r'   r'   r(   r,     s    zDebuggingVariable.reconstructT)r?   c                 C   s:   t jt jtf}t||g}|D ]}t||s  dS q dS )zm
        Run some additional checks for what sort of function calls can we
        actually reorder.
        FT)r   r   rA   r&  pytreeZtree_leavesrU   )r   rJ   r$   Zallowed_input_typesZ	flat_argsr   r'   r'   r(   r4    s    
z"DebuggingVariable.can_reorder_logs)re   rf   rg   rs   r    r   r2  rY   r,   r4  rj   r'   r'   r%   r(   r0    s   
r0  c                       s2   e Zd ZdZ fddZdddddd	Z  ZS )
LoggingLoggerVariablez<
    Represents a call to any of logging.Logger methods
    c                    s   t  jf | d S r   r   r   r%   r'   r(   r    8  s    zLoggingLoggerVariable.__init__rG   rH   r   rI   c                 C   s   |j r
d S td d S )Nz)Logger not supported for non-export cases)r3  r
   r   r'   r'   r(   rc   ;  s    z!LoggingLoggerVariable.call_method)re   rf   rg   rs   r    rc   rj   r'   r'   r%   r(   r6  3  s   r6  c                       s$   e Zd Z fddZdd Z  ZS )StopIterationVariablec                    s   t  jf | || _d S r   )r   r    rJ   )r#   rJ   r$   r%   r'   r(   r    I  s    zStopIterationVariable.__init__c                 C   s.   | dd || j |t| jd d S )Nrm   StopIterationT)rn   ro   rJ   rY   r8   r*   r'   r'   r(   r,   M  s    z!StopIterationVariable.reconstructrp   r'   r'   r%   r(   r7  H  s   r7  c                       s   e Zd ZdZd ZzddlmZmZ	m
Z W n, ek
rT   eddi Z	eddi ZY nX  fddZdd	 Zd
d Zee eeef edddZeedddZ  ZS )ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericinvalid_typer'   c                    s   t  jf | || _d S r   r   r   r%   r'   r(   r    a  s    zConstantLikeVariable.__init__c                 C   s
   t | jS r   r  r   r'   r'   r(   r1   e  s    z ConstantLikeVariable.python_typec                 C   s   | j S r   r   r   r'   r'   r(   r0   h  s    z'ConstantLikeVariable.as_python_constantrI   c                 C   s   z$dd |D }dd |  D }W n6 tk
rZ   t| j d| d| d| d Y nX t| j|||}tj|rtj	|S t
|tjrt|S t| j d| d	|  d S )
Nc                 S   s   g | ]}|  qS r'   r  r   r'   r'   r(   r  t  s     z4ConstantLikeVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r'   r  r  r'   r'   r(   r   u  s      z4ConstantLikeVariable.call_method.<locals>.<dictcomp>r  z(*z, **r   z() -> )r   rv   r
   _error_prefixr/   r5   r   rA   rB   rE   rU   reMatchConstantRegexMatchVariable)r#   r:   r;   rJ   r$   ZcargsZckwargsr   r'   r'   r(   rc   k  s    (z ConstantLikeVariable.call_methodr>   c                 C   st   t | j|}t|| jr t|}t|| jr4t|S t|trRt|| j	rRt
|S tj|rjtj|S t| |S r   )r/   r5   rU   np_floatingfloatnp_dtypeNumpyDTypeVariabler4   r3   
np_genericr  r   rA   rB   rE   rC   r   r'   r'   r(   rF     s    z ConstantLikeVariable.var_getattr)re   rf   rg   rs   r>  r  r:  rD  r;  rB  r<  rF  ImportErrorr4   r    r1   r0   r   r   r   ri   rc   rF   rj   r'   r'   r%   r(   r9  S  s   
r9  c                   @   s   e Zd ZdZdS )RegexPatternVariablez
re.PatternNre   rf   rg   r>  r'   r'   r'   r(   rH    s   rH  c                   @   s   e Zd ZdZdS )rA  zre.MatchNrI  r'   r'   r'   r(   rA    s   rA  c                       s    e Zd ZdZ fddZ  ZS )TorchVersionVariableztorch.__version__c                    s2   | dtj |d tjks tt jf | d S )Nr5   )
setdefaultrQ   __version__r.   r   r    r   r%   r'   r(   r      s    zTorchVersionVariable.__init__)re   rf   rg   r>  r    rj   r'   r'   r%   r(   rJ    s   rJ  c                   @   s   e Zd ZdZdS )NumpyTypeInfoVariableznp.iinfo/np.finfoNrI  r'   r'   r'   r(   rM    s   rM  c                   @   s   e Zd ZdZdd ZdS )rE  znp.dtype[...]c                 C   s
   | j jjS )a0  Similar to how numpy dtype descriptors (e.g. np.float32 ) are handled by NumpyVariable:

        np.dtype() objects are serialized as strings, torch._numpy wrappers will normalize to the torch dtype.
        This also handles unsupported things nicely (i.e. structured arrays and object arrays).
        )r5   r4   re   r   r'   r'   r(   r     s    zNumpyDTypeVariable.as_proxyN)re   rf   rg   r>  r   r'   r'   r'   r(   rE    s   rE  )Zr]   r   r   r   r)  r?  r#  rW   r   r   r   Ztorch._CrQ   Ztorch._numpyr	  r  Ztorch.utils._pytreer  Z_pytreer5  r   r   r   Zbytecode_transformationr   r   excr
   Zguardsr   r   r2   r   r   r   r   r   r   r   r   r   baser   rx   r   r   Zuser_definedr   r   r   rk   rq   rt   ru   r   r   r   r   r   r   r   r   	dataclassr   r   rP   rC   r   r   r   r   	lru_cacher  r  r  r%  r&  r0  r6  r7  r9  rH  rA  rJ  rM  rE  ZfinfoZiinfor:  r  r'   r'   r'   r(   <module>   s    : 0j^

n,7=	   