
    IЦi5B                        S SK r S SKJr  S SKJrJrJrJrJrJ	r	  SSK
Jr  SSKJr  SSKJr  SSKJrJr  SS	KJrJr  SS
KJr  \	(       a  SSKJrJr   " S S\5      r " S S5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r " S S\5      r S r!S\4S jr" " S S\#5      r$ " S  S!\$S"9r%S# r&g)$    N)Enum)AnyCallableDictListOptionalTYPE_CHECKING   )	variables)current_scope_idunimplemented)GuardBuilderinstall_guard)
AttrSourceSource)istype   )InstructionTranslatorInstructionTranslatorBasec                        \ rS rSrSrSrSrSrg)
SourceType   av  
This Enum divides VariableTracker into 2 cases, depending on the variable
it represents:
- already existed that Dynamo began tracking while introspection (Existing)
- is a new variable that is created during Dynamo introspection (New)

In general, we have these invariants:
1. for `VariableTracker` associated with `Existing`, its `source` field must not be None.
2. for `VariableTracker` associated with `New`, most of the time its
   `source` field is None, except for cases like side effect codegen for
   `AttributeMutationNew`, during which we generate a
   `LocalSource('tmp...')` for such variable, to facilitate codegen.
r   r    N)__name__
__module____qualname____firstlineno____doc__ExistingNew__static_attributes__r       [/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_dynamo/variables/base.pyr   r      s     H
Cr#   r   c                   *    \ rS rSrSrS\SS4S jrSrg)MutationType&   z
Base class for Variable.mutation_type. It encodes information about
1. The type of mutation Dynamo allows on the variable.
2. Whether the value represented by this variable already existed before
Dynamo tracing.
typreturnNc                     U[         R                  L a  SU l        g U[         R                  L a  [	        5       U l        g [        SU 35        g )Nr   zUnsupported SourceType: )r   r    scoper!   r   r   )selfr(   s     r$   __init__MutationType.__init__.   s?    0 *%%%DJJNN")+DJ4SE:;r#   )r+   )r   r   r   r   r   r   r-   r"   r   r#   r$   r&   r&   &   s    <J <4 <r#   r&   c                   <   ^  \ rS rSrSrSU 4S jjrS rS rSrU =r	$ )ValueMutationNewN   a  
This case of VariableTracker.mutation_type marker indicates
1. Dynamo allows mutation on the value itself (rather than its attributes).
2. The value is created by the bytecode Dynamo is tracing through.

For instance, Dynamo could model a newly created list with this marker,
indicating that while we need to model mutations to this list, we don't have
to emit bytecode for these mutations if the list doesn't escape into the
Python world.
c                 @   > [         TU ]  [        R                  5        g N)superr-   r   r!   r,   	__class__s    r$   r-   ValueMutationNew.__init__Z   s    (r#   c                     [        U 5      $ r3   )idr,   s    r$   __hash__ValueMutationNew.__hash__]   s    $xr#   c                     XL $ r3   r   )r,   others     r$   __eq__ValueMutationNew.__eq__`   s
    }r#   r   r)   N)
r   r   r   r   r   r-   r;   r?   r"   __classcell__r6   s   @r$   r0   r0   N   s    	) r#   r0   c                   D   ^  \ rS rSr% Sr\\S'   SS\4U 4S jjjrSrU =r	$ )ValueMutationExistingd   a  
This case of VariableTracker.mutation_type marker indicates
1. Dynamo allows mutation on the value itself (rather than its attributes).
2. The value exists before Dynamo tracing started.

For instance, Dynamo could model a pre-existing list with this marker,
indicating that if we encounter mutations to this list, we need to buffer
and re-apply those mutations after the graph runs, since the list might be
used afterwards in Python.
is_modifiedc                 L   > [         TU ]  [        R                  5        Xl        g r3   )r4   r-   r   r    rG   )r,   rG   r6   s     r$   r-   ValueMutationExisting.__init__u   s    ,,-&r#   )rG   )F)
r   r   r   r   r   bool__annotations__r-   r"   rB   rC   s   @r$   rE   rE   d   s!    	 'D ' 'r#   rE   c                   4   ^  \ rS rSrSrS\4U 4S jjrSrU =r$ )AttributeMutationz   zt
This case of VariableTracker.mutation_type marker indicates that Dynamo
allows mutation on the value's attributes.
r(   c                 $   > [         TU ]  U5        g r3   )r4   r-   )r,   r(   r6   s     r$   r-   AttributeMutation.__init__   s    r#   r   )	r   r   r   r   r   r   r-   r"   rB   rC   s   @r$   rM   rM   z   s    
J  r#   rM   c                   ,   ^  \ rS rSrSrU 4S jrSrU =r$ )AttributeMutationExisting   a  
This case of VariableTracker.mutation_type marker indicates
1. Dynamo allows mutation on the value's attributes.
2. The value exists before Dynamo tracing started.

For instance, Dynamo could model a pre-existing object with this marker,
indicating that if we encounter mutations to this object, we need to buffer
then re-apply those mutations after the graph runs, since the object might
be used afterwards in Python.
c                 @   > [         TU ]  [        R                  5        g r3   )r4   r-   r   r    r5   s    r$   r-   "AttributeMutationExisting.__init__   s    ,,-r#   r   )r   r   r   r   r   r-   r"   rB   rC   s   @r$   rR   rR      s    	. .r#   rR   c                   >   ^  \ rS rSrSrSS\\   4U 4S jjjrSrU =r	$ )AttributeMutationNew   a  
This case of VariableTracker.mutation_type marker indicates
1. Dynamo allows mutation on the value's attributes.
2. The value is created by the bytecode Dynamo is tracing through.

For instance, Dynamo could model a newly created object with this marker,
indicating that while we need to model mutations to this object, we don't
have to emit bytecode for these mutations if the object doesn't escape into
the Python world.

cls_sourcec                 L   > [         TU ]  [        R                  5        Xl        g r3   )r4   r-   r   r!   rY   )r,   rY   r6   s     r$   r-   AttributeMutationNew.__init__   s    ($r#   )rY   r3   )
r   r   r   r   r   r   r   r-   r"   rB   rC   s   @r$   rW   rW      s    	%8F#3 % %r#   rW   c                     U S:H  $ )Nr   r   )scope_ids    r$   _is_top_level_scoper^      s    q=r#   mc                 V    [        5       n[        U5      (       a  gU R                  U:H  $ )NT)r   r^   r+   )r_   r]   s     r$   is_side_effect_safera      s)    !H
 8$$77hr#   c                   >   ^  \ rS rSr/ rS\4S jrSU 4S jjrSrU =r	$ )VariableTrackerMeta   r)   c                     [         R                  [        R                  U5      (       a*  U [        [        R                  4;  a  UR                  5       n[         R                  X5      $ )z-Make isinstance work with LazyVariableTracker)type__instancecheck__r   LazyVariableTrackerVariableTrackerrealize)clsinstances     r$   rg   %VariableTrackerMeta.__instancecheck__   sZ    !!))8
 
))
 
  '')H%%c44r#   c                 d   > [         TU ]  XU5        [        R                  R	                  U 5        g r3   )r4   r-   rc   all_subclassesappend)rk   namebasesattrsr6   s       r$   r-   VariableTrackerMeta.__init__   s&    e,**11#6r#   r   rA   )
