U
    zh                     @   sr  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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# ddl$m$Z$ ddl%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l0m1Z1m2Z2m3Z3m4Z4m5Z5m6Z6m7Z7m8Z8m9Z9m:Z: ddl;m<Z<m=Z= ddl>m?Z? ddl@mAZA zd dlBZCW n eDk
r   dZCY nX eEeFZGejHejIejJejKejLejMdZNejOejPejLejMdZQeNeQZReSTeNU ZVeSTeQU ZWG dd de=ZXG dd de=ZYG dd deXZZG dd deXZ[G dd  d eXZ\G d!d" d"e=Z]G d#d$ d$e=Z^dS )%    N)DictList)compiled_autograd)is_sparse_any)guard_scalarGuardOnDataDependentSymNodehas_free_symbolsis_symbolicSymTypes)is_traceable_wrapper_subclass   )config	variables)trace_wrapped)create_call_method)current_scope_id)unimplemented	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   )_is_top_level_scopeVariableTracker)ConstantVariableSizeVariable)><z>=z<===!=)iszis notr+   r,   c                       s  e Zd ZdZ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ejj	d fddZ
dd Zdd Zdd ZeejdddZd d! Zd"d# Zd$d% Zd&d' Zd(d) Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zdd>d?Z d@dA Z!dBdCdDdEdFdGZ"dHdI Z#dJdK Z$ddLdMZ%dNdO Z&e&Z'dPdQ Z(e(Z)dRdS Z*dTdU Z+ddVdWZ,ddYdZZ-d[d\ Z.d]d^ Z/d_d` Z0dXdadbdcZ1ddde Z2dfdg Z3dhdi Z4djdk Z5ee67ddldm Z8dndo Z9dpdq Z:drds Z;dtdu Z<dvdw Z=dxdy Z>dzd{ Z?dd|d}d~Z@ddddZAdd ZBdd ZCdd ZDdd ZEdd ZFeGedddZHdddZIdd ZJdd ZKeGdddZL  ZMS )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_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self r?   P/var/www/html/venv/lib/python3.8/site-packages/torch/_dynamo/variables/tensor.pyr   k   s    zTensorVariable.get_real_valueN)r4   r5   r8   r;   r/   c                   s|   t  jf | || _|| _|| _|| _|| _|| _|| _|| _	|| _
|| _|| _|	| _|
| _|d krr| jjjdk}|| _d S )Nplaceholder)super__init__r/   r0   r1   r2   r3   r4   r5   r6   r7   r8   r9   r:   has_grad_fnr<   opr;   )r>   r/   r0   r1   r2   r3   r6   r7   r9   r:   rE   r4   r5   r8   r;   kwargs	__class__r?   r@   rD   t   s"    zTensorVariable.__init__c                 C   s   t | jjjd S Nexample_value)reprr/   r<   metar=   r?   r?   r@   
debug_repr   s    zTensorVariable.debug_reprc                 C   s   | j S NrA   r=   r?   r?   r@   as_proxy   s    zTensorVariable.as_proxyc                 C   s   | j S rO   )r:   r=   r?   r?   r@   python_type   s    zTensorVariable.python_typevaluec              	      s    j  j jt j j j jt d}z j	d k	|d< W n t
k
rZ   d|d< Y nX t rt stdd   D |d< nht stdd   D |d< t  |d< tjj rd |d	< nt fd
dtjjD |d	< |S )N)r0   r1   r2   r3   r6   r7   r9   r:   rE   Fc                 S   s    g | ]}t |rt|n|qS r?   r	   int.0sr?   r?   r@   
<listcomp>   s     z-TensorVariable.specialize.<locals>.<listcomp>r4   c                 S   s    g | ]}t |rt|n|qS r?   rT   rV   r?   r?   r@   rY      s     r5   r8   c                    s   g | ]} j |d r|qS )memory_format)r8   rW   xrR   r?   r@   rY      s   )r0   r1   r2   rU   r3   r6   r7   r9   typeZgrad_fn	Exceptionr   r   tupler4   r5   torchZ_CZ
_functorchZis_batchedtensorZ_prims_commonZ_memory_formats)rS   propsr?   rR   r@   
specialize   s<    



zTensorVariable.specializec              
   C   s  | j jjd }| jst|r| j jjd }| \}}t|  |}t||}||krt|t	j
