
    ^j*                   H   U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	m
Z
mZ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 dd
lm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&  e'h d      Z(er*ddl)m*Z* ddl+m,Z, ddl-m.Z. ddl/m0Z0 ddl1m2Z2 ddl3m4Z4 ddl5m6Z6  ejn                  e8      Z9 ejt                  d       G d d             Z; edd      Z<de=d<    G d d e      Z> G d! d"      Z? G d# d$e      Z@ G d% d&e?      ZA G d' d(e?      ZB G d) d*e?      ZC G d+ d,eC      ZD G d- d.eC      ZEdAd/ZFdBd0ZG G d1 d2      ZH G d3 d4eI      ZJ G d5 d6eK      ZL G d7 d8eL      ZM G d9 d:eL;      ZNdCd<ZOeKj                  ZQd=ZRdDd>ZSdd?lmTZT dd@lUmVZV y)Ea  
Core variable tracking functionality for Dynamo. This module defines the fundamental
classes and systems used to track and manage variables during Dynamo's operation.

The module provides:
1. VariableTracker - The base class for tracking variables during compilation
2. MutationType system - Classes for tracking and managing mutations to variables
3. Source type management - Utilities for tracking variable origins and scope
4. Variable state management - Tools for managing variable state and transformations

These components form the foundation of Dynamo's variable handling system,
enabling accurate tracking and transformation of Python code into optimized
computations.
    )annotationsN)Callable	ItemsViewKeysView
ValuesView)
ContextVar)Enum)AnyNoReturnTYPE_CHECKING   )graph_break_hints	variables)current_scope_id)raise_observed_exceptionunimplemented)GuardBuilderinstall_guard)
AttrSourceSource)format_source_rangeistyperaise_args_mismatch>   __eq____ge____gt____le____lt____ne__)Guard)Node)	PyCodegen)SideEffects)InstructionTranslatorBase   )ConstantVariable)UserFunctionVariableT)frozenc                  X    e Zd ZU dZded<   ded<   dZded<   dZded	<   dZded
<   ddZy)SourceLocationzMSource position of the bytecode instruction that generated a VariableTracker.strfilenameintlinenoN
int | None
end_lineno
col_offsetend_col_offsetc                   d| j                    d| j                   d}t        | j                   | j                  | j                  | j                  | j
                        }|r+|t        j                  |j                  d      d      dz   z  }|S )Nz  File "z", line 
z    )	r,   r.   r   r0   r1   r2   textwrapindentrstrip)selfresultsources      g/var/www/ramen.bs-engineer-server.com/venv/lib/python3.12/site-packages/torch/_dynamo/variables/base.pyformatzSourceLocation.formatB   sz    DMM?(4;;-rB$MMKKOOOO
 hoofmmD&96BTIIF    returnr+   )	__name__
__module____qualname____doc____annotations__r0   r1   r2   r<    r=   r;   r*   r*   8   s2    WMK!J
!!J
!!%NJ%r=   r*   _vt_active_calls)defaultz'ContextVar[set[tuple[int, int]] | None]c                      e Zd ZdZdZdZy)
SourceTypea  
    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)r@   rA   rB   rC   ExistingNewrE   r=   r;   rI   rI   [   s     H
Cr=   rI   c                      e Zd ZdZddZy)MutationTypez
    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.
    c                    |t         j                  u rd| _        y |t         j                  u rt	               | _        y t        dd|  d| d| ddgt        j                         y )	Nr   zUnsupported SourceTypezMutationType.__init__  z"Dynamo does not support the type ``z,This branch is not supposed to be reachable.gb_typecontextexplanationhints)rI   rJ   scoperK   r   r   r   
