U
    zhr"                  
   @   s   d dl Z d dlmZmZ d dlZd dlmZ ddlmZ ddl	m
Z
mZmZ ddlmZmZ ddlmZmZmZ d	d
lmZmZ ededejdejdejdiZG dd deZG dd deZdS )    N)DictList)GetItemSource   )	variables)unimplemented	UserErrorUserErrorType)GuardBuilderinstall_guard)common_constant_typesistypenp   )typestrVariableTrackerz!List types must use ListVariable.z&Dict types must use ConstDictVariable.z%Tensor types must use TensorVariable.zvSymInts must use SymNodeVariable. If the underlying value is static, we will create a ConstantVariable and specialize.z SymInts must use SymNodeVariablec                       s   e Zd ZeedddZ fddZdd Zdd	 Zd
d Z	dd Z
dd Zedd ZedddZedd Zdd Zdd Zdddd fdd Zedd!d"d#Z  ZS )$ConstantVariable)returnc           
      K   s   | dd }t| }|s>t D ]\}}t| |r"t|q"|rt| ttt	t
frg }t| D ]B\}}|rvt||nd }	|	rt|	tj |tj||	d q`t| ttfrtjt| |f|S t| t	t
fstt| t|S t| f|S )Nsource)r   )getr   
is_literal_type_to_assert_reasonitems
isinstanceAssertionErrorlisttupleset	frozenset	enumerater   r   Z
make_guardr
   ZCONSTANT_MATCHappendcreater   ZBaseListVariableZcls_fortypeZSetVariable)
valuekwargsr   r   disallowed_typereasonr   ixZitem_source r)   R/var/www/html/venv/lib/python3.8/site-packages/torch/_dynamo/variables/constant.pyr!   !   s,    

zConstantVariable.createc                    s|   t  jf | t|s<t D ]\}}t||r t|q t|tt	frRtdt
d k	rrt|t
jrr| | _n|| _d S )NzFConstantVariable(list) is banned - please create a ListVariable(items))super__init__r   r   r   r   r   r   r   r   r   numberitemr#   )selfr#   r$   r%   r&   	__class__r)   r*   r,   >   s    
 zConstantVariable.__init__c                 C   s   | j S Nr#   r/   r)   r)   r*   as_proxyL   s    zConstantVariable.as_proxyc                 C   s    dt | jj dt| j dS )NzConstantVariable(z: ))r"   r#   __name__reprr4   r)   r)   r*   __str__O   s    zConstantVariable.__str__c                 C   s
   t | jS r2   r"   r#   r4   r)   r)   r*   python_typeR   s    zConstantVariable.python_typec                 C   s   | j S r2   r3   r4   r)   r)   r*   as_python_constantU   s    z#ConstantVariable.as_python_constantc                 C   s   dS )NTr)   r4   r)   r)   r*   is_python_constantX   s    z#ConstantVariable.is_python_constantc                 C   s   | j ddS )zz
        Need this when adding a BaseListVariable and a ConstantVariable together.
        Happens in detectron2.
        N)tx)unpack_var_sequencer4   r)   r)   r*   r   [   s    zConstantVariable.items)argc                 C   s   t | j|  S r2   )r   r!   r#   r<   )r/   r@   r)   r)   r*   getitem_constc   s    zConstantVariable.getitem_constc                 C   s>   t | tkrdS t | tttttjfkr:tdd | D S dS )NTc                 s   s   | ]}t |V  qd S r2   )r   r   .0r(   r)   r)   r*   	<genexpr>n   s     z.ConstantVariable.is_literal.<locals>.<genexpr>F)	r"   r   r   r   r   r   torchSizeall)objr)   r)   r*   r   h   s
    zConstantVariable.is_literalc              
   C   sB   zdd |   D W S  tk
r< } z
t|W 5 d }~X Y nX d S )Nc                 S   s   g | ]}t |qS r)   )r   r!   rB   r)   r)   r*   
<listcomp>s   s     z8ConstantVariable.unpack_var_sequence.<locals>.<listcomp>)r<   	TypeErrorNotImplementedError)r/   r>   er)   r)   r*   r?   q   s    z$ConstantVariable.unpack_var_sequencec                 C   s8   t | jtrttjdddt| j|}t|r4t|S )Nz]Can't access members of type(obj) for a generated custom object. Please use __class__ insteadZtype_reflection_method)Z	case_name)	r   r#   r"   r   r	   ZANTI_PATTERNgetattrcallablerK   r/   r>   namememberr)   r)   r*   const_getattrw   s    zConstantVariable.const_getattrzList[VariableTracker]zDict[str, VariableTracker]r   )argsr$   r   c              	      s  ddl m  dkr<tjtr<ttj|f|S t	 fddD rpt
 j||S z$dd D }dd	 | D }W n& tk
r   t || Y S X fd
d}tjtrtj krtj}t|||S |ts|trtt}	|d }
t|
tjtjfrrddl m  |jd|	j|
fi } |||
S t|	j|
S dkrs|sttjS dkrtdkrd  r|rtd   }|jk}t|S t!dt"j d  d S )Nr   SymNodeVariableformatc                 3   s   | ]}t | V  qd S r2   )r   rB   rT   r)   r*   rD      s     z/ConstantVariable.call_method.<locals>.<genexpr>c                 S   s   g | ]}|  qS r)   r<   )rC   ar)   r)   r*   rI      s     z0ConstantVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]\}}||  qS r)   rW   )rC   kvr)   r)   r*   
<dictcomp>   s      z0ConstantVariable.call_method.<locals>.<dictcomp>c                    s.   t j| o,tto,t dko, d  S )Nr   r   )r   r#   hasattroperatorlenr=   )Znum_ty)rS   rP   r/   r)   r*   has_arith_binop   s    

z5ConstantVariable.call_method.<locals>.has_arith_binopr   call_function__len____contains__zconst method call .)#ZtensorrU   r   r#   strr   ZBuiltinVariablerV   r`   anyr5   call_methodr   rK   r+   r   __dict__keysrM   r   r!   intfloatr]   rE   SymIntSymFloatoutputZcreate_proxyr^   r=   r   r<   r   r   )r/   r>   rP   rS   r$   Z
const_argsZconst_kwargsr_   methodopZ
add_targetproxysearchresultr0   )rU   rS   rP   r/   r*   rf      sX         
   &


