
    rh                         d dl mZ d dlmZ d dlmZmZmZmZ d dl	m
Z
 d dlmZmZmZmZmZmZ d dlmZ d dlmZ 	  ed      Z G d	 d
eee         Z G d de      Zy)    )abstractmethod)defaultdict)AnyGenericOptionalTypeVar)override)_serialize_single_cacheCacheArtifactCacheArtifactFactoryCacheArtifactManagerCacheArtifactsResult	CacheInfo)AppendingByteSerializer)
OrderedSetTc                   N    e Zd ZdZedd       Zedefd       Zede	fd       Z
y)PrecompileCacheArtifacta5  
    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                     t        d      )Nz1Precompile cache artifacts do not populate caches)RuntimeErrorselfs    s/var/www/html/ai-insurance-compliance-backend/venv/lib/python3.12/site-packages/torch/_dynamo/precompile_context.pypopulate_cachez&PrecompileCacheArtifact.populate_cache.   s    NOO    c                      y)NT r   s    r   precompile_compatiblez-PrecompileCacheArtifact.precompile_compatible2   s    r   c                      y)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_deserializationz-PrecompileCacheArtifact.after_deserialization6   s     	r   r   N)__name__
__module____qualname____doc__r	   r   boolr   r   r   r!   r   r   r   r   r      sS    ( P P t   q  r   r   c            
       ~    e Zd ZU dZi Zeeef   ed<    e	e
      Zeed<    e       Zee   ed<    ee      Zeeee
e   f      ed<    e       Zeed<   ed fd
       Zeededededd	fd              Zedd       Zededee   fd       Zedeeeef      f fd       Zededefd       Z edd       Z! xZ"S )PrecompileContexta  
    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    | j                   j                          t        |           y N)r*   clearsupercls	__class__s    r   r2   zPrecompileContext.clear]   s    ''--/r   artifact_typekeycontentc                     t        j                  |||      }|| j                  v ry|| j                  |<   | j                  j	                  |       y)zg
        Called from each caching operation to record the artifact in this
        "mega" list
        N)r   encode_creater,   r*   add)r5   r7   r8   r9   artifacts        r   record_artifactz!PrecompileContext.record_artifactb   sO     (55mS'R s***/7'',)r   c                     | j                   j                         D ]8  }| j                  |j                  j	                            j                  |       : | j                   j                          y)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)r*   valuesr+   r6   typeappendr2   )r5   r=   s     r   _save_artifacts_by_typez)PrecompileContext._save_artifacts_by_typez   s^     77>>@ 	QH$$X%7%7%<%<%>?FFxP	Q''--/r   c                 :    | j                   j                  |d      S )zP
        Serialize all artifacts with the given key returned in a list.
        N)r*   get)r5   r8   s     r   serialize_artifact_by_keyz+PrecompileContext.serialize_artifact_by_key   s    
 ..223==r   c                 @    | j                          t        | 	         S r1   )rC   r3   	serializer4   s    r   rH   zPrecompileContext.serialize   s    ##%w ""r   	artifactsc                     t        d      )NTODO)NotImplementedError)rI   s    r   populate_cachesz!PrecompileContext.populate_caches   s    !&))r   c                     ddl m} y )Nr   )BundledAOTAutogradCacheArtifact)-torch._functorch._aot_autograd.autograd_cacherO   )r5   rO   s     r   "_ensure_cache_artifacts_registeredz4PrecompileContext._ensure_cache_artifacts_registered   s    	
r   r"   )#r#   r$   r%   r&   r*   dictstrr   __annotations__r   listr+   r   r   r,   r   r
   r.   tupler   r/   classmethodr2   r	   r   r>   rC   r   rF   bytesrH   staticmethodrM   rQ   __classcell__)r6   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>rb      sY     # 2 2 &  K / CL#mWQZ #LY
, Y
r   