
    i                    @   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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mZ ej8                  dk\  rd d	lmZ nd
 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jV                  e,      Z-er,d dl.m/Z/m0Z0m1Z1 d dl2m3Z3 d dl4Z4d dl5m6Z6 d dl7m8Z8 d dl9m:Z: d dl;m<Z< 	  ejz                  d      Z> ejz                  d      Z? eddd       G d d             Z@ G d de      ZA G d dej                        ZC	  G d  d!      ZD ej&                  d"       G d# d$             ZE G d% d&e      ZF ej&                  d'       G d( d)             ZG ed*      ZH	  ej&                  d"       G d+ d,             ZI	  ej&                  d"       G d- d.eI             ZJ	  ej&                  d"       G d/ d0eI             ZK	  G d1 d2eeH         ZL G d3 d4      ZM G d5 d6      ZN G d7 d8eLeN         ZO G d9 d:      ZP G d; d<eLeP         ZQ G d= d>      ZR	  G d? d@eLeM         ZS G dA dB      ZT G dC dDeT      ZU G dE dF      ZV e	j                         ZX	  G dG dH      ZY G dI dJ      ZZe	 	 	 	 dVdK       Z[e	 	 	 	 dWdL       Z\edXdM       Z]e	 dY	 	 	 	 	 dZdN       Z] e       	 dY	 	 	 	 	 d[dO       Z] e]d"       G dP dQ             Z^ e]d"       G dR dSe^             Z_dYd\dTZ`d]dUZay)^    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyGeneric
NamedTupleOptionaloverloadTYPE_CHECKINGTypeVar)      )dataclass_transformc                     d } | S )Nc                    | S N )fns    G/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_guards.py	decoratorz&dataclass_transform.<locals>.decorator   s    I    r   )r   s    r   r   r      s    	 r   )_pytree)
OrderedSet)is_traceable_wrapper_subclass)CapturedTracebackformat_frame)WeakTensorKeyDictionary)Callable	GeneratorIterator)CodeType)DDPOptimizerContext)	PyCodegen)ViewAndMutationMeta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                  J    e Zd ZU ded<   ded<   dZded<   ddZed	d       Zy)
	CompileId
int | Noneframe_idframe_compile_idNcompiled_autograd_idc                8   | j                   Z| j                  d u | j                  d u k(  sJ d}| j                  d| j                   d| j                   }d| j                    | S | j                  | j                  J | j                   d| j                   S )N /!)r1   r/   r0   )self	frame_strs     r   __str__zCompileId.__str___   s    $$0MMT)t/D/D/LMMMI}}(a0E0E/FG	t001)====,1F1F1RRRmm_Ad&;&;%<==r   c                &   |y	 t         t        fD ]X  }|j                  |      x}s|j                         }|j	                         D ]  \  }}|	t        |      ||<     | di |c S  t        # 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 ''r   )COMPILE_ID_PATTERNCA_COMPILE_ID_PATTERNmatch	groupdictitemsint
ValueError	Exception)cls
compile_idpatternr=   groupskves           r   from_stringzCompileId.from_stringl   s     	J.0EF !#MM*5555"__.F & /1=(+AF1I/ ==(! !  	J3J<qABI	Js(   "A2 (A2 A2 +A2 2	B;BBreturnstr)rD   
str | NonerL   CompileId | None)__name__
__module____qualname____annotations__r1   r8   classmethodrJ   r   r   r   r-   r-   P   s8    
 !  (,*+
> J Jr   r-   c                  *    e Zd ZU ded<   ded<   ddZy)TraceIdr-   rD   r@   attemptc                    | j                   dk(  rt        | j                        S | j                   d| j                    S )Nr   _)rW   rM   rD   r6   s    r   r8   zTraceId.__str__   s7    <<1t''oo&a~66r   NrK   rP   rQ   rR   rS   r8   r   r   r   rV   rV      s     L7r   rV   c                  x    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y)GuardSourcer         r                  	   
   r                  c                F    | t         j                  t         j                  fv S r   )r]   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULErZ   s    r   is_fsdp_modulezGuardSource.is_fsdp_module   s    668U8UVVVr   c                    dd l mc m} |j                  r4| t        j
                  t        j                  fv xs | j                         S | t        j
                  t        j                  fv S Nr   )torch._dynamo.config_dynamoconfig_unsafe_skip_fsdp_module_guardsr]   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULEro   )r6   rt   s     r   is_specialized_nn_modulez$GuardSource.is_specialized_nn_module   sn    --11<<;; )
 &&( 4433
 
 	
r   c                    | t         j                  t         j                  t         j                  t         j                  fv S r   )r]   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErZ   s    r   is_unspecialized_nn_modulez&GuardSource.is_unspecialized_nn_module   s8    6655>>==	
 
 	
r   c                F    | t         j                  t         j                  fv S r   )r]   r|   r}   rZ   s    r   "is_unspecialized_builtin_nn_modulez.GuardSource.is_unspecialized_builtin_nn_module   s&    >>==
 
 	
r   c                    | t         j                  t         j                  t         j                  t         j                  t         j
                  fv S r   )r]   LOCALrw   rn   r{   r}   rZ   s    r   is_localzGuardSource.is_local   sA    33))55==
 
 	
r   NrL   bool)rP   rQ   rR   r   GLOBALrw   rv   CONSTANTRANDOM_VALUE	SHAPE_ENVrn   rm   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALr{   rz   r}   r|   
TEMP_LOCALro   rx   r~   r   r   r   r   r   r]   r]      sy    EF"##$ HLINIO$&!%'",.)-/*JW
"


r   r]   c                      e Zd Zy)GuardBuilderBaseNrP   rQ   rR   r   r   r   r   r      s    r   r   )r)   c                  *    e Zd ZU ded<   ded<   ddZy)SLocz#traceback.FrameSummary | str | Noneframework_locrN   maybe_user_locc                    t        | j                  t              r| j                  nt        | j                        }| j                  | j                   d| dS d| dS )Nz ()()
isinstancer   rM   r   r   )r6   flocs     r   r8   zSLoc.__str__   sh     $,,c2 d001 	
 *))*"TF!44tfA;r   NrK   r[   r   r   r   r   r      s    66	r   r   c                  ,    e Zd ZU ded<   ded<   ded<   y)
ShapeGuardzsympy.logic.boolalg.Booleanexprr   slocr   size_obliviousNrP   rQ   rR   rS   r   r   r   r   r      s    
%%
Jr   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<   d%dZd&dZd'dZd(dZed)d       Zed*d       Zed+d       Zd)dZd)dZd,dZd-d Zd-d!Zd-d"Zd)d#Z	 	 	 	 	 	 	 	 	 	 d.d$Zy)/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.   _hashFr   _unserializablec                    | j                   :t        | j                  | j                  t	        | j
                        f      | _         | j                   S r   )r   hashnamesourceidr   rZ   s    r   __hash__zGuard.__hash__  s:    ::tyy$++r$..7IJKDJzzr   c                   t        | j                  t        j                        xrD | j                  j                  t
        j                  j                  j                  j                  u }|| j                  r| j                  j                  ndt        | j                        | j                  | j                         j                  j                   fS )N)r   r   	functoolspartialfunctorchrs   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r6   is_duplicate_inputs     r   sort_keyzGuard.sort_key  s    
 t~~y'8'89 Y##u}}';';'H'H'X'XX 	
 !%DKK"		NII  "++::
 	
r   c                D    | j                         |j                         k  S r   )r   r6   others     r   __lt__zGuard.__lt__/  s    }}!111r   c                    t        | j                  t        j                        r| j                  j                  S | j                  S r   )r   r   r   r   r   rZ   s    r   r   zGuard.inner_create_fn2  s0    dnni&7&78>>&&&>>!r   c                .    | j                   j                  S r   )r   r   rZ   s    r   r   z
Guard.name8  s    &&+++r   c                .    | j                   j                  S r   )r   guard_sourcerZ   s    r   r   zGuard.source<  s    &&333r   c           
        t        | t        j                        re |        }|Dd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__'
        z<weakref at z; to 'z' at >z; dead>)r   weakrefReferenceTypehexr   	__class__rP   rM   )r   objs     r   weakref_to_strzGuard.weakref_to_str@  s      k7#8#89-C%c"[/&:%;6#--BXBXAYY^_bceficj_k^llmnn%c"[/&:%;7CC{##r   c                Z   d| j                   r$| j                   j                  j                         nd dt        | j                         d| j	                         j
                   d| j                   d| j                   d| j                  | j                         d| j                   d}|S )	Nz	
        r3    z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )r   r   lowerreprr   rP   r   r   r   r   r   )r6   ss     r   __repr__zGuard.__repr__Y  s    	%)[[					!b94		?:K1TMaMaMcMlMlLm n ,,- .^^$ % //0@0@AB C"889 :	 r   c                   dt        | j                         d}| j                  r$| j                  j                  j                         nd}|d| dz  }|d| j	                         j
                   dz  }|d| j                   dz  }|d| j                   dz  }|d| j                  | j                         dz  }|d	| j                   dz  }|S )
NzName: 
r3   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )r   r   r   r   r   rP   r   r   r   r   r   )r6   outputr   s      r   r8   zGuard.__str__e  s    $tyy/*"--1[[!!'')bL++)$*>*>*@*I*I)J"MM%d&6&6%7r::ODNN#3266()<)<T=M=M)N(OrRR/0J0J/K2NNr   c           	     R   	 | j                  ||       S # t        $ r t        j                  dt	        |       j                                | j                  rNt        j                  ddj                  | j                  j                         dd        j                                 w xY w)NzError while creating guard:
%szCreated at:
%sr3   )
r   rB   log	exceptionrM   rstripr   errorjoinformat)r6   builders     r   createzGuard.createp  s    	>>'400 	MM;SY=M=M=OPzz		+RWWTZZ5F5F5H5M-N-U-U-WX		s
    BB&c                6    | j                   j                         S r   )r   rx   rZ   s    r   rx   zGuard.is_specialized_nn_moduley  s    {{3355r   c                6    | j                   j                         S r   )r   ro   rZ   s    r   ro   zGuard.is_fsdp_module|  s    {{))++r   c                6    | j                   j                         S r   )r   r   rZ   s    r   r   zGuard.is_local  s    {{##%%r   c                    t        | j                  t        j                        r"| j                  j                  }|j
                  S | j                  }|j
                  S r   )r   r   r   r   r   rP   )r6   r   s     r   create_fn_namezGuard.create_fn_name  sJ    dnni&7&78++I !!! I!!!r   c                   | j                   sg | _         | j                   j                  |       | j                  |d fv sJ d       || _        | j                  s|| _        n| j                  j	                  |       | j
                  |d fv s,t        | j
                        r| j                         J d       || _        y )Nz+Guarded class id must be identical, or NonezBGuarded object must be identical, None or ephemeral (dead weakref))r   appendr   r   extendr   callable)r6   
guard_typeguarded_classr   r   s        r   set_export_infozGuard.set_export_info  s     !D
+)).
 
 	9 9	9 
 &3"~~&DNNN!!), d 33(()  "*	P P		P+ 'r   rL   r@   )rL   ztuple[bool, int, int, str, int])r   r   rL   r   )rL   z(Callable[[GuardBuilderBase, Guard], Any]rK   rL   r]   )r   objectrL   rM   )r   r   rL   r   r   )
r   rM   r   r   r   	list[str]r   r   rL   None)rP   rQ   rR   rS   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r8   r   rx   ro   r   r   r   r   r   r   r   r      s   & 88 %)K!("&I&!%K%?C<C&*E#*04J-4E:!OT!

 2" , , 4 4 $ $0
	6,&" ' ' 9 ' 	 '
  ' 
 'r   r   Tc                      e Zd Zy)GuardEnvExprNr   r   r   r   r   r     s    r   r   c                  *    e Zd ZU ded<   ded<   ddZy)DuplicateInputsr   input_source_ainput_source_bc                :    | j                   | j                  k7  sJ y r   )r   r   rZ   s    r   __post_init__zDuplicateInputs.__post_init__  s    ""d&9&9999r   NrL   r   )rP   rQ   rR   rS   r  r   r   r   r   r     s    :r   r   c                  "    e Zd ZU ded<   ded<   y)StorageOverlapzlist[Source]overlapping_sourcesnon_overlapping_sourcesNr   r   r   r   r  r    s    %%))r   r  c                  0    e Zd Zedd       Zedd       Zy)Checkpointablec                     y r   r   rZ   s    r   copy_graphstatezCheckpointable.copy_graphstate  s    $'r   c                     y r   r   r6   states     r   restore_graphstatez!Checkpointable.restore_graphstate  s    47r   N)rL   r   )r  r   rL   r   )rP   rQ   rR   r   r  r  r   r   r   r	  r	    s    ' '7 7r   r	  c                  4    e Zd ZU dZded<   ddZd	dZd
dZy)GuardsCheckpointStatezW
    The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
    OrderedSet[Guard]dynamo_guardsc                    || _         y r   )r  )r6   r  s     r   __init__zGuardsCheckpointState.__init__  s
    *r   c                n    | j                   j                  |j                         }t        |      dk(  ry|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   r6   r   rs      r   diffzGuardsCheckpointState.diff  s3     ))%*=*=>q6Q;r   c                J    t        |t              sy| j                  |      d u S NF)r   r  r  r   s     r   __eq__zGuardsCheckpointState.__eq__	  s$    %!67yy4''r   N)r  r  rL   r   )r   r  rL   Optional[OrderedSet[Guard]]r   r   rL   r   )rP   rQ   rR   __doc__rS   r  r  r  r   r   r   r  r    s     %$+
(r   r  c                  4    e Zd ZU i Zded<   ddZddZd	dZy)
ModuleContextCheckpointStatedict[str, torch.nn.Module]
nn_modulesc                    || _         y r   r$  )r6   r$  s     r   r  z%ModuleContextCheckpointState.__init__  s	    $r   c                    t        | j                  j                               j                  t        |j                  j                                     }t	        |      dk(  ry|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      r   r  z!ModuleContextCheckpointState.diff  sM     $$&'223u7G7G7L7L7N3OPq6Q;r   c                J    t        |t              sy| j                  |      d u S r  )r   r"  r  r   s     r   r  z#ModuleContextCheckpointState.__eq__!  $    %!=>yy4''r   N)r$  r#  rL   r   )r   r"  rL   set[str] | Noner  )rP   rQ   rR   r$  rS   r  r  r  r   r   r   r"  r"    s    -/J*/%
(r   r"  c                  $    e Zd ZddZddZddZy)ModuleContextc                    i | _         y r   r&  rZ   s    r   r  zModuleContext.__init__(  s	    *,r   c                >    t        t        | j                              S r   )r"  dictr$  rZ   s    r   r  zModuleContext.copy_graphstate+  s    +D,ABBr   c                J    t        |t              sJ |j                  | _        y r   )r   r"  r$  r  s     r   r  z ModuleContext.restore_graphstate.  s     %!=>>>**r   Nr  )rL   r"  )r  r"  rL   r   rP   rQ   rR   r  r  r  r   r   r   r.  r.  '  s    -C+r   r.  c                  4    e Zd ZU i Zded<   ddZddZd	dZy)
GlobalContextCheckpointStatedict[str, tuple[Callable, Any]]global_statec                    || _         y r   r7  )r6   global_statess     r   r  z%GlobalContextCheckpointState.__init__6  s
    )r   c                    t        | j                  j                               j                  t        |j                  j                                     }t	        |      dk(  ry|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(  r7  r)  r  r   r  s      r   r  z!GlobalContextCheckpointState.diff9  sO     !!&&()44S9K9K9P9P9R5STq6Q;r   c                J    t        |t              sy| j                  |      d u S r  )r   r5  r  r   s     r   r  z#GlobalContextCheckpointState.__eq__E  r+  r   N)r:  r6  rL   r   )r   r5  rL   r,  r  )rP   rQ   rR   r7  rS   r  r  r  r   r   r   r5  r5  3  s    46L16*
(r   r5  c                  0    e Zd ZdZh dZddZddZd	dZy)
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_enabledc                    i | _         y r   r9  rZ   s    r   r  zGlobalContext.__init__Z  s
    =?r   c                ,    t        | j                        S r   )r5  r7  rZ   s    r   r  zGlobalContext.copy_graphstate]  s    +D,=,=>>r   c                b   t        |t              sJ |j                  | _        t        | j                        t        | j                        k(  r0t        | j                  j                               | j                  k(  sJ d       | j                  j                         D ]  \  }} ||        y )NzGlobal state mismatch)r   r5  r7  r   _supported_global_statesr(  r)  values)r6   r  r   argss       r   r  z GlobalContext.restore_graphstate`  s    %!=>>>!..!!"c$*G*G&HHD%%**,-1N1NN	# #	#O ++224 	JD$J	r   Nr  )rL   r5  )r  r5  rL   r   )rP   rQ   rR   r   rH  r  r  r  r   r   r   r>  r>  K  s    
 @?r   r>  c                  d    e Zd Zd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
