
    #Ki                    0   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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Jr  \R8                  S:  a  S S	KJr  OS
 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*  \RV                  " \,5      r-\(       a,  S SK.J/r/J0r0J1r1  S SK2J3r3  S SK4r4S SK5J6r6  S SK7J8r8  S SK9J:r:  S SK;J<r<   \Rz                  " S5      r>\Rz                  " S5      r?\" SSSS9 " S S5      5       r@ " S S\5      rA " S S\R                  5      rC  " S  S!5      rD\R&                  " SS"9 " S# S$5      5       rE " S% S&\5      rF\R&                  " SS'9 " S( S)5      5       rG\" S*5      rH \R&                  " SS"9 " S+ S,5      5       rI \R&                  " SS"9 " S- S.\I5      5       rJ \R&                  " SS"9 " S/ S0\I5      5       rK  " S1 S2\\H   5      rL " S3 S45      rM " S5 S65      rN " S7 S8\L\N   5      rO " S9 S:5      rP " S; S<\L\P   5      rQ " S= S>5      rR  " S? S@\L\M   5      rS " SA SB5      rT " SC SD\T5      rU " SE SF5      rV\	R                  " 5       rX  " SG SH5      rY " SI SJ5      rZ\    SVSK j5       r[\    SWSL j5       r\\SXSM j5       r]\ SY     SZSN jj5       r]\" 5        SY     S[SO jj5       r]\]" SS"9 " SP SQ5      5       r^\]" SS"9 " SR SS\^5      5       r_SYS\ST jjr`S]SU jrag)^    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyGeneric
NamedTupleOptionaloverloadTYPE_CHECKINGTypeVar)      )dataclass_transformc                     S n U $ )Nc                    U $ N )fns    O/var/www/html/dynamic-report/venv/lib/python3.13/site-packages/torch/_guards.py	decorator&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                  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)	CompileIdP   
int | Noneframe_idframe_compile_idNcompiled_autograd_idc                B   U R                   b\  U R                  S L U R                  S L :X  d   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   eU R                   SU R                   3$ )N /!)r3   r1   r2   )self	frame_strs     r   __str__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                <   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 ''r   )COMPILE_ID_PATTERNCA_COMPILE_ID_PATTERNmatch	groupdictitemsint
ValueError	Exception)cls
compile_idpatternr@   groupskves           r   from_stringCompileId.from_stringl   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r   returnstr)rG   
str | NonerP   CompileId | None)
__name__
__module____qualname____firstlineno____annotations__r3   r:   classmethodrM   __static_attributes__r   r   r   r.   r.   P   s8    
 !  (,*+
> J Jr   r.   c                  4    \ rS rSr% S\S'   S\S'   S	S jrSrg)
TraceId   r.   rG   rC   attemptc                    U R                   S:X  a  [        U R                  5      $ U R                   SU R                    3$ )Nr   _)r^   rQ   rG   r8   s    r   r:   TraceId.__str__   s7    <<1t''oo&a~66r   r   NrO   rT   rU   rV   rW   rX   r:   rZ   r   r   r   r\   r\      s     L7r   r\   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         r                  	   
   r                  c                H    U [         R                  [         R                  4;   $ r   )re   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULEra   s    r   is_fsdp_moduleGuardSource.is_fsdp_module   s    668U8UVVVr   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_guardsre   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErx   )r8   r~   s     r   is_specialized_nn_module$GuardSource.is_specialized_nn_module   sn    --11<<;; )
 &&( 4433
 
 	
r   c                    U [         R                  [         R                  [         R                  [         R                  4;   $ r   )re   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULEra   s    r   is_unspecialized_nn_module&GuardSource.is_unspecialized_nn_module   s8    6655>>==	
 
 	
r   c                H    U [         R                  [         R                  4;   $ r   )re   r   r   ra   s    r   "is_unspecialized_builtin_nn_module.GuardSource.is_unspecialized_builtin_nn_module   s&    >>==
 
 	
r   c                    U [         R                  [         R                  [         R                  [         R                  [         R
                  4;   $ r   )re   LOCALr   rw   r   r   ra   s    r   is_localGuardSource.is_local   sA    33))55==
 
 	
r   r   NrP   bool)rT   rU   rV   rW   r   GLOBALr   r   CONSTANTRANDOM_VALUE	SHAPE_ENVrw   rv   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALr   r   r   r   
TEMP_LOCALrx   r   r   r   r   rZ   r   r   r   re   re      sy    EF"##$ HLINIO$&!%'",.)-/*JW
"


r   re   c                      \ rS rSrSrg)GuardBuilderBase   r   NrT   rU   rV   rW   rZ   r   r   r   r   r      s    r   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_locrR   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   rQ   r   r   )r8   flocs     r   r:   SLoc.__str__   sk     $,,c22 d001 	
 *))*"TF!44tfA;r   r   NrO   rc   r   r   r   r   r      s    66	r   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_obliviousr   NrT   rU   rV   rW   rX   rZ   r   r   r   r   r      s    
%%
Jr   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_stackr0   _hashFr   _unserializablec                    U R                   c:  [        U R                  U R                  [	        U R
                  5      45      U l         U R                   $ r   )r   hashnamesourceidr   ra   s    r   __hash__Guard.__hash__  s:    ::tyy$++r$..7IJKDJzzr   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torchr}   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r8   is_duplicate_inputs     r   sort_keyGuard.sort_key  s    
 t~~y'8'89 Y##u}}';';'H'H'X'XX 	
 !%DKK"		NII  "++::
 	
r   c                D    U R                  5       UR                  5       :  $ r   )r   r8   others     r   __lt__Guard.__lt__/  s    }}!111r   c                    [        U R                  [        R                  5      (       a  U R                  R                  $ U R                  $ r   )r   r   r   r   r   ra   s    r   r   Guard.inner_create_fn2  s3    dnni&7&788>>&&&>>!r   c                .    U R                   R                  $ r   )r   r   ra   s    r   r   
Guard.name8  s    &&+++r   c                .    U R                   R                  $ r   )r   guard_sourcera   s    r   r   Guard.source<  s    &&333r   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__rT   rQ   )r   objs     r   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{##r   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	
        r5    z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )r   r   lowerreprr   rT   r   r   r   r   r   )r8   ss     r   __repr__Guard.__repr__Y  s    	%)[[					!b94		?:K1TMaMaMcMlMlLm n ,,- .^^$ % //0@0@AB C"889 :	 r   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: 
r5   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )r   r   r   r   r   rT   r   r   r   r   r   )r8   outputr   s      r   r:   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           	     \    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:
%sr5   )
r   rE   log	exceptionrQ   rstripr   errorjoinformat)r8   builders     r   createGuard.createp  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       $ r   )r   r   ra   s    r   r   Guard.is_specialized_nn_moduley  s    {{3355r   c                6    U R                   R                  5       $ r   )r   rx   ra   s    r   rx   Guard.is_fsdp_module|  s    {{))++r   c                6    U R                   R                  5       $ r   )r   r   ra   s    r   r   Guard.is_local  s    {{##%%r   c                    [        U R                  [        R                  5      (       a"  U R                  R                  nUR
                  $ U R                  nUR
                  $ r   )r   r   r   r   r   rT   )r8   r   s     r   create_fn_nameGuard.create_fn_name  sM    dnni&7&788++I !!! I!!!r   c                   U R                   (       d  / U l         U R                   R                  U5        U R                  US 4;   d   S5       eX l        U R                  (       d  X0l        OU R                  R	                  U5        U R
                  US 4;   d2  [        U R
                  5      (       a  U R                  5       b   S5       eX@l        g )Nz+Guarded class id must be identical, or NonezBGuarded object must be identical, None or ephemeral (dead weakref))r   appendr   r   extendr   callable)r8   
guard_typeguarded_classr   r   s        r   set_export_infoGuard.set_export_info  s     !D
+)).
 
 	9 9	9 
 &3"~~&NNN!!), d 33(())  "*	P P		P+ 'r   )r   r   r   r   r   rP   rC   )rP   ztuple[bool, int, int, str, int])r   r   rP   r   )rP   z(Callable[[GuardBuilderBase, Guard], Any]rO   rP   re   )r   objectrP   rQ   )r  r   rP   r   r   )
r  rQ   r  r   r   	list[str]r   r  rP   None)rT   rU   rV   rW   rX   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r:   r  r   rx   r   r  r  rZ   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                      \ rS rSrSrg)GuardEnvExpri  r   Nr   r   r   r   r  r    s    r   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                <    U R                   U R                  :w  d   eg r   )r!  r"  ra   s    r   __post_init__DuplicateInputs.__post_init__  s    ""d&9&9999r   r   NrP   r  )rT   rU   rV   rW   rX   r$  rZ   r   r   r   r   r     s    :r   r   c                  *    \ rS rSr% S\S'   S\S'   Srg)StorageOverlapi  zlist[Source]overlapping_sourcesnon_overlapping_sourcesr   Nr   r   r   r   r(  r(    s    %%))r   r(  c                  <    \ rS rSr\SS j5       r\SS j5       rSrg)Checkpointablei  c                    g r   r   ra   s    r   copy_graphstateCheckpointable.copy_graphstate  s    $'r   c                    g r   r   r8   states     r   restore_graphstate!Checkpointable.restore_graphstate  s    47r   r   N)rP   r  )r2  r  rP   r  )rT   rU   rV   rW   r   r.  r3  rZ   r   r   r   r,  r,    s    ' '7 7r   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 r   r8  )r8   r8  s     r   __init__GuardsCheckpointState.__init__  s    *r   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)r8  
differencer   r8   r   rs      r   diffGuardsCheckpointState.diff  s3     ))%*=*=>q6Q;r   c                T    [        U[        5      (       d  gU R                  U5      S L $ NF)r   r6  rA  r   s     r   __eq__GuardsCheckpointState.__eq__	  s'    %!677yy4''r   r:  N)r8  r7  rP   r  )r   r6  rP   Optional[OrderedSet[Guard]]r   r  rP   r   )
rT   rU   rV   rW   __doc__rX   r;  rA  rE  rZ   r   r   r   r6  r6    s     %$+
(r   r6  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 r   rM  )r8   rM  s     r   r;  %ModuleContextCheckpointState.__init__  s    $r   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)setrM  keysr>  r   r?  s      r   rA  !ModuleContextCheckpointState.diff  sM     $$&'223u7G7G7L7L7N3OPq6Q;r   c                T    [        U[        5      (       d  gU R                  U5      S L $ rD  )r   rK  rA  r   s     r   rE  #ModuleContextCheckpointState.__eq__!  '    %!=>>yy4''r   rO  N)rM  rL  rP   r  )r   rK  rP   set[str] | NonerH  )
rT   rU   rV   rW   rM  rX   r;  rA  rE  rZ   r   r   r   rK  rK    s    -/J*/%
(r   rK  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 r   rO  ra   s    r   r;  ModuleContext.__init__(  s	    *,r   c                >    [        [        U R                  5      5      $ r   )rK  dictrM  ra   s    r   r.  ModuleContext.copy_graphstate+  s    +D,ABBr   c                T    [        U[        5      (       d   eUR                  U l        g r   )r   rK  rM  r1  s     r   r3   ModuleContext.restore_graphstate.  s#    %!=>>>>**r   rO  Nr&  )rP   rK  )r2  rK  rP   r  rT   rU   rV   rW   r;  r.  r3  rZ   r   r   r   rZ  rZ  '  s    -C+r   rZ  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)GlobalContextCheckpointStatei3  dict[str, tuple[Callable, Any]]global_statec                    Xl         g r   rf  )r8   global_statess     r   r;  %GlobalContextCheckpointState.__init__6  s    )r   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)rR  rf  rS  r>  r   r?  s      r   rA  !GlobalContextCheckpointState.diff9  sO     !!&&()44S9K9K9P9P9R5STq6Q;r   c                T    [        U[        5      (       d  gU R                  U5      S L $ rD  )r   rd  rA  r   s     r   rE  #GlobalContextCheckpointState.__eq__E  rW  r   rh  N)ri  re  rP   r  )r   rd  rP   rX  rH  )
rT   rU   rV   rW   rf  rX   r;  rA  rE  rZ   r   r   r   rd  rd  3  s    46L16*
(r   rd  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)GlobalContextiK  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 r   rh  ra   s    r   r;  GlobalContext.__init__Z  s
    =?r   c                ,    [        U R                  5      $ r   )rd  rf  ra   s    r   r.  GlobalContext.copy_graphstate]  s    +D,=,=>>r   c                r   [        U[        5      (       d   eUR                  U l        [        U R                  5      [        U R                  5      :X  a1  [        U R                  R                  5       5      U R                  :X  d   S5       eU R                  R                  5        H  u  p#U" U5        M     g )NzGlobal state mismatch)r   rd  rf  r   _supported_global_statesrR  rS  values)r8   r2  r   argss       r   r3   GlobalContext.restore_graphstate`  s    %!=>>>>!..!!"c$*G*G&HHD%%**,-1N1NN	# #	#O ++224JDJ 5r   rh  Nr&  )rP   rd  )r2  rd  rP   r  )
rT   rU   rV   rW   rI  r|  r;  r.  r3  rZ   r   r   r   rp  rp  K  s    
 @?r   rp  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	S
.       SS jjr	SS jr
SS jrSrg)	GuardsSetim  Nc                6    Uc  [        5       U l        g Xl        g r   )r   inner)r8   r  s     r   r;  GuardsSet.__init__n  s    =,6LDJJr   c                ,    [        U R                  5      $ r   )iterr  ra   s    r   __iter__GuardsSet.__iter__t      DJJr   c                ,    [        U R                  5      $ r   )r   r  ra   s    r   __len__GuardsSet.__len__w  s    4::r   c                F    [        U R                  UR                  -
  5      $ r   )r  r  r   s     r   __sub__GuardsSet.__sub__|  s    ekk122r   c                ,    [        U R                  5      $ r   )r   r  ra   s    r   __bool__GuardsSet.__bool__  r  r   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        g Nrg   )r  )r  r   r   extractr   TracingContextextract_stackadd)r8   guardr  r  s       r   r  GuardsSet.add  sa     JJ{{"/77QXF#-;;=E

ur   c                H    U H  nU H  nU R                  USS9  M     M     g r  )r  )r8   othersogs       r   updateGuardsSet.update  s&    A#  r   c                ^   ^^ SSK Jm  [        UU4S jU R                   5       5      U l        g)z.Delete all guards that contains a given sourcerg   )is_from_sourcec              3  \   >#    U  H!  nT" UR                   T5      (       a  M  Uv   M#     g 7fr   )r   ).0r  r  r   s     r   	<genexpr>6GuardsSet.remove_guards_with_source.<locals>.<genexpr>  s%       
!!8L8Lf)UAAzs   ,	,N)_dynamo.sourcer  r   r  )r8   r   r  s    `@r   remove_guards_with_source#GuardsSet.remove_guards_with_source  s#    2  
zz 
 

r   )r  r   )r  rG  rP   r  )rP   zIterator[Guard]r  )r   r  rP   r  r   )r  r   r  r   r  rC   rP   r  )r  z
set[Guard]rP   r  )r   r   rP   r  )rT   rU   rV   rW   r;  r  r  r  r  r  r  r  rZ   r   r   r   r  r  m  sR     
3  <@Q

48
GJ
	
$

r   r  c                  2    \ rS rSrSS jrSS jrS	S jrSrg)
GuardsContexti  c                0    [        5       U l        / U l        g r   )r  r8  aotautograd_guardsra   s    r   r;  GuardsContext.__init__  s    (168r   c                R    [        [        U R                  R                  5      5      $ r   )r6  r   r8  r  ra   s    r   r.  GuardsContext.copy_graphstate  s    $Z0B0B0H0H%IJJr   c                f    [        U[        5      (       d   e[        UR                  5      U l        g r   )r   r6  r  r8  r1  s     r   r3   GuardsContext.restore_graphstate  s)    %!67777&u':':;r   )r  r8  Nr&  )rP   r6  )r2  r6  rP   r  rb  r   r   r   r  r    s    9K<r   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 r   r   r8   fn_id
identifiers      r   add_dynamo_installed_submodule/HopSubgraphCache.add_dynamo_installed_submodule  s    SVr   c                    g r   r   r8   r  s     r   get_dynamo_installed_submodules0HopSubgraphCache.get_dynamo_installed_submodules      HKr   c                    g r   r   r8   r  keys      r   add_autograd_key_entry'HopSubgraphCache.add_autograd_key_entry  s    NQr   c                    g r   r   r8   r  s     r   get_autograd_key_entry'HopSubgraphCache.get_autograd_key_entry  s    JMr   c                    g r   r   r  s      r   add_proxy_dispatch_entry)HopSubgraphCache.add_proxy_dispatch_entry  s    PSr   c                    g r   r   r  s     r   get_proxy_dispatch_entry)HopSubgraphCache.get_proxy_dispatch_entry  s    LOr   c                    g r   r   )r8   r  tangent_metadatagmods       r   add_lazy_bwd_entry#HopSubgraphCache.add_lazy_bwd_entry  s     r   c                    g r   r   r8   r  r  s      r   get_lazy_bwd_entry#HopSubgraphCache.get_lazy_bwd_entry  s     :=r   r   Nr  rC   r  rQ   rP   r  r  rC   rP   r  r  rQ   r  r!   rP   r  r  rQ   rP   zCallable | Noner  rQ   r  tuple[object]r  ztorch.fx.GraphModulerP   rC   r  rQ   r  r  rP   z.tuple[torch.fx.GraphModule | None, int | None])rT   rU   rV   rW   r   r  r  r  r  r  r  r  r  rZ   r   r   r   r  r    s    V VK KQ QM MS SO O ( #	
 
  ==1>=	7= =r   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rg)InvokeSubgraphCachei  c                ~    0 U l         0 U l        [        [        5      U l        [        [
        5      U l        0 U l        g r   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr^  lazy_bwd_cacheeffects_cachera   s    r   r;  InvokeSubgraphCache.__init__  s>    359;!ALTAR(  	
  	r   c                @    U R                   U   R                  U5        g r   )r  r  r  s      r   r  2InvokeSubgraphCache.add_dynamo_installed_submodule  s    ((/66zBr   c                :    U R                   R                  U/ 5      $ r   )r  getr  s     r   r  3InvokeSubgraphCache.get_dynamo_installed_submodules  s    //33E2>>r   c                     X R                   U'   g r   )r  r  s      r   r  *InvokeSubgraphCache.add_autograd_key_entry  s    *-J'r   c                :    U R                   R                  US 5      $ r   )r  r  r  s     r   r  *InvokeSubgraphCache.get_autograd_key_entry  s    ""&&z488r   c                     X R                   U'   g r   )r  r  s      r   r  ,InvokeSubgraphCache.add_proxy_dispatch_entry  s    03!!*-r   c                :    U R                   R                  US 5      $ r   )r  r  r  s     r   r  ,InvokeSubgraphCache.get_proxy_dispatch_entry  s    ((,,Z>>r   c                \    [        U R                  U   5      nX44U R                  U   U'   U$ r   )r   r  )r8   r  r  r  	num_gmodss        r   r  &InvokeSubgraphCache.add_lazy_bwd_entry  s8     ++J78	=A<MJ'(89r   c                `    XR                   ;  a  gU R                   U   R                  US5      $ )N)NN)r  r  r  s      r   r  &InvokeSubgraphCache.get_lazy_bwd_entry  s2     000"":.223C\RRr   c                    U R                   R                  US5      =n(       a  X#:X  d   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  )r8   r  effectsprev_effectss       r   add_effectsInvokeSubgraphCache.add_effects  si    --11*dCC<C* ((2| 4<<H> J$$+9A/* *1:&r   c                :    U R                   R                  US5      $ )zARetrieve the effect types for a given invoke_subgraph identifier.Nr  r  s     r   get_effectsInvokeSubgraphCache.get_effects  s    !!%%j$77r   )r  r  r  r  r  Nr&  r  r  r  r  r  r  )r  rQ   r   rR  rP   r  )r  rQ   rP   z
set | None)rT   rU   rV   rW   r;  r  r  r  r  r  r  r  r  r  r  rZ   r   r   r   r  r    s|    	C?.94?		 (	 #		
 
	SS1>S	7S	18r   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_subgraphr
  r  hop_cache_map)r8   r
  s     r   r;  HopDispatchSetCache.__init__  s    K-/B/DEr   c                @    XR                   ;  a  g U R                   U   $ r   r  )r8   ops     r   	get_cacheHopDispatchSetCache.get_cache  s"    '''!!"%%r   r  Nr&  )r  ztorch._ops.HigherOrderOperatorrP   zHopSubgraphCache | None)rT   rU   rV   rW   r;  r  rZ   r   r   r   r  r    s    F&r   r  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)CompileContexti6  c                 H    [         R                  c   e[         R                  $ r   )_TLScompile_contextr   r   r   r  CompileContext.get7  s     ##///###r   c                 $    [        [        SS 5      $ Nr  getattrr  r   r   r   try_getCompileContext.try_get<      t.55r   c                `    Ub  [        U[        5      (       d   eXl        SU l        / U l        g r{   )r   r.   rG   r^   shape_env_guards)r8   rG   s     r   r;  CompileContext.__init__@  s.    !Z
I%F%FFF,6+-r   c                 J    [         R                  5       n U c  g U R                  $ r   )r  r  rG   ra   s    r   current_compile_id!CompileContext.current_compile_idG  s"    %%'<r   c                     [         R                  5       n U c  g U R                  c  g [        U R                  U R                  5      $ r   )r  r  rG   r\   r^   ra   s    r   current_trace_idCompileContext.current_trace_idN  s:    %%'<??"t55r   )r^   rG   r!  N)rP   r  )rP   CompileContext | None)rG   rS   rP   r  )rP   rS   )rP   zTraceId | None)rT   rU   rV   rW   r  r  r  r;  r$  r'  rZ   r   r   r   r  r  6  sU    $ $ 6 6.   6 6r   r  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  iX  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_contextr  r   r   r   r  TracingContext.try_get`  r  r   c                 R    [         R                  5       =n (       a  U $ [        S5      e)Nz<TracingContext.get() must be called within an ongoing trace.)r  r  RuntimeError)ctxs    r   r  TracingContext.getd  s+     ((**3*JJ
 	