DYNAMO_BUG)r8   typs     r;   __init__zMutationType.__init__v   sl    0 *%%%DJJNN")+DJ00au=@QGB&11	r=   N)rX   rI   r?   None)r@   rA   rB   rC   rY   rE   r=   r;   rM   rM   n   s    %r=   rM   c                      e Zd ZdZdZy)AttrMutationKindr   r%   N)r@   rA   rB   GENERIC_SETATTRINSTANCE_DICTrE   r=   r;   r\   r\      s     OMr=   r\   c                  4     e Zd ZdZd fdZddZddZ xZS )ValueMutationNewa  
    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         |   t        j                         y N)superrY   rI   rK   r8   	__class__s    r;   rY   zValueMutationNew.__init__   s    (r=   c                    t        |       S rb   )idr8   s    r;   __hash__zValueMutationNew.__hash__   s    $xr=   c                
    | |u S rb   rE   r8   others     r;   r   zValueMutationNew.__eq__   s    u}r=   r?   rZ   )r?   r-   rl   objectr?   bool)r@   rA   rB   rC   rY   ri   r   __classcell__re   s   @r;   r`   r`      s    	)r=   r`   c                  2     e Zd ZU dZded<   dd fdZ xZS )ValueMutationExistinga  
    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.
    rp   is_modifiedc                N    t         |   t        j                         || _        y rb   )rc   rY   rI   rJ   ru   )r8   ru   re   s     r;   rY   zValueMutationExisting.__init__   s    ,,-&r=   F)ru   rp   r?   rZ   )r@   rA   rB   rC   rD   rY   rq   rr   s   @r;   rt   rt      s    	 ' 'r=   rt   c                      e Zd ZdZy)AttributeMutationz
    This case of VariableTracker.mutation_type marker indicates that Dynamo
    allows mutation on the value's attributes.
    Nr@   rA   rB   rC   rE   r=   r;   ry   ry      s    r=   ry   c                  $     e Zd ZdZd fdZ xZS )AttributeMutationExistinga  
    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         |   t        j                         y rb   )rc   rY   rI   rJ   rd   s    r;   rY   z"AttributeMutationExisting.__init__   s    ,,-r=   rm   r@   rA   rB   rC   rY   rq   rr   s   @r;   r|   r|      s    	. .r=   r|   c                  &     e Zd ZdZdd fdZ xZS )AttributeMutationNewa  
    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.
    c                N    t         |   t        j                         || _        y rb   )rc   rY   rI   rK   
cls_source)r8   r   re   s     r;   rY   zAttributeMutationNew.__init__   s    ($r=   rb   )r   Source | Noner?   rZ   r~   rr   s   @r;   r   r      s    	% %r=   r   c                    | dk(  S )Nr%   rE   )scope_ids    r;   _is_top_level_scoper      s    q=r=   c                L    t               }t        |      ry| j                  |k(  S NT)r   r   rV   )mr   s     r;   is_side_effect_safer      s&    !H
 8$77hr=   c                      e Zd ZdZy)NO_SUCH_SUBOBJz;Sentinel indicating no concrete Python object is available.Nrz   rE   r=   r;   r   r     s    Er=   r   c                  .     e Zd ZU ded<   dd fdZ xZS )#AsPythonConstantNotImplementedErrorVariableTrackervtc                D    || dn|}t         |   |       || _        y )Nz is not a constant)rc   rY   r   )r8   r   msgre   s      r;   rY   z,AsPythonConstantNotImplementedError.__init__  s*    +.;&'Cr=   rb   )r   r   r   z
str | Noner?   rZ   )r@   rA   rB   rD   rY   rq   rr   s   @r;   r   r     s     r=   r   c                  d     e Zd ZU g Zded<   	 	 	 	 	 	 	 	 	 	 d fdZ	 	 	 	 	 	 	 	 	 	 d fdZ xZS )VariableTrackerMetaz
list[type]all_subclassesc                    |j                  dd      xs t        d |D              }|s|dk(  rt        |   t        |||      S t        |   t
        |||      S )N_no_implicit_realizeFc              3  6   K   | ]  }t        |d d        yw)r   FN)getattr).0bases     r;   	<genexpr>z.VariableTrackerMeta.__new__.<locals>.<genexpr>%  s!      N
=AGD0%8N
s   r   )getanyrc   __new__r   $ImplicitRealizingVariableTrackerMeta)mcsnamebasesattrsno_implicit_realizere   s        r;   r   zVariableTrackerMeta.__new__  sr     $ii(>F 
# N
EJN
 K
 $*;";7?#6eUKK 7?4dE5 r=   c                f    t         |   |||       t        j                  j	                  |        y rb   )rc   rY   r   r   append)clsr   r   r   re   s       r;   rY   zVariableTrackerMeta.__init__1  s*     	ue,**11#6r=   )
r   typer   r+   r   tuple[type, ...]r   dict[str, Any]r?   r   )
r   r   r   r+   r   r   r   r   r?   rZ   )r@   rA   rB   r   rD   r   rY   rq   rr   s   @r;   r   r     sl    !#NJ#%5>L	&777%57>L7	7 7r=   r   c                      e Zd ZddZy)r   c                \    t        t        |      r|j                  |       S t        | |      S )z-Make isinstance work with LazyVariableTracker)instancecheckLazyVariableTrackerlazy_isinstance)r8   instances     r;   __instancecheck__z6ImplicitRealizingVariableTrackerMeta.__instancecheck__9  s+    ,h7++D11T8,,r=   N)r   ro   r?   rp   )r@   rA   rB   r   rE   r=   r;   r   r   8  s    -r=   r   c                      e Zd ZU dZdZded<   h dZdkdZe	 	 dl	 	 	 	 	 	 	 	 	 dmd       Z	dndZ
dnd	Zdod
ZdndZdpdZdpdZdqdZdrdZdqdZdsdZ	 	 	 	 	 	 	 	 dtdZdudZdqdZdvdZdwdZdqdZdqdZdxdZdydZdqdZdpdZdzdZdqdZ d{dZ!d|d Z"d}d!Z#	 	 	 	 	 	 d~d"Z$dd#Z%	 	 	 	 	 	 	 	 dd$Z&dd%Z'dd&Z(	 	 	 	 	 	 dd'Z)	 	 	 	 	 	 dd(Z*dd)Z+	 	 	 	 	 	 	 	 dd*Z,	 	 	 	 	 	 	 	 dd+Z-	 	 	 	 	 	 dd,Z.	 	 	 	 	 	 dd-Z/	 	 	 	 	 	 	 	 	 	 dd.Z0	 	 	 	 	 	 	 	 	 	 	 	 dd/Z1	 	 	 	 	 	 	 	 	 	 	 	 dd0Z2	 	 	 	 	 	 	 	 	 	 	 	 dd1Z3	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd2Z4	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd3Z5	 	 	 	 	 	 	 	 	 	 	 	 	 	 dd4Z6dd5Z7dd6Z8dd7Z9dd8Z:dqd9Z;dd:Z<	 	 	 	 	 	 dd;Z=dd<Z>dd=Z?dqd>Z@dqd?ZAeB	 	 d	 	 	 	 	 	 	 	 	 dd@       ZCddAZDddBZEddCZFddDZG	 	 	 	 ddEZH	 	 	 	 ddFZI	 	 	 	 ddGZJ	 	 	 	 ddHZK	 	 	 	 ddIZL	 d	 	 	 	 	 ddJZM	 d	 	 	 	 	 	 	 ddKZN	 	 	 	 	 	 ddLZO	 d	 	 	 	 	 	 	 ddMZP	 	 	 	 	 	 ddNZQ	 d	 	 	 	 	 	 	 ddOZR	 	 	 	 	 	 ddPZS	 d	 	 	 	 	 	 	 ddQZT	 	 	 	 	 	 ddRZU	 d	 	 	 	 	 	 	 ddSZV	 	 	 	 	 	 ddTZW	 d	 	 	 	 	 	 	 ddUZX	 	 	 	 	 	 ddVZY	 d	 	 	 	 	 	 	 ddWZZ	 	 	 	 	 	 ddXZ[	 d	 	 	 	 	 	 	 ddYZ\	 d	 	 	 	 	 	 	 ddZZ]	 	 	 	 	 	 dd[Z^	 	 	 	 	 	 dd\Z_	 	 	 	 	 	 dd]Z`	 d	 	 	 	 	 	 	 dd^Za	 	 	 	 	 	 dd_Zb	 	 	 	 dd`Zc	 	 	 	 ddaZd	 d	 	 	 	 	 	 	 ddbZe	 	 	 	 	 	 ddcZf	 d	 	 	 	 	 	 	 dddZg	 	 	 	 	 	 ddeZh	 	 	 	 ddfZiddddg	 	 	 	 	 	 	 d fdhZjd fdiZkeB	 	 	 	 	 	 	 	 ddj       Zl xZmS )r   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__().
    Nztype | tuple[type, ...] | None_cpython_type>   valueguardsr:   mutation_typeparents_trackersource_locationuser_code_variable_namec                r    t        | j                        }|j                  |        | j                  di |S )z)Shallow copy with some (optional) changesrE   )dict__dict__updatere   )r8   kwargsargss      r;   clonezVariableTracker.cloneZ  s0    DMM"Ft~~%%%r=   c                   |i }t        |      }||v ry|||<   t        |t              r|j                         } ||       |j                         }|j                  }|j
                  j                         D ]  \  }}||vs| j                  ||||         |F||j                  v r7|j                  |   j                         D ]  }	| j                  ||	||        yyyt        |t        t        f      r|D ]  }| j                  ||||        yt        |t        t        j                  f      r*|j                         D ]  }| j                  ||||        yy)a  
        Walk value and call fn on all the VariableTracker instances.

        When side_effects is provided, also walks attributes stored in
        store_attr_mutations (e.g. dataclass fields set during tracing
        that aren't in the VT's __dict__).
        N)rg   
isinstancer   unwrap_nonvar_fieldsr   itemsvisitstore_attr_mutationsvaluesr   listtupler   collectionsOrderedDict)
r   fnr   cacheside_effectsidxnonvarskeysubvalueattr_vts
             r;   r   zVariableTracker.visit`  s_    =Ei%<c
e_-LLNEuILLNE**G!&!5!5!7 AXg%IIb(E<@A 'E\5V5V,V+@@GNNP @GIIb'5,?@ -W' ED%=)! =		"h|<=ED+"9"9:;!LLN =		"h|<= <r=   c                4    | j                   j                   dS )Nz())re   r@   rh   s    r;   __repr__zVariableTracker.__repr__  s    ..))*"--r=   c                j    	 t        | j                               S # t        $ r t        |       cY S w xY wrb   )repras_python_constantNotImplementedErrorrh   s    r;   
debug_reprzVariableTracker.debug_repr  s4    	//122" 	:	s    22c                n    	 t        | j                               S # t        $ r t        |  d      dw xY w)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)r   r   r   rh   s    r;   python_typezVariableTracker.python_type  sA    0	G//122" 	G%l&;<$F	Gs    4c                V    	 | j                         j                  S # t        $ r Y yw xY w)Nz<unknown type>)r   r@   r   rh   s    r;   python_type_namez VariableTracker.python_type_name  s.    	$##%..." 	$#	$s    	((c                    t        |       )zFor constantsr   rh   s    r;   r   z"VariableTracker.as_python_constant  s    1$77r=   c                n    	 | j                         S # t        $ r t        dd|  d|  dg        Y yw xY w)zcSimilar to as_python_constant(), but add ID_MATCH guards to try to force things to become constantszNot a Python constantzguard_as_python_constant zFailed to convert z into a Python constant.rQ   N)r   r   r   rh   s    r;   guard_as_python_constantz(VariableTracker.guard_as_python_constant  sJ    	**,," 	/3D6:06NO		s    44c                D    	 | j                          y# t        $ r Y yw xY wNTF)r   r   rh   s    r;   is_python_constantz"VariableTracker.is_python_constant  s(    	##%" 		    	c                     y rb   rE   r8   txs     r;   	bool_implzVariableTracker.bool_impl  s     r=   c                     y)u   Whether the underlying Python object is hashable.

        Defaults to True — in CPython, all objects are hashable unless the type
        explicitly sets __hash__ = None (PyObject_HashNotImplemented).
        https://github.com/python/cpython/blob/e76aa128fe/Objects/typeobject.c#L7288

        Subclasses override to return False for unhashable types (list, dict,
        set, etc.).  This is a fast, side-effect-free check used by
        HashableTracker and __contains__ before attempting to hash.
        TrE   rh   s    r;   is_hashablezVariableTracker.is_hashable  s     r=   c                   | j                   r|j                  j                  | j                         }t        |      j                  t
        j                  urdt        dd|  t        |       j                   dt        |      j                   dt        |       j                   dg t        j                         t        |      dfS t        |       dfS )a  Default tp_hash: object.__hash__ = PyObject_GenericHash (identity hash).

        PyObject_GenericHash: https://github.com/python/cpython/blob/e76aa128fe/Python/pyhash.c#L143-L147
        Assigned on object: https://github.com/python/cpython/blob/e76aa128fe/Objects/typeobject.c#L7288

        Returns (hash_value, is_fake).  is_fake=True means the hash depends on
        a sourceless object's identity and must not escape into output bytecode.

        VT subclasses override this for types with their own C tp_hash.
        UserDefinedObjectVariable.hash_impl handles the MRO walk for custom
        Python __hash__, builtin-inherited C hash, and unknown C tp_hash.
        zMissing hash_impl overridez
