a
    h*                     @   s  d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlm	Z	 d dl
mZ d dlmZ d dlmZmZ d dlmZmZmZ d d	lmZ eeZejd
dG dd deZG dd dZejG dd dZedddddZeddddZe e!e"e f Z#G dd dZ$dS )    N)ABCabstractmethod)defaultdict)	Generator)contextmanager)chain)AnyOptional)AppendingByteSerializerBytesReaderBytesWriter)
OrderedSetT)frozenc                   @   s   e Zd ZU dZeed< ejddZe	ed< e
ed dddd	Ze
eed d
ddZe
ee	dddZeddddZedddZe
edddZdS )CacheArtifactzO
    Data for each cache artifact that will be serialized and deserialized
    keyF)reprcontentNwriterclsreturnc                 C   s   |  |j | |j d S N)	write_strr   write_bytesr   )r   r    r   C/var/www/auris/lib/python3.9/site-packages/torch/compiler/_cache.py	serialize   s    zCacheArtifact.serialize)artifact_typereaderr   c                 C   s   |  }| }t| ||S r   )read_str
read_bytesCacheArtifactFactorycreate)r   r   r   r   r   r   r   deserialize$   s    zCacheArtifact.deserialize)r   r   c                 C   s    t | tsJ dt|  | S )NzExpected bytes, got )
isinstancebytestype)r   r   r   r   encode*   s    zCacheArtifact.encoder   c                 C   s   d S r   r   selfr   r   r   populate_cache/   s    zCacheArtifact.populate_cachec                 C   s   dS )NFr   r)   r   r   r   precompile_compatible3   s    z#CacheArtifact.precompile_compatiblec                   C   s   t ddS )a  
        Returns the type of the artifact. Must be unique across all CacheArtifact classes.

        CacheArtifactFactory.register will add property method to CacheInfo based on this (def {type}_artifacts)
        that returns all artifacts for specific cache.
        z9CacheArtifact is an abstract class, please use a subclassN)RuntimeErrorr   r   r   r   r&   6   s    zCacheArtifact.type)__name__
__module____qualname____doc__str__annotations__dataclassesfieldr   r%   staticmethodr   r   r   r#   r   r'   r   r+   boolr,   r&   r   r   r   r   r      s   
r   c                   @   s   e Zd ZU dZi Zeeee f e	d< e
ee ee dddZe
eee dddZe
eeeed	d
dZe
eeeed	ddZdS )r!   zH
    Factory for creating CacheArtifact objects based on their type
    _artifact_types)artifact_clsr   c                    sR   |   |  | jvs&J d  d|| j < tt  dt fdd |S )NArtifact of type=z2 already registered in mega-cache artifact factoryZ
_artifactsc                    s
   | j   S r   	artifactsr)   artifact_type_keyr   r   <lambda>R       z/CacheArtifactFactory.register.<locals>.<lambda>)r&   r8   setattr	CacheInfoproperty)r   r9   r   r=   r   registerH   s    

zCacheArtifactFactory.register)r>   r   c                 C   s$   || j v sJ d| d| j | S )Nr:   z. not registered in mega-cache artifact factory)r8   )r   r>   r   r   r   _get_artifact_typeV   s
    
z'CacheArtifactFactory._get_artifact_type)r>   r   r   r   c                 C   s   |  |}|||S r   )rE   r   r>   r   r   r9   r   r   r   r"   ]   s    
zCacheArtifactFactory.createc                 C   s   |  |}||||S r   )rE   r'   rF   r   r   r   encode_createb   s    
z"CacheArtifactFactory.encode_createN)r.   r/   r0   r1   r8   dictr2   r&   r   r3   classmethodrD   rE   r%   r"   r   rG   r   r   r   r   r!   A   s   
r!   c                   @   s   e Zd ZU dZejdd dZeee	e f e
d< ee	e dddZee	e dd	d
Zee	e dddZee	e dddZee	e dddZeddddZddddZedddZdS )rB   zb
    Return value of serialization and deserialization for the purpose of
    instrumentation
    c                   C   s   t tS r   )r   listr   r   r   r   r?   r   r@   zCacheInfo.<lambda>)default_factoryr<   r(   c                 C   s   d S r   r   r)   r   r   r   inductor_artifactsv   s    zCacheInfo.inductor_artifactsc                 C   s   d S r   r   r)   r   r   r   autotune_artifactsz   s    zCacheInfo.autotune_artifactsc                 C   s   d S r   r   r)   r   r   r   aot_autograd_artifacts~   s    z CacheInfo.aot_autograd_artifactsc                 C   s   d S r   r   r)   r   r   r   pgo_artifacts   s    zCacheInfo.pgo_artifactsc                 C   s   d S r   r   r)   r   r   r   !precompile_aot_autograd_artifacts   s    z+CacheInfo.precompile_aot_autograd_artifactsN)artifactr   c                 C   s   | j |  |j d S r   )r<   r&   appendr   )r*   rQ   r   r   r   add   s    zCacheInfo.addc                 C   s   | j   d S r   )r<   clearr)   r   r   r   rT      s    zCacheInfo.clearc                 C   s   | j  S r   r;   r)   r   r   r   empty   s    zCacheInfo.empty)r.   r/   r0   r1   r4   r5   r<   r   r2   rJ   r3   rC   rL   rM   rN   rO   rP   r   rS   rT   r7   rU   r   r   r   r   rB   j   s"   
