o
    Zh                  	   @   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:   M/var/www/auris/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_typeZ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>   nodeZtracerselfr:   r:   r;   r      s   zTensorVariable.get_real_valueN)_sizerD   rG   rK   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   rO   rD   rE   rF   rG   rH   rI   rJ   has_grad_fnrL   oprK   )rN   r>   r?   r@   rA   rB   rE   rH   rF   rI   rJ   rT   rO   rD   rG   rK   kwargs	__class__r:   r;   rS      s$   
zTensorVariable.__init__c                 C   s   t | jjjd S Nexample_value)reprr>   rL   metarM   r:   r:   r;   
debug_repr   s   zTensorVariable.debug_reprc                 C      | j S Nr>   rM   r:   r:   r;   as_proxy      zTensorVariable.as_proxyc                 C   r^   r_   )rJ   rM   r:   r:   r;   python_type   rb   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   rT   Fc                 S       g | ]}t |rt|n|qS r:   r   int.0sr:   r:   r;   
<listcomp>        z-TensorVariable.specialize.<locals>.<listcomp>rO   c                 S   rd   r:   re   rg   r:   r:   r;   rj      rk   rD   rG   c                    s   g | ]
} j |d r|qS )memory_format)rG   rh   xr8   r:   r;   rj      s    
)r?   r@   rA   rf   rB   rE   rH   rF   rI   typeZgrad_fn	Exceptionr   r   tuplerC   rD   r0   _CZ
_functorchZis_batchedtensorZ_prims_commonZ_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 )NrZ   r!   wrap_fx_proxy)rv   r>   rZ   LGGetAttrVariable)sourceZpy_type)#r>   rL   r\   r~   r
   Z__tensor_flatten__r6   ra   r4   r0   r5   builderrx   r/   r"   buildsubguards_allowedNotImplementedErroroutputlocal_scopeglobal_scopeevalnamerq   r   r   r   r   
make_guardr   HASATTRr<   miscr}   rp   )rN   rv   r   Zfake_valattrsZ_ctxr>   rZ   rx   scope_input_associated_real_valueexc
real_valueZ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rN   rv   r:   r:   r;   method_attr_ndimD  s   
zTensorVariable.method_attr_ndimc                 C      | j d urt| j S d S r_   )r?   r#   r   r   r:   r:   r;   method_attr_dtypeJ     
z TensorVariable.method_attr_dtypec                 C   r   r_   )r@   r#   r   r   r:   r:   r;   method_attr_deviceN  r   z!TensorVariable.method_attr_devicec                 C   r   r_   )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   rp   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   rn   r:   r:   r;   rj   \  s    z4TensorVariable.method_attr_shape.<locals>.<listcomp>rC   )
valid_sizerC   r%   r   )rN   rv   sizesr:   r:   r;   method_attr_shapeZ  s   z TensorVariable.method_attr_shapec                 C   r   r_   )rE   r#   r   r   r:   r:   r;   method_attr_requires_grada  r   z(TensorVariable.method_attr_requires_gradc                 C   r   r_   )rF   r#   r   r   r:   r:   r;   method_attr_is_quantizede  r   z'TensorVariable.method_attr_is_quantizedc                 C   r   r_   )rI   r#   r   r   r:   r:   r;   method_attr_is_sparsei  r   z$TensorVariable.method_attr_is_sparsec                 C   r   r_   )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 r_   )r   TorchInGraphFunctionVariabler0   rs   Z	_autogradZ_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)rT   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)Ztensor_version_opr   r   r   r   )rN   rv   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   )rN   rv   r   r}   r   varZ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 againrX   Z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!   rw   r|   )rv   r>   r~   rv   r>   )r   rx   r   r}   inspectgetattr_staticr0   r5   r   rp   typesGetSetDescriptorTypeZcreate_getattr_proxyra   r~   r   )rx   r}   Zstatic_attrr>   r   rN   rv   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   rc   r6   r~   r   Zis_python_constantr   r   r   Z
TYPE_MATCHr   r3   r0   opsZatenr   TagZinplace_viewtagsr   r   ZDelayGraphBreakVariabler   r   )rN   rv   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 TensorVariablery   z%error getting associated real value: z%call_id without associated real value)r~   r   r   r   r   r   r   rq   r   r   r   ZID_MATCHidr#   r   )rN   rv   r   r   r   Z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 )Z
target_clsrv   r>   )rp   ra   )rh   irN   rv   r   r:   r;   rj   %  s    z6TensorVariable.unpack_var_sequence.<locals>.<listcomp>)r   r   r   lenrC   r   r4   r%   itemsr#   r   SymNodeVariableevaluate_exprr   r9   range)rN   rv   ZidxesZsize_lenZsize_varlengthZ
dyn_lengthr:   r   r;   unpack_var_sequence  s,   


