
    9i+                        S SK r S SKrS SKrS SKJrJ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  S SKJrJrJr  S S	KJr  \R.                  " \5      r\R4                  " S
S9 " S S\5      5       r " S S5      r\R4                   " S S5      5       rS\SSSS4S jrS\SS4S jr\ \!\"\   4   r# " S S5      r$g)    N)ABCabstractmethod)defaultdict)	Generator)contextmanager)chain)AnyOptional)AppendingByteSerializerBytesReaderBytesWriter)
OrderedSetT)frozenc                       \ rS rSr% Sr\\S'   \R                  " SS9r	\
\S'   \S\SS S	S
4S j5       r\S\S\S	S 4S j5       r\S\S	\
4S j5       r\SS j5       rS	\4S jr\S	\4S j5       rSrg
)CacheArtifact   zG
Data for each cache artifact that will be serialized and deserialized
keyF)reprcontentwriterclsreturnNc                 p    U R                  UR                  5        U R                  UR                  5        g N)	write_strr   write_bytesr   )r   r   s     U/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/compiler/_cache.py	serializeCacheArtifact.serialize   s&    !3;;'    artifact_typereaderc                 n    UR                  5       nUR                  5       n[        R                  XU5      $ r   )read_str
read_bytesCacheArtifactFactorycreate)r!   r"   r   r   s       r   deserializeCacheArtifact.deserialize$   s/    oo##%#**=wGGr    c                 V    [        U [        5      (       d   S[        U 5       35       eU $ )NzExpected bytes, got )
isinstancebytestype)r   s    r   encodeCacheArtifact.encode*   s+    '5))Q-A$w-+QQ)r    c                     g r    selfs    r   populate_cacheCacheArtifact.populate_cache/   s    r    c                     g)NFr1   r2   s    r   precompile_compatible#CacheArtifact.precompile_compatible3   s    r    c                      [        S5      e)z
Returns the type of the artifact. Must be unique across all CacheArtifact classes.

