o
    "i                      @   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                       sn   e Zd ZdhejZd fdd	Z fddZ fdd	Zd
eddfddZ							d fddZ
  ZS )SuperVariablespecializedNFc                    s(   t  jdi | || _|| _|| _d S N )super__init__typevarobjvarr   )selfr"   r#   r   kwargs	__class__r   Z/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.pyr!   &   s   
zSuperVariable.__init__c                    sT   |t t || j | jd ur || j |tdd d S |tdd d S )Nr   Tr   )r   BuiltinVariabler    r"   r#   extend_outputr   r$   codegenr&   r   r(   reconstruct3   s   


zSuperVariable.reconstructc           
         s   | j sJ d| jrt| j |S | j }| j  }| j jr&t| j jnd }t|t	r5| j j
}| j j}d }| j jd ure|j}||d }t|t|D ]}	t||	 |rdttt|d|	|} nqNtt||||fS )Nz1-arg super not implementedr   __mro__)r#   r   getattrr"   as_python_constantpython_typesourcer   
issubclasstypevaluer.   indexrangelenhasattrr   r   r    )
r$   txnamesearch_typetype_to_usetype_to_use_sourcer2   
search_mrostart_indexr6   r&   r   r(   _resolved_getattr_and_source<   s.   

	
	z*SuperVariable._resolved_getattr_and_sourcer;   returnr   c                 C   sV   |  | |\}}tj|st| |S |r%t|tj tjj	||dS tj	|S )Nr2   )
rA   r   ConstantVariable
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)r$   r:   r;   r5   r2   r   r   r(   var_getattrc   s   	
zSuperVariable.var_getattrargsList[VariableTracker]r%   Dict[str, VariableTracker]c                    s  |  | |\}}|tju rttS |tjjju rI| j}ddl	m
} t|tjrDt|j|rD|sD|sD|jj	|dtjd tjd S td nt|tjr_tj||d|| jg| |S t|tjrttj|j| j|d|||S |tjju rt| jtjr| jjrt|dkrt|dkr|d   rdd	l!m"}	 |d # }
|	|t$| jj|
tj| jj%|
S |tjj&tj'fv rt| jtj(r|st|dksJ t)tj(| j*|d
||S t+|rt| jtr| jj,|g|R i |S td|  d S )Nr   )AttributeMutationNew__call_nn_module_initTzsuper() nn.Module.__init__rC   r   r   VariableBuilder__setitem__znon-function or method super: )-rA   objectr!   LambdaVariabler   torchnnModuler#   side_effectsrN   
isinstancer   r   mutable_localoutput
store_attrrD   rI   r
   typesFunctionTyper   call_function
MethodTypeUserMethodVariable__func__collectionsOrderedDict__getitem__r2   r8   is_python_constantbuilderrQ   r0   r   r5   rR   __setattr__CustomizedDictVariabler    call_methodr   method_setattr_standard)r$   r:   r;   rK   r%   inner_fnr2   r#   rN   rQ   keyr&   r   r(   rj   t   s|   








zSuperVariable.call_method)NFrK   rL   r%   rM   rB   r   )__name__
__module____qualname__r   _nonvar_fieldsr!   r-   rA   strrJ   rj   __classcell__r   r   r&   r(   r       s     	'r   c                       $   e Zd Z fddZdd Z  ZS )ExceptionVariablec                    s"   t  jdi | || _|| _d S r   )r    r!   exc_typerK   )r$   rw   rK   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_fromrw   ro   foreachrK   r_   r8   r+   r   r   r(   r-      s   zExceptionVariable.reconstructro   rp   rq   r!   r-   rt   r   r   r&   r(   rv      s    rv   c                   @      e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nro   rp   rq   __doc__r   r   r   r(   r}          r}   c                   @   r|   )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nr~   r   r   r   r(   r      r   r   c                   @   s@   e Zd ZdZdd ZdeddfddZ			
				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)Nzcomptime is special form)NotImplementedErrorr+   r   r   r(   r-      s   zComptimeVariable.reconstructr;   rB   r   c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r   rC   )r   	functionsr   r/   r   r2   )r$   r:   r;   r   r   r   r   r(   rJ      s
   zComptimeVariable.var_getattrrK   rL   r%   rM   c                 C   s   ddl m} |r
