
    IЦi                       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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JrJrJrJr  S SKJr  S SKJ r J!r!  S SK"J#r#  \RH                  " \%5      r&\(       a  S SK'r'S SK(r(  " S	 S
\5      r) " S S\5      r* " S S\RV                  5      r,  " S S5      r-\R\                  " SS9 " S S5      5       r/ " S S\5      r0\R\                   " S S5      5       r1\" S5      r2 \R\                   " S S5      5       r3 \R\                   " S S\35      5       r4 \R\                   " S S\35      5       r5  " S  S!\\2   5      r6 " S" S#5      r7 " S$ S%5      r8 " S& S'\6\8   5      r9 " S( S)5      r: " S* S+\6\:   5      r;  " S, S-5      r< " S. S/\6\7   5      r= " S0 S15      r> " S2 S3\>5      r? " S4 S55      r@\R                  " 5       rB  " S6 S75      rC " S8 S95      rD\SBS: j5       rE\SCS; j5       rF\R\                  " SS9 " S< S=5      5       rG\R\                  " SS9 " S> S?\G5      5       rHSDSES@ jjrISA rJg)F    )annotationsN)abstractmethod)contextmanager)AnyCallableDictGenericList
NamedTupleOptionalSetTupleTYPE_CHECKINGTypeVarUnion)_pytree)CapturedTracebackformat_frame)WeakTensorKeyDictionaryc                  0    \ rS rSr% S\S'   S\S'   S rSrg)	CompileId7   intframe_idframe_compile_idc                8    U R                    SU R                   3$ )N/)r   r   selfs    L/var/www/html/ai-image-ml/venv/lib/python3.13/site-packages/torch/_guards.py__str__CompileId.__str__@   s    --$"7"7!899     N__name__
__module____qualname____firstlineno____annotations__r!   __static_attributes__r$   r#   r    r   r   7   s    M
 :r#   r   c                  0    \ rS rSr% S\S'   S\S'   S rSrg)	TraceIdD   r   
compile_idr   attemptc                    U R                   S:X  a  [        U R                  5      $ U R                   SU R                    3$ )Nr   _)r0   strr/   r   s    r    r!   TraceId.__str__J   s7    <<1t''oo&a~66r#   r$   Nr%   r$   r#   r    r-   r-   D   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S jrSS jrSS jrSS jrS rSrg)GuardSourceQ   r                           	   
                  c                H    U [         R                  [         R                  4;   $ N)r6   GLOBAL_FSDP_MODULELOCAL_FSDP_MODULEr   s    r    is_fsdp_moduleGuardSource.is_fsdp_modulec   s    668U8UVVVr#   c                H    U [         R                  [         R                  4;   $ rH   )r6   GLOBAL_SPECIALIZED_NN_MODULELOCAL_SPECIALIZED_NN_MODULEr   s    r    is_specialized_nn_module$GuardSource.is_specialized_nn_modulef   s&    4433
 
 	
r#   c                    U [         R                  [         R                  [         R                  [         R                  4;   $ rH   )r6   GLOBAL_UNSPECIALIZED_NN_MODULELOCAL_UNSPECIALIZED_NN_MODULE&GLOBAL_UNSPECIALIZED_BUILTIN_NN_MODULE%LOCAL_UNSPECIALIZED_BUILTIN_NN_MODULEr   s    r    is_unspecialized_nn_module&GuardSource.is_unspecialized_nn_modulel   s8    6655>>==	
 
 	
r#   c                H    U [         R                  [         R                  4;   $ rH   )r6   rU   rV   r   s    r    "is_unspecialized_builtin_nn_module.GuardSource.is_unspecialized_builtin_nn_modulet   s&    >>==
 
 	
r#   c                    U [         R                  [         R                  [         R                  [         R                  [         R
                  4;   $ rH   )r6   LOCALrO   rJ   rT   rV   r   s    r    is_localGuardSource.is_localz   sA    33))55==
 
 	
r#   r$   Nreturnbool)r&   r'   r(   r)   r]   GLOBALrO   rN   CONSTANTRANDOM_VALUE	SHAPE_ENVrJ   rI   BACKWARD_STATE	EPHEMERALSYNTHETIC_LOCALrT   rS   rV   rU   rK   rP   rW   rZ   r^   r+   r$   r#   r    r6   r6   Q   st    EF"##$ HLINIO$&!%'",.)-/*W



