o
    )vi                     @   s  d 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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# dd
l$m%Z% ddl&m'Z'm(Z(m)Z)m*Z*m+Z+ ddlm,Z,m-Z-m.Z.m/Z/m0Z0m1Z1m2Z2m3Z3m4Z4 ddl5m6Z6 ddl7m8Z8 ddl9m:Z:m;Z; ddl<m=Z=m>Z>m?Z? erddl@mAZA G dd dZBG dd de6ZCG dd de6ZDG dd de6ZEG dd deEZFG dd de6ZGG dd  d e6ZHG d!d" d"e6ZId#d$ ZJG d%d& d&e6ZKejLG d'd( d(ZMG d)d* d*e?ZNG d+d, d,e?ZOG d-d. d.e6ZPG d/d0 d0e6ZQG d1d2 d2e6ZRG d3d4 d4e6ZSG d5d6 d6e6ZTG d7d8 d8e6ZUejVdd9d:d; ZWejVdd9d<d= ZXG d>d? d?e6ZYG d@dA dAe6ZZG dBdC dCe6Z[G dDdE dEe6Z\G dFdG dGe6Z]G dHdI dIe6Z^G dJdK dKe6Z_G dLdM dMe_Z`G dNdO dOe_ZaG dPdQ dQe_ZbG dRdS dSe_ZcG dTdU dUe_ZdejeecejfecejgediZhG dVdW dWe6ZiG dXdY dYe6ZjG dZd[ d[e6ZkdS )\a  
This module contains miscellaneous variable tracker implementations for various Python types
and features used in Dynamo's symbolic execution. These classes help track and propagate
information about different kinds of variables during graph capture.

Key classes include:
- SuperVariable: Handles super() calls and method resolution
- ExceptionVariable: Tracks exception objects
- RandomVariable: Manages random number generators
- GetAttrVariable: Tracks attribute access
- MethodWrapperVariable: Handles method wrappers
- PythonModuleVariable: Tracks Python modules
- NumpyVariable: Handles numpy functions and types
- StringFormatVariable: Manages string formatting
- DebuggingVariable: Handles print and logging
    N)OptionalTYPE_CHECKING   )config	variables)create_call_functioncreate_instruction)$do_not_convert_to_tracable_parameter)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)unpatched_nn_module_init)
AttrSourceGenericAttrSourceGetItemSource
TypeSourceWeakRefCallSource)	check_unspec_or_constant_argscmp_name_to_op_mappingidentityis_tensor_base_attr_getteristypelist_methodsproxy_args_kwargsset_example_valuetuple_methods   )VariableTracker)ConstantVariable)NestedUserFunctionVariableUserFunctionVariable)call_random_fnis_standard_setattrUserDefinedObjectVariable)InstructionTranslatorc                   @   s   e Zd ZdS )NO_SUCH_SUBOBJN)__name__
__module____qualname__ r*   r*   Y/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/torch/_dynamo/variables/misc.pyr&   E   s    r&   c                       sp   e Zd Zh ejZdd fddZ fddZd fd	d
ZdddeddfddZ							dddZ
  ZS )SuperVariableNreturnc                    s"   t  jdi | || _|| _d S Nr*   )super__init__typevarobjvar)selfr1   r2   kwargs	__class__r*   r+   r0   N   s   
zSuperVariable.__init__c                    sZ      fdd  | j | jd ur# | j  tdd d S  tdd d S )Nc                      s   t tS N)r   BuiltinVariabler/   r*   )r6   codegenr*   r+   <lambda>[   s    z+SuperVariable.reconstruct.<locals>.<lambda>r   Fr   )add_push_nullr1   r2   extend_outputr   r3   r9   r5   r9   r+   reconstructZ   s   


zSuperVariable.reconstructtxr%   c                    s   | j sJ d| j }| j  }| j jrt| j jnd }t|tr*| j j}| j j}d }|j	}z	|
|d }W n tyK   tt|||d f Y S w t|t|D ]$}	||	 j|t }
rw|
turw|rqttt|d|	|}|
|f  S qStd d S )Nz1-arg super not implementedr   __mro__zUnable to resolve super getattr)r2   r1   as_python_constantpython_typesourcer   
issubclasstypevaluerA   index
ValueErrorgetattrr/   rangelen__dict__getr&   r   r   r   )r3   r@   namesearch_typetype_to_usetype_to_use_sourcerD   
search_mrostart_indexrH   resolved_getattrr5   r*   r+   _resolved_getattr_and_sourcec   s8   

	