y)	GuardsSetNc                6    |t               | _        y || _        y r   )r   inner)r6   rN  s     r   r  zGuardsSet.__init__n  s    =,6LDJDJr   c                ,    t        | j                        S r   )iterrN  rZ   s    r   __iter__zGuardsSet.__iter__t      DJJr   c                ,    t        | j                        S r   )r   rN  rZ   s    r   __len__zGuardsSet.__len__w  s    4::r   c                F    t        | j                  |j                  z
        S r   )rL  rN  r   s     r   __sub__zGuardsSet.__sub__|  s    ekk122r   c                ,    t        | j                        S r   )r   rN  rZ   s    r   __bool__zGuardsSet.__bool__  rR  r   Tr   )collect_debug_stackskipc                   || j                   v ry |r*|j                  t        j                  d|z         |_        |j                  t
        j                         |_        | j                   j                  |       y Nr^   )rZ  )rN  r   r   extractr   TracingContextextract_stackadd)r6   guardrY  rZ  s       r   r`  zGuardsSet.add  sc     DJJ{{"/77QXF#-;;=E

ur   c                F    |D ]  }|D ]  }| j                  |d         y r\  )r`  )r6   othersogs       r   updatezGuardsSet.update  s0     	$A $#$	$r   c                \    ddl m t        fd| j                  D              | _        y)z.Delete all guards that contains a given sourcer^   )is_from_sourcec              3  J   K   | ]  } |j                         r|  y wr   )r   ).0re  rh  r   s     r   	<genexpr>z6GuardsSet.remove_guards_with_source.<locals>.<genexpr>  s%       
8L8Lf)UA 
s   ##N)_dynamo.sourcerh  r   rN  )r6   r   rh  s    `@r   remove_guards_with_sourcez#GuardsSet.remove_guards_with_source  s#    2  
zz 
 

