o
    I&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Z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% d
dl&m'Z'm(Z( d
dl)m*Z* d
dl+m,Z,m-Z-m.Z. d
dl/m0Z0 G dd de(Z1G dd de(Z2G dd de2Z3G dd de(Z4G dd de2Z5G dd de2Z6G dd de(Z7G dd de(Z8G dd  d e(Z9G d!d" d"e(Z:d#d$ Z;d%d& Z<d'd( Z=G d)d* d*e(Z>ej?G d+d, d,Z@G d-d. d.e0ZAG d/d0 d0e(ZBG d1d2 d2e(ZCG d3d4 d4e(ZDG d5d6 d6e(ZEG d7d8 d8e(ZFG d9d: d:e(ZGG d;d< d<e(ZHejId
d=d>d? ZJG d@dA dAe(ZKG dBdC dCe(ZLG dDdE dEe(ZMdS )F    N)DictList   )configpolyfill	variables)create_call_functioncreate_instructionunimplemented)GuardBuilderinstall_guard)
AttrSourceGetItemSourceODictGetItemSource
TypeSource)check_constant_argsidentityis_tensor_base_attr_getterproxy_args_kwargs   )MutableLocalVariableTracker)DefaultDictVariable)NestedUserFunctionVariableUserFunctionVariableUserMethodVariable)UserDefinedObjectVariablec                       s^   e Zd Zd fdd	Z fddZ fddZd	ed
dfddZ					
	dddZ  Z	S )SuperVariableNFc                    s(   t  jdi | || _|| _|| _d S N )super__init__typevarobjvarspecialized)selfr#   r$   r%   kwargs	__class__r    GC:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/variables/misc.pyr"   #   s   
zSuperVariable.__init__c                    s@   |t t || j | jd ur|| j tddS tddS )Nr   Tr   )r   BuiltinVariabler!   r#   r$   r   r&   Zcodegenr(   r    r*   reconstruct)   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nameZsearch_typeZtype_to_useZtype_to_use_sourcer2   Z
search_mroZstart_indexr6   r(   r    r*   _resolved_getattr_and_source2   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   )
r<   r   ConstantVariable
is_literalGetAttrVariabler   
make_guardr   ZCONSTANT_MATCHcreate)r&   r:   r;   r5   r2   r    r    r*   var_getattrU   s   	
zSuperVariable.var_getattrargsList[VariableTracker]r'   Dict[str, VariableTracker]c                 C   s  |  | |\}}|tju rttS |tjjju rJ| 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 d S t|tjr`tj||d|| jg| |S t|tjrut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|rtj)*|d r| jjr|st|dksJ tj)+|d }t,| jj-}|d ||< |.| j| j/|S td
|  d S )Nr   )AttributeMutationNewZ__call_nn_module_initTzsuper() nn.Module.__init__r?   r   r   VariableBuilderznon-function or method super: )0r<   objectr"   LambdaVariabler   torchnnModuler$   side_effectsrI   
isinstancer   r   mutable_localoutputZ
store_attrr@   rD   r   typesFunctionTyper   call_function
MethodTyper   __func__collectionsOrderedDict__getitem__r2   r8   is_python_constantbuilderrK   r0   r   r5   __setitem____setattr__ZCustomizedDictVariableConstDictVariableZis_valid_keyget_keydictitemsZreplace_allZmodifed)r&   r:   r;   rF   r'   Zinner_fnr2   r$   rI   rK   keykZnewvalr    r    r*   call_methodf   s   







zSuperVariable.call_method)NFrF   rG   r'   rH   r=   r   )
__name__
__module____qualname__r"   r-   r<   strrE   rg   __classcell__r    r    r(   r*   r   "   s    	#r   c                   @      e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nri   rj   rk   __doc__r    r    r    r*   ro          ro   c                   @   rn   )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nrp   r    r    r    r*   rs      rr   rs   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;   r=   r   c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r   r?   )rv   	functionsr   r/   r   r2   )r&   r:   r;   rv   r   r    r    r*   rE      s
   zComptimeVariable.var_getattrrF   rG   r'   rH   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   r   zOcomptime function must not have free variables, but these variables were free: z"unsupported argument to comptime: )rv   rx   r8   rR   r   Zget_functionr   get_codeclosureco_freevarsrU   rV   	f_globalsfn_namer0   defaultstuplerd   RuntimeErrorr4   r   r@   rD   )r&   r:   rF   r'   rx   fncodefuncr    r    r*   rW      s.   

