o
    h                     @   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mZmZ d dl	Z
d dlmZ ddlmZmZ ddlmZmZ ddlmZ ddlmZmZ ddlmZmZmZ d	d
lmZmZ d	dl m!Z! d	dl"m#Z#m$Z$m%Z% d	dl&m'Z' G dd deZ(G dd deZ)G dd de)Z*G dd deZ+G dd de)Z,G dd de)Z-G dd deZ.G dd deZ/G dd deZ0d d! Z1d"d# Z2d$d% Z3G d&d' d'eZ4G d(d) d)e'Z5G d*d+ d+eZ6G d,d- d-eZ7G d.d/ d/eZ8G d0d1 d1eZ9G d2d3 d3eZ:ej;d	d4d5d6 Z<G d7d8 d8eZ=G d9d: d:eZ>G d;d< d<eZ?dS )=    N)DictList   )config	variables)create_call_functioncreate_instructionunimplemented)
AttrSourceODictGetItemSource)check_constant_argsidentityproxy_args_kwargs   )MutableLocalVariableTracker)DefaultDictVariable)NestedUserFunctionVariableUserFunctionVariableUserMethodVariable)UserDefinedObjectVariablec                       sL   e Zd Zd fdd	Z fddZ 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   R/var/www/html/ai/venv/lib/python3.10/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    codegenr"   r   r$   reconstruct!   s   




zSuperVariable.reconstructc                    sZ   | j sJ d| jrt| j |S | j }| j  }t|tr%| j j}tt	|||S )Nz1-arg super not implemented)
r   r   getattrr   as_python_constantpython_type
issubclasstypevaluer   )r    txnamesearch_typetype_to_user"   r   r$   const_getattr*   s   


zSuperVariable.const_getattrargsList[VariableTracker]r!   Dict[str, VariableTracker]returnr   c                 C   s  t | || | j| j}| | |}| jd u rd nt| j|}|tj	u r-t
tfi |S |tjjj	u rp| j}ddlm}	 t|tjrjt|j|	rj|sj|sj|jj|dt  |jj|dtd td S td d S t|tjrtj|fd|i| || jg| |S t|tj!rtj"|j#| jfd|i| |||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d|  d S )Nr   )AttributeMutationNewguards__call_nn_module_initTzsuper() nn.Module.__init__sourcer   r   )VariableBuilderznon-function or method super: ).r   	propagatevaluesr   r   r3   r;   r   objectr   LambdaVariabler   torchnnModuleside_effectsr8   
isinstancer   r   mutable_localoutputr9   updategetset
store_attrConstantVariabler
   typesFunctionTyper   call_function
MethodTyper   __func__collectionsOrderedDict__getitem__lenis_python_constantbuilderr<   r*   r   r.   )r    r/   r0   r4   r!   optionsinner_fnr;   r   r8   r<   keyr   r   r$   call_method:   sv   