r   r   r   r   ro   rJ   rg   r-   r"   rB   rC   s   @r$   rc   rc      s    N	5D 	57 7r#   rc   c                     ^  \ rS rSrSr1 SkrS r\ S2S\S /S4   S\	S\
\\\	4      S	S4S
 jj5       rS	\4S jrS rS rS rS rS rS rSSS\S	\	4S jrSSS\S	S 4S jrS rS rS rS rS	\S    4S jrS	\S    4S jrS	\4S jr S	\4S jr!S	\\   4S jr"SSS\S	S 4S  jr#        S3S! jr$      S4S" jr%S# r&S5S$ jr'S5S% jr(S& r)S' r*S( r+S) r,S* r-\. S2SS+S\	S,\
\/   S	\	4S- jj5       r0SSS..S,\/S/\1S	S4U 4S0 jjjr2S1r3U =r4$ )6ri      z
Base class for tracked locals and stack values

VariableTracker instances are immutable and should be copied in
order to change them.

Prefer the factory function VariableTracker.build() over VariableTracker.__init__().
>   valueguardssourcemutation_typeparents_trackeruser_code_variable_namec                 r    [        U R                  5      nUR                  U5        U R                  " S0 UD6$ )z)Shallow copy with some (optional) changesr   )dict__dict__updater6   )r,   kwargsargss      r$   cloneVariableTracker.clone   s.    DMM"F~~%%%r#   Nfnrw   cacher)   c                 P   Uc  0 n[        U5      nXC;   a  gX#U'   [        U[        5      (       aq  UR                  5       nU" U5        UR                  5       nUR                  nUR
                  R                  5        H  u  pgXe;  d  M  U R                  XU5        M      g[        U[        [        45      (       a  U H  nU R                  XU5        M     g[        U[        [        R                  45      (       a*  UR                  5        H  nU R                  XU5        M     gg)z=
Walk value and call fn on all the VariableTracker instances
N)r9   
isinstanceri   unwrap_nonvar_fieldsr   itemsvisitr   listtupler~   collectionsOrderedDictvalues)rk   r   rw   r   idxnonvarskeysubvalues           r$   r   VariableTracker.visit   s     =Ei<c
e_--LLNEuILLNE**G!&!5!5!7%IIbE2 "8 ED%=))!		". "ED+"9"9:;;!LLN		". + <r#   c                 4    U R                   R                   S3$ )Nz())r6   r   r:   s    r$   __repr__VariableTracker.__repr__  s    ..))*"--r#   c                 l     [        U R                  5       5      $ ! [         a    [        U 5      s $ f = fr3   )repras_python_constantNotImplementedErrorr:   s    r$   
debug_reprVariableTracker.debug_repr  s4    	//122" 	:	s    33c                 p     [        U R                  5       5      $ ! [         a    [        U  S35      Sef = f)a  
Abstract method to be implemented by subclasses of VariableTracker.

This method should return the type represented by the instance of the subclass.
The purpose is to provide a standardized way to retrieve the Python type information
of the variable being tracked.

Returns:
    type: The Python type (such as int, str, list, etc.) of the variable tracked by
        the subclass. If the type cannot be determined or is not relevant,
        leaving it undefined or invoking super() is always sound.

Note:
    This is an abstract method and may be overridden in subclasses.

Example:
    class SetVariable(VariableTracker):
        def python_type(self):
            return set

Raises:
    NotImplementedError: If the method is not implemented in a subclass.
z has no typeN)rf   r   r   r:   s    r$   python_typeVariableTracker.python_type  sA    0	G//122" 	G%l&;<$F	Gs    5c                     [        U  S35      e)zFor constantsz is not a constantr   r:   s    r$   r   "VariableTracker.as_python_constant+  s    !TF*<"=>>r#   c                 z     U R                  5       $ ! [         a  n[        [        U5      5         SnAgSnAff = f)zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constantsN)r   r   r   str)r,   es     r$   guard_as_python_constant(VariableTracker.guard_as_python_constant/  s5    	"**,," 	"#a&!!	"s    
:5:c                 F     U R                  5         g! [         a     gf = fNTF)r   r   r:   s    r$   is_python_constant"VariableTracker.is_python_constant6  s(    	##%" 		    
  c                 f    U R                   (       a  U R                   R                  U5      $ [        er3   )ry   
make_guardr   )r,   r   s     r$   r   VariableTracker.make_guard=  s$    ;;;;))"--!!r#   txr   rq   c                     [         e)z/getattr(self, name) returning a python constantr   r,   r   rq   s      r$   const_getattrVariableTracker.const_getattrB  s    !!r#   c                 b   U R                  X5      n[        R                  R                  U5      (       d  [        eU R
                  =(       a    [        U R
                  U5      nU(       a(  [        UR                  [        R                  5      5        [        R                  R                  X4S9$ )z,getattr(self, name) returning a new variable)ry   )r   r   ConstantVariable
is_literalr   ry   r   r   r   r   CONSTANT_MATCHcreate)r,   r   rq   rw   ry   s        r$   var_getattrVariableTracker.var_getattrF  s~    ""2,))44U;;%%>DKK!>&++L,G,GHI))000FFr#   c                 F     U R                  5         g! [         a     gf = fr   )as_proxyr   r:   s    r$   is_proxyVariableTracker.is_proxyP  s%    	MMO" 		r   c                 *    [        [        U 5      5      er3   )r   r   r:   s    r$   r   VariableTracker.as_proxyW  s    !#d),,r#   c                      U R                  5       nSS Kn[        XR                  R                  5      (       a  UR
                  $ g ! [         a     g f = f)Nr   )r   torch.fxr   fxProxynoder   )r,   proxytorchs      r$   maybe_fx_nodeVariableTracker.maybe_fx_nodeZ  sG    	MMOE%00zz!" 		s   AA 