z"TensorVariable.unpack_var_sequencec                 C   s
   | j d uS r_   rO   rM   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   rM   r:   r:   r;   rC   -  s   zTensorVariable.sizec                 C   
   t jjjS r_   )r0   r1   r   Z)_autograd_backward_strict_mode_banned_opsrM   r:   r:   r;   r   2  r   z&TensorVariable._strict_mode_banned_opsc                 C   r   r_   )r0   r1   r   Z5_autograd_backward_strict_mode_conditional_banned_opsrM   r:   r:   r;   r   5  s   z2TensorVariable._strict_mode_conditional_banned_opsargslist[VariableTracker]rV   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 modeFTrX   __eq__r   Zmethod_zunhandled args for z: rw   r   )r   r   r   torch_functionr   r   r   r   r   r   r   r0   r5   r   rr   listr~   r   r   r6   r4   r   r   r#   	TypeErrorrx   r   create_proxyr   )rN   rv   r   r   rV   r   r   r   r   Zis_base_tensor_methodZfunc_varZhandler_methodr   erx   r:   r:   r;   r   :  sh   
 zTensorVariable.call_methodc                 O      | j dg|R i |S )NrC   _method_size_striderN   r   rV   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   )rh   yoptionsr:   r;   rj         zXTensorVariable._method_size_stride.<locals>.make_const_size_variable.<locals>.<listcomp>r$   )ro   r   r:   r   r;   make_const_size_variable  s
   zDTensorVariable._method_size_stride.<locals>.make_const_size_variablerC   rZ   c                 s       | ]}t |V  qd S r_   rf   rh   rr:   r:   r;   	<genexpr>      z5TensorVariable._method_size_stride.<locals>.<genexpr>)r   r#   r   r6   r   rC   r>   rL   r\   getr   rr   rf   )rN   r   r   r   Z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 rY   )r   r#   r   r   rC   r>   rL   r\   r   Znumelr   rf   )rN   r   r   r:   r:   r;   method_numel  s   zTensorVariable.method_numelc                 C   r   r_   )rB   r#   r   rM   r:   r:   r;   
method_dim  r   zTensorVariable.method_dimc                 C      | j d urt| j jS d S r_   )r?   r#   r   Zis_floating_pointrM   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 rY   )r>   rL   r\   r   r#   r   Zis_inference)rN   r   r:   r:   r;   method_is_inference  s   z"TensorVariable.method_is_inferencec                 C   r   r_   )r?   r#   r   Z
is_complexrM   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 )NrZ   rl   )
as_python_constantr0   Zcontiguous_formatrG   r#   r   r>   rL   r\   r   )rN   rm   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 r_   )r?   rh   kvrM   r:   r;   r     s    z-TensorVariable.method_type.<locals>.<genexpr>cpuztorch..ztorch.tensortyper   r&   r!   rw   non_blockingr   rp   )r?   r4   r@   r0   nextr    r   rp   r#   r   r7   r   r  symbolic_convertr'   r   rx   
current_txr   r   r   )
rN   r?   r
  rV   Z