sftddlm} ||||dS t|sddlm}	 |	||S | jr| j st|jj|jjd}
zt| j |
}W n* tk
r } z
t|W 5 d }~X Y nX |d krtt|r"tt|r0tt||}t|rHtddlm} dd	lm} t| j|}t| |j! ||||S )
NrK   r#   wrap_fx_proxy)txr/   rK   SourcelessBuilder)LGr   )r   VariableBuilder)"r/   r<   rM   sourcer   Z__tensor_flatten__getattrrP   
isinstancera   TensorAssertionErrorbuilderre   callablerh   createsubguards_allowedNotImplementedErroroutputZlocal_scopeZglobal_scopeevalnamer_   r   r   guardsr   rl   r   r   
make_guardZHASATTR)r>   rf   ry   Zfake_valattrsctxr/   rK   re   rh   scopeZ_input_associated_real_valueexc
real_valuer   rl   Zattr_sourcer?   r?   r@   dynamic_getattr   sD    





zTensorVariable.dynamic_getattrc                 C   s*   | j d k	rt| j S | |dg i S d S )Ndim)r3   r&   rt   call_methodr>   rf   r?   r?   r@   method_attr_ndim  s    
zTensorVariable.method_attr_ndimc                 C   s   | j d k	rt| j S d S rO   )r0   r&   rt   r   r?   r?   r@   method_attr_dtype  s    
z TensorVariable.method_attr_dtypec                 C   s   | j d k	rt| j S d S rO   )r1   r&   rt   r   r?   r?   r@   method_attr_device#  s    
z!TensorVariable.method_attr_devicec                 C   s   | j d k	rt| j S d S rO   )r2   r&   rt   r   r?   r?   r@   method_attr_layout'  s    
z!TensorVariable.method_attr_layoutc                 C   s    | j d k	rt| j jdkS d S )Ncuda)r1   r&   rt   r^   r   r?   r?   r@   method_attr_is_cuda+  s    
z"TensorVariable.method_attr_is_cudac                 C   s6   | j d k	r"dd | j D }t|S | |dg i S d S )Nc                 S   s   g | ]}t j|qS r?   )r   r&   rt   r\   r?   r?   r@   rY   1  s     z4TensorVariable.method_attr_shape.<locals>.<listcomp>r4   )r4   r(   r   )r>   rf   sizesr?   r?   r@   method_attr_shape/  s    
z TensorVariable.method_attr_shapec                 C   s   | j d k	rt| j S d S rO   )r6   r&   rt   r   r?   r?   r@   method_attr_requires_grad6  s    
z(TensorVariable.method_attr_requires_gradc                 C   s   | j d k	rt| j S d S rO   )r7   r&   rt   r   r?   r?   r@   method_attr_is_quantized:  s    
z'TensorVariable.method_attr_is_quantizedc                 C   s   | j d k	rt| j S d S rO   )r9   r&   rt   r   r?   r?   r@   method_attr_is_sparse>  s    
z$TensorVariable.method_attr_is_sparsec                 C   s   |  |dg i S )Ndetach)r   r   r?   r?   r@   method_attr_dataB  s    zTensorVariable.method_attr_datac                 C   s   | j rtd n
td S d S )NzTensorVariable has a grad_fn)rE   r   r   r&   r   r?   r?   r@   method_attr_grad_fnE  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   TorchInGraphFunctionVariablecall_function)r>   rf   r   r?   r?   r@   method_attr__versionK  s    
  z#TensorVariable.method_attr__versionc                    s  ddl m} r2  kr2td  d  dkrF| S td  d }|d k	rh|nd }|d k	rjrj r dkr|	 st
tj tj |_jd k	r0ttjj r0ttjj }t|dr0t|| d	 r0tjjt|| d	 jkr0tjjtj d
S |d krZ dkrZ fdd}| }|d krp }|d kr~t|S )Nr#   )UserDefinedClassVariablezIllegal getattr invocation  in strict moderI   Zmethod_attr_)gradr6   	overloadsr   )rm   r   c                     s   ddl m}  ddlm} zttj }W n tk
r@   Y d S X t	|t
jkrTd S |  }jd k	r| |tj dS | |dS d S )Nr#   rd   )GetAttrVariable)rf   r/   rm   rf   r/   )rr   re   miscr   inspectgetattr_staticra   rp   AttributeErrorr^   typesGetSetDescriptorTypeZcreate_getattr_proxyrP   rm   r   )re   r   Zstatic_attrr/   ry   r>   rf   r?   r@   try_generic_attr_handling  s     
  
