
    7h]                    T   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JrJr  S SKrS SKJr  S S	K J!r!  S S
K"J#r#J$r$  S SK%J&r&  \RN                  " \(5      r)\(       a
  S SK*J+r+  S SK,r, \RZ                  " S5      r.\RZ                  " S5      r/\" SS9 " S S5      5       r0 " S S\5      r1 " S S\Rd                  5      r3  " S S5      r4\R$                  " SS9 " S S5      5       r5 " S S\5      r6\!\R$                   " S S5      5       5       r7\" S5      r8 \R$                  " SS9 " S  S!5      5       r9 \R$                  " SS9 " S" S#\95      5       r: \R$                  " SS9 " S$ S%\95      5       r;  " S& S'\\8   5      r< " S( S)5      r= " S* S+5      r> " S, S-\<\>   5      r? " S. S/5      r@ " S0 S1\<\@   5      rA " S2 S35      rB  " S4 S5\<\=   5      rC " S6 S75      rD " S8 S9\D5      rE " S: S;5      rF\R                  " 5       rH  " S< S=5      rI " S> S?5      rJ\SHS@ j5       rK\SISA j5       rL\R$                  " SS9 " SB SC5      5       rM\R$                  " SS9 " SD SE\M5      5       rNSJSKSF jjrOSG rPg)L    )annotationsN)abstractmethod)defaultdict)contextmanager)	dataclass)AnyCallableGeneric
NamedTupleOptionalTYPE_CHECKINGTypeVarUnion)_pytree)dataclass_slots)CapturedTracebackformat_frame)WeakTensorKeyDictionary)CodeTypez-^(?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)frozenc                  R    \ rS rSr% S\S'   S\S'   SrS\S'   S r\S
S j5       rS	r	g)	CompileIdG   Optional[int]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 /!)r   r   r   )self	frame_strs     G/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_guards.py__str__CompileId.__str__V   s    $$0MMT)t/D/D/LMMMI}}(a0E0E/FG	t001)====,1F1F1RRR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           r$   from_stringCompileId.from_stringc   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*   )r4   Optional[str])
__name__
__module____qualname____firstlineno____annotations__r   r%   classmethodr:   __static_attributes__r*   r'   r$   r   r   G   s8    
 $# +/-.
> J Jr'   r   c                  0    \ rS rSr% S\S'   S\S'   S rSrg)	TraceIdz   r   r4   r0   attemptc                    U R                   S:X  a  [        U R                  5      $ U R                   SU R                    3$ )Nr   _)rG   strr4   r"   s    r$   r%   TraceId.__str__   s7    <<1t''oo&a~66r'   r*   Nr=   r>   r?   r@   rA   r%   rC   r*   r'   r$   rE   rE   z   s     L7r'   rE   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S jrSS jrSS jrSS jrS rSrg)GuardSource   r                           	   
                  c                H    U [         R                  [         R                  4;   $ N)rO   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULErK   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_guardsrO   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULErd   )r"   rj   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;   $ ra   )rO   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULErK   s    r$   is_unspecialized_nn_module&GuardSource.is_unspecialized_nn_module   s8    6655>>==	
 
 	
r'   c                H    U [         R                  [         R                  4;   $ ra   )rO   rs   rt   rK   s    r$   "is_unspecialized_builtin_nn_module.GuardSource.is_unspecialized_builtin_nn_module   s&    >>==
 
 	
r'   c                    U [         R                  [         R                  [         R                  [         R                  [         R
                  4;   $ ra   )rO   LOCALrm   rc   rr   rt   rK   s    r$   is_localGuardSource.is_local   sA    33))55==
 
 	