tensortypeZtensor_typeZtensor_type_constr'   rx   rv   r:   rM   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~   r  r'   r   r  r  r  r"   r   r  r  r   from_tensor_var)rN   clsr'   r  rv   Z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 )Nr  )r4   r@   r0   rp   indexr#   r   )rN   r  r:   r:   r;   method_get_device  s   
z TensorVariable.method_get_devicec                 C   s   t | jjS r_   )r#   r   r?   itemsizerM   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   r  Zview_as)r   Ztrace_numpyr   nprA   r0   Zstridedr   r  r'   r  r  r   r   r   ra   r   NumpyNdarrayVariabler   )rN   r  r'   rv   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!   rw   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 )NZallow_scalar_outputsT)unittestZmockpatchobjectZ	fake_modeitem)r   	sub_proxy)rv   rx   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:   )rh   r   valr'  r(  r:   r;   rj   [  r   z@TensorVariable.method_tolist.<locals>.tolist.<locals>.<listcomp>c                    s    g | ]\}}| | d qS ))r'  r:   )rh   r   Z
sub_tensor)r'  tolistr:   r;   rj   ]  s    )	r?   r0   int8Zint16Zint32Zint64r   r   	enumerate)tensorr'  r+  rv   rx   r*  r;   r+  C  s   
z,TensorVariable.method_tolist.<locals>.tolistrZ   )
r  r'   r   rx   r  ra   rL   r\   r"   r   )rN   r'   r.  outr:   r/  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 r_   )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   Z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!   rw   r   r   )r  r'   r   rx   r  r4   r   r0   selectr   r#   r   operatorgetitemr   r   r   r   )rN   r   rV   r'   rx   rv   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   Z_guardsZTracingContextextract_stackjoin	tracebackformat_listlogwarningtextwrapdedent)Z
user_stackZuser_stack_formattedr:   r:   r;   r5    s   z+TensorVariable._warn_capture_scalar_outputsc                 C   s,   ddl m} | }| |dtdgi S )Nr   r&   rC   r   )r  r'   r  r   r#   r   )rN   r'   rv   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)	r  r'   r  r   rD  Zinline_user_function_returnr"   r   Zaddcmul_inplace)rN   tensor1tensor2r9   r'   rv   rD  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 r_   r:   )rh   r&  has_bool_keyr:   r;   r     r   zJTensorVariable.method___setitem__.<locals>.has_bool_key.<locals>.<genexpr>F)
r4   r=   r?   r0   boolr,  r   ZTupleVariableanyr   )r  rH  r:   r;   rI    s
   