zComptimeVariable.call_functionNrh   )ri   rj   rk   rq   r-   rl   rE   rW   r    r    r    r*   rt      s    
rt   c                       $   e Zd Z fddZdd Z  ZS )ClosureVariablec                       t  jdi | || _d S r   r!   r"   r;   r&   r;   r'   r(   r    r*   r"         
zClosureVariable.__init__c                 C      | | jgS NZcreate_load_closurer;   r,   r    r    r*   r-         zClosureVariable.reconstructri   rj   rk   r"   r-   rm   r    r    r(   r*   r          r   c                       r   )InlinedClosureVariablec                    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"        zNewCellVariable.__init__ri   rj   rk   r"   rm   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*   rD     s   zInspectSignatureVariable.create	inspectedc                    r   r   )r!   r"   r   )r&   r   r'   r(   r    r*   r"     r   z!InspectSignatureVariable.__init__r;   r=   r   c                    s4   |dkrt jdd | j D tdS t ||S )N
parametersc                 S   s   i | ]}|t  qS r    )InspectParameterVariable).0r;   r    r    r*   
<dictcomp>  s    z8InspectSignatureVariable.var_getattr.<locals>.<dictcomp>)Zuser_cls)r   ra   r   Zinspect_parameter_namesrc   r!   rE   r&   r:   r;   r(   r    r*   rE     s   z$InspectSignatureVariable.var_getattr)ri   rj   rk   rq   staticmethodrD   r   r"   rl   rE   rm   r    r    r(   r*   r     s    
r   c                   @   rn   )r   z2This is not implemented, if used will graph break.Nrp   r    r    r    r*   r   '  s    r   c                        fdd}t |_|S )Nc                         j | i |S r   )forwardrF   r'   fn_clsr    r*   trampoline_autograd_fwd.     z@produce_trampoline_autograd_fwd.<locals>.trampoline_autograd_fwd)produce_trampoline_autograd_fwd_origin)r   r   r    r   r*   r   -     r   c                    r   )Nc                     r   r   )backwardr   r   r    r*   trampoline_autograd_bwd6  r   z@produce_trampoline_autograd_bwd.<locals>.trampoline_autograd_bwd)produce_trampoline_autograd_bwdr   )r   r   r    r   r*   r   5  r   r   c                    r   )Nc                     r   r   )applyr   r   r    r*   trampoline_autograd_apply>  r   zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)!produce_trampoline_autograd_applyr   )r   r   r    r   r*   r   =  r   r   c                       sB   e Zd ZdZ fddZdd Zdd Z				
	dddZ  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassc                    r   r   )r!   r"   r   )r&   r   r'   r(   r    r*   r"   H  r   z!AutogradFunctionVariable.__init__c              	      s,  d  fdd}t |||f t}|g|} rt rtjr| jj	tj
jjj	kr2td | jj}|tj
jjurAtd | jj}|tj
jjurPtd ddlm}m}	 t| j}
t| j}t| j}jj }}t| jj| jj}tjjj jjj!d	d
}|	j"|t|d|d}|#||}t$|t%j&r|g|j'}n||g}||||	j"|t|d|d| |dd  }|	j"|
d d#||S | j(rtt| j(dd}nd }| jj)}t$|t*j+rt%j,||d#||S t$|t*j-rt%j.|j/t%0| j|d#||S td|  d S )NFc                    s8   t | tjr| jdurd t | tjr| rd | S )NFT)rR   r   TensorVariablerequires_gradNNModuleVariableZis_training)noder   r:   r    r*   visitO  s   

z2AutogradFunctionVariable.call_apply.<locals>.visitz8NYI - autograd.Function with custom setup_context methodzNYI - User defind vjpzNYI - User defind jvpr   )safe_or_raise_always_restore TorchHigherOrderOperatorVariablezautograd.Function)parentZsource_targetr   )r2   fwd_bwd_tracerr   )r   r)   r?   z?non-function or method in subclass of torch.autograd.Function: )1r   r   AutogradFunctionContextVariablerD   rN   Zis_grad_enabledr   Zcapture_autograd_functionr   Zsetup_contextautogradfunctionZ_SingleLevelFunctionr   ZvjpFunctionZjvpZhigher_order_opsr   r   r   r   r   rT   graphZcopy_graphstater   Zimport_sourcerj   ri   Z_dynamoZoutput_graphZSubgraphTracerZcurrent_tracermakerW   rR   r   TupleVariablerd   r2   r   rU   rV   r   rX   r   rY   UserDefinedClassVariable)r&   r:   rF   r'   r   ctxZvjp_fnZjvp_fnr   r   r   r   r   Zgraph_checkpoint
checkpointZmodule_sourcer   Zhigher_order_autograd_fnZspeculated_fwd_resultZbwd_argsr2   r   r    r   r*   
call_applyL  s   