AAc                     [         er3   r   )r,   codegens     r$   reconstructVariableTracker.reconstructe      !!r#   c                     [         er3   r   r,   r   s     r$   unpack_var_sequence#VariableTracker.unpack_var_sequenceh  r   r#   c                 $    U R                  U5      $ r3   )r   r   s     r$   force_unpack_var_sequence)VariableTracker.force_unpack_var_sequencek  s     ''++r#   c                 H     U R                  U5        g! [         a     gf = fr   )r   r   r   s     r$   has_unpack_var_sequence'VariableTracker.has_unpack_var_sequencet  s*    	$$R(" 		s    
!!c                 $    U R                  U5      $ r3   )r   r   s     r$   has_force_unpack_var_sequence-VariableTracker.has_force_unpack_var_sequence|  s    ++B//r#   c                      [        SU  35        g )Nzinspect_parameter_names: r   r:   s    r$   inspect_parameter_names'VariableTracker.inspect_parameter_names  s    1$89r#   c                 N    [        SU R                  R                   SU 35        g )Nzhasattr  )r   r6   r   r   s      r$   call_hasattrVariableTracker.call_hasattr  s"    !8!8 94&ABr#   c                 ,    [        SU  SU SU 35        g )Nzcall_function r   r   )r,   r   r   r   s       r$   call_functionVariableTracker.call_function  s     	tfAdV1VH=>r#   c           
         US:X  a]  U R                  U5      (       aG  U(       d  U(       a   e[        R                  R                  [	        U R                  U5      5      5      $ US:X  aP  [	        U5      S:X  aA  US   R                  5       (       a)  U(       d"  U R                  XS   R                  5       5      $ [        SU  SU SU SU 35        g )N__len____getattr__r   r   zcall_method r   )
r   r   r   r   lenr   r   r   r   r   )r,   r   rq   r   r   s        r$   call_methodVariableTracker.call_method  s     9!=!=b!A!A''--44S9Q9QRT9U5VWWM!D	QQ**,,##BQ(B(B(DEETF!D64&&BCr#   c                     g r3   r   )r,   rq   s     r$   set_name_hintVariableTracker.set_name_hint  s    r#   c                     U $ )z=Used by LazyVariableTracker to build the real VariableTrackerr   r:   s    r$   rj   VariableTracker.realize      r#   c                     U $ )zSUsed by LazyVariableTracker to return the real VariableTracker if it already existsr   r:   s    r$   r   VariableTracker.unwrap  r   r#   c                     g)z:Used by LazyVariableTracker to indicate an unrealized nodeTr   r:   s    r$   is_realizedVariableTracker.is_realized  s    r#   c                 "    [        SU  S35        g )Nznext()r   r   s     r$   next_variableVariableTracker.next_variable  s    dV1o&r#   c                 J    UR                   =(       a    UR                  U 5      $ r3   )strict_checks_fnr   s     r$   is_strict_modeVariableTracker.is_strict_mode  s    ""@r':':4'@@r#   c                 ,    U R                  5       (       + $ )z0Whether Dynamo allows mutation on this variable.)is_immutabler:   s    r$   
is_mutableVariableTracker.is_mutable  s    $$&&&r#   c                     U R                   SL $ )z.Whether Dynamo bans mutation on this variable.N)rz   r:   s    r$   r  VariableTracker.is_immutable  s    !!T))r#   r   ry   c                 x    SSK Jn  Uc  UR                  R                  X5      $ UR	                  X5      " U5      $ )z=Create a new VariableTracker from a value and optional Sourcer   )builder) r
  SourcelessBuilderr   VariableBuilder)r   rw   ry   r
  s       r$   buildVariableTracker.build  s9     	>,,33B>>**26u==r#   )ry   rz   rz   c                   > [         TU ]  5         Xl        X l        UbD  [	        U[
        [        45      (       a  Ub   eg [	        U[        [        45      (       d   eUc   eg g r3   )	r4   r-   ry   rz   r   r0   rW   rE   rR   )r,   ry   rz   r6   s      r$   r-   VariableTracker.__init__  sy     	* $-*:<P)QRR ~%~!!$9;T#U    ))) %r#   )rz   ry   r3   )r   r   r   List[VariableTracker]r   Dict[str, VariableTracker]r)   ri   )r   r  r   r  r)   ri   )r)   ri   )5r   r   r   r   r   r   r   classmethodr   r   r   r   intr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rJ   r   r   r   r   r   r   r   rj   r   r   r   r  r  r  staticmethodr   r  r&   r-   r"   rB   rC   s   @r$   ri   ri      sB   N& 
 +/	/'($.// / S#X'	/
 
/ /B.# .G:?""
" 7 "s "s "G5 GS GEV G-	"".?)@ ",t4E/F ,T 04 0:c :C6 Cc CFW C?#? &? -	?
 
?D &	D
 -D 
D&'A'*  $(>'>>  > 
	> >  &*	* * $	*
 
* *r#   ri   )	metaclassc                      [        U 5      S:X  a9  U u  n[        U[        5      (       a  [        U5      $ [	        U5      R
                  $ SR                  [        [        U 5      5      $ )Nr   r   )	r   r   ri   r   rf   r   joinmaptypestr)objsobjs     r$   r  r    sP    
4yA~c?++s8O9%%%xxGT*++r#   )'r   enumr   typingr   r   r   r   r   r	   r  r   r   excr   rx   r   r   ry   r   r   utilsr   symbolic_convertr   r   r   r&   r0   rE   rM   rR   rW   r^   ra   rf   rc   ri   r  r   r#   r$   <module>r#     s      E E  /  0 '  R &%< %<P| ,'L ', . 1 . %, %"	< 	7$ 7&\* 3 \*~,r#   