o
    h                     @   s   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
 d
eee ZG dd deZdS )    )abstractmethod)defaultdict)AnyGenericOptionalTypeVar)override)_serialize_single_cacheCacheArtifactCacheArtifactFactoryCacheArtifactManagerCacheArtifactsResult	CacheInfo)AppendingByteSerializer)
OrderedSetTc                   @   sB   e Zd ZdZed
ddZedefddZede	fdd	Z
dS )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                 C      t d)Nz1Precompile cache artifacts do not populate caches)RuntimeErrorself r   f/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torch/_dynamo/precompile_context.pypopulate_cache.      z&PrecompileCacheArtifact.populate_cachec                 C      dS )NTr   r   r   r   r   precompile_compatible2   s   z-PrecompileCacheArtifact.precompile_compatiblec                 C   r   )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   r   r   r   after_deserialization6   s   z-PrecompileCacheArtifact.after_deserializationr   N)__name__
__module____qualname____doc__r   r   boolr   r   r   r   r   r   r   r   r      s    r   c                
       s  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dZeeee
e f  ed< e Zeed< ed fd
dZeededededd	fddZedddZededee fddZedeeeef  f fddZededefddZ edddZ!  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)Zserialize_fn_serializer_cache_infor   Nc                    s   | j   t   d S N)r&   clearsupercls	__class__r   r   r,   ]   s   
zPrecompileContext.clearartifact_typekeycontentc                 C   s6   t |||}|| jv rdS || j|< | j| dS )zg
        Called from each caching operation to record the artifact in this
        "mega" list
        N)r   Zencode_creater(   r&   add)r/   r2   r3   r4   artifactr   r   r   record_artifactb   s
   

z!PrecompileContext.record_artifactc                 C   s4   | j  D ]}| j|j  | q| j   dS )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'   r1   typeappendr,   )r/   r6   r   r   r   _save_artifacts_by_typez   s   z)PrecompileContext._save_artifacts_by_typec                 C   s   | j |dS )zP
        Serialize all artifacts with the given key returned in a list.
        N)r&   get)r/   r3   r   r   r   serialize_artifact_by_key   s   z+PrecompileContext.serialize_artifact_by_keyc                    s   |    t  S r+   )r;   r-   	serializer.   r0   r   r   r>      s   
zPrecompileContext.serialize	artifactsc                 C   r   )NTODO)NotImplementedError)r?   r   r   r   populate_caches   r   z!PrecompileContext.populate_cachesc                 C   s   ddl m} d S )Nr   )BundledAOTAutogradCacheArtifact)Z-torch._functorch._aot_autograd.autograd_cacherC   )r/   rC   r   r   r   "_ensure_cache_artifacts_registered   s   z4PrecompileContext._ensure_cache_artifacts_registeredr   )#r    r!   r"   r#   r&   dictstrr
   __annotations__r   listr'   r   r   r(   r   r	   r)   tupler   r*   classmethodr,   r   r   r7   r;   r   r=   bytesr>   staticmethodrB   rD   __classcell__r   r   r0   r   r%   ?   s>   
 	 r%   N)abcr   collectionsr   typingr   r   r   r   Ztyping_extensionsr   Ztorch.compiler._cacher	   r
   r   r   r   r   Z&torch.utils._appending_byte_serializerr   Ztorch.utils._ordered_setr   r   r   r%   r   r   r   r   <module>   s     &