z#AutogradFunctionVariable.call_applyc                 C   
   t | jS r   )r   r   r&   r:   rF   r'   r    r    r*   rW        
z&AutogradFunctionVariable.call_functionrF   rG   r'   rH   c           
      C   s  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|
 C t| jjtjrKt| jj}n$t| jjtjrft| jjjt| j}|jg| }n	td	| jj  |||||W  d    S 1 sw   Y  d S |d
krt| jjtjrt| jj}	n$t| jjtjrt| jjjt| j}	|	jg| }n	td| jj  |||	||S td|  d S )Nr   )is_user_defined_allowedr   )wrap_fx_proxyr   rW   )r:   proxyr   z&backward is a non-function or method: r   z%forward is a non-function or method: zUnsupported method: )Zallowed_functionsr   r^   r   r   r   rT   create_proxyr   r   Zstrict_translation_moderR   r   rU   rV   r   rX   r   rY   r   r   objr   Zinline_callr   )
r&   r:   r;   rF   r'   r   r   r   r   r   r    r    r*   rg     s\   
	

$
z$AutogradFunctionVariable.call_method)rF   rG   r'   rH   )	ri   rj   rk   rq   r"   r   rW   rg   rm   r    r    r(   r*   r   E  s     r   c                   @   s&   e Zd ZU ejedZee e	d< dS )SavedTensorBox)default_factorytensorsN)
ri   rj   rk   dataclassesfieldlistr   r   r   __annotations__r    r    r    r*   r     s   
 r   c                       sn   e Zd ZdZddhejZ				d fdd	Zedd	 Zd
d Z							dddZ
 fddZ  ZS )r   zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    r   	inferenceNFc                    s.   t  jd||d| || _|| _|| _d S )N)r5   
value_typer    )r!   r"   r   r   saved_tensors)r&   r5   r   r   r   r   r'   r(   r    r*   r"     s   	
z(AutogradFunctionContextVariable.__init__c              
   C   sV   | j dtjjjt i }| j jd tjjjt	j
td|t di }|j|jjd< |S )NrW   T)r   r   r   Zexample_value)rT   r   rN   r   r   ZFunctionCtxr   rQ   Ztrack_object_new	functoolspartialr   r   r5   r   meta)r:   r   outr    r    r*   rD   -  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_proxy@  s   
z(AutogradFunctionContextVariable.as_proxyrF   rG   r'   rH   r=   r   c                 C   sp   |dkrt d|  | jd u rt d | js&| jr|rJ |jj| | |D ]	}| jj| q(t	j
d S )Nsave_for_backwardz"autograd.Function context method: zCsave_for_backward only supported on a newly constructed FunctionCtx)r   r   r   r2   rT   rQ   Ztrack_save_for_backwardr   appendr   r@   rD   )r&   r:   r;   rF   r'   argr    r    r*   rg   E  s   
z+AutogradFunctionContextVariable.call_methodc                    sN    dkrt  fddS  dkr jd ur ttjjS t  S )Nr   c                     s     | |S r   )rg   r   r;   r&   r:   r    r*   <lambda>^  s    z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r   )rM   r   r   r   r   r   r!   rE   r   r(   r   r*   rE   [  s   z+AutogradFunctionContextVariable.var_getattr)NFNNrh   )ri   rj   rk   rq   r   Z_nonvar_fieldsr"   r   rD   r   rg   rE   rm   r    r    r(   r*   r     s.    	