z*SuperVariable._resolved_getattr_and_sourcerO   r   c                 C   sJ   |  | |\}}tj|st| |S |rt|tj tjj	||dS )NrD   )
rV   r   r   
is_literalGetAttrVariabler   
make_guardr   CONSTANT_MATCHcreate)r3   r@   rO   rG   rD   r*   r*   r+   var_getattr   s   	
zSuperVariable.var_getattrargslist[VariableTracker]r4   dict[str, VariableTracker]c                 C   s  |  | |\}}|tju rttS |tjjju r\| j}ddl	m
} t|tjrVt|j|rV|sV|sVt  tjt|d|| jg| |W  d    S 1 sOw   Y  ntd n| jjrt|dr|jdkrtj|r|j}	t|	dr|	jdkrt|	}
n
|j}tj|	|d}
|
|d||S t|trt|jt j!rtj|j|d|||S t|t"rt|jt j!rtj#|j| j|d|||S t|t j!rtj||d|| jg| |S t|t j$rtj#|j| j|d|||S t%|rt| jtr| jj&|g|R i |S |tj'u rU|d	 }z|( }W n t)y.   td
|  Y nw |j*j	+| jsDtd| j d| d |j*j	,| j|t-  t.d S t| jtj/rn|| jj0v rn| jj1||||S t| jtj2r|t3v r| jj4||||S t| jtj5r|t6v r| jj7||||S |tj8u r|d	 j9}|j*j	:| j|r|j*j	j;| j|dd}t|tj-rt<t=| |S z
t8| jj9|}W n t=y   t<t=| Y nw d }| jjd urt>| jj|}t?@|||S td|  d S )Nr   )AttributeMutationNewrW   zsuper() nn.Module.__init__r'   __new__r(   builtinsr   znon-const delattr attr: zdelattr(, z, ...)T)
deleted_okznon-function or method super: )ArV   objectr0   LambdaVariabler   torchnnModuler2   side_effectsra   
isinstancer   r$   mutation_typer	   r!   r   call_functionr   rD   hasattrr'   UserDefinedClassVariableis_supported_new_method__self__r(   r8   membercall_methodstaticmethod__func__typesFunctionTypeclassmethodUserMethodVariable
MethodTyper#   method_setattr_standard__delattr__rB   NotImplementedErroroutputis_attribute_mutation
store_attrDeletedVariabler   UserDefinedDictVariable_dict_methods_dict_vtUserDefinedTupleVariabler   	_tuple_vtUserDefinedListVariabler   _list_vt__getattribute__rG   has_pending_mutation_of_attr	load_attrr
   AttributeErrorr   r   build)r3   r@   rO   r^   r4   inner_fnrD   r2   ra   user_clsuser_cls_vtuser_cls_sourceattr	attr_nameresult
attr_valueattr_sourcer*   r*   r+   rt      s   


$











zSuperVariable.call_methodr7   r-   Nr@   r%   r^   r_   r4   r`   r-   r   )r'   r(   r)   r   _nonvar_fieldsr0   r?   rV   strr]   rt   __classcell__r*   r*   r5   r+   r,   I   s    	/r,   c                       sr   e Zd Zd fddZdddZdd	 Zd
d ZdddedefddZ fddZ	 fddZ
dd ZeZ  ZS )ExceptionVariabler-   Nc                    sJ   t  jdi | || _|| _td | _td | _td| _td | _d S )NFr*   )	r/   r0   exc_typer^   r   __context__	__cause____suppress_context____traceback__)r3   r   r^   r4   r5   r*   r+   r0     s   


zExceptionVariable.__init__contextc                 C   s
   || _ d S r7   )r   )r3   r   r*   r*   r+   set_context.     
zExceptionVariable.set_contextc                    sf      fdd  j  tjd dtdd f fdd}|d |d	 |d
 d S )Nc                      s     djjS )Nrc   )load_import_fromr   r'   r*   r9   r3   r*   r+   r:   3      z/ExceptionVariable.reconstruct.<locals>.<lambda>FrO   r-   c                    sX   t | }t|tr|jdv sJ |d S     |   d  |  d S )N)TFNr   )rJ   r   r   rG   dup_topr<   rot_nr   )rO   r   r   r*   r+   codegen_attr8  s   

z3ExceptionVariable.reconstruct.<locals>.codegen_attrr   r   r   )r;   foreachr^   rn   rL   r   )r3   r9   r   r*   r   r+   r?   1  s   
zExceptionVariable.reconstructc                 C      | j S r7   )r   r3   r*   r*   r+   rC   F     zExceptionVariable.python_typer@   r%   name_varvalc                    s   fdd}|  }|dkr| | nj|dkr>t|tr"|jd u s/t|tjtjtjtj	fr9|| _
td| _nE|d n@|dkrUt|trP|jdv rP|| _n.|d n)|d	krst|trg|jd u rg|| _ntd
| d| d ntd
| d| d td S )Nc                    s   t t t| gd d S )N)r^   )r
   	TypeErrorr   )msgr@   r*   r+   raise_errorO  s   z3ExceptionVariable.call_setattr.<locals>.raise_errorr   r   Tz9exception cause must be None or derive from BaseExceptionr   )TFr   zsetattr(ExceptionVariable, rd   ))rB   r   rl   r   rG   r   r8   r   !UserDefinedExceptionClassVariable"UserDefinedExceptionObjectVariabler   r   r   r   )r3   r@   r   r   r   rO   r*   r   r+   call_setattrI  s4   	


zExceptionVariable.call_setattrc                    sP   |dkr| j |g|R  S |dkr|\}|  |td| | S t ||||S )N__setattr__with_tracebackr   )r   r   r/   rt   )r3   r@   rO   r^   r4   tbr5   r*   r+   rt   q  s   zExceptionVariable.call_methodc                    sd   |dkr| j S |dkr| jS |dkr| jS |dkrtd S |dkr+tj| j| jdS t 	||S )Nr   r   r   r   r^   rW   )
r   r   r   r   r   ListVariabler^   rD   r/   r]   )r3   r@   rO   r5   r*   r+   r]   {  s   
zExceptionVariable.var_getattrc                 C   s   | j j d| j dS )N(r   )r6   r'   r   r   r*   r*   r+   __str__     zExceptionVariable.__str__r   )r   r   )r'   r(   r)   r0   r   r?   rC   r   r   rt   r]   r   __repr__r   r*   r*   r5   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+   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                   @   sH   e Zd ZdZdd Zdd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)r~   r=   r*   r*   r+   r?     s   zComptimeVariable.reconstructr@   r%   rO   r-   r   c                 C   s2   ddl m } ddlm} |t||t| j|dS )Nr   )comptimer   )r!   rW   )r   	functionsr!   rJ   r   rD   )r3   r@   rO   r   r!   r*   r*   r+   r]     s
   zComptimeVariable.var_getattrr^   r_   r4   r`   c                 C   s   ddl m} |r