z7TensorVariable.method___setitem__.<locals>.has_bool_keyr   r&   r   )
r  r'   r  r   r   r8  setitemr   r#   r   )rN   keyr9   r'   rv   r:   rH  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&   Zadd_)	r  r'   r  r   r   r0   mulr   r   )rN   otherrT  r'   rv   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 rU  )
r  r'   r  r   r   r0   divr   rV  r   )rN   rE  rF  r9   r'   rv   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&  )
r  r'   r  r   r   r0   eqr   rK  r   )rN   argr'   rv   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  rn   r:   r:   r;   rj         z6TensorVariable.method_redistribute.<locals>.<listcomp>c                 S      i | ]	\}}||  qS r:   r`  r  r:   r:   r;   
<dictcomp>      z6TensorVariable.method_redistribute.<locals>.<dictcomp>c                       | j  i S r_   )Zredistributero   Zargs_as_valueZkwargs_as_valuer:   r;   redistribute_fn_with_prim_types     zKTensorVariable.method_redistribute.<locals>.redistribute_fn_with_prim_typesZprim_redistributer!   rw   r   r   
r  r'   r  r   r7   r   rx   r   r   r   )rN   r   rV   r'   rv   rh  rx   r:   rg  r;   method_redistribute      
z"TensorVariable.method_redistributec                    r^  )Nr   r&   c                 S   r_  r:   r`  rn   r:   r:   r;   rj   %  ra  z2TensorVariable.method_to_local.<locals>.<listcomp>c                 S   rb  r:   r`  r  r:   r:   r;   rc  &  rd  z2TensorVariable.method_to_local.<locals>.<dictcomp>c                    re  r_   )Zto_localrf  rg  r:   r;   to_local_fn_with_prim_types(  ri  zCTensorVariable.method_to_local.<locals>.to_local_fn_with_prim_typesZprim_to_localr!   rw   r   r   rj  )rN   r   rV   r'   rv   rm  rx   r:   rg  r;   method_to_local  rl  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   )NZ"register_post_accumulate_grad_hookrp  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   )r.  ru  ro  rv  r   r:   r;   _register_hook_trampoline_  s   
zGTensorVariable._method_register_hook.<locals>._register_hook_trampoliner!   rw   TZhas_backward_hookr   )Zmutation_type)r  r'   r  r~   r   Zcompiled_autograd_enabledr   r   Zadd_backward_state_hookr   rx   ra   rL   r\   r   r   ZRemovableHandleVariablebaseZValueMutationNewZside_effectsro  )
rN   r   rt  r'   rv   Zbw_state_proxyrz  rx   Z
self_proxyZhandle_variabler:   ry  r;   rq  A  s4   
z$TensorVariable._method_register_hookTc                 C   s6   |dur|  }|  jjd j|krtd d S | S )NTrZ   zTensor.requires_grad_)r  ra   rL   r\   rE   r   )rN   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 r_   )r4   r#   rc   rf   )rh   ar:   r:   r;   r     s    
z,TensorVariable.method_new.<locals>.<genexpr>r   r&   Z	new_empty)r   r4   r%   allr  r'   r   r  )rN   r   rV   r'   r:   r:   r;   
method_new  s   zTensorVariable.method_newc                 C   s   t | |  jjd  S rY   )UntypedStorageVariablera   rL   r\   untyped_storagerM   r:   r:   r;   method_untyped_storage  s   z%TensorVariable.method_untyped_storagec                 C   s"   | j s| jj| d| _ d S d S )NT)rK   r>   rL   Z_rename)rN   r   r:   r:   r;   set_name_hint  s   
zTensorVariable.set_name_hintrv   r'   r_   r   r   rV   r   rP   r"   )NFT)Yr7   
__module____qualname____doc__r"   _nonvar_fieldsr   r0   fxProxyrS   r]   ra   rc   staticmethodr5   ru   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   Zmethod_nelementr   Zmethod_ndimensionr   r  r  r  r  r  r  r  r"  r1  r2  r4  r6  r:  rw  	lru_cacher5  rC  rG  rN  rO  rP  rQ  rR  rS  rX  rZ  r]  rk  rn  rr  rs  strrq  r|  r  r  r  __classcell__r:   r:   rW   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 r_   )r[   r  rM   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 rY   )r   rL   r\   r   r4   sympyIntegerrf   rJ  r#   r   r   )r  rv   r>   r  r   r:   r:   r;   r     s   
zSymNodeVariable.createrP   c                    s(   t  jdi | || _|| _d | _d S Nr:   )rR   rS   r>   r  _tensor_var)rN   r>   r  rV   rW   r:   r;   rS     s   
zSymNodeVariable.__init__c                 C   s    t | jtr| jjjS t| jS r_   )r4   r  r	   rL   Zpytyperp   rM   r:   r:   r;   rc     s   

zSymNodeVariable.python_typec                 C   r^   r_   r`   rM   r:   r:   r;   ra     rb   zSymNodeVariable.as_proxyc              	   C   s8   | j d u rt|tj|| gdt||i| _ | j S )Nr?   )r  r"   r   r0   Zscalar_tensorr   )rN   rv   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*(). Zconstrain_as_size_example)Z	case_name)r   r  r   r0   r  Zexperimental_configZno_data_dependent_graph_breakr   r   ZANTI_PATTERNr  )rN   Zoutput_graphr   r:   r:   r;   r     s   zSymNodeVariable.evaluate_exprr   r   rV   r   r"   c                 C   s4   ddl m} |||jjd|gt| g||R  S )Nr!   rw   r   )r   rx   r   r   r   )rN   rv   r   r   rV   rx   r:   r:   r;   r     s   zSymNodeVariable.call_methodr_   rP   Nr  )r7   r  r  r  r"   r  r]   classmethodr   rS   rc   ra   r  r   r   r  r:   r:   rW   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 )r   z}
    Represents a np.ndarray, but backed by torch Tensor via torch._numpy.ndarray.
    Use this for Tensor.numpy() call.
    rv   r'   c                 K   s"   ddl m} |dt| |d|S )Nr!   r   r   r:   )r   r   r   )rv   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!   rw   rZ   c                      s    j d  fi S )Nr   )r   r   ra   r:   r   r  rN   rv   rx   r:   r;   insert_into_graph  s   z;NumpyNdarrayVariable.var_getattr.<locals>.insert_into_graph)Trealimagr   )rB   r  )shaperD   c                 s   r   r_   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   rx   ra   rL   r\   tnpndarrayr   r   r   r   r#   r6   r   rr   rC   rf   r   r   )	rN   rv   r   r   rZ   Z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 )NZclipminmax)Za_minZa_maxc                    s   i | ]\}}  |||qS r:   )r   r  Zkwargs_renamer:   r;   rc  M  r   z3NumpyNdarrayVariable.patch_args.<locals>.<dictcomp>)r   )r   r   rV   r:   r  r;   