hash_impl z wraps z> which has a non-default tp_hash. Add a hash_impl override to .rQ   T)r:   outputresolve_source_valuer   ri   ro   r   r@   r   rW   hashrg   )r8   r   real_vals      r;   	hash_implzVariableTracker.hash_impl  s     ;;yy55dkkBHH~&&foo=8(/#':#6#6"7wH~../ 0<Dz**+1!. :-889 >4''$x~r=   c           	         t        dd|  d| t        |       j                   dt        |       j                   dg t        j                         y)a  Per-VT tp_richcompare slot. Subclasses must override.

        Analogous to CPython's tp_richcompare function pointer on PyTypeObject.
        Returns ConstantVariable(NotImplemented) when the type does not handle
        the comparison (signaling do_richcompare to try the other operand).

        Called from two paths:
        - call_method("__eq__") calls richcompare_impl directly (like CPython's
          a.__eq__(b) calling tp_richcompare without do_richcompare).
        - generic_richcompare calls richcompare_impl as part of the 4-step
          do_richcompare algorithm (subclass priority, forward, reflected,
          fallback).
        z!Missing richcompare_impl overridezrichcompare_impl rO   zI does not implement richcompare_impl. Add a richcompare_impl override to r   rQ   N)r   r   r@   r   rW   )r8   r   rl   ops       r;   richcompare_implz VariableTracker.richcompare_impl  s]    & 	7'vQrd3:../ 0DDz""#1& 2%001	
r=   c                     y)an  
        Check if this variable is a python constant matching one of the given values.

        Examples:
            var.is_constant_match(None)  # True if var is constant None
            var.is_constant_match(True, False)  # True if var is constant True or False
            var.is_constant_match(NotImplemented)  # True if var is constant NotImplemented
        FrE   )r8   r   s     r;   is_constant_matchz!VariableTracker.is_constant_match"  s     r=   c                     y)z0Check if this variable is a constant None value.FrE   rh   s    r;   is_constant_nonez VariableTracker.is_constant_none-      r=   c                \    | j                   r| j                   j                  |      S t        rb   )r:   
make_guardr   )r8   r   s     r;   r   zVariableTracker.make_guard1  s$    ;;;;))"--!!r=   c                    t         )z/getattr(self, name) returning a python constantr   r8   r   r   s      r;   const_getattrzVariableTracker.const_getattr6  s    !!r=   c                     y)zAReturn True for values that can participate in SymNode operationsFrE   rh   s    r;   is_symnode_likezVariableTracker.is_symnode_like:  r   r=   c                     y)z(Return True for TensorVariable instancesFrE   rh   s    r;   	is_tensorzVariableTracker.is_tensor>  r   r=   c                    t        |       S )a  Return the VariableTracker type to use for builtin handler dispatch.

        This is used by BuiltinVariable to look up the appropriate handler for
        a given set of arguments. Most VariableTrackers just return their own
        type, but LazyConstantVariable returns ConstantVariable since it can
        be treated as a constant for most builtin operations.

        Subclasses that override this should install appropriate guards to
        ensure the dispatched handler remains valid.
        )r   rh   s    r;   get_handler_type_for_dispatchz-VariableTracker.get_handler_type_for_dispatchB  s     Dzr=   c                n   | j                  ||      }t        j                  j                  |      st        | j
                  xr t        | j
                  |      }|r8| j                         s(t        |j                  t        j                               t        j                  j                  ||      S )z,getattr(self, name) returning a new variable)r:   )r  r   r&   
is_literalr   r:   r   r   r   r   r   CONSTANT_MATCHcreate)r8   r   r   r   r:   s        r;   var_getattrzVariableTracker.var_getattrO  s    ""2t,))44U;%%>DKK!>$113 &++L,G,GHI))00v0FFr=   c                D    	 | j                          y# t        $ r Y yw xY wr   )as_proxyr   rh   s    r;   is_proxyzVariableTracker.is_proxy[  s%    	MMO" 		r   c                *    t        t        |             rb   )r   r+   rh   s    r;   r  zVariableTracker.as_proxyb  s    !#d),,r=   c                    	 | j                         }dd l}t        ||j                  j                        r|j
                  S y # t        $ r Y y w xY wNr   )r  torch.fxr   fxProxynoder   )r8   proxytorchs      r;   maybe_fx_nodezVariableTracker.maybe_fx_nodee  sF    	MMOE%0zz!" 		s   ?A 	AAc                     dd fd} j                   j                         D ]*  \  }}| j                  vst        j	                  ||       , S )zBCheck if this variable references itself (directly or indirectly).Fc                    | u rdy y r   rE   )r   
found_selfr8   s    r;   checkz7VariableTracker._contains_self_reference.<locals>.checkt  s    Tz!
 r=   )r   r   r?   rZ   )r   r   r   r   r   )r8   r  r   r   r  s   `   @r;   _contains_self_referencez(VariableTracker._contains_self_referencep  sU    
	" "]]002 	7MC$---%%eX6	7 r=   c                    t         rb   r  r8   codegens     r;   reconstructzVariableTracker.reconstruct      !!r=   c                    t        d|        )Nz'reconstruct_pycode not implemented for r  r"  s     r;   reconstruct_pycodez"VariableTracker.reconstruct_pycode  s    !$KD6"RSSr=   c                    t         rb   r  r   s     r;   unpack_var_sequencez#VariableTracker.unpack_var_sequence  r%  r=   c           
         t        dd|  d| d| j                          dd| j                  j                   d| dgt        j
                  	       y )
NzUnsupported hasattr callzcall_obj_hasattr rO   0Dynamo does not know how to trace the function `rP   zAvoid calling `hasattr(, z)` in your code.rQ   )r   r   re   r@   r   SUPPORTABLEr  s      r;   call_obj_hasattrz VariableTracker.call_obj_hasattr  se     	.'vQtf5J4??K\J]]^_)$..*A*A)B"TFJZ["..		
r=   c                    t        dd| j                          d| j                          dg t        j                         y)z~
        Implements PyObject_GetIter semantics (tp_iter slot).
        Subclasses override this to support iteration.
        zmissing tp_iterz!tp_iter_impl not implemented for z*Dynamo does not know how to iterate over ``.rQ   Nr   r   r   r   r-  r   s     r;   tp_iter_implzVariableTracker.tp_iter_impl  sG     	%78M8M8O7PQDT__EVDWWYZ2%112		
r=   c           	         t        dd|  d| d| d| j                          dd| j                          ddg	       y )
NzUnsupported function callzcall_function rO   r+  rP   Avoid calling `` in your code."Please report an issue to PyTorch.rQ   )r   r   )r8   r   r   r   s       r;   call_functionzVariableTracker.call_function  sV     	/$TF!D66(;J4??K\J]]^_!$//"3!4OD4		
r=   c                N    t        t        |d| j                          dg       y)z)Called when sq_length is not implemented.object of type '' has no len()r   Nr   	TypeErrorr   r   s     r;   	sq_lengthzVariableTracker.sq_length  )     $T%:%:%<$=^LM	
r=   c                N    t        t        |d| j                          dg       y)z)Called when mp_length is not implemented.r9  r:  r;  Nr<  r   s     r;   	mp_lengthzVariableTracker.mp_length  r?  r=   c                    t        ddt        |       j                   d| j                          dg t        j
                         y )Nmissing_mp_subscriptz"mp_subscript_impl not defined for z*Dynamo does not yet support subscripting ''.rQ   r   r   r@   r   r   r-  r8   r   r   s      r;   mp_subscript_implz!VariableTracker.mp_subscript_impl  sJ     	*8d9L9L8MNDTEZEZE\D]]_`2%112		
