
    3j                    n   S SK Jr  S SKrS SKrS SKrS SKrS SKrS SKrS SKrS SK	r	S SK
rS SKrS SKJr  S SKJr  S SKJr  S SKJr  S SKJrJrJrJrJrJr  S SKJr  S SKrS S	KJr  S S
K J!r!  S SK"J#r#  S SK$J%r%J&r&  S SK'J(r(  \RR                  " \*5      r+\(       a8  S SK,J-r-J.r.J/r/  S SK0J1r1  S SK2r2S SK3J4r4  S SK5J6r6  S SK7J8r8  S SK9J:r:  S SK;J<r<  S SK=J>r>   \R~                  " S5      r@\R~                  " S5      rA\" SSSS9 " S S5      5       rB " S S\5      rC " S S\R                  5      rE  " S  S!5      rF\R$                  " SS"9 " S# S$5      5       rG " S% S&\5      rH\R$                  " SS'9 " S( S)5      5       rI\" S*5      rJ \R$                  " SS"9 " S+ S,5      5       rK \R$                  " SS"9 " S- S.\K5      5       rL \R$                  " SS"9 " S/ S0\K5      5       rM  " S1 S2\\J   5      rN " S3 S45      rO " S5 S65      rP " S7 S8\N\P   5      rQ " S9 S:5      rR " S; S<\N\R   5      rS " S= S>5      rT  " S? S@\N\O   5      rU " SA SB5      rV\ " SC SD5      5       rW\ " SE SF5      5       rX " SG SH\V5      rY " SI SJ5      rZ\R                  " 5       r\  " SK SL5      r]\ " SM SN5      5       r^ " SO SP5      r_\    S\SQ j5       r`\    S]SR j5       ra\S^SS j5       rb\ S_     S`ST jj5       rb\" 5        S_     SaSU jj5       rb\b" SS"9 " SV SW5      5       rc\b" SS"9 " SX SY\c5      5       rdS_SbSZ jjreScS[ jrfg)d    )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                  V    \ rS rSr% S\S'   S\S'   SrS\S'   S
S jr\SS j5       rS	r	g)	CompileIdF   
int | Noneframe_idframe_compile_idNcompiled_autograd_idc                   U R                   b  U R                  S L U R                  S L :w  a%  [        SU R                   SU R                   35      eSnU R                  b  SU R                   SU R                   3nSU R                    U 3$ U R                  b  U R                  c%  [        SU R                   SU R                   35      eU R                   SU R                   3$ )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     G/home/wildlama/miniconda3/lib/python3.13/site-packages/torch/_guards.py__str__CompileId.__str__U   s   $$0%4+@+@D+HI$$$(MM?2EdF[F[E\^  I}}(a0E0E/FG	t001)==}}$(=(=(E$$$(MM?2EdF[F[E\^  mm_Ad&;&;%<==    c                <   Uc  g [         [        4 H`  nUR                  U5      =n(       d  M  UR                  5       nUR	                  5        H  u  pVUc  M
  [        U5      XE'   M     U " S0 UD6s  $    [        e! [         a  n[        SU S35      UeSnAff = f)zt
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)cls
compile_idpatternr:   groupskves           r1   from_stringCompileId.from_stringj   s     	J.0EF#MM*5555"__.F &=(+AFI !/ ==( G !  	J3J<qABI	Js(   %A< &A< A< 4A< <
BBBr7   returnstr)rA   
str | NonerJ   CompileId | None)
__name__
__module____qualname____firstlineno____annotations__r)   r2   classmethodrG   __static_attributes__r7   r4   r1   r$   r$   F   s8    
 !  (,*+
>* J Jr4   r$   c                  4    \ rS rSr% S\S'   S\S'   S	S jrSrg)
TraceId   r$   rA   r=   attemptc                    U R                   S:X  a  [        U R                  5      $ U R                   SU R                    3$ )Nr   _)rX   rK   rA   r/   s    r1   r2   TraceId.__str__   s7    <<1t''oo&a~66r4   r7   NrI   rN   rO   rP   rQ   rR   r2   rT   r7   r4   r1   rV   rV      s     L7r4   rV   c                      \ rS rSrSrSrSrSrSrSr	Sr
S	rS
rSrSrSrSrSrSrSrSrSS jrSS jrSS jrSS jrSS jrSrg)GuardSource   r                           	   
                     c                H    U [         R                  [         R                  4;   $ N)r_   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULEr[   s    r1   is_fsdp_moduleGuardSource.is_fsdp_module   s    668U8UVVVr4   c                    SS K Js  Jn  UR                  (       a:  U [        R
                  [        R                  4;   =(       d    U R                  5       $ U [        R
                  [        R                  4;   $ Nr   )torch._dynamo.config_dynamoconfig_unsafe_skip_fsdp_module_guardsr_   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULEru   )r/   r{   s     r1   is_specialized_nn_module$GuardSource.is_specialized_nn_module   sn    --11<<;; )
 &&( 4433
 
 	
r4   c                    U [         R                  [         R                  [         R                  [         R                  4;   $ rr   )r_   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULEr[   s    r1   is_unspecialized_nn_module&GuardSource.is_unspecialized_nn_module   s8    6655>>==	
 
 	
r4   c                H    U [         R                  [         R                  4;   $ rr   )r_   r   r   r[   s    r1   "is_unspecialized_builtin_nn_module.GuardSource.is_unspecialized_builtin_nn_module   s&    >>==
 
 	
r4   c                    U [         R                  [         R                  [         R                  [         R                  [         R
                  4;   $ rr   )r_   LOCALr~   rt   r   r   r[   s    r1   is_localGuardSource.is_local   sA    33))55==
 
 	
r4   r7   NrJ   bool)rN   rO   rP   rQ   r   GLOBALr~   r}   CONSTANTRANDOM_VALUE	SHAPE_ENVrt   rs   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALr   r   r   r   
TEMP_LOCALru   r   r   r   r   rT   r7   r4   r1   r_   r_      sy    EF"##$ HLINIO$&!%'",.)-/*JW
"


r4   r_   c                      \ rS rSrSrg)GuardBuilderBase   r7   NrN   rO   rP   rQ   rT   r7   r4   r1   r   r      s    r4   r   )r    c                  4    \ rS rSr% S\S'   S\S'   S	S jrSrg)
SLoc   z#traceback.FrameSummary | str | Noneframework_locrL   maybe_user_locc                    [        U R                  [        5      (       a  U R                  O[        U R                  5      nU R                  b  U R                   SU S3$ SU S3$ )Nz ()()
isinstancer   rK   r   r   )r/   flocs     r1   r2   SLoc.__str__   sk     $,,c22 d001 	
 *))*"TF!44tfA;r4   r7   NrI   r]   r7   r4   r1   r   r      s    66	r4   r   c                  4    \ rS rSr% S\S'   S\S'   S\S'   Srg	)

ShapeGuard   zsympy.logic.boolalg.Booleanexprr   slocr   size_obliviousr7   NrN   rO   rP   rQ   rR   rT   r7   r4   r1   r   r      s    
%%
Jr4   r   )r"   c                  b   \ rS rSr% S\S'   S\S'   SrS\S'   SrS\S	'   SrS
\S'   SrS\S'   Sr	S\S'   Sr
S\S'   SrS\S'   SrS\S'   S'S jrS(S jrS)S jrS*S jr\S+S j5       r\S,S j5       r\S-S j5       rS+S jrS+S jrS.S  jrS/S! jrS/S" jrS/S# jrS+S$ jr          S0S% jrS&rg)1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                    U R                   c:  [        U R                  U R                  [	        U R
                  5      45      U l         U R                   $ rr   )r   hashnamesourceidr   r[   s    r1   __hash__Guard.__hash__  s:    ::tyy$++r$..7IJKDJzzr4   c                   [        U R                  [        R                  5      =(       aD    U R                  R                  [
        R                  R                  R                  R                  L nUU R                  (       a  U R                  R                  OS[        U R                  5      U R                  U R                  5       R                  R                   4$ )N)r   r   	functoolspartialfunctorchrz   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r/   is_duplicate_inputs     r1   sort_keyGuard.sort_key  s    
 t~~y'8'89 Y##u}}';';'H'H'X'XX 	
 !%DKK"		NII  "++::
 	
r4   c                D    U R                  5       UR                  5       :  $ rr   )r   r/   others     r1   __lt__Guard.__lt__-  s    }}!111r4   c                    [        U R                  [        R                  5      (       a  U R                  R                  $ U R                  $ rr   )r   r   r   r   r   r[   s    r1   r   Guard.inner_create_fn0  s3    dnni&7&788>>&&&>>!r4   c                .    U R                   R                  $ rr   )r   r   r[   s    r1   r   
Guard.name6  s    &&+++r4   c                .    U R                   R                  $ rr   )r   guard_sourcer[   s    r1   r   Guard.source:  s    &&333r4   c           
     "   [        U [        R                  5      (       af  U " 5       nUbD  S[        [	        U 5      5       SUR
                  R                   S[        [	        U5      5       S3$ S[        [	        U 5      5       S3$ [        U 5      $ )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__rN   rK   )r   objs     r1   weakref_to_strGuard.weakref_to_str>  s      k7#8#899-C%c"[/&:%;6#--BXBXAYY^_bceficj_k^llmnn%c"[/&:%;7CC{##r4   c                d   SU R                   (       a$  U R                   R                  R                  5       OS S[        U R                  5       SU R	                  5       R
                   SU R                   SU R                   SU R                  U R                  5       SU R                   S3nU$ )	Nz	
        r+    z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )r   r   lowerreprr   rN   r   r   r   r   r   )r/   ss     r1   __repr__Guard.__repr__W  s    	%)[[					!b94		?:K1TMaMaMcMlMlLm n ,,- .^^$ % //0@0@AB C"889 :	 r4   c                   S[        U R                  5       S3nU R                  (       a$  U R                  R                  R                  5       OSnUSU S3-  nUSU R	                  5       R
                   S3-  nUSU R                   S3-  nUSU R                   S3-  nUSU R                  U R                  5       S3-  nUS	U R                   S3-  nU$ )
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   rN   r   r   r   r   r   )r/   outputr   s      r1   r2   Guard.__str__c  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r4   c           	     \    U R                  X5      $ ! [         a    [        R                  S[	        U 5      R                  5       5        U R                  (       aN  [        R                  SSR                  U R                  R                  5       SS  5      R                  5       5        e f = f)NzError while creating guard:
%szCreated at:
%sr+   )
r   r?   log	exceptionrK   rstripr   errorjoinformat)r/   builders     r1   createGuard.createn  s    	>>'00 	MM;SY=M=M=OPzz		+RWWTZZ5F5F5H5M-N-U-U-WX		s
    BB+c                6    U R                   R                  5       $ rr   )r   r   r[   s    r1   r   Guard.is_specialized_nn_modulew  s    {{3355r4   c                6    U R                   R                  5       $ rr   )r   ru   r[   s    r1   ru   Guard.is_fsdp_modulez  s    {{))++r4   c                6    U R                   R                  5       $ rr   )r   r   r[   s    r1   r   Guard.is_local}  s    {{##%%r4   c                    [        U R                  [        R                  5      (       a"  U R                  R                  nUR
                  $ U R                  nUR
                  $ rr   )r   r   r   r   r   rN   )r/   r   s     r1   create_fn_nameGuard.create_fn_name  sM    dnni&7&788++I !!! I!!!r4   c                   U R                   (       d  / U l         U R                   R                  U5        U R                  US 4;  a  [        SU R                   SU 35      eX l        U R                  (       d  X0l        OU R                  R                  U5        U R                  US 4;   =(       d.    [        U R                  5      =(       a    U R                  5       S L nU(       d  [        SU R                   SU 35      eX@l        g )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)r/   
guard_typeguarded_classr   r   is_valids         r1   set_export_infoGuard.set_export_info  s    !D
+%%mT-BB 112$}oG  &3"~~&NNN!!), d 33 +(() +  "d* 	
  ''([M;  'r4   )r   r   r   r   r   rJ   r=   )rJ   ztuple[bool, int, int, str, int])r   r   rJ   r   )rJ   z(Callable[[GuardBuilderBase, Guard], Any]rI   rJ   r_   )r   objectrJ   rK   )r   r   rJ   r   r   )
r  rK   r  r   r   	list[str]r   r  rJ   None)rN   rO   rP   rQ   rR   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r2   r   r   ru   r   r  r  rT   r7   r4   r1   r   r      s   & 88 %)K!("&I&!%K%?C<C&*E#*04J-4E:!OT!

 2" , , 4 4 $ $0
	6,&"&'&' 9&' 	&'
 &' 
&'r4   r   Tc                      \ rS rSrSrg)GuardEnvExpri  r7   Nr   r7   r4   r1   r  r    s    r4   r  c                  4    \ rS rSr% S\S'   S\S'   SS jrSrg)	DuplicateInputsi  r   input_source_ainput_source_bc                h    U R                   U R                  :X  a  [        SU R                    35      eg )Nz9input_source_a and input_source_b must be different, got )r  r   r.   r[   s    r1   __post_init__DuplicateInputs.__post_init__  s>    $"5"55 **+-  6r4   r7   NrJ   r  )rN   rO   rP   rQ   rR   r"  rT   r7   r4   r1   r  r    s    r4   r  c                  *    \ rS rSr% S\S'   S\S'   Srg)StorageOverlapi  zlist[Source]overlapping_sourcesnon_overlapping_sourcesr7   Nr   r7   r4   r1   r&  r&    s    %%))r4   r&  c                  <    \ rS rSr\SS j5       r\SS j5       rSrg)Checkpointablei  c                    g rr   r7   r[   s    r1   copy_graphstateCheckpointable.copy_graphstate  s    $'r4   c                    g rr   r7   r/   states     r1   restore_graphstate!Checkpointable.restore_graphstate  s    47r4   r7   N)rJ   r  )r0  r  rJ   r  )rN   rO   rP   rQ   r   r,  r1  rT   r7   r4   r1   r*  r*    s    ' '7 7r4   r*  c                  B    \ rS rSr% SrS\S'   S
S jrSS jrSS jrSr	g	)GuardsCheckpointStatei  zO
The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext
OrderedSet[Guard]dynamo_guardsc                    Xl         g rr   r6  )r/   r6  s     r1   __init__GuardsCheckpointState.__init__  s    *r4   c                p    U R                   R                  UR                   5      n[        U5      S:X  a  gU$ )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)r6  
differencer   r/   r   rs      r1   diffGuardsCheckpointState.diff  s3     ))%*=*=>q6Q;r4   c                T    [        U[        5      (       d  gU R                  U5      S L $ NF)r   r4  r?  r   s     r1   __eq__GuardsCheckpointState.__eq__  s'    %!677yy4''r4   r8  N)r6  r5  rJ   r  )r   r4  rJ   OrderedSet[Guard] | Noner   r  rJ   r   )
rN   rO   rP   rQ   __doc__rR   r9  r?  rC  rT   r7   r4   r1   r4  r4    s     %$+
(r4   r4  c                  B    \ rS rSr% 0 rS\S'   S	S jrS
S jrSS jrSr	g)ModuleContextCheckpointStatei  dict[str, torch.nn.Module]
nn_modulesc                    Xl         g rr   rK  )r/   rK  s     r1   r9  %ModuleContextCheckpointState.__init__  s    $r4   c                    [        U R                  R                  5       5      R                  [        UR                  R                  5       5      5      n[	        U5      S:X  a  gU$ )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)setrK  keysr<  r   r=  s      r1   r?  !ModuleContextCheckpointState.diff  sM     $$&'223u7G7G7L7L7N3OPq6Q;r4   c                T    [        U[        5      (       d  gU R                  U5      S L $ rB  )r   rI  r?  r   s     r1   rC  #ModuleContextCheckpointState.__eq__)  '    %!=>>yy4''r4   rM  N)rK  rJ  rJ   r  )r   rI  rJ   set[str] | NonerF  )
rN   rO   rP   rQ   rK  rR   r9  r?  rC  rT   r7   r4   r1   rI  rI    s    -/J*/%
(r4   rI  c                  2    \ rS rSrSS jrSS jrS	S jrSrg)
ModuleContexti/  c                    0 U l         g rr   rM  r[   s    r1   r9  ModuleContext.__init__0  s	    *,r4   c                >    [        [        U R                  5      5      $ rr   )rI  dictrK  r[   s    r1   r,  ModuleContext.copy_graphstate3  s    +D,ABBr4   c                ~    [        U[        5      (       d  [        S[        U5       35      eUR                  U l        g )Nz+expected ModuleContextCheckpointState, got )r   rI  r.   typerK  r/  s     r1   r1   ModuleContext.restore_graphstate6  s:    %!=>> =d5k]K   **r4   rM  Nr$  )rJ   rI  )r0  rI  rJ   r  )rN   rO   rP   rQ   r9  r,  r1  rT   r7   r4   r1   rX  rX  /  s    -C+r4   rX  c                  B    \ rS rSr% 0 rS\S'   S	S jrS
S jrSS jrSr	g)GlobalContextCheckpointStatei>  dict[str, tuple[Callable, Any]]global_statec                    Xl         g rr   rd  )r/   global_statess     r1   r9  %GlobalContextCheckpointState.__init__A  s    )r4   c                    [        U R                  R                  5       5      R                  [        UR                  R                  5       5      5      n[	        U5      S:X  a  gU$ )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)rP  rd  rQ  r<  r   r=  s      r1   r?  !GlobalContextCheckpointState.diffD  sO     !!&&()44S9K9K9P9P9R5STq6Q;r4   c                T    [        U[        5      (       d  gU R                  U5      S L $ rB  )r   rb  r?  r   s     r1   rC  #GlobalContextCheckpointState.__eq__P  rU  r4   rf  N)rg  rc  rJ   r  )r   rb  rJ   rV  rF  )
rN   rO   rP   rQ   rd  rR   r9  r?  rC  rT   r7   r4   r1   rb  rb  >  s    46L16*
(r4   rb  c                  >    \ rS rSrSr1 SkrS	S jrS
S jrSS jrSr	g)GlobalContextiV  zn
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                    0 U l         g rr   rf  r[   s    r1   r9  GlobalContext.__init__e  s
    =?r4   c                ,    [        U R                  5      $ rr   )rb  rd  r[   s    r1   r,  GlobalContext.copy_graphstateh  s    +D,=,=>>r4   c                   [        U[        5      (       d  [        S[        U5       35      eUR                  U l        [        U R                  5      [        U R                  5      :X  a1  [        U R                  R                  5       5      U R                  :X  d<  [        S[        U R                  R                  5       5       SU R                   35      eU R                  R                  5        H  u  p#U" U5        M     g )Nz+expected GlobalContextCheckpointState, got z Global state mismatch: got keys z, expected )
r   rb  r.   r_  rd  r   _supported_global_statesrP  rQ  values)r/   r0  r   argss       r1   r1   GlobalContext.restore_graphstatek  s    %!=>> =d5k]K  "..!!"c$*G*G&HHD%%**,-1N1NN 23t7H7H7M7M7O3P2Q R 99:<  ++224JDJ 5r4   rf  Nr$  )rJ   rb  )r0  rb  rJ   r  )
rN   rO   rP   rQ   rG  rz  r9  r,  r1  rT   r7   r4   r1   rn  rn  V  s    
 @?r4   rn  c                      \ rS rSrSSS jjrSS jrSS jrSS jrSS jrSS jr	SS	 jr
S
SS.       SS jjrSS jrSS jrSS jrSrg)	GuardsSeti  Nc                    Uc  [        5       U l        OXl        [        [        5      U l        U R                   H  nU R                  U5        M     g rr   )r   innerr   listsource_to_guardstrack_guard_by_source)r/   r  guards      r1   r9  GuardsSet.__init__  sB    =,6LDJJ CNdBSZZE&&u-  r4   c                X    UR                   nU R                  U   R                  U5        g rr   )r   r  r  )r/   r  r   s      r1   r  GuardsSet.track_guard_by_source  s&    ))f%,,U3r4   c                ,    [        U R                  5      $ rr   )iterr  r[   s    r1   __iter__GuardsSet.__iter__      DJJr4   c                ,    [        U R                  5      $ rr   )r   r  r[   s    r1   __len__GuardsSet.__len__  s    4::r4   c                F    [        U R                  UR                  -
  5      $ rr   )r  r  r   s     r1   __sub__GuardsSet.__sub__  s    ekk122r4   c                ,    [        U R                  5      $ rr   )r   r  r[   s    r1   __bool__GuardsSet.__bool__  r  r4   c                J    [        5       U l        [        [        5      U l        g rr   )r   r  r   r  r  r[   s    r1   clearGuardsSet.clear  s    \
 +D 1r4   Tr   )collect_debug_stackskipc               (   XR                   ;   a  g U(       a)  UR                  c  [        R                  " SU-   S9Ul        UR                  c  [
        R                  5       Ul        U R                   R                  U5        U R                  U5        g Nra   )r  )	r  r   r   extractr   TracingContextextract_stackaddr  )r/   r  r  r  s       r1   r  GuardsSet.add  so     JJ{{"/77QXF#-;;=E

u""5)r4   c                H    U H  nU H  nU R                  USS9  M     M     g r  )r  )r/   othersogs       r1   updateGuardsSet.update  s&    A#  r4   c                2    [        U R                  U   5      $ )z4Return all guards with the given originating_source.)r  r  )r/   r   s     r1   get_guards_for_sourceGuardsSet.get_guards_for_source  s    D))&122r4   c                   ^^ SSK Jm  [        UU4S jU R                   5       5      U l        [	        [
        5      U l        U R                   H  nU R                  U5        M     g)z.Delete all guards that contains a given sourcera   )is_from_sourcec              3  \   >#    U  H!  nT" UR                   T5      (       a  M  Uv   M#     g 7frr   )r   ).0r  r  r   s     r1   	<genexpr>6GuardsSet.remove_guards_with_source.<locals>.<genexpr>  s%       
!!8L8Lf)UAAzs   ,	,N)_dynamo.sourcer  r   r  r   r  r  r  )r/   r   r  r  s    ` @r1   remove_guards_with_source#GuardsSet.remove_guards_with_source  sN    2  
zz 
 


 !,D 1ZZE&&u-  r4   )r  r  rr   )r  rE  rJ   r  )r  r   rJ   r  )rJ   zIterator[Guard]r  )r   r  rJ   r  r   r$  )r  r   r  r   r  r=   rJ   r  )r  z
set[Guard]rJ   r  )r   r   rJ   zlist[Guard])r   r   rJ   r  )rN   rO   rP   rQ   r9  r  r  r  r  r  r  r  r  r  r  rT   r7   r4   r1   r  r    sa    	.4 
3 2
 <@Q**48*GJ*	*$
