
    +j                    n   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Zd dl	Z	d dl
Zd dlZd dlmZ d dlmZ d dlmZ d dlmZ d dlmZmZmZmZmZmZ d dlmZ d dlZd d	lmZ d d
l m!Z! d dl"m#Z# d dl$m%Z%m&Z& d dl'm(Z(  ej)        e*          Z+er8d dl,m-Z-m.Z.m/Z/ d dl0m1Z1 d dl2Z2d dl3m4Z4 d dl5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< d dl=m>Z> 	  ej?        d          Z@ ej?        d          ZA eddd           G d d                      ZB G d de          ZC G d dejD                  ZE	  G d  d!          ZF ej        d"           G d# d$                      ZG G d% d&e          ZH ej        d'           G d( d)                      ZI ed*          ZJ	  ej        d"           G d+ d,                      ZK	  ej        d"           G d- d.eK                      ZL	  ej        d"           G d/ d0eK                      ZM	  G d1 d2eeJ                   ZN G d3 d4          ZO G d5 d6          ZP G d7 d8eNeP                   ZQ G d9 d:          ZR G d; d<eNeR                   ZS G d= d>          ZT	  G d? d@eNeO                   ZU G dA dB          ZVe G dC dD                      ZWe G dE dF                      ZX G dG dHeV          ZY G dI dJ          ZZ ej[                    Z\	  G dK dL          Z]e G dM dN                      Z^ G dO dP          Z_edldU            Z`edmdX            Zaednd]            Zbe	 dodpd`            Zb e            	 dodqdc            Zb ebd"           G dd de                      Zc ebd"           G df dgec                      ZddodrdjZedsdkZfdS )t    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyGeneric
NamedTupleoverloadTYPE_CHECKINGTypeVar)dataclass_transform)_pytree)
OrderedSet)is_traceable_wrapper_subclass)CapturedTracebackformat_frame)WeakTensorKeyDictionary)Callable	GeneratorIterator)CodeType)DDPOptimizerContext)	PyCodegen)GuardCheckSpec)ViewAndMutationMeta)NestedCompileRegionOptionsFakeTensorModez-^(?P<frame_id>\d+)/(?P<frame_compile_id>\d+)$zQ^!(?P<compiled_autograd_id>\d+)(?:/(?P<frame_id>\d+)/(?P<frame_compile_id>\d+))?$T)frozenkw_onlyslotsc                  R    e Zd ZU ded<   ded<   dZded<   ddZedd            ZdS )	CompileId
int | Noneframe_idframe_compile_idNcompiled_autograd_idreturnstrc                @   | j         Z| j        d u | j        d u k    rt          d| j         d| j                   d}| j        d| j         d| j         }d| j          | S | j        | j        t          d| j         d| j                   | j         d| j         S )NzMframe_id and frame_compile_id must both be None or both be set, got frame_id=z, frame_compile_id= /!z=frame_id and frame_compile_id must not be None, got frame_id=)r(   r&   r'   AssertionError)self	frame_strs     P/home/wildlama/comfy/ComfyUI/.venv/lib/python3.11/site-packages/torch/_guards.py__str__zCompileId.__str__U   s   $0%4+@D+HII$^$(M^ ^FJF[^ ^   I}(GGG0EGG	=t0=)===}$(=(E$^$(M^ ^FJF[^ ^   m==d&;===    
compile_id
str | NoneCompileId | Nonec                N   |dS 	 t           t          fD ]e}|                    |          x}rL|                                }|                                D ]\  }}|t          |          ||<    | di |c S ft          # t          $ r}t          d| d          |d}~ww xY w)z
        Factory method that creates a CompileId from its string representation.
        Keep this in sync with the __str__ method.
        NzInvalid compile_id '' )COMPILE_ID_PATTERNCA_COMPILE_ID_PATTERNmatch	groupdictitemsint