z=TensorVariable.var_getattr.<locals>.try_generic_attr_handling) r   is_strict_mode_strict_mode_banned_opsr   rQ   rn   rm   ru   Zis_python_constantr   r{   r   Z
TYPE_MATCHr   hasattrra   opsZatenr   TagZinplace_viewtagsr   r   ZDelayGraphBreakVariabler   rv   )r>   rf   ry   r   handlerresultfnr   r?   r   r@   var_getattrR  sL    


zTensorVariable.var_getattrc                 C   s
   | j dkS )Nr   )r3   r   r?   r?   r@   has_unpack_var_sequence  s    z&TensorVariable.has_unpack_var_sequencec                    s   ddl m |d krv jr& jd }nH dtdgi }t|ttfsPtt|trh|	j
}n|j}t|} fdd|D S )Nr#   wrap_fx_proxy_clsr   r4   c                    s&   g | ]}t    | d qS )Z
target_clsrf   r/   )r^   rP   )rW   ir>   rf   r   r?   r@   rY     s   z6TensorVariable.unpack_var_sequence.<locals>.<listcomp>)rr   r   r4   r   r&   rt   ro   SymNodeVariablerq   evaluate_exprrw   rS   range)r>   rf   ZidxeslengthZ
dyn_lengthr?   r   r@   unpack_var_sequence  s$      
 
z"TensorVariable.unpack_var_sequencec                 C   s
   t jjjS rO   )ra   Z_dynamor   Z)_autograd_backward_strict_mode_banned_opsr=   r?   r?   r@   r     s    z&TensorVariable._strict_mode_banned_opsList[VariableTracker]Dict[str, VariableTracker]r%   argsrG   returnc           	   
   C   s   |  |r&||  kr&td| d zt| d| }W n tk
rN   Y nRX z|||}|rf|W S W n6 tk
r } ztd| d|  W 5 d }~X Y nX ddlm} |||jj	d|ft
| f|| S )	NzIllegal method invocation r   Zmethod_zunhandled args for z: r#   rd   r   )r   r   r   rn   r   	TypeErrorrr   re   rw   create_proxyr    )	r>   rf   ry   r   rG   Zhandler_methodr   ere   r?   r?   r@   r     s,    