J t|dksJ |d }t|tr$| || n:t|trU| }|jr8J d|j	 t
||j|j |jrJt|jjnd d}||| n	tdt| tjd S )Nr   )ComptimeContextr   zOcomptime function must not have free variables, but these variables were free: r*   z"unsupported argument to comptime: )r   r   rL   rl   r!   get_functionr    get_codeclosureco_freevarsrw   rx   	f_globalsfn_namerB   defaultstupleitemsRuntimeErrorrF   r   r   r\   )r3   r@   r^   r4   r   fncodefuncr*   r*   r+   rn     s.   

zComptimeVariable.call_functionNr@   r%   r^   r_   r4   r`   r-   r   )r'   r(   r)   r   r?   r   r]   rn   r*   r*   r*   r+   r     s    
r   c                       sL   e Zd ZU ee ed< dZee ed< 	ddee ddf fddZ  Z	S )CellVariablepre_existing_contentsN
local_namer-   c                       t  jdi | || _d S r.   )r/   r0   r   )r3   r   r4   r5   r*   r+   r0     s   
zCellVariable.__init__r7   )
r'   r(   r)   r   r   __annotations__r   r   r0   r   r*   r*   r5   r+   r     s   
 r   c                       s   e Zd Zd fddZ  ZS )NewGlobalVariabler-   Nc                       t  jdi | d S r.   r/   r0   r3   r4   r5   r*   r+   r0     r   zNewGlobalVariable.__init__r   )r'   r(   r)   r0   r   r*   r*   r5   r+   r     s    r   c                    s    fdd}t |_|S )Nc                     s    j | i |S r7   )applyr^   r4   fn_clsr*   r+   trampoline_autograd_apply  s   zDproduce_trampoline_autograd_apply.<locals>.trampoline_autograd_apply)!produce_trampoline_autograd_apply_origin)r   r   r*   r   r+   r     s   r   c                       s^   e Zd ZdZdhejZd fddZdd	d
ZdddZdddZ					dddZ
  ZS )AutogradFunctionVariablez-represents a torch.autograd.Function subclassr   r-   Nc                    r   r.   )r/   r0   r   )r3   r   r4   r5   r*   r+   r0         
z!AutogradFunctionVariable.__init__r@   r%   c                    s*  d  fdd}t |||f  rt rtjdu r!td ddlm	} ddl
m} | jj}|| jj}|rB|| jj| jj}| jj}	|	tjjjurQtd | jj}
|
tjjjur`td	 d
dlm} | j}|d u ryt| 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&'|}t(|d
 t(|j)kr|d
d  }t*j+||d||S t#|t$j,rt*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)rl   r   TensorVariablerequires_gradNNModuleVariableis_training)noder   r@   r*   r+   visit  s   

z2AutogradFunctionVariable.call_apply.<locals>.visitzNThe config.capture_autograd_function flag is deprecated, it's now always true.r   )#autograd_function_forward_rewritten)_is_setup_context_definedzNYI - User defind vjpzNYI - User defind jvpr   )AutogradFunctionApplyVariabler   )rs   rW   forwardsetup_contextz?non-function or method in subclass of torch.autograd.Function: )0r   r   rh   is_grad_enabledr   capture_autograd_functionwarningswarn"torch._functorch.autograd_functionr   torch.autograd.functionr   r   r   r   vjpautogradFunctionr   jvphigher_order_opsr   rD   r   import_sourcer(   r'   backwardrn   r   rZ   r   FUNCTION_MATCHAutogradFunctionContextVariabler\   rl   rw   rx   inspect	signaturerL   _parametersr   r!   r{   rz   rv   rp   )r3   r@   r^   r4   r   r   r   
forward_fnis_setup_ctx_definedvjp_fnjvp_fnr   rD   r   fwd_srcsetup_ctx_srcr   ctxsigr*   r   r+   
call_apply  s   	






z#AutogradFunctionVariable.call_applyc                 C   sX   | j j}t|d jtjjju sJ t|t	j
sJ t| jd}tj||d|||S )Nr   r  rW   )r   r  rF   rG   rh   _dynamoexternal_utilsFakeBackwardCFunctionrl   rw   rx   r   rD   r   r!   rn   )r3   r@   r^   r4   r   	fn_sourcer*   r*   r+   call_backwarde  s   z&AutogradFunctionVariable.call_backwardc                 C   s
   t | jS r7   )r   r   r3   r@   r^   r4   r*   r*   r+   rn   o  r   z&AutogradFunctionVariable.call_functionr^   r_   r4   r`   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   rn   )r@   proxyr  )trace_rulesrW   zUnsupported method: )r  r  builderr  r   r   r   create_proxyr   r  r   rD   r   r  getattr_staticr   rl   ru   __get__lookupcreate_with_sourcern   ry   r   rz   rv   r   )r3   r@   rO   r^   r4   r  r  r   r  rD   objr   r*   r*   r+   rt   r  sX   
	



z$AutogradFunctionVariable.call_methodr   r   )r^   r_   r4   r`   )r'   r(   r)   r   r   r   r0   r  r  rn   rt   r   r*   r*   r5   r+   r     s    

a

r   c                   @   s&   e Zd ZU ejedZee ed< dS )SavedTensorBox)default_factorytensorsN)	r'   r(   r)   dataclassesfieldlistr(  r   r   r*   r*   r*   r+   r&    s   
 r&  c                       s   e Zd ZdZdddhejZ						d	d fdd	ZeddddZdd Z							d fddZ