zConstantVariable.call_method)rP   r   c                 C   s   t | j|}tj|S r2   )r\   r#   r   r   r!   )r/   r>   rP   rr   r)   r)   r*   call_hasattr   s    zConstantVariable.call_hasattr)r7   
__module____qualname__staticmethodr   r!   r,   r5   r9   r;   r<   r=   propertyr   rA   r   r?   rR   rf   rd   rs   __classcell__r)   r)   r0   r*   r       s(   

>r   c                       sP   e Zd Z fddZedd Zdd Zdd Zd	d
 Zdd Z	dd Z
  ZS )EnumVariablec                    s   t  jf | || _d S r2   )r+   r,   r#   )r/   r#   r$   r0   r)   r*   r,      s    zEnumVariable.__init__c                 C   sD   t |tjr8t|D ]"}|j| kr| |f|  S qtd d S )Nz5Enum variable is constructed with non constant values)r   r   r   r   r#   r<   r   )clsZcls_typeZvalue_vtoptionsrQ   r)   r)   r*   r!      s
    zEnumVariable.createc                 C   s   | j S r2   r3   r4   r)   r)   r*   r5      s    zEnumVariable.as_proxyc                 C   s   dt | j dS )NzEnumVariable(r6   r:   r4   r)   r)   r*   r9      s    zEnumVariable.__str__c                 C   s
   t | jS r2   r:   r4   r)   r)   r*   r;      s    zEnumVariable.python_typec                 C   s   | j S r2   r3   r4   r)   r)   r*   r<      s    zEnumVariable.as_python_constantc                 C   s   t | j|}t|rt|S r2   )rM   r#   rN   rK   rO   r)   r)   r*   rR      s    zEnumVariable.const_getattr)r7   rt   ru   r,   classmethodr!   r5   r9   r;   r<   rR   rx   r)   r)   r0   r*   ry      s   
ry   )r]   typingr   r   rE   Ztorch._dynamo.sourcer    r   excr   r   r	   Zguardsr
   r   utilsr   r   r   baser   r   r   dictZTensorrk   rl   r   r   ry   r)   r)   r)   r*   <module>   s,   
      (