J t|dksJ |d }t|tr$| || n;t|trV| }|jr8J d|j	 t
||j|j |jrJt|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: )r   r   r8   rY   r   get_functionr   get_codeclosureco_freevarsr]   r^   	f_globalsfn_namer0   defaultstupleitemsRuntimeErrorr4   r   rD   rI   )r$   r:   rK   r%   r   fncodefuncr   r   r(   r_      s.   

zComptimeVariable.call_functionNrn   )ro   rp   rq   r   r-   rs   rJ   r_   r   r   r   r(   r      s    
r   c                       0   e Zd ZdhejZ fddZdd Z  ZS )ClosureVariabler;   c                       t  jdi | || _d S r   r    r!   r;   r$   r;   r%   r&   r   r(   r!        
zClosureVariable.__init__c                 C      | || j d S Nappend_outputcreate_load_closurer;   r+   r   r   r(   r-        zClosureVariable.reconstructro   rp   rq   r}   rr   r!   r-   rt   r   r   r&   r(   r         r   c                       r   )InlinedClosureVariabler;   c                    r   r   r   r   r&   r   r(   r!     r   zInlinedClosureVariable.__init__c                 C   r   r   r   r+   r   r   r(   r-   !  r   z"InlinedClosureVariable.reconstructr   r   r   r&   r(   r     r   r   c                          e Zd Z fddZ  ZS )NewCellVariablec                       t  jdi | d S r   r    r!   r$   r%   r&   r   r(   r!   &  r   zNewCellVariable.__init__ro   rp   rq   r!   rt   r   r   r&   r(   r   %      r   c                       r   )NewGlobalVariablec                    r   r   r   r   r&   r   r(   r!   +  r   zNewGlobalVariable.__init__r   r   r   r&   r(   r   *  r   r   c                       sH   e Zd ZdZedd Zdef fddZdedd	f 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(   rI   2  s   zInspectSignatureVariable.create	inspectedc                    r   r   )r    r!   r   )r$   r   r%   r&   r   r(   r!   8  r   z!InspectSignatureVariable.__init__r;   rB   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   rD   rI   InspectParameterVariable).0r;   r   r   r(   
<dictcomp>?  s    z8InspectSignatureVariable.var_getattr.<locals>.<dictcomp>)user_cls)r   ConstDictVariabler   inspect_parameter_namesdictr    rJ   )r$   r:   r;   r&   r   r(   rJ   <  s   z$InspectSignatureVariable.var_getattr)ro   rp   rq   r   staticmethodrI   r   r!   rs   rJ   rt   r   r   r&   r(   r   /  s    
r   c                   @   r|   )r   z2This is not implemented, if used will graph break.Nr~   r   r   r   r(   r   H  s    r   c                    s    fdd}t |_|S )Nc                     s    j | i |S r   )applyrK   r%   fn_clsr   r(   trampoline_autograd_applyO  s   zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)!produce_trampoline_autograd_apply_origin)r   r   r   r   r(   r   N  s   r   c                       sV   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Z
  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr   c                    r   r   )r    r!   r   )r$   r   r%   r&   r   r(   r!   ^  r   z!AutogradFunctionVariable.__init__c                    s  d  fdd}t |||f  rt rtjrddlm} ddlm	} | j
