o
    Ih                  	   @   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	m
Z
 ddlZddlmZ ddlZddlZddl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 dd	lmZm Z  dd
l!m"Z" ddl#m$Z$m%Z%m&Z&m'Z' ddl(m)Z) ddl*m+Z+m,Z, ddl-m.Z. ddl/m0Z0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9 ddl:m;Z; ddl<m=Z= ddl>m?Z? zddl@ZAW n eBy   dZAY nw e
rddlCmDZD eEeFZGejHejIejJejKejLejMejNejOdZPejNejOejLejMdZQi ePeQZReSTePU ZVeSTeQU ZWdd ZXG dd de;ZYG dd de;ZZG dd deYZ[G dd  d eYZ\G d!d" d"eYZ]G d#d$ d$e;Z^G d%d& d&e;Z_G d'd( d(e;Z`dS ))a  
This module contains variable tracker classes for handling tensors and tensor-related operations in Dynamo.

The main class is TensorVariable which represents torch.Tensor inputs and intermediate values in the FX graph.
It handles tensor operations, method calls, and maintains metadata about tensor properties like dtype, device, etc.

Other key classes include:
- SymNodeVariable: Represents symbolic scalars (int/float/bool) used for size computation and unspecialized values
- NumpyNdarrayVariable: Handles numpy array interop through torch._numpy
- UnspecializedPythonVariable: Represents unspecialized Python numeric values as 1-element tensors
- TensorSubclassVariable: Handles tensor subclasses with __torch_function__ overrides
- UntypedStorageVariable: Represents tensor storage objects
- DataPtrVariable: Handles tensor data pointer operations

These classes work together to track tensor operations and properties during Dynamo's tracing process.
    N)TYPE_CHECKING)compiled_autograd)is_sparse_any)guard_scalarGuardOnDataDependentSymNodehas_free_symbolsis_symbolicSymTypes)is_traceable_wrapper_subclass   )config	variables)trace_wrapped)unimplemented$UnknownPropertiesDuringBackwardTrace	UserErrorUserErrorType)call_hook_from_backward_state)GuardBuilderinstall_guard)
AttrSource)
fqnget_custom_getattrget_fake_valueget_real_valueguard_if_dynobject_has_getattributeproductproxy_args_kwargsset_example_valuetensortype_to_dtype   )VariableTracker)ConstantVariableSizeVariableInstructionTranslator)><z>=z<===!=isis not)r,   r-   r*   r+   c                 C   s@   t | otjj|  ot| dot| jtjot	| j| j
d S )N__self__)callabletorch_dynamoutilsr   hasattr
isinstancer.   Tensorgetattr__name__value r:   R/var/www/vscode/kcb/lib/python3.10/site-packages/torch/_dynamo/variables/tensor.pyis_bound_tensor_methodt   s   r<   c                       s   e Zd ZdZdddddddd	d
ddddddhejZdd Zddddddejj	ddf fddZ
dd Zdd Zdd Zedejfdd Zdd#d$Zd%d& Zd'd( Zd)d* Zd+d, Zd-d. Zd/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ ZddAdBZ ddCdDZ!dEdF Z"dGdH Z#dddIdJZ$dKdL Z%e&dMdN Z'dOdP Z(dQdR Z)	S	T	U	V		WddXdYZ*dZd[ Z+d\d] Z,dd^d_Z-d`da Z.e.Z/dbdc Z0e0Z1ddde Z2dfdg Z3dhdi Z4ddjdkZ5ddmdnZ6dodp Z7dqdr Z8dsdt Z9dldudvdwZ:dxdy Z;dzd{ Z<d|d} Z=d~d Z>dd Z?ee@Addd ZBdd ZCddddZDdd ZEdd ZFdd ZGdd ZHdd ZIdd ZJddddZKddddZLdd ZMdd ZNdd ZOdd ZPdd ZQdeRdefddZSdddZTdd ZUdd ZVdeRfddZW  ZXS )TensorVariablez=A torch.Tensor input or an intermediate value in the FX graphproxydtypedevicelayoutndimsizestriderequires_gradis_quantizedis_contiguous	is_nested	is_sparse
class_typespecialized_value_is_name_setc                 C   s   t | jj| jjS )z
        Get the actual value represented by this variable if computation is run
        using the user-provided inputs.
        NOTE: this runs actual tensor computation and may be
        slow and memory-intensive.
        )r   r>   nodetracerselfr:   r:   r;   r      s   zTensorVariable.get_real_valueN)_sizerD   rG   rL   returnc                   s   t  jdi | || _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|	| _|