ValueError	Exception)clsr5   patternr=   groupskves           r2   from_stringzCompileId.from_stringj   s     4	J.0EF ! !#MM*5555 )"__..F & / /1=(+AF1I3====((() !  	J 	J 	JAJAAABBI	Js   A2B 9B 
B$BB$r)   r*   )r5   r6   r)   r7   )__name__
__module____qualname____annotations__r(   r3   classmethodrI   r:   r4   r2   r$   r$   F   s~         
 !    (,++++
> > > >* J J J [J J Jr4   r$   c                  ,    e Zd ZU ded<   ded<   d	dZdS )
TraceIdr$   r5   r@   attemptr)   r*   c                b    | j         dk    rt          | j                  S | j         d| j          S )Nr   _)rR   r*   r5   r0   s    r2   r3   zTraceId.__str__   s7    <1t'''o66666r4   NrJ   rK   rL   rM   rN   r3   r:   r4   r2   rQ   rQ      sA          LLL7 7 7 7 7 7r4   rQ   c                  z    e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdZdZdZdZdZdZddZddZddZddZddZdS )GuardSourcer                           	   
                     r)   boolc                6    | t           j        t           j        fv S N)rX   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULErU   s    r2   is_fsdp_modulezGuardSource.is_fsdp_module   s    68UVVVr4   c                    dd l mc m} |j        r.| t          j        t          j        fv p|                                 S | t          j        t          j        fv S Nr   )torch._dynamo.config_dynamoconfig_unsafe_skip_fsdp_module_guardsrX   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErn   )r0   rs   s     r2   is_specialized_nn_modulez$GuardSource.is_specialized_nn_module   s|    ---------1 	<; )
 &&(( 43
 
 	
r4   c                b    | t           j        t           j        t           j        t           j        fv S rk   )rX   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErU   s    r2   is_unspecialized_nn_modulez&GuardSource.is_unspecialized_nn_module   s,    65>=	
 
 	
r4   c                6    | t           j        t           j        fv S rk   )rX   r{   r|   rU   s    r2   "is_unspecialized_builtin_nn_modulez.GuardSource.is_unspecialized_builtin_nn_module   s     >=
 
 	
r4   c                x    | t           j        t           j        t           j        t           j        t           j        fv S rk   )rX   LOCALrv   rm   rz   r|   rU   s    r2   is_localzGuardSource.is_local   s2    3)5=
 
 	
r4   Nr)   ri   )rK   rL   rM   r   GLOBALrv   ru   CONSTANTRANDOM_VALUE	SHAPE_ENVrm   rl   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrz   ry   r|   r{   
TEMP_LOCALrn   rw   r}   r   r   r:   r4   r2   rX   rX      s        EF"##$ HLINIO$&!%'",.)-/*JW W W W
 
 
 
"
 
 
 

 
 
 

 
 
 
 
 
r4   rX   c                      e Zd ZdS )GuardBuilderBaseNrK   rL   rM   r:   r4   r2   r   r      s        Dr4   r   )r    c                  ,    e Zd ZU ded<   ded<   d	dZdS )
SLocz#traceback.FrameSummary | str | Noneframework_locr6   maybe_user_locr)   r*   c                    t          | j        t                    r| j        nt          | j                  }| j        | j         d| dS d| dS )Nz ()()
isinstancer   r*   r   r   )r0   flocs     r2   r3   zSLoc.__str__   sg     $,c222Dd011 	
 *)44T4444t;;;r4   NrJ   rV   r:   r4   r2   r   r      sB         6666	 	 	 	 	 	r4   r   c                  .    e Zd ZU ded<   ded<   ded<   dS )
ShapeGuardzsympy.logic.boolalg.Booleanexprr   slocri   size_obliviousNrK   rL   rM   rN   r:   r4   r2   r   r      s3         %%%%JJJr4   r   )r"   c                  <   e Zd ZU ded<   ded<   dZded<   dZded<   dZd	ed
<   dZded<   dZded<   dZ	ded<   dZ
ded<   dZded<   d4dZd5dZd6dZd7dZed8d             Zed9d"            Zed:d$            Zd8d%Zd8d&Zd;d*Zd<d+Zd<d,Zd<d-Zd8d.Zd=d3ZdS )>GuardSourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]	create_fnNzlist[str] | Noneguard_types	code_listzobject | Noneobj_weakref!weakref.ReferenceType[Any] | Noneguarded_class_weakrefzCapturedTraceback | Nonestackztraceback.StackSummary | None
user_stackr%   _hashFri   _unserializabler)   r@   c                    | j         3t          | j        | j        t	          | j                  f          | _         | j         S rk   )r   hashnamesourceidr   rU   s    r2   __hash__zGuard.__hash__  s6    :ty$+r$.7I7IJKKDJzr4   tuple[bool, int, int, str, int]c                (   t          | j        t          j                  o&| j        j        t
          j        j        j        j	        u }|| j
        r| j
        j        ndt          | j                  | j        |                                 j        j        fS )N)r   r   	functoolspartialfunctorchrr   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r0   is_duplicate_inputs     r2   sort_keyzGuard.sort_key  s    
 t~y'899 Y#u}';'H'XX 	
 !%4DK"	NNI  ""+:
 	
r4   otherc                V    |                                  |                                 k     S rk   )r   r0   r   s     r2   __lt__zGuard.__lt__-  s    }}!1!111r4   (Callable[[GuardBuilderBase, Guard], Any]c                f    t          | j        t          j                  r| j        j        S | j        S rk   )r   r   r   r   r   rU   s    r2   r   zGuard.inner_create_fn0  s,    dni&788 	">&&>!r4   r*   c                    | j         j        S rk   )r   r   rU   s    r2   r   z
Guard.name6  s    &++r4   rX   c                    | j         j        S rk   )r   guard_sourcerU   s    r2   r   zGuard.source:  s    &33r4   objectc           
     @   t          | t          j                  rv |             }|Jdt          t	          |                      d|j        j         dt          t	          |                     dS dt          t	          |                      dS t          |           S )a  
        This is a workaround of a Python weakref bug.

        `obj_weakref` is instance returned by `weakref.ref`,
        `str(obj_weakref)` is buggy if the original obj overrides __getattr__, e.g:

            class MyConfig(dict):
                def __getattr__(self, x):
                    return self[x]

            obj = MyConfig(offset=5)
            obj_weakref = weakref.ref(obj)
            str(obj_weakref)  # raise error: KeyError: '__name__'
        Nz<weakref at z; to 'z' at >z; dead>)r   weakrefReferenceTypehexr   	__class__rK   r*   )r   objs     r2   weakref_to_strzGuard.weakref_to_str>  s      k7#899 	$+--Cnc"[//&:&:nn#-BXnn_bceficjcj_k_knnnnCc"[//&:&:CCCC{###r4   c                    d| j         r| j         j                                        nd dt          | j                   d|                                 j         d| j         d| j         d|                     | j	                   d| j
         d}|S )	Nz	
        r,    z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )r   r   lowerreprr   rK   r   r   r   r   r   )r0   ss     r2   __repr__zGuard.__repr__W  s    %)[	8				!	!	!b ;?	?? MQMaMaMcMcMl  !,  ^	 
 !//0@AA  #8   r4   c                l   dt          | j                   d}| j        r| j        j                                        nd}|d| dz  }|d|                                 j         dz  }|d| j         dz  }|d| j         dz  }|d|                     | j	                   dz  }|d	| j
         dz  }|S )
NzName: 
r,   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )r   r   r   r   r   rK   r   r   r   r   r   )r0   outputr   s      r2   r3   zGuard.__str__c  s    -$ty//----1[@!'')))b+++++M$*>*>*@*@*IMMMM:d&6::::6DN6666R)<)<T=M)N)NRRRRN0JNNNNr4   builderr   r   c           	        	 |                      ||           S # t          $ r t                              dt	          |                                                      | j        r_t                              dd                    | j        	                                dd                                                                w xY w)NzError while creating guard:
%szCreated at:
%sr,   )
r   rB   log	exceptionr*   rstripr   errorjoinformat)r0   r   s     r2   createzGuard.createn  s    	>>'4000 	 	 	MM;SYY=M=M=O=OPPPz Y		+RWWTZ5F5F5H5H5M-N-N-U-U-W-WXXX		s
    B,Cc                4    | j                                         S rk   )r   rw   rU   s    r2   rw   zGuard.is_specialized_nn_modulew  s    {33555r4   c                4    | j                                         S rk   )r   rn   rU   s    r2   rn   zGuard.is_fsdp_modulez  s    {))+++r4   c                4    | j                                         S rk   )r   r   rU   s    r2   r   zGuard.is_local}  s    {##%%%r4   c                v    t          | j        t          j                  r| j        j        }n| j        }|j        S rk   )r   r   r   r   r   rK   )r0   r   s     r2   create_fn_namezGuard.create_fn_name  s5    dni&788 	'+III!!r4   
guard_typeguarded_class	list[str]Nonec                   | j         sg | _         | j                             |           | j        |d fvrt          d| j         d|           || _        | j        s|| _        n| j                            |           | j        |d fv p)t          | j                  o|                                 d u }|st          d| j         d|           || _        d S )Nz1Guarded class id must be identical, or None, got z vs zHGuarded object must be identical, None or ephemeral (dead weakref), got )r   appendr   r/   r   extendr   callable)r0   r   r   r   r   is_valids         r2   set_export_infozGuard.set_export_info  s>     	"!D
+++%mT-BBB G1G G7DG G   &3"~ 	-&DNNN!!),,, d 33 +()) +  ""d* 	
  	 ;'; ;-8; ;   'r4   r)   r@   )r)   r   )r   r   r)   ri   )r)   r   rJ   r)   rX   )r   r   r)   r*   )r   r   r)   r   r   )
r   r*   r   r   r   r   r   r   r)   r   )rK   rL   rM   rN   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r3   r   rw   rn   r   r   r   r:   r4   r2   r   r      s        & 8888 %)K(((("&I&&&&!%K%%%%?CCCCC&*E****04J4444E!O!!!!   

 
 
 
 2 2 2 2" " " " , , , X, 4 4 4 X4 $ $ $ \$0
 
 
 
	 	 	 	   6 6 6 6, , , ,& & & &" " " "&' &' &' &' &' &'r4   r   Tc                      e Zd ZdS )GuardEnvExprNr   r:   r4   r2   r   r     s        Dr4   r   c                  ,    e Zd ZU ded<   ded<   ddZdS )	DuplicateInputsr   input_source_ainput_source_br)   r   c                T    | j         | j        k    rt          d| j                    d S )Nz9input_source_a and input_source_b must be different, got )r   r  r/   rU   s    r2   __post_init__zDuplicateInputs.__post_init__  sB    $"555 -*- -   65r4   Nr)   r   )rK   rL   rM   rN   r  r:   r4   r2   r   r     sB              r4   r   c                  $    e Zd ZU ded<   ded<   dS )StorageOverlapzlist[Source]overlapping_sourcesnon_overlapping_sourcesNr   r:   r4   r2   r  r    s*         %%%%))))))r4   r  c                  >    e Zd Zedd            Zed	d            ZdS )
Checkpointabler)   r   c                    d S rk   r:   rU   s    r2   copy_graphstatezCheckpointable.copy_graphstate  s    $'Cr4   stater   c                    d S rk   r:   r0   r  s     r2   restore_graphstatez!Checkpointable.restore_graphstate  s    47Cr4   N)r)   r   )r  r   r)   r   )rK   rL   rM   r   r  r  r:   r4   r2   r
  r
    s<        ''' ^'777 ^777r4   r
  c                  6    e Zd ZU dZded<   ddZdd	ZddZdS )GuardsCheckpointStatezW
    The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
    OrderedSet[Guard]dynamo_guardsr)   r   c                    || _         d S rk   )r  )r0   r  s     r2   __init__zGuardsCheckpointState.__init__  s    *r4   r   OrderedSet[Guard] | Nonec                n    | j                             |j                   }t          |          dk    rdS |S )z
        Produces a delta against another GuardsCheckpointState.

        Returns None if no delta is found, otherwise, return an OrderedSet() of mismatched
        Guard type objects.
        r   N)r  