r=   c                4    t        dd|  d| d|  g        y )Nz!unsupported __getitem__ (sq_item)zsq_item_impl rO   z.Dynamo does not know how to handle sq_item on rQ   r   rF  s      r;   sq_item_implzVariableTracker.sq_item_impl  s,     	7#D63%0HO		
r=   c           	         t        dd| j                          d|j                          d| j                          dg t        j                         y)z+Called when sq_contains is not implemented.zmissing sq_containsz sq_contains not implemented for z&Dynamo does not know how to check if `z	` is in `r0  rQ   Nr1  )r8   r   items      r;   sq_containszVariableTracker.sq_contains  sW    )6t7L7L7N6OP@AR@SS\]a]l]l]n\ooqr2%112		
r=   c                    t        dd| j                          d| j                          dg t        j                         y )Nmissing_mp_ass_subscriptz&mp_ass_subscript_impl not defined for z0Dynamo does not yet support item assignment on 'rD  rQ   r   r   r   r-  r8   r   r   r   s       r;   mp_ass_subscript_implz%VariableTracker.mp_ass_subscript_impl  sI     	.<T=R=R=T<UVJ4K`K`KbJccef2%112		
r=   c           	     :    t        dd|  d| d| d|  g        y )Nz%unsupported __setitem__ (sq_ass_item)zsq_ass_item_impl rO   z2Dynamo does not know how to handle sq_ass_item on rQ   rI  rQ  s       r;   sq_ass_item_implz VariableTracker.sq_ass_item_impl  s2     	;'vQse1UG<LTFS		
r=   c           	         t        ddt        |       j                   d| j                          d|j                          dg t        j
                         y)z7Sequence concatenation via + operator (sq_concat slot).zmissing sq_concatzsq_concat not defined for z+Dynamo does not yet support concatenating '' and ''rQ   NrE  r8   r   rl   s      r;   sq_concat_implzVariableTracker.sq_concat_impl  sc     	'0d1D1D0EFEdF[F[F]E^^efkf|f|f~e  @A  B2%112		
r=   c           	         t        ddt        |       j                   d| j                          d|j                          dg t        j
                         y)zIIn-place sequence concatenation via += operator (sq_inplace_concat slot).zmissing sq_inplace_concatz"sq_inplace_concat not defined for z/Dynamo does not yet support inplace concat of 'rV  rW  rQ   NrE  rX  s      r;   sq_inplace_concat_implz&VariableTracker.sq_inplace_concat_impl  ss     	/8d9L9L8MNI$J_J_JaIbbijo  kA  kA  kC  jD  DE  F2%112		
r=   c                R   |dk(  rKt        |      dk(  r|sddlm}  ||| |d         S t        ||dt        |       dt        |       d       n-|dk(  rOt        |      d	k(  r|sdd
lm}  ||| |d   |d         S t        ||dt        |       dt        |       d       n|dk(  rKt        |      dk(  r|sddlm}  ||| |d         S t        ||dt        |       dt        |       d       n|dk(  r|s|sddlm}  |||       S |dk(  r|s|sddlm}	  |	||       S |dk(  r|s|s| j                  |      S |dk(  r|s|s| j                  |      S |dk(  r|s|s| j                  |      S |dk(  rZ|sXt        |      dk7  r5t        j                  |dt        |             }
t        t        ||
g       | j                  ||d         S |dk(  rFt        |      dk(  r8|d   j!                         r%|s#| j#                  ||d   j%                               S |dk(  r|s|s| j'                  |      S |dk(  r|s|s| j)                  |      S |dk(  r|s|s| j+                  |      S |dk(  rYt        |      dv rL|sJt-        | d      r|d   }t        |      dkD  r|d   n|j#                  |d      }| j/                  |||      S |d k(  r| j1                  ||d         S |d!k(  r| j1                  ||d   d"#      S |d$k(  r| j3                  ||d         S |d%v rj|st        |      dk7  rt        t        |dt        |       g       dd&lm}m} |d'k(  r ||| |d         S |d(k(  r ||| |d   d"#      S  ||| |d         S |d)k(  r| j9                  ||d         S |d*k(  r| j9                  ||d   d"#      S |d+k(  r| j;                  ||d         S |d,k(  r| j=                  ||d         S |d-k(  r| j=                  ||d   d"#      S |d.k(  r| j?                  ||d         S |d/k(  r| jA                  ||d         S |d0k(  r| jA                  ||d   d"#      S |d1k(  r| jC                  ||d         S |d2k(  r| jE                  ||d         S |d3k(  r| jE                  ||d   d"#      S |d4k(  r| jG                  ||d         S |d5k(  r| jI                  ||d         S |d6k(  r| jI                  ||d   d"#      S |d7k(  r| jK                  ||d         S |d8k(  r| jM                  ||d         S |d9k(  r| jM                  ||d   d"#      S |d:k(  r| jO                  ||d         S |d;k(  r| jQ                  ||d         S |d<k(  r| jQ                  ||d   d"#      S |d=k(  r| jS                  ||d         S |d>k(  r| jU                  ||d         S |d?k(  r| jU                  ||d   d"#      S |d@k(  r|s|sddAlm+}  |||       S |dBk(  r| jY                  ||d         S |dCk(  r| jY                  ||d   d"#      S |dDk(  r| j[                  ||d         S |dEk(  r| j]                  ||d         S |dFk(  r| j]                  ||d   d"#      S |dGk(  r| j_                  ||d         S |t`        v rE|sCt        |      dk7  rt        t        |dt        |       g       | jc                  ||d   |      S |dHk(  rt        |      dk(  ry|sw| j%                         x}re|d   j%                         x}rPte        jf                  |dHd       th        jj                  u r)tl        jn                  jq                  ts        ||            S |dIk(  rft        |      dk(  rX|sV| j!                         rF|d   j%                         }t        j                  || j%                         ju                  |            S dJ| jw                          dK| dLdMg}ty        | tl        jz                        r\|dNv rXty        | j|                  t~        t        t        f      r|j                  dO       |j                  dP       |j                  dQ       t        dRdS|  dT| dT| dT| dU| dV| jw                          dW|X       y )YN__getitem__r%   )
vt_getitemr   z1 args and 0 kwargsz
 args and z kwargs__setitem__r   )generic_setitemz2 args and 0 kwargs__delitem__)generic_delitem__len__)generic_len__str__)generic_strr   __iter____next____contains__zexpected 1 argument, got r;  __getattr__	__index____int__	__float____get__)r%   r   tp_descr_get_implre   __or____ror__Treverse__ior__)__mul____rmul____imul__)slot_wrapper_imulslot_wrapper_mulru  rv  