zSuperVariable.call_methodNFr4   r5   r!   r6   r7   r   )__name__
__module____qualname__r   r(   r3   r[   __classcell__r   r   r"   r$   r      s    	r   c                   @      e Zd ZdZdS )UnknownVariablez
    It could be anything!
    Nr^   r_   r`   __doc__r   r   r   r$   rc   r       rc   c                   @   rb   )DelayGraphBreakVariablez^
    Used to insert a dummy variable in the stack to do the graph break at CALL_FUNCTION.
    Nrd   r   r   r   r$   rg   x   rf   rg   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.reconstructr0   r7   r   c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r   r;   )rj   	functionsr   r)   r   r;   )r    r/   r0   rj   r   r   r   r$   var_getattr   s
   zComptimeVariable.var_getattrr4   r5   r!   r6   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d S )Nr   )ComptimeContextr   r   zOcomptime function must not have free variables, but these variables were free: z"unsupported argument to comptime: )rj   rn   rU   rE   r   get_functionr   get_codeclosureco_freevarsrM   rN   	f_globalsfn_namer*   defaultstupleitemsRuntimeErrorr-   r   rL   )r    r/   r4   r!   rn   fncodefuncr   r   r$   rO      s.   


zComptimeVariable.call_functionNr]   )r^   r_   r`   re   r(   strrm   rO   r   r   r   r$   rh   ~   s    
rh   c                       $   e Zd Z fddZdd Z  ZS )ClosureVariablec                       t  jdi | || _d S r   r   r   r0   r    r0   r!   r"   r   r$   r         
zClosureVariable.__init__c                 C      | | jgS Ncreate_load_closurer0   r&   r   r   r$   r(         zClosureVariable.reconstructr^   r_   r`   r   r(   ra   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__r^   r_   r`   r   ra   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                       s,   e Zd ZdZedd Z 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$   create   s   zInspectSignatureVariable.createc                    r   r   )r   r   	inspected)r    r   r!   r"   r   r$   r      r   z!InspectSignatureVariable.__init__)r^   r_   r`   re   staticmethodr   r   ra   r   r   r"   r$   r      s
    
r   c                        fdd}t |_|S )Nc                         j | i |S r   )forwardr4   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_bwd   r   z@produce_trampoline_autograd_bwd.<locals>.trampoline_autograd_bwd)produce_trampoline_autograd_bwdr   )r   r   r   r   r$   r      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      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}|	j|t|d	d
}|||}||g}||||	j|t|dd
| |dd  }|	|
||S t  | ||! }tt| j"dd	|d< | jj#}t$|t%j&rt'j(|fi |||S t$|t%j)rt'j*|j+t',| jfi |||S td|  d S )NFc                    s8   t | tjr| jdurd t | tjr| rd | S )NFT)rE   r   TensorVariablerequires_gradNNModuleVariableis_training)noder   r/   r   r$   visit  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 TorchHigherOrderOperatorVariabler   rk   r   r#   r;   z?non-function or method in subclass of torch.autograd.Function: )-r   r   AutogradFunctionContextVariabler   rA   is_grad_enabledr   capture_autograd_functionr   setup_contextautogradfunction_SingleLevelFunctionr
   vjpFunctionjvphigher_order_opsr   r   r   r   r   rG   graphcopy_graphstater   import_sourcer_   r^   makerO   r=   r>   r;   r   rE   rM   rN   r   r   rP   r   rQ   UserDefinedClassVariable)r    r/   r4   r!   r   ctxvjp_fnjvp_fnr   r   r   r   r   graph_checkpoint
checkpointmodule_sourcehigher_order_autograd_fnspeculated_fwd_resultbwd_argsrX   ry   r   r   r$   
call_apply  s   