differencer   r0   r   rs      r2   diffzGuardsCheckpointState.diff  s6     ))%*=>>q66Q;;4r4   r   ri   c                ^    t          |t                    sdS |                     |          d u S NF)r   r  r  r   s     r2   __eq__zGuardsCheckpointState.__eq__  s1    %!677 	5yy4''r4   N)r  r  r)   r   )r   r  r)   r  r   r   r)   ri   )rK   rL   rM   __doc__rN   r  r  r  r:   r4   r2   r  r    sj           %$$$+ + + +
 
 
 
( ( ( ( ( (r4   r  c                  6    e Zd ZU i Zded<   ddZddZddZdS )ModuleContextCheckpointStatedict[str, torch.nn.Module]
nn_modulesr)   r   c                    || _         d S rk   r%  )r0   r%  s     r2   r  z%ModuleContextCheckpointState.__init__  s    $r4   r   set[str] | Nonec                    t          | j                                                                      t          |j                                                            }t	          |          dk    rdS |S )z
        Produces a delta against another ModuleContextCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        module key names.
        r   N)setr%  keysr  r   r  s      r2   r  z!ModuleContextCheckpointState.diff  s]     $$&&''223u7G7L7L7N7N3O3OPPq66Q;;4r4   r   ri   c                ^    t          |t                    sdS |                     |          d u S r  )r   r#  r  r   s     r2   r  z#ModuleContextCheckpointState.__eq__)  1    %!=>> 	5yy4''r4   N)r%  r$  r)   r   )r   r#  r)   r(  r   )rK   rL   rM   r%  rN   r  r  r  r:   r4   r2   r#  r#    sc         -/J////% % % %
 
 
 
( ( ( ( ( (r4   r#  c                  &    e Zd Zd	dZd
dZddZdS )ModuleContextr)   r   c                    i | _         d S rk   r'  rU   s    r2   r  zModuleContext.__init__0  s    *,r4   r#  c                D    t          t          | j                            S rk   )r#  dictr%  rU   s    r2   r  zModuleContext.copy_graphstate3  s    +D,A,ABBBr4   r  c                    t          |t                    st          dt          |                     |j        | _        d S )Nz+expected ModuleContextCheckpointState, got )r   r#  r/   typer%  r  s     r2   r  z ModuleContext.restore_graphstate6  sH    %!=>> 	 Kd5kkKK    *r4   Nr  )r)   r#  )r  r#  r)   r   )rK   rL   rM   r  r  r  r:   r4   r2   r/  r/  /  sT        - - - -C C C C+ + + + + +r4   r/  c                  6    e Zd ZU i Zded<   ddZdd	ZddZdS )GlobalContextCheckpointStatedict[str, tuple[Callable, Any]]global_stateglobal_statesr)   r   c                    || _         d S rk   r8  )r0   r9  s     r2   r  z%GlobalContextCheckpointState.__init__A  s    )r4   r   r(  c                    t          | j                                                                      t          |j                                                            }t	          |          dk    rdS |S )z
        Produces a delta against another GlobalContextCheckpointState.

        Returns None if no delta is found, otherwise, return a set() of mismatched
        global key names.
        r   N)r*  r8  r+  r  r   r  s      r2   r  z!GlobalContextCheckpointState.diffD  s^     !&&(())44S9K9P9P9R9R5S5STTq66Q;;4r4   r   ri   c                ^    t          |t                    sdS |                     |          d u S r  )r   r6  r  r   s     r2   r  z#GlobalContextCheckpointState.__eq__P  r-  r4   N)r9  r7  r)   r   )r   r6  r)   r(  r   )rK   rL   rM   r8  rN   r  r  r  r:   r4   r2   r6  r6  >  sc         46L6666* * * *
 
 
 
( ( ( ( ( (r4   r6  c                  2    e Zd ZdZh dZddZddZdd	Zd
S )GlobalContextzz
    This keeps track of the global torch state during tracing of a function.
    For example, torch.is_grad_enabled.
    >   grad_enabledautocast_enabledautocast_cpu_dtypeautocast_gpu_dtypeautocast_cpu_enabledautocast_cache_enabledr)   r   c                    i | _         d S rk   r;  rU   s    r2   r  zGlobalContext.__init__e  s    =?r4   r6  c                *    t          | j                  S rk   )r6  r8  rU   s    r2   r  zGlobalContext.copy_graphstateh  s    +D,=>>>r4   r  c                   t          |t                    st          dt          |                     |j        | _        t          | j                  t          | j                  k    r/t          | j                                                  | j        k    s>t          dt          | j                                                   d| j                   | j        	                                D ]\  }} ||           d S )Nz+expected GlobalContextCheckpointState, got z Global state mismatch: got keys z, expected )
r   r6  r/   r4  r8  r   _supported_global_statesr*  r+  values)r0   r  r   argss       r2   r  z GlobalContext.restore_graphstatek  s   %!=>> 	 Kd5kkKK   ".!""c$*G&H&HHHD%**,,--1NNN <3t7H7M7M7O7O3P3P < < 9< <   +2244 	 	JD$DJJJJ	 	r4   Nr  )r)   r6  )r  r6  r)   r   )rK   rL   rM   r!  rI  r  r  r  r:   r4   r2   r?  r?  V  sp         
     @ @ @ @? ? ? ?     r4   r?  c                  p    e Zd Zd!d"dZd#d	Zd$dZd%dZd&dZd'dZd(dZ	dddd)dZ
