o
    ߗi!                     @   s   d dl Z d dlmZmZmZ d dlZd dlmZ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mZ er@d d
lmZ G dd deZG dd deZdS )    N)DictListTYPE_CHECKING)
AttrSourceGetItemSource   )	variables)unimplemented)common_constant_typesistypenp   )typestrVariableTracker)InstructionTranslatorc                       s   e Zd ZedefddZd( fddZdd Zdefd	d
Z	dd Z
dd Zedd ZdddefddZedd Zedd Zdd Zd)ddZ			 	!		"d* fd#d$Zddd%edd"fd&d'Z  ZS )+ConstantVariablereturnc                 K   s   | dd}t| trdd | D }tj|fi |S t| tr0dd | D }tj|fi |S t| ttfrcg }t	| D ]\}}|rHt
||nd}|tj||d q=tjt| |fi |S t| fi |S )aV  
        Create a `ConstantVariable` based on the given value, and supports
        automatic routing for collection types like `tuple` (in which case we'd
        create `ConstantVariable` for the leaf items).

        NOTE: the caller must install the proper guards if needed; most often
        the guard will be `CONSTANT_MATCH`.
        sourceNc                 S      g | ]}t |qS  r   create.0xr   r   ^/home/ubuntu/transcripts/venv/lib/python3.10/site-packages/torch/_dynamo/variables/constant.py
<listcomp>"       z+ConstantVariable.create.<locals>.<listcomp>c                 S   r   r   r   r   r   r   r   r   %   r   r   )get
isinstancesetr   SetVariable	frozensetFrozensetVariablelisttuple	enumerater   appendr   r   BaseListVariablecls_fortype)valuekwargsr   itemsir   item_sourcer   r   r   r      s&   


zConstantVariable.createNc                    s\   t  jdi | t|sJ dt| dtd ur)t|tjr)| | _	d S || _	d S )Nz7
Cannot construct `ConstantVariable` for value of type a`  .

This failure likely due to PyTorch-internal use of `ConstantVariable` on
non-literal python values, please try using `VariableTracker.build` instead. If
you believe it's a necessary and legitimate use case (the value is immutable and
can't easily be represented with another `VariableTracker` class), please add
its type to `common_constant_types`.
r   )
super__init__r   is_base_literalr+   r   r    numberitemr,   selfr,   r-   	__class__r   r   r2   5   s   
zConstantVariable.__init__c                 C      | j S Nr,   r7   r   r   r   as_proxyG      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   __repr__J   s    zConstantVariable.__repr__c                 C   r:   r;   r<   r=   r   r   r   as_python_constantM   r?   z#ConstantVariable.as_python_constantc                 C   s   dS )NTr   r=   r   r   r   is_python_constantP   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   s   zConstantVariable.itemsrF   r   argc                 C   s   t | j|  S r;   )r   r   r,   rD   )r7   rF   rH   r   r   r   getitem_const[   s   zConstantVariable.getitem_constc                 C   s   t | tv S r;   )r+   r
   objr   r   r   r3   `   s   z ConstantVariable.is_base_literalc                 C   s4   t | tttttjfv rtdd | D S t	| S )Nc                 s   s    | ]}t |V  qd S r;   )r   
is_literalr   r   r   r   	<genexpr>g       z.ConstantVariable.is_literal.<locals>.<genexpr>)
r+   r%   r&   r!   r#   torchSizeallr   r3   rJ   r   r   r   rL   d   s   
zConstantVariable.is_literalc              
   C   s4   z