r   r   )rN  r  rL   r   )rL   zIterator[Guard]r   )r   rL  rL   rL  r   )ra  r   rY  r   rZ  r@   rL   r   )rc  z
set[Guard]rL   r   )r   r   rL   r   )rP   rQ   rR   r  rQ  rT  rV  rX  r`  rf  rm  r   r   r   rL  rL  m  sR     
3  <@Q

48
GJ
	
$

r   rL  c                  $    e Zd ZddZddZddZy)GuardsContextc                0    t               | _        g | _        y r   )rL  r  aotautograd_guardsrZ   s    r   r  zGuardsContext.__init__  s    (168r   c                R    t        t        | j                  j                              S r   )r  r   r  rN  rZ   s    r   r  zGuardsContext.copy_graphstate  s    $Z0B0B0H0H%IJJr   c                \    t        |t              sJ t        |j                        | _        y r   )r   r  rL  r  r  s     r   r  z GuardsContext.restore_graphstate  s&    %!6777&u':':;r   Nr  )rL   r  )r  r  rL   r   r3  r   r   r   ro  ro    s    9K<r   ro  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y	)HopSubgraphCachec                     y r   r   r6   fn_id
identifiers      r   add_dynamo_installed_submodulez/HopSubgraphCache.add_dynamo_installed_submodule  s    SVr   c                     y r   r   r6   rx  s     r   get_dynamo_installed_submodulesz0HopSubgraphCache.get_dynamo_installed_submodules      HKr   c                     y r   r   r6   ry  keys      r   add_autograd_key_entryz'HopSubgraphCache.add_autograd_key_entry  s    NQr   c                     y r   r   r6   ry  s     r   get_autograd_key_entryz'HopSubgraphCache.get_autograd_key_entry  s    JMr   c                     y r   r   r  s      r   add_proxy_dispatch_entryz)HopSubgraphCache.add_proxy_dispatch_entry  s    PSr   c                     y r   r   r  s     r   get_proxy_dispatch_entryz)HopSubgraphCache.get_proxy_dispatch_entry  s    LOr   c                     y r   r   )r6   ry  tangent_metadatagmods       r   add_lazy_bwd_entryz#HopSubgraphCache.add_lazy_bwd_entry  s     r   c                     y r   r   r6   ry  r  s      r   get_lazy_bwd_entryz#HopSubgraphCache.get_lazy_bwd_entry  s     :=r   Nrx  r@   ry  rM   rL   r   rx  r@   rL   r   ry  rM   r  r    rL   r   ry  rM   rL   zCallable | Nonery  rM   r  tuple[object]r  ztorch.fx.GraphModulerL   r@   ry  rM   r  r  rL   z.tuple[torch.fx.GraphModule | None, int | None])rP   rQ   rR   r   rz  r}  r  r  r  r  r  r  r   r   r   ru  ru    s    V VK KQ QM MS SO O ( #	
 
  ==1>=	7= =r   ru  c                      e Zd ZddZddZddZddZddZddZddZ		 	 	 	 	 	 	 	 ddZ
	 	 	 	 	 	 dd	Zdd
ZddZy)InvokeSubgraphCachec                ~    i | _         i | _        t        t              | _        t        t
              | _        i | _        y r   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr1  lazy_bwd_cacheeffects_cacherZ   s    r   r  zInvokeSubgraphCache.__init__  s>    359;!ALTAR(  	
  	r   c                @    | j                   |   j                  |       y r   )r  r   rw  s      r   rz  z2InvokeSubgraphCache.add_dynamo_installed_submodule  s    ((/66zBr   c                :    | j                   j                  |g       S r   )r  getr|  s     r   r}  z3InvokeSubgraphCache.get_dynamo_installed_submodules  s    //33E2>>r   c                "    || j                   |<   y r   )r  r  s      r   r  z*InvokeSubgraphCache.add_autograd_key_entry  s    *-J'r   c                :    | j                   j                  |d       S r   )r  r  r  s     r   r  z*InvokeSubgraphCache.get_autograd_key_entry  s    ""&&z488r   c                "    || j                   |<   y r   )r  r  s      r   r  z,InvokeSubgraphCache.add_proxy_dispatch_entry  s    03!!*-r   c                :    | j                   j                  |d       S r   )r  r  r  s     r   r  z,InvokeSubgraphCache.get_proxy_dispatch_entry  s    ((,,Z>>r   c                ^    t        | j                  |         }||f| j                  |   |<   |S r   )r   r  )r6   ry  r  r  	num_gmodss        r   r  z&InvokeSubgraphCache.add_lazy_bwd_entry  s:     ++J78	=A9<MJ'(89r   c                ^    || j                   vry| j                   |   j                  |d      S )N)NN)r  r  r  s      r   r  z&InvokeSubgraphCache.get_lazy_bwd_entry  s4     T000"":.223C\RRr   c                    | j                   j                  |d      x}r||k(  sJ d| d| d| d       || j                   |<   y)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  )r6   ry  effectsprev_effectss       r   add_effectszInvokeSubgraphCache.add_effects  sm    --11*dCC<Cl* ((2| 4<<H> J$$+9A/* *1:&r   c                :    | j                   j                  |d      S )zARetrieve the effect types for a given invoke_subgraph identifier.Nr  r  s     r   get_effectszInvokeSubgraphCache.get_effects  s    !!%%j$77r   Nr  r  r  r  r  r  r  )ry  rM   r  r(  rL   r   )ry  rM   rL   z
set | None)rP   rQ   rR   r  rz  r}  r  r  r  r  r  r  r  r  r   r   r   r  r    s|    	C?.94?		 (	 #		
 
	SS1>S	7S	18r   r  c                      e Zd ZddZddZy)HopDispatchSetCachec                2    ddl m} |t               i| _        y )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphr  r  hop_cache_map)r6   r  s     r   r  zHopDispatchSetCache.__init__  s    K-/B/DEr   c                >    || j                   vry | j                   |   S r   )r  )r6   ops     r   	get_cachezHopDispatchSetCache.get_cache  s$    T'''!!"%%r   Nr  )r  ztorch._ops.HigherOrderOperatorrL   zHopSubgraphCache | None)rP   rQ   rR   r  r  r   r   r   r  r    s    F&r   r  c                  \    e Zd Zedd       Zedd       Zd	dZed
d       Zedd       Zy)CompileContextc                 F    t         j                  J t         j                  S r   )_TLScompile_contextr   r   r   r  zCompileContext.get7  s     ##///###r   c                 $    t        t        dd       S Nr  getattrr  r   r   r   try_getzCompileContext.try_get<      t.55r   c                V    |t        |t              sJ || _        d| _        g | _        y rq   )r   r-   rD   rW   shape_env_guards)r6   rD   s     r   r  zCompileContext.__init__@  s-    !Z
I%FFF,6+-r   c                 H    t         j                         } | y | j                  S r   )r  r  rD   rZ   s    r   current_compile_idz!CompileContext.current_compile_idG  s"    %%'<r   c                     t         j                         } | y | j                  y t        | j                  | j                        S r   )r  r  rD   rV   rW   rZ   s    r   current_trace_idzCompileContext.current_trace_idN  s:    %%'<??"t55r   N)rL   r  )rL   CompileContext | None)rD   rO   rL   r   )rL   rO   )rL   zTraceId | None)	rP   rQ   rR   r   r  r  r  r  r  r   r   r   r  r  6  sU    $ $ 6 6.   6 6r   r  c                  6   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y)r^  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.
    c                 $    t        t        dd       S )Ntracing_contextr  r   r   r   r  zTracingContext.try_get`  r  r   c                 H    t         j                         x} r| S t        d      )Nz<TracingContext.get() must be called within an ongoing trace.)r^  r  RuntimeError)ctxs    r   r  zTracingContext.getd  s+     ((**3*JJ
 	
r   c                   t               | _        t               | _        t	               | _        t               | _        t               | _        || _	        g | _
        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d | _        d| _        t)               | _        d| _        t/               | _        g | _        y r  )ro  guards_contextr.  module_contextr>  global_contextr1  previously_inlined_functionspreviously_cleaned_instructions	fake_mode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)r6   r  s     r   r  zTracingContext.__init__l  s    +o+o+o<@F)?Cv,09AC  :>7;=A04-1?C*@D+ DH 490!8!:
 "'&9&;#+-r   c                    i | j                   _        | j                  j                          | j                  j                          y r   )r  r7  r  clearr  rZ   s    r   r  zTracingContext.clear  s6     ,.())//1,,224r   c               +  V  K   i }t         j                         }| D ]  }t        ||      ||<    | j                         D ]  \  }}t	        |||        	 d  |j                         D ]  \  }}t	        |||        y # |j                         D ]  \  }}t	        |||        w xY wwr   )r^  r  r  r?   setattr)kwargspriorr  r  vals        r   patchzTracingContext.patch  s        " 	+C c*E#J	+  	#HCCc"	#	'!KKM 'SS#&'EKKM 'SS#&'s   AB)A? &B)?'B&&B)c                     t         j                         } | t        j                         S | j                  }| j
                  || j                         gz   }t        j                  j                  |      S r   )r^  r  	tracebackStackSummaryr  r  _populate_loc_in_frame_summary	from_list)r6   r   s     r   r_  zTracingContext.extract_stack  sh    %%'<))++(((T@@BCCE%%//66r   c                p    | j                   J | j                   \  }}}t        j                  |||d      S )NF)lookup_line)r  r  FrameSummary)r6   filenamelineno
