
    Vh'                         d Z ddlZddlmZ ddlZddlmZmZ ddlm	Z	m
Z
 ddlmZmZ ddlmZmZmZmZ d	d
lmZ erddlmZ  G d de      Z G d de      Zy)a  
Constant and enum variable tracking in Dynamo.

This module is fundamental to Dynamo's ability to track and propagate constant
values during compilation, ensuring proper handling of Python literals and
maintaining type safety through the compilation process.
    N)TYPE_CHECKING)
AttrSourceGetItemSource   )graph_break_hints	variables)raise_observed_exceptionunimplemented_v2)cmp_name_to_op_mappingcommon_constant_typesistypenp   )VariableTracker)InstructionTranslatorc                        e Zd ZdZedefd       Zd fdZd Zde	fdZ
d Zd Zed	        Zd
ddefdZed        Zed        Zd ZddZ	 	 	 	 	 	 d fdZd
dde	ddfdZ xZS )ConstantVariablea  
    Variable tracker for Python literals and basic immutable types, with automatic
    routing support for collection types (lists, tuples, sets, etc.).

    The create() method intelligently constructs appropriate variable types for
    nested collections.
    returnc                    |j                  dd      }t        | t              r8| D cg c]  }t        j	                  |       }}t        j                  |fi |S t        | t              r8| D cg c]  }t        j	                  |       }}t        j                  |fi |S t        | t        t        f      ryg }t        |       D ];  \  }}|rt        ||      nd}|j                  t        j	                  ||             = t        j                  j                  t!        |             |fi |S t        | fi |S c c}w c c}w )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`.
        sourceNr   )get
isinstancesetr   creater   SetVariable	frozensetFrozensetVariablelisttuple	enumerater   appendBaseListVariablecls_fortype)valuekwargsr   xitemsiitem_sources          P/home/dcms/DCMS/lib/python3.12/site-packages/torch/_dynamo/variables/constant.pyr   zConstantVariable.create$   s7    Hd+ eS!9>?A%,,Q/?E?((9&99y)9>?A%,,Q/?E?..u???e}-E!%( 1:@mFA6d$++* ,  --55d5kB5SFSS000# @ @s   D>/Ec                     t        |   di | t        j                  |      sJ dt	        |       d       t
        0t        |t
        j                        r|j                         | _	        y || _	        y )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`.
 )
super__init__r   is_base_literalr%   r   r   numberitemr&   selfr&   r'   	__class__s      r,   r0   zConstantVariable.__init__E   so    "6"//6 	 =77;E{m D9 	6 >j		:DJDJ    c                     | j                   S Nr&   r5   s    r,   as_proxyzConstantVariable.as_proxyU       zzr7   c                 t    dt        | j                        j                   dt        | j                         dS )NzConstantVariable(z: ))r%   r&   __name__reprr;   s    r,   __repr__zConstantVariable.__repr__X   s2    "4

#3#<#<"=RTZZ@P?QQRSSr7   c                     | j                   S r9   r:   r;   s    r,   as_python_constantz#ConstantVariable.as_python_constant[   r=   r7   c                      y)NTr.   r;   s    r,   is_python_constantz#ConstantVariable.is_python_constant^   s    r7   c                 &    | j                  d      S )zz
        Need this when adding a BaseListVariable and a ConstantVariable together.
        Happens in detectron2.
        N)tx)unpack_var_sequencer;   s    r,   r)   zConstantVariable.itemsa   s     ''4'00r7   rH   r   argc                 b    t         j                  | j                  |j                                  S r9   )r   r   r&   rD   )r5   rH   rJ   s      r,   getitem_constzConstantVariable.getitem_consti   s*    &&JJs--/0
 	
r7   c                 $    t        |       t        v S r9   )r%   r   objs    r,   r1   z ConstantVariable.is_base_literaln   s    Cy111r7   c                     t        |       t        t        t        t        t
        j                  fv rt        d | D              S t        j                  |       S )Nc              3   F   K   | ]  }t         j                  |        y wr9   )r   