d*dZd+dZd,d ZdS )-	GuardsSetNinnerr  r)   r   c                    |t                      | _        n|| _        t          t                    | _        | j        D ]}|                     |           d S rk   )r   rN  r   listsource_to_guardstrack_guard_by_source)r0   rN  guards      r2   r  zGuardsSet.__init__  sa    =,6LLDJJDJ CNdBSBSZ 	. 	.E&&u----	. 	.r4   rS  r   c                T    |j         }| j        |                             |           d S rk   )r   rQ  r   )r0   rS  r   s      r2   rR  zGuardsSet.track_guard_by_source  s,    )f%,,U33333r4   Iterator[Guard]c                *    t          | j                  S rk   )iterrN  rU   s    r2   __iter__zGuardsSet.__iter__      DJr4   r@   c                *    t          | j                  S rk   )r   rN  rU   s    r2   __len__zGuardsSet.__len__  s    4:r4   r   c                :    t          | j        |j        z
            S rk   )rM  rN  r   s     r2   __sub__zGuardsSet.__sub__  s    ek1222r4   ri   c                *    t          | j                  S rk   )ri   rN  rU   s    r2   __bool__zGuardsSet.__bool__  rY  r4   c                ^    t                      | _        t          t                    | _        d S rk   )r   rN  r   rP  rQ  rU   s    r2   clearzGuardsSet.clear  s#    \\
 +D 1 1r4   Tr   )collect_debug_stackskiprb  rc  c                  || j         v rd S |r$|j        t          j        d|z             |_        |j        t
                                          |_        | j                             |           |                     |           d S NrY   )rc  )	rN  r   r   extractr   TracingContextextract_stackaddrR  )r0   rS  rb  rc  s       r2   ri  zGuardsSet.add  s     DJF 	G{"/7QXFFF#-;;==E
u""5)))))r4   others
set[Guard]c                H    |D ]}|D ]}|                      |d           d S re  )ri  )r0   rj  ogs       r2   updatezGuardsSet.update  sH     	$ 	$A $ $####$	$ 	$r4   r   r   list[Guard]c                6    t          | j        |                   S )z4Return all guards with the given originating_source.)rP  rQ  )r0   r   s     r2   get_guards_for_sourcezGuardsSet.get_guards_for_source  s    D)&1222r4   c                    ddl m t          fd| j        D                       | _        t	          t
                    | _        | j        D ]}|                     |           dS )z.Delete all guards that contains a given sourcerY   )is_from_sourcec              3  >   K   | ]} |j                   |V  d S rk   )r   ).0rn  rt  r   s     r2   	<genexpr>z6GuardsSet.remove_guards_with_source.<locals>.<genexpr>  sK        
  
8Lf)U)U 
 
  
  
  
  
  
r4   N)_dynamo.sourcert  r   rN  r   rP  rQ  rR  )r0   r   rS  rt  s    ` @r2   remove_guards_with_sourcez#GuardsSet.remove_guards_with_source  s    222222  
  
  
  
  
z 
  
  
 
 


 !,D 1 1Z 	. 	.E&&u----	. 	.r4   rk   )rN  r  r)   r   )rS  r   r)   r   )r)   rU  r   )r   rM  r)   rM  r   r  )rS  r   rb  ri   rc  r@   r)   r   )rj  rk  r)   r   )r   r   r)   rp  )r   r   r)   r   )rK   rL   rM   r  rR  rX  r[  r]  r_  ra  ri  ro  rr  ry  r:   r4   r2   rM  rM    s       	. 	. 	. 	. 	.4 4 4 4          
3 3 3 3       2 2 2 2
 <@Q* * * * * *$ $ $ $
3 3 3 3. . . . . .r4   rM  c                  H    e Zd ZddZej        dd            ZddZdd	Zd
S )GuardsContextr)   r   c                H    t                      | _        g | _        d| _        d S r  )rM  r  aotautograd_guardsskip_installrU   s    r2   r  zGuardsContext.__init__  s$    (168"'r4   Generator[None, None, None]c              #  V   K   | j         }d| _         	 d V  || _         d S # || _         w xY w)NT)r~  )r0   olds     r2   skip_guard_installz GuardsContext.skip_guard_install  sF       	$EEE #DD####s    	(r  c                N    t          t          | j        j                            S rk   )r  r   r  rN  rU   s    r2   r  zGuardsContext.copy_graphstate  s    $Z0B0H%I%IJJJr4   r  c                    t          |t                    st          dt          |                     t	          |j                  | _        d S )Nz$expected GuardsCheckpointState, got )r   r  r/   r4  rM  r  r  s     r2   r  z GuardsContext.restore_graphstate  sL    %!677 	W !UU!U!UVVV&u':;;r4   Nr  r)   r  )r)   r  )r  r  r)   r   )	rK   rL   rM   r  
contextlibr   r  r  r  r:   r4   r2   r{  r{    sx        ( ( ( (
 $ $ $ $K K K K< < < < < <r4   r{  c                      e Zd Zedd            Zedd	            Zedd            Zedd            Zedd            Zedd            Z	edd            Z
edd            ZdS ) HopSubgraphCachefn_coder   
identifierr*   r)   r   c                    d S rk   r:   r0   r  r  s      r2   add_dynamo_installed_submodulez/HopSubgraphCache.add_dynamo_installed_submodule  s	     sr4   r   c                    d S rk   r:   r0   r  s     r2   get_dynamo_installed_submodulesz0HopSubgraphCache.get_dynamo_installed_submodules  s    ORsr4   keyr   c                    d S rk   r:   r0   r  r  s      r2   add_autograd_key_entryz'HopSubgraphCache.add_autograd_key_entry  s    NQcr4   Callable | Nonec                    d S rk   r:   r0   r  s     r2   get_autograd_key_entryz'HopSubgraphCache.get_autograd_key_entry  s    JM#r4   c                    d S rk   r:   r  s      r2   add_proxy_dispatch_entryz)HopSubgraphCache.add_proxy_dispatch_entry  s    PSPSr4   c                    d S rk   r:   r  s     r2   get_proxy_dispatch_entryz)HopSubgraphCache.get_proxy_dispatch_entry  s    LOCr4   tangent_metadatatuple[object]gmodtorch.fx.GraphModuler@   c                    d S rk   r:   )r0   r  r  r  s       r2   add_lazy_bwd_entryz#HopSubgraphCache.add_lazy_bwd_entry  s	     cr4   .tuple[torch.fx.GraphModule | None, int | None]c                    d S rk   r:   r0   r  r  s      r2   get_lazy_bwd_entryz#HopSubgraphCache.get_lazy_bwd_entry  s	     :=r4   N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  )rK   rL   rM   r   r  r  r  r  r  r  r  r  r:   r4   r2   r  r    s           ^ RRR ^RQQQ ^QMMM ^MSSS ^SOOO ^O   ^ = = = ^= = =r4   r  c                  d    e Zd ZU ded<   ded<   ded<   ded<   d	ed
<   ded<   ded<   dZded<   dS )InvokeSubgraphReuseEntryr*   	body_namer  	body_gmodz!NestedCompileRegionOptions | Noners   	list[Any]subgraph_input_mappingri   single_tensor_outputzIlist[tuple[torch.Size, tuple[int, ...], torch.dtype, torch.device, bool]]output_metadatazlist[Source | None]arg_sourcesr   r@   num_user_outputsN)rK   rL   rM   rN   r  r:   r4   r2   r  r  	  s         NNN####----        %$$$ r4   r  c                  ^    e Zd ZU ded<   ded<   dZded<    ej        e          Zd	ed
<   dS )InvokeSubgraphReuseConditionzlist[tuple[Any, object]]input_checksz2list[tuple[Source, GuardCheckSpec, object, Guard]]r   Nzpytree.TreeSpec | Nonetreespec)default_factoryzOrderedSet[Source]traced_sources)	rK   rL   rM   rN   r  dataclassesfieldr   r  r:   r4   r2   r  r  #  sd          +***
 ?>>> (,H++++
 *;)::)V)V)VNVVVVVVr4   r  c                      e Zd Zd.dZd/dZd0d
Zd1dZd2dZd1dZd2dZ	d3dZ
d4dZd5dZd6dZ	 d7d8d%Zd9d)Zd:d+Z	 d7d;d,Zd-S )<InvokeSubgraphCacher)   r   c                    i | _         i | _        t          t                    | _        t          t
                    | _        i | _        t          t                    | _        t          t
                    | _	        d S rk   )
autograd_cacheproxy_dispatch_cacher   rP  dynamo_installed_submodulesr2  lazy_bwd_cacheeffects_cachesubgraph_reuse_cachesubgraph_reuse_key_cacherU   s    r2   r  zInvokeSubgraphCache.__init__?  su    359;!FQRVFWFW(  	
  	  	!  	%%%r4   r  r   r  r*   c                F    | j         |                             |           d S rk   )r  r   r  s      r2   r  z2InvokeSubgraphCache.add_dynamo_installed_submoduleU  s&     	(188DDDDDr4   r   c                8    | j                             |g           S rk   )r  getr  s     r2   r  z3InvokeSubgraphCache.get_dynamo_installed_submodulesZ  s    /33GR@@@r4   r  r   c                    || j         |<   d S rk   )r  r  s      r2   r  z*InvokeSubgraphCache.add_autograd_key_entry]  s    *-J'''r4   r  c                8    | j                             |d           S rk   )r  r  r  s     r2   r  z*InvokeSubgraphCache.get_autograd_key_entry`  s    "&&z4888r4   c                    || j         |<   d S rk   )r  r  s      r2   r  z,InvokeSubgraphCache.add_proxy_dispatch_entryc  s    03!*---r4   c                8    | j                             |d           S rk   )r  r  r  s     r2   r  z,InvokeSubgraphCache.get_proxy_dispatch_entryf  s    (,,Z>>>r4   r  r  r  r  r@   c                ^    t          | j        |                   }||f| j        |         |<   |S rk   )r   r  )r0   r  r  r  	num_gmodss        r2   r  z&InvokeSubgraphCache.add_lazy_bwd_entryi  s7     +J788	=A9<MJ'(89r4   r  c                Z    || j         vrdS | j         |                             |d          S )N)NN)r  r  r  s      r2   r  z&InvokeSubgraphCache.get_lazy_bwd_entryt  s6     T000<":.223C\RRRr4   effectsr*  c           	         | j                             |d          x}r||k    rt          d| d| d| d          || j         |<   dS )z>Store the effect types for a given invoke_subgraph identifier.NzPDifferent number of effects were found for invoke_subgraph call with identifier z,. 