| _|| _|d u r>| jjjdk}|| _d S )Nplaceholderr:   )super__init__r>   r?   r@   rA   rB   rQ   rD   rE   rF   rG   rH   rI   rJ   has_grad_fnrM   oprL   )rP   r>   r?   r@   rA   rB   rE   rH   rF   rI   rJ   rV   rQ   rD   rG   rL   kwargs	__class__r:   r;   rU      s$   
zTensorVariable.__init__c                 C   s   t | jjjd S Nexample_value)reprr>   rM   metarO   r:   r:   r;   
debug_repr   s   zTensorVariable.debug_reprc                 C      | j S Nr>   rO   r:   r:   r;   as_proxy      zTensorVariable.as_proxyc                 C   r`   ra   )rJ   rO   r:   r:   r;   python_type   rd   zTensorVariable.python_typer9   c              
      s    j  j jt j j j j jt	 d	}z	 j
d u|d< W n ty.   d|d< Y nw t rFt sFtdd   D |d< |S t s{tdd   D |d< t  |d< tjj rld |d	< |S t fd
dtjjD |d	< |S )N)	r?   r@   rA   rB   rE   rH   rF   rI   rJ   rV   Fc                 S       g | ]}t |rt|n|qS r:   r   int.0sr:   r:   r;   
<listcomp>        z-TensorVariable.specialize.<locals>.<listcomp>rQ   c                 S   rf   r:   rg   ri   r:   r:   r;   rl      rm   rD   rG   c                    s   g | ]
} j |d r|qS )memory_format)rG   rj   xr8   r:   r;   rl      s    
)r?   r@   rA   rh   rB   rE   rH   rF   rI   typegrad_fn	Exceptionr   r   tuplerC   rD   r0   _C
_functorchis_batchedtensor_prims_common_memory_formats)r9   propsr:   r8   r;   
specialize   sD   	
zTensorVariable.specializetxr'   c              
   C   s`  | j jjd }| jsCt|rC| \}}t|  |}t||}||v r9t|t	j
s,J ddlm} ||||dS t|sCt||S | jrK| j sMt|jj|jjd}	z
t| j |	}
W n tyo } zt|d }~ww |
d u rvtt|
r|tt|
rtt|
|}t| j|}t|tj t|rddl m!} || ||t"|dS t|||S )Nr\   r!   wrap_fx_proxy)r}   r>   r\   LGGetAttrVariable)sourcepy_type)#r>   rM   r^   r   r
   __tensor_flatten__r6   rc   r4   r0   r5   builderr   r/   r"   buildsubguards_allowedNotImplementedErroroutputlocal_scopeglobal_scopeevalnamert   r   r   r   r   
make_guardr   HASATTRr<   miscr   rr   )rP   r}   r   fake_valattrs_ctxr>   r\   r   scope_input_associated_real_valueexc
real_valueattr_sourcer   r:   r:   r;   dynamic_getattr  sF   

zTensorVariable.dynamic_getattrc                 C   s&   | j d urt| j S | |dg i S )Ndim)rB   r#   createcall_methodrP   r}   r:   r:   r;   method_attr_ndimD  s   
zTensorVariable.method_attr_ndimc                 C      | j d urt| j S d S ra   )r?   r#   r   r   r:   r:   r;   method_attr_dtypeJ     
z TensorVariable.method_attr_dtypec                 C   r   ra   )r@   r#   r   r   r:   r:   r;   method_attr_deviceN  r   z!TensorVariable.method_attr_devicec                 C   r   ra   )rA   r#   r   r   r:   r:   r;   method_attr_layoutR  r   z!TensorVariable.method_attr_layoutc                 C   s    | j d urt| j jdkS d S )Ncuda)r@   r#   r   rr   r   r:   r:   r;   method_attr_is_cudaV  s   
z"TensorVariable.method_attr_is_cudac                 C   s0   |   rdd | jD }t|S | |dg i S )Nc                 S   s   g | ]}t j|qS r:   )r   r#   r   rp   r:   r:   r;   rl   \  s    z4TensorVariable.method_attr_shape.<locals>.<listcomp>rC   )
valid_sizerC   r%   r   )rP   r}   sizesr:   r:   r;   method_attr_shapeZ  s   z TensorVariable.method_attr_shapec                 C   r   ra   )rE   r#   r   r   r:   r:   r;   method_attr_requires_grada  r   z(TensorVariable.method_attr_requires_gradc                 C   r   ra   )rF   r#   r   r   r:   r:   r;   method_attr_is_quantizede  r   z'TensorVariable.method_attr_is_quantizedc                 C   r   ra   )rI   r#   r   r   r:   r:   r;   method_attr_is_sparsei  r   z$TensorVariable.method_attr_is_sparsec                 C   r   ra   )rH   r#   r   r   r:   r:   r;   method_attr_is_nestedm  r   z$TensorVariable.method_attr_is_nestedc                 C      t d d S )Nz,retain_grad does not work with AOTDispatcherr   r   r:   r:   r;   method_attr_retain_gradq     z&TensorVariable.method_attr_retain_gradc                 C   s   t tjjj|| gi S ra   )r   TorchInGraphFunctionVariabler0   rv   	_autograd_get_data_attrcall_functionr   r:   r:   r;   method_attr_datat  s
   zTensorVariable.method_attr_datac                 C   s   | j r	td d S td S )NzTensorVariable has a grad_fn)rV   r   r   r#   r   r:   r:   r;   method_attr_grad_fny  s   
z"TensorVariable.method_attr_grad_fnc                 C   s"   ddl m} t||| gi S )Nr   )_tensor_version)tensor_version_opr   r   r   r   )rP   r}   r   r:   r:   r;   method_attr__version  s   
z#TensorVariable.method_attr__versionc                 C   s   ddl m} ddlm} z|t|| t|gi }t|| }W n ty,   d}Y nw | j	r<t
t| j	|tj t|S )Nr!   r   )BuiltinVariableF) r   builtinr   r6   r   r#   r4   AttributeErrorr   r   r   r   r   r   )rP   r}   r   r   r   varret_valr:   r:   r;   call_obj_hasattr  s   zTensorVariable.call_obj_hasattrc                    s  ddl m} r(  v rtd  d n  v r(td  d dkr2| S td  d }|d urC|nd }|d urhj	rhj	
 rh d	vrY| shttj tj	 |_	j	d urttjj rttjj }t|d
