o
    I&i                   
   @   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 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)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defddZ fddZdd Zdd	 Zd
d Z	dd Z
edd ZdefddZedd Zdd Zdd Z						d# fddZd eddfd!d"Z  ZS )$ConstantVariablereturnc           
      K   s   | dd }t| }|st D ]\}}t| |rJ |q|r^t| ttfr^g }t| D ]!\}}|r9t	||nd }	|	rEt
|	tj |tj||	d q.tjt| |fi |S t| fi |S )Nsource)r   )getr   
is_literal_type_to_assert_reasonitems
isinstancelisttuple	enumerater   r   Z
make_guardr
   ZCONSTANT_MATCHappendcreater   ZBaseListVariableZcls_fortype)
valuekwargsr   r   disallowed_typereasonr   ixZitem_source r$   KC:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/variables/constant.pyr      s&   
zConstantVariable.createc                    s   t  jdi | t|s t D ]\}}t||rJ |qt|ttfr+J dt	d ur<t|t	j
r<| | _d S || _d S )NzFConstantVariable(list) is banned - please create a ListVariable(items)r$   )super__init__r   r   r   r   r   r   r   r   numberitemr   )selfr   r   r    r!   	__class__r$   r%   r'   8   s   

zConstantVariable.__init__c                 C      | j S Nr   r*   r$   r$   r%   as_proxyF      zConstantVariable.as_proxyc                 C   s   dt | jj dS )NzConstantVariable())r   r   __name__r0   r$   r$   r%   __str__I   s   zConstantVariable.__str__c                 C   
   t | jS r.   r   r   r0   r$   r$   r%   python_typeM      
zConstantVariable.python_typec                 C   r-   r.   r/   r0   r$   r$   r%   as_python_constantP   r2   z#ConstantVariable.as_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_sequencer0   r$   r$   r%   r   S   s   zConstantVariable.itemsargc                 C   s   t | j|  S r.   )r   r   r   r:   )r*   r=   r$   r$   r%   getitem_const[   s   zConstantVariable.getitem_constc              	   C   sX   t | tttt d ttjtjtj	fv rdS t | t
ttttjfv r*tdd | D S dS )NTc                 s   s    | ]}t |V  qd S r.   )r   r   .0r#   r$   r$   r%   	<genexpr>o       z.ConstantVariable.is_literal.<locals>.<genexpr>F)r   intfloatboolstrEllipsisr,   torchZdtypeZdevicer   r   set	frozensetSizeall)objr$   r$   r%   r   `   s   
zConstantVariable.is_literalc              
   C   s4   z
dd |   D W S  ty } zt|d }~ww )Nc                 S   s   g | ]}t |qS r$   )r   r   r?   r$   r$   r%   
<listcomp>t   s    z8ConstantVariable.unpack_var_sequence.<locals>.<listcomp>)r:   	TypeErrorNotImplementedError)r*   r;   er$   r$   r%   r<   r   s   z$ConstantVariable.unpack_var_sequencec                 C   s:   t | jtrttjdddt| j|}t|rt |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callablerP   r*   r;   namememberr$   r$   r%   const_getattrx   s   zConstantVariable.const_getattrargsList[VariableTracker]r   Dict[str, VariableTracker]r   c              	      s  ddl m  t fddD r t j||S zdd D }dd | D }W n tyD   t	 || Y S w fd	d
}t
jtrjtj v rjtj}t||i |S |tsr|trtt}	|d }
t
|
tjtjfrd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|rJ d  }|jv }t|S tdtj d  d S )Nr   SymNodeVariablec                 3   s    | ]}t | V  qd S r.   )r   r?   r[   r$   r%   rA      rB   z/ConstantVariable.call_method.<locals>.<genexpr>c                 S   s   g | ]}|  qS r$   r:   )r@   ar$   r$   r%   rN      s    z0ConstantVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r$   r]   )r@   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lenis_python_constant)Znum_ty)rX   rU   r*   r$   r%   has_arith_binop   s   

z5ConstantVariable.call_method.<locals>.has_arith_binopr   call_function__len____contains__zconst method call .)Ztensorr\   anyr   r1   r   call_methodr   rP   r&   r   rF   __dict__keysrR   r   r   rC   rD   rc   rH   SymIntSymFloatoutputZcreate_proxyrd   re   r:   r   r   )r*   r;   rU   rX   r   Z
const_argsZconst_kwargsrf   methodopZ
add_targetproxysearchresultr+   )r\   rX   rU   r*   r%   rl      sB   
 

zConstantVariable.call_methodrU   c                 C   s   t | j|}tj|S r.   )rb   r   r   r   r   )r*   r;   rU   rv   r$   r$   r%   call_hasattr   s   zConstantVariable.call_hasattr)rX   rY   r   rZ   r   r   )r4   
__module____qualname__staticmethodr   r   r'   r1   r5   r8   r:   propertyr   r>   r   r<   rW   rl   rF   rw   __classcell__r$   r$   r+   r%   r      s.    

9r   c                       sD   e Zd Z fddZdd Zdd Zdd Zd	d
 Zdd Z  Z	S )EnumVariablec                    s   t  jdi | || _d S )Nr$   )r&   r'   r   )r*   r   r   r+   r$   r%   r'      s   
zEnumVariable.__init__c                 C   r-   r.   r/   r0   r$   r$   r%   r1      r2   zEnumVariable.as_proxyc                 C   s   dt | j dS )NzEnumVariable(r3   r7   r0   r$   r$   r%   r5      s   zEnumVariable.__str__c                 C   r6   r.   r7   r0   r$   r$   r%   r8      r9   zEnumVariable.python_typec                 C   r-   r.   r/   r0   r$   r$   r%   r:      r2   zEnumVariable.as_python_constantc                 C   s   t | j|}t|rt |S r.   )rR   r   rS   rP   rT   r$   r$   r%   rW      s   zEnumVariable.const_getattr)
r4   rx   ry   r'   r1   r5   r8   r:   rW   r|   r$   r$   r+   r%   r}      s    r}   )rc   typingr   r   rH   Ztorch._dynamo.sourcer    r   excr   r   r	   Zguardsr
   r   utilsr   baser   r   r   dictZTensorro   rp   r   r   r}   r$   r$   r$   r%   <module>   s$    
 &