d fddZ  ZS )r  zZ
    Tracks an autograd.Function() context using mutation tracking in side_effects.py
    r  	inferencesaved_tensorsNFr-   c           	         s:   t  jd||d| || _|| _|| _|| _|| _d S N)rG   
value_typer*   )r/   r0   r,  r  r-  needs_input_gradnon_differentiable)	r3   rG   r/  r,  r  r-  r0  r1  r4   r5   r*   r+   r0     s   
z(AutogradFunctionContextVariable.__init__r@   r%   c                 C   st   d }|r|st dd |D }| jdtjjjd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 r7   )rl   r   r   r   .0xr*   r*   r+   	<genexpr>  s
    
z9AutogradFunctionContextVariable.create.<locals>.<genexpr>rn   r*   T)r,  r  r-  r0  )r   r   r  rh   r   functionFunctionCtxrk   track_object_new	functoolspartialr  r&  r   r   rG   )r@   r^   r4   r0  r  outr*   r*   r+   r\     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_proxyr^   r_   r4   r`   r   c                    s   |dkrt  ||||S |dkr't|dksJ t|i d | _tjd S |dkr2td|  | j	d u r;td | j
sM| jrC|rEJ |jj| | t| j	jdkrYg | j	_|D ]	}| j	j| q[tjd S )Nr   mark_non_differentiabler   save_for_backwardz"autograd.Function context method: zCsave_for_backward only supported on a newly constructed FunctionCtx)r/   rt   rL   r   r1  r   r   r\   r   r-  r,  rD   r   rk   track_save_for_backwardr(  append)r3   r@   rO   r^   r4   argr5   r*   r+   rt     s(   
z+AutogradFunctionContextVariable.call_methodc                    s    dv rt  fddS  dkr jd ur ttjjS  dkrBjd ur0tjjS j	rBt
j	d}tjj|S t  S )N)r>  r=  c                     s     | |S r7   )rt   r   rO   r3   r@   r*   r+   r:     r   z=AutogradFunctionContextVariable.var_getattr.<locals>.<lambda>r-  r0  )rg   r-  r   TupleVariabler+  r(  r0  r   r\   rD   r   r   r   rG   r/   r]   r3   r@   rO   rD   r5   rB  r+   r]     s   
z+AutogradFunctionContextVariable.var_getattr)NFNNNNr   NNr   r   )r'   r(   r)   r   r$   r   r0   ru   r\   r<  rt   r]   r   r*   r*   r5   r+   r    s8    

 r  c                       s>   e Zd ZdZ	d	d fddZ							
dddZ  ZS )AutogradEngineVariablez;
    Represents a torch._C._ImperativeEngine instance.
    Nr-   c                    s   t  jd||d| d S r.  r   )r3   rG   r/  r4   r5   r*   r+   r0   $  s   zAutogradEngineVariable.__init__r^   r_   r4   r`   r   c                 C   sp   |dkr/t jjjr)|jsJ dtjt jjjj	| j
d||jj g|R |S td d S td|  d S )Nqueue_callbackzXqueue_callback() is only supported when Compiled Autograd is enabled with fullgraph=TruerW   z#torch._C._ImperativeEngine method: )rh   r  compiled_autogradin_compiled_autograd_region	one_graphr   r!   r  FakeCompiledAutogradEnginerG  rD   rn   r   rk   get_ca_final_callbacks_varr   r3   r@   rO   r^   r4   r*   r*   r+   rt   ,  s&   

	z"AutogradEngineVariable.call_methodr7   r   r   r'   r(   r)   r   r0   rt   r   r*   r*   r5   r+   rF    s    rF  c                       s8   e Zd Zd fddZ							
		dddZ  ZS )rg   r-   Nc                    r   r.   )r/   r0   r   )r3   r   r4   r5   r*   r+   r0   I  r   zLambdaVariable.__init__r@   r%   r^   r_   r4   r`   r   c                 C   s   | j |i |S r7   )r   r  r*   r*   r+   rn   M  s   zLambdaVariable.call_functionr   r   )r'   r(   r)   r0   rn   r   r*   r*   r5   r+   rg   H  s    rg   c                       s   e Zd ZddhejZd!d" fddZ fddZdefd	d
Ze	de
jjfddZdd Zdd Zd#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 )%rY   rO   py_typeNr-   c                    sD   t  jdi | t|tsJ t|tsJ || _|| _|| _d S r.   )r/   r0   rl   r   r   r%  rO   rO  )r3   r%  rO   rO  r4   r5   r*   r+   r0   ]  s   
zGetAttrVariable.__init__c                    s   | j d ur| j S t  S r7   )rO  r/   rC   r   r5   r*   r+   rC   e  s   

zGetAttrVariable.python_typec                 C   s   | j j d| j d| j dS Nr   rd   r   )r6   r'   r%  rO   r   r*   r*   r+   r   k  s   zGetAttrVariable.__repr__
base_proxyc                 C   s
   t | |S r7   )rJ   )rQ  r   r*   r*   r+   create_getattr_proxyn  s   
z$GetAttrVariable.create_getattr_proxyc                 C   s   t | j | jS r7   )rY   rR  r%  r<  rO   r   r*   r*   r+   r<  r  s   zGetAttrVariable.as_proxyc                 C   s8   | j  }zt|| jW S  ty   t|  dd w )Nz is not a constant)r%  rB   rJ   rO   r   r~   )r3   constantr*   r*   r+   rB   u  s   
z"GetAttrVariable.as_python_constantr@   r%   c                 C   sZ   t | jtjs	t|j| jj}| j|j	vrtt
|| j}||j	vr'tt
||S r7   )rl   r%  r   r   r~   r   get_submodule
module_keyrO   rM   r  r!  )r3   r@   rO   step1step2r*   r*   r+   const_getattr|  s   
zGetAttrVariable.const_getattrc                 C   s    || j  ||| j d S r7   )r%  r<   create_load_attrsrO   r=   r*   r*   r+   r?     s   
zGetAttrVariable.reconstructr^   r_   r4   r`   r   c                 C   s   | j || j||S r7   )r%  rt   rO   r  r*   r*   r+   rn     s   zGetAttrVariable.call_functionc                    sl  |dv rF| j dkrF|sF|d  rFt| jtjtjtjfrF| j}|d  }|	||r2|
||S |dkrEt|dkr@|d S td S ng|dkr| j dkrt|dkr|d  r|st| jtjtjtjfr| j}|d  }|	||r}tdS td	S |d
kr| j dkr|st| jtjr| j||d |d S t| jtjr| j| t ||||S )N)__getitem__rN   rM   r   rN   r   r   __contains__TF__setitem__)rO   is_python_constantrl   r%  r   r$   r   rp   rB   has_key_in_generic_dictr]   rL   r   r|   convert_to_unspecializedr/   rt   )r3   r@   rO   r^   r4   r%  keyr5   r*   r+   rt     sb   