is_literal).0r(   s     r,   	<genexpr>z.ConstantVariable.is_literal.<locals>.<genexpr>u   s     C!'2215Cs   !)
r%   r   r    r   r   torchSizeallr   r1   rN   s    r,   rR   zConstantVariable.is_literalr   s?    9uc9ejjAACsCCC//44r7   c                     	 | j                         D cg c]  }t        j                  |       c}S c c}w # t        $ r}t        |d }~ww xY wr9   )rD   r   r   	TypeErrorNotImplementedError)r5   rH   r(   es       r,   rI   z$ConstantVariable.unpack_var_sequencex   sH    	-8<8O8O8QR1$++A.RRR 	-%1,	-s!   8 38 8 	AAAc                     t        | j                  |      st        t        | j                  |      }t	        |      rt        |S r9   )hasattrr&   rZ   getattrcallable)r5   rH   namemembers       r,   const_getattrzConstantVariable.const_getattr~   s9    tzz4(%%T*F%%r7   r   c                 (
   ddl m |dk(  rQt        | j                  t              r7t        j                  t        j                        j                  || g||      S |dk(  rt        | j                  t              r~t        |      dk(  rt        |      dk(  sJ |d   j                  |      }	 |D cg c]  }|j                          }}t        j                  | j                  j                  |            S t%        fd|D              r@t        j                  | j'                         | j                        j#                  ||||      S 	 |D cg c]  }|j                          }	}|j)                         D 
ci c]  \  }
}|
|j                          }}
}t+        | j                  t              rR|t        j,                  j/                         v r2t1        | j                  |      }	 t        j                   ||	i |      S t+        | j                  t8        t:        f      r|s0|s.t        j                   t1        | j                  |                   S t=        t>        |      r$t        |      dk(  r|d   jA                         r|	d   }t1        t>        |      }t+        |tB        jD                  tB        jF                  tB        jH                  f      r=|jJ                  jM                  d|| j                  |fi       }j                  |||      S t        j                   || j                  |            S t+        | j                  tN              r6|dk(  r1t1        | j                  |      }t        j                   ||	i |      S |d	k(  r,|s*|s(t        j                  t        | j                              S |d
k(  r[t        |      dk(  rM|d   jA                         r:t        j                  tQ        | j                  |d   j                                     S |dk(  r[t        |      dk(  rM|d   jA                         r:|rJ |d   j                         }|| j                  v }t        j                  |      S t         | E  ||||      S c c}w # t        $ r t         | E  ||||      cY S w xY wc c}w c c}}
w # t        $ r t         | E  ||||      cY S w xY w# t2        $ r }t5        t7        |      |       Y d }~yd }~ww xY w)Nr   )SymNodeVariableformatjoinr   c              3   6   K   | ]  }t        |        y wr9   )r   )rS   r(   rd   s     r,   rT   z/ConstantVariable.call_method.<locals>.<genexpr>   s     <!z!_-<s   call_functiondecode__len__	__round____contains__))tensorrd   r   r&   strr   BuiltinVariablere   rh   lenforce_unpack_var_sequencerD   r   r   rf   rZ   r/   call_methodanyr<   r)   r   __dict__keysr^   	Exceptionr	   r%   floatintr]   operatorrF   rU   SymBoolSymFloatSymIntoutputcreate_proxybytesround)r5   rH   r`   argsr'   arg_unpackedr(   	arg_consta
const_argskvconst_kwargsmethodr[   
add_targetopproxysearchresultrd   r6   s                       @r,   rr   zConstantVariable.call_method   sH    	,8tzz3 7,,SZZ8FFTMDM6  V^tzz3 7t9>c&kQ&6667<<R@LC=IJQ113J	J'..tzzy/IJJ <t<<,,T]]_djjIUUD$ 	?:>?Q!..0?J?BH,,.Q$!QAq3355QLQ djj#&43<<3D3D3F+FTZZ.F6'..vz/R\/RSS 

UCL1F'../Hwtzz4/H/JKK$'ING..0']
Xt, M II22'djj*-ErE +11"eZHH+222djj*3MNN

E*tx/?TZZ.F#**6:+N+NOO9df#**3tzz?;;[ SY!^Q8R8R8T#**djj$q'"<"<">?  ^#D	Q47;U;U;W:!W//1Ftzz)F#**622w"2tT6::u K& Cw*2tT6BBC @Q" 	?7&r4v>>	?  6(a"556sl   1R 5R/R S R<,S SS 0S( R R98R9<S S%$S%(	T1TTr`   c                 l    t        | j                  |      }t        j                  j	                  |      S r9   )r]   r&   r   r   r   )r5   rH   r`   r   s       r,   call_obj_hasattrz!ConstantVariable.call_obj_hasattr   s+     T*))0088r7   r   NrH   r   )r   zlist[VariableTracker]r'   zdict[str, VariableTracker]r   r   )r@   
__module____qualname____doc__staticmethodr   r   r0   r<   rn   rB   rD   rF   propertyr)   rL   r1   rR   rI   rb   rr   r   __classcell__r6   s   @r,   r   r      s     1? 1 1@ T# T 1 1
 7 
o 

 2 2 5 5
-K; &	K;
 -K; 
K;Z9)9149	9r7   r   c                   T     e Zd ZdZd	 fdZed        Zd ZdefdZ	d Z
d
dZ xZS )EnumVariablezVariableTracker for enum.Enum and enum.IntEnum instances

    Provides specialized handling for Python enum types, supporting
    both standard Enum and IntEnum with proper value tracking and comparison.
    r   c                 2    t        |   di | || _        y )Nr.   )r/   r0   r&   r4   s      r,   r0   zEnumVariable.__init__   s    "6"
r7   c           
      D   t        |t        j                        r8t        |      D ]*  }|j                  |j                         k(  s! | |fi |c S  t        dd| dt        |       d| dt        |       dg t        j                  t        j                         y )Nz!Failed to construct Enum variablezvalue: z, allowed enum values: zAttempted to construct an Enum value that is non-constant (e.g. int, string) or is not an acceptable value for the Enum. Acceptable values for Enum `z`: .)gb_typecontextexplanationhints)