3.r4   r  c                  Z    \ rS rSrSS jr\R                  S	S j5       rS
S jrSS jr	Sr
g)GuardsContexti  c                >    [        5       U l        / U l        SU l        g rB  )r  r6  aotautograd_guardsskip_installr[   s    r1   r9  GuardsContext.__init__  s    (168"'r4   c              #  ^   #    U R                   nSU l          S v   Xl         g ! Xl         f = f7f)NT)r  )r/   olds     r1   skip_guard_install GuardsContext.skip_guard_install  s,      	$ #s   -" -*-c                R    [        [        U R                  R                  5      5      $ rr   )r4  r   r6  r  r[   s    r1   r,  GuardsContext.copy_graphstate  s    $Z0B0B0H0H%IJJr4   c                    [        U[        5      (       d  [        S[        U5       35      e[	        UR
                  5      U l        g )Nz$expected GuardsCheckpointState, got )r   r4  r.   r_  r  r6  r/  s     r1   r1   GuardsContext.restore_graphstate  s:    %!677 #GU}!UVV&u':':;r4   )r  r6  r  Nr$  rJ   Generator[None, None, None])rJ   r4  )r0  r4  rJ   r  )rN   rO   rP   rQ   r9  
contextlibr   r  r,  r1  rT   r7   r4   r1   r  r    s,    (
 $ $K<r4   r  c                      \ rS rSr\      SS j5       r\SS j5       r\SS j5       r\SS j5       r\SS j5       r	\SS j5       r
\        SS j5       r\      SS	 j5       rS
rg)HopSubgraphCachei  c                    g rr   r7   r/   fn_code
identifiers      r1   add_dynamo_installed_submodule/HopSubgraphCache.add_dynamo_installed_submodule  s     r4   c                    g rr   r7   r/   r  s     r1   get_dynamo_installed_submodules0HopSubgraphCache.get_dynamo_installed_submodules  s    ORr4   c                    g rr   r7   r/   r  keys      r1   add_autograd_key_entry'HopSubgraphCache.add_autograd_key_entry  s    NQr4   c                    g rr   r7   r/   r  s     r1   get_autograd_key_entry'HopSubgraphCache.get_autograd_key_entry  s    JMr4   c                    g rr   r7   r  s      r1   add_proxy_dispatch_entry)HopSubgraphCache.add_proxy_dispatch_entry  s    PSr4   c                    g rr   r7   r  s     r1   get_proxy_dispatch_entry)HopSubgraphCache.get_proxy_dispatch_entry  s    LOr4   c                    g rr   r7   )r/   r  tangent_metadatagmods       r1   add_lazy_bwd_entry#HopSubgraphCache.add_lazy_bwd_entry  s     r4   c                    g rr   r7   r/   r  r  s      r1   get_lazy_bwd_entry#HopSubgraphCache.get_lazy_bwd_entry  s     :=r4   r7   Nr  r   r  rK   rJ   r  r  r   rJ   r  r  rK   r  r   rJ   r  r  rK   rJ   zCallable | Noner  rK   r  tuple[object]r  torch.fx.GraphModulerJ   r=   r  rK   r  r  rJ   z.tuple[torch.fx.GraphModule | None, int | None])rN   rO   rP   rQ   r   r  r  r  r  r  r  r  r  rT   r7   r4   r1   r  r    s    -0	  R RQ QM MS SO O ( #	
 
  ==1>=	7= =r4   r  c                  j    \ rS rSr% S\S'   S\S'   S\S'   S\S	'   S
\S'   S\S'   S\S'   SrS\S'   Srg)InvokeSubgraphReuseEntryi	  rK   	body_namer  	body_gmodz!NestedCompileRegionOptions | Noner{   	list[Any]subgraph_input_mappingr   single_tensor_outputzIlist[tuple[torch.Size, tuple[int, ...], torch.dtype, torch.device, bool]]output_metadatazlist[Source | None]arg_sourcesr   r=   num_user_outputsr7   N)rN   rO   rP   rQ   rR   r  rT   r7   r4   r1   r  r  	  sD    N##--    %$ cr4   r  c                  b    \ rS rSr% S\S'   S\S'   SrS\S'   \R                  " \S	9r	S
\S'   Sr
g)InvokeSubgraphReuseConditioni#  zlist[tuple[Any, object]]input_checksz2list[tuple[Source, GuardCheckSpec, object, Guard]]r   Nzpytree.TreeSpec | Nonetreespec)default_factoryzOrderedSet[Source]traced_sourcesr7   )rN   rO   rP   rQ   rR   r  dataclassesfieldr   r  rT   r7   r4   r1   r  r  #  s7     +*
 ?> (,H$+
 *5):)::)VN&Vr4   r  c                     \ rS rSrSS jr      SS jrSS jrSS jrSS jrSS jr	SS jr
        SS	 jr      SS
 jrSS jrSS jr S         SS jjr      SS jr      SS jr S         S S jjrSrg)!InvokeSubgraphCachei>  c                    0 U l         0 U l        [        [        5      U l        [        [
        5      U l        0 U l        [        [        5      U l        [        [
        5      U l	        g rr   )
autograd_cacheproxy_dispatch_cacher   r  dynamo_installed_submodulesr\  lazy_bwd_cacheeffects_cachesubgraph_reuse_cachesubgraph_reuse_key_cacher[   s    r1   r9  InvokeSubgraphCache.__init__?  sc    359;!FQRVFW(  	
  	  	!  	%r4   c                @    U R                   U   R                  U5        g rr   )r	  r  r  s      r1   r  2InvokeSubgraphCache.add_dynamo_installed_submoduleU  s     	((188Dr4   c                :    U R                   R                  U/ 5      $ rr   )r	  getr  s     r1   r  3InvokeSubgraphCache.get_dynamo_installed_submodulesZ  s    //33GR@@r4   c                     X R                   U'   g rr   )r  r  s      r1   r  *InvokeSubgraphCache.add_autograd_key_entry]  s    *-J'r4   c                :    U R                   R                  US 5      $ rr   )r  r  r  s     r1   r  *InvokeSubgraphCache.get_autograd_key_entry`  s    ""&&z488r4   c                     X R                   U'   g rr   )r  r  s      r1   r  ,InvokeSubgraphCache.add_proxy_dispatch_entryc  s    03!!*-r4   c                :    U R                   R                  US 5      $ rr   )r  r  r  s     r1   r  ,InvokeSubgraphCache.get_proxy_dispatch_entryf  s    ((,,Z>>r4   c                \    [        U R                  U   5      nX44U R                  U   U'   U$ rr   )r   r
  )r/   r  r  r  	num_gmodss        r1   r  &InvokeSubgraphCache.add_lazy_bwd_entryi  s8     ++J78	=A<MJ'(89r4   c                `    XR                   ;  a  gU R                   U   R                  US5      $ )N)NN)r
  r  r  s      r1   r  &InvokeSubgraphCache.get_lazy_bwd_entryt  s2     000"":.223C\RRr4   c           	         U R                   R                  US5      =n(       a  X#:w  a  [        SU SU SU S35      eX R                   U'   g)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.   )r/   r  effectsprev_effectss       r1   add_effectsInvokeSubgraphCache.add_effects|  sj    --11*dCC<C&$,,6< 8@@L~ N((/y3  *1:&r4   c                :    U R                   R                  US5      $ )zARetrieve the effect types for a given invoke_subgraph identifier.N)r  r  r  s     r1   get_effectsInvokeSubgraphCache.get_effects  s    !!%%j$77r4   c                    U R                   U   n[        U5      U:  a  [        SU SU S35      eUR                  X#45        g )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  )r/   r  	conditionentrymax_reuse_entriesentriess         r1   add_reuse_entry#InvokeSubgraphCache.add_reuse_entry  s^     ++G4w<,,%&&:7) DIJ	 	 		)*r4   c                    U R                   R                  U/ 5      n[        U5       H@  u  nu  pVU" XV5      (       d  M  US:  a!  UR                  SUR	                  U5      5        Us  $    g rx   )r  r  	enumerateinsertpop)r/   r  	evaluatorr1  ir.  r/  s          r1   find_reuse_entry$InvokeSubgraphCache.find_reuse_entry  se     ++//<%.w%7!A!	**q5NN1gkk!n5 &8 r4   c                X    U R                   R                  U0 5      R                  U5      $ rr   )r  r  )r/   r  hash_keys      r1   find_reuse_entry_by_key+InvokeSubgraphCache.find_reuse_entry_by_key  s(     ,,00"=AA(KKr4   c                v    U R                   U   n[        U5      U:  a  X%;  a  [        SU SU S35      eX5U'   g )Nr+  r,  zc (hash-key path). Increase the limit via the max_reuse_entries argument to nested_compile_region().)r  r   r-  )r/   r  r=  r/  r0  	key_caches         r1   add_reuse_entry_by_key*InvokeSubgraphCache.add_reuse_entry_by_key  sZ     11':	y>..83L%&&:7) D+,  $(r4   )r  r	  r  r
  r  r  r  Nr$  r  r  r  r  r  r  )r  rK   r#  rP  rJ   r  )r  rK   rJ   z
set | None)rh   )
r  r   r.  r  r/  r  r0  r=   rJ   r  )r  r   r8  zHCallable[[InvokeSubgraphReuseCondition, InvokeSubgraphReuseEntry], bool]rJ   InvokeSubgraphReuseEntry | None)r  r   r=  r=   rJ   rD  )
r  r   r=  r=   r/  r  r0  r=   rJ   r  )rN   rO   rP   rQ   r9  r  r  r  r  r  r  r  r  r%  r(  r2  r:  r>  rB  rT   r7   r4   r1   r  r  >  sS   ,EE-0E	E
A.94?		 (	 #		
 
	SS1>S	7S
18 "#++ 0+ (	+
 + 
+*
 
) LL+.L	(L "#$$ $ (	$
 $ 
$ $r4   r  c                  (    \ rS rSrSS jrSS jrSrg)HopDispatchSetCachei  c                2    SSK Jn  U[        5       0U l        g )Nr   )invoke_subgraph)'torch._higher_order_ops.invoke_subgraphrH  r  hop_cache_map)r/   rH  s     r1   r9  HopDispatchSetCache.__init__  s    K-/B/DEr4   c                @    XR                   ;  a  g U R                   U   $ rr   rJ  )r/   ops     r1   	get_cacheHopDispatchSetCache.get_cache  s"    '''!!"%%r4   rM  Nr$  )rN  ztorch._ops.HigherOrderOperatorrJ   zHopSubgraphCache | None)rN   rO   rP   rQ   r9  rO  rT   r7   r4   r1   rF  rF    s    F&r4   rF  c                  n    \ rS rSr\S	S j5       r\S
S j5       rSS jr\SS j5       r\SS j5       r	Sr
g)CompileContexti  c                 Z    [         R                  c  [        S5      e[         R                  $ )Nzcompile_context is not set)_TLScompile_contextr.   r7   r4   r1   r  CompileContext.get  s&    ' !=>>###r4   c                 $    [        [        SS 5      $ NrU  getattrrT  r7   r4   r1   try_getCompileContext.try_get      t.55r4   c                    Ub,  [        U[        5      (       d  [        S[        U5       35      eXl        SU l        / U l        g )Nz*compile_id must be None or CompileId, got r   )r   r$   r.   r_  rA   rX   shape_env_guards)r/   rA   s     r1   r9  CompileContext.__init__  sG    !*Z*K*K <T*=M<NO  -7+-r4   c                 J    [         R                  5       n U c  g U R                  $ rr   )rR  r[  rA   r[   s    r1   current_compile_id!CompileContext.current_compile_id  s"    %%'<r4   c                     [         R                  5       n U c  g U R                  c  g [        U R                  U R                  5      $ rr   )rR  r[  rA   rV   rX   r[   s    r1   current_trace_idCompileContext.current_trace_id  s:    %%'<??"t55r4   )rX   rA   r_  N)rJ   rR  )rJ   CompileContext | None)rA   rM   rJ   r  )rJ   rM   )rJ   zTraceId | None)rN   rO   rP   rQ   r  r  r[  r9  rb  re  rT   r7   r4   r1   rR  rR    sU    $ $
 6 6.   6 6r4   rR  c                  8    \ rS rSr% SrS\S'   S\S'   S\S'   S	rg
)InlinedCodeCachei  z8Cache for code-object-derived data used during inlining.r  instructionszdict[Any, int]indexofdict[str, Any]code_optionsr7   N)rN   rO   rP   rQ   rG  rR   rT   r7   r4   r1   ri  ri    s    B  r4   ri  c                  V   \ rS rSrSr\SS j5       r\SS j5       rSS jrSS jr	\\
SS j5       5       r\SS j5       rSS	 jr\\R                  SS
 j5       5       r\\R                      SS j5       5       r\\R                  SS j5       5       r\SS j5       r\SS j5       rSrg)r  i  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                 $    [        [        SS 5      $ )Ntracing_contextrY  r7   r4   r1   r[  TracingContext.try_get!  r]  r4   c                 R    [         R                  5       =n (       a  U $ [        S5      e)Nz<TracingContext.get() must be called within an ongoing trace.)r  r[  r-  )ctxs    r1   r  TracingContext.get%  s+     ((**3*JJ
 	
r4   c                   [        5       U l        [        5       U l        [	        5       U l        [        5       U l        [        5       U l        [        5       U l	        Xl
        / U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        S U l        SU l        [+        5       U l        SU l        [1        5       U l        / U l        S U l        g rB  )r  guards_contextrX  module_contextrn  global_contextr\  previously_inlined_functionspreviously_cleaned_instructionsinlined_code_cache	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_callrF  hop_dispatch_set_cachetraced_codecudagraph_annotation)r/   r|  s     r1   r9  TracingContext.__init__-  s    +o+o+o<@F)?Cv,?Cv09AC  :>7;=A04-1?C*@D+ DH 490!8!:
 "'&9&;#+-)-!r4   c                    0 U R                   l        U R                  R                  5         U R                  R                  5         U R
                  R                  5         g rr   )rx  rd  ry  r  rz  r{  r[   s    r1   r  TracingContext.clearc  sH     ,.())//1,,224%%'r4   c               +  V  #    0 n[         R                  5       nU  H  n[        X#5      X'   M     U R                  5        H  u  p4[	        X#U5        M      S v   UR                  5        H  u  p4[	        X#U5        M     g ! UR                  5        H  u  p4[	        X#U5        M     f = f7frr   )r  r  rZ  r<   setattr)kwargspriorrs  r  vals        r1   patchTracingContext.patchk  s        "C *EJ  HCCc" '	'!KKM#& *EKKM#& *s   AB)A? &B)?'B&&B)c                     [         R                  5       n U c  [        R                  " 5       $ U R                  nU R
                  b  XR                  5       /-   n[        R                  R                  U5      $ rr   )r  r[  	tracebackStackSummaryr}  r~  _populate_loc_in_frame_summary	from_list)r/   r   s     r1   r  TracingContext.extract_stack|  sf    %%'<))++(((@@BCCE%%//66r4   c                |    U R                   c  [        S5      eU R                   u  pn[        R                  " XUSS9$ )Nzloc_in_frame must not be NoneF)lookup_line)r~  r.   r  FrameSummary)r/   filenamelineno
frame_names       r1   r  -TracingContext._populate_loc_in_frame_summary  s@    $ !@AA'+'8'8$*%%h
PUVVr4   c               #    #    [         R                  5       n [        R                  R                  R                  U S/ 5         [        R                  R                  R                  U SS 5          S v    S S S 5        S S S 5        g ! [         a  n[        US5      (       d  S Ul        e S nAff = f! , (       d  f       NB= f! , (       d  f       g = f7f)Nr}  r~  
real_stack)	r  r  unittestmockr  r  r?   hasattrr  )tcrF   s     r1   clear_frameTracingContext.clear_frame  s      !MM&&r+@"EMM&&r>4@ A FE
  $ q,//#'AL)	 A@ FEsT   A C,C.B10B5C=	C
B.B))B..B11
B?	;C
CCc              #    #    [         R                  5       nU b  UR                  R                  U 5        UR                  nS Ul         S v    U b  UR                  R                  5         X!l        g ! [
         a,  n[        US5      (       d  UR                  5       Ul        e S nAff = f! U b  UR                  R                  5         X!l        f = f7f)Nr  )
r  r  r}  r  r~  r?   r  r  r  r7  )frame_summaryr  r  rF   s       r1   current_frameTracingContext.current_frame  s      !$""))-8oo		" (&&**,!O  	1l++!//1	
 (&&**,!Os6   AC	A2 $C2
B(<'B##B((B+ +%CCc               #     #    [         R                  5       n U c  S v   g U R                  n/ U l         U R                  v   Xl        g ! Xl        f = f7frr   )r  r[  r  )r  old_output_stridess     r1   report_output_strides$TracingContext.report_output_strides  sT     
 ##%:J..	3### 2 2s   0AA AAAc                :    XU4[         R                  5       l        g rr   )r  r  r~  )r  r  r  s      r1   set_current_locTracingContext.set_current_loc  s     .6z,J)r4   c                 J    [         R                  5       n U c  g U R                  $ rr   )r  r[  r  )r  s    r1   get_traced_codeTracingContext.get_traced_code  s"    ##%:~~r4   )r  r  r  r|  r  r  r}  r  rx  rv  r  r{  r~  rw  r  r  r  r  rz  ry  r  r  N)rJ   TracingContext | None)rJ   r  )r|  FakeTensorMode | NonerJ   r  r$  )r  r   rJ   r  )rJ   ztraceback.StackSummary)rJ   ztraceback.FrameSummaryr  )r  ztraceback.FrameSummary | NonerJ   r  )rJ   z:Generator[list[tuple[int, ...] | None] | None, None, None])r  rK   r  r=   r  rK   rJ   r  )rJ   zlist[CodeType] | None)rN   rO   rP   rQ   rG  r  r[  r  r9  r  r   r  r  r  r  r  r  r  r  r  rT   r7   r4   r1   r  r    s    6 6 
 
4.l( '  ' 7 7W   < "4"	$"  "* 3  3 K K
  r4   r  c              #     #    [        [        SS 5      nU [        l         U v   U[        l        g ! U[        l        f = f7frX  )rZ  rT  rU  )contextold_contexts     r1   rU  rU    s8      $ 148K"D+*{s   A 0 A =A c              #  *  #    [        [        SS5      nU [        l         U v    U bH  U R                  b;  U R                  R                  b$  U R                  R                  R                  5         U[        l        g! [         a/  n[	        US5      (       d  U b  U R                  5       Ul        e SnAff = f! U bH  U R                  b;  U R                  R                  b$  U R                  R                  R                  5         U[        l        f = f7f)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.
rp  Nr  )
rZ  rT  rp  r?   r  r  r  r|  	shape_envcleanup)r  r  rF   s      r1   tracingr    s      $ 148K"D+ !!-!!++7''//1*  q,''G,?"002AL !!-!!++7''//1*s5   DA< AD<
B5*B00B55B8 8ADDc                    g rr   r7   r@   r  s     r1   dataclass_with_cached_hashr    s    HKr4   c                    g rr   r7   r  s     r1   r  r    s     $'r4   c                .   ^ SU4S jjnU c  U$ U" U 5      $ )Nc                ~   >^ [         R                  " U 40 TD6nU R                  mSU4S jjnS nX!l        X1l        U$ )Nc                x   > [        U S5      (       d  [        R                  U ST" U 5      5        U R                  $ )Nr   )r  r  __setattr__r   )r/   old_hashs    r1   r   :dataclass_with_cached_hash.<locals>.wrap.<locals>.__hash__  s0    4))""4(4.A::r4   c                v   ^  [         R                  " T 5      n[        U 4S jU 5       5      nT R                  U4$ )Nc              3  t   >#    U  H-  oR                   (       d  M  [        TUR                  5      v   M/     g 7frr   )initrZ  r   )r  fr/   s     r1   r  Odataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__.<locals>.<genexpr>(  s&      Q1&&!6qvv!6!6s   88)r  fieldstupler   )r/   r  field_valuess   `  r1   
__reduce__<dataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__#  s4     !''-F  Q QQLNNL11r4   r  )r  r   r   r  )	cls_innernew_clsr   r  r  r  s       @r1   wrap(dataclass_with_cached_hash.<locals>.wrap  s@    ''	<V<%%	
	2 $'r4   )r  type[T]rJ   r  r7   )r@   r  r  s    ` r1   r  r    s    * {9r4   c                      \ rS rSrSS jrSS jrSS jr\R                  SS j5       r	\
SS j5       r\R                  SS j5       r        SS jrSS	 jrSS
 jrSS jrSSS jjrSrg)r   i7  c                    grB  r7   r[   s    r1   is_dict_keySource.is_dict_key9      r4   c                    grB  r7   r[   s    r1   is_ephemeralSource.is_ephemeral<  r  r4   c                    [         err   NotImplementedError)r/   codegens     r1   reconstructSource.reconstruct?  s    !!r4   c                    [         err   r  r[   s    r1   r   Source.guard_sourceB  s    !!r4   c                    [         e)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  r[   s    r1   _name_templateSource._name_templateF  s
     "!r4   c                    U R                   $ rr   )r  r[   s    r1   r   Source.nameT  s    """r4   c                L    X;   a  X0   $ [        U R                  X5      nXCU '   U$ rr   )evalr  )r/   globalslocalscacher   s        r1   	get_valueSource.get_valueX  s/     =;T((':dr4   c                ^    U R                   [        R                  L a  [        e[	        X5      $ rr   )r   r_   r   r  r   )r/   fns     r1   
make_guardSource.make_guardd  s&     4 44%%Tr4   c                6    U R                   R                  5       $ rr   )r   r   r[   s    r1   r   Source.is_specialized_nn_modulei  s      99;;r4   c                <    U R                   [        R                  :g  $ )z+True if you can guard on attributes of this)r   r_   r   r[   s    r1   subguards_allowedSource.subguards_allowedl  s      K$?$???r4   Nc                    Ub  U" U 5      $ U $ rr   r7   )r/   transform_fns     r1   cloneSource.clonep  s     #%%r4   r7   r   )r  r   rJ   r  r  rI   r  rl  r  rl  r  zdict[Source, Any]rJ   r   )r  zCallable[..., Any]rJ   r   rr   r  z!Callable[[Source], Source] | NonerJ   r   )rN   rO   rP   rQ   r  r  r  r   cached_propertyr   r  r  r   r  r  r   r  r  rT   r7   r4   r1   r   r   7  s    " " " " " # #

 
 !	

 


<@ r4   r   c                      \ rS rSr% S\S'   SS jrSS jr\R                  SS j5       r	SS jr
\R                  SS j5       r        SS	 jrSSS jjrSrg
)ChainedSourceiy  r   basec                6    U R                   R                  5       $ rr   )r	  r  r[   s    r1   r  ChainedSource.is_dict_key}  s    yy$$&&r4   c                6    U R                   R                  5       $ rr   )r	  r  r[   s    r1   r  ChainedSource.is_ephemeral  s    yy%%''r4   c                .    U R                   R                  $ rr   )r	  r   r[   s    r1   r   ChainedSource.guard_source  s    yy%%%r4   c                z    U n[        U[        5      (       a#  UR                  n[        U[        5      (       a  M#  U$ rr   )r   r  r	  )r/   currents     r1   get_baseChainedSource.get_base  s1    -00llG -00r4   c                `    U R                   R                  U R                  R                  5      $ rr   )r  r   r	  r   r[   s    r1   r   ChainedSource.name  s!    ""))$))..99r4   c                    X;   a  X0   $ SnSnXB;   a  SU 3nUS-  nXB;   a  M  U R                   R                  XU5      X$'   [        U R                  R	                  U5      X5      nX$	 XcU '   U$ )Ntmpr   ra   )r	  r  r  r  r   )r/   r  r  r  tmpvarcounterr   s          r1   r  ChainedSource.get_value  s     =;7)_FqLG  ,,WeDT((//7INdr4   Nc                t   SU R                   R                  U5      0n[        R                  " U 5       H^  nUR                  S:X  a  M  [        XR                  5      n[        U[        5      (       d  MA  UR                  U5      X#R                  '   M`     [        R                  " U 40 UD6nUb  U" U5      nU$ )Nr	  )	r	  r  r  r  r   rZ  r   r   replace)r/   r  cloned_fieldsr  r  results         r1   r  ChainedSource.clone  s    )/1N(O##D)Avv$'C#v&&(+		,(?ff% * $$T;];#!&)Fr4   r7   r   r  )rJ   r   rI   r  rr   r  )rN   rO   rP   rQ   rR   r  r  r   r  r   r  r   r  r  rT   r7   r4   r1   r  r  y  s    
L'( & & : :  !	
 
& r4   r  c                   SSK JnJnJn  [        R                  5       =n(       a  UR                  nUb  U$ / nSSKJn  [        [        U" 5       5      5       H+  u  p[        X5      (       d  M  UR                  U	SU45        M-     [        R                  " U 5      n
[        U
5       H  u  p[        X5      (       a  UR                  UR                  SU45        [        U5      (       d  ME  / nU" XS9  U Vs/ s H  n[        X5      (       d  M  UPM     nnUR!                  [        U5       VVs/ s H  u  nnUR                  SU 3U4PM     snn5        M     U(       ab  US   u  nnnUS	S  HN  u  n	nnXYLd  M  [#        S
U SU SU SU	 SU SU SU SU SUR$                   SU SU SU	R$                   35      e   U$ gs  snf s  snnf )ax  
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)outzsubclass input ra   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|  torch.utils._python_dispatchr$  r5  reversedr   r  pytreetree_leavesr   r  r.   r   )inputsr!  r   r"  r  r|  
fake_modesr$  r9  mflat_inputs
flat_inputr%  xfake_tensorsixtensordesc1i1desc2i2s                        r1   detect_fake_moder8    s     !((**w*%%	 JM(#C#EFGa((q"4a89 H $$V,K";/j--z335H!LM(44;=Cj2.a*Q";3  .  '0&=&=
F %%'<bA&= 0  )!}	5"&qrNLAub!$!)GE7!B4?TUVTWW^_d^eefgifj k&&+WAbT1A)//AR S&&+WAbT1A!''L  + -.s   9GG2Gc                     SSK Jn   SSKJn  [	        [        U" 5       5      5       H  u  p#[        X05      (       d  M  Us  $    g)zr
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$  r5  r(  r   )r   r$  rZ   r-  s       r1   active_fake_moder:    s:    
 =M(#C#EFGa((H H r4   )r  rg  rJ   z,Generator[CompileContext | None, None, None])r  r  rJ   z,Generator[TracingContext | None, None, None])r@   r  r  r   rJ   r  rr   )r@   r  r  r   rJ   zCallable[[type[T]], type[T]])r@   ztype[T] | Noner  r   rJ   z&type[T] | Callable[[type[T]], type[T]])r+  r   rJ   r  )rJ   r  )g
__future__r   r  r  enumr   loggingre	threadingr  unittest.mockr  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   	getLoggerrN   r   collections.abcr   r   r   typesr   sympy"torch._dynamo.backends.distributedr   torch._dynamo.codegenr   torch._dynamo.guardsr   &torch._functorch._aot_autograd.schemasr   rI  r   r&  r   compiler8   r9   r$   rV   Enumr_   r   r   r   r   r  r  r  r&  r*  r4  rI  rX  rb  rn  r  r  r  r  r  r  rF  localrT  rR  ri  r  rU  r  r  r   r  r8  r:  r7   r4   r1   <module>rT     s   "      	      # % ! M M 1  ) / F B 4 ! ==F/3JR< ZZ PQ 

X " $D17J 7J 27Jt7j 7<
$)) <
~	 	 d#  $   T"u' u' #u'p CL d#	 	 $	 d#	l 	 $	 d#*\ * $*

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