__lshift____rlshift____ilshift__
__rshift____rrshift____irshift____and____rand____iand____xor____rxor____ixor____floordiv____rfloordiv____ifloordiv____truediv____rtruediv____itruediv____mod____rmod____imod__
__divmod____rdivmod__ri   )generic_hash__add____radd____iadd____sub____rsub____isub____subclasscheck____reduce_ex__r4  r   r5  r6  )rg  rh  zConsider moving the creation of dict view object (e.g. `dict.keys()`, `dict.items()`,) to the compiled region, instead of passing it as an input to the compiled region.a   Dynamo does not fully support tracing builtin iterators (e.g. `map`, `zip`, `enumerate`) passed in from uncompiled to compiled regions (e.g. `torch.compile(fn)(enumerate(...))`). This can happen unintentionally if a previous graph break happens with a builtin iterator in the local scope.aF  List/dict comprehensions in Python <= 3.11 result in implicit function calls, which Dynamo cannot trace as a top level frame. Possible workarounds are (1) use a loop instead of a comprehension, (2) fix any graph breaks in the function above the comprehension, (3) wrap the comprehension in a function, or (4) use Python 3.12+.zUnsupported method callzcall_method rO   z*Dynamo does not know how to trace method `z` of class `rP   rQ   )Dlenobject_protocolr^  r   r`  rb  rd  rf  	repr_implr2  tp_iternext_implr   buildr   r=  rM  r   r  r   nb_index_implnb_int_implnb_float_implhasattrro  
nb_or_implnb_inplace_or_implrx  ry  nb_lshift_implnb_inplace_lshift_implnb_rshift_implnb_inplace_rshift_implnb_and_implnb_inplace_and_implnb_xor_implnb_inplace_xor_implnb_floor_divide_implnb_inplace_floor_divide_implnb_true_divide_implnb_inplace_true_divide_implnb_remainder_implnb_inplace_remainder_implnb_divmod_implr  nb_add_implnb_inplace_add_implnb_subtract_implnb_inplace_subtract_impl_RICHCOMPARE_OPSr   inspectgetattr_staticr   r  r   r&   r  
issubclassr  r   r   UserDefinedObjectVariabler   r   r   r   r   r   )r8   r   r   r   r   r^  r`  rb  rd  rf  r   objownerrx  ry  r  self_py
derived_pyprotocolrU   s                       r;   call_methodzVariableTracker.call_method!  s	    = 4yA~f7!"dDG44%t9+ZF}G<	 ]"4yA~f<&r4a$q'BB%t9+ZF}G<	 ]"4yA~f<&r4a99%t9+ZF}G<	 Y4r4((Y4r4((ZV>>"%%ZV$$R((ZV((,,^#F4yA~%++B2KCPTI;0WX(BcUC##BQ00M!D	QQ**,##BQ(B(B(DEE[ f%%b))YtF##B''[ f%%b))Y3t9#6v t011g#&t9q=Qcoob+6V--b#u==X ??2tAw//Y??2tAw?==Y**2tAw7788Ta(5c$i[AB
 My 'D$q'::z!'D$q'4HH$RtAw77\! &&r4733]"&&r47D&AA]"..r47;;\!&&r4733]"&&r47D&AA]"..r47;;Y ##BQ00Z##BQ#>>Z++BQ88Y ##BQ00Z##BQ#>>Z++BQ88^#,,Ra99_$,,Ra$,GG_$44RaAA]"++BQ88^#++BQ+FF^#33BQ@@Y))"d1g66Z))"d1gt)DDZ11"d1g>>\!&&r4733]"&&r47D&AAZV5D))Y ##BQ00Z##BQ#>>Z++BQ88Y ((T!W55Z((T!Wd(CCZ00T!W==%%f4yA~(5c$i[AB ((T!Wd;;((SY!^F22444"1g88::
: **74GN--. %55<<":w7  O#D	Q'')Aw113H"((D++-;;HE 
 d3356av_M0

 dI??@T N
 F
 $**xJ&GHh LL& LL5 	-"4&$qax@DTF,W[WlWlWnVoopq		
r=   c                4   |j                  d      }|M|j                         s=|j                  || gi       }	 |j                         }|r|j                  || g|i       S | j                  |||||      S # t        $ r | j                  |||||      cY S w xY w)zLPerformance optimization to implement optree.tree_map faster than tracing itis_leaf)r   r   r7  r   r   _tree_map_fallbackcall_tree_map_branch)	r8   r   tree_map_fnmap_fnresttree_map_kwargsis_leaf_varpred_resultleaf_decisions	            r;   call_tree_mapzVariableTracker.call_tree_map  s     &)))4";+G+G+I%33BCK	 + > > @ ++BrBB((
 	
 ' ..# s   A6 6BBc                ,    | j                  |||||      S )zKEmulate optree.tree_map without is_leaf/none_is_leaf checks (handled above))r  )r8   r   r  r  r  r  s         r;   r  z$VariableTracker.call_tree_map_branch=  s&     &&
 	
r=   c                    |j                         }d |_        t        j                  d| ||       |j	                  ||| g||      S )Nc                      y rb   rE   r   r   s     r;   <lambda>z4VariableTracker._tree_map_fallback.<locals>.<lambda>W      r=   z@tree_map fastpath fallback triggered for %s (rest=%s, kwargs=%s)r   _maybe_call_tree_map_fastpathlogdebugr7  )r8   r   r  r  r  r  tree_map_fn_copys          r;   r  z"VariableTracker._tree_map_fallbackN  s\     ',,.9U6		N		
  --T!D!
 	
r=   c           	        |j                  d      }||j                         su|j                  || gi       }	 |j                         }	|	rNt        j                  |D 
cg c]  }
t        j                  ||
       c}
      }|j                  ||| g|i       S | j                  ||||||      S # t        $ r | j                  ||||||      cY S w xY wc c}
w )zOPerformance optimization to implement tree_map_with_path faster than tracing itr  )r   r   r7  r   r   _tree_map_with_path_fallbackr   TupleVariabler   r  call_tree_map_with_path_branch)r8   r   r  r  r  r  keypathr  r  r  kkeypath_vars               r;   call_tree_map_with_pathz'VariableTracker.call_tree_map_with_pathd  s     &)))4";+G+G+I%33BCK
 + > > @ '55;BCa_**2q1C ++Bd0JT0JBOO22
 	
 ' 88#  Ds   B/ C/CCc           	         t        j                  |D cg c]  }t        j                  ||       c}      }|j	                  ||| g|i       S c c}w )z;Handle tree_map_with_path for leaf nodes (default behavior))r   r  r   r  r7  )	r8   r   r  r  r  r  r  r  r  s	            r;   r  z.VariableTracker.call_tree_map_with_path_branch  sU      --3:;a_""2q);
 ##Bd(BT(BBGG <s   Ac                    |j                         }d |_        t        j                  d| |||       |j	                  ||| g||      S )Nc                      y rb   rE   r  s     r;   r  z>VariableTracker._tree_map_with_path_fallback.<locals>.<lambda>  r  r=   zVtree_map_with_path fastpath fallback triggered for %s (rest=%s, kwargs=%s, keypath=%s)r  )r8   r   r  r  r  r  r  r  s           r;   r  z,VariableTracker._tree_map_with_path_fallback  s_     ',,.9U6		d	
  --T!D!
 	
r=   c                     y rb   rE   )r8   r   s     r;   set_name_hintzVariableTracker.set_name_hint  s    r=   c                    || _         y rb   )r   )r8   r   s     r;   set_source_locationz#VariableTracker.set_source_location  s
    .r=   c                    | S )z=Used by LazyVariableTracker to build the real VariableTrackerrE   rh   s    r;   realizezVariableTracker.realize      r=   c                    | S )zSUsed by LazyVariableTracker to return the real VariableTracker if it already existsrE   rh   s    r;   r   zVariableTracker.unwrap  r  r=   c                     y)z:Used by LazyVariableTracker to indicate an unrealized nodeTrE   rh   s    r;   is_realizedzVariableTracker.is_realized  s    r=   c                    t        dd| j                          dd| j                          dg t        j                         y)u   
        Implements tp_iternext slot semantics.
        Subclasses override this to support next(). Reaching this base is a
        bug — it means tp_iternext is missing for that VariableTracker subclass.
        zMissing tp_iternextznext()z"Dynamo does not support next() on z8. Add tp_iternext_impl to this VariableTracker subclass.rQ   NrP  r   s     r;   r  z VariableTracker.tp_iternext_impl  sT     	)D1134A64T5J5J5L4M NJ J 3%112	
r=   c                    t        dd| j                          d| j                          dg t        j                         y)a  
        Implements the 3.15 _tp_iteritem slot used by the virtual-iterator
        FOR_ITER/SEND fast paths.

        Mirrors CPython's slot signature: takes (self, index) and returns
        (next_value, next_index).  Exhaustion is signaled by raising
        StopIteration via raise_observed_exception, matching how the rest
        of Dynamo signals iterator end.

        ref: https://github.com/python/cpython/blob/f31a89bb901067dd105b00cfa90523cf7ffdbbdd/Include/object.h#L312-L313
        zMissing tp_iteritemz_tp_iteritem on z-Dynamo does not support virtual iteration on z8. Add tp_iteritem_impl to this VariableTracker subclass.rQ   NrP  )r8   r   indexs      r;   tp_iteritem_implz VariableTracker.tp_iteritem_impl  sR     	)&t'<'<'>&?@?((*+ ,JJ 3%112		
r=   c                $    | j                  |      S rb   )r  r   s     r;   next_variablezVariableTracker.next_variable  s    $$R((r=   c                R    t        |j                  xr |j                  |             S rb   )rp   strict_checks_fnr   s     r;   is_strict_modezVariableTracker.is_strict_mode  s#    B''EB,?,?,EFFr=   c                $    | j                          S )z0Whether Dynamo allows mutation on this variable.)is_immutablerh   s    r;   
is_mutablezVariableTracker.is_mutable  s    $$&&&r=   c                    | j                   du S )z.Whether Dynamo bans mutation on this variable.N)r   rh   s    r;   r  zVariableTracker.is_immutable  s    !!T))r=   c                P   | t         j                  j                  | |      S |r t        j                  | |      |      S t	        |      t
        j                  j                  v r t
        j                  j                  ||      S t
        j                  j                  |||       S )z=Create a new VariableTracker from a value and optional Source)r   )	builderSourcelessBuilderr  VariableBuilderr   r   LazyConstantVariablesupported_typesr   )r   r   r:   r  s       r;   r  zVariableTracker.build  s     >,,33B>>67**2v6u==%[I::JJJ 1188GG0077v"7MMr=   c                x    | j                   r.t        |j                  j                  | j                               S y)u  Return id() of the underlying Python object, or None if unavailable.

        The base implementation uses source resolution for sourceful VTs.
        Subclasses override for special cases (e.g. NNModuleVariable uses
        get_submodule, ConstantVariable handles singletons).

        Returns None for sourceless VTs — callers use FakeIdVariable in
        that case (see generic_id in object_protocol.py).
        N)r:   rg   r   r   r   s     r;   get_idzVariableTracker.get_id  s,     ;;bii44T[[ABBr=   c                <    | j                   rt        j                  S y rb   )r:   r   ID_MATCHrh   s    r;   get_id_guard_typez!VariableTracker.get_id_guard_type  s    ;;(((r=   c                N    	 | j                         S # t        $ r	 t        cY S w xY w)zReturn the Python object this VT wraps, for `is` comparison.

        Returns NO_SUCH_SUBOBJ if no concrete Python object is available.
        )r   r   r   rh   s    r;   get_real_python_backed_valuez,VariableTracker.get_real_python_backed_value"  s+    	"**,," 	"!!	"s    $$c                t    t        dd|  d|  d| j                          dgt        j                         y)z
        NB - Deliberately not overriding the __eq__ method because that can
        disable the __hash__ for the vt itself.
        z<Dynamo cannot determine the equality comparison of an objectzis_python_equal zQDynamo does not know the equality comparison of the underlying python object for zKConsider using a different type of object as the dictionary key instead of r   rQ   N)r   r   r   r-  rk   s     r;   is_python_equalzVariableTracker.is_python_equal-  sU    
 	R&tf-klpkqr bbfbrbrbtauuvw #..		
	
r=   c                N    t        t        |d| j                          dg       y)a(  Mirrors CPython's PyNumber_Index / nb_index slot.

        https://github.com/python/cpython/blob/c09ccd9c429/Objects/abstract.c#L1411-L1450

        The base implementation raises TypeError, matching CPython's behavior
        when tp_as_number->nb_index is NULL (_PyIndex_Check fails).
        rW  z,' object cannot be interpreted as an integerr;  Nr<  r   s     r;   r  zVariableTracker.nb_index_impl>  s.     	!D))+,,XY	
r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)a  Mirrors CPython's tp_repr slot.

        https://github.com/python/cpython/blob/v3.13.3/Objects/object.c#L745-L778

        Called when type_implements_tp_repr returns True for this type.
        Subclasses override to provide the actual repr implementation.
        zrepr_impl not implementedz+ has tp_repr slot but no repr_impl override	The type zX has a tp_repr C slot but the corresponding VariableTracker doesn't implement repr_impl.rQ   NrE  r   s     r;   r  zVariableTracker.repr_implQ  sQ     	/Dz**++VW#D$9$9$;#< =M M2%112	
r=   c           
         t        dt        |       j                   d| j                          d| j                          dt        |       j                   dg t        j
                         y)zDynamo hook for VariableTrackers with dedicated str behavior.
        Subclasses override this for the per-type str result; generic_str()
        handles dispatch and repr fallback.
        zstr_impl not implementedz has no str_impl override for z&Dynamo does not implement __str__ for z in r   rQ   NrE  r   s     r;   str_implzVariableTracker.str_impld  sp     	.Dz**++I$J_J_JaIbc@AVAVAX@Y Zt*%%&a)2%112	
r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)zMirrors CPython's tp_as_number->nb_int slot.

        Called when type_implements_nb_int returns True for this type.
        Subclasses override to provide the actual conversion.
        znb_int_impl not implementedz, has nb_int slot but no nb_int_impl overrider  zZ has an nb_int C slot but the corresponding VariableTracker doesn't implement nb_int_impl.rQ   NrE  r   s     r;   r  zVariableTracker.nb_int_implt  sQ     	1Dz**++WX#D$9$9$;#< =O O2%112	
r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)zMirrors CPython's tp_as_number->nb_float slot.

        Called when type_implements_nb_float returns True for this type.
        Subclasses override to provide the actual conversion.
        znb_float_impl not implementedz0 has nb_float slot but no nb_float_impl overrider  z^ has an nb_float C slot but the corresponding VariableTracker doesn't implement nb_float_impl.rQ   NrE  r   s     r;   r  zVariableTracker.nb_float_impl  sQ     	3Dz**++[\#D$9$9$;#< =Q Q2%112	
r=   c                    t        d| d| j                          d|j                          d| dd| j                          d| g t        j                         y )	Nz!tp_as_number slot not implemented(r,  z
, reverse=r  r  z does not implement the rQ   rP  )r8   tp_slot_namerl   rs  s       r;   _nb_slot_not_implementedz(VariableTracker._nb_slot_not_implemented  so     	7#nAd&;&;&=%>bAWAWAY@ZZdeldmmno#D$9$9$;#<<TUaTbc2%112		
r=   c                4    t        j                  t              S )ztp_as_number->nb_lshift slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rlshift__`` instead of ``__lshift__``).
        r   r&   NotImplementedr8   r   rl   rs  s       r;   r  zVariableTracker.nb_lshift_impl       )).99r=   c                4    t        j                  t              S )zFtp_as_number->nb_inplace_lshift slot. Default: returns NotImplemented.r  rX  s      r;   r  z&VariableTracker.nb_inplace_lshift_impl       )).99r=   c                4    t        j                  t              S )ztp_as_number->nb_rshift slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rrshift__`` instead of ``__rshift__``).
        r  r  s       r;   r  zVariableTracker.nb_rshift_impl  r  r=   c                4    t        j                  t              S )zFtp_as_number->nb_inplace_rshift slot. Default: returns NotImplemented.r  rX  s      r;   r  z&VariableTracker.nb_inplace_rshift_impl  r  r=   c                4    t        j                  t              S )ztp_as_number->nb_and slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rand__`` instead of ``__and__``).
        r  r  s       r;   r  zVariableTracker.nb_and_impl  r  r=   c                4    t        j                  t              S )zCtp_as_number->nb_inplace_and slot. Default: returns NotImplemented.r  rX  s      r;   r  z#VariableTracker.nb_inplace_and_impl  r  r=   c                4    t        j                  t              S )ztp_as_number->nb_xor slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rxor__`` instead of ``__xor__``).
        r  r  s       r;   r  zVariableTracker.nb_xor_impl  r  r=   c                4    t        j                  t              S )zCtp_as_number->nb_inplace_xor slot. Default: returns NotImplemented.r  rX  s      r;   r  z#VariableTracker.nb_inplace_xor_impl  r  r=   c                4    t        j                  t              S )ztp_as_number->nb_floor_divide slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rfloordiv__`` instead of ``__floordiv__``).
        r  r  s       r;   r  z$VariableTracker.nb_floor_divide_impl  r  r=   c                4    t        j                  t              S )zLtp_as_number->nb_inplace_floor_divide slot. Default: returns NotImplemented.r  rX  s      r;   r  z,VariableTracker.nb_inplace_floor_divide_impl  r  r=   c                4    t        j                  t              S )ztp_as_number->nb_true_divide slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rtruediv__`` instead of ``__truediv__``).
        r  r  s       r;   r  z#VariableTracker.nb_true_divide_impl  r  r=   c                4    t        j                  t              S )zKtp_as_number->nb_inplace_true_divide slot. Default: returns NotImplemented.r  rX  s      r;   r  z+VariableTracker.nb_inplace_true_divide_impl  r  r=   c                4    t        j                  t              S )ztp_as_number->nb_remainder slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rmod__`` instead of ``__mod__``).
        r  r  s       r;   r  z!VariableTracker.nb_remainder_impl!  r  r=   c                4    t        j                  t              S )zItp_as_number->nb_inplace_remainder slot. Default: returns NotImplemented.r  rX  s      r;   r  z)VariableTracker.nb_inplace_remainder_impl.  r  r=   c                4    t        j                  t              S )ztp_as_number->nb_divmod slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rdivmod__`` instead of ``__divmod__``). divmod has no
        in-place form.
        r  r  s       r;   r  zVariableTracker.nb_divmod_impl6       )).99r=   c                *    | j                  d||      S )af  tp_as_number->nb_multiply slot. Default: graph-breaks.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rmul__`` instead of ``__mul__``).  The base default
        graph-breaks so unmigrated VTs surface loudly; ``binary_op1`` only
        invokes this on types whose CPython type advertises the slot.
        nb_multiply_implrr  r  r  s       r;   r-  z VariableTracker.nb_multiply_implD  s     ,,-?PW,XXr=   c                &    | j                  d|      S )z>tp_as_number->nb_inplace_multiply slot. Default: graph-breaks.nb_inplace_multiply_implr.  rX  s      r;   r0  z(VariableTracker.nb_inplace_multiply_implS  s     ,,-GOOr=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)ztp_as_sequence->sq_repeat slot.

        Implements sequence repetition (e.g. ``[1, 2] * 3``).  ``count`` is
        an int VariableTracker already produced by ``nb_index_impl``.
        zsq_repeat_impl not implementedz2 has sq_repeat slot but no sq_repeat_impl overrider  z` has an sq_repeat C slot but the corresponding VariableTracker doesn't implement sq_repeat_impl.rQ   NrE  r8   r   counts      r;   sq_repeat_implzVariableTracker.sq_repeat_impl[  sQ     	4Dz**++]^#D$9$9$;#< =R R2%112	
