o
    Zh                     @   s   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ eeZG dd	 d	eZejd
dG dd dZejG dd dZG dd dZdS )    N)Enum)OptionalUnion)
JsonDataTyRemoteCacheJsonSerde)	cache_dir)AppendingByteSerializerBytesReaderBytesWriter)
OrderedSetc                   @   s    e Zd ZdZdZdZdZdZdS )CacheArtifactTypez
    Type of cache
    r            N)__name__
__module____qualname____doc__INDUCTORAUTOTUNEAOT_AUTOGRADPGO r   r   D/var/www/auris/lib/python3.10/site-packages/torch/compiler/_cache.pyr      s    r   T)frozenc                   @   sf   e Zd ZU dZeed< eed< ejddZ	e
ed< ededd d	d
fddZeded	d fddZd
S )CacheArtifactzO
    Data for each cache artifact that will be serialized and deserialized
    typekeyF)reprcontentwriterclsreturnNc                 C   s*   |  |jj | |j | |j d S N)Zwrite_uint64r   valueZ	write_strr   write_bytesr   )r    r!   r   r   r   	serialize*   s   zCacheArtifact.serializereaderc                 C   s(   |   }|  }|  }tt|||S r#   )Zread_uint64Zread_str
read_bytesr   r   )r'   r   r   r   r   r   r   deserialize0   s   zCacheArtifact.deserialize)r   r   r   r   r   __annotations__strdataclassesfieldr   bytesstaticmethodr
   r&   r	   r)   r   r   r   r   r       s   
 r   c                   @   s   e Zd ZU dZejedZee e	d< ejedZ
ee e	d< ejedZee e	d< ejedZee e	d< dedd	fd
dZdddZd	S )	CacheInfozb
    Return value of serialization and deserialization for the purpose of
    instrumentation
    )default_factoryinductor_artifactsautotune_artifactsaot_autograd_artifactspgo_artifactsartifactr"   Nc                 C   s   |j tjkr| j|j d S |j tjkr| j|j d S |j tjkr-| j	|j d S |j tj
kr<| j|j d S td|j   d S )NUnsupported artifact type )r   r   r   r2   appendr   r   r3   r   r4   r   r5   logwarning)selfr6   r   r   r   addD   s   zCacheInfo.addc                 C   ,   | j   | j  | j  | j  d S r#   )r2   clearr3   r4   r5   )r;   r   r   r   r>   P   s   


zCacheInfo.clearr"   N)r   r   r   r   r,   r-   listr2   r+   r*   r3   r4   r5   r   r<   r>   r   r   r   r   r0   8   s   
 r0   c                	   @   s   e Zd ZU dZg Zee ed< e Z	ee ed< e
ejdZe
e ed< e Zeed< edd	d
Zedededeeef ddfddZedefddZedeeeef  fddZededee fddZdS )CacheArtifactManageraQ  
    Lightweight manager class for collecting and processing cache artifacts for
    hot loading

    Intended Lifecycle:
    - Execute code via torch.compile, this will call
        CacheArtifactManager.record_artifact on each cache artifact
    - Call CacheArtifactManager.serialize to convert all the cache artifacts
        to portable format
    - Call CacheArtifactManager.deserialize to hot load the cache artifacts on
        a potentially different process

    NOTE: There's no FB/FC guarentees, results of cache artifacts will not be
          used unless code version matches.
    _new_cache_artifacts_seen_artifacts)Zserialize_fn_serializer_cache_infor"   Nc                 C   r=   r#   )rB   r>   rC   rD   rE   r!   r   r   r   r>   u   s   


zCacheArtifactManager.clearartifact_typer   r   c                 C   s|   |t jkrt|trJ t }||}t|tsJ t|||}|| jv r(dS t	dt
| | j| | j| dS )zg
        Called from each caching operation to record the artifact in this
        "mega" list
        NzRecording %s)r   r   
isinstancer.   r   encoder   rC   r9   debugr+   rB   r8   r<   )r!   rG   r   r   Zserder6   r   r   r   record_artifact|   s   


z$CacheArtifactManager.record_artifactc                 C   s   t | jdkS )zG
        Have we seen new artifacts since last serialize call?
        r   )lenrB   rF   r   r   r   need_serialize   s   z#CacheArtifactManager.need_serializec                 C   s   | j D ]}td| | j| qzt| j}| j| j  | j	 }| j 
  ||fW S  ty?   tjddd Y dS w )z?
        Converts the "mega" list into portable format
        z
saving: %sz Failed to pickle cache artifactsTexc_infoN)rB   r9   rJ   rE   r<   copydeepcopyrD   extendto_bytesr>   	Exceptionr:   )r!   r6   infoZartifact_bytesr   r   r   r&      s   



zCacheArtifactManager.serializeserialized_artifactsc                 C   s<  z
t j| tjd}W n ty   tjddd Y dS w ddlm} ddl	m
} dd	lm} dd
lm} | }t }|D ]_}td| || |jtjkrX||j|j q<|jtjkrotjt |j}	||	|j q<|jtjkr~||j|j q<|jtjkr||j|j}
|
dusJ q<td|j  q<|S )zR
        Converts the portable format back into various filesystem caches
        )Zdeserialize_fnz#Failed to un-pickle cache artifactsTrN   Nr   )write_local_impl)AOTAutogradCache)FxGraphCache)_LocalAutotuneCacheBackendzwriting: %sr7   ) r   Zto_listr   r)   rT   r9   r:   Ztorch._dynamo.pgorW   Z-torch._functorch._aot_autograd.autograd_cacherX   Ztorch._inductor.codecacherY   Z&torch._inductor.runtime.autotune_cacherZ   r0   rJ   r<   r   r   r   Z_write_to_local_cacher   r   r   ospathjoinr   _putr   r   )rV   Z	artifactsrW   rX   rY   rZ   Zautotune_cacherU   r6   r   metar   r   r   r)      s:   

z CacheArtifactManager.deserializer?   )r   r   r   r   rB   r@   r   r*   r   rC   r   r&   rD   r0   rE   classmethodr>   r   r+   r   r.   r   rK   boolrM   r   tupler/   r)   r   r   r   r   rA   W   s4   
 
rA   )rP   r,   loggingr[   enumr   typingr   r   Ztorch._inductor.remote_cacher   r   Z%torch._inductor.runtime.runtime_utilsr   Z&torch.utils._appending_byte_serializerr   r	   r
   Ztorch.utils._ordered_setr   	getLoggerr   r9   r   	dataclassr   r0   rA   r   r   r   r   <module>   s"    