rt|| d rtjjt|| d jv rtjjtj	 dS |d u r dkr fdd}| }|d u r }|d u rt|S )Nr!   )UserDefinedClassVariablezGetattr invocation z  in strict mode is not supportedzUnknown property z] during speculating backward, dynamo will insert contiguous call ahead and speculate it againrZ   method_attr_)gradrE   	overloadsr   )r   r   c                     s   ddl m}  ddlm} z	ttj }W n
 ty   Y d S w t	|t
jkr)d S |  }jd urA| |tj dS | |dS )Nr!   r~   r   )r}   r>   r   r}   r>   )r   r   r   r   inspectgetattr_staticr0   r5   r   rr   typesGetSetDescriptorTypecreate_getattr_proxyrc   r   r   )r   r   static_attrr>   r   rP   r}   r:   r;   try_generic_attr_handling  s   	
z=TensorVariable.var_getattr.<locals>.try_generic_attr_handling)r   r   is_strict_mode_strict_mode_banned_opsr   #_strict_mode_conditional_banned_opsr   re   r6   r   r   is_python_constantr   r   r   
TYPE_MATCHr   r3   r0   opsatenr   Taginplace_viewtagsr   r   DelayGraphBreakVariabler   r   )rP   r}   r   r   handlerresultfnr   r:   r   r;   var_getattr  sP   



zTensorVariable.var_getattrc              
   C   s   | j std |jj|jjd}z
t| j  |}W n ty3 } ztd|  W Y d }~nd }~ww |d u r<td t| j 	t
j t|}t|S )Nz3call_id not supported for sourceless TensorVariabler   z%error getting associated real value: z%call_id without associated real value)r   r   r   r   r   r   r   rt   r   r   r   ID_MATCHidr#   r   )rP   r}   r   r   r   id_valuer:   r:   r;   call_id  s   
zTensorVariable.call_idc                 C   s
   | j dkS )Nr   )rB   r   r:   r:   r;   has_unpack_var_sequence     
z&TensorVariable.has_unpack_var_sequencec                    s  ddl m   rt j}n dg i }t|tsJ t|j}|dks,J d  r6 jd }n$ dt	
dgi }t|tt	fsKJ t|trW|j}n|j}|d u rct|}nt||ksvJ d| dt| d fd	d
|D S )Nr!   wrap_fx_proxy_clsrC   r   zCan't unpack scalar tensors.zCan't unpack a tensor of z rows into a tuple of z
 elements.c                    s&   g | ]}t    | d qS )
target_clsr}   r>   )rr   rc   )rj   irP   r}   r   r:   r;   rl   %  s    z6TensorVariable.unpack_var_sequence.<locals>.<listcomp>)r   r   r   lenrC   r   r4   r%   itemsr#   r   SymNodeVariableevaluate_exprr   r9   range)rP   r}   idxessize_lensize_varlength
dyn_lengthr:   r   r;   unpack_var_sequence  s,   


z"TensorVariable.unpack_var_sequencec                 C   s
   | j d uS ra   rQ   rO   r:   r:   r;   r   *  r   zTensorVariable.valid_sizec                 C   s   | j d us	J d| j S )Nz%accessing None size in TensorVariabler   rO   r:   r:   r;   rC   -  s   zTensorVariable.sizec                 C   
   t jjjS ra   )r0   r1   r   )_autograd_backward_strict_mode_banned_opsrO   r:   r:   r;   r   2  r   z&TensorVariable._strict_mode_banned_opsc                 C   r   ra   )r0   r1   r   5_autograd_backward_strict_mode_conditional_banned_opsrO   r:   r:   r;   r   5  s   z2TensorVariable._strict_mode_conditional_banned_opsargslist[VariableTracker]rX   dict[str, VariableTracker]r"   c              
   C   s  ddl m}m} ddlm}m} | |r#||  v r#td| d d}	zt	
tj| d}	W n ty;   d}	Y nw ||t| gt| |rz|	rz| jrb||tt| jd|t	
tj|}
n
||ttj|}
|||
t| gt| |S 	 |d	krt|d
 tjrtdS z
t| d| }W n	 ty   Y n,w z||i |}|r|W S W n ty } ztd| d|  W Y d }~nd }~ww ddl m} |||jjd|gt| g||R  S )Nr!   )SourcelessBuilderVariableBuilder)can_dispatch_torch_functiondispatch_torch_functionzIllegal method invocation z in strict modeFTrZ   __eq__r   method_zunhandled args for z: r~   r   )r   r   r   torch_functionr   r  r   r   r   r   r   r0   r5   r   ru   listr   r   r   r6   r4   r   r   r#   	TypeErrorr   r   create_proxyr   )rP   r}   r   r   rX   r   r   r   r  is_base_tensor_methodfunc_varhandler_methodr   er   r:   r:   r;   r   :  sh   
 zTensorVariable.call_methodc                 O      | j dg|R i |S )NrC   _method_size_striderP   r   rX   r:   r:   r;   method_size     zTensorVariable.method_sizec                 O   r  )NrD   r  r  r:   r:   r;   method_stride  r  zTensorVariable.method_stridec                 C   s   t |}dd }|dkr|ntj}|dkrt| |}n|dkr'|  r'| j}nd }|d ur<|d u r5||S t|| S | jjj	d }d uru|d u rct|| }t