r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)z'tp_as_sequence->sq_inplace_repeat slot.z&sq_inplace_repeat_impl not implementedzB has sq_inplace_repeat slot but no sq_inplace_repeat_impl overrider  zp has an sq_inplace_repeat C slot but the corresponding VariableTracker doesn't implement sq_inplace_repeat_impl.rQ   NrE  r2  s      r;   sq_inplace_repeat_implz&VariableTracker.sq_inplace_repeat_implm  sQ     	<Dz**++mn#D$9$9$;#< =Z Z2%112	
r=   c                4    t        j                  t              S )ztp_as_number->nb_or slot. Default: returns NotImplemented.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__ror__`` instead of ``__or__``).
        r  r  s       r;   r  zVariableTracker.nb_or_impl{  r  r=   c                4    t        j                  t              S )zBtp_as_number->nb_inplace_or slot. Default: returns NotImplemented.r  rX  s      r;   r  z"VariableTracker.nb_inplace_or_impl  r  r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)zMirrors CPython's tp_as_number->nb_negative slot.

        Called when type_implements_nb_negative returns True for this type.
        Subclasses override to provide the actual negation.
        z nb_negative_impl not implementedz6 has nb_negative slot but no nb_negative_impl overrider  zd has an nb_negative C slot but the corresponding VariableTracker doesn't implement nb_negative_impl.rQ   NrE  r   s     r;   nb_negative_implz VariableTracker.nb_negative_impl  Q     	6Dz**++ab#D$9$9$;#< =T T2%112	
