
    %Vji4%                     L   d dl Z d dlZd dlZd dlmZ d dl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mZmZmZmZmZ d dlmZ d d	lmZ 	  ed
          Z ej        e          Z  G d deee                   Z! G d dee                   Z" G d de          Z#dS )    N)abstractmethod)defaultdict)chain)AnyCallableGenericOptionalTypeVarUnion)override)_serialize_single_cacheCacheArtifactCacheArtifactFactoryCacheArtifactManagerCacheArtifactsResult	CacheInfo)AppendingByteSerializer)
OrderedSetTc                   b    e Zd ZdZedd            Zedefd            Zede	f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                      t          d          )Nz1Precompile cache artifacts do not populate caches)RuntimeErrorselfs    Z/root/voice-cloning/.venv/lib/python3.11/site-packages/torch/_dynamo/precompile_context.pypopulate_cachez&PrecompileCacheArtifact.populate_cache3   s    NOOO    c                     dS )NT r   s    r   precompile_compatiblez-PrecompileCacheArtifact.precompile_compatible7   s    tr   c                     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   s    r   after_deserializationz-PrecompileCacheArtifact.after_deserialization;   s	     	r   r   N)__name__
__module____qualname____doc__r   r   boolr"   r   r   r$   r!   r   r   r   r      s         ( P P P XP t    X q    ^  r   r   c                   b    e Zd ZdZdedededdfdZdee         fdZ	d	e
d
ef         ddfdZdS )EditablePrecompileCacheArtifactzk
    A PrecompileCacheArtifact whose content isn't encoded until we call PrecompileContext.serialize()
    artifact_typecontentkeyr   Nc                 T    t          j        |          | _        || _        || _        d S N)copydeepcopyr.   r/   r-   )r   r-   r.   r/   s       r   __init__z(EditablePrecompileCacheArtifact.__init__I   s*     }W--*r   c                     t          j        | j                  }t          j        | j        | j        |          }t          |t                    sJ |S )z,
        Actually encode the object
        )	pickledumpsr.   r   encode_creater-   r/   
isinstancer   )r   r.   artifacts      r   real_encodez+EditablePrecompileCacheArtifact.real_encodeP   sQ     ,t|,,'5'
 
 ($;<<<<<r   edit_fn.c                 0     || j                   | _         dS ):
        Edit the content of an existing artifact
        N)r.   )r   r<   s     r   edit_contentsz-EditablePrecompileCacheArtifact.edit_contents[   s     wt|,,r   )r&   r'   r(   r)   strr   r4   r   r   r;   r   r?   r!   r   r   r,   r,   D   s         +c +C +c +d + + + +	4Q7 	 	 	 	-Xc3h%7 -D - - - - - -r   r,   c                   N    e Zd ZU dZi Zeeeee	         e
f         f         ed<    ee          Zeed<    e            Zee
         ed<    ee          Zeeeee
         f                  ed<    e            Zeed<   ed fd
            Zee	 ddededededd	f
d                        Zedd            Zedededef         dd	fd            Z edede!e
         fd            Z"ede!ee#ef                  f fd            Z$e%dedefd            Z&edd            Z' x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
     - DynamoCodeStateArtifact
     - AutotuneCacheArtifact (regular autotune results, same as Megacache)
    _new_cache_artifacts_by_key_new_cache_artifacts_seen_artifacts)serialize_fn_serializer_cache_infor   Nc                 z    | j                                          t                                                       d S r1   )rC   clearsupercls	__class__s    r   rJ   zPrecompileContext.clear   s-    '--///r   Fr-   r/   r.   editablec                     |rt          |||          }n;t          j        |||          }|| 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,   r   r8   rE   addrC   )rM   r-   r/   r.   rO   r:   s         r   record_artifactz!PrecompileContext.record_artifact   sr      	.6}gsSSHH+9-gVVH 3...##H---/7',,,r   c                 0   | j                                         D ]b}t          |t                    r|                                }| j        |j                                                                     |           c| 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)