r#   r6   c                      \ rS rSrSrg)GuardBuilderBase   r$   Nr&   r'   r(   r)   r+   r$   r#   r    rk   rk      s    r#   rk   T)frozenc                  0    \ rS rSr% S\S'   S\S'   S rSrg)	SLoc   z,Optional[Union[traceback.FrameSummary, str]]framework_locOptional[str]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 ()()
isinstancerr   r3   r   rt   )r   flocs     r    r!   SLoc.__str__   sk     $,,c22 d001 	
 *))*"TF!44tfA;r#   r$   Nr%   r$   r#   r    rp   rp      s    ??!!	r#   rp   c                  *    \ rS rSr% S\S'   S\S'   Srg)
ShapeGuard   zsympy.logic.boolalg.Booleanexprrp   slocr$   Nr&   r'   r(   r)   r*   r+   r$   r#   r    r|   r|      s    
%%
Jr#   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_stackzOptional[int]_hashc                    U R                   c:  [        U R                  U R                  [	        U R
                  5      45      U l         U R                   $ rH   )r   hashnamesourceidr   r   s    r    __hash__Guard.__hash__   s:    ::tyy$++r$..7IJKDJzzr#   c                   SSK Jn  [        U R                  [        R
                  5      =(       a"    U R                  R                  UR                  L nUU R                  (       a  U R                  R                  OS[        U R                  5      U R                  U R                  5       R                  R                  4$ )Nr   )GuardBuilder)torch._dynamo.guardsr   rx   r   	functoolspartialfuncDUPLICATE_INPUTr   valuelenr   inner_create_fn__code__co_firstlineno)r   r   is_duplicate_inputs      r    sort_keyGuard.sort_key   s     	6 t~~y'8'89 D##|'C'CC 	
 !%DKK"		NII  "++::
 	
r#   c                D    U R                  5       UR                  5       :  $ rH   )r   r   others     r    __lt__Guard.__lt__   s    }}!111r#   c                    [        U R                  [        R                  5      (       a  U R                  R                  $ U R                  $ rH   )rx   r   r   r   r   r   s    r    r   Guard.inner_create_fn   s3    dnni&7&788>>&&&>>!r#   c                6    U R                   R                  5       $ rH   )r   r   r   s    r    r   
Guard.name   s    &&++--r#   c                6    U R                   R                  5       $ rH   )r   guard_sourcer   s    r    r   Guard.source   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>)rx   weakrefReferenceTypehexr   	__class__r&   r3   )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	
          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__  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   	Exceptionlog	exceptionr3   rstripr   errorjoinformat)r   builders     r    createGuard.create#  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       $ rH   )r   rP   r   s    r    rP   Guard.is_specialized_nn_module,  s    {{3355r#   c                6    U R                   R                  5       $ rH   )r   rK   r   s    r    rK   Guard.is_fsdp_module/  s    {{))++r#   c                6    U R                   R                  5       $ rH   )r   r^   r   s    r    r^   Guard.is_local2  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_info5  s    !D
+)).
 
 	9 9	9 
 &3"~~&NNN!!), d 33(())  "*	P P		P+ 'r#   )r   r   r   r   r   ra   r3   ra   r6   )r   rk   )r&   r'   r(   r)   r*   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   staticmethodr   r   r!   r   rP   rK   r^   r   r+   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$   Nrm   r$   r#   r    r   r   \  s    r#   r   c                  0    \ rS rSr% S\S'   S\S'   S rSrg)DuplicateInputsig  r   input_source_ainput_source_bc                <    U R                   U R                  :w  d   eg rH   )r   r   r   s    r    __post_init__DuplicateInputs.__post_init__l  s    ""d&9&9999r#   r$   N)r&   r'   r(   r)   r*   r   r+   r$   r#   r    r   r   g  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 rH   r$   r   s    r    copy_graphstateCheckpointable.copy_graphstate  s    $'r#   c                    g rH   r$   r   states     r    restore_graphstate!Checkpointable.restore_graphstate  s    ,/r#   r$   N)ra   r   )r   r   )r&   r'   r(   r)   r   r   r   r+   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 rH   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 $ rH   r  r   s     r    __eq__GuardsCheckpointState.__eq__      yy4''r#   r  N)r&   r'   r(   r)   __doc__setr   r*   r  r  r  r+   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 rH   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 $ rH   r  r   s     r    r  #ModuleContextCheckpointState.__eq__  r  r#   r  N)