r'   r*   Nreturnbool)r=   r>   r?   r@   r{   GLOBALrm   rl   CONSTANTRANDOM_VALUE	SHAPE_ENVrc   rb   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrr   rq   rt   rs   rd   rn   ru   rx   r|   rC   r*   r'   r$   rO   rO      st    EF"##$ HLINIO$&!%'",.)-/*W
"


r'   rO   c                      \ rS rSrSrg)GuardBuilderBase   r*   Nr=   r>   r?   r@   rC   r*   r'   r$   r   r      s    r'   r   c                  0    \ rS rSr% S\S'   S\S'   S rSrg)	SLoc   z,Optional[Union[traceback.FrameSummary, str]]framework_locr<   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   rJ   r   r   )r"   flocs     r$   r%   SLoc.__str__   sk     $,,c22 d001 	
 *))*"TF!44tfA;r'   r*   NrM   r*   r'   r$   r   r      s    ??!!	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r=   r>   r?   r@   rA   rC   r*   r'   r$   r   r      s    
%%
Jr'   r   c                  
   \ 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 rS rS r\S#S j5       r\S$S j5       r\S 5       rS rS rS%S jrS rS rS  rS! rS"rg)&Guard   Sourceoriginating_sourcez)Callable[[GuardBuilderBase, Guard], None]	create_fnNzOptional[list[str]]guard_types	code_listzOptional[object]obj_weakrefzOptional[type]guarded_class_weakrefzOptional[CapturedTraceback]stackz Optional[traceback.StackSummary]
user_stackr   _hashc                    U R                   c:  [        U R                  U R                  [	        U R
                  5      45      U l         U R                   $ ra   )r   hashnamesourceidr   rK   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torchri   guardsGuardBuilderDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r"   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       :  $ ra   )r   r"   others     r$   __lt__Guard.__lt__%  s    }}!111r'   c                    [        U R                  [        R                  5      (       a  U R                  R                  $ U R                  $ ra   )r   r   r   r   r   rK   s    r$   r   Guard.inner_create_fn(  s3    dnni&7&788>>&&&>>!r'   c                6    U R                   R                  5       $ ra   )r   r   rK   s    r$   r   
Guard.name.  s    &&++--r'   c                6    U R                   R                  5       $ ra   )r   guard_sourcerK   s    r$   r   Guard.source2  s    &&3355r'   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__r=   rJ   )r   objs     r$   weakref_to_strGuard.weakref_to_str6  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	
        r    z&
        {
            'guard_types': z,
            'code': z,
            'obj_weakref': z
            'guarded_class': z
        }
        )r   r   lowerreprr   r=   r   r   r   r   r   )r"   ss     r$   __repr__Guard.__repr__O  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: 
r   z    Source: z    Create Function: z    Guard Types: z    Code List: z    Object Weakref: z    Guarded Class Weakref: )r   r   r   r   r   r=   r   r   r   r   r   )r"   outputr   s      r$   r%   Guard.__str__[  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:
%sr   )
r   r2   log	exceptionrJ   rstripr   errorjoinformat)r"   builders     r$   createGuard.createf  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       $ ra   )r   rn   rK   s    r$   rn   Guard.is_specialized_nn_moduleo  s    {{3355r'   c                6    U R                   R                  5       $ ra   )r   rd   rK   s    r$   rd   Guard.is_fsdp_moduler  s    {{))++r'   c                6    U R                   R                  5       $ ra   )r   r|   rK   s    r$   r|   Guard.is_localu  s    {{##%%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)r"   
guard_typeguarded_classr   r   s        r$   set_export_infoGuard.set_export_infox  s    !D
+)).
 
 	9 9	9 
 &3"~~&NNN!!), d 33(())  "*	P P		P+ 'r'   )r   r   r   r   r   r   rJ   r   rO   )r   r   )r=   r>   r?   r@   rA   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r%   r   rn   rd   r|   r   rC   r*   r'   r$   r   r      s    ( 88 (,K$+%)I")$(K!(,0>0)-E&-37J07E=

 2" . . 6 6 $ $0
	6,&'r'   r   Tc                      \ rS rSrSrg)GuardEnvExpri  r*   Nr   r*   r'   r$   r  r    s    r'   r  c                  0    \ rS rSr% S\S'   S\S'   S rSrg)DuplicateInputsi  r   input_source_ainput_source_bc                <    U R                   U R                  :w  d   eg ra   )r  r  rK   s    r$   __post_init__DuplicateInputs.__post_init__  s    ""d&9&9999r'   r*   N)r=   r>   r?   r@   rA   r  rC   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 ra   r*   rK   s    r$   copy_graphstateCheckpointable.copy_graphstate  s    $'r'   c                    g ra   r*   r"   states     r$   restore_graphstate!Checkpointable.restore_graphstate  s    ,/r'   r*   N)r   r   )r  r   )r=   r>   r?   r@   r   r  r  rC   r*   r'   r$   r  r    s    ' '/ /r'   r  c                  D    \ rS rSr% Sr\" 5       rS\S'   S rS r	S r
Srg	)
GuardsCheckpointStatei  zO
The GuardCheckpointState - it is the T of Checkpointable[T] for GuardsContext

set[Guard]dynamo_guardsc                    Xl         g ra   r  )r"   r  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 a set() of mismatched
Guard type objects.
r   N)r  
differencer   r"   r   rs      r$   diffGuardsCheckpointState.diff  s3     ))%*=*=>q6Q;r'   c                (    U R                  U5      S L $ ra   r$  r   s     r$   __eq__GuardsCheckpointState.__eq__      yy4''r'   r  N)r=   r>   r?   r@   __doc__setr  rA   r  r$  r(  rC   r*   r'   r$   r  r    s$     !$M:%+
(r'   r  c                  6    \ rS rSr% 0 rS\S'   S rS rS rSr	g)	ModuleContextCheckpointStatei  zdict[str, torch.nn.Module]
nn_modulesc                    Xl         g ra   r/  )r"   r/  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)r,  r/  keysr!  r   r"  s      r$   r$  !ModuleContextCheckpointState.diff  sM     $$&'223u7G7G7L7L7N3OPq6Q;r'   c                (    U R                  U5      S L $ ra   r'  r   s     r$   r(  #ModuleContextCheckpointState.__eq__  r*  r'   r1  N)
r=   r>   r?   r@   r/  rA   r  r$  r(  rC   r*   r'   r$   r.  r.    s    -/J*/%
(r'   r.  c                  *    \ rS rSrSS jrS rS rSrg)ModuleContexti  c                    0 U l         g ra   r1  rK   s    r$   r  ModuleContext.__init__  s	    *,r'   c                >    [        [        U R                  5      5      $ ra   )r.  dictr/  rK   s    r$   r  ModuleContext.copy_graphstate  s    +D,ABBr'   c                T    [        U[        5      (       d   eUR                  U l        g ra   )r   r.  r/  r  s     r$   r   ModuleContext.restore_graphstate  s#    %!=>>>>**r'   r1  Nr   Noner=   r>   r?   r@   r  r  r  rC   r*   r'   r$   r9  r9    s    -C+r'   r9  c                  6    \ rS rSr% 0 rS\S'   S rS rS rSr	g)	GlobalContextCheckpointStatei  zdict[str, tuple[Callable, ...]]global_statec                    Xl         g ra   rF  )r"   global_statess     r$   r  %GlobalContextCheckpointState.__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 GlobalContextCheckpointState.

Returns None if no delta is found, otherwise, return a set() of mismatched
global key names.
r   N)r,  rF  r4  r!  r   r"  s      r$   r$  !GlobalContextCheckpointState.diff  sO     !!&&()44S9K9K9P9P9R5STq6Q;r'   c                (    U R                  U5      S L $ ra   r'  r   s     r$   r(  #GlobalContextCheckpointState.__eq__*  r*  r'   rH  N)
r=   r>   r?   r@   rF  rA   r  r$  r(  rC   r*   r'   r$   rE  rE    s    46L16*
(r'   rE  c                  6    \ rS rSrSr1 SkrS	S jrS rS rSr	g)
GlobalContexti.  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 ra   rH  rK   s    r$   r  GlobalContext.__init__=  s
    =?r'   c                >    [        [        U R                  5      5      $ ra   )rE  r=  rF  rK   s    r$   r  GlobalContext.copy_graphstate@  s    +D1B1B,CDDr'   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   rE  rF  r   _supported_global_statesr,  r4  values)r"   r  r   argss       r$   r   GlobalContext.restore_graphstateC  s    %!=>>>>!..!!"c$*G*G&HHD%%**,-1N1NN	# #	#O ++224JDJ 5r'   rH  NrA  )
r=   r>   r?   r@   r+  r\  r  r  r  rC   r*   r'   r$   rP  rP  .  s     
 @Er'   rP  c                  Z    \ rS rSrSS jrS rS rS rS rSS	S
.SS jjr	SS jr
S rSrg)	GuardsSetiP  Nc                *    Uc
  [        5       nXl        g ra   )r,  inner)r"   rc  s     r$   r  GuardsSet.__init__Q  s    =EE