r   c                   [        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        [/        5       U l        / U l        g rD  )r  guards_contextrZ  module_contextrp  global_contextr^  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)r8   r8  s     r   r;  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                    0 U R                   l        U R                  R                  5         U R                  R                  5         g r   )r5  rf  r6  clearr7  ra   s    r   rI  TracingContext.clear  s6     ,.())//1,,224r   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r   )r  r  r  rB   setattr)kwargspriorr0  r  vals        r   patchTracingContext.patch  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      $ r   )r  r  	tracebackStackSummaryr9  r:  _populate_loc_in_frame_summary	from_list)r8   r   s     r   r  TracingContext.extract_stack  sf    %%'<))++(((@@BCCE%%//66r   c                j    U R                   c   eU R                   u  pn[        R                  " XUSS9$ )NF)lookup_line)r:  rS  FrameSummary)r8   filenamelineno
frame_names       r   rU  -TracingContext._populate_loc_in_frame_summary  s:      ,,,'+'8'8$*%%h
PUVVr   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)Nr9  r:  
real_stack)	r  r  unittestmockrP  r  rE   hasattrr`  )tcrL   s     r   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  r9  r  r:  rE   rc  r  r`  pop)frame_summaryrd  oldrL   s       r   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r   )r  r  rA  )rd  old_output_stridess     r   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 r   )r  r  r:  )r[  r\  r]  s      r   set_current_locTracingContext.set_current_loc  s     .6z,J)r   c                 J    [         R                  5       n U c  g U R                  $ r   )r  r  rF  )rd  s    r   get_traced_codeTracingContext.get_traced_code  s"    ##%:~~r   )r=  r<  r8  rD  rB  r9  r;  r5  r3  rE  r:  r4  rA  r>  r?  r@  r7  r6  rC  rF  N)rP   TracingContext | None)rP   r  )r8  FakeTensorMode | NonerP   r  r&  )rM  r   rP   Generator[None, None, None])rP   ztraceback.StackSummary)rP   ztraceback.FrameSummary)rP   ry  )ri  ztraceback.FrameSummary | NonerP   ry  )rP   z:Generator[list[tuple[int, ...] | None] | None, None, None])r[  rQ   r\  rC   r]  rQ   rP   r  )rP   zlist[CodeType] | None)rT   rU   rV   rW   rI  r  r  r  r;  rI  r   rP  r  rU  
contextlibre  rk  ro  rr  ru  rZ   r   r   r   r  r  X  s    6 6 
 