|sa|tdd |D S d S t|||}t
|swtt|S d S d S )Nc                    s   t  fdd| D fi  S )Nc                    s   g | ]}t j|fi  qS r:   )r#   r   )rj   yoptionsr:   r;   rl         zXTensorVariable._method_size_stride.<locals>.make_const_size_variable.<locals>.<listcomp>r$   )rq   r  r:   r  r;   make_const_size_variable  s
   zDTensorVariable._method_size_stride.<locals>.make_const_size_variablerC   r\   c                 s       | ]}t |V  qd S ra   rh   rj   rr:   r:   r;   	<genexpr>      z5TensorVariable._method_size_stride.<locals>.<genexpr>)r   r#   r   r6   r   rC   r>   rM   r^   getr   ru   rh   )rP   r   r   r  RetVariabler  fakefake_rr:   r:   r;   r    s0   	z"TensorVariable._method_size_stridec                 C   sV   |   rtt| jS | jjjd }d ur'|	 }t
|s)tt|S d S d S r[   )r   r#   r   r   rC   r>   rM   r^   r  numelr   rh   )rP   r   r!  r:   r:   r;   method_numel  s   zTensorVariable.method_numelc                 C   r   ra   )rB   r#   r   rO   r:   r:   r;   
method_dim  r   zTensorVariable.method_dimc                 C      | j d urt| j jS d S ra   )r?   r#   r   is_floating_pointrO   r:   r:   r;   method_is_floating_point     
z'TensorVariable.method_is_floating_pointc                 C   s*   | j jjd }d urt| S d S r[   )r>   rM   r^   r  r#   r   is_inference)rP   r   r:   r:   r;   method_is_inference  s   z"TensorVariable.method_is_inferencec                 C   r%  ra   )r?   r#   r   
is_complexrO   r:   r:   r;   method_is_complex  r(  z TensorVariable.method_is_complexc                 C   s^   |d ur|  ntj}| jd urt|| jv S | jjj	d }d ur-t|j|dS d S )Nr\   rn   )
as_python_constantr0   contiguous_formatrG   r#   r   r>   rM   r^   r  )rP   ro   r   r:   r:   r;   method_is_contiguous  s   

z#TensorVariable.method_is_contiguousFc           
         s  |d u r: j d ur:t jtjr:t fddt D } jjdkr,t	d|j
 S t	d jj d|j
 S |d ur~tt| dkr| }t	t|}ddlm} d	d
lm} | }	|rkd|i|}||	|	jjddgt |g|R  S d S d S )Nc                 3   s"    | ]\}} j |v r|V  qd S ra   )r?   rj   kvrO   r:   r;   r    s    z-TensorVariable.method_type.<locals>.<genexpr>cpuztorch..ztorch.tensortyper   r&   r!   r~   non_blockingr   rr   )r?   r4   r@   r0   nextr    r   rr   r#   r   r7   r   r-  symbolic_convertr'   r   r   
current_txr   r  r   )
rP   r?   r5  rX   
tensortypetensor_typetensor_type_constr'   r   r}   r:   rO   r;   method_type  s@   
zTensorVariable.method_typec                 C   sp   t |tr4|jr6ddlm} ddlm} | }| }t	
||jjtt|jdd}||| ||S d S d S )Nr   r&   r!   TensorWithTFOverrideVariable__torch_function____func__)r4   TensorSubclassVariabler   r7  r'   r  r>  r8  r-  r"   r   r?  r@  r   from_tensor_var)rP   clsr'   r>  r}   py_clstorch_fnr:   r:   r;   method_as_subclass  s   z!TensorVariable.method_as_subclassc                 C   s4   t | jtjr| jjdkr| jjnd}t|S d S )Nr3  )r4   r@   r0   rr   indexr#   r   )rP   rH  r:   r:   r;   method_get_device  s   
z TensorVariable.method_get_devicec                 C   s   t | jjS ra   )r#   r   r?   itemsizerO   r:   r:   r;   method_element_size   s   z"TensorVariable.method_element_size)forcec                C   s   t jstd tstd | jtjkrtd| j dddlm	} |
 }|rA| rA| |dg i }|jdd	| fi }n|jjdd