&zTensorVariable.call_methodc                 O   s   | j d||S )Nr4   )r4   _method_size_strider>   r   rG   r?   r?   r@   method_size  s    zTensorVariable.method_sizec                 O   s   | j d||S )Nr5   )r5   r   r   r?   r?   r@   method_stride  s    zTensorVariable.method_stridec                 C   s   t |}dd }|dkr|ntj}t| | }d k	rR|d krD||S t|| S | jjjd }d k	r|d krt|| }t|s|t	dd |D S n$t|||}t|stt
|S d S )Nc                    s   t  fdd| D f S )Nc                    s   g | ]}t j|f qS r?   )r&   rt   )rW   yoptionsr?   r@   rY     s     zXTensorVariable._method_size_stride.<locals>.make_const_size_variable.<locals>.<listcomp>r'   )r]   r   r?   r   r@   make_const_size_variable  s
    zDTensorVariable._method_size_stride.<locals>.make_const_size_variabler4   rK   c                 s   s   | ]}t |V  qd S rO   rU   rW   rr?   r?   r@   	<genexpr>  s     z5TensorVariable._method_size_stride.<locals>.<genexpr>)r   r&   rt   rn   r/   r<   rM   getr   r`   rU   )r>   ry   r   r   ZRetVariabler   fakefake_rr?   r?   r@   r     s     z"TensorVariable._method_size_stridec                 C   sT   | j d k	rtt| j S | jjjd }d k	rP| }t	|sPtt
|S d S rJ   )r4   r&   rt   r   r/   r<   rM   r   Znumelr   rU   )r>   r   r   r?   r?   r@   method_numel  s    
zTensorVariable.method_numelc                 C   s   | j d k	rt| j S d S rO   )r3   r&   rt   r=   r?   r?   r@   
method_dim  s    
zTensorVariable.method_dimc                 C   s   | j d k	rt| j jS d S rO   )r0   r&   rt   Zis_floating_pointr=   r?   r?   r@   method_is_floating_point  s    
z'TensorVariable.method_is_floating_pointc                 C   s   | j d k	rt| j jS d S rO   )r0   r&   rt   Z
is_complexr=   r?   r?   r@   method_is_complex#  s    
z TensorVariable.method_is_complexc                 C   s^   |d k	r|  ntj}| jd k	r0t|| jkS | jjj	d }d k	rZt|j|dS d S )NrK   rZ   )
as_python_constantra   Zcontiguous_formatr8   r&   rt   r/   r<   rM   r   )r>   r[   r   r?   r?   r@   method_is_contiguous'  s    


z#TensorVariable.method_is_contiguousFc           
         s   |d krl j d k	rlt jtjrlt fddt D } jjdkrXt	d|j
 S t	d|j
 S n|d k	rtt| dkr| }t	t|}ddlm} d	d
lm} | }	|rd|i|}||	|	jjdt |g| S d S )Nc                 3   s    | ]\}} j |kr|V  qd S rO   )r0   rW   kvr=   r?   r@   r   :  s    
 z-TensorVariable.method_type.<locals>.<genexpr>r   ztorch.cuda.ztorch.ztorch.tensortyper   InstructionTranslatorr#   rd   non_blockingr   r^   )r   r^   )r0   ro   r1   ra   nextr"   itemsr^   r&   rt   __name__r   r   symbolic_convertr   rr   re   
current_txrw   r   r    )
r>   r0   r   rG   Z
tensortypeZtensor_typeZtensor_type_constr   re   rf   r?   r=   r@   method_type4  s@      zTensorVariable.method_typec                 C   sx   t |trt|jrtddlm} ddlm} ddlm} |	 }|
 }||tt|jdd|jj}||| ||S d S )Nr   r   r#   rk   TensorWithTFOverrideVariable__torch_function____func__)ro   TensorSubclassVariablerm   r   r   rr   rl   torch_functionr   r   r   r   r   r   from_tensor_var)r>   clsr   rl   r   rf   Zpy_clstorch_fnr?   r?   r@   method_as_subclass\  s$       z!TensorVariable.method_as_subclassc                 C   s4   t | jtjr0| jjdkr"| jjnd}t|S d S )Ncpu)ro   r1   ra   r^   indexr&   rt   )r>   r   r?   r?   r@   method_get_devicer  s    z TensorVariable.method_get_devicec                 C   s   t | jjS rO   )r&   rt   r0   itemsizer=   r?   r?   r@   method_element_sizew  s    z"TensorVariable.method_element_size)forcec                C   s   t jstd tstd | jtjkr8td| j dddlm	} |
 }|r| r| |dg i }|jdd	| fi }n|jjdt| | gi  }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   r   r   r   view_as)r   r   )r   Ztrace_numpyr   npr2   ra   Zstridedr   r   r   r   r   r   rw   r   rP   r    NumpyNdarrayVariablert   )r>   r   r   rf   tr/   r?   r?   r@   method_numpyz  s(       zTensorVariable.method_numpyc                    sX   ddl m} ddlm} |  fdd |  jjd } ||  }||S )Nr   r   r#   rg   c                    s   fdd| j tjtjtjtjfkr.td |  dkrD|  S |  dkrh fddt| D S  fddt| D S )	Nc              
      s<   t jj jdd t | W  5 Q R  S Q R X d S )NZallow_scalar_outputsT)unittestZmockpatchobjectZ	fake_moder   rt   item)r   	sub_proxy)rf   r?   r@   wrap  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?   )rW   r   valr   r   r?   r@   rY     s     z@TensorVariable.method_tolist.<locals>.tolist.<locals>.<listcomp>c                    s    g | ]\}}| | d qS ))r   r?   )rW   r   Z
sub_tensor)r   tolistr?   r@   rY     s   )	r0   ra   int8Zint16Zint32Zint64r   r   	enumerate)tensorr   r  rf   r  r@   r    s    
z,TensorVariable.method_tolist.<locals>.tolistrK   )	r   r   rr   rh   r   rP   r<   rM   rt   )r>   r   rh   r  outr?   r  r@   method_tolist  s    zTensorVariable.method_tolistc                 O   s   t d d S )NzTensor.backwardr   r   r?   r?   r@   method_backward  s    zTensorVariable.method_backwardc                 O   s   t d d S )NzTensor.data_ptrr
  r   r?   r?   r@   method_data_ptr  s    zTensorVariable.method_data_ptrc                 O   s   t js|   td d S )NzTensor.item)r   Zcapture_scalar_outputs_warn_capture_scalar_outputsr   r   r?   r?   r@   method_item  s    zTensorVariable.method_itemc                   C   s   t td d S )NaC                      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.
                )logwarningtextwrapdedentr?   r?   r?   r@   r    s
    z+TensorVariable._warn_capture_scalar_outputsc                 C   s,   ddl m} | }| |dtdgi S )Nr   r   r4   r   )r   r   r   r   r&   rt   )r>   r   rf   r?   r?   r@   method___len__  s    zTensorVariable.method___len__c                    st    fdd  |r4t |tr4|jr4t r4td ddlm} | }|j	j
dtjft| ||gi   td S )Nc                    sH   t | tr| jtjtjfkS t | tjr@t fdd| j	D S dS d S )Nc                 3   s   | ]} |V  qd S rO   r?   )rW   r   has_bool_keyr?   r@   r     s     zJTensorVariable.method___setitem__.<locals>.has_bool_key.<locals>.<genexpr>F)
ro   r.   r0   ra   boolr  r   ZTupleVariableanyr   )r   r  r?   r@   r    s
    
z7TensorVariable.method___setitem__.<locals>.has_bool_keyzWboolean masking setitem backwards, see https://github.com/pytorch/pytorch/issues/114123r   r   r   )ro   r.   r6   ra   Zis_grad_enabledr   r   r   r   rw   r   operatorsetitemr    r&   rt   )r>   keyrS   r   rf   r?   r  r@   method___setitem__  s*    	z!TensorVariable.method___setitem__c                 O   s   t d d S )NzTensor.resize_r
  r   r?   r?   r@   method_resize_  s    zTensorVariable.method_resize_c                 O   s   t d d S )NzTensor.resize_as_r
  r   r?   r?   r@   method_resize_as_  s    z TensorVariable.method_resize_as_c                 O   s   t d d S )NzTensor.sparse_resize_r
  r   r?   r?   r@   method_sparse_resize_  s    z$TensorVariable.method_sparse_resize_c                 O   s   t d d S )NzTensor.sparse_resize_and_clear_r
  r   r?   r?   r@   method_sparse_resize_and_clear_  s    z.TensorVariable.method_sparse_resize_and_clear_c                 O   s   t |dkrtd d S )Nr#   z(Tensor.set_.source_Tensor_storage_offset)lenr   r   r?   r?   r@   method_set_  s    zTensorVariable.method_set_)alphac                C   sL   |d k	rHddl m} | }ttj|||gi }| |d|gi S d S Nr   r   Zadd_)	r   r   r   r   r   ra   mulr   r   )r>   otherr"  r   rf   r   r?   r?   r@   method_add_  s      zTensorVariable.method_add_c                C   sf   ddl m} | }|d k	rbttj|||gi }ttj|||gi }| 	|d|gi S d S r#  )
r   r   r   r   r   ra   divr   r$  r   )r>   Ztensor1Ztensor2rS   r   rf   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   ra   eqr   r  r   )r>   argr   rf   r   r?   r?   r@   method___contains__&  s        z"TensorVariable.method___contains__c                    sx   ddl m} | }dd |D  dd | D  fdd}d	|_d
dlm} |||jjd|ft	| gi  dS )Nr   r   c                 S   s   g | ]}|  qS r?   r   r\   r?   r?   r@   rY   =  s     z6TensorVariable.method_redistribute.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r?   r,  r   r?   r?   r@   
<dictcomp>>  s      z6TensorVariable.method_redistribute.<locals>.<dictcomp>c                    s   | j  S rO   )Zredistributer]   Zargs_as_valueZkwargs_as_valuer?   r@   redistribute_fn_with_prim_types@  s    zKTensorVariable.method_redistribute.<locals>.redistribute_fn_with_prim_typesZprim_redistributer#   rd   r   r   
r   r   r   r   r   rr   re   rw   r   r    )r>   r   rG   r   rf   r0  re   r?   r/  r@   method_redistribute7  s     
z"TensorVariable.method_redistributec                    sx   ddl m} | }dd |D  dd | D  fdd}d	|_d
dlm} |||jjd|ft	| gi  dS )Nr   r   c                 S   s   g | ]}|  qS r?   r,  r\   r?   r?   r@   rY   W  s     z2TensorVariable.method_to_local.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r?   r,  r   r?   r?   r@   r-  X  s      z2TensorVariable.method_to_local.<locals>.<dictcomp>c                    s   | j  S rO   )Zto_localr.  r/  r?   r@   to_local_fn_with_prim_typesZ  s    zCTensorVariable.method_to_local.<locals>.to_local_fn_with_prim_typesZprim_to_localr#   rd   r   r   r1  )r>   r   rG   r   rf   r3  re   r?   r/  r@   method_to_localQ  s     
zTensorVariable.method_to_localc                 O   s   | j d||S )Nregister_hook)r5  _method_register_hookr   r?   r?   r@   method_register_hookk  s    z#TensorVariable.method_register_hookc                 O   s   | j d||S )N"register_post_accumulate_grad_hook)r9  r6  r   r?   r?   r@   )method_register_post_accumulate_grad_hookn  s     z8TensorVariable.method_register_post_accumulate_grad_hook)ry   hookc           	         s   ddl m} | }| jsrtjs(td |j|\ } fdd}ddl	m
} |||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)rn   	functoolspartialr   r   )r  r<  r5  r=  ry   r?   r@   _register_hook_trampoline  s    
zGTensorVariable._method_register_hook.<locals>._register_hook_trampoliner#   rd   r   )Zmutable_local)r   r   r   rm   r   Zcompiled_autograd_enabledr   rw   Zadd_backward_state_hookrr   re   r   rP   r   ZRemovableHandleVariablebaseZMutableLocalZside_effectsr5  )	r>   ry   r;  r   rf   Zbw_state_proxyrA  re   Zhandle_variabler?   r@  r@   r7  s  s0    

z$TensorVariable._method_register_hookTc                 C   s8   |dk	r|  }|  jjd j|kr0td n| S d S )NTrK   zTensor.requires_grad_)r   rP   r<   rM   r6   r   )r>   r6   r?   r?   r@   method_requires_grad_  s
    
z$TensorVariable.method_requires_grad_c                 O   s\   t |dkrt|d ts8t |dkrXtdd |D rXddlm} | | d||S d S )Nr#   r   c                 s   s$   | ]}t |to| tkV  qd S rO   )ro   r&   rQ   rU   )rW   ar?   r?   r@   r     s    z,TensorVariable.method_new.<locals>.<genexpr>r   r   Z	new_empty)r   ro   r(   allr   r   r   r   )r>   r   rG   r   r?   r?   r@   
method_new  s    
   zTensorVariable.method_newc                 C   s   t | |  jjd  S rJ   )UntypedStorageVariablerP   r<   rM   untyped_storager=   r?   r?   r@   method_untyped_storage  s     z%TensorVariable.method_untyped_storage)ry   c                 C   s(   | j s$tt r$| jj| d| _ d S )NT)r;   r$   r   r/   r<   Z_rename)r>   ry   r?   r?   r@   set_name_hint  s    zTensorVariable.set_name_hint)N)N)N)NF)T)Nr   
__module____qualname____doc__r%   _nonvar_fieldsr   ra   fxProxyrD   rN   rP   rQ   staticmethodrp   rc   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   Zmethod_nelementr   Zmethod_ndimensionr   r   r   r   r   r   r   r   r	  r  r  r  r>  	lru_cacher  r  r  r  r  r  r  r!  r&  r(  r+  r2  r4  r8  r:  strr7  rC  rF  rI  rJ  __classcell__r?   r?   rH   r@   r.   V   s   &1CP
&
"


()

?
	r.   c                       sx   e Zd ZdZddhejZdd ZedddZ fd	d
Z	dd Z
dd Zdd ZdddZddd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 rO   )rL   rU  r=   r?   r?   r@   rN     s    zSymNodeVariable.debug_reprNc                 K   s   |d krt |j|}d|jjkr4|jjd |ks4tt|j| t|tjtt	frtt|tjrft|n|}t
|S t||f|S rJ   )r   r<   rM   rq   r!   ro   sympyIntegerrU   r  r&   rt   r   )r   rf   r/   rU  r   r?   r?   r@   rt     s    
zSymNodeVariable.createc                    s$   t  jf | || _|| _d | _d S rO   )rC   rD   r/   rU  _tensor_var)r>   r/   rU  rG   rH   r?   r@   rD     s    zSymNodeVariable.__init__c                 C   s$   t | jtr| jjjS t| jS d S rO   )ro   rU  r
   r<   Zpytyper^   r=   r?   r?   r@   rQ     s    
zSymNodeVariable.python_typec                 C   s   | j S rO   rA   r=   r?   r?   r@   rP     s    zSymNodeVariable.as_proxyc                 C   s8   | j d kr2ddlm} ||tj|| gi | _ | j S )Nr#   rg   )rX  rr   rh   rt   ra   Zscalar_tensorr   )r>   rf   rh   r?   r?   r@   	as_tensor  s    
   zSymNodeVariable.as_tensorc              
   C   sN   zt | jW S  tk
rH } zttjdt| ddW 5 d }~X Y nX d S )Nz5Consider annotating your code using torch._check*(). Zconstrain_as_size_example)Z	case_name)r   rU  r   r   r   ZANTI_PATTERNrS  )r>   Zoutput_graphr   r?   r?   r@   r     s    zSymNodeVariable.evaluate_exprr   r   r%   r   c                 C   s2   ddl m} |||jjd|ft| f|| S )Nr#   rd   r   )rr   re   rw   r   r    )r>   rf   ry   r   rG   re   r?   r?   r@   r     s    zSymNodeVariable.call_method)N)N)r   rK  rL  rM  r%   rN  rN   classmethodrt   rD   rQ   rP   rY  r   r   rT  r?   r?   rH   r@   r     s$   	
r   c                       sR   e Zd ZdZedd Zdd Zedd Zdd	d
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.
    c                 K   s"   ddl m} |f t| |d|S )Nr#   r   r   )rr   r   r   )rf   r/   r   r   r?   r?   r@   rt   2  s    zNumpyNdarrayVariable.createc           	         s2  ddl m ddlm d } jjd }t|} fdd} dkrzj	
d	  fi }t|}n d
krtt| S  dkrtt|  }sttdd |D S | S  dkrt|j }stt|S | S  dkrtd   n dkr td |d kr.t|S )Nr   )numpy_attr_wrapperr#   rd   rK   c                      s    j d  fi S )Nr   )rw   r   rP   r?   ry   r[  r>   rf   re   r?   r@   insert_into_graphJ  s      
 z;NumpyNdarrayVariable.var_getattr.<locals>.insert_into_graph)Trealimagr   )r3   r   )shaper5   c                 s   s   | ]}t |V  qd S rO   r   r   r?   r?   r@   r   l  s     z3NumpyNdarrayVariable.var_getattr.<locals>.<genexpr>r4   )rB  flagsr0   zTODO: add support for ndarray.)__version__z delegate np.__version__ to NumPy)utilsr[  rr   re   rP   r<   rM   tnpndarrayrw   r   r   rt   r&   rn   r   r`   r4   rU   r   rv   )	r>   rf   ry   r   rK   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@   r-  ~  s     
 z3NumpyNdarrayVariable.patch_args.<locals>.<dictcomp>)r   )ry   r   rG   r?   ri  r@   