zGetAttrVariable.call_methodr7   r   r   r   )r'   r(   r)   r   r   r0   rC   r   r   ru   rh   fxProxyrR  r<  rB   rX  r?   rn   r+  dictrt   r   r*   r*   r5   r+   rY   V  s>    


rY   c                       sL   e Zd Zd fddZ							
		d fddZdd Zdd Z  ZS )MethodWrapperVariabler-   Nc                    s"   t  jdi | || _i | _d S r.   )r/   r0   method_wrapper_builtin_fns)r3   re  r4   r5   r*   r+   r0     s   
zMethodWrapperVariable.__init__r@   r%   r^   r_   r4   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 | jj}| jj}|dkrJt	|j
}|tj
u rJtt|||g||S t ||| d S )Nr   r   r0   )r   re  rl   r   r   rL   r]   rr   r'   rF   r0   rf   r8   rt   r/   rn   )r3   r@   r^   r4   self_objwrapper_namefn_objr5   r*   r+   rn     s   



z#MethodWrapperVariable.call_functionc                 C      dS NTr*   r   r*   r*   r+   r]       z(MethodWrapperVariable.is_python_constantc                 C   r   r7   )re  r   r*   r*   r+   rB     r   z(MethodWrapperVariable.as_python_constantr   r   )r'   r(   r)   r0   rn   r]  rB   r   r*   r*   r5   r+   rd    s    rd  c                       s<   e Zd Zd fddZd fddZd	d
 Zdd Z  ZS )GetSetDescriptorVariabler-   Nc                    r   r.   )r/   r0   desc)r3   rn  r4   r5   r*   r+   r0     r   z!GetSetDescriptorVariable.__init__r@   r%   c                    s:   |dkr| j rt| j d}t|| jj|S t ||S )Nr"  )rD   r   r   r   rn  r"  r/   r]   rD  r5   r*   r+   r]     s   z$GetSetDescriptorVariable.var_getattrc                 C   rj  rk  r*   r   r*   r*   r+   r]    rl  z+GetSetDescriptorVariable.is_python_constantc                 C   r   r7   )rn  r   r*   r*   r+   rB     r   z+GetSetDescriptorVariable.as_python_constantr   r   )r'   r(   r)   r0   r]   r]  rB   r   r*   r*   r5   r+   rm    s
    rm  c                       sh   e Zd ZddhejZdejddf fddZdd Zd	d
 Z	de
fddZdddZdddZ  ZS )PythonModuleVariablerG   is_torchr-   Nc                    s6   t  jdi | || _| jtu p| jjd| _d S )Nztorch.r*   )r/   r0   rG   rh   r'   
startswithrp  r3   rG   r4   r5   r*   r+   r0     s   zPythonModuleVariable.__init__c                 C      t jS r7   )rw   
ModuleTyper   r*   r*   r+   rC   !  r   z PythonModuleVariable.python_typec                 C   r   r7   rG   r   r*   r*   r+   rB   $  r   z'PythonModuleVariable.as_python_constantc                 C   s   d| j  dS )NzPythonModuleVariable(r   ru  r   r*   r*   r+   r   '  s   zPythonModuleVariable.__repr__r@   r%   c                 C   s   t | j|}tj|S r7   )ro   rG   r   r   r\   r3   r@   rO   r   r*   r*   r+   call_obj_hasattr*  s   z%PythonModuleVariable.call_obj_hasattrc                 C   sl   |j j| |r|j j| |S | js|| jjvr t| j|}n| jj| }| jo.t	| j|}t
|||S r7   )r   rk   r   r   rp  rG   rM   rJ   rD   r   r   r   )r3   r@   rO   r   rD   r*   r*   r+   r]   .  s   z PythonModuleVariable.var_getattrr   )r'   r(   r)   r   r   rw   rt  r0   rC   rB   r   r   rw  r]   r   r*   r*   r5   r+   ro    s    
ro  c                       sX   e Zd Zd fddZ							dd
dZdddefddZdd ZdddZ  Z	S )TypingVariabler-   Nc                    r   r.   r/   r0   rG   rr  r5   r*   r+   r0   <  r   zTypingVariable.__init__r^   r_   r4   r`   r   c                 C   s:   |dkrt |dkr| j|d   }t|S td d S )NrZ  r   r   z+unsupported method call on typing variablel)rL   rG   rB   rx  r   )r3   r@   rO   r^   r4   
new_typingr*   r*   r+   rt   @  s   zTypingVariable.call_methodr@   r%   rO   c                 C   sz   ddl m}m} |tv rt| |S |jj| |r!|j	| |S t
| j|}| jr7t| j|}||||S |||S )Nr   )SourcelessBuilderVariableBuilder)r  r{  r|  r   r   rY   r   rk   r   r   rJ   rG   rD   r   r\   )r3   r@   rO   r{  r|  rG   r   r*   r*   r+   r]   M  s   zTypingVariable.var_getattrc                 C   r   r7   ru  r   r*   r*   r+   rB   ]  r   z!TypingVariable.as_python_constantr9   torch._dynamo.codegen.PyCodegenc                 C   s   | || j d S r7   )append_outputcreate_load_constrG   r=   r*   r*   r+   r?   `  s   zTypingVariable.reconstructr   r   )r9   r}  r-   N)
r'   r(   r)   r0   rt   r   r]   rB   r?   r   r*   r*   r5   r+   rx  ;  s    
rx  )maxsizec                  C   sZ   ddl m}  i }|  D ]\}}|j D ]\}}t|r)t||d }r)|||< qq|S )zd
    This generates a mapping from numpy modules to their torch._numpy
    modules equivalents.
    r   )NP_TO_TNP_MODULEN)utilsr  r   rM   callablerJ   )r  np_fn_to_tnp_fnnp_modtnp_modr   tnp_fnnp_fnr*   r*   r+   get_np_to_tnp_map{  s   r  c                  C   s   t  } dd |  D S )z
    This is just the reverse mapping of get_np_to_tnp_map() - mapping from
    torch._numpy modules to numpy equivalents.
    c                 S   s   i | ]\}}||qS r*   r*   r3  kvr*   r*   r+   
