
    7h                         S SK Jr  S SKJr  S SK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 " S	 S
\\\   5      r " S S\5      rg)    )abstractmethod)defaultdict)AnyGenericOptionalTypeVar)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    Z/var/www/fran/franai/venv/lib/python3.13/site-packages/torch/_dynamo/precompile_context.pypopulate_cache&PrecompileCacheArtifact.populate_cache.   s    NOO    c                     g)NT r   s    r   precompile_compatible-PrecompileCacheArtifact.precompile_compatible2   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_deserialization6   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            
         ^  \ rS rSr% Sr0 r\\\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
4S j5       5       r\S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$$ )PrecompileContext?   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
 - CodeStateArtifact (from torch._dynamo.package once available)
_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 N)r1   clearsupercls	__class__s    r   r9   PrecompileContext.clear]   s    ''--/r   artifact_typekeycontentc                     [         R                  " XU5      nX@R                  ;   a  gX@R                  U'   U R                  R	                  U5        g)zO
Called from each caching operation to record the artifact in this
"mega" list
N)r   encode_creater3   r1   add)r<   r?   r@   rA   artifacts        r   record_artifact!PrecompileContext.record_artifactb   sI     (55m'R ***/7'',)r   c                     U R                   R                  5        H9  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)r1   valuesr2   r=   typeappendr9   )r<   rE   s     r   _save_artifacts_by_type)PrecompileContext._save_artifacts_by_typez   sX     77>>@H$$X%7%7%<%<%>?FFxP A''--/r   c                 :    U R                   R                  US5      $ )z@
Serialize all artifacts with the given key returned in a list.
N)r1   get)r<   r@   s     r   serialize_artifact_by_key+PrecompileContext.serialize_artifact_by_key   s    
 ..223==r   c                 @   > U R                  5         [        TU ]	  5       $ r8   )rL   r:   	serializer;   s    r   rS   PrecompileContext.serialize   s    ##%w ""r   	artifactsc                     [        S5      e)NTODO)NotImplementedError)rU   s    r   populate_caches!PrecompileContext.populate_caches   s    !&))r   c                     SSK Jn  g )Nr   )BundledAOTAutogradCacheArtifact)-torch._functorch._aot_autograd.autograd_cacher\   )r<   r\   s     r   "_ensure_cache_artifacts_registered4PrecompileContext._ensure_cache_artifacts_registered   s    	
r   r    r&   )%r'   r(   r)   r*   r+   r1   dictstrr   __annotations__r   listr2   r   r   r3   r   r
   r5   tupler   r6   classmethodr9   r	   r   rF   rL   r   rP   bytesrS   staticmethodrY   r^   r-   __classcell__)r=   s   @r   r/   r/   ?   sm   	  =?c=&8!9>1<T1B.B 2<OZ.=
 	 -DE (sD4G/G)HI  '[K(  ** * 	*
 
*  *, 0 0 >C >H]4K > > #(5	)9#:; # # *#7 *I * * 
 
r   r/   N)abcr   collectionsr   typingr   r   r   r   typing_extensionsr	   torch.compiler._cacher
   r   r   r   r   r   &torch.utils._appending_byte_serializerr   torch.utils._ordered_setr   r   r   r/   r    r   r   <module>rp      sY     # 2 2 &  K / CL#mWQZ #LY
, Y
r   