r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)zMirrors CPython's tp_as_number->nb_positive slot.

        Called when type_implements_nb_positive returns True for this type.
        Subclasses override to provide the actual positive.
        z nb_positive_impl not implementedz6 has nb_positive slot but no nb_positive_impl overrider  zd has an nb_positive C slot but the corresponding VariableTracker doesn't implement nb_positive_impl.rQ   NrE  r   s     r;   nb_positive_implz VariableTracker.nb_positive_impl  r;  r=   c                4    t        j                  t              S )ztp_as_number->nb_add slot. Default: graph break.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__radd__`` instead of ``__add__``).
        r  r  s       r;   r  zVariableTracker.nb_add_impl  r+  r=   c                4    t        j                  t              S )z8tp_as_number->nb_inplace_add slot. Default: graph break.r  rX  s      r;   r  z#VariableTracker.nb_inplace_add_impl  s     )).99r=   c                4    t        j                  t              S )ztp_as_number->nb_subtract slot. Default: graph break.

        ``reverse=True`` means self is the right-hand operand (CPython would
        look up ``__rsub__`` instead of ``__sub__``).
        r  r  s       r;   r  z VariableTracker.nb_subtract_impl  r  r=   c                4    t        j                  t              S )z=tp_as_number->nb_inplace_subtract slot. Default: graph break.r  rX  s      r;   r  z(VariableTracker.nb_inplace_subtract_impl  r  r=   c                    t        dt        |       j                   dd| j                          dg t        j
                         y)zMirrors CPython's tp_as_number->nb_absolute slot.

        Called when type_implements_nb_absolute returns True for this type.
        Subclasses override to provide the actual absolute value.
        z nb_absolute_impl not implementedz6 has nb_absolute slot but no nb_absolute_impl overrider  zd has an nb_absolute C slot but the corresponding VariableTracker doesn't implement nb_absolute_impl.rQ   NrE  r   s     r;   nb_absolute_implz VariableTracker.nb_absolute_impl  r;  r=   )r:   r   r   c                  t         |           || _        || _        || _        |_t        |t        t        f      r|t        d      y t        |t        t        f      st        dt        |             |t        d      y y )Nz=source must be None for ValueMutationNew/AttributeMutationNewzAExpected ValueMutationExisting or AttributeMutationExisting, got zKsource must not be None for ValueMutationExisting/AttributeMutationExisting)rc   rY   r:   r   r   r   r`   r   AssertionErrorrt   r|   r   )r8   r:   r   r   re   s       r;   rY   zVariableTracker.__init__  s     	.* $-*:<P)QR %(W  &
 "!$9;T#U )#M235  >(e  ") %r=   c                    t        |   di | dd}t        j                  | d|       dd}t        j                  | d|       y)a  
        Wraps all subclasses' `as_python_constant` and `reconstruct` so that it cannot be
        called twice in the same call chain - i.e. self-referential objects.
        For `as_python_constant` - self-referential objects are NOT treated as constants.
        For `reconstruct` - we will graph break. The graph break can be avoided if the VT subclass
        can generate and cache itself before recursively `reconstruct`ing - see ListVariable for an example.
        c                "    t        | |  d      )Nz is self-referential)r   r   rh   s    r;   as_python_constant_failurezEVariableTracker.__init_subclass__.<locals>.as_python_constant_failure   s    5TF"67 r=   r   c                d    t        dt        |       d|  dddgt        j                  d       y )Nz)Reconstruction failure (self-referential)z0Dynamo tried to reconstruct sourceless variable z~, but it is self-referential. Dynamo must manually implement reconstruction rules for self-referentiable sourceless variables.zIf Dynamo is attempting to trace a return statement and your code is attempting to return a variable that Dynamo cannot reconstruct, then remove it from the return statement.zhRemove the self-reference in the variable. A self-referring list, for example, is `l = []; l.append(l)`.zPReport an issue to PyTorch if you need self-referential reconstrtuction support.rQ   )r   r+   r   CAUSED_BY_EARLIER_GRAPH_BREAKrh   s    r;   reconstruct_failurez>VariableTracker.__init_subclass__.<locals>.reconstruct_failure)  sQ    CD	Ntf Us s`~ 'DD	
 gr=   r$  NrE   )r?   r   )rc   __init_subclass__r   _add_call_once_guard)r   r   rH  rK  re   s       r;   rL  z!VariableTracker.__init_subclass__  sM     	!+F+	
 	,,%'A	
	 	,,S-ATUr=   c                    t        | |      t        t        |      u st        d      ry t        j                        dfd       }d|_        t        | ||       y )N_call_once_guardedc                <   t         j                         }|t               }t         j                  |       t        |       t              f}||v r |        |j	                  |       	  | g|i ||j                  |       S # |j                  |       w xY wrb   )rF   r   setrg   adddiscard)r8   r   r   activer   callbackoriginal_methods        r;   guarded_methodz<VariableTracker._add_call_once_guard.<locals>.guarded_methodG  s    %))+F~ $$V,d8R01Cf}JJsO$&t=d=f=s#s#s   +B BT)r   r
   r   r
   r?   r   )r   r   r  	functoolswrapsrO  setattr)r   methodrU  rW  rV  s     ` @r;   rM  z$VariableTracker._add_call_once_guard:  sd     "#v.gov>>'1C
 		)	$ 