r&   r'   r(   r)   r  r*   r  r  r  r+   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 rH   r  r   s    r    r  ModuleContext.__init__  s	    *,r#   c                >    [        [        U R                  5      5      $ rH   )r  dictr  r   s    r    r   ModuleContext.copy_graphstate  s    +D,ABBr#   c                T    [        U[        5      (       d   eUR                  U l        g rH   )rx   r  r  r   s     r    r    ModuleContext.restore_graphstate  s#    %!=>>>>**r#   r  Nra   Noner&   r'   r(   r)   r  r   r   r+   r$   r#   r    r  r    s    -C+r#   r  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 rH   r*  )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  r*  r  r  r   r  s      r    r  !GlobalContextCheckpointState.diff  sO     !!&&()44S9K9K9P9P9R5STq6Q;r#   c                (    U R                  U5      S L $ rH   r  r   s     r    r  #GlobalContextCheckpointState.__eq__  r  r#   r,  N)
r&   r'   r(   r)   r*  r*   r  r  r  r+   r$   r#   r    r)  r)    s    46L16*
(r#   r)  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_enabledtorch_function_enabledc                    0 U l         g rH   r,  r   s    r    r  GlobalContext.__init__  s
    =?r#   c                >    [        [        U R                  5      5      $ rH   )r)  r!  r*  r   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)rx   r)  r*  r   _supported_global_statesr  r  values)r   r   r   argss       r    r    GlobalContext.restore_graphstate  s    %!=>>>>!..!!"c$*G*G&HHD%%**,-1N1NN	# #	#O ++224JDJ 5r#   r,  Nr%  )
r&   r'   r(   r)   r  rA  r  r   r   r+   r$   r#   r    r4  r4    s     
 @Er#   r4  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)	GuardsSeti  Nc                *    Uc
  [        5       nXl        g rH   )r  inner)r   rH  s     r    r  GuardsSet.__init__  s    =EE
r#   c                ,    [        U R                  5      $ rH   )iterrH  r   s    r    __iter__GuardsSet.__iter__      DJJr#   c                ,    [        U R                  5      $ rH   )r   rH  r   s    r    __len__GuardsSet.__len__  s    4::r#   c                F    [        U R                  UR                  -
  5      $ rH   )rF  rH  r   s     r    __sub__GuardsSet.__sub__$  s    ekk122r#   c                ,    [        U R                  5      $ rH   )rb   rH  r   s    r    __bool__GuardsSet.__bool__'  rN  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 Nr8   )rY  )rH  r   r   extractr   TracingContextextract_stackadd)r   guardrX  rY  s       r    r_  GuardsSet.add*  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 r[  )r_  )r   othersogs       r    updateGuardsSet.update4  s&    A#  r#   c                t    U R                    Vs1 s H  o"R                  U:w  d  M  UiM     snU l         gs  snf )z%Delete all guards with a given sourceN)rH  r   )r   r   re  s      r    remove_guards_with_source#GuardsSet.remove_guards_with_source9  s*    !%NA/C/Cv/MaN