j}|| j
j}|r;|| j
j| j
j}| j
j}	|	tjjjurJtd | j
j}
|
tjjjurYtd d	d
lm} | j}|d u rrt| j
j| j
j}||| j
j|t|ddd||}| jrt| jd}t|tj |rt| jd}t|tj |S | jrt| jd}nd }| j
j}t ||}|g|}t!|t"j#rt$j%||d||S t!|t"j&rt$j'|j(t$)| j
|d||S td|  d S )NFc                    s@   t | tjr| jdurd t | tjr| rd d S d S d S )NFT)rY   r   TensorVariablerequires_gradNNModuleVariable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   )memberrC   forwardsetup_contextz?non-function or method in subclass of torch.autograd.Function: )*r   r   rU   is_grad_enabledr   capture_autograd_function"torch._functorch.autograd_functionr   torch.autograd.functionr   r   r   r   vjpautogradFunctionr
   jvphigher_order_opsr   r2   r   import_sourcerp   ro   backwardr_   r   rG   r   FUNCTION_MATCHAutogradFunctionContextVariablerI   rY   r]   r^   r   r   r`   ra   rb   UserDefinedClassVariable)r$   r:   rK   r%   r   r   r   
forward_fnis_setup_ctx_definedvjp_fnjvp_fnr   r2   valfwd_src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	u sJ t
|tjs!J tj|| jd|||S )Nr   r   rC   )r   r   r   r2   r4   r5   rU   _dynamoexternal_utilsFakeBackwardCFunctionrY   r]   r^   r   r   r_   )r$   r:   rK   r%   r   r   r   r(   call_backward  s   z&AutogradFunctionVariable.call_backwardc                 C   
   t | jS r   )r   r   r$   r:   rK   r%   r   r   r(   r_        
z&AutogradFunctionVariable.call_functionrK   rL   r%   rM   c                 C   sX  ddl m} ddlm} |dkr3|| jr,t| j}|||jjd|gt||R  dS | 	|||S |dkr>| 
|||S dd	lm } | jd urOt| j|nd }	z	t| j|}
W n tye   d }
Y nw t|
tr|
| j}|	d ur||j||	d
|||S ||||||S t|
trtj|
j| |	d
|||S td|  d S )Nr   )is_callable_allowedr   )wrap_fx_proxyr   r_   )r:   proxyr   )trace_rulesrC   zUnsupported method: )r   r   rg   r   r   r   r[   create_proxyr   r   r    r2   r   inspectgetattr_staticAttributeErrorrY   r   __get__lookupcreate_with_sourcer_   classmethodr   ra   rb   r
   )r$   r:   r;   rK   r%   r   r   r   r   r2   objr   r   r   r(   rj     sX   
	



z$AutogradFunctionVariable.call_method)rK   rL   r%   rM   )ro   rp   rq   r   r   rr   r!   r   r   r_   rj   rt   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)
ro   rp   rq   dataclassesfieldlistr   r   r   __annotations__r   r   r   r(   r     s   
 r   c                       sx   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 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d||d| || _|| _|| _|| _d S )N)r5   
value_typer   )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   )rY   r   r   r   r   xr   r   r(   	<genexpr>*  s
    
z9AutogradFunctionContextVariable.create.<locals>.<genexpr>r_   T)r   r   r   r  )r   r[   r   rU   r   functionFunctionCtxrX   track_object_new	functoolspartialr   r   r   r   r5   )r:   rK   r%   r  r   outr   r   r(   rI   &  s,   z&AutogradFunctionContextVariable.createc                 C   s   | j d u r	td | j S )Nzproxy not set)r   r
   r$   r   r   r(   as_proxyA  s   
z(AutogradFunctionContextVariable.as_proxyrK   rL   r%   rM   rB   r   c                    s   |dkrt  ||||S |dkrtd|  | jd u r!td | js3| jr)|r+J |jj| | t	| jj
dkr?g | j_
|D ]	}| jj
| qAtjd S )Nrh   save_for_backwardz"autograd.Function context method: zCsave_for_backward only supported on a newly constructed FunctionCtxr   )r    rj   r
   r   r   r2   r[   rX   track_save_for_backwardr8   r   appendr   rD   rI   )r$   r:   r;   rK   r%   argr&   r   r(   rj   F  s    
z+AutogradFunctionContextVariable.call_methodc                    s    dkrt  fddS  dkr jd ur ttjjS  dkrFjd ur0tjjS j	rFddl
m} |tj	djjS t  S )Nr  c                     s     | |S r   )rj   r   r;   r$   r:   r   r(   <lambda>d  s    z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r   r  r   rP   )rT   r   r   TupleVariabler   r   r  rD   rI   r2   rg   rQ   r   r5   r    rJ   r$   r:   r;   rQ   r&   r  r(   rJ   a  s   
z+AutogradFunctionContextVariable.var_getattr)NFNNN)NNrn   )ro   rp   rq   r   r   rr   r!   r   rI   r  rj   rJ   rt   r   r   r&   r(   r   
  s2    
r   c                       s2   e Zd Z fddZ						dd	d
Z  ZS )rT   c                    r   r   )r    r!   r   )r$   r   r%   r&   r   r(   r!   u  r   zLambdaVariable.__init__rK   rL   r%   rM   rB   r   c                 C   s   | j |i |S r   )r   r   r   r   r(   r_   y  s   zLambdaVariable.call_functionrn   )ro   rp   rq   r!   r_   rt   r   r   r&   r(   rT   t  s    rT   c                       s   e Zd ZdhejZ fddZdd Zedej	j
fddZd	d
 Zdd Zdd Z						dddZdee deeef def fddZ  ZS )rF   r;   c                    s>   t  jdi | t|tsJ t|tsJ || _|| _d S r   )r    r!   rY   r   rs   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'   ro   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   )rF   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	vrtt
|| j}||j	vr'tt
||S r   )rY   r   r   r   r   r[   get_submodule
module_keyr;   __dict__r   r   )r$   r:   r;   step1step2r   r   r(   const_getattr  s   
zGetAttrVariable.const_getattrc                 C   s    || j  ||| j d S r   )r   r*   create_load_attrsr;   r+   r   r   r(   r-     s   
zGetAttrVariable.reconstructrK   rL   r%   rM   rB   r   c                 C   s   | j || j||S r   )r   rj   r;   r   r   r   r(   r_     s   zGetAttrVariable.call_functionc                    s  |dv rD| j dkrD|sD|d  rDt| jtjtjfrD| j}|d  }|||r0|	||S |dkrCt
|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  }|||rytdS td	S t ||||S )
N)re   getr  r   r$  r   r   __contains__TF)r;   rf   rY   r   r   r   r   r0   has_key_in_generic_dictrJ   r8   rD   r    rj   )r$   r:   r;   rK   r%   r   rm   r&   r   r(   rj     sL   








zGetAttrVariable.call_methodrn   )ro   rp   rq   r   rr   r!   r  r   rU   fxProxyr  r  r"  r-   r_   r   r   rs   rj   rt   r   r   r&   r(   rF     s4    

rF   c                       sF   e Zd Z fddZ						d fd	d
Zdd Zdd Z  ZS )MethodWrapperVariablec                    r   r   )r    r!   method_wrapper)r$   r*  r%   r&   r   r(   r!     r   zMethodWrapperVariable.__init__rK   rL   r%   rM   rB   r   c                    s`   t | jr&t|d tjr&t|dkrt|dksJ |d || jjjS t	 
||| d S )Nr   r   )r   r*  rY   r   r   r8   rJ   __self__ro   r    r_   r   r&   r   r(   r_     s   
z#MethodWrapperVariable.call_functionc                 C      dS NTr   r  r   r   r(   rf        z(MethodWrapperVariable.is_python_constantc                 C      | j S r   )r*  r  r   r   r(   r0        z(MethodWrapperVariable.as_python_constantrn   )ro   rp   rq   r!   r_   rf   r0   rt   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                    r   r   )r    r!   desc)r$   r2  r%   r&   r   r(   r!     r   z!GetSetDescriptorVariable.__init__c                    sB   |dkr| j rddlm} ||t| j d| jjS t ||S )Nr   r   rP   )r2   rg   rQ   r   r2  r   r    rJ   r  r&   r   r(   rJ     s   z$GetSetDescriptorVariable.var_getattrc                 C   r,  r-  r   r  r   r   r(   rf     r.  z+GetSetDescriptorVariable.is_python_constantc                 C   r/  r   )r2  r  r   r   r(   r0     r0  z+GetSetDescriptorVariable.as_python_constant)ro   rp   rq   r!   rJ   rf   r0   rt   r   r   r&   r(   r1    s
    
r1  c                       sV   e Zd ZddhejZdejf fddZdd Zdd Z	d	d
 Z
 fddZ  ZS )PythonModuleVariabler5   is_torchc                    s6   t  jdi | || _| jtu p| jjd| _d S )Nztorch.r   )r    r!   r5   rU   ro   