z#AutogradFunctionVariable.call_applyc                 C   s*   t | || }t| jfd| ji|S )Nr;   )r   r=   r>   r   r   r;   )r    r/   r4   r!   rX   r   r   r$   rO   s  s   z&AutogradFunctionVariable.call_functionr4   r5   r!   r6   c                 C   s:  |dkrU|  C t| jjtjrt| jj}n$t| jjtjr3t| jjj	t
| j}|jg| }n	td| jj  |||||W  d    S 1 sNw   Y  d S |dkrt| jjtjrht| 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   z&backward is a non-function or method: r   z%forward is a non-function or method: zUnsupported method: )strict_translation_moderE   r   r   rM   rN   r   rP   r   rQ   r   r   objr
   inline_callr   )r    r/   r0   r4   r!   r   r   r   r   r$   r[   w  s:   

$
z$AutogradFunctionVariable.call_method)r4   r5   r!   r6   )	r^   r_   r`   re   r   r   rO   r[   ra   r   r   r"   r$   r      s    rr   c                       sX   e Zd Zd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
    NFc                    s4   | dg }t jd||d| || _|| _d S )N_saved_tensors)r.   
value_typer   )popr   r   r   	inference)r    r.   r   r   r!   saved_tensorsr"   r   r$   r     s   
z(AutogradFunctionContextVariable.__init__c                 C   sV   | j jd tjjjtjt	ddi }| j 
dtjjjt i }|j|jjd< ||_|S )NT)r   rO   example_value)rG   rD   track_object_newrA   r   r   FunctionCtx	functoolspartialr   create_proxyrv   r.   r   metaproxy)r/   outr   r   r   r$   r     s   z&AutogradFunctionContextVariable.createc                 C      | j S r   )r   r    r   r   r$   as_proxy     z(AutogradFunctionContextVariable.as_proxyr4   r5   r!   r6   r7   r   c                 C   s   |dkrt d|  | js| jr|rJ |jj| | t| || }t	| ds.g | _
|D ]}t| tjjrCd| jjd< | j
| q0tjdi |S )Nsave_for_backwardz"autograd.Function context method: r   Tsaved_tensor_markedr   )r
   r   r;   rG   rD   track_save_for_backwardr   r=   r>   hasattrr   rE   r   rA   fxProxyr   r   appendr   rL   )r    r/   r0   r4   r!   rX   argr   r   r$   r[     s   
z+AutogradFunctionContextVariable.call_methodc                    sH    dkrt  fddS  dkrttjS t  S )Nr   c                     s     | |S r   )r[   r   r0   r    r/   r   r$   <lambda>  s    z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r   )r@   add_optionsr   TupleVariablelistr   r   rm   )r    r/   r0   r"   r   r$   rm     s   z+AutogradFunctionContextVariable.var_getattrr\   r]   )r^   r_   r`   re   r   r   r   r   r[   rm   ra   r   r   r"   r$   r     s    

r   c                       s2   e Zd Z fddZ						dd	d
Z  ZS )r@   c                    r   r   )r   r   ry   )r    ry   r!   r"   r   r$   r     r   zLambdaVariable.__init__r4   r5   r!   r6   r7   r   c                 C   s   | j |i || S r   )ry   r   )r    r/   r4   r!   r   r   r$   rO     s   zLambdaVariable.call_functionr]   )r^   r_   r`   r   rO   ra   r   r   r"   r$   r@     s    r@   c                       s   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						d fddZ  ZS )GetAttrVariablec                    s>   t  jdi | t|tsJ t|tsJ || _|| _d S r   )r   r   rE   r   r|   r   r0   )r    r   r0   r!   r"   r   r$   r     s
   
zGetAttrVariable.__init__c                 C   s   | j j d| j d| j dS )N(z, ))r#   r^   r   r0   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   )r   r   r   r   r0   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   )rE   r   r   r   ri   rG   get_submodule
module_keyr0   __dict__inspectgetattr_static)r    r/   r0   step1step2r   r   r$   r3      s   
zGetAttrVariable.const_getattrc                 C   s   || j  || jS r   )r   create_load_attrsr0   r&   r   r   r$   r(     s   
zGetAttrVariable.reconstructr4   r5   r!   r6   r7   r   c              	   C   s  ddl m} | jdko%t| jto%t| jjjj	dko%| jjjj	d t
jk}|rddlm} |d }|d j}|d j}	t| ||	 }
t
j Y t|d |rp|d||jjd|jgt||	R  d	|
W  d    S t|d tr|d||jjd
|jgt||	R  d	|
W  d    S td|  W d    n1 sw   Y  t| jtr| jdkr| j|||| S t| jtjrt|dkrt|d  | jjrt| j|d d || j|dd  |S | j || j||| S )Nr   )wrap_fx_proxy__torch_function__)TorchVariabler   r      rO   )r/   r   r[   z:GetAttrVariable.call_function original __torch_function__ r   Tr   )!rW   r   r0   rE   r   r   rU   r   r.   __mro__rA   Tensor r  rw   r   r=   r>   _CDisableTorchFunctionSubclassrG   r   r   r   r
   r   r   r   r   r   r,   r+   r[   )r    r/   r4   r!   r   !is_original_tensor_torch_functionr  "original_torch_or_getattr_variablenew_args