Previously we had the following effects: z.
But now we have: .)r  r  r/   )r0   r  r  prev_effectss       r2   add_effectszInvokeSubgraphCache.add_effects|  s    -11*dCCC< 	,&&$3,63 3@L3 3 )03 3 3   *1:&&&r4   
set | Nonec                8    | j                             |d          S )zARetrieve the effect types for a given invoke_subgraph identifier.N)r  r  r  s     r2   get_effectszInvokeSubgraphCache.get_effects  s    !%%j$777r4   r`   	conditionr  entryr  max_reuse_entriesc                    | j         |         }t          |          |k    rt          d| d| d          |                    ||f           d S )N1invoke_subgraph: exceeded maximum reuse entries () for function code a?  . This most likely means a guard keeps failing on every invocation, preventing subgraph reuse. Set TORCH_LOGS='+hierarchical_compile' to identify which guard is failing. If reuse is genuinely not possible and you need more cache entries, increase the limit via the max_reuse_entries argument to nested_compile_region().)r  r   RuntimeErrorr   )r0   r  r  r  r  entriess         r2   add_reuse_entryz#InvokeSubgraphCache.add_reuse_entry  s~     +G4w<<,,,J%J J;BJ J J	 	 	 		5)*****r4   	evaluatorHCallable[[InvokeSubgraphReuseCondition, InvokeSubgraphReuseEntry], bool]InvokeSubgraphReuseEntry | Nonec                    | j                             |g           }t          |          D ]G\  }\  }} |||          r3|dk    r)|                    d|                    |                     |c S Hd S rp   )r  r  	enumerateinsertpop)r0   r  r  r  ir  r  s          r2   find_reuse_entryz$InvokeSubgraphCache.find_reuse_entry  s     +//<<%.w%7%7 	 	!A!	5yE** q55NN1gkk!nn555	
 tr4   hash_keyc                ^    | j                             |i                               |          S rk   )r  r  )r0   r  r  s      r2   find_reuse_entry_by_keyz+InvokeSubgraphCache.find_reuse_entry_by_key  s+     ,00"==AA(KKKr4   c                    | j         |         }t          |          |k    r||vrt          d| d| d          |||<   d S )Nr  r  zc (hash-key path). Increase the limit via the max_reuse_entries argument to nested_compile_region().)r  r   r  )r0   r  r  r  r  	key_caches         r2   add_reuse_entry_by_keyz*InvokeSubgraphCache.add_reuse_entry_by_key  su     1':	y>>...893L3L,%, ,;B, , ,   $	(r4   Nr  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  r@   r)   r  )
r  r   r  r@   r  r  r  r@   r)   r   )rK   rL   rM   r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r:   r4   r2   r  r  >  sd          ,E E E E
A A A A. . . .9 9 9 94 4 4 4? ? ? ?	 	 	 	S S S S
1 
1 
1 
18 8 8 8 "#+ + + + +*    L L L L "#$ $ $ $ $ $ $r4   r  c                      e Zd Zd	dZd
dZdS )HopDispatchSetCacher)   r   c                <    ddl m} |t                      i| _        d S )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphr  r  hop_cache_map)r0   r  s     r2   r  zHopDispatchSetCache.__init__  s.    KKKKKK-/B/D/DEr4   optorch._ops.HigherOrderOperatorHopSubgraphCache | Nonec                2    || j         vrd S | j         |         S rk   )r  )r0   r  s     r2   	get_cachezHopDispatchSetCache.get_cache  s#    T'''4!"%%r4   Nr  )r  r  r)   r  )rK   rL   rM   r  r  r:   r4   r2   r  r    s@        F F F F& & & & & &r4   r  c                  v    e Zd Zedd            Zedd            ZddZedd	            Zedd            ZdS )CompileContextr)   c                 P    t           j        t          d          t           j        S )Nzcompile_context is not set)_TLScompile_contextr/   r:   r4   r2   r  zCompileContext.get  s#    ' !=>>>##r4   CompileContext | Nonec                 .    t          t          dd           S Nr  getattrr  r:   r4   r2   try_getzCompileContext.try_get      t.555r4   r5   r7   r   c                    |4t          |t                    st          dt          |                     || _        d| _        g | _        d S )Nz*compile_id must be None or CompileId, got r   )r   r$   r/   r4  r5   rR   shape_env_guards)r0   r5   s     r2   r  zCompileContext.__init__  sZ    !*Z*K*K! OT*=M=MOO   -7+-r4   c                 J    t                                           } | d S | j        S rk   )r  r  r5   rU   s    r2   current_compile_idz!CompileContext.current_compile_id  s%    %%''<4r4   TraceId | Nonec                     t                                           } | d S | j        d S t          | j        | j                  S rk   )r  r  r5   rQ   rR   rU   s    r2   current_trace_idzCompileContext.current_trace_id  s>    %%''<4?"4t555r4   N)r)   r  )r)   r  )r5   r7   r)   r   )r)   r7   )r)   r  )	rK   rL   rM   r   r  r  r  r  r  r:   r4   r2   r  r    s        $ $ $ \$
 6 6 6 \6. . . .    \ 6 6 6 \6 6 6r4   r  c                  2    e Zd ZU dZded<   ded<   ded<   dS )	InlinedCodeCachez8Cache for code-object-derived data used during inlining.r  instructionszdict[Any, int]indexofdict[str, Any]code_optionsN)rK   rL   rM   r!  rN   r:   r4   r2   r  r    s<         BB      r4   r  c                  `   e Zd ZdZed"d            Zed#d            Zd$d	Zd%d