startswithr4  r$   r5   r%   r&   r   r(   r!     s   zPythonModuleVariable.__init__c                 C   s   t jS r   )r]   
ModuleTyper  r   r   r(   r1     r0  z PythonModuleVariable.python_typec                 C   r/  r   r5   r  r   r   r(   r0     r0  z'PythonModuleVariable.as_python_constantc                 C   s   d| j  dS )NzPythonModuleVariable(r  r8  r  r   r   r(   __repr__  s   zPythonModuleVariable.__repr__c                    s,   | j rt| j|}tj|S t ||S r   )r4  r9   r5   r   rD   rI   r    call_hasattrr$   r:   r;   resultr&   r   r(   r:     s   z!PythonModuleVariable.call_hasattr)ro   rp   rq   r   rr   r]   r7  r!   r1   r0   r9  r:  rt   r   r   r&   r(   r3    s    r3  c                       sB   e Zd Z fddZ						dd	d
Zdd Zdd Z  ZS )TypingVariablec                    r   r   r    r!   r5   r6  r&   r   r(   r!   (  r   zTypingVariable.__init__rK   rL   r%   rM   rB   r   c                 C   s:   |dkrt |dkrtj| j|d   S td d S )Nre   r   r   typing)r8   r   rD   rI   r5   r0   r
   r$   r:   r;   rK   r%   r   r   r(   rj   ,  s
   zTypingVariable.call_methodc                 C   r   r   r4   r5   r  r   r   r(   r1   9  r   zTypingVariable.python_typec                 C   r/  r   r8  r  r   r   r(   r0   <  r0  z!TypingVariable.as_python_constantrn   )ro   rp   rq   r!   rj   r1   r0   rt   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)utilsrC  r   r  r   r/   )rC  np_fn_to_tnp_fnnp_modtnp_modr   tnp_fnnp_fnr   r   r(   get_np_to_tnp_map@  s   rJ  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Z				
			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                    r   r   r>  r6  r&   r   r(   r!   X  r   zNumpyVariable.__init__c                 C   s:   |j d}t|dkr|d d ddgksJ || jv S N.r   rU   _numpy)rp   splitr8   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sJ t|d S rL  )rp   rO  r8   np_constant_collections_mapr$  rQ  r   r   r(    get_constant_collection_for_funcb  s   $z.NumpyVariable.get_constant_collection_for_funcrK   rL   r%   rM   rB   r   c           
   	   C   s  t jstd| j d ddlm} ddlm} t 	| j}|d u r,td| j d | 