*	$  -1)V^,r=   )r   r
   r?   r   )NN)
r   z!Callable[[VariableTracker], None]r   r
   r   zdict[int, Any] | Noner   zSideEffects | Noner?   rZ   r>   )r?   r   )r?   r
   )r?   rp   )r   r$   r?   VariableTracker | None)r   r$   r?   ztuple[int, bool])r   r$   rl   r   r   r+   r?   r   )r   r
   r?   rp   )r   zCallable[..., Any]r?   r    )r   r$   r   r+   r?   r
   )r?   type[VariableTracker])r   r$   r   r+   r?   r   )r?   zNode | None)r#  r"   r?   rZ   )r#  r"   r?   r+   )r   r
   r?   list[VariableTracker])r   r$   r   r+   r?   r&   )r   r$   r?   r   )r   r
   r   r^  r   dict[str, VariableTracker]r?   r   )r   r
   r?   r   )r   r$   r   r   r?   r   )r   r
   rL  r   r?   r   )r   r$   r   r   r   r\  r?   r   )r   r$   rl   r   r?   r   )
r   r
   r   r+   r   r^  r   r_  r?   r   )r   r
   r  r'   r  r   r  r^  r  r_  r?   r   )r   r
   r  r'   r  r   r  r^  r  r_  r  ztuple[Any, ...]r?   r   )r   r+   r?   rZ   )r   r*   r?   rZ   )r?   r   )r   r$   r  r   r?   z'tuple[VariableTracker, VariableTracker])r   r
   r?   rp   )NF)
r   r
   r   r
   r:   r   r  rp   r?   r
   )r   r$   r?   r/   )r?   zCallable[..., Any] | None)r?   ro   rn   rw   )r  r+   rl   r   rs  rp   )r   r
   rl   r   rs  rp   r?   r   )r   r
   rl   r   r?   r   )r   r$   rl   r   rs  rp   r?   r   )r   r$   r3  r   r?   r   )r:   r   r   zMutationType | Noner   zSourceLocation | Noner?   rZ   )r   r
   r?   rZ   )r   r]  r[  r+   rU  z Callable[[VariableTracker], Any]r?   rZ   )nr@   rA   rB   rC   r   rD   r   r   classmethodr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r	  r  r  r  r  r   r$  r'  r)  r.  r2  r7  r>  rA  rG  rJ  rM  rR  rT  rY  r[  r  r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r  r  staticmethodr  r  r  r	  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r-  r0  r4  r6  r  r  r:  r=  r  r  r  r  rC  rY   rL  rM  rq   rr   s   @r;   r   r   @  s/
    59M18N& 
 (,+/'=-'= '= %	'=
 )'= 
'= '=R.G:$8
!F
%
 
 	

 

8	"
"
G-	 "T"
+
36
	



 $
 +	

 

 


%
 
 
	
 
%
 
 
	


%
 
 &	

 


%
 
 &	

 


%
 
 
	

%
 
 
	
y
y
 y
 $	y

 +y
 
y
v

 *
  	

 $
 4
 

B

 *
  	

 $
 4
 

"

 *
  	

 $
 4
 

,%
%
 *%
  	%

 $%
 4%
 !%
 
%
NHH *H  	H
 $H 4H !H 
H

 *
  	

 $
 4
 !
 

0/
 
+
4C
	0
2)G'*  !%	NNN N 	N
 
N N&
	"
"

 

&

 

&

 

 

 

"

 

* 	

 
 	
" 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	: 	:: : 	:
 
:$ 	Y%Y Y 	Y
 
YP%P P 
	P
%
 
 
	
$
%
 
 
	
$ 	:: : 	:
 
::: : 
	:

 

"

 

* 	:: : 	:
 
::: : 
	: 	:: : 	:
 
::: : 
	:

 

( !%-115% % +	%
 /% 
%N"VH -"-- 3- 
	- -r=   r   )	metaclassc                     t        |       dk(  r4| \  }t        |t              rt        |      S t	        |      j
                  S dj                  t        t        |             S )Nr%   rO   )	r  r   r   r+   r   r@   joinmaptypestr)objsr  s     r;   rf  rf  \  sM    
4yA~c?+s8O9%%%xxGT*++r=   z/https://github.com/python/cpython/blob/v3.13.0/c            	        ddl m} m}m}m}m}m}m}m}m	}m
}	m}
m}m}m}m}m}m} i }t$        j&                  D ]]  }|j(                  j+                  d      }|!t-        |t.              r|n|f}|D ]#  }|j1                  |g       j3                  |       % _ t5        |j7                         d       D ]>  \  }}|D ]4  }t9        |j:                   d|j<                  dd	|j>                          6 @ y)
zPrint the mapping from CPython types to Dynamo VariableTracker classes.

    Reads _cpython_type from each VT subclass (own attribute only, not inherited).
    r%   )builtinconstantctx_managerdictsdistributed	functionshigher_order_opsiterlazylistsmisc	nn_modulestreamstensorr  torch_functionuser_definedr   Nc                     | d   j                   S r  )rB   )xs    r;   r  z-print_cpython_to_vt_mapping.<locals>.<lambda>  s    QqTEVEV r=   )r   r   30sz -> )  ri  rj  rk  rl  rm  rn  ro  rp  rq  rr  rs  rt  ru  rv  r  rw  rx  r   r   r   r   r   r   
setdefaultr   sortedr   printrA   rB   r@   )ri  rj  rk  rl  rm  rn  ro  rp  rq  rr  rs  rt  ru  rv  r  rw  rx  mappingvt_clscpython_typetypestpy_type
vt_classess                           r;   print_cpython_to_vt_mappingr  m  s        ( ')G%44 5**?; *< ?l_ 	5Aq"%,,V4	55  &gmmo;VW   	F%%&a(<(<S'AfooEVW	r=   )r  )r   )r   r-   r?   rp   )r   rM   r?   rp   )rg  ro   r?   r+   rm   )WrC   
__future__r   r   dataclassesrX  r  loggingr5   collections.abcr   r   r   r   contextvarsr   enumr	   typingr
   r   r   r|  r   r   r   excr   r   r   r   r   r:   r   r   utilsr   r   r   	frozensetr  torch._guardsr    torch.fx.proxyr!   r#  r"   r   r#   symbolic_convertr$   rj  r&   rn  r'   	getLoggerr@   r  	dataclassr*   rF   rD   rI   rM   r\   r`   rt   ry   r|   r   r   r   r   r   r   r   r   r   r   rf  r   r   _CPYTHON_BASE_URLr  r  rq  r   rE   r=   r;   <module>r     s   #       E E "  / / + / 9 0 ' D D @ 
 ###*<*/ g! d#  $: =G= 9 
 &- -`t | ,'L ', . 1 . %, %"	F F*= 7$ 7:-+> -Y- 3 Y-x0, && F 'T  %r=   