Zee	d&d                        Z
ed'd            Zd(dZeej	        d)d                        Zeej	        d*d                        Zeej	        d+d                        Zed,d            Zed-d             Zd!S ).rg  z
    Provides the currently installed TracingContext, or None.

    Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but
    will return None.
    r)   TracingContext | Nonec                 .    t          t          dd           S )Ntracing_contextr  r:   r4   r2   r  zTracingContext.try_get!  r	  r4   c                 Z    t                                           x} r| S t          d          )Nz<TracingContext.get() must be called within an ongoing trace.)rg  r  r  )ctxs    r2   r  zTracingContext.get%  s5     ((***3 	JJ
 
 	
r4   	fake_modeFakeTensorMode | Noner   c                   t                      | _        t                      | _        t	                      | _        t                      | _        t                      | _        t                      | _	        || _
        g | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d| _        t+                      | _        d| _        t1                      | _        g | _        d | _        d S r  )r{  guards_contextr/  module_contextr?  global_contextr2  previously_inlined_functionspreviously_cleaned_instructionsinlined_code_cacher  frame_summary_stackloc_in_framefw_metadataddp_optimizer_ctxaot_graph_nameparams_flatparams_flat_unwrap_subclassesparams_unwrapped_to_flat_indexoutput_strides#force_unspec_int_unbacked_size_liker   tensor_to_contextfakify_first_callr  hop_dispatch_set_cachetraced_codecudagraph_annotation)r0   r  s     r2   r  zTracingContext.__init__-  s    +oo+oo+oo<@FF)?Cvv,?Cvv09AC  :>7;=A04-1?C*@D+ DH 490!8!:!:
 "'&9&;&;#+-)-!!!r4   c                    i | j         _        | j                                         | j                                         | j                                         d S rk   )r"  r8  r#  ra  r$  r%  rU   s    r2   ra  zTracingContext.clearc  sT     ,.()//111,22444%%'''''r4   kwargsr   r  c               +    K   i }t                                           }| D ]}t          ||          ||<   |                                 D ]\  }}t	          |||           	 d V  |                                D ]\  }}t	          |||           d S # |                                D ]\  }}t	          |||           w xY wrk   )rg  r  r  r?   setattr)r6  priorr  r  vals        r2   patchzTracingContext.patchk  s         "" 	+ 	+C c**E#JJ 	# 	#HCCc""""	'EEE!KKMM ' 'SS#&&&&' 'EKKMM ' 'SS#&&&&'s   "B -C traceback.StackSummaryc                     t                                           } | t          j                    S | j        }| j        ||                                 gz   }t          j                            |          S rk   )rg  r  	tracebackStackSummaryr&  r'  _populate_loc_in_frame_summary	from_list)r0   r   s     r2   rh  zTracingContext.extract_stack|  sh    %%''<)+++((T@@BBCCE%//666r4   traceback.FrameSummaryc                t    | j         t          d          | j         \  }}}t          j        |||d          S )Nzloc_in_frame must not be NoneF)lookup_line)r'  r/   r>  FrameSummary)r0   filenamelineno
frame_names       r2   r@  z-TracingContext._populate_loc_in_frame_summary  sD    $ !@AAA'+'8$&*%h
PUVVVVr4   c               #    K   t                                           } t          j        j                            | dg           5  t          j        j                            | dd           5  	 d V  n*# t          $ r}t          |d          sd |_         d }~ww xY w	 d d d            n# 1 swxY w Y   d d d            d S # 1 swxY w Y   d S )Nr&  r'  
real_stack)	rg  r  unittestmockr;  r   rB   hasattrrJ  )tcrH   s     r2   clear_framezTracingContext.clear_frame  s_      !!M&&r+@"EE	 	M&&r>4@@	 	   $ q,// (#'AL) 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	sZ   'B?)B'+A0/B'0
B:BBB'B?'B+	+B?.B+	/B??CCframe_summarytraceback.FrameSummary | Nonec              #    K   t                                           }| |j                            |            |j        }d |_        	 d V  n<# t
          $ r/}t          |d          s|                                |_         d }~ww xY w	 | |j        	                                 ||_        d S # | |j        	                                 ||_        w xY w)NrJ  )