r'   c                ,    [        U R                  5      $ ra   )iterrc  rK   s    r$   __iter__GuardsSet.__iter__V      DJJr'   c                ,    [        U R                  5      $ ra   )r   rc  rK   s    r$   __len__GuardsSet.__len__Y  s    4::r'   c                F    [        U R                  UR                  -
  5      $ ra   )ra  rc  r   s     r$   __sub__GuardsSet.__sub__^  s    ekk122r'   c                ,    [        U R                  5      $ ra   )r   rc  rK   s    r$   __bool__GuardsSet.__bool__a  ri  r'   Tr   )collect_debug_stackskipc                  XR                   ;   a  g U(       aO  UR                  c  [        R                  " SU-   S9Ul        UR                  c  [
        R                  5       Ul        U R                   R                  U5        g NrQ   )rt  )rc  r   r   extractr   TracingContextextract_stackadd)r"   guardrs  rt  s       r$   rz  GuardsSet.addd  s_    JJ{{"/77QXF'#1#?#?#A 

ur'   c                H    U H  nU H  nU R                  USS9  M     M     g rv  )rz  )r"   othersogs       r$   updateGuardsSet.updaten  s&    A#  r'   c                    SSK Jn  U R                   Vs1 s H  o2" UR                  U5      (       a  M  UiM      snU l        gs  snf )z.Delete all guards that contains a given sourcerQ   )is_from_sourceN)_dynamo.sourcer  rc  r   )r"   r   r  r  s       r$   remove_guards_with_source#GuardsSet.remove_guards_with_sources  s:    2 zz
!!8L8Lf)UAz

 
s
   AA)rc  ra   )r{  r   )r~  r  )r=   r>   r?   r@   r  rg  rk  rn  rq  rz  r  r  rC   r*   r'   r$   ra  ra  P  s1    
 