new_kwargsrX   r   r   r$   rO     sp   
	


zGetAttrVariable.call_functionc                    sZ   |dkr$t | jtr$| jdkr$tj| jj fi t	| | j| jjS t
 ||||S )N__len__
parameters)rE   r   r   r0   r   rL   r   num_parametersr   r=   r   r[   r    r/   r0   r4   r!   r"   r   r$   r[   T  s   


zGetAttrVariable.call_methodr]   )r^   r_   r`   r   r   r   rA   r   r   r   r   r3   r(   rO   r[   ra   r   r   r"   r$   r     s,    
Er   c                       s,   e Zd Zdejf fddZdd Z  ZS )PythonModuleVariabler.   c                    r   r   r   r   r.   r    r.   r!   r"   r   r$   r   h  r   zPythonModuleVariable.__init__c                 C   s   t jS r   )rM   
ModuleTyper   r   r   r$   r+   l  r   z PythonModuleVariable.python_type)r^   r_   r`   rM   r  r   r+   ra   r   r   r"   r$   r  g  s    r  c                       sX   e Zd Z fddZdd Zdd Zeeddd	 Z		
					dddZ
  ZS )SkipFilesVariablec                    r   r   r  r  r"   r   r$   r   q  r   zSkipFilesVariable.__init__c                 C   
   t | jS r   r-   r.   r   r   r   r$   r+   u     
zSkipFilesVariable.python_typec                 C   r   r   r.   r   r   r   r$   r*   x  r   z$SkipFilesVariable.as_python_constantNc                   C   s   t jtjiS r   )rR   
namedtupler   r   r   r   r   r$    fold_through_function_to_wrapper{  s   z2SkipFilesVariable.fold_through_function_to_wrapperr4   r5   r!   r6   r7   r   c                    s  ddl m} t|  | }t| jddr!td| j  d S | jt	j
u rZt dks;t dkrZ| d rZt dkrJ|rGt|nd  n d  |jt	j
 fi |S | jt	ju r}t dkr}t d r}ti t	j d fdt i|S | j|   v rt |r| jdd	  D i d
d | D }|  | j|fdt i|S | jtju r|stfdd D rfdd	 D }g }tj| D ]}	|tjt|	fi | qtj|fdt i|S | jtj u r+|s+tfdd D r+fdd	 D }g }tj | D ]}	||	 qtj|fdt i|S | jtj!u r~|s~t dkr~ d "r~ d # r~ d $}
 d % }g }t!|