rg  r  r&  r   r'  rB   rM  rh  rJ  r  )rP  rN  r  rH   s       r2   current_framezTracingContext.current_frame  s       !!$"))-888o		"EEEE 	 	 	1l++ 2!//11	  (&**,,,!BOOO (&**,,,!BO!!!!s*   A B- 
B*B  BB- -$C:Generator[list[tuple[int, ...] | None] | None, None, None]c               #     K   t                                           } | d V  d S | j        }g | _        	 | j        V  || _        d S # || _        w xY wrk   )rg  r  r.  )rN  old_output_stridess     r2   report_output_stridesz$TracingContext.report_output_strides  st      
 ##%%:JJJF.	3#### 2B 2B2222s   	A 	ArF  r*   rG  r@   rH  c                H    | ||ft                                           _        d S rk   )rg  r  r'  )rF  rG  rH  s      r2   set_current_loczTracingContext.set_current_loc  s%     .6vz,J)))r4   list[CodeType] | Nonec                 J    t                                           } | d S | j        S rk   )rg  r  r3  )rN  s    r2   get_traced_codezTracingContext.get_traced_code  s%    ##%%:4~r4   N)r)   r  )r)   rg  )r  r  r)   r   r  )r6  r   r)   r  )r)   r<  )r)   rB  r  )rP  rQ  r)   r  )r)   rT  )rF  r*   rG  r@   rH  r*   r)   r   )r)   rZ  )rK   rL   rM   r!  r   r  r  r  ra  r   r;  rh  r@  r  rO  rS  rW  rY  r\  r:   r4   r2   rg  rg    s         6 6 6 \6 
 
 
 \
4. 4. 4. 4.l( ( ( ( ' ' ' ^ \' 7 7 7 \7W W W W     \< " " "  \"* 3 3 3  \3 K K K \K
    \  r4   rg  contextr  r)   ,Generator[CompileContext | None, None, None]c              #     K   t          t          dd           }| t          _        	 | V  |t          _        d S # |t          _        w xY wr  )r  r  r  )r]  old_contexts     r2   r  r    sR       $ 1488K"D+*{****s	   8 Ar  ,Generator[TracingContext | None, None, None]c              #    K   t          t          dd          }| t          _        	 | V  n># t          $ r1}t	          |d          s| |                                 |_         d}~ww xY w	 | 1| j        *| j        j        | j        j        	                                 |t          _        dS # | 1| j        *| j        j        | j        j        	                                 |t          _        w xY w)z
    This function installs the passed in tracing context as a dynamic scoped
    global variable.

    Calls to TracingContext.get() while not under a `with tracing()` context
    will return None.
    r  NrJ  )
r  r  r  rB   rM  rh  rJ  r  	shape_envcleanup)r]  r`  rH   s      r2   tracingre    s      $ 1488K"D+   q,'' 	3G,?"0022AL 	 !-!+7'//111* !-!+7'//111*****s&   + B+ 
A&,A!!A&&B+ +AC,rC   type[T]r6  r   c                    d S rk   r:   rC   r6  s     r2   dataclass_with_cached_hashri    s    HKr4   r   Callable[[type[T]], type[T]]c                    d S rk   r:   rh  s     r2   ri  ri    s	     $'3r4   type[T] | None&type[T] | Callable[[type[T]], type[T]]c                .    dfd}| |S  ||           S )N	cls_innerrf  r)   c                j    t          j        | fi }| j        dfd}d }||_        ||_        |S )Nr)   r@   c                |    t          | d          s%t                              | d |                      | j        S )Nr   )rM  r   __setattr__r   )r0   old_hashs    r2   r   z:dataclass_with_cached_hash.<locals>.wrap.<locals>.__hash__  s>    4)) B""4((4..AAA:r4   c                t     t          j                   }t           fd|D                       } j        |fS )Nc              3  P   K   | ] }|j         	t          |j                  V  !d S rk   )initr  r   )rv  fr0   s     r2   rw  zOdataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__.<locals>.<genexpr>(  s6       Q Q1!& Qqv!6!6 Q Q Q Q Q Qr4   )r  fieldstupler   )r0   rx  field_valuess   `  r2   
__reduce__z<dataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__#  sD     !'--F  Q Q Q Q Q Q QQQLNL11r4   r   )r  r   r   r{  )ro  new_clsr   r{  rs  r6  s       @r2   wrapz(dataclass_with_cached_hash.<locals>.wrap  sh    '	<<V<<%	 	 	 	 	 	
	2 	2 	2 $'r4   )ro  rf  r)   rf  r:   )rC   r6  r}  s    ` r2   ri  ri    s;         * {499r4   c                      e Zd ZddZddZd dZej        d!d
            Ze	d"d            Z
ej        d"d            Zd#dZd$dZddZddZd%d&dZdS )'r   r)   ri   c                    dS r  r:   rU   s    r2   is_dict_keyzSource.is_dict_key9      ur4   c                    dS r  r:   rU   s    r2   is_ephemeralzSource.is_ephemeral<  r  r4   codegenr   r   c                    t           rk   NotImplementedError)r0   r  s     r2   reconstructzSource.reconstruct?  s    !!r4   rX   c                    t           rk   r  rU   s    r2   r   zSource.guard_sourceB  s    !!r4   r*   c                    t           )a  
        A template for the name of the source. Used to prevent code duplication between
        `name` and `get_value`.

        For non-ChainedSources, `name` and `get_value` use the returned string directly.

        For ChainedSources, `name` and `get_value` expect the return to be a format string
        with `{0}` present - `name` and `get_value` will apply different values to this function's
        returned format string.
        r  rU   s    r2   _name_templatezSource._name_templateF  s
     "!r4   c                    | j         S rk   )r  rU   s    r2   r   zSource.nameT  s    ""r4   globalsr  localscachedict[Source, Any]r   c                T    | |v r||          S t          | j        ||          }||| <   |S rk   )evalr  )r0   r  r  r  r   s        r2   	get_valuezSource.get_valueX  s9     5==;T('6::dr4   fnCallable[..., Any]r   c                V    | j         t          j        u rt          t	          | |          S rk   )r   rX   r   r  r   )r0   r  s     r2   