rC   valuesr9   r,   r;   rD   rN   typeappendrJ   )rM   r:   s     r   _save_artifacts_by_typez)PrecompileContext._save_artifacts_by_type   s     7>>@@ 	Q 	QH($CDD 2#//11$X%7%<%<%>%>?FFxPPPP'--/////r   r<   .c                     || j         v sJ d| d            | j         |         }t          |t                    s
J d            |                    |           dS )r>   zKey  not found in artifactszArtifact is not editableN)rC   r9   r,   r?   )rM   r/   r<   r:   s       r   edit_artifactzPrecompileContext.edit_artifact   s    
 c5555/3/// 655 237($CDD 	
 	
&	
 	
D 	w'''''r   c                     | j                             |d          }t          |t                    r|                                }|S )zP
        Serialize all artifacts with the given key returned in a list.
        N)rC   getr9   r,   r;   )rM   r/   results      r   serialize_artifact_by_keyz+PrecompileContext.serialize_artifact_by_key   sE    
 044S$??f=>> 	*''))Fr   c                     |                                   d| j        vrd S t                                                      S )Nprecompile_dynamo)rW   rD   rK   	serializerL   s    r   ra   zPrecompileContext.serialize   s>    ##%%%c&>>>4ww  """r   	artifactsc                 h   t                                            i }t                      }t          |                                  D ]N}|                                dk    r|                                 n
|||j        <   |                    |           Oddl	m
}m} | d         D ]}t          |t                    sJ |                                }|j        }i }	|D ]8}
|
|v sJ d|
 d            ||
         }t          |t                    sJ ||	|
<   9|                    ||	|j                   |S )Nautotuner   )
_BackendIdDynamoCacher`   zBackend rY   )rB   "_ensure_cache_artifacts_registeredr   r   rT   rU   r   r/   rQ   torch._dynamo.packagere   rf   r9   r   r$   backend_idswrite)rb   artifacts_by_key
cache_infor:   re   rf   dynamo_entrycache_entrybackendsbackend_contentid_s              r   populate_cachesz!PrecompileContext.populate_caches   sx   <<>>>[[
y//112 	% 	%H}}*,,''))))19 .NN8$$$$AAAAAAAA%&9: 	N 	NLl,CDDDDD&<<>>K".HNPO 0 0....0W30W0W0W...+C0!(,CDDDDD'/$$k?L<LMMMMr   c                     ddl m} ddlm} d S )Nr   )_DynamoCacheArtifact)BundledAOTAutogradCacheArtifact)rh   rt   -torch._functorch._aot_autograd.autograd_cacheru   )rM   rt   ru   s      r   rg   z4PrecompileContext._ensure_cache_artifacts_registered   s<    >>>>>>	
 	
 	
 	
 	
 	
 	
 	
r   r%   )F))r&   r'   r(   r)   rC   dictr@   r   r,   objectr   __annotations__r   listrD   r   r   rE   r   r   rG   tupler   rH   classmethodrJ   r   r   r*   rR   rW   r   rZ   r	   r^   bytesra   staticmethodrr   rg   __classcell__)rN   s   @r   rB   rB   b   s        
 
& 	  U26:MIJJ"    2=T1B1B.BBB 2<OZ.===
 	 -DEEE (sD4G/G)HI    'Y[[K(((     [  8 88 8 	8
 8 
8 8 8 [ X86 	0 	0 	0 [	0 ( (hsCx.@ (T ( ( ( [( C H]4K    [ #(5	)9#:; # # # # # [# #7 I    \< 
 
 
 [
 
 
 
 
r   rB   )$r2   loggingr6   abcr   collectionsr   	itertoolsr   typingr   r   r   r	   r
   r   typing_extensionsr   torch.compiler._cacher   r   r   r   r   r   &torch.utils._appending_byte_serializerr   torch.utils._ordered_setr   r   	getLoggerr&   loggerr   r,   rB   r!   r   r   <module>r      s            # # # # # #       C C C C C C C C C C C C C C C C & & & & & &                K J J J J J / / / / / / GCLL		8	$	$# # # # #mWQZ # # #L- - - - -gaj - - -<S
 S
 S
 S
 S
, S
 S
 S
 S
 S
r   