<dictcomp>  s    z%get_tnp_to_np_map.<locals>.<dictcomp>)r  r   )mr*   r*   r+   get_tnp_to_np_map  s   r  c                       s   e Zd ZdZejfZd fddZedd Z	edd	 Z
	
							dddZ						d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.
    r-   Nc                    r   r.   ry  rr  r5   r*   r+   r0     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   rh   _numpy)r(   splitrL   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 r  )r(   r  rL   np_constant_collections_maprN   r  r*   r*   r+    get_constant_collection_for_func  s   $z.NumpyVariable.get_constant_collection_for_funcr@   r%   r^   r_   r4   r`   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*   rB   r2  r*   r*   r+   
<listcomp>      z/NumpyVariable.call_function.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r*   r  r  r*   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   r  r*   r  r2  r*   r*   r+   r    r  c                 S   r  r*   r  r  r*   r*   r+   r    r  rn   )r   trace_numpyr   rG   r  r  tensorr  r  rN   r  r   r~   r'   r(   use_numpy_random_streamr)   
patch_argsr  r   r   r   r\   rB   r   r  r   )
r3   r@   r^   r4   r  r  r   collection_variable_typr   r  r*   r*   r+   rn     sb   


zNumpyVariable.call_functionc                 C   s   t d d S )Nnumpy)r   rM  r*   r*   r+   rt     s   zNumpyVariable.call_methodc                 C   r   r7   ru  r   r*   r*   r+   rB     r   z NumpyVariable.as_python_constantc                    s$   t jrt| jtr| jjS t  S r7   )r   r  rl   rG   rF   r'   r/   r<  r   r5   r*   r+   r<    s   
zNumpyVariable.as_proxyr   r   r   )r'   r(   r)   r   tnp
issubdtyper  r0   ry   r  r  rn   rt   rB   r<  r   r*   r*   r5   r+   r    s4    


@
	r  c                       s4   e Zd Zd	 fddZdefddZdd Z  ZS )
NullVariabler-   Nc                    r   r.   r   r   r5   r*   r+   r0   	  r   zNullVariable.__init__c                 C   rj  )Nr  r*   r   r*   r*   r+   r     rl  zNullVariable.__repr__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.reconstructr   )r'   r(   r)   r0   r   r   r?   r   r*   r*   r5   r+   r    s    r  c                   @   r   )r   z"Marker used to implement delattr()Nr   r*   r*   r*   r+   r     r   r   c                       sP   e Zd ZdZdhejZedd Zd fddZde	f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 r7   )r]  r2  r*   r*   r+   r5  "  s
    
z.StringFormatVariable.create.<locals>.<genexpr>c                 S   r  r*   r  )r3  r  r*   r*   r+   r  (  r  z/StringFormatVariable.create.<locals>.<listcomp>c                 S   r  r*   r  r  r*   r*   r+   r  )  r  z/StringFormatVariable.create.<locals>.<dictcomp>)all	itertoolschainvaluesr   r   r\   formatr   r+  rc  )r  r  sym_args
sym_kwargsr*   r*   r+   r\      s   zStringFormatVariable.creater-   Nc                    s6   t  jdi | t|tsJ || _|| _|| _d S r.   )r/   r0   rl   r   r  r  r  )r3   r  r  r  r4   r5   r*   r+   r0   .  s
   
zStringFormatVariable.__init__c                 C   s&   | j j d| jd| jd| jdS rP  )r6   r'   r  r  r  r   r*   r*   r+   r   5  s   &zStringFormatVariable.__repr__c                    s`    j  fdddd  tj dd j D } t|  tddd	 d S )
Nc                      s      j dgS )Nr  )r<   r  r  create_load_attrr*   r   r*   r+   r:   :  s
    