gt| | gi R  }t||S )Nz+Tensor.numpy(). config.trace_numpy is Falsez&Tensor.numpy(). NumPy is not availablezcan't convert z1 layout tensor to numpy. Use Tensor.dense() firstr   r&   detachr   r3  view_as)r   trace_numpyr   nprA   r0   stridedr  r7  r'   r8  r-  r   r   r  rc   r   NumpyNdarrayVariabler   )rP   rL  r'   r}   tr>   r:   r:   r;   method_numpy#  s&   zTensorVariable.method_numpyc                    sZ   ddl m} ddlm |  fdd |  jjd } ||  }t	|S )Nr   r&   r!   r~   c                    s   fdd| j tjtjtjtjfvrtd |  dkr#|  S |  dkr5 fddt| D S  fddt| D S )	Nc                    sF   t jj jdd  | W  d    S 1 sw   Y  d S )Nallow_scalar_outputsT)unittestmockpatchobject	fake_modeitem)r   	sub_proxy)r}   r   r:   r;   wrapD  s   $z:TensorVariable.method_tolist.<locals>.tolist.<locals>.wrapz1Input tensor for tolist must be an integer tensorr   r!   c                    s   g | ]\}}| | qS r:   r:   )rj   r   valr\  r]  r:   r;   rl   [  r  z@TensorVariable.method_tolist.<locals>.tolist.<locals>.<listcomp>c                    s    g | ]\}}| | d qS ))r\  r:   )rj   r   
sub_tensor)r\  tolistr:   r;   rl   ]  s    )	r?   r0   int8int16int32int64r   r   	enumerate)tensorr\  ra  r}   r   r_  r;   ra  C  s   
z,TensorVariable.method_tolist.<locals>.tolistr\   )
r7  r'   r   r   r8  rc   rM   r^   r"   r   )rP   r'   rg  outr:   rh  r;   method_tolist=  s   zTensorVariable.method_tolistc                 O   r   )NzTensor.backwardr   r  r:   r:   r;   method_backwardf  r   zTensorVariable.method_backwardc                 O   s   t | S ra   )DataPtrVariabler  r:   r:   r;   method_data_ptri  s   zTensorVariable.method_data_ptrc                 O   s   t js|   td d S d S )NzTensor.item)r   capture_scalar_outputs_warn_capture_scalar_outputsr   r  r:   r:   r;   method_iteml  s   zTensorVariable.method_itemc                 O   s   ddl m} ddlm} | }t|d tr&tjt	j
d|d g}}ntj}|jjd|gt| gt| |R  }|||S )Nr   r&   r!   r~   r   r   )r7  r'   r   r   r8  r4   r   r0   selectr   r#   r   operatorgetitemr   r  r   r  )rP   r   rX   r'   r   r}   r   r>   r:   r:   r;   method___getitem__q  s"   

z!TensorVariable.method___getitem__c                  C   s2   t jj } dt| }tt	
d| d S )Nr   a                      Graph break from `Tensor.item()`, consider setting:
                        torch._dynamo.config.capture_scalar_outputs = True
                    or:
                        env TORCHDYNAMO_CAPTURE_SCALAR_OUTPUTS=1
                    to include these operations in the captured graph.

                    Graph break: from user code at:
                    %s
                )r0   _guardsTracingContextextract_stackjoin	tracebackformat_listlogwarningtextwrapdedent)
user_stackuser_stack_formattedr:   r:   r;   ro    s   z+TensorVariable._warn_capture_scalar_outputsc                 C   s,   ddl m} | }| |dtdgi S )Nr   r&   rC   r   )r7  r'   r8  r   r#   r   )rP   r'   r}   r:   r:   r;   method___len__  s   zTensorVariable.method___len__r8   c                C   sL   ddl m} | }|d ur$ddlm} |t||j| |||gi S d S )Nr   r&   )	polyfills)	r7  r'   r8  r   r  inline_user_function_returnr"   r   addcmul_inplace)rP   tensor1tensor2r9   r'   r}   r  r:   r:   r;   method_addcmul_  s   
zTensorVariable.method_addcmul_c                    sN    fdd ddl m} | }|jjdtjgt| ||gi R   t	d S )Nc                    sD   t | tr| jtjtjfv S t | tjr t fdd| j	D S dS )Nc                 3   s    | ]} |V  qd S ra   r:   )rj   r[  has_bool_keyr:   r;   r    r  zJTensorVariable.method___setitem__.<locals>.has_bool_key.<locals>.<genexpr>F)
r4   r=   r?   r0   boolrb  r   TupleVariableanyr   )r2  r  r:   r;   r    s
   
z7TensorVariable.method___setitem__.<locals>.has_bool_keyr   r&   r   )
r7  r'   r8  r   r  rr  setitemr   r#   r   )rP   keyr9   r'   r}   r:   r  r;   method___setitem__  s   
z!TensorVariable.method___setitem__c                 O   r   )NzTensor.resize_r   r  r:   r:   r;   method_resize_  r   zTensorVariable.method_resize_c                 O   r   )NzTensor.resize_as_r   r  r:   r:   r;   method_resize_as_  r   z TensorVariable.method_resize_as_c                 O   r   )NzTensor.sparse_resize_r   r  r:   r:   r;   method_sparse_resize_  r   z$TensorVariable.method_sparse_resize_c                 O   r   )NzTensor.sparse_resize_and_clear_r   r  r:   r:   r;   method_sparse_resize_and_clear_  r   z.TensorVariable.method_sparse_resize_and_clear_c                 O   s   t |dkrtd d S d S )Nr!   z(Tensor.set_.source_Tensor_storage_offset)r   r   r  r:   r:   r;   method_set_  s   zTensorVariable.method_set_)alphac                C   sL   |d ur$ddl m} | }ttj|||gi }| |d|gi S d S Nr   r&   add_)	r7  r'   r8  r   r   r0   mulr   r   )rP   otherr  r'   r}   r   r:   r:   r;   method_add_  s   
zTensorVariable.method_add_c                C   sf   ddl m} | }|d ur1ttj|||gi }ttj|||gi }| 	|d|gi S d S r  )
r7  r'   r8  r   r   r0   divr   r  r   )rP   r  r  r9   r'   r}   r   r:   r:   r;   method_addcdiv_  s   