r   r   r   r   r&   rD   r
   r   
USER_ERRORSUPPORTABLE)clscls_typevalue_vtoptionsra   s        r,   r   zEnumVariable.create   s    h	 : :;x. 2<<8#>#>#@@v1112 	7hZ'>tH~>NO++3*CX7GqJ R%00Q3D3P3PQ	
r7   c                 x    t        | j                  t              rt        | j                        S | j                  S r9   )r   r&   rx   r;   s    r,   r<   zEnumVariable.as_proxy   s(    djj#&tzz?"zzr7   c                 4    dt        | j                         dS )NzEnumVariable(r?   )r%   r&   r;   s    r,   rB   zEnumVariable.__repr__   s    tDJJ/022r7   c                     | j                   S r9   r:   r;   s    r,   rD   zEnumVariable.as_python_constant   r=   r7   c                    t        | j                  |      st        |t        v rt	        j
                  | |      S t        | j                  |      }| j                  xr t        | j                  |      }t        j                  |||      S )Nr   )r]   r&   rZ   r   r   GetAttrVariabler^   r   r   r   build)r5   rH   r`   ra   r   s        r,   var_getattrzEnumVariable.var_getattr   sp    tzz4(%%)),,T488T*>DKK!>$$R??r7   r   r   )r@   r   r   r   r0   classmethodr   r<   rn   rB   rD   r   r   r   s   @r,   r   r      s=     
 

3# 3@r7   r   )r   ry   typingr   rU   torch._dynamo.sourcer   r    r   r   excr	   r
   utilsr   r   r   r   baser   torch._dynamo.symbolic_convertr   r   r   r.   r7   r,   <module>r      sL        : + < M M ! D|9 |9~,@? ,@r7   