CacheArtifactFactory.register will add property method to CacheInfo based on this (def {type}_artifacts)
that returns all artifacts for specific cache.
z9CacheArtifact is an abstract class, please use a subclass)RuntimeErrorr1   r    r   r-   CacheArtifact.type6   s     VWWr    r1   r   N)__name__
__module____qualname____firstlineno____doc__str__annotations__dataclassesfieldr   r,   staticmethodr   r   r   r(   r	   r.   r   r4   boolr7   r-   __static_attributes__r1   r    r   r   r      s     
H &&E2GU2(+ (O ( ( ( H3 H H H H
      t  X# X Xr    r   c            	           \ rS rSr% Sr0 r\\\\	   4   \
S'   \S\\	   S\\	   4S j5       r\S\S\\	   4S j5       r\S\S	\S
\S\	4S j5       r\S\S	\S
\S\	4S j5       rSrg)r&   A   z@
Factory for creating CacheArtifact objects based on their type
_artifact_typesartifact_clsr   c                    ^ UR                  5       mUR                  5       U R                  ;  d   ST S35       eXR                  T'   [        [        T S3[	        U4S j5      5        U$ )NArtifact of type=z2 already registered in mega-cache artifact factory
_artifactsc                 "   > U R                   T   $ r   	artifacts)r3   artifact_type_keys    r   <lambda>/CacheArtifactFactory.register.<locals>.<lambda>R   s    $..1B"Cr    )r-   rK   setattr	CacheInfoproperty)r   rL   rS   s     @r   registerCacheArtifactFactory.registerH   s}    (--/  "#*=*== 	
 122de	
= 2>-. !,CD	

 r    rS   c                 T    XR                   ;   d   SU S35       eU R                   U   $ )NrN   z. not registered in mega-cache artifact factory)rK   )r   rS   s     r   _get_artifact_type'CacheArtifactFactory._get_artifact_typeV   s>     $7$77 	
 122`a	
7 ""#455r    r   r   c                 4    U R                  U5      nU" X#5      $ r   )r\   r   rS   r   r   rL   s        r   r'   CacheArtifactFactory.create]   s    --.?@C))r    c                 R    U R                  U5      nU" X$R                  U5      5      $ r   )r\   r.   r_   s        r   encode_create"CacheArtifactFactory.encode_createb   s+     --.?@C!4!4W!=>>r    r1   N)r=   r>   r?   r@   rA   rK   dictrB   r-   r   rC   classmethodrY   r\   r,   r'   r	   rb   rH   r1   r    r   r&   r&   A   s     79OT#tM2238D$7 D<O   63 64;N 6 6 *s * *u * * * ? #?*-?8;?	? ?r    r&   c                   4   \ rS rSr% Sr\R                  " S S9r\\	\
\	   4   \S'   \S\
\	   4S j5       r\S\
\	   4S j5       r\S\
\	   4S	 j5       r\S\
\	   4S
 j5       r\S\
\	   4S j5       r\S\
\	   4S j5       rS\SS4S jrSS jrS\4S jrSrg)rW   j   zV
Return value of serialization and deserialization for the purpose of
instrumentation
c                       [        [        5      $ r   )r   listr1   r    r   rT   CacheInfo.<lambda>r   s	    D 1r    )default_factoryrR   r   c                     g r   r1   r2   s    r   inductor_artifactsCacheInfo.inductor_artifactsv       r    c                     g r   r1   r2   s    r   autotune_artifactsCacheInfo.autotune_artifactsz   ro   r    c                     g r   r1   r2   s    r   aot_autograd_artifacts CacheInfo.aot_autograd_artifacts~   ro   r    c                     g r   r1   r2   s    r   pgo_artifactsCacheInfo.pgo_artifacts   ro   r    c                     g r   r1   r2   s    r   !precompile_aot_autograd_artifacts+CacheInfo.precompile_aot_autograd_artifacts   ro   r    c                     g r   r1   r2   s    r   precompile_dynamo_artifacts%CacheInfo.precompile_dynamo_artifacts   ro   r    artifactNc                 p    U R                   UR                  5          R                  UR                  5        g r   )rR   r-   appendr   )r3   r   s     r   addCacheInfo.add   s#    x}}'..x||<r    c                 8    U R                   R                  5         g r   )rR   clearr2   s    r   r   CacheInfo.clear   s    r    c                 $    U R                   (       + $ r   rQ   r2   s    r   emptyCacheInfo.empty   s    >>!!r    r1   r<   )r=   r>   r?   r@   rA   rD   rE   rR   r   rB   ri   rC   rX   rm   rq   rt   rw   rz   r}   r   r   r   rG   r   rH   r1   r    r   rW   rW   j   s   
 .9->->1.I{3S	>* 
 DI   DI   S	   tCy   49   T#Y  =M =d ="t "r    rW   r   r   ztuple[str, list[CacheArtifact]]r   c                     U R                  US   5        U R                  [        US   5      5        US    H  n[        R	                  X5        M     g )Nr      )r   write_uint64lenr   r   )r   r   r   s      r   _serialize_single_cacher      sH     SV
CF$F1 r    r"   c                     / nU R                  5       nU R                  5       n[        U5       H'  nUR                  [        R                  X 5      5        M)     X!4$ r   )r$   read_uint64ranger   r   r(   )r"   rR   rS   num_artifacts_s        r   _deserialize_single_cacher      sX     I)&&(M=!223DMN " ''r    c            	          \ rS rSr% Sr\" \5      r\\	S'   \
" 5       r\
\   \	S'   \" \S9r\\\\\   4      \	S'   \" 5       r\\	S'   \SS
 j5       r\\S\S   4S j5       5       r\S\S\S\SS	4S j5       r\S\4S j5       r\S\\\\4      4S j5       r\ S\S\\   4S j5       r!\ S\S\4S j5       r"\SS j5       r#Sr$g	)CacheArtifactManager   a!  
Lightweight manager class for collecting and processing cache artifacts for
hot loading

Intended Lifecycle:
- Execute code via torch.compile, this will call
    CacheArtifactManager.record_artifact on each cache artifact
- Call CacheArtifactManager.serialize to convert all the cache artifacts
    to portable format
- Call CacheArtifactManager.deserialize to hot load the cache artifacts on
    a potentially different process

NOTE: There's no FB/FC guarantees, results of cache artifacts will not be
      used unless code version matches.
_new_cache_artifacts_seen_artifactsserialize_fn_serializer_cache_infor   Nc                     U R                   R                  5         U R                  R                  5         U R                  R                  5         U R                  R                  5         g r   )r   r   r   r   r   r   s    r   r   CacheArtifactManager.clear   sF      &&(!!#r    )NNNc              #     #    U R                   nU R                  nU R                  nU R                  n[	        [
        5      U l         [        5       U l        [        [        S9U l        U R                  R                  5       U l         S v   Xl         X l        X0l        X@l        g ! Xl         X l        X0l        X@l        f = f7f)Nr   )
r   r   r   r   r   ri   r   r   r   	__class__)r   original_new_cache_artifactsoriginal_seen_artifactsoriginal_serializeroriginal_cache_infos        r   with_fresh_cache%CacheArtifactManager.with_fresh_cache   s      (+'?'?$"%"5"5!oo!oo#.t#4 (l1?VW//335	2'C$"91O1O (D$"91O1Os   BCB% C%B??Cr!   r   r   c                     [         R                  XU5      nX@R                  ;   a  g[        R	                  S[        U5      5        U R                  U   R                  U5        U R                  R                  U5        g)zO
Called from each caching operation to record the artifact in this
"mega" list
NzRecording %s)	r&   rb   r   logdebugrB   r   r   r   )r   r!   r   r   r   s        r   record_artifact$CacheArtifactManager.record_artifact   sd     (55m'R***		.#h-0  /66x@)r    c                 2    [        U R                  5      S:g  $ )z7
Have we seen new artifacts since last serialize call?
r   )r   r   r   s    r   need_serialize#CacheArtifactManager.need_serialize   s    
 3++,11r    c                 P   [        U R                  R                  5       6  H4  n[        R	                  SU5        U R
                  R                  U5        M6     U R
                  R                  5       (       a  g [        R                  " U R
                  5      nU R                  R                  U R                  R                  5       5        U R                  R                  5       nU R                  R                  5         X24$ ! [         a    [        R!                  SSS9   gf = f)z/
Converts the "mega" list into portable format
z
saving: %sNz Failed to pickle cache artifactsTexc_info)r   r   valuesr   r   r   r   r   copydeepcopyr   extenditemsto_bytesr   	Exceptionwarning)r   r   infoartifact_bytess       r   r   CacheArtifactManager.serialize  s    
 s77>>@AHIIlH-OO) B ??  "" 		K ==1DOO""3#;#;#A#A#CD __557N$$**,!'' 	KKK:TKJ	Ks   :B	D D%$D%serialized_artifactsc                      [         R                  5         [        [        R                  " U [
        S95      nU$ ! [         a    [        R                  SSS9   gf = f)z7
Converts the portable format back into CacheArtifacts
)deserialize_fnz#Failed to un-pickle cache artifactsTr   N)	r   "_ensure_cache_artifacts_registeredrd   r   to_listr   r   r   r   )r   rR   s     r   r(    CacheArtifactManager.deserialize  s^    

	 CCE'//(#<I 	  	KK=KM	s   6: AArR   c                     [        5       n[        U R                  5       6  H:  n[        R	                  SU5        UR                  U5        UR                  5         M<     U$ )Nzwriting: %s)rW   r   r   r   r   r   r4   )rR   r   r   s      r   populate_caches$CacheArtifactManager.populate_caches/  sN    {y//12HIImX.HHX##% 3
 r    c                 4    SSK Jn  SSKJn  SSKJn  SSKJn  g)zWhen deserializing caches in fresh process, we need to ensure that all
cache artifacts are registered in the cache registry. This is done by
simply importing all the cache artifacts already wrapped with register call.
r   )PGOCacheArtifact)AOTAutogradCacheArtifact)InductorCacheArtifact)AutotuneCacheArtifactN)torch._dynamo.pgor   -torch._functorch._aot_autograd.autograd_cacher   torch._inductor.codecacher   &torch._inductor.runtime.autotune_cacher   )r   r   r   r   r   s        r   r   7CacheArtifactManager._ensure_cache_artifacts_registered9  s     	7	
 	D	
r    r1   r<   )%r=   r>   r?   r@   rA   r   ri   r   CacheArtifactsResultrC   r   r   r   r   r   r   tuplerB   rW   r   re   r   r   r   r   r	   r   rG   r   r
   r,   r   rF   r(   r   r   rH   r1   r    r   r   r      s   " 2=T1B.B 2<OZ.=
 	 -DE (sD4G/G)HI  '[K(    2+;!< 2  2$ ** * 	*
 
* *" 2t 2 2 (5	)9#:;  2 % H=Q4R  $ #7 I   
 
r    r   )%r   rD   loggingabcr   r   collectionsr   collections.abcr   
contextlibr   	itertoolsr   typingr	   r
   &torch.utils._appending_byte_serializerr   r   r   torch.utils._ordered_setr   	getLoggerr=   r   	dataclassr   r&   rW   r   r   rd   rB   ri   r   r   r1   r    r   <module>r      s       # # % %    
 0 ! d#'XC 'X $'XT&? &?R *" *" *"Z22?2	2	(	(&	( Cm!445 V
 V
r    