a
    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                   @   sF   e Zd ZdZeddddZeedddZee	d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
    Nreturnc                 C   s   t dd S )Nz1Precompile cache artifacts do not populate caches)RuntimeErrorself r   N/var/www/auris/lib/python3.9/site-packages/torch/_dynamo/precompile_context.pypopulate_cache.   s    z&PrecompileCacheArtifact.populate_cachec                 C   s   dS )NTr   r   r   r   r   precompile_compatible2   s    z-PrecompileCacheArtifact.precompile_compatiblec                 C   s   dS )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)__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d	 fd
dZeeeeeddddZedd	ddZeeee dddZeeeeef  d	 fddZeeedddZ ed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_infoNr   c                    s   | j   t   d S N)r#   clearsupercls	__class__r   r   r)   ]   s    
zPrecompileContext.clear)artifact_typekeycontentr   c                 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,   r/   r0   r1   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$   r.   typeappendr)   )r,   r3   r   r   r   _save_artifacts_by_typez   s    z)PrecompileContext._save_artifacts_by_type)r0   r   c                 C   s   | j |dS )zP
        Serialize all artifacts with the given key returned in a list.
        N)r#   get)r,   r0   r   r   r   serialize_artifact_by_key   s    z+PrecompileContext.serialize_artifact_by_keyc                    s   |    t  S r(   )r8   r*   	serializer+   r-   r   r   r;      s    zPrecompileContext.serialize)	artifactsr   c                 C   s   t dd S )NZTODO)NotImplementedError)r<   r   r   r   populate_caches   s    z!PrecompileContext.populate_cachesc                 C   s   ddl m} d S )Nr   )BundledAOTAutogradCacheArtifact)Z-torch._functorch._aot_autograd.autograd_cacher?   )r,   r?   r   r   r   "_ensure_cache_artifacts_registered   s    z4PrecompileContext._ensure_cache_artifacts_registered)#r   r   r   r    r#   dictstrr
   __annotations__r   listr$   r   r   r%   r   r	   r&   tupler   r'   classmethodr)   r   r   r4   r8   r   r:   bytesr;   staticmethodr>   r@   __classcell__r   r   r-   r   r"   ?   s4   
	 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    &