|D ]}	|tjt|	fi | q`tj|fdt i|S | jt&j'u r|st dkr d j(r fdd}tj)|fi |S | jt	j*u r|st dkrg }nt dkrψ d "rψ d $}ntd tj+j,|fdt i|S zt-| j}W n t.y   d| jj/ }Y nw td| jj0 d|  d S )Nr   )r%   _torchdynamo_disableFz.call torch._dynamo.disable() wrapped function r   rF   c                 S   s   g | ]}|  qS r   r*   ).0xr   r   r$   
<listcomp>  s    z3SkipFilesVariable.call_function.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r   r  )r  kvr   r   r$   
<dictcomp>  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-  r   c                    s2   t | tjr| j d jdS td|  d d S )Nr   )wraps_sourcezfunctools.wraps(r   )rE   r   r   cloner;   r
   )ry   )r4   r   r$   wraps  s   z.SkipFilesVariable.call_function.<locals>.wrapsz*deque() with more than 1 arg not supportedzBuiltin zcall_function z in skip_files )1builtinr%   r   r=   r>   r   r   r.   r
   rR   rS   rU   is_supported_call_dict_argdictcall_dict_helperdefaultdictr   is_supported_argr   r  keysr   rw   rI   	itertoolsproductallr   r   r   r   ListIteratorVariablechaincombinationsr%  rV   r,  r*   r   r0  r;   r@   dequelistsDequeVariablegetfile	TypeErrorr^   r`   )r    r/   r4   r!   r%   rX   r.   seqsrw   itemiterablerr0  pathr   )r4   r/   r$   rO     s   

 zSkipFilesVariable.call_functionr]   )r^   r_   r`   r   r+   r*   r   r   	lru_cacher  rO   ra   r   r   r"   r$   r  p  s    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__r4   r5   r!   r6   r7   r   c                 C   sH   |dkrt |dkrtj| j|d   fi t| |S td d S )NrT   r   r   typing)rU   r   rL   r.   r*   r   r=   r
   r  r   r   r$   r[     s   
zTypingVariable.call_methodc                 C   r  r   r  r   r   r   r$   r+     r  zTypingVariable.python_typec                 C   r   r   r  r   r   r   r$   r*     r   z!TypingVariable.as_python_constantr]   )r^   r_   r`   r   r[   r+   r*   ra   r   r   r"   r$   rI    s    
rI  )maxsizec                  C   sZ   ddl m}  i }|  D ]\}}|j D ]\}}t|r)t||d  }r)|||< qq|S )Nr   )NP_TO_TNP_MODULE)utilsrL  rw   r   r   r)   )rL  np_fn_to_tnp_fnnp_modtnp_modrt   tnp_fnnp_fnr   r   r$   get_np_to_tnp_map  s   rS  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__r4   r5   r!   r6   r7   r   c           	      C   s   t jstd| j d ddlm} ddlm} t	| gg|gt
| g}| jjdkr:tdt| j d	 d S t | j}|d u rNtd
| j d |jjd||gt||R  }|j||fi |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.rO   )r   trace_numpyr
   r.   rM  rU  tensorrV  r   r=   r   r>   r^   r-   rS  rI   rG   r   r   r   )	r    r/   r4   r!   rU  rV  rX   r{   r   r   r   r$   rO   2  s*   zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpyr	   r  r   r   r$   r[   S  s   zNumpyVariable.call_methodc                 C   r  r   r  r   r   r   r$   r+   \  r  zNumpyVariable.python_typec                 C   r   r   r  r   r   r   r$   r*   _  r   z NumpyVariable.as_python_constantc                    s4   t jrt| jtr| jj}t|}|jS t	 
 S r   )r   rY  rE   r.   r-   r^   tnprW  r0   r   r   )r    r   	tnp_dtyper"   r   r$   r   b  s
   

zNumpyVariable.as_proxyr]   )r^   r_   r`   re   r   rO   r[   r+   r*   r   ra   r   r   r"   r$   rT  )  s(    
!
	rT  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   u  r   zNullVariable.__init__c                 C   s   dS )Nr^  r   r   r   r   r$   r   x  s   zNullVariable.__str__c                 C   s   t jdk r	td tdgS )N)r     z0cannot reconstruct NullVariable in < Python 3.11	PUSH_NULL)sysversion_infor
   r   r&   r   r   r$   r(   {  s   

zNullVariable.reconstruct)r^   r_   r`   r   r   r(   ra   r   r   r"   r$   r^  t  s    r^  c                   @   rb   )DeletedVariablez"Marker used to implement delattr()Nrd   r   r   r   r$   rc    rf   rc  )@rR   r   r   r8  ra  rM   rJ  r   r   torch._CrA   torch._numpy_numpyr\  r  r   r   bytecode_transformationr   r   excr
   r;   r   r   rM  r   r   r   baser   r   dictsr   rl   r   r   r   user_definedr   r   rc   rg   rh   r~   r   r   r   r   r   r   r   r   r   r@   r   r  r  rI  rH  rS  rT  r^  rc  r   r   r   r$   <module>   sX    X9
	 *@y	 

K