1.f5 '  ' 7 7W   < "4"	$"  "* 3  3 K K
  r   r  c              #     #    [        [        SS 5      nU [        l         U v   U[        l        g ! U[        l        f = f7fr  )r  r  r  )contextold_contexts     r   r  r    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.
r,  Nr`  )
r  r  r,  rE   rc  r  r`  r8  	shape_envcleanup)r|  r}  rL   s      r   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 r   r   rF   rM  s     r   dataclass_with_cached_hashr  F  r  r   c                    g r   r   r  s     r   r  r  J  s     $'r   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   )rc  r  __setattr__r   )r8   old_hashs    r   r   :dataclass_with_cached_hash.<locals>.wrap.<locals>.__hash__X  s0    4))""4(4.A::r   c                v   ^  [         R                  " T 5      n[        U 4S jU 5       5      nT R                  U4$ )Nc              3  P   >#    U  H  n[        TUR                  5      v   M     g 7fr   )r  r   )r  fr8   s     r   r  Odataclass_with_cached_hash.<locals>.wrap.<locals>.__reduce__.<locals>.<genexpr>b  s      G1qvv!6!6s   #&)dataclassesfieldstupler   )r8   r  field_valuess   `  r   
__reduce__<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  rM  s       @r   wrap(dataclass_with_cached_hash.<locals>.wrapT  s@    ''	<V<%%	
	2 $'r   )r  type[T]rP   r  r   )rF   rM  r  s    ` r   r  r  P  s    * {9r   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rg)r   iq  c                    grD  r   ra   s    r   is_dict_keySource.is_dict_keys      r   c                    grD  r   ra   s    r   is_ephemeralSource.is_ephemeralv  r  r   c                    [         er   NotImplementedError)r8   codegens     r   reconstructSource.reconstructy  s    !!r   c                    [         er   r  ra   s    r   r   Source.guard_source|  s    !!r   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  ra   s    r   _name_templateSource._name_template  s
     "!r   c                    U R                   $ r   )r  ra   s    r   r   Source.name  s    """r   c                L    X;   a  X0   $ [        U R                  X5      nXCU '   U$ r   )evalr  )r8   globalslocalscacher   s        r   	get_valueSource.get_value  s/     =;T((':dr   c                ^    U R                   [        R                  L a  [        e[	        X5      $ r   )r   re   r   r  r   )r8   r   s     r   
make_guardSource.make_guard  s&     4 44%%Tr   c                6    U R                   R                  5       $ r   )r   r   ra   s    r   r   Source.is_specialized_nn_module  s      99;;r   c                <    U R                   [        R                  :g  $ )z+True if you can guard on attributes of this)r   re   r   ra   s    r   subguards_allowedSource.subguards_allowed  s      K$?$???r   r   Nr   )r  r&   rP   r  r  rO   r  dict[str, Any]r  r  r  z&weakref.WeakKeyDictionary[Source, Any]rP   r   )r   zCallable[..., Any]rP   r   )rT   rU   rV   rW   r  r  r  r   cached_propertyr   r  r  r   r  r  r   r  rZ   r   r   r   r   r   q  s    " " " " " # #

 
 6	

 


