o
    h                     @   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   i/var/www/html/construction_image-detection-poc/venv/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)write_uint64r   value	write_strr   write_bytesr   )r    r!   r   r   r   	serialize*   s   zCacheArtifact.serializereaderc                 C   s(   |   }|  }|  }tt|||S r#   )read_uint64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   r6   appendr   r   r7   r   r8   r   r9   logwarning)selfr:   r   r   r   addD   s   zCacheInfo.addc                 C   ,   | j   | j  | j  | j  d S r#   )r6   clearr7   r8   r9   )r?   r   r   r   rB   P   s   


zCacheInfo.clearr"   N)r   r   r   r   r0   r1   listr6   r/   r.   r7   r8   r9   r   r@   rB   r   r   r   r   r4   8   s   
 r4   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)serialize_fn_serializer_cache_infor"   Nc                 C   rA   r#   )rF   rB   rG   rI   rJ   r!   r   r   r   rB   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   
isinstancer2   r   encoder   rG   r=   debugr/   rF   r<   r@   )r!   rL   r   r   serder:   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   )lenrF   rK   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)rF   r=   rO   rJ   r@   copydeepcopyrI   extendto_bytesrB   	Exceptionr>   )r!   r:   info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
        )deserialize_fnz#Failed to un-pickle cache artifactsTrT   Nr   )write_local_impl)AOTAutogradCache)FxGraphCache)_LocalAutotuneCacheBackendzwriting: %sr;   ) r   to_listr   r-   rZ   r=   r>   torch._dynamo.pgor_   -torch._functorch._aot_autograd.autograd_cacher`   torch._inductor.codecachera   &torch._inductor.runtime.autotune_cacherb   r4   rO   r@   r   r   r   _write_to_local_cacher   r   r   ospathjoinr   _putr   r   )r]   	artifactsr_   r`   ra   rb   autotune_cacher[   r:   r   metar   r   r   r-      s:   

z CacheArtifactManager.deserializerC   )r   r   r   r   rF   rD   r   r.   r   rG   r   r(   rI   r4   rJ   classmethodrB   r   r/   r   r2   r   rQ   boolrS   r   tupler3   r-   r   r   r   r   rE   W   s4   
 
rE   )rV   r0   loggingri   enumr   typingr   r   torch._inductor.remote_cacher   r   %torch._inductor.runtime.runtime_utilsr   &torch.utils._appending_byte_serializerr   r	   r
   torch.utils._ordered_setr   	getLoggerr   r=   r   	dataclassr   r4   rE   r   r   r   r   <module>   s"    