rB   ztuple[str, list[CacheArtifact]]r   c                 C   s>   |  |d  | t|d  |d D ]}t| | q(d S )Nr      )r   Zwrite_uint64lenr   r   )r   r   rQ   r   r   r   _serialize_single_cache   s    rX   )r   r   c                 C   s<   g }|   }|  }t|D ]}|t||  q||fS r   )r   Zread_uint64rangerR   r   r#   )r   r<   r>   Znum_artifacts_r   r   r   _deserialize_single_cache   s    r[   c                   @   s  e Zd ZU dZ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d	d
Zeeed dddZeeeeddddZeedddZeeeeef  dddZeeee dddZ eeedddZ!edd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_infoNr(   c                 C   s,   | j   | j  | j  | j  d S r   )r]   rT   r^   r`   ra   r   r   r   r   rT      s    


zCacheArtifactManager.clear)NNNc                 c   s   | j }| j}| j}| j}tt| _ t | _ttd| _| j	 | _z"d V  W || _ || _|| _|| _n|| _ || _|| _|| _0 d S )Nr_   )
r]   r^   r`   ra   r   rJ   r   r
   rX   	__class__)r   Zoriginal_new_cache_artifactsZoriginal_seen_artifactsZoriginal_serializerZoriginal_cache_infor   r   r   with_fresh_cache   s$    
z%CacheArtifactManager.with_fresh_cache)r   r   r   r   c                 C   sL   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!   rG   r^   logdebugr2   r]   rR   rS   )r   r   r   r   rQ   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   )rW   r]   rb   r   r   r   need_serialize   s    z#CacheArtifactManager.need_serializec                 C   s   t | j  D ]}td| | j| q| j r:dS z<t	| j}| j
| j  | j
 }| j  ||fW S  ty   tjddd Y n0 dS )z?
        Converts the "mega" list into portable format
        z
saving: %sNz Failed to pickle cache artifactsTexc_info)r   r]   valuesre   rf   ra   rS   rU   copydeepcopyr`   extenditemsto_bytesrT   	Exceptionwarning)r   rQ   infoZartifact_bytesr   r   r   r      s    



zCacheArtifactManager.serialize)serialized_artifactsr   c                 C   sF   zt   ttj| td}W n" ty@   tjddd Y dS 0 |S )zG
        Converts the portable format back into CacheArtifacts
        )Zdeserialize_fnz#Failed to un-pickle cache artifactsTri   N)	r\   "_ensure_cache_artifacts_registeredrH   r
   Zto_listr[   rq   re   rr   )rt   r<   r   r   r   r#     s    z CacheArtifactManager.deserialize)r<   r   c                 C   s:   t  }t|   D ]"}td| || |  q|S )Nzwriting: %s)rB   r   rk   re   rf   rS   r+   )r<   rs   rQ   r   r   r   populate_caches+  s    

z$CacheArtifactManager.populate_cachesc                 C   s4   ddl m} ddlm} ddlm} ddlm} dS )zWhen deserializing caches in fresh process, we need to ensure that all
        cache artifacts are registered in the cache registry. This is done by
        simply importing all the cache artifacts already wrapped with register call.
        r   )PGOCacheArtifact)AOTAutogradCacheArtifact)InductorCacheArtifact)AutotuneCacheArtifactN)Ztorch._dynamo.pgorw   Z-torch._functorch._aot_autograd.autograd_cacherx   Ztorch._inductor.codecachery   Z&torch._inductor.runtime.autotune_cacherz   )r   rw   rx   ry   rz   r   r   r   ru   5  s    z7CacheArtifactManager._ensure_cache_artifacts_registered)#r.   r/   r0   r1   r   rJ   r]   CacheArtifactsResultr3   r   r^   r   r
   rX   r`   tupler2   rB   ra   rI   rT   r   r   rd   r   rg   r7   rh   r	   r%   r   r6   r#   rv   ru   r   r   r   r   r\      s:   
	r\   )%rl   r4   loggingabcr   r   collectionsr   collections.abcr   
contextlibr   	itertoolsr   typingr   r	   Z&torch.utils._appending_byte_serializerr
   r   r   Ztorch.utils._ordered_setr   	getLoggerr.   re   Z	dataclassr   r!   rB   rX   r[   rH   r2   rJ   r{   r\   r   r   r   r   <module>   s0   

*)*