Ns   55)rH  rH   )r`  r   )rc  r   )r&   r'   r(   r)   r  rL  rP  rS  rV  r_  rf  ri  r+   r$   r#   r    rF  rF    s2    
 
3  8<! $
Or#   rF  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 rH   )rF  r   aotautograd_guardsr   s    r    r  GuardsContext.__init__?  s    (168r#   c                R    [        [        U R                  R                  5      5      $ rH   )r   r  r   rH  r   s    r    r   GuardsContext.copy_graphstateC  s    $S););)A)A%BCCr#   c                f    [        U[        5      (       d   e[        UR                  5      U l        g rH   )rx   r   rF  r   r   s     r    r    GuardsContext.restore_graphstateF  s)    %!67777&u':':;r#   )rn  r   Nr%  r'  r$   r#   r    rl  rl  >  s    9D<r#   rl  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rg	)HopSubgraphCacheiL  c                    g rH   r$   r   	cache_key
identifiers      r    add_dynamo_identifier&HopSubgraphCache.add_dynamo_identifierM      FIr#   c                    g rH   r$   r   rx  s     r    get_dynamo_identifier&HopSubgraphCache.get_dynamo_identifierP  r|  r#   c                    g rH   r$   r   ry  keys      r    add_autograd_key_entry'HopSubgraphCache.add_autograd_key_entryS  r|  r#   c                    g rH   r$   r   ry  s     r    get_autograd_key_entry'HopSubgraphCache.get_autograd_key_entryV  s    7:r#   c                    g rH   r$   r  s      r    add_proxy_dispatch_entry)HopSubgraphCache.add_proxy_dispatch_entryY  s    HKr#   c                    g rH   r$   r  s     r    get_proxy_dispatch_entry)HopSubgraphCache.get_proxy_dispatch_entry\  s    9<r#   r$   Nrx  r3   ry  r3   rx  r3   ra   rs   ry  r3   r  r   ry  r3   )r&   r'   r(   r)   r   rz  r  r  r  r  r  r+   r$   r#   r    ru  ru  L  sV    I II II I: :K K< <r#   ru  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	rg
)InvokeSubgraphCachei`  c                .    0 U l         0 U l        0 U l        g rH   )autograd_cacheproxy_dispatch_cachedynamo_identifiersr   s    r    r  InvokeSubgraphCache.__init__a  s    359;!24r#   c                     X R                   U'   g rH   )r  rw  s      r    rz  )InvokeSubgraphCache.add_dynamo_identifierf  s    -7	*r#   c                :    U R                   R                  US 5      $ rH   )r  getr~  s     r    r  )InvokeSubgraphCache.get_dynamo_identifieri  s    &&**9d;;r#   c                     X R                   U'   g rH   )r  r  s      r    r  *InvokeSubgraphCache.add_autograd_key_entryl  s    *-J'r#   c                :    U R                   R                  US 5      $ rH   )r  r  r  s     r    r  *InvokeSubgraphCache.get_autograd_key_entryo  s    ""&&z488r#   c                     X R                   U'   g rH   )r  r  s      r    r  ,InvokeSubgraphCache.add_proxy_dispatch_entryr  s    03!!*-r#   c                :    U R                   R                  US 5      $ rH   )r  r  r  s     r    r  ,InvokeSubgraphCache.get_proxy_dispatch_entryu  s    ((,,Z>>r#   )r  r  r  Nr%  r  r  r  r  )r&   r'   r(   r)   r  rz  r  r  r  r  r  r+   r$   r#   r    r  r  `  s%    5
8<.94?r#   r  c                  0    \ rS rSrSS jr    SS jrSrg)HopDispatchSetCacheiy  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__z  s    K-/B/DEr#   c                @    XR                   ;  a  g U R                   U   $ rH   r  )r   ops     r    	get_cacheHopDispatchSetCache.get_cache  s$     '''!!"%%r#   r  Nr%  )r  ztorch._ops.HigherOrderOperatorra   zOptional[HopSubgraphCache])r&   r'   r(   r)   r  r  r+   r$   r#   r    r  r  y  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                  $ rH   )_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 )Nr   )rx   r   r/   r0   shape_env_guards)r   r/   s     r    r  CompileContext.__init__  s.    !Z
I%F%FFF/9+-r#   c                 J    [         R                  5       n U c  g U R                  $ rH   )r  r  r/   r   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      $ rH   )r  r  r/   r-   r0   r   s    r    current_trace_idCompileContext.current_trace_id  s:    %%'<??"t55r#   )r0   r/   r  N)ra   r  )ra   Optional[CompileContext])r&   r'   r(   r)   r   r  r  r  r  r  r+   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\\R                  S	 5       5       r\\R                  S
 5       5       r\\R                  S 5       5       r\S 5       rSrg)r]  i  z
Provides the currently installed TracingContext, or None.

Note that it is a staticmethod, and invocations outside of `with tracing()` (see below), are valid but
will return None.
c                 $    [        [        SS 5      $ )Ntracing_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.get  s+     ((**3*JJ
 	
r#   c                2   [        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        g NF)rl  guards_contextr  module_contextr4  global_context	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)r   r  s     r    r  TracingContext.__init__  s    +o+o+o"#%  !"-1*.2+ JN 490!8!:
 "'&9&;#r#   c                &    0 U R                   l        g rH   )r  r*  r   s    r    clearTracingContext.clear   s     ,.(r#   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rH   )r]  r  r  r  itemssetattr)kwargspriorr  r  vals        r    patchTracingContext.patch  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
                  /-   n[        R                  R                  U5      $ rH   )r]  r  	tracebackStackSummaryr  r  	from_list)r   r   s     r    r^  TracingContext.extract_stack  sc    %%'<))++(((..//E%%//66r#   c               #    #    [         R                  5       n [        R                  R                  R                  U S/ 5         [        R                  R                  R                  U SS 5          S v    S S S 5        S S S 5        g ! [         a  n[        US5      (       d  S Ul        e S nAff = f! , (       d  f       NB= f! , (       d  f       g = f7f)Nr  r  
real_stack)	r]  r  unittestmockr  objectr   hasattrr  )tces     r    clear_frameTracingContext.clear_frame"  s      !]]  ''%r
==%%b.$? @
 

  $ q,//#'AL) @?
 
