
    9i4%                     *   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
JrJrJrJrJr  S SKJr  S SKJrJrJrJrJrJr  S SKJr  S S	KJr   \" S
5      r\R<                  " \5      r  " S S\\\   5      r! " S S\\   5      r" " S S\5      r#g)    N)abstractmethod)defaultdict)chain)AnyCallableGenericOptionalTypeVarUnion)override)_serialize_single_cacheCacheArtifactCacheArtifactFactoryCacheArtifactManagerCacheArtifactsResult	CacheInfo)AppendingByteSerializer)
OrderedSetTc                   \    \ rS rSrSr\S	S j5       r\S\4S j5       r\	S\
4S j5       rSrg)
PrecompileCacheArtifact   a  
Data for each cache artifact that will be serialized and deserialized by
PrecompileContext, rather than CacheArtifactManager.
T represents the deserialized type of the artifact, i.e. the return type of after_deserialization

PrecompileCacheArtifact is a frozen dataclass - you can add new serializable fields and metadata specific to your own artifacts
as needed, and use them in after_deserialization.

Example implementation:

class MyPrecompileCacheArtifact(PrecompileCacheArtifact[MySerializableType]):
    my_field: int

    def after_deserialization(self) -> MySerializableType:
        result = pickle.loads(self.content)
        # Do some extra work post deserialization
        result.my_post_deserialization_function(self.my_field)
        return result
returnNc                     [        S5      e)Nz1Precompile cache artifacts do not populate caches)RuntimeErrorselfs    `/var/www/html/land-doc-ocr/venv/lib/python3.13/site-packages/torch/_dynamo/precompile_context.pypopulate_cache&PrecompileCacheArtifact.populate_cache3   s    NOO    c                     g)NT r   s    r   precompile_compatible-PrecompileCacheArtifact.precompile_compatible7   s    r!   c                     g)z
Code to be run after reading raw byte contents from disk.
Generally converts self.content from raw bytes back into its original form.
Nr#   r   s    r   after_deserialization-PrecompileCacheArtifact.after_deserialization;   s     	r!   r#   r   N)__name__
__module____qualname____firstlineno____doc__r   r   boolr$   r   r   r'   __static_attributes__r#   r!   r   r   r      sS    ( P P t   q  r!   r   c                   b    \ rS rSrSrS\S\S\SS4S jrS\\	   4S	 jr
S
\S\4   SS4S jrSrg)EditablePrecompileCacheArtifactD   zc
A PrecompileCacheArtifact whose content isn't encoded until we call PrecompileContext.serialize()
artifact_typecontentkeyr   Nc                 R    [         R                  " U5      U l        X0l        Xl        g N)copydeepcopyr5   r6   r4   )r   r4   r5   r6   s       r   __init__(EditablePrecompileCacheArtifact.__init__I   s     }}W-*r!   c                     [         R                  " U R                  5      n[        R                  " U R
                  U R                  U5      n[        U[        5      (       d   eU$ )z
Actually encode the object
)	pickledumpsr5   r   encode_creater4   r6   
isinstancer   )r   r5   artifacts      r   real_encode+EditablePrecompileCacheArtifact.real_encodeP   sR     ,,t||,'55'
 ($;<<<<r!   edit_fn.c                 2    U" U R                   5      U l         g)*
Edit the content of an existing artifact
N)r5   )r   rE   s     r   edit_contents-EditablePrecompileCacheArtifact.edit_contents[   s     t||,r!   )r4   r5   r6   )r*   r+   r,   r-   r.   strr   r;   r   r   rC   r   rH   r0   r#   r!   r   r2   r2   D   sV    +c +C +c +d +	4Q7 	-Xc3h%7 -D -r!   r2   c                     ^  \ rS rSr% Sr0 r\\\\	\
   \4   4   \S'   \" \5      r\\S'   \" 5       r\\   \S'   \" \S9r\\\\\   4      \S'   \" 5       r\\S'   \SU 4S jj5       r\\ SS\S\S\S\S	S
4
S jj5       5       r\SS j5       r\S\S\ S\4   S	S
4S j5       r!\S\S	\"\   4S j5       r#\S	\"\\$\4      4U 4S jj5       r%\&S\S	\4S j5       r'\SS j5       r(Sr)U =r*$ )PrecompileContextb   a  
PrecompileContext is a special CacheArtifactManager for handling precompilation
It uses the same interface as CacheArtifactManager, but handles deserialization differently: instead
of placing each artifact into respective caches, it will stitch all the cache artifacts for a single key
together and place it into a global Precompile Cache.

The following artifact types are supported by PrecompileContext:
 - BundledAOTAutogradCacheArtifact
 - DynamoCodeStateArtifact
 - AutotuneCacheArtifact (regular autotune results, same as Megacache)
_new_cache_artifacts_by_key_new_cache_artifacts_seen_artifacts)serialize_fn_serializer_cache_infor   Nc                 V   > U R                   R                  5         [        TU ]  5         g r8   )rN   clearsupercls	__class__s    r   rU   PrecompileContext.clear   s    ''--/r!   r4   r6   r5   editablec                     U(       a  [        XU5      nOB[        R                  " XU5      nXPR                  ;   a  gU R                  R	                  U5        XPR
                  U'   g)zO
Called from each caching operation to record the artifact in this
"mega" list
N)r2   r   r@   rP   addrN   )rX   r4   r6   r5   r[   rB   s         r   record_artifact!PrecompileContext.record_artifact   sX     6}sSH+99-gVH ...##H-/7'',r!   c                 0   U R                   R                  5        H^  n[        U[        5      (       a  UR	                  5       nU R
                  UR                  R                  5          R                  U5        M`     U R                   R                  5         g)z
