
    ȅiM                     &   S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJrJrJrJr  S SKrS SKJrJrJrJr   \" S5      r\R.                  " \5      r\
 " S	 S
\\   5      5       r " S S\\   5      r " S S\5      r " S S5      rg)    N)abstractmethod)defaultdict)Callable)	dataclass)AnyGenericOptionalTypeVar)
_BackendId_DynamoCacheEntryDynamoCachePrecompileCacheEntryTc                   b    \ rS rSr% Sr\\S'   \\S'   \S\	4S j5       r
S\S\4   SS	4S
 jrSrg	)BackendCacheArtifact   a  
Represents a single serializable backend artifact from a dynamo backend.
Each BackendCacheArtifact has a key associated with it along with some
serializable content.

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
keycontentreturnc                     g)z
Code to be run after reading raw byte contents from disk.
Generally converts self.content from raw bytes back into its original form.
N selfs    Z/home/james-whalen/.local/lib/python3.13/site-packages/torch/_dynamo/precompile_context.pyafter_deserialization*BackendCacheArtifact.after_deserialization1   s     	    edit_fn.Nc                 2    U" U R                   5      U l         g)z$
Edit the contents of the artifact.
Nr   )r   r   s     r   edit_contents"BackendCacheArtifact.edit_contents9   s     t||,r   r    )__name__
__module____qualname____firstlineno____doc__str__annotations__r   r   r   r   r   r!   __static_attributes__r   r   r   r   r      sH    " 
HLq  -Xc3h%7 -D -r   r   c                   "    \ rS rSrS\4S jrSrg)EagerCacheArtifact@   r   c                     U R                   $ Nr    r   s    r   r   (EagerCacheArtifact.after_deserializationA   s    ||r   r   N)r#   r$   r%   r&   r   r   r*   r   r   r   r,   r,   @   s    s r   r,   c                       \ rS rSrSrg)BypassDynamoCacheEntryE   r   N)r#   r$   r%   r&   r*   r   r   r   r2   r2   E   s    r   r2   c            	          \ rS rSr% Sr0 r\\\\	   4   \
S'   0 r\\\4   \
S'   \SS j5       r\S\\	   SS4S	 j5       r\S
\S\SS4S j5       r\S\S\S\	4   SS4S j5       r\S\S\\\	      4S j5       r\S\\\4   S\\\\	   4   S\\\	4   4S j5       r\S\\\	4   4S j5       r\S\\\\4   \\\	4   4   4S j5       rSrg)PrecompileContextI   a  
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.

PrecompileContext has two main portions: dynamo_cache_entries and backend_cache_artifacts.
When saving, PrecompileContext.serialize() will serialize all dynamo cache entries along with any PrecompileCacheArtifacts that
are needed to save those dynamo cache entries.

The following artifact types are supported by PrecompileContext:
 - BundledAOTAutogradCacheArtifact

_backend_artifacts_by_key_dynamo_cache_entriesr   Nc                 l    U R                   R                  5         U R                  R                  5         g r/   )r7   clearr8   )clss    r   r:   PrecompileContext.clearb   s&    %%++-!!'')r   artifactc                     SSK Jn  U" 5          [        R                  " U5      U R                  [        UR                  5      '   SSS5        g! , (       d  f       g= f)zA
Records a backend artifact to be used with dynamo cache entries
r   )no_dispatchN)torch.utils._mode_utilsr?   copydeepcopyr7   r   r   )r;   r=   r?   s      r   record_artifact!PrecompileContext.record_artifactg   s<     	8]FJmmGC))*X\\*BC ]]s   7A
Acache_entryr   c                      XR                   U'   g r/   )r8   )r;   rE   r   s      r   record_dynamo_cache_entry+PrecompileContext.record_dynamo_cache_entryy   s     *5!!#&r   r   .c                     XR                   ;   d   SU S35       eU R                   [        U5         nUR                  U5        g)z*
Edit the content of an existing artifact
zKey z not found in artifactsN)r7   r   r!   )r;   r   r   r=   s       r   edit_artifactPrecompileContext.edit_artifact   sG    
 333XtC5@W5XX300CAw'r   c                 L    U R                   R                  [        U5      S5      $ )z;