z2StringFormatVariable.reconstruct.<locals>.<lambda>T)call_function_exc                 S   s   i | ]\}}t j||qS r*   r   r   r\   r  r*   r*   r+   r  C  s    z4StringFormatVariable.reconstruct.<locals>.<dictcomp>CALL_FUNCTION_EXr   )rA  )	r;   r   rC  r  r  r   ConstDictVariabler~  r   )r3   r9   r4   r*   r   r+   r?   8  s   	z StringFormatVariable.reconstructr   )r'   r(   r)   r   r   r   ry   r\   r0   r   r   r?   r   r*   r*   r5   r+   r    s    
r  c                       sN   e Zd ZdZd fddZedd Zd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.
    r-   Nc                    r   r.   ry  rr  r5   r*   r+   r0   P  r   zDebuggingVariable.__init__c                 C   s(   t | ot| tjtjfo| tjjjv S r7   )	r  rl   rw   rx   BuiltinFunctionTyperh   r  r   reorderable_logging_functions)r%  r*   r*   r+   is_reorderable_logging_functionT  s
   z1DebuggingVariable.is_reorderable_logging_functionr@   r%   c                 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 r  z is not yet implemented.)exportcan_reorder_logsrG   r   debug_localsr@  r+  r  r*   r*   r+   rn   \  s   
zDebuggingVariable.call_functionc                 C   s   | j |S r7   )rD   r?   r=   r*   r*   r+   r?   i  s   zDebuggingVariable.reconstructTc                 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   r   r  pytreetree_leavesrl   )r   r^   r4   allowed_input_types	flat_argsrA  r*   r*   r+   r  l  s   
z"DebuggingVariable.can_reorder_logsr   r   )r-   T)r'   r(   r)   r   r0   ru   r  rn   r?   r  r   r*   r*   r5   r+   r  J  s    

r  c                       s8   e Zd ZdZd fddZ							
dddZ  ZS )LoggingLoggerVariablez<
    Represents a call to any of logging.Logger methods
    r-   Nc                    r   r.   ry  rr  r5   r*   r+   r0     r   zLoggingLoggerVariable.__init__r^   r_   r4   r`   r   c                 C   sP   |j rd S t| j|d }t|dd }||htjjjr"tj	
d S td d S )Nrv   zLogger not supported for non-export cases. To avoid graph breaks caused by logger in compile-mode, it is recommended to disable logging by adding logging methods to config.ignore_logger_methods)r  rJ   rG   intersectionrh   r  r   ignore_logger_methodsr   r   r\   r   )r3   r@   rO   r^   r4   methodr6  r*   r*   r+   rt     s   z!LoggingLoggerVariable.call_methodr   r   rN  r*   r*   r5   r+   r    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 d fdd	Zd
d Zdee deeef defddZdd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*   r-   Nc                    r   r.   ry  rr  r5   r*   r+   r0     r   zConstantLikeVariable.__init__c                 C   r   r7   ru  r   r*   r*   r+   rB     r   z'ConstantLikeVariable.as_python_constantr^   r4   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   r  r*   r  r2  r*   r*   r+   r    r  z4ConstantLikeVariable.call_method.<locals>.<listcomp>c                 S   r  r*   r  r  r*   r*   r+   r    r  z4ConstantLikeVariable.call_method.<locals>.<dictcomp>r  z(*z, **r   z() -> )r   r~   r   _error_prefixrJ   rG   r   r   rX   r\   rl   reMatchConstantRegexMatchVariable)r3   r@   rO   r^   r4   cargsckwargsr   r*   r*   r+   rt     s   &z ConstantLikeVariable.call_methodr@   r%   rO   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 r7   )rJ   rG   rl   np_floatingfloatnp_dtypeNumpyDTypeVariablerF   rE   
np_genericr  r   r   rX   r\   rY   rv  r*   r*   r+   r]     s   
z ConstantLikeVariable.var_getattrr   )r'   r(   r)   r   r  r  r  r  r  r  r  r  ImportErrorrF   r0   rB   r+  r   rc  r   rt   r]   r   r*   r*   r5   r+   r    s&    

r  c                   @   r   )RegexPatternVariablez
re.PatternNr'   r(   r)   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d fddZ  ZS )TorchVersionVariableztorch.__version__r-   Nc                    s6   | dtj |d tju sJ t jdi | d S )NrG   r*   )
setdefaultrh   __version__r/   r0   r   r5   r*   r+   r0     s   zTorchVersionVariable.__init__r   )r'   r(   r)   r  r0   r   r*   r*   r5   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).
        )rG   rF   r'   r   r*   r*   r+   r<    s   
zNumpyDTypeVariable.as_proxyN)r'   r(   r)   r  r<  r*   r*   r*   r+   r    s    r  c                       s,   e Zd ZdZd
 fddZddd	Z  ZS )RandomClassVariablezrandom.Randomr-   Nc                    r   r.   r   r   r5   r*   r+   r0     r   zRandomClassVariable.__init__r@   r%   c                 C   sT   t |dkrtd n|rtd t |dkrtjd n|d }t|tj dS )Nr   zrandom.Random() with > 1 argzrandom.Random() with kwargsr   )seedrm   )rL   r   r   r   r\   RandomVariablebaseValueMutationNew)r3   r@   r^   r4   r  r*   r*   r+   rn     s   
 
z!RandomClassVariable.call_functionr   r   )r'   r(   r)   r   r0   rn   r   r*   r*   r5   r+   r    s    r  c                       s   e Zd ZdZdhejZh dZ		ddeej	 dee ddf fdd	Z
d
d Zdd Zedd Zedd Zedd Zedd Zdee deeef def fddZdd Z  ZS )r  zrandom.Random()

    Implemented by wrapping a VariableTracker around a random.Random object.
    The supported methods for the random.Random object cannot be overriden.
    Assumes that random objects behave the same given a set seed or state.
    random>   r  randintuniform	randrangeNrandr  r-   c                    sj   t  jdi | |d ur#| |sJ t | _| j|  d S |d ur+| nd }t|| _d S r.   )r/   r0   is_supported_random_objr  RandomsetstategetstaterB   )r3   r  r  r4   r5   r*   r+   r0   &  s   
zRandomVariable.__init__c                 C   rs  r7   )r  r  r   r*   r*   r+   rC   5  r   zRandomVariable.python_typec                 C   r   r7   )r  r   r*   r*   r+   rB   8  r   z!RandomVariable.as_python_constantc                 C   s   t | tjur	dS ttjdD ]2}t| |s dS t| |}t	
|r3|ttj|| kr2 dS qt|dd ttj|urB dS qdS )NF)r  r  r  rv   T)rF   r  r  r  r  r  _supported_fn_namesro   rJ   r  	isbuiltinr"  )r   rO   methr*   r*   r+   r  ;  s    


z&RandomVariable.is_supported_random_objc                 C   sz   t | tu sJ t | d tu sJ t | d tu sJ tdd | d D s)J | d d u s9t | d tu s;J d S d S )Nr   r   c                 s   s    | ]	}t |tu V  qd S r7   )rF   intr2  r*   r*   r+   r5  S  s    z-RandomVariable.check_state.<locals>.<genexpr>r   )rF   r   r  r  r  stater*   r*   r+   check_stateN  s
   (zRandomVariable.check_statec                 C   sF   t |  ttj| d tdd | d D tj| d gS )Nr   c                 S   s   g | ]}t j|qS r*   r  r2  r*   r*   r+   r  ]  s    z-RandomVariable.wrap_state.<locals>.<listcomp>r   r   )r  r  r   rC  r   r\   r  r*   r*   r+   
wrap_stateV  s   
zRandomVariable.wrap_statec                 C   s   |   }t| |S r7   )rB   r  r  )r  	state_objr*   r*   r+   unwrap_statec  s   
zRandomVariable.unwrap_stater^   r4   c                    s   dkr&|j j|  | jjdd |D i dd | D  tjd S  dkr2| 	| j
 S  dkrN|j j|  | j| |d  tjd S  | jv r|j j|  | j
  fd	d
}t| j dd |D i dd | D  t||||S t | ||S )Nr  c                 S   r  r*   r  r2  r*   r*   r+   r  s  r  z.RandomVariable.call_method.<locals>.<listcomp>c                 S   r  r*   r  )r3  r`  r   r*   r*   r+   r  t  r  z.RandomVariable.call_method.<locals>.<dictcomp>r  r  r   c                     s&   t  }| t| | i |S r7   )r  r  r  rJ   )r^   r4   rrO   r  r*   r+   call_random_meth  s   
z4RandomVariable.call_method.<locals>.call_random_methc                 S   r  r*   r  r2  r*   r*   r+   r    r  c                 S   r  r*   r  r  r*   r*   r+   r    r  )r   rk   mutationr  r  r   r   r   r\   r  r  r  r  r  rJ   r"   r/   rt   )r3   r@   rO   r^   r4   r  r5   r   r+   rt   i  s2   