We normally record artifacts by key, but serialization expects them to be organized
by artifact type. This function transfers artifacts from _new_cache_artifacts_by_key to _new_cache_artifacts
N)
rN   valuesrA   r2   rC   rO   rY   typeappendrU   )rX   rB   s     r   _save_artifacts_by_type)PrecompileContext._save_artifacts_by_type   sv     77>>@H($CDD#//1$$X%7%7%<%<%>?FFxP A 	''--/r!   rE   .c                     XR                   ;   d   SU S35       eU R                   U   n[        U[        5      (       d   S5       eUR                  U5        g)rG   zKey  not found in artifactszArtifact is not editableN)rN   rA   r2   rH   )rX   r6   rE   rB   s       r   edit_artifactPrecompileContext.edit_artifact   sj    
 555 	
3%./	
5 2237($CDD 	
&	
D 	w'r!   c                     U R                   R                  US5      n[        U[        5      (       a  UR	                  5       nU$ )z@
Serialize all artifacts with the given key returned in a list.
N)rN   getrA   r2   rC   )rX   r6   results      r   serialize_artifact_by_key+PrecompileContext.serialize_artifact_by_key   s=    
 0044S$?f=>>'')Fr!   c                 b   > U R                  5         SU R                  ;  a  g [        TU ]  5       $ )Nprecompile_dynamo)rd   rO   rV   	serializerW   s    r   rq   PrecompileContext.serialize   s.    ##%c&>&>>w ""r!   	artifactsc                 B   [         R                  5         0 n[        5       n[        U R	                  5       6  HG  nUR                  5       S:X  a  UR                  5         OX1UR                  '   UR                  U5        MI     SSK	J
nJn  U S    H  n[        U[        5      (       d   eUR                  5       nUR                  n0 n	U H2  n
X;   d   SU
 S35       eX   n[        U[        5      (       d   eX9U
'   M4     UR!                  XyUR                  5        M     U$ )Nautotuner   )
_BackendIdDynamoCacherp   zBackend rg   )rL   "_ensure_cache_artifacts_registeredr   r   ra   rb   r   r6   r]   torch._dynamo.packagerv   rw   rA   r   r'   backend_idswrite)rs   artifacts_by_key
cache_inforB   rv   rw   dynamo_entrycache_entrybackendsbackend_contentid_s              r   populate_caches!PrecompileContext.populate_caches   s   <<>[
y//12H}}*,'')19.NN8$ 3 	B%&9:Ll,CDDDD&<<>K"..HNPO.W(3%?V0WW.+0!(,CDDDD'/$	  
 kL<L<LM ; r!   c                     SSK Jn  SSKJn  g )Nr   )_DynamoCacheArtifact)BundledAOTAutogradCacheArtifact)ry   r   -torch._functorch._aot_autograd.autograd_cacher   )rX   r   r   s      r   rx   4PrecompileContext._ensure_cache_artifacts_registered   s
    >	
r!   r#   r)   )F)+r*   r+   r,   r-   r.   rN   dictrJ   r   r2   objectr   __annotations__r   listrO   r   r   rP   r   r   rR   tupler   rS   classmethodrU   r   r   r/   r^   rd   r   rh   r	   rm   bytesrq   staticmethodr   rx   r0   __classcell__)rY   s   @r   rL   rL   b   s   
& 	  U26:MIJJ"  2=T1B.B 2<OZ.=
 	 -DE (sD4G/G)HI  '[K(   88 8 	8
 8 
8  86 	0 	0 ( (hsCx.@ (T ( ( C H]4K   #(5	)9#:; # # #7 I  < 
 
r!   rL   )$r9   loggingr>   abcr   collectionsr   	itertoolsr   typingr   r   r   r	   r
   r   typing_extensionsr   torch.compiler._cacher   r   r   r   r   r   &torch.utils._appending_byte_serializerr   torch.utils._ordered_setr   r   	getLoggerr*   loggerr   r2   rL   r#   r!   r   <module>r      s        #  C C &  K / CL			8	$#mWQZ #L-gaj -<S
, S
r!   