r   c                       s2   e Zd Z fddZ						dd	d
Z  ZS )rM   c                    r   r   )r!   r"   r   )r&   r   r'   r(   r    r*   r"   f  r   zLambdaVariable.__init__rF   rG   r'   rH   r=   r   c                 C   s   | j |i |S r   )r   r   r    r    r*   rW   j  s   zLambdaVariable.call_functionrh   )ri   rj   rk   r"   rW   rm   r    r    r(   r*   rM   e  s    rM   c                       sh   e Zd 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  ZS )rB   c                    s>   t  jdi | t|tsJ t|tsJ || _|| _d S r   )r!   r"   rR   r   rl   r   r;   )r&   r   r;   r'   r(   r    r*   r"   q  s
   
zGetAttrVariable.__init__c                 C   s   | j j d| j d| j dS )N(z, ))r)   ri   r   r;   r   r    r    r*   __str__x  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   )rB   r   r   r   r;   r   r    r    r*   r     s   zGetAttrVariable.as_proxyc                 C   s`   t | jtjs
t |j| jj}| j|j	vrt t
|| j}||j	vr*t t
||S r   )rR   r   r   r   ru   rT   Zget_submoduleZ
module_keyr;   __dict__inspectgetattr_static)r&   r:   r;   Zstep1Zstep2r    r    r*   const_getattr  s   
zGetAttrVariable.const_getattrc                 C   s   || j  || jS r   )r   Zcreate_load_attrsr;   r,   r    r    r*   r-     s   
zGetAttrVariable.reconstructrF   rG   r'   rH   r=   r   c                 C   s   | j || j||S r   )r   rg   r;   r   r    r    r*   rW     s   zGetAttrVariable.call_functionrh   )ri   rj   rk   r"   r   r   rN   ZfxZProxyr   r   r   r-   rW   rm   r    r    r(   r*   rB   p  s    rB   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__rF   rG   r'   rH   r=   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   rR   r   r   r8   rE   __self__ri   r!   rW   r   r(   r    r*   rW     s   
z#MethodWrapperVariable.call_functionc                 C      dS NTr    r   r    r    r*   r]        z(MethodWrapperVariable.is_python_constantc                 C      | j S r   )r   r   r    r    r*   r0        z(MethodWrapperVariable.as_python_constantrh   )ri   rj   rk   r"   rW   r]   r0   rm   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&   r   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 )N__get__r   rJ   )r2   r^   rK   r   r   r   r!   rE   )r&   r:   r;   rK   r(   r    r*   rE     s   z$GetSetDescriptorVariable.var_getattrc                 C   r   r   r    r   r    r    r*   r]     r   z+GetSetDescriptorVariable.is_python_constantc                 C   r   r   )r   r   r    r    r*   r0     r   z+GetSetDescriptorVariable.as_python_constant)ri   rj   rk   r"   rE   r]   r0   rm   r    r    r(   r*   r     s
    
r   c                       s,   e Zd Zdejf fddZdd Z  ZS )PythonModuleVariabler5   c                    r   r   r!   r"   r5   r&   r5   r'   r(   r    r*   r"     r   zPythonModuleVariable.__init__c                 C   s   t jS r   )rU   
ModuleTyper   r    r    r*   r1     r   z PythonModuleVariable.python_type)ri   rj   rk   rU   r   r"   r1   rm   r    r    r(   r*   r     s    r   c                       s   e Zd Zd fdd	Zdd Zdd Zedd	 Zee	
dd
d Z						dddZ						d fddZ  ZS )SkipFilesVariableNc                    s"   t  jdi | || _|| _d S r   )r!   r"   r5   reason)r&   r5   r  r'   r(   r    r*   r"     s   
zSkipFilesVariable.__init__c                 C   r   r   r4   r5   r   r    r    r*   r1     r   zSkipFilesVariable.python_typec                 C   r   r   r5   r   r    r    r*   r0     r   z$SkipFilesVariable.as_python_constantc                 C   s   t |tj | ||dS r>   )r   rC   r   ZFUNCTION_MATCH)clsr5   r2   r    r    r*   create_with_source  s
   z$SkipFilesVariable.create_with_sourcec                   C   s   t jtjiS r   )rZ   
namedtupler   r   r    r    r    r*    fold_through_function_to_wrapper  s   z2SkipFilesVariable.fold_through_function_to_wrapperrF   rG   r'   rH   r=   r   c                    s.  ddl m} t| jddrtd| j  d S | jtju r,|jtjg R i S | jtj	u rKt
 dkrKt d rKti tj	 d t dS | j|   v rzt rz| jdd	  D i d
d  D }|  | j|t dS | jtju rstfdd D rfdd	 D }g }tj| D ]}|tt| qtj|t dS | jtju rstfdd D rfdd	 D }g }tj| D ]}|| qtj|t dS | jtju rddl m} tdd  D rtddt ddh   d}	t
 dv r] d  r] d !}
dv r=t
 dkr=d j"}n$t
 dkrJ d j"}nt
 dkrX|t#j$j"}n	td ntd g }|	d urm||	 |
D ]0}|	d u ry|}	n z
||	|gi }	W n t%y   td| d| |	 dw ||	 qotj|t dS | jtj&u rst
 dkr d  r d ' r d !} d ( }g }t&||D ]}|tt| qtj|t dS | jtj)u rtdd  D rtddt dh   d d! t
 dkrA d  rA d !}
dv r>fd"d#nd }ntd$ g }z1tj)|
|d%D ]'\}}|tjtj*+|rdtj*,|n|tjt|t dgt d qOW n t%y   td&w tj|t dS | jt-j.u rst
 dkr d j/d us d 0j1j2r fd'd(}t3|S | jtj4u rst
 dkrg }nt
 dkr d  r d !}ntd) tj5j6|t dS | jt-j7u r std*  d } dd  }tj8j9||d+S | jtj:u r=t
 dk r+tj; d,t iS dd-l<m=} >| t?j: S | jtj@u rMtjA d,t iS | jtjBu r]tjC d,t iS ztD| j}W n tEyv   d.| jjF }Y nw d/| jjG d0| d1}|| jHrd2| jH d1nd37 }t| d S )4Nr   r+   Z_torchdynamo_disableFz.call torch._dynamo.disable() wrapped function r   )rS   c                 S   s   g | ]}|  qS r    r0   )r   xr    r    r*   
<listcomp>  s    z3SkipFilesVariable.call_function.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r    r	  )r   rf   vr    r    r*   r     s    z3SkipFilesVariable.call_function.<locals>.<dictcomp>c                 3       | ]}|  V  qd S r   has_unpack_var_sequencer   r   r:   r    r*   	<genexpr>      z2SkipFilesVariable.call_function.<locals>.<genexpr>c                       g | ]}|  qS r    unpack_var_sequencer  r  r    r*   r        c                 3   r  r   r  r  r  r    r*   r    r  c                    r  r    r  r  r  r    r*   r    r  c                 s   s    | ]}|d vV  qdS ))initialr   Nr    )r   re   r    r    r*   r  $      z-Unsupported kwargs for itertools.accumulate: ,r  r   )r   r   r   zDitertools.accumulate can only accept one of: `func` kwarg, pos 2 argz.Unsupported arguments for itertools.accumulatezOUnexpected failure in invoking function during accumulate. Failed running func r   r   c                 s   s    | ]}|d kV  qdS )re   Nr    )r   kwr    r    r*   r  \  r  z*Unsupported kwargs for itertools.groupby: re   c                 S   s<   t | tjr
|  S t | tjr|  S tdtt|  )Nz,Unsupported key type for itertools.groupby: )	rR   r   ZSymNodeVariableZevaluate_exprr@   r0   r   rl   r4   re   r    r    r*   retrieve_const_keyb  s   z;SkipFilesVariable.call_function.<locals>.retrieve_const_keyc                    s     d| gi S )Nre   )getrW   )r
  )r'   r  r:   r    r*   r   p  s   z1SkipFilesVariable.call_function.<locals>.<lambda>z+Unsupported arguments for itertools.groupbyr  z1Unexpected failure when calling itertools.groupbyc                    sJ   t | tjr d jr d j}n d }| j|dS td|  d d S )Nr   )Zwrapped_reconstructiblezfunctools.wraps(r   )rR   r   r   r2   cloner   )r   Zreconstructible)rF   r    r*   wraps  s   
z.SkipFilesVariable.call_function.<locals>.wrapsz*deque() with more than 1 arg not supportedzfunctools.partial malformed)rF   keywordsrS   )SourcelessBuilderzBuiltin z'skip function z	 in file 'z',  )Ibuiltinr+   r   r   r5   r   rZ   r[   Zcall_custom_dictdefaultdictr8   r   Zis_supported_argr   r  keysr   rd   r  	itertoolsproductallr   r   r   r   ZListIteratorVariablechain
accumulateanyjoinsetr  r  rW   operatoradd	Exceptioncombinationsr]   r0   groupbyr@   rA   rD   r   r   r2   Zcan_reconstructrT   Zroot_txrM   dequelistsZDequeVariabler   rw   ZFunctoolsPartialVariablerepeatZRepeatIteratorVariabler^   r"  Zinline_user_function_returnr   countZCountIteratorVariablecycleZCycleIteratorVariablegetfile	TypeErrorri   rk   r  )r&   r:   rF   r'   r+   r5   Zseqsrd   itemaccseqr   iterablerZkeyfuncresultrf   r  r   r   Z	rest_argsr"  pathmsgr    )rF   r'   r  r:   r*   rW     sp  	






	

$
	zSkipFilesVariable.call_functionc                    sT   | j tjtjhv r!|dkr!ddlm} |j|| j g|R i |S t ||||S )Nfromkeysr   r  )	r5   rZ   r[   r&  r%  r+   Zcall_custom_dict_fromkeysr!   rg   )r&   r:   r;   rF   r'   r+   r(   r    r*   rg     s   zSkipFilesVariable.call_methodr   rh   )ri   rj   rk   r"   r1   r0   classmethodr  r   r   	lru_cacher  rW   rg   rm   r    r    r(   r*   r     s0    

 gr   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   r(   r    r*   r"     r   zTypingVariable.__init__rF   rG   r'   rH   r=   r   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   r@   rD   r5   r0   r   r&   r:   r;   rF   r'   r    r    r*   rg     s
   zTypingVariable.call_methodc                 C   r   r   r  r   r    r    r*   r1     r   zTypingVariable.python_typec                 C   r   r   r  r   r    r    r*   r0     r   z!TypingVariable.as_python_constantrh   )ri   rj   rk   r"   rg   r1   r0   rm   r    r    r(   r*   rG    s    
rG  )maxsizec                  C   sZ   ddl m}  i }|  D ]\}}|j D ]\}}t|r)t||d  }r)|||< qq|S )Nr   )NP_TO_TNP_MODULE)utilsrK  rd   r   r   r/   )rK  Znp_fn_to_tnp_fnZnp_modZtnp_modr}   Ztnp_fnZnp_fnr    r    r*   get_np_to_tnp_map  s   rM  c                       sh   e Zd ZdZ f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   r   r(   r    r*   r"     r   zNumpyVariable.__init__rF   rG   r'   rH   r=   r   c           	      C   s   t jstd| j d ddlm} ddlm} | jjdkr+tdt	| j d	 d S t
 | j}|d u r?td
| j d |jdkrZt jrZd|j d}|dt j 7 }t| |jjd||gt||R  }|||S )Nznumpy.z()r   )numpy_to_tensor_wrapperr   )NumpyNdarrayVariabledtypez4numpy dtype function is not supported yet. Got type .zCan't find numpy function zM in torch._numpy.  Please file an issue to request support for this function.ztorch._numpy.randomz
delegate 'z' to NumPy itself via zconfg.use_numpy_random_stream=rW   )r   trace_numpyr   r5   rL  rO  ZtensorrP  ri   r4   rM  r  rj   Zuse_numpy_random_streamrk   rT   r   r   rD   )	r&   r:   rF   r'   rO  rP  r   rC  r   r    r    r*   rW     s4   
zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpyr
   rI  r    r    r*   rg   =  s   zNumpyVariable.call_methodc                 C   r   r   r  r   r    r    r*   r1   F  r   zNumpyVariable.python_typec                 C   r   r   r  r   r    r    r*   r0   I  r   z NumpyVariable.as_python_constantc                    s4   t jrt| jtr| jj}t|}|jS t	 
 S r   )r   rS  rR   r5   r4   ri   tnprQ  r;   r!   r   )r&   r   Z	tnp_dtyper(   r    r*   r   L  s
   

zNumpyVariable.as_proxyrh   )ri   rj   rk   rq   r"   rW   rg   r1   r0   r   rm   r    r    r(   r*   rN    s(    
(
	rN  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   )NrV  r    r   r    r    r*   r   b  r   zNullVariable.__str__c                 C   s   t jdk r	td tdgS )N)      z0cannot reconstruct NullVariable in < Python 3.11Z	PUSH_NULL)sysversion_infor   r	   r,   r    r    r*   r-   e  s   

zNullVariable.reconstruct)ri   rj   rk   r"   r   r-   rm   r    r    r(   r*   rV  ^  s    rV  c                   @   rn   )DeletedVariablez"Marker used to implement delattr()Nrp   r    r    r    r*   r[  k  rr   r[  )NrZ   r   r   r   r(  r0  rY  rU   rH  r   r   Ztorch._CrN   Ztorch._numpyZ_numpyrU  r$  r   r   r   Zbytecode_transformationr   r	   excr   Zguardsr   r   r2   r   r   r   r   rL  r   r   r   r   baser   r   Zdictsr   rw   r   r   r   Zuser_definedr   r   ro   rs   rt   r   r   r   r   r   r   r   r   r   r   	dataclassr   r   rM   rB   r   r   r   r   rG  rF  rM  rN  rV  r[  r    r    r    r*   <module>   sl     9
	 KQ'	  

R