zTensorVariable.method_addcdiv_c                 C   sV   ddl m} | }ttj|| |gi }ttj||gi }|	|dg i S )Nr   r&   r[  )
r7  r'   r8  r   r   r0   eqr   r  r   )rP   argr'   r}   r   r:   r:   r;   method___contains__  s   
z"TensorVariable.method___contains__c                    z   ddl m} | }dd |D  dd | D  fdd}d	|_d
dlm} |||jjd|gt	| gi R  dS )Nr   r&   c                 S      g | ]}|  qS r:   r-  rp   r:   r:   r;   rl         z6TensorVariable.method_redistribute.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r:   r  r0  r:   r:   r;   
<dictcomp>      z6TensorVariable.method_redistribute.<locals>.<dictcomp>c                       | j  i S ra   )redistributerq   args_as_valuekwargs_as_valuer:   r;   redistribute_fn_with_prim_types     zKTensorVariable.method_redistribute.<locals>.redistribute_fn_with_prim_typesprim_redistributer!   r~   r   r   
r7  r'   r8  r   r7   r   r   r   r  r   )rP   r   rX   r'   r}   r  r   r:   r  r;   method_redistribute      
z"TensorVariable.method_redistributec                    r  )Nr   r&   c                 S   r  r:   r  rp   r:   r:   r;   rl   %  r  z2TensorVariable.method_to_local.<locals>.<listcomp>c                 S   r  r:   r  r0  r:   r:   r;   r  &  r  z2TensorVariable.method_to_local.<locals>.<dictcomp>c                    r  ra   )to_localr  r  r:   r;   to_local_fn_with_prim_types(  r  zCTensorVariable.method_to_local.<locals>.to_local_fn_with_prim_typesprim_to_localr!   r~   r   r   r  )rP   r   rX   r'   r}   r  r   r:   r  r;   method_to_local  r  zTensorVariable.method_to_localc                 O   r  )Nregister_hook_method_register_hookr  r:   r:   r;   method_register_hook9  r  z#TensorVariable.method_register_hookc                 O   r  )N"register_post_accumulate_grad_hookr  r  r:   r:   r;   )method_register_post_accumulate_grad_hook<  s   z8TensorVariable.method_register_post_accumulate_grad_hookr   hookc           
         s   ddl m} | }| jsAtjstd |j|\ } fdd}ddl	m
} |  }d|jjd	< |||jd
|||fi S tjtj d}	|jj| ||	 |	S )Nr   r&   z<Compilation of intermediate hooks requires compiled autogradc                    s$   t | }|tjtt| d d S )N)r   bw_state	hook_name)r6   	functoolspartialr   r   )rg  r  r  r  r   r:   r;   _register_hook_trampoline_  s   
zGTensorVariable._method_register_hook.<locals>._register_hook_trampoliner!   r~   Thas_backward_hookr   )mutation_type)r7  r'   r8  r   r   compiled_autograd_enabledr   r   add_backward_state_hookr   r   rc   rM   r^   r  r   RemovableHandleVariablebaseValueMutationNewside_effectsr  )
rP   r   r  r'   r}   bw_state_proxyr  r   
self_proxyhandle_variabler:   r  r;   r  A  s4   
z$TensorVariable._method_register_hookTc                 C   s6   |dur|  }|  jjd j|krtd d S | S )NTr\   zTensor.requires_grad_)r-  rc   rM   r^   rE   r   )rP   rE   r:   r:   r;   method_requires_grad_  s
   z$TensorVariable.method_requires_grad_c                 O   s`   t |dkrt|d tst |dkr,tdd |D r.ddlm} | | d||S d S d S )Nr!   r   c                 s   s&    | ]}t |to| tkV  qd S ra   )r4   r#   re   rh   )rj   ar:   r:   r;   r    s    
z,TensorVariable.method_new.<locals>.<genexpr>r   r&   	new_empty)r   r4   r%   allr7  r'   r   r8  )rP   r   rX   r'   r:   r:   r;   
method_new  s   zTensorVariable.method_newc                 C   s   t | |  jjd  S r[   )UntypedStorageVariablerc   rM   r^   untyped_storagerO   r:   r:   r;   method_untyped_storage  s   z%TensorVariable.method_untyped_storagec                 C   s"   | j s| jj| d| _ d S d S )NT)rL   r>   rM   _rename)rP   r   r:   r:   r;   set_name_hint  s   
zTensorVariable.set_name_hintr}   r'   ra   r   r   rX   r   rR   r"   )NFT)Yr7   
__module____qualname____doc__r"   _nonvar_fieldsr   r0   fxProxyrU   r_   rc   re   staticmethodr5   r|   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyrC   r   r   r   r  r  r  r#  method_nelementr$  method_ndimensionr'  r*  r,  r/  r<  rF  rI  rK  rT  rj  rk  rm  rp  rt  r  	lru_cachero  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  strr  r  r  r  r  __classcell__r:   r:   rY   r;   r=   ~   s    (
2B

W#

G
)


*)


B	r=   c                       s~   e Zd ZdZddhejZdd ZedddZd fd
dZ	dd Z
dd Zdd ZdddZ							dddZ  ZS )r   z
    Represents a symbolic scalar, either int, float or bool.  This is most commonly used to
    handle symbolic size computation, e.g., tensor.size(0), but it is also used to
    handle logic like float_tensor.item() or unspecialized float inputs.
    r>   sym_numc                 C   s
   t | jS ra   )r]   r  rO   r:   r:   r;   r_     r   zSymNodeVariable.debug_reprNc                 K   s   |d u r
t |j|}d|jjv r|jjd |ksJ t|j| t|tjttfr:t|tjr3t|n|}t	
|S t||fi |S r[   )r   rM   r^   r   r4   sympyIntegerrh   r  r#   r   r   )rC  r}   r>   r  r  r:   r:   r;   r     s   
zSymNodeVariable.createrR   c                    s(   t  jdi | || _|| _d | _d S Nr:   )rT   rU   r>   r  _tensor_var)rP   r>   r  rX   rY   r:   r;   rU     s   
zSymNodeVariable.__init__c                 C   s    t | jtr| jjjS t| jS ra   )r4   r  r	   rM   pytyperr   rO   r:   r:   r;   re     s   

zSymNodeVariable.python_typec                 C   r`   ra   rb   rO   r:   r:   r;   rc     rd   zSymNodeVariable.as_proxyc              	   C   s8   | j d u rt|tj|| gdt||i| _ | j S )Nr?   )r  r"   r   r0   scalar_tensorr   )rP   r}   r?   r:   r:   r;   	as_tensor  s   
zSymNodeVariable.as_tensorc              
   C   sN   zt | jW S  ty& } ztjjjjr tt	j