<@r   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
rg)ChainedSourcei  r   basec                6    U R                   R                  5       $ r   )r  r  ra   s    r   r  ChainedSource.is_dict_key  s    yy$$&&r   c                6    U R                   R                  5       $ r   )r  r  ra   s    r   r  ChainedSource.is_ephemeral  s    yy%%''r   c                .    U R                   R                  $ r   )r  r   ra   s    r   r   ChainedSource.guard_source  s    yy%%%r   c                z    U n[        U[        5      (       a#  UR                  n[        U[        5      (       a  M#  U$ r   )r   r  r  )r8   currents     r   get_baseChainedSource.get_base  s1    -00llG -00r   c                `    U R                   R                  U R                  R                  5      $ r   )r  r   r  r   ra   s    r   r   ChainedSource.name  s!    ""))$))..99r   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   rg   )r  r  r  r  r   )r8   r  r  r  tmpvarcounterr   s          r   r  ChainedSource.get_value  s     =;7)_FqLG  ,,WeDT((//7INdr   r   Nr   r  )rP   r   rO   r  )rT   rU   rV   rW   rX   r  r  r   r  r   r  r   r  rZ   r   r   r   r  r    su    
L'( & & : :  6	
 
r   r  c                   SSK JnJnJn  / n[        R                  5       =n(       a#  UR                  nUb  UR                  USS45        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(       a^  US   u  nnnUS
S  HJ  u  n	nnXiL a  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_tensorsNztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor input)outzsubclass input rg   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  r8  r  torch.utils._python_dispatchr  	enumeratereversedr   pytreetree_leavesr   r  r   )inputsr  r)   r  
fake_modesr|  r8  r  imflat_inputs
flat_inputr  xfake_tensorsixtensordesc1i1desc2i2s                        r   detect_fake_moder    s     J ((**w*%%	 y*;Q?@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> i[wat;PQRPSSZ[`Zaabcebf g""'"-=ioo=N O""'"-=aggY	H> + 1.
s   G#G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  r  r  r   )r)   r  r`   r  s       r   active_fake_moder    s:    
 =M(#C#EFGa((H H r   )r|  r)  rP   z,Generator[CompileContext | None, None, None])r|  rw  rP   z,Generator[TracingContext | None, None, None])rF   r  rM  r   rP   r  r   )rF   r  rM  r   rP   zCallable[[type[T]], type[T]])rF   ztype[T] | NonerM  r   rP   z&type[T] | Callable[[type[T]], type[T]])r  r   rP   rx  )rP   rx  )b
__future__r   rz  r  enumr   loggingresys	threadingrS  unittest.mockra  r   abcr   collectionsr   r   r   typingr   r	   r
   r   r   r   r   version_infor   r   torch.utilsr   r  torch.utils._ordered_setr   r  r   torch.utils._tracebackr   r   torch.utils.weakr    	getLoggerrT   r   collections.abcr!   r"   r#   typesr$   sympy"torch._dynamo.backends.distributedr%   torch._dynamo.codegenr&   &torch._functorch._aot_autograd.schemasr'   r  r)   compiler>   r?   r.   r\   Enumre   r   r   r   r   r  r  r   r(  r,  r6  rK  rZ  rd  rp  r  r  r  r  r  localr  r  r  r  r  r  r   r  r  r  r   r   r   <module>r     s   "      	 
      # % ! W W W w*  ) / F B 4 ! ==F/J< ZZ 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
& 
& &6 6DB BJ +"+1+ + +"+1+ +8 
 K 
 K 
'	' #'!' 
'
 	*-+ @ 4(6@ 6@ )6@t 4()F ) ))X?Dr   