make_guardzSource.make_guardd  s'     444%%T2r4   c                4    | j                                         S rk   )r   rw   rU   s    r2   rw   zSource.is_specialized_nn_modulei  s     99;;;r4   c                ,    | j         t          j        k    S )z+True if you can guard on attributes of this)r   rX   r   rU   s    r2   subguards_allowedzSource.subguards_allowedl  s     K$???r4   Ntransform_fn!Callable[[Source], Source] | Nonec                     | ||           S | S rk   r:   )r0   r  s     r2   clonezSource.clonep  s     #<%%%r4   r   )r  r   r)   r   r   rJ   r  r  r  r  r  r  r)   r   )r  r  r)   r   rk   r  r  r)   r   )rK   rL   rM   r  r  r  r   cached_propertyr   r   r  r   r  r  rw   r  r  r:   r4   r2   r   r   7  s%             " " " " " " " " " " " X" # # # #
 
 
 
   
< < < <@ @ @ @      r4   r   c                      e Zd ZU ded<   ddZddZej        dd            Zdd	Z	ej        dd            Z
ddZdddZdS )ChainedSourcer   baser)   ri   c                4    | j                                         S rk   )r  r  rU   s    r2   r  zChainedSource.is_dict_key}  s    y$$&&&r4   c                4    | j                                         S rk   )r  r  rU   s    r2   r  zChainedSource.is_ephemeral  s    y%%'''r4   rX   c                    | j         j        S rk   )r  r   rU   s    r2   r   zChainedSource.guard_source  s    y%%r4   c                l    | }t          |t                    r|j        }t          |t                    |S rk   )r   r  r  )r0   currents     r2   get_basezChainedSource.get_base  s9    -00 	#lG -00 	#r4   r*   c                J    | j                             | j        j                  S rk   )r  r   r  r   rU   s    r2   r   zChainedSource.name  s    "))$).999r4   r  r  r  r  r  r   c                    | |v r||          S d}d}||v rd| }|dz  }||v | j                             |||          ||<   t          | j                            |          ||          }||= ||| <   |S )Ntmpr   rY   )r  r  r  r  r   )r0   r  r  r  tmpvarcounterr   s          r2   r  zChainedSource.get_value  s     5==;$7__FqLG  ,,WfeDDvT(//77&II6Ndr4   Nr  r  c                P   d| j                             |          i}t          j        |           D ]U}|j        dk    rt          | |j                  }t          |t                    r|                    |          ||j        <   Vt          j        | fi |}| ||          }|S )Nr  )	r  r  r  rx  r   r  r   r   replace)r0   r  cloned_fieldsrw  r:  results         r2   r  zChainedSource.clone  s    )/1N1N(O#D)) 	@ 	@Av$''C#v&& @(+		,(?(?af%$T;;];;#!\&))Fr4   r   r   )r)   r   rJ   r  rk   r  )rK   rL   rM   rN   r  r  r   r  r   r  r   r  r  r:   r4   r2   r  r  y  s         LLL' ' ' '( ( ( ( & & & &    : : : :   &      r4   r  inputsr  c                B   ddl mm}m} t                                          x}r|j        }||S g }ddlm} t          t           |                                D ]-\  }t          ||          r|                    |df           .t          j        |           }t          |          D ]\  }	t          |	          r|                    |	j        df           t          |	          rKg }
 ||	|
           fd|
D             }|                    fd	t          |          D                        |r`|d         \  }}}|d
d         D ]G\  }}}||ur=t#          d| d| d| d| d| d| d| d| d|j         d| d| d|j                   H|S dS )a  
    Attempts to "detect" what the current fake mode is.  If there is one ambiently
    available from TracingContext, we preferentially use that.  Otherwise, we
    heuristically detect the fake mode via the following sources, in order of
    priority:

        - Currently active fake mode on stack
        - Fake mode associated with passed in tensors (inputs does not
          have to be flattened)
    r   )
FakeTensorr   get_plain_tensorsN _get_current_dispatch_mode_stackzactive fake modezfake tensor input)outc                4    g | ]}t          |          |S r:   )r   )rv  xr  s     r2   
<listcomp>z$detect_fake_mode.<locals>.<listcomp>  s8     . . .*Q
";";.. . .r4   c                0    g | ]\  }}|j         d  |fS )zsubclass input )r  )rv  ixtensorr  s      r2   r  z$detect_fake_mode.<locals>.<listcomp>  s@       "F %'<'<'<bA  r4   rY   zfake mode (z) from r   z doesn't match mode (z

fake mode from z allocated at:
z
fake mode from )torch._subclasses.fake_tensorr  r   r  rg  r  r  torch.utils._python_dispatchr  r  reversedr   r   pytreetree_leavesr   r   r/   r   )r  r   r  r]  r  
fake_modesr  mflat_inputs
flat_inputr  fake_tensorsdesc1i1desc2i2r  r  s                   @@r2   detect_fake_moder    s             !((***w %	 JMMMMMM(#C#C#E#EFFGG : :1a(( 	:q"4a8999$V,,K";//  :j*-- 	Nz35H!LMMM(44 	;=Cjc2222. . . .. . .L    &/&=&=      )!}	5"&qrrN 	 	LAub!!$L) L LE L LB L LUV L L_d L Lgi L L&+L L.0L LBK/L L&+L L.0L LBC'L L   " tr4   c                     ddl m}  ddlm} t	          t           |                                D ]\  }}t          ||           r|c S dS )z~
    Inspects the dispatch mode stack for an active fake mode and returns it.
    Returns None if no fake mode is active.
    r   r   r  N)r  r   r  r  r  r  r   )r   r  rT   r  s       r2   active_fake_moder    s|    
 =<<<<<MMMMMM(#C#C#E#EFFGG  1a(( 	HHH	 4r4   )r]  r  r)   r^  )r]  r  r)   ra  )rC   rf  r6  r   r)   rf  rk   )rC   r   r6  r   r)   rj  )rC   rl  r6  r   r)   rm  )r  r   r)   r  )r)   r  )g
__future__r   r  r  enumr   loggingre	threadingr>  unittest.mockrK  r   abcr   collectionsr   r   r   typingr   r	   r
   r   r   r   typing_extensionsr   r   torch.utilsr   r  torch.utils._ordered_setr   r  r   torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerrK   r   collections.abcr   r   r   typesr   sympy"torch._dynamo.backends.distributedr   torch._dynamo.codegenr   torch._dynamo.guardsr   &torch._functorch._aot_autograd.schemasr   r  r   r  r   compiler;   r<   r$   rQ   EnumrX   r   r   r   r   r   r   r   r  r
  r  r#  r/  r6  r?  rM  r{  r  r  r  r  r  localr  r  r  rg  r  re  ri  r   r  r  r  r:   r4   r2   <module>r     sk	   " " " " " "               				                    # # # # # # % % % % % % ! ! ! ! ! ! M M M M M M M M M M M M M M M M 1 1 1 1 1 1  ) ) ) ) ) ) / / / / / / F F F F F F B B B B B B B B 4 4 4 4 4 4 g!!  ===========LLLFFFFFF//////333333JJJJJJRRRRRR<<<<<<  RZ PQQ "
X  " $D1117J 7J 7J 7J 7J 7J 7J 217Jt7 7 7 7 7j 7 7 7<
 <
 <
 <
 <
$) <
 <
 <
~	 	 	 	 	 	 	 	 d###       $#         T"""u' u' u' u' u' u' u' #"u'p GCLL d###	 	 	 	 	 	 	 $#	 d###	 	 	 	 	l 	 	 $#	 d###* * * * *\ * * $#*

8 8 8 8 8WQZ 8 8 8( ( ( ( ( ( ( (8( ( ( ( ( ( ( (0+ + + + +N#?@ + + +( ( ( ( ( ( ( (0$ $ $ $ $N#?@ $ $ $RC. C. C. C. C. C. C. C.L< < < < <N#89 < < <2 =  =  =  =  =  =  =  =F        2 W W W W W W W W4G$ G$ G$ G$ G$* G$ G$ G$T
& 
& 
& 
& 
& 
& 
& 
& y&#6 #6 #6 #6 #6 #6 #6 #6L ! ! ! ! ! ! ! !G G G G G G G GT + + + + + + + +8 
 K K K 
 K 
' ' ' ' 
'
     @ 4(((= = = = = = = )(=B 4(((6 6 6 6 6F 6 6 )(6r> > > > >B     r4   