o
    "ir"                  
   @   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def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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|ryt| tttt	fryg }t
| D ]!\}}|r;t||nd }	|	rGt|	tj |tj||	d q0t| ttfrgtjt| |fi |S t| tt	fstJ t| t|S t| fi |S )Nsource)r   )getr   
is_literal_type_to_assert_reasonitems
isinstancelisttupleset	frozenset	enumerater   r   
make_guardr
   CONSTANT_MATCHappendcreater   BaseListVariablecls_fortypeSetVariable)
valuekwargsr   r   disallowed_typereasonr   ixitem_source r.   ^/home/ubuntu/SoloSpeech/.venv/lib/python3.10/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/   r1   >   s   

zConstantVariable.__init__c                 C      | j S Nr'   r4   r.   r.   r/   as_proxyL      zConstantVariable.as_proxyc                 C   s    dt | jj dt| j dS )NzConstantVariable(z: ))r%   r'   __name__reprr:   r.   r.   r/   __str__O   s    zConstantVariable.__str__c                 C   
   t | jS r8   r%   r'   r:   r.   r.   r/   python_typeR      
zConstantVariable.python_typec                 C   r7   r8   r9   r:   r.   r.   r/   as_python_constantU   r<   z#ConstantVariable.as_python_constantc                 C   s   dS )NTr.   r:   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_sequencer:   r.   r.   r/   r   [   s   zConstantVariable.itemsargc                 C   s   t | j|  S r8   )r   r"   r'   rE   )r4   rI   r.   r.   r/   getitem_constc   s   zConstantVariable.getitem_constc                 C   s>   t | t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 r8   )r   r   .0r,   r.   r.   r/   	<genexpr>n       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   s4   z
dd |   D W S  ty } zt|d }~ww )Nc                 S   s   g | ]}t |qS r.   )r   r"   rK   r.   r.   r/   
<listcomp>s   s    z8ConstantVariable.unpack_var_sequence.<locals>.<listcomp>)rE   	TypeErrorNotImplementedError)r4   rG   er.   r.   r/   rH   q   s   z$ConstantVariable.unpack_var_sequencec                 C   s8   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__ insteadtype_reflection_method)	case_name)	r   r'   r%   r   r	   ANTI_PATTERNgetattrcallablerU   r4   rG   namememberr.   r.   r/   const_getattrw   s   zConstantVariable.const_getattrargsList[VariableTracker]r(   Dict[str, VariableTracker]r   c              	      s  ddl m  dkrtjtrttj|g|S t	 fddD r8t
 j||S zdd D }dd	 | D }W n ty\   t || Y S w fd
d}tjtrtj v rtj}t||i |S |ts|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   SymNodeVariableformatc                 3   s    | ]}t | V  qd S r8   )r   rK   rc   r.   r/   rM      rN   z/ConstantVariable.call_method.<locals>.<genexpr>c                 S   s   g | ]}|  qS r.   rE   )rL   ar.   r.   r/   rS      s    z0ConstantVariable.call_method.<locals>.<listcomp>c                 S   s   i | ]	\}}||  qS r.   rf   )rL   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lenrF   )num_ty)r`   r]   r4   r.   r/   has_arith_binop   s   

z5ConstantVariable.call_method.<locals>.has_arith_binopr   call_function__len____contains__zconst method call .)"tensorrd   r   r'   strr   BuiltinVariablere   rp   anyr;   call_methodr   rU   r0   r   __dict__keysrZ   r   r"   intfloatrl   rO   SymIntSymFloatoutputcreate_proxyrm   rF   rE   r   r   )r4   rG   r]   r`   r(   
const_argsconst_kwargsro   methodop
add_targetproxysearchresultr5   )rd   r`   r]   r4   r/   rx      sJ   
 

zConstantVariable.call_methodr]   c                 C   s   t | j|}tj|S r8   )rk   r'   r   r   r"   )r4   rG   r]   r   r.   r.   r/   call_hasattr   s   zConstantVariable.call_hasattr)r`   ra   r(   rb   r   r   )r>   
__module____qualname__staticmethodr   r"   r1   r;   r@   rC   rE   rF   propertyr   rJ   r   rH   r_   rx   ru   r   __classcell__r.   r.   r5   r/   r       s0    

>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di | || _d S )Nr.   )r0   r1   r'   )r4   r'   r(   r5   r.   r/   r1      s   
zEnumVariable.__init__c                 C   sH   t |tjrt|D ]}|j| kr| |fi |  S q
td d S )Nz5Enum variable is constructed with non constant values)r   r   r   r   r'   rE   r   )clscls_typevalue_vtoptionsr^   r.   r.   r/   r"      s   zEnumVariable.createc                 C   r7   r8   r9   r:   r.   r.   r/   r;      r<   zEnumVariable.as_proxyc                 C   s   dt | j dS )NzEnumVariable(r=   rB   r:   r.   r.   r/   r@      s   zEnumVariable.__str__c                 C   rA   r8   rB   r:   r.   r.   r/   rC      rD   zEnumVariable.python_typec                 C   r7   r8   r9   r:   r.   r.   r/   rE      r<   zEnumVariable.as_python_constantc                 C   s   t | j|}t|rt|S r8   )rZ   r'   r[   rU   r\   r.   r.   r/   r_      s   zEnumVariable.const_getattr)r>   r   r   r1   classmethodr"   r;   r@   rC   rE   r_   r   r.   r.   r5   r/   r      s    
r   )rl   typingr   r   rO   torch._dynamo.sourcer    r   excr   r   r	   guardsr
   r   utilsr   r   r   baser   r   r   dictTensorr}   r~   r   r   r   r.   r.   r.   r/   <module>   s$   
 (