3  8<! $

r'   ra  c                  *    \ rS rSrSS jrS rS rSrg)GuardsContexti  c                0    [        5       U l        / U l        g ra   )ra  r  aotautograd_guardsrK   s    r$   r  GuardsContext.__init__  s    (168r'   c                R    [        [        U R                  R                  5      5      $ ra   )r  r,  r  rc  rK   s    r$   r  GuardsContext.copy_graphstate  s    $S););)A)A%BCCr'   c                f    [        U[        5      (       d   e[        UR                  5      U l        g ra   )r   r  ra  r  r  s     r$   r   GuardsContext.restore_graphstate  s)    %!67777&u':':;r'   )r  r  NrA  rC  r*   r'   r$   r  r    s    9D<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 ra   r*   r"   fn_id
identifiers      r$   add_dynamo_installed_submodule/HopSubgraphCache.add_dynamo_installed_submodule  s    KNr'   c                    g ra   r*   r"   r  s     r$   get_dynamo_installed_submodules0HopSubgraphCache.get_dynamo_installed_submodules      HKr'   c                    g ra   r*   r"   r  keys      r$   add_autograd_key_entry'HopSubgraphCache.add_autograd_key_entry  s    FIr'   c                    g ra   r*   r"   r  s     r$   get_autograd_key_entry'HopSubgraphCache.get_autograd_key_entry  s    7:r'   c                    g ra   r*   r  s      r$   add_proxy_dispatch_entry)HopSubgraphCache.add_proxy_dispatch_entry  r  r'   c                    g ra   r*   r  s     r$   get_proxy_dispatch_entry)HopSubgraphCache.get_proxy_dispatch_entry  s    9<r'   c                    g ra   r*   )r"   r  tangent_metadatagmods       r$   add_lazy_bwd_entry#HopSubgraphCache.add_lazy_bwd_entry  s     r'   c                    g ra   r*   r"   r  r  s      r$   get_lazy_bwd_entry#HopSubgraphCache.get_lazy_bwd_entry  s     r'   r*   Nr  r0   r  rJ   r  r0   r   z	list[str]r  rJ   r  r	   r  rJ   r  rJ   r  tuple[object]r  ztorch.fx.GraphModule)r  rJ   r  r  r   r0   )r=   r>   r?   r@   r   r  r  r  r  r  r  r  r  rC   r*   r'   r$   r  r    s    N NK KI I: :K K< < ( #	  1>	 r'   r  c                  z    \ 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rg)InvokeSubgraphCachei  c                p    0 U l         0 U l        [        [        5      U l        [        [
        5      U l        g ra   )autograd_cacheproxy_dispatch_cacher   listdynamo_installed_submodulesr=  lazy_bwd_cacherK   s    r$   r  InvokeSubgraphCache.__init__  s1    359;!ALTAR(  	r'   c                @    U R                   U   R                  U5        g ra   )r  r   r  s      r$   r  2InvokeSubgraphCache.add_dynamo_installed_submodule  s    ((/66zBr'   c                :    U R                   R                  U/ 5      $ ra   )r  getr  s     r$   r  3InvokeSubgraphCache.get_dynamo_installed_submodules  s    //33E2>>r'   c                     X R                   U'   g ra   )r  r  s      r$   r  *InvokeSubgraphCache.add_autograd_key_entry  s    *-J'r'   c                :    U R                   R                  US 5      $ ra   )r  r  r  s     r$   r  *InvokeSubgraphCache.get_autograd_key_entry  s    ""&&z488r'   c                     X R                   U'   g ra   )r  r  s      r$   r  ,InvokeSubgraphCache.add_proxy_dispatch_entry  s    03!!*-r'   c                :    U R                   R                  US 5      $ ra   )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$ ra   )r   r  )r"   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  s0    000"":.223C\RRr'   )r  r  r  r  NrA  r  r  r  r  r  )r  rJ   r  r  )r=   r>   r?   r@   r  r  r  r  r  r  r  r  r  rC   r*   r'   r$   r  r    sM    C?.94?		 (	 #		Sr'   r  c                  0    \ 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)r"   r  s     r$   r  HopDispatchSetCache.__init__  s    K-/B/DEr'   c                @    XR                   ;  a  g U R                   U   $ ra   r  )r"   ops     r$   	get_cacheHopDispatchSetCache.get_cache  s$     '''!!"%%r'   r  NrA  )r  ztorch._ops.HigherOrderOperatorr   zOptional[HopSubgraphCache])r=   r>   r?   r@   r  r  rC   r*   r'   r$   r  r    s    F&0&	#&r'   r  c                  b    \ rS rSr\S	S j5       r\S