| }d urez|| jd	d
 |D i dd | D W S  tyd   t| jj d| d|  Y d S w |jdkrt jrd|j d}|dt j 7 }t| ||j||\}}| |rt||rtj|  dd
 |D i dd | D S |jjd||gt||R  }	|||	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      g | ]}|  qS r   r0   r  r   r   r(   
<listcomp>      z/NumpyVariable.call_function.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r   rZ  r   kvr   r   r(   r         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   rY  r   rZ  r  r   r   r(   r[    r\  c                 S   r]  r   rZ  r^  r   r   r(   r     ra  r_   )r   trace_numpyr
   r5   rD  rW  tensorrX  rJ  r$  rV  r   r   ro   rp   use_numpy_random_streamrq   
patch_argsrT  r   r   rD   rI   r0   r[   r   r   )
r$   r:   rK   r%   rW  rX  r   collection_variable_typmsgr   r   r   r(   r_   h  sb   


zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpyr	   r@  r   r   r(   rj     s   zNumpyVariable.call_methodc                 C   r   r   rA  r  r   r   r(   r1     r   zNumpyVariable.python_typec                 C   r/  r   r8  r  r   r   r(   r0     r0  z NumpyVariable.as_python_constantc                    s$   t jrt| jtr| jjS t  S r   )r   rc  rY   r5   r4   ro   r    r  r  r&   r   r(   r    s   
zNumpyVariable.as_proxyrn   )ro   rp   rq   r   tnp
issubdtyperP  r!   r   rT  rV  r_   rj   r1   r0   r  rt   r   r   r&   r(   rK  Q  s2    


=
	rK  c                       s,   e Zd Z fddZdd Zdd Z  ZS )NullVariablec                    r   r   r   r   r&   r   r(   r!     r   zNullVariable.__init__c                 C   r,  )Nrl  r   r  r   r   r(   r    r.  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)ro   rp   rq   r!   r  r-   rt   r   r   r&   r(   rl    s    rl  c                   @   r|   )DeletedVariablez"Marker used to implement delattr()Nr~   r   r   r   r(   rs    r   rs  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   sb   t dd t|| D r'tj|jdd |D i dd | D S | |t	|t
|S )Nc                 s   s    | ]}|  V  qd S r   )rf   r  r   r   r(   r    s
    
z.StringFormatVariable.create.<locals>.<genexpr>c                 S   rY  r   rZ  )r   r`  r   r   r(   r[    r\  z/StringFormatVariable.create.<locals>.<listcomp>c                 S   r]  r   rZ  r^  r   r   r(   r     ra  z/StringFormatVariable.create.<locals>.<dictcomp>)all	itertoolschainvaluesr   rD   rI   formatr   r   r   )rR  ru  sym_args
sym_kwargsr   r   r(   rI     s   zStringFormatVariable.createc                    s6   t  jdi | t|tsJ || _|| _|| _d S r   )r    r!   rY   rs   ru  r{  r|  )r$   ru  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'   ro   ru  r{  r|  r  r   r   r(   r9    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 )	Nrm  rp  rz  c                 S   s   i | ]\}}t j||qS r   )r   rD   rI   r^  r   r   r(   r     s    z4StringFormatVariable.reconstruct.<locals>.<dictcomp>CALL_FUNCTION_EXr   )r  )rq  rr  r   r   create_load_constru  create_load_attrr   r  r{  r|  r   r   )r$   r,   r%   r   r   r(   r-     s   
z StringFormatVariable.reconstruct)ro   rp   rq   r   r   rr   r   rI   r!   r9  r-   rt   r   r   r&   r(   rt    s    
rt  c                       sJ   e Zd ZdZ fddZedd Zdd Zdd	 Ze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                    r   r   r>  r6  r&   r   r(   r!     r   zDebuggingVariable.__init__c                 C   s(   t | ot| tjtjfo| tjjjv S r   )	r   rY   r]   r^   BuiltinFunctionTyperU   r   r   reorderable_logging_functions)r   r   r   r(   is_reorderable_logging_function  s
   z1DebuggingVariable.is_reorderable_logging_functionc                 C   sP   |j rd S | | j||std| j d| d| d |j| t|f d S )NzReordering debugging function z with inputs rb  z is not yet implemented.)exportcan_reorder_logsr5   r
   debug_localsr  r   r   r   r   r(   r_     s   
zDebuggingVariable.call_functionc                 C   s   | j |S r   )r2   r-   r+   r   r   r(   r-     s   zDebuggingVariable.reconstructrB   Tc                 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   rD   rt  pytreetree_leavesrY   )r   rK   r%   allowed_input_types	flat_argsr  r   r   r(   r    s   
z"DebuggingVariable.can_reorder_logs)rB   T)ro   rp   rq   r   r!   r   r  r_   r-   r  rt   r   r   r&   r(   r    s    
r  c                       s6   e Zd ZdZ fddZ							dd
dZ  ZS )LoggingLoggerVariablez<
    Represents a call to any of logging.Logger methods
    c                    s   t  jdi | d S r   r   r6  r&   r   r(   r!   8  r   zLoggingLoggerVariable.__init__rK   rL   r%   rM   rB   r   c                 C   s   |j rd S td d S )Nz)Logger not supported for non-export cases)r  r
   r@  r   r   r(   rj   ;  s   z!LoggingLoggerVariable.call_methodrn   )ro   rp   rq   r   r!   rj   rt   r   r   r&   r(   r  3  s    r  c                       ru   )StopIterationVariablec                    r   r   )r    r!   rK   )r$   rK   r%   r&   r   r(   r!   I  r   zStopIterationVariable.__init__c                 C   s.   | dd || j |t| jd d S )Nrx   StopIterationT)ry   rz   rK   r_   r8   r+   r   r   r(   r-   M  s   z!StopIterationVariable.reconstructr{   r   r   r&   r(   r  H  s    r  c                       s   e Zd ZdZd ZzddlmZmZ	m
Z W n ey)   eddi Z	eddi ZY nw  fddZdd	 Zd
d Zdee deeef defddZdedefddZ  ZS )ConstantLikeVariablez8self.value is a compile-time constant, but not a literalr   )dtypefloatinggenericinvalid_typer   c                    r   r   r>  r6  r&   r   r(   r!   a  r   zConstantLikeVariable.__init__c                 C   r   r   rA  r  r   r   r(   r1   e  r   z ConstantLikeVariable.python_typec                 C   r/  r   r8  r  r   r   r(   r0   h  r0  z'ConstantLikeVariable.as_python_constantrK   r%   rB   c                 C   s   zdd |D }dd |  D }W n ty,   t| j d| d| d| d Y nw t| j||i |}tj|rDtj	|S t
|tjrNt|S t| j d| d	|  d S )
Nc                 S   rY  r   rZ  r  r   r   r(   r[  t  r\  z4ConstantLikeVariable.call_method.<locals>.<listcomp>c                 S   r]  r   rZ  r^  r   r   r(   r   u  ra  z4ConstantLikeVariable.call_method.<locals>.<dictcomp>rM  z(*z, **r  z() -> )r   r   r
   _error_prefixr/   r5   r   rD   rE   rI   rY   reMatchConstantRegexMatchVariable)r$   r:   r;   rK   r%   cargsckwargsr<  r   r   r(   rj   k  s   &z ConstantLikeVariable.call_methodr;   c                 C   st   t | j|}t|| jrt|}t|| jrt|S t|tr)t|| j	r)t
|S tj|r5tj|S t| |S r   )r/   r5   rY   np_floatingfloatnp_dtypeNumpyDTypeVariabler4   r3   
np_genericrK  r   rD   rE   rI   rF   r;  r   r   r(   rJ     s   
z ConstantLikeVariable.var_getattr)ro   rp   rq   r   r  ri  r  r  r  r  r  r  ImportErrorr4   r!   r1   r0   r   r   r   rs   rj   rJ   rt   r   r   r&   r(   r  S  s(    

r  c                   @   r|   )RegexPatternVariablez
re.PatternNro   rp   rq   r  r   r   r   r(   r    r   r  c                   @   r|   )r  zre.MatchNr  r   r   r   r(   r    r   r  c                       s    e Zd ZdZ fddZ  ZS )TorchVersionVariableztorch.__version__c                    s6   | dtj |d tju sJ t jdi | d S )Nr5   r   )
setdefaultrU   __version__r    r!   r   r&   r   r(   r!     s   zTorchVersionVariable.__init__)ro   rp   rq   r  r!   rt   r   r   r&   r(   r    s    r  c                   @   r|   )NumpyTypeInfoVariableznp.iinfo/np.finfoNr  r   r   r   r(   r    r   r  c                   @   s   e Zd ZdZdd ZdS )r  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   ro   r  r   r   r(   r    s   
zNumpyDTypeVariable.as_proxyN)ro   rp   rq   r  r  r   r   r   r(   r    s    r  )Zrc   r   r	  r   rw  r  rq  r]   r?  r   r   torch._CrU   torch._numpyrN  rj  torch.utils._pytreerD  _pytreer  r   r   r   bytecode_transformationr   r   excr
   guardsr   r   r2   r   r   r   r   r   r   r   r   r   baser   r   r   r   user_definedr   r   r   rv   r}   r   r   r   r   r   r   r   r   r   r   	dataclassr   r   rT   rF   r)  r1  r3  r=  	lru_cacherJ  rK  rl  rs  rt  r  r  r  r  r  r  r  r  r  finfoiinfor  rU  r   r   r   r(   <module>   s    : 0j^

n,7=	