dt| ddd }~ww )Nz5Consider annotating your code using torch._check*(). constrain_as_size_example)	case_name)r   r  r   r0   r  experimental_configno_data_dependent_graph_breakr   r   ANTI_PATTERNr  )rP   output_graphr  r:   r:   r;   r     s   zSymNodeVariable.evaluate_exprr   r   rX   r   r"   c                 C   s4   ddl m} |||jjd|gt| g||R  S )Nr!   r~   r   )r   r   r   r  r   )rP   r}   r   r   rX   r   r:   r:   r;   r     s   zSymNodeVariable.call_methodra   rR   Nr  )r7   r  r  r  r"   r  r_   classmethodr   rU   re   rc   r  r   r   r  r:   r:   rY   r;   r     s,    
r   c                       sZ   e Zd ZdZedddZdddZedd	 Z	
					d fddZdd Z	  Z
S )rR  z}
    Represents a np.ndarray, but backed by torch Tensor via torch._numpy.ndarray.
    Use this for Tensor.numpy() call.
    r}   r'   c                 K   s"   ddl m} |dt| |d|S )Nr!   r   r   r:   )r   r   rR  )r}   r>   r  r   r:   r:   r;   r     s   zNumpyNdarrayVariable.createc           	         s,  ddl m ddlm d } jjd }t|} fdd} dv r=j	
d	  fi }t|}nQ d
v rItt| S  dv rett|  }sbttdd |D S | S  dkrzt|j }swtt|S | S  dv rtd   n dv rtd |d u rt|S )Nr   )numpy_attr_wrapperr!   r~   r\   c                      s    j d  fi S )Nr   )r   r  rc   r:   r   r  rP   r}   r   r:   r;   insert_into_graph  s   z;NumpyNdarrayVariable.var_getattr.<locals>.insert_into_graph)Trealimagr   )rB   rJ  )shaperD   c                 s   r  ra   r  r  r:   r:   r;   r  ;  r  z3NumpyNdarrayVariable.var_getattr.<locals>.<genexpr>rC   )r  flagsr?   zTODO: add support for ndarray.)__version__z delegate np.__version__ to NumPy)r2   r  r   r   rc   rM   r^   tnpndarrayr   r  rR  r   r#   r6   r   ru   rC   rh   r   r   )	rP   r}   r   r   r\   example_ndarrayr  r>   r  r:   r  r;   r     s>   

z NumpyNdarrayVariable.var_getattrc                    s0   | dkrddd  fdd|  D }||fS )Nclipminmax)a_mina_maxc                    s   i | ]\}}  |||qS r:   )r  r0  kwargs_renamer:   r;   r  M  r  z3NumpyNdarrayVariable.patch_args.<locals>.<dictcomp>)r   )r   r   rX   r:   r  r;   
patch_argsI  s   
zNumpyNdarrayVariable.patch_argsr   r   rX   r   rR   r"   c                    s   ddl m} | |||\}}|dv rt ||||S |dv r't| d |jjd||gt| gt	| |R  }t
||S )Nr   )numpy_method_wrapper)__len__rC   ra  )tostringtobytesz  is not modelled in torch._numpyr   )r2   r  r  rT   r   r   r   r  r   r  rR  r   )rP   r}   r   r   rX   r  r>   rY   r:   r;   r   P  s   z NumpyNdarrayVariable.call_methodc                 C   s   t jS ra   )rP  r  rO   r:   r:   r;   re   g  rd   z NumpyNdarrayVariable.python_typer  r  )r7   r  r  r  r  r   r   r  r   re   r  r:   r:   rY   r;   rR    s    