S j5       rS r\S 5       r\S 5       r	Sr
g)CompileContexti  c                 H    [         R                  c   e[         R                  $ ra   )_TLScompile_contextr*   r'   r$   r  CompileContext.get  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 rg   )r   r   r4   rG   shape_env_guards)r"   r4   s     r$   r  CompileContext.__init__  s.    !Z
I%F%FFF/9+-r'   c                 J    [         R                  5       n U c  g U R                  $ ra   )r  r  r4   rK   s    r$   current_compile_id!CompileContext.current_compile_id  s"    %%'<r'   c                     [         R                  5       n U c  g U R                  c  g [        U R                  U R                  5      $ ra   )r  r  r4   rE   rG   rK   s    r$   current_trace_idCompileContext.current_trace_id  s:    %%'<??"t55r'   )rG   r4   r  N)r   r  )r   Optional[CompileContext])r=   r>   r?   r@   r   r  r  r  r  r  rC   r*   r'   r$   r  r    sU    $ $ 6 6.   6 6r'   r  c                  &   \ rS rSrSr\SS j5       r\SS j5       rS rS r	\\
S 5       5       r\S 5       rS	 r\\R                  S
 5       5       r\\R                  S 5       5       r\\R                  S 5       5       r\S 5       r\S 5       rSrg)rx  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_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.)rx  r  RuntimeError)ctxs    r$   r  TracingContext.get2  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        ['        5       U l        SU l        [-        5       U l        / U l        g NF)r  guards_contextr9  module_contextrP  global_contextr=  previously_inlined_functionspreviously_cleaned_instructions	fake_modeframe_summary_stackloc_in_framefw_metadata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)r"   r	  s     r$   r  TracingContext.__init__:  s    +o+o+o,0F)/3v,"#%  !"-1*.2+ JN 490!8!:
 "'&9&;#+-r'   c                    0 U R                   l        U R                  R                  5         U R                  R                  5         g ra   )r  rF  r  clearr  rK   s    r$   r  TracingContext.cleark  s6     ,.())//1,,224r'   c               +  r  #    0 n[         R                  5       nU R                  5        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ra   )rx  r  r4  r  r/   setattr)kwargspriorr   r  vals        r$   patchTracingContext.patchr  s        ";;=C *EJ ! HCCc" '	'!KKM#& *EKKM#& *s   A B7#B '&B7'B44B7c                     [         R                  5       n U c  [        R                  " 5       $ U R                  nU R
                  b  XR                  5       /-   n[        R                  R                  U5      $ ra   )rx  r  	tracebackStackSummaryr
  r  _populate_loc_in_frame_summary	from_list)r"   r   s     r$   ry  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  r#  FrameSummary)r"   filenamelineno