Return the backend cache artifact with the associated key
N)r7   getr   )r;   r   s     r   serialize_artifact_by_key+PrecompileContext.serialize_artifact_by_key   s!    
 ,,00C$GGr   dynamo_entriesbackend_artifactsc                    [        [        5      nU R                  5        H-  u  p4UR                  5       nX5S'   US   R	                  U5        M/     UR                  5        H!  nUS   R	                  UR                  5        M#     U$ )z
Return a JSON serializable debug dump of all entries in the precompile context
Called in serialize before serialization, and in populate_caches after deserialization
r   dynamobackends)r   listitems
debug_infoappendvaluesr   )rP   rQ   rW   r   rE   infor=   s          r   dump_debug_info!PrecompileContext.dump_debug_info   s     3>d2C
 . 4 4 6C))+DKx ''- !7
 *002Hz"))(,,7 3 r   c                     U R                  5       u  pUR                  5        H  u  p4[        R                  " XC5        M     U$ r/   )create_cache_entriesrV   r   write)r;   precompile_cache_entriesrW   r   entrys        r   save_to_dynamo_cache&PrecompileContext.save_to_dynamo_cache   s=    /2/G/G/I, 288:JCe) ;r   c                 X  ^^ U R                   nU R                  n[        U5      n[        R	                  X5      n[
        R                  " UUS.5      m[        R                  R                  SS U4S jSS9  0 nUR                  5        H%  u  pg [        R                  " Xr5      nUb  XU'   M%  M'     XT4$ ! [         ah  n	[        R                  SUS	S
9  U	n
[
        R                  " U[!        U
5      S.5      m[        R                  R                  SS U4S jS9   Sn	A	M  Sn	A	ff = f)zt
Grabs all the cache entries in the precompile context and
stitches them together into full PrecompileCacheEntries.
)num_entries	artifactsr=   c                      SSS.$ )Ndynamo_cache_entriesjsonnameencodingr   r   r   r   <lambda>8PrecompileContext.create_cache_entries.<locals>.<lambda>   s    ."!r   c                     > T $ r/   r   )	debug_strs   r   rm   rn      s    yr   F)metadata_fn
payload_fnexpect_trace_idNzFailed to create cache entry %sT)exc_info)r   errorc                      SSS.$ )Ndynamo_cache_exceptionri   rj   r   r   r   r   rm   rn      s     8$*)r   c                     > T $ r/   r   )datas   r   rm   rn      s    tr   )rq   rr   )r8   r7   lenr5   r[   ri   dumpstorch_loggingtrace_structuredrV   r   from_cache_entry	Exceptionloggerwarningr(   )r;   rP   rQ   num_artifactsrW   r`   r   rE   resulteru   ry   rp   s              @@r   r^   &PrecompileContext.create_cache_entries   sF    2299N+&66

 JJ,'
	 	'' )! 	( 	
 $&  . 4 4 6C->> %4:S1 & !74 (33'  @#PTUzz"!$U //!  , 0  %s   B77
D)AD$$D)r   )r   N)r#   r$   r%   r&   r'   r7   dictr   r   r   r)   r8   r(   r   classmethodr:   rC   rG   r   rJ   r	   rN   staticmethodr[   rb   tupler   r^   r*   r   r   r   r5   r5   I   s   $ NPtJ0DS0I$IJO ;=4%6 67<* * &s+ 
 " 5+5255	5 5
 ( (hsCx.@ (T ( ( HC HH=QRU=V4W H H S"334
,@,E EF 
c3h ( T#s(^   ;4	tC--.S#X>	?;4 ;4r   r5   )rA   ri   loggingabcr   collectionsr   collections.abcr   dataclassesr   typingr   r   r	   r
   r|   torch._dynamo.packager   r   r   r   r   	getLoggerr#   r   r   r,   r   r2   r5   r   r   r   <module>r      s        # $ ! 2 2   CL			8	$ !-71: !- !-H-c2 
	Y 	^4 ^4r   