dd |   D W S  ty } zt|d }~ww )Nc                 S   r   r   r   r   r   r   r   r   l   r   z8ConstantVariable.unpack_var_sequence.<locals>.<listcomp>)rD   	TypeErrorNotImplementedError)r7   rF   er   r   r   rG   j   s   z$ConstantVariable.unpack_var_sequencec                 C   s   t | j|}t|rt|S r;   )getattrr,   callablerS   )r7   rF   namememberr   r   r   const_getattrp   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 |dkrat| jtrat	|dkr4t	|dks6J |d 
|}zdd |D }t| j|W S  ty`   t |||| Y S w t fdd	|D r{t|  | j||||S zd
d |D }dd | D }W n ty   t |||| Y S w t| jtr|tj v rt| j|}	t|	|i |S t| jttfr|s|stt| j| S tt|rt	|dkr|d  r|d }
tt|}t|
tjtjtj fr|j!"d|| j|
fi } |||
S t|| j|
S nt| jt#r6|dkr6t| j|}	t|	|i |S |dkrI|sI|sItt	| jS |dkrit	|dkri|d  ritt$| j|d  S |dkrt	|dkr|d  r|rJ |d % }|| jv }t|S t&dt'| j d|  d S )Nr   SymNodeVariableformatjoinr   c                 S      g | ]}|  qS r   rD   r   r   r   r   r          z0ConstantVariable.call_method.<locals>.<listcomp>c                 3   s    | ]}t | V  qd S r;   )r    r   r]   r   r   rM      rN   z/ConstantVariable.call_method.<locals>.<genexpr>c                 S   ra   r   rb   )r   ar   r   r   r      rc   c                 S   s   i | ]	\}}||  qS r   rb   )r   kvr   r   r   
<dictcomp>   s    z0ConstantVariable.call_method.<locals>.<dictcomp>call_functiondecode__len__	__round____contains__zconst method call .)(tensorr^   r   r,   strr   BuiltinVariabler_   rh   lenforce_unpack_var_sequencer   r   r`   rS   r1   call_methodanyr>   r.   r    __dict__keysrU   floatinthasattroperatorrE   rO   SymBoolSymFloatSymIntoutputcreate_proxybytesroundrD   r	   r   )r7   rF   rW   rZ   r-   arg_unpacked	arg_const
const_argsconst_kwargsmethod
add_targetopproxysearchresultr8   r]   r   rs   v   sz   

&&


zConstantVariable.call_methodrW   c                 C   s   t | j|}tj|S r;   )ry   r,   r   r   r   )r7   rF   rW   r   r   r   r   call_hasattr   s   zConstantVariable.call_hasattrr   NrF   r   )rZ   r[   r-   r\   r   r   )rA   
__module____qualname__staticmethodr   r   r2   r>   ro   rC   rD   rE   propertyr.   rI   r3   rL   rG   rY   rs   r   __classcell__r   r   r8   r   r      s2     



Kr   c                       sR   e Zd Zd fddZedd Zdd Zdefd	d
Zdd Z	dddZ
  ZS )EnumVariabler   Nc                    s   t  jdi | || _d S )Nr   )r1   r2   r,   r6   r8   r   r   r2      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,   rD   r	   )clscls_typevalue_vtoptionsrX   r   r   r   r      s   zEnumVariable.createc                 C   s   t | jtrt| jS | jS r;   )r    r,   rx   r=   r   r   r   r>      s   
zEnumVariable.as_proxyc                 C   s   dt | j dS )NzEnumVariable(r@   )r+   r,   r=   r   r   r   rC      s   zEnumVariable.__repr__c                 C   r:   r;   r<   r=   r   r   r   rD      r?   zEnumVariable.as_python_constantrF   r   c                 C   s.   t | j|}| jot| j|}tj|||dS )Nr   )rU   r,   r   r   r   build)r7   rF   rW   rX   r   r   r   r   var_getattr   s   zEnumVariable.var_getattrr   r   )rA   r   r   r2   classmethodr   r>   ro   rC   rD   r   r   r   r   r8   r   r      s    
r   )rz   typingr   r   r   rO   torch._dynamo.sourcer   r    r   excr	   utilsr
   r   r   baser   r   torch._dynamo.symbolic_convertr   r   r   r   r   r   r   <module>   s    4