frame_names       r   r  z-TracingContext._populate_loc_in_frame_summary  s>      ,,,'+'8'8$&*%%h
PUVVr   c               #    K   t         j                         } t        j                  j                  j                  | dg       5  t        j                  j                  j                  | dd       5  	 d  	 d d d        d d d        y # t        $ r}t        |d      sd |_         d }~ww xY w# 1 sw Y   7xY w# 1 sw Y   y xY ww)Nr  r  
real_stack)	r^  r  unittestmockr  r   rB   hasattrr  )tcrI   s     r   clear_framezTracingContext.clear_frame  s      !MM&&r+@"E	MM&&r>4@		 	 	  $ q,/#'AL)	 	 	 	sT   A C,B7.B+0B5B7=	C	B(B##B((B++B4	0B77C <Cc              #    K   t         j                         }| |j                  j                  |        |j                  }d |_        	 d  	 | |j                  j                          ||_        y # t
        $ r'}t        |d      s|j                         |_         d }~ww xY w# | |j                  j                          ||_        w xY ww)Nr  )
r^  r  r  r   r  rB   r   r_  r  pop)frame_summaryr  oldrI   s       r   current_framezTracingContext.current_frame  s      !$""))-8oo		" (&&**,!BO  	1l+!//1	
 (&&**,!BOs6   ACA1 $C1	B!:"BB!!B$ $%C		Cc               #     K   t         j                         } | d  y | j                  }g | _        	 | j                   || _        y # || _        w xY wwr   )r^  r  r  )r  old_output_stridess     r   report_output_stridesz$TracingContext.report_output_strides  sY     
 ##%:J..	3### 2B 2Bs   /AA  A	AAc                <    | ||ft         j                         _        y r   )r^  r  r  )r  r  r  s      r   set_current_loczTracingContext.set_current_loc  s     .6vz,J)r   c                 H    t         j                         } | y | j                  S r   )r^  r  r  )r  s    r   get_traced_codezTracingContext.get_traced_code  s"    ##%:~~r   N)rL   TracingContext | None)rL   r^  )r  FakeTensorMode | NonerL   r   r  )r  r   rL   Generator[None, None, None])rL   ztraceback.StackSummary)rL   ztraceback.FrameSummary)rL   r  )r  ztraceback.FrameSummary | NonerL   r  )rL   z:Generator[list[tuple[int, ...] | None] | None, None, None])r  rM   r  r@   r  rM   rL   r   )rL   zlist[CodeType] | None)rP   rQ   rR   r   r   r  r  r  r  r   r  r_  r  
contextlibr  r  r
  r  r  r   r   r   r^  r^  X  s    6 6 
 
1.f5 '  ' 7 7W   < "4"	$"  "* 3  3 K K
  r   r^  c              #     K   t        t        dd       }| t        _        	 |  |t        _        y # |t        _        w xY wwr  )r  r  r  )contextold_contexts     r   r  r    s9      $ 148K"D+*{s   A 0 A =A c              #    K   t        t        dd      }| t        _        	 |  	 | F| j                  :| j                  j                  $| j                  j                  j                          |t        _        y# t        $ r)}t	        |d      s| | j                         |_         d}~ww xY w# | F| j                  :| j                  j                  $| j                  j                  j                          |t        _        w xY w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  Nr  )
r  r  r  rB   r   r_  r  r  	shape_envcleanup)r  r  rI   s      r   tracingr  )  s      $ 148K"D+ !!-!!++7''//1*  q,'G,?"002AL !!-!!++7''//1*s5   DA9 AD9	B+$B&&B++B. .ADDc                     y r   r   rC   r  s     r   dataclass_with_cached_hashr  F  r~  r   c                     y r   r   r  s     r   r  r  J  s     $'r   c                (    dfd}| |S  ||       S )Nc                |    t        j                  | fi }| j                  dfd}d }||_        ||_        |S )Nc                n    t        | d      st        j                  | d |              | j                  S )Nr   )r   r   __setattr__r   )r6   old_hashs    r   r   z:dataclass_with_cached_hash.<locals>.wrap.<locals>.__hash__X  s-    4)""4(4.A::r   c                r     t        j                         }t         fd|D              } j                  |fS )Nc              3  J   K   | ]  }t        |j                          y wr   )r  r   )rj  fr6   s     r   rk  zOdataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__.<locals>.<genexpr>b  s      G1qvv!6 Gs    #)dataclassesfieldstupler   )r6   r'  field_valuess   `  r   
__reduce__z<dataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__]  s4     !''-F  G GGLNNL11r   r   )r&  r   r   r*  )	cls_innernew_clsr   r*  r"  r  s       @r   wrapz(dataclass_with_cached_hash.<locals>.wrapT  sD    ''	<V<%%	
	2 $'r   )r+  type[T]rL   r.  r   )rC   r  r-  s    ` r   r  r  P  s    * {9r   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y)r   c                     yr  r   rZ   s    r   is_dict_keyzSource.is_dict_keys      r   c                     yr  r   rZ   s    r   is_ephemeralzSource.is_ephemeralv  r2  r   c                    t         r   NotImplementedError)r6   codegens     r   reconstructzSource.reconstructy  s    !!r   c                    t         r   r6  rZ   s    r   r   zSource.guard_source|  s    !!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.
        r6  rZ   s    r   _name_templatezSource._name_template  s
     "!r   c                    | j                   S r   )r<  rZ   s    r   r   zSource.name  s    """r   c                P    | |v r||    S t        | j                  ||      }||| <   |S r   )evalr<  )r6   globalslocalscacher   s        r   	get_valuezSource.get_value  s7     5=;T(('6:dr   c                ^    | j                   t        j                  u rt        t	        | |      S r   )r   r]   r   r7  r   )r6   r   s     r   
make_guardzSource.make_guard  s(     4 44%%T2r   c                6    | j                   j                         S r   )r   rx   rZ   s    r   rx   zSource.is_specialized_nn_module  s      99;;r   c                <    | j                   t        j                  k7  S )z+True if you can guard on attributes of this)r   r]   r   rZ   s    r   subguards_allowedzSource.subguards_allowed  s      K$?$???r   Nr   )r8  r%   rL   r   r   rK   r@  dict[str, Any]rA  rJ  rB  z&weakref.WeakKeyDictionary[Source, Any]rL   r   )r   zCallable[..., Any]rL   r   )rP   rQ   rR   r1  r4  r9  r   cached_propertyr   r   r<  r   rC  rE  rx   rH  r   r   r   r   r   q  s    " " " " " # #

 
 6	

 


