
    i4%                     *   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#y)    N)abstractmethod)defaultdict)chain)AnyCallableGenericOptionalTypeVarUnion)override)_serialize_single_cacheCacheArtifactCacheArtifactFactoryCacheArtifactManagerCacheArtifactsResult	CacheInfo)AppendingByteSerializer)
OrderedSetTc                   N    e Zd ZdZedd       Zedefd       Zede	fd       Z
y)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/var/www/html/engine/venv/lib/python3.12/site-packages/torch/_dynamo/precompile_context.pypopulate_cachez&PrecompileCacheArtifact.populate_cache3   s    NOO    c                      y)NT r   s    r   precompile_compatiblez-PrecompileCacheArtifact.precompile_compatible7   s    r   c                      y)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      sS    ( P P t   q  r   r   c                   T    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y)EditablePrecompileCacheArtifactzk
    A PrecompileCacheArtifact whose content isn't encoded until we call PrecompileContext.serialize()
    artifact_typecontentkeyr   Nc                 T    t        j                  |      | _        || _        || _        y 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   sO     ,,t||,'55'
 ($;<<<r   edit_fn.c                 2     || j                         | _         y):
        Edit the content of an existing artifact
        N)r.   )r   r<   s     r   edit_contentsz-EditablePrecompileCacheArtifact.edit_contents[   s     t||,r   )r&   r'   r(   r)   strr   r4   r   r   r;   r   r?   r!   r   r   r,   r,   D   sV    +c +C +c +d +	4Q7 	-Xc3h%7 -D -r   r,   c                       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                 V    | j                   j                          t        |           y r1   )rC   clearsupercls	__class__s    r   rJ   zPrecompileContext.clear   s    ''--/r   r-   r/   r.   editablec                     |rt        |||      }nAt        j                  |||      }|| j                  v ry| j                  j	                  |       || j
                  |<   y)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   s`     6}gsSH+99-gVH 3...##H-/7'',r   c                 "   | j                   j                         D ]X  }t        |t              r|j	                         }| j
                  |j                  j                            j                  |       Z | j                   j                          y)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   sy     77>>@ 	QH($CD#//1$$X%7%7%<%<%>?FFxP	Q 	''--/r   r<   .c                     || j                   v sJ d| d       | j                   |   }t        |t              sJ d       |j                  |       y)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   si    
 c555 	
3%./	
5 2237($CD 	
&	
D 	w'r   c                 ~    | j                   j                  |d      }t        |t              r|j	                         }|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   s:    
 0044S$?f=>'')Fr   c                 ^    | j                          d| j                  vry t        |          S )Nprecompile_dynamo)rW   rD   rK   	serializerL   s    r   ra   zPrecompileContext.serialize   s.    ##%c&>&>>w ""r   	artifactsc                 &   t         j                          i }t               }t        | j	                          D ]F  }|j                         dk(  r|j                          n|||j                  <   |j                  |       H ddl	m
}m} | d   D ]  }t        |t              sJ |j                         }|j                  }i }	|D ]-  }
|
|v sJ d|
 d       ||
   }t        |t              sJ ||	|
<   / |j!                  ||	|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   s-   <<>[
y//12 	%H}}*,'')19 .NN8$	% 	B%&9: 	NLl,CDDD&<<>K"..HNPO 0..W(3%?V0WW.+C0!(,CDDD'/$	0
 k?L<L<LM	N r   c                     ddl m} ddlm} y )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=T1B.B 2<OZ.=
 	 -DE (sD4G/G)HI  '[K(   88 8 	8
 8 
8  86 	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 &  K / CL			8	$#mWQZ #L-gaj -<S
, S
r   