zRandomVariable.call_methodc                    s\      fdd  dd     d  | | j   dd    d S )Nc                      s      t dgS )Nr  )r<   create_load_python_moduler  r  r*   r>   r*   r+   r:     s
    z,RandomVariable.reconstruct.<locals>.<lambda>r   Fr  r   T)r;   rn   r   r   r  r  r  pop_topr=   r*   r>   r+   r?     s   

zRandomVariable.reconstructrE  )r'   r(   r)   r   r   r   r  r   r  r  r0   rC   rB   ru   r  r  r  r  r+  rc  r   rt   r?   r   r*   r*   r5   r+   r    sD    	




'r  c                       sJ   e Zd Zedd Z fddZ							
		dddZdd Z  ZS )WeakRefVariablec                 K   sD   | dd }| }|ot|}t| ||}||d< t|fi |S )NrD   )rN   r   r   r   r  )r@   weakref_valueoptionsrD   referentreferent_vtr*   r*   r+   r     s   zWeakRefVariable.buildc                    r   r.   )r/   r0   r	  )r3   r	  r  r5   r*   r+   r0     r   zWeakRefVariable.__init__r@   r%   r^   r_   r4   r`   r-   r   c                 C   r   r7   )r	  r  r*   r*   r+   rn     s   zWeakRefVariable.call_functionc                    s0      fdd  | j  tdd d S )Nc                      s     ddS )Nweakrefref)r   r*   r>   r*   r+   r:     s    z-WeakRefVariable.reconstruct.<locals>.<lambda>r   F)r;   r	  r<   r   r=   r*   r>   r+   r?     s   
zWeakRefVariable.reconstructr   )	r'   r(   r)   ru   r   r0   rn   r?   r   r*   r*   r5   r+   r    s    

r  )lr   r)  r9  r  r  r  r  r  rw   r   typingr   r   torch._Crh   torch._numpyr  r  torch.utils._pytreer  _pytreer  r   r   r   bytecode_transformationr   r   create_parameter_opr	   excr
   r   guardsr   r   mutation_guardr   rD   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r   rS  r   r   r    r!   user_definedr"   r#   r$   torch._dynamo.symbolic_convertr%   r&   r,   r   r   r   r   r   r   r   r   	dataclassr&  r  rF  rg   rY   rd  rm  ro  rx  	lru_cacher  r  r  r  r   r  r  r  r  r  r  r  r  r  finfoiinfor  r  r  r  r  r*   r*   r*   r+   <module>   s   , Ss= 4o) ,&
@


	n17:	 