<@r   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y	)ChainedSourcer   basec                6    | j                   j                         S r   )rN  r1  rZ   s    r   r1  zChainedSource.is_dict_key  s    yy$$&&r   c                6    | j                   j                         S r   )rN  r4  rZ   s    r   r4  zChainedSource.is_ephemeral  s    yy%%''r   c                .    | j                   j                  S r   )rN  r   rZ   s    r   r   zChainedSource.guard_source  s    yy%%%r   c                d    | }t        |t              r|j                  }t        |t              r|S r   )r   rM  rN  )r6   currents     r   get_basezChainedSource.get_base  s+    -0llG -0r   c                `    | j                   j                  | j                  j                        S r   )r<  r   rN  r   rZ   s    r   r   zChainedSource.name  s!    ""))$))..99r   c                    | |v r||    S d}d}||v rd| }|dz  }||v r| j                   j                  |||      ||<   t        | j                  j	                  |      ||      }||= ||| <   |S )Ntmpr   r^   )rN  rC  r?  r<  r   )r6   r@  rA  rB  tmpvarcounterr   s          r   rC  zChainedSource.get_value  s     5=;7)_FqLG  ,,WfeDvT((//7&I6Ndr   Nr   r   )rL   r   rK   rI  )rP   rQ   rR   rS   r1  r4  r   rK  r   rT  r   rC  r   r   r   rM  rM    su    
L'( & & : :  6	
 
r   rM  c                t   ddl m}m}m} g }t        j                         x}r"|j                  }||j                  |ddf       ddlm	} t        t         |                   D ]&  \  }}	t        |	|      s|j                  |	d|f       ( t        j                  |       }
t        |
      D ]  \  }}t        ||      r|j                  |j                  d|f       t        |      s<g } |||       |D cg c]  }t        ||      s| }}|j!                  t        |      D cg c]  \  }}|j                  d	| |f c}}        |r\|d   \  }}}|d
d D ]I  \  }	}}||	u rJ d| d| d| d|	 d| d| d| d| d|j"                   d| d| d|	j"                           |S yc c}w c c}}w )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_tensorsNztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor input)outzsubclass input r^   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\  r^  r  r  r   torch.utils._python_dispatchr^  	enumeratereversedr   pytreetree_leavesr   r   r   )inputsr[  r(   r\  
fake_modesr  r  r^  imflat_inputs
flat_inputr_  xfake_tensorsixtensordesc1i1desc2i2s                        r   detect_fake_modert    s&     J ((**w*%%	 y*;Q?@M(#C#EFG :1a(q"4a89:
 $$V,K";/ :j*-z335H!LM(4;=Cjc2.*Q
";.L .  '0&="F %%'<bA$ )!}	5"&qrN 	LAub> i[wat;PQRPSSZ[`Zaabcebf g""'"-=ioo=N O""'"-=aggY	H>	 1.
s   9F/F/*F4c                 ~    ddl m}  ddlm} t	        t         |                   D ]  \  }}t        ||       s|c S  y)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(   ra  r^  rb  rc  r   )r(   r^  rY   ri  s       r   active_fake_moderv    s?    
 =M(#C#EFG 1a(H r   )r  r  rL   z,Generator[CompileContext | None, None, None])r  r  rL   z,Generator[TracingContext | None, None, None])rC   r.  r  r   rL   r.  r   )rC   r   r  r   rL   zCallable[[type[T]], type[T]])rC   ztype[T] | Noner  r   rL   z&type[T] | Callable[[type[T]], type[T]])rf  r   rL   r  )rL   r  )b
__future__r   r  r&  enumr   loggingresys	threadingr  unittest.mockr  r   abcr   collectionsr   r   r   typingr   r	   r
   r   r   r   r   version_infor   r   torch.utilsr   rd  torch.utils._ordered_setr   ra  r   torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerrP   r   collections.abcr    r!   r"   typesr#   sympy"torch._dynamo.backends.distributedr$   torch._dynamo.codegenr%   &torch._functorch._aot_autograd.schemasr&   r`  r(   compiler;   r<   r-   rV   Enumr]   r   r   r   r   r   r   r   r  r	  r  r"  r.  r5  r>  rL  ro  ru  r  r  localr  r  r^  r  r  r  r   rM  rt  rv  r   r   r   <module>r     s   "      	 
      # % ! W W W w*  ) / F B 4 g! ==F/J<  RZZ PQ "

X " $D1/J /J 2/Jd7j 7<
$)) <
~	 	 d#  $   T"o' o' #o'd CL d#	 	 $	 d#:l : $: d#*\ * $*

8WQZ 8( (8( (0	+N#?@ 	+( (0N#?@ D,
 ,
^<N#89 <= =B>8* >8B
& 
& y&6 6DB BJ +"+1+ + +"+1+ +8 
 K 
 K 
'	' #'!' 
'
 	*-+ @ 4(6@ 6@ )6@t 4()F ) ))X?Dr   