patch_argsz  s    
zNumpyNdarrayVariable.patch_argsr   r   r%   r   c                    s|   ddl m} | |||\}}|dkr8t ||||S |dkrHtd |jjd||ft| gt	| | }t
||S )Nr   )numpy_method_wrapper)__len__r4   r  tobytesz'tobytes is not modelled in torch._numpyr   )rd  rk  rj  rC   r   r   rw   r   r    listr   rt   )r>   rf   ry   r   rG   rk  r/   rH   r?   r@   r     s    z NumpyNdarrayVariable.call_methodc                 C   s   t jS rO   )r   rf  r=   r?   r?   r@   rQ     s    z NumpyNdarrayVariable.python_type)r   rK  rL  rM  rQ  rt   r   rj  r   rQ   rT  r?   r?   rH   r@   r   ,  s   

=

r   c                       sN   e Zd ZdZddhejZdddejjd 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rp  rq  rA   c                   s    t  j|f| || _|| _d S rO   )rC   rD   rp  rq  )r>   r/   rp  rq  rG   rH   r?   r@   rD     s    z$UnspecializedPythonVariable.__init__c                 C   s   t f t|j||dS )Nrr  )ro  dict__dict__)r   tensor_variablerp  rq  r?   r?   r@   from_tensor_variable  s
    z0UnspecializedPythonVariable.from_tensor_variable)Tr   rK  rL  rM  r.   rN  ra   rO  rP  rD   rZ  rv  rT  r?   r?   rH   r@   ro    s    ro  c                       sB   e Zd ZdZdhejZejjd 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.rq  rA   c                    s&   | dd}t j|f| || _d S )Nrq  F)poprC   rD   rq  )r>   r/   rG   rq  rH   r?   r@   rD     s    zFakeItemVariable.__init__c                 C   s   t f t|jS rO   )rx  rs  rt  )r   ru  r?   r?   r@   rv    s    z%FakeItemVariable.from_tensor_variablerw  r?   r?   rH   r@   rx    s   rx  c                       sN   e Zd Z fddZee eeef ed fddZdd Z	dd	 Z
  ZS )