=
rR  c                       sR   e Zd ZdZddhejZddddejjddf fd	d
Z	e
dddZ  ZS )UnspecializedPythonVariablezO
    This is a 1-element tensor represents unspecialized python float/int.
    	raw_valueneed_unwrapNTr  r  r>   rR   c                   s$   t  j|fi | || _|| _d S ra   )rT   rU   r  r  )rP   r>   r  r  rX   rY   r:   r;   rU   v  s   
z$UnspecializedPythonVariable.__init__c                 C   s   t di t|j||dS )Nr  r:   )r  dict__dict__)rC  tensor_variabler  r  r:   r:   r;   from_tensor_variable}  s   
z0UnspecializedPythonVariable.from_tensor_variabler  r7   r  r  r  r=   r  r0   r  r  rU   r  r  r  r:   r:   rY   r;   r  k  s    r  c                       sF   e Zd ZdZdhejZdejjddf fddZ	e
dd	 Z  ZS )
FakeItemVariablezAn unspecialized python variable which prevents access to the underlying raw value.
    This is needed if item is called on a FakeTensor.r  r>   rR   Nc                    s*   | dd}t j|fi | || _d S )Nr  F)poprT   rU   r  )rP   r>   rX   r  rY   r:   r;   rU     s   
zFakeItemVariable.__init__c                 C   s   t di t|jS r  )r  r  r  )rC  r  r:   r:   r;   r    s   z%FakeItemVariable.from_tensor_variabler  r:   r:   rY   r;   r    s    r  c                       sP   e Zd Zd fddZdddee deeef def fd	d
Zdd Z	  Z
S )rA  rR   Nc                    s   || _ t j|i | d S ra   )r9   rT   rU   )rP   r9   r   rX   rY   r:   r;   rU     s   zTensorSubclassVariable.__init__r}   r'   r   rX   c                    sj   t |dkr-t|d tr-ddlm} t| jd}t|| j	j
|}|||d | j	|S t |||S )Nr!   r   r=  r?  )r   r4   r=   r  r>  r   r   r"   r   r9   r?  rB  rT   r   )rP   r}   r   rX   r>  r   rE  rY   r:   r;   r     s   z$TensorSubclassVariable.call_functionc                 C   r`   ra   r8   rO   r:   r:   r;   r-    rd   z)TensorSubclassVariable.as_python_constantr  )r7   r  r  rU   r  r"   r  r  r   r-  r  r:   r:   rY   r;   rA    s    
rA  c                       sf   e Zd ZdhejZdedejddf fddZde	e de
eef def fd	d
Zdd Z  ZS )r  r\   from_tensorrR   Nc                    s"   t  jdi | || _|| _d S r  )rT   rU   r  r\   )rP   r  r\   rX   rY   r:   r;   rU     s   
zUntypedStorageVariable.__init__r   rX   c                    s   |dkr8|rJ |rJ | j  }t|stt|S ddlm} ddlm	} |||j
d|| j fi S |dkr]t|dkr]|rFJ |j
dtjjj| j |d  fi  | S t ||||S )	NrC   r   )untyped_storage_sizer!   r~   r   resize_r   )r\   rC   r   r#   r   rh   external_utilsr  r   r   r   r  r  rc   r   r0   r   inductorresize_storage_bytes_rT   r   )rP   r}   r   r   rX   r   r  r   rY   r:   r;   r     s6   

	z"UntypedStorageVariable.call_methodc                 C   "   || j  |d |d d S )Nr  r   r  load_methodr   rP   codegenr:   r:   r;   reconstruct     

z"UntypedStorageVariable.reconstruct)r7   r  r  r"   r  r=   r0   UntypedStoragerU   r  r  r  r   r(  r  r:   r:   rY   r;   r    s(    
'r  c                       s.   e Zd Zdeddf fddZdd Z  ZS )rl  r  rR   Nc                    s   t  jdi | || _d S r  )rT   rU   r  )rP   r  rX   rY   r:   r;   rU     s   
zDataPtrVariable.__init__c                 C   r#  )Ndata_ptrr   r$  r&  r:   r:   r;   r(    r)  zDataPtrVariable.reconstruct)r7   r  r  r=   rU   r(  r  r:   r:   rY   r;   rl    s    rl  )ar  r  r   loggingrr  r}  ry  r   rV  typingr   r  torch._numpy_numpyr  torch.fxr0   torch.randomtorch._dynamor   torch._subclasses.meta_utilsr   %torch.fx.experimental.symbolic_shapesr   r   r   r   r	   torch.utils._python_dispatchr
   r   r   r   _trace_wrapped_higher_order_opr   r   r   r   r   r   r   r   guardsr   r   r   r   r2   r   r   r   r   r   r   r   r   r   r    r  r"   constantr#   listsr%   numpyrP  ModuleNotFoundErrortorch._dynamo.symbolic_convertr'   	getLoggerr7   r{  gtltgeler  neis_is_notsupported_tensor_comparison_opssupported_const_comparison_opssupported_comparison_opsr  fromkeysvalues%supported_tensor_comparison_op_values$supported_const_comparison_op_valuesr<   r=   r   rR  r  r  rA  r  rl  r:   r:   r:   r;   <module>   s   0

        0Up>