sT   A C,C.B10B5C=	C
B.B))B..B11
B?	;C
CCc              #    #    [         R                  5       nU b  UR                  R                  U 5        UR                  nS Ul         S v    U b  UR                  R                  5         X!l        g ! [
         a,  n[        US5      (       d  UR                  5       Ul        e S nAff = f! U b  UR                  R                  5         X!l        f = f7f)Nr  )
r]  r  r  r   r  r   r  r^  r  pop)frame_summaryr  oldr  s       r    current_frameTracingContext.current_frameA  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rH   )r]  r  r  )r  old_output_stridess     r    report_output_strides$TracingContext.report_output_stridesV  sT      ##%:J..	3### 2 2s   0AA AAAc                ^    [         R                  " XUSS9[        R                  5       l        g )NF)lookup_line)r  FrameSummaryr]  r  r  )filenamelineno
frame_names      r    set_current_locTracingContext.set_current_locd  s%    ,5,B,Bje-
)r#   )r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  N)ra   Optional[TracingContext])ra   r]  )r&   r'   r(   r)   r  r   r  r  r  r  r   r  r^  
contextlibr  r  r  r  r+   r$   r#   r    r]  r]    s     6 6 
 
+<Z.
 '  ' 7 7   : "  "& 
3  
3 
 
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  k  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  Nr  )
r  r  r  r   r  r^  r  r  	shape_envcleanup)r  r  r  s      r    tracingr  u  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$   r   s    r    is_dict_keySource.is_dict_key      r#   c                    gr  r$   r   s    r    is_ephemeralSource.is_ephemeral  r  r#   c                    [         erH   NotImplementedError)r   codegens     r    reconstructSource.reconstruct      !!r#   c                    [         erH   r$  r   s    r    r   Source.guard_source  r)  r#   c                    [         erH   r$  r   s    r    r   Source.name  r)  r#   c                f    U R                  5       [        R                  L a  [        e[	        X5      $ rH   )r   r6   rd   r%  r   )r   fns     r    
make_guardSource.make_guard  s)    +"6"66%%Tr#   c                >    U R                  5       R                  5       $ rH   )r   rP   r   s    r    rP   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   r6   ri   r   s    r    subguards_allowedSource.subguards_allowed  s      "k&A&AAAr#   r$   Nr   r   )ra   r   r`   )r&   r'   r(   r)   r  r!  r'  r   r   r0  rP   r5  r+   r$   r#   r    r   r     s+    """
>Br#   r   c                  ,    \ rS rSr% S\S'   S rS rSrg)ChainedSourcei  r   basec                6    U R                   R                  5       $ rH   )r9  r  r   s    r    r  ChainedSource.is_dict_key  s    yy$$&&r#   c                6    U R                   R                  5       $ rH   )r9  r!  r   s    r    r!  ChainedSource.is_ephemeral  s    yy%%''r#   r$   N)r&   r'   r(   r)   r*   r  r!  r+   r$   r#   r    r8  r8    s    
L'(r#   r8  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 inputr8   zfake mode (z) from r   z doesn't match mode (z

fake mode from z allocated at:
z
fake mode from )torch._subclasses.fake_tensorr?  r@  r]  r  r  r   torch.utils._python_dispatchrB  	enumeratereversedrx   pytreetree_leavesr   )inputsr?  r@  
fake_modesr  r  rB  imflat_inputs
flat_inputdesc1i1desc2i2s                  r    detect_fake_moderS    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   )r@  rA  N)rC  r@  rD  rB  rE  rF  rx   )r@  rB  r2   rL  s       r    active_fake_moderU    s:    
 =M(#C#EFGa((H H r#   )r  r  )r  r  rH   )rI  r   )K
__future__r   r  dataclassesenumr   logging	threadingr  unittest.mockr  r   abcr   r   typingr   r   r   r	   r
   r   r   r   r   r   r   r   torch.utilsr   rG  torch.utils._tracebackr   r   torch.utils.weakr   	getLoggerr&   r   sympytorchr   r-   Enumr6   rk   	dataclassrp   r|   r   r   r   r   r   r   r   r  r  r)  r4  rF  rl  ru  r  r  localr  r  r]  r  r  r   r8  rS  rU  r$   r#   r    <module>rg     s   "           %    * B 4 ! 
 
:
 
:
7j 
70
$)) 0
f	 	 d#  $  
 b' b' b'J CL 	 	 	 :l : : *\ * *

0WQZ 0( (4( (,	+N#?@ 	+( (,N#?@ B%O %OP<N#89 <= =(?* ?2& & &6 6Di
 i
X + + + +8 d#B B $B< d#(F ( $()Xr#   