r   c                    s   || _ t j|| d S rO   )rS   rC   rD   )r>   rS   r   rG   rH   r?   r@   rD     s    zTensorSubclassVariable.__init__r   c                    sr   t |dkrbt|d trbddlm} ddlm} ||t| jd| j	j
}|||d | j	|S t |||S )Nr#   r   rk   r   r   )r   ro   r.   rr   rl   r   r   r   rm   rS   r   r   rC   r   )r>   rf   r   rG   rl   r   r   rH   r?   r@   r     s      
   z$TensorSubclassVariable.call_functionc                 C   s   | j S rO   rR   r=   r?   r?   r@   r     s    z)TensorSubclassVariable.as_python_constantc                 C   s
   t | jS rO   )r^   rS   r=   r?   r?   r@   rQ     s    z"TensorSubclassVariable.python_type)r   rK  rL  rD   r   r%   r   rS  r   r   rQ   rT  r?   r?   rH   r@   r     s    
r   c                       s\   e Zd ZdhejZeejd fddZe	e e
eef ed fddZdd	 Z  ZS )
rG  rK   )from_tensorrK   c                    s    t  jf |f || _|| _d S rO   )rC   rD   rz  rK   )r>   rz  rK   rG   rH   r?   r@   rD     s    zUntypedStorageVariable.__init__r   c                    s   |dkrp|rt |rt | j }t|s8tt|S ddlm} ddl	m
} |||jd|| j fi S |dkrt|dkr|rt |jdtjjj| j |d  fi  | S t ||||S )	Nr4   r   )untyped_storage_sizer#   rd   r   Zresize_r   )rq   rK   r4   r   r&   rt   rU   external_utilsr{  rr   re   rw   r   rz  rP   r   ra   r   ZinductorZresize_storage_bytes_rC   r   )r>   rf   ry   r   rG   r   r{  re   rH   r?   r@   r     s6    

	z"UntypedStorageVariable.call_methodc                 C   s,   || j  ||d |td d S )NrH  r   )rz  Zappend_outputZcreate_load_methodZextend_outputr   )r>   Zcodegenr?   r?   r@   reconstruct   s    
z"UntypedStorageVariable.reconstruct)r   rK  rL  r%   rN  r.   ra   ZUntypedStoragerD   r   r   rS  r   r}  rT  r?   r?   rH   r@   rG    s   
'rG  )_r>  r   loggingr  r  r   r   typingr   r   rV  Ztorch._numpyZ_numpyre  Ztorch.fxra   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   Zbytecode_transformationr   r   r   r   r   r   r|  r   rz   r   r   rm   r   rd  r   r   r   r   r   r   r   r    r!   r"   rB  r$   r%   Zconstantr&   listsr(   numpyr   ModuleNotFoundError	getLoggerr   r  gtltgeler)  neZsupported_tensor_comparison_opsis_is_notZsupported_const_comparison_opsZsupported_comparison_opsrs  fromkeysvaluesZ%supported_tensor_comparison_op_valuesZ$supported_const_comparison_op_valuesr.   r   r   ro  rx  r   rG  r?   r?   r?   r@   <module>   s   0


       	Tp