frame_names       r$   r%  -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)Nr
  r  
real_stack)	rx  r  unittestmockr   objectr2   hasattrr0  )tcr9   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)Nr0  )
rx  r  r
  r   r  r2   r4  ry  r0  pop)frame_summaryr5  oldr9   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ra   )rx  r  r  )r5  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 ra   )rx  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                  $ ra   )rx  r  r  )r5  s    r$   get_traced_codeTracingContext.get_traced_code  s"    ##%:~~r'   )r  r	  r  r  r
  r  r  r  r  r  r  r  r  r  r  r  r  r  r  N)r   Optional[TracingContext])r   rx  )r=   r>   r?   r@   r+  r   r  r  r  r  r   r   ry  r%  
contextlibr6  r<  r@  rC  rF  rC   r*   r'   r$   rx  rx  &  s     6 6 
 
/.b5 '  ' 7 7W   < "  "& 
3  
3 K K
  r'   rx  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    s6     $ 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  Nr0  )
r  r  r  r2   r4  ry  r0  r	  	shape_envcleanup)rK  rL  r9   s      r$   tracingrP    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                  T    \ 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	 rS
rg)r   i  c                    gr  r*   rK   s    r$   is_dict_keySource.is_dict_key      r'   c                    gr  r*   rK   s    r$   is_ephemeralSource.is_ephemeral  rU  r'   c                    [         era   NotImplementedError)r"   codegens     r$   reconstructSource.reconstruct      !!r'   c                    [         era   rZ  rK   s    r$   r   Source.guard_source  r_  r'   c                    [         era   rZ  rK   s    r$   r   Source.name  r_  r'   c                f    U R                  5       [        R                  L a  [        e[	        X5      $ ra   )r   rO   r   r[  r   )r"   fns     r$   
make_guardSource.make_guard  s)    +"6"66%%Tr'   c                >    U R                  5       R                  5       $ ra   )r   rn   rK   s    r$   rn   Source.is_specialized_nn_module"  s      ";;==r'   c                D    U R                  5       [        R                  :g  $ )z+True if you can guard on attributes of this)r   rO   r   rK   s    r$   subguards_allowedSource.subguards_allowed%  s      "k&A&AAAr'   r*   Nr   r   )r   r   r~   )r=   r>   r?   r@   rS  rW  r]  r   r   rf  rn   rk  rC   r*   r'   r$   r   r     s+    """
>Br'   r   c                  6    \ rS rSr% S\S'   S rS rS	S jrSrg)
ChainedSourcei+  r   basec                6    U R                   R                  5       $ ra   )ro  rS  rK   s    r$   rS  ChainedSource.is_dict_key/  s    yy$$&&r'   c                6    U R                   R                  5       $ ra   )ro  rW  rK   s    r$   rW  ChainedSource.is_ephemeral3  s    yy%%''r'   c                z    U n[        U[        5      (       a#  UR                  n[        U[        5      (       a  M#  U$ ra   )r   rn  ro  )r"   currents     r$   get_baseChainedSource.get_base6  s1    -00llG -00r'   r*   N)r   r   )	r=   r>   r?   r@   rA   rS  rW  rv  rC   r*   r'   r$   rn  rn  +  s    
L'(r'   rn  c                   SSK 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  px[        X5      (       d  M  UR                  USU45        M-     [        R                  " U 5      n	[        U	5       H5  u  pz[        X5      (       d  M  UR                  U
R
                  SU45        M7     U(       a\  US   u  p[nUSS  HI  u  pnXXL 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)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   )
FakeTensorFakeTensorModeNztracing context _get_current_dispatch_mode_stackzactive fake modezfake tensor inputrQ   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_tensorry  rz  rx  r  r	  r   torch.utils._python_dispatchr|  	enumeratereversedr   pytreetree_leavesr   )inputsry  rz  
fake_modesrK  r	  r|  imflat_inputs
flat_inputdesc1i1desc2i2s                  r$   detect_fake_moder  =  s    IJ ((**w*%%	 y*;Q?@M(#C#EFGa((q"4a89 H $$V,K";/j--z335H!LM 0 )!}	"&qrNLAb> i[wat;PQRPSSZ[`Zaabcebf g""'"-=ioo=N O""'"-=aggYH> + r'   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   )rz  r{  N)r}  rz  r~  r|  r  r  r   )rz  r|  rI   r  s       r$   active_fake_moder  i  s:    
 =M(#C#EFGa((H H r'   )rK  r  )rK  rH  ra   )r  r   )Q
__future__r   rI  dataclassesenumr   loggingre	threadingr#  unittest.mockr1  r   abcr   collectionsr   r   r   typingr   r	   r
   r   r   r   r   r   r   torch.utilsr   r  torch.utils._backport_slotsr   torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerr=   r   typesr   sympycompiler+   r,   r   rE   EnumrO   r   r   r   r   r   r  r  r  r  r  r.  r9  rE  rP  ra  r  r  r  r  localr  r  rx  r  rP  r   rn  r  r  r*   r'   r$   <module>r     s   "      	      # % !	 	 	  ) 7 B 4 !  ZZ PQ 

X $ $/J /J /Jd7j 7;
$)) ;
|	 	 d#  $   a' a'  a'H CL d#	 	 $	 d#:l : $: d#*\ * $*

0WQZ 0( (4( (,	+N#?@ 	+( (,N#?@ D)
 )
X<N#89 < B*S* *SZ& & &6 6D| |~ + + + +8 d#B B $B< d#F  $")Xr'   