patch_argsI  s   
zNumpyNdarrayVariable.patch_argsr   r   rV   r   rP   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   r+  )Ztostringtobytesz  is not modelled in torch._numpyr   )r2   r  r  rR   r   r   r   r   r   r   r   r   )rN   rv   r   r   rV   r  r>   rW   r:   r;   r   P  s   z NumpyNdarrayVariable.call_methodc                 C   s   t jS r_   )r  r  rM   r:   r:   r;   rc   g  rb   z NumpyNdarrayVariable.python_typer  r  )r7   r  r  r  r  r   r   r  r   rc   r  r:   r:   rW   r;   r     s    

=
r   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>   rP   c                   s$   t  j|fi | || _|| _d S r_   )rR   rS   r  r  )rN   r>   r  r  rV   rW   r:   r;   rS   v  s   
z$UnspecializedPythonVariable.__init__c                 C   s   t di t|j||dS )Nr  r:   )r  dict__dict__)r  tensor_variabler  r  r:   r:   r;   from_tensor_variable}  s   
z0UnspecializedPythonVariable.from_tensor_variabler  r7   r  r  r  r=   r  r0   r  r  rS   r  r  r  r:   r:   rW   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>   rP   Nc                    s*   | dd}t j|fi | || _d S )Nr  F)poprR   rS   r  )rN   r>   rV   r  rW   r:   r;   rS     s   
zFakeItemVariable.__init__c                 C   s   t di t|jS r  )r  r  r  )r  r  r:   r:   r;   r    s   z%FakeItemVariable.from_tensor_variabler  r:   r:   rW   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 )r  rP   Nc                    s   || _ t j|i | d S r_   )r9   rR   rS   )rN   r9   r   rV   rW   r:   r;   rS     s   zTensorSubclassVariable.__init__rv   r'   r   rV   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  r  rR   r   )rN   rv   r   rV   r  r~   r  rW   r:   r;   r     s   z$TensorSubclassVariable.call_functionc                 C   r^   r_   r8   rM   r:   r:   r;   r    rb   z)TensorSubclassVariable.as_python_constantr  )r7   r  r  rS   r   r"   r  r  r   r  r  r:   r:   rW   r;   r    s    
r  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  rZ   from_tensorrP   Nc                    s"   t  jdi | || _|| _d S r  )rR   rS   r  rZ   )rN   r  rZ   rV   rW   r:   r;   rS     s   
zUntypedStorageVariable.__init__r   rV   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!   rw   r   Zresize_r   )rZ   rC   r   r#   r   rf   external_utilsr  r   rx   r   r   r  ra   r   r0   r   ZinductorZresize_storage_bytes_rR   r   )rN   rv   r   r   rV   r   r  rx   rW   r:   r;   r     s6   

	z"UntypedStorageVariable.call_methodc                 C   "   || j  |d |d d S )Nr  r   r  Zload_methodr   rN   Zcodegenr:   r:   r;   reconstruct     

z"UntypedStorageVariable.reconstruct)r7   r  r  r"   r  r=   r0   ZUntypedStoragerS   r   r  r  r   r  r  r:   r:   rW   r;   r    s(    
'r  c                       s.   e Zd Zdeddf fddZdd Z  ZS )r3  r  rP   Nc                    s   t  jdi | || _d S r  )rR   rS   r  )rN   r  rV   rW   r:   r;   rS     s   
zDataPtrVariable.__init__c                 C   r  )NZdata_ptrr   r  r  r:   r:   r;   r    r  zDataPtrVariable.reconstruct)r7   r  r  r=   rS   r  r  r:   r:   rW   r;   r3    s    r3  )ar  rw  r   loggingr8  rA  r=  r   r#  typingr   r  Ztorch._numpyZ_numpyr  Ztorch.fxr0   Ztorch.randomZtorch._dynamor   Ztorch._subclasses.meta_utilsr   Z%torch.fx.experimental.symbolic_shapesr   r   r   r   r	   Ztorch.utils._python_dispatchr
   r   r   r   Z_trace_wrapped_higher_order_opr   r   r   r   r   r   r  r   Zguardsr   r   r~   r   r2   r   r   r   r   r   r   r   r   r   r    r{  r"   Zconstantr#   listsr%   numpyr  ModuleNotFoundErrorZtorch._dynamo.symbolic_convertr'   	getLoggerr7   r?  gtltgeler[  neis_is_notZsupported_tensor_comparison_opsZsupported_const_comparison_opsZsupported_comparison_opsr  fromkeysvaluesZ%supported_tensor_comparison_op_valuesZ$supported_const_comparison_op_valuesr<   r=   r   r   r  r  r  r  r3  r:   r:   r:   r;   <module>   s   0

        0Up>