ó
    Eñ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    Ú]/home/mande/repo/quber/.venv/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ó ó ðð- X¨c°3¨hÑ%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)ÚBypassDynamoCacheEntryéE   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)ÚPrecompileContextéI   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á�]ÜFJÇmÂmØó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ÐX°t¸C¸5Ð@WÐ5XÓ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>¼dÓ2Cˆ
Ø .× 4Ñ 4Ö 6ÑˆCØ×)Ñ)Ó+ˆDØ�‰KØ�xÑ ×'Ñ'¨Ö-ñ !7ð
 *×0Ñ0Ö2ˆHØ�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Ö:‰JˆCÜ×Ò˜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  X…U'   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Øó
ˆ
ô —J’Jà,Ø'ñó
ˆ	ô 	�‰×'Ñ'Øñô )Ø!ð 	(ñ 	
ð $&Ð à .× 4Ñ 4Ö 6ÑˆCðÜ-×>Ò>Øó�ð Ñ%Ø4:¨SÓ1ñ &ñ !7ð4 (Ð3Ð3øô' ó Ü—‘Ð@À#ÐPT�ÑUà�Ü—z’zà"Ü!$ 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Ð˜t JÐ0DÀSÑ0IÐ$IÑJÓOð ;=Ð˜4 Ð%6Ð 6Ñ7Ó<àó*ó ð*ð ðà& sÑ+ðð 
óó ðð" ð5Ø+ð5Ø25ð5à	ó5ó ð5ð
 ð( ð (¨h°s¸C°xÑ.@ð (ÀTó (ó ð(ð ðH¨Cð H°HÐ=QÐRUÑ=VÑ4Wó Hó ðHð ðØ˜SÐ"3Ð3Ñ4ðà 
Ð,@ÀÑ,EÐ EÑFðð 
ˆc�3ˆh‰óó ðð( ð T¨#¨s¨(¡^ó ó ðð ð;4à	ˆt�CÐ-Ð-Ñ.°°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ã ÷ó ðñ ˆCƒL€Ø	×	Ò	˜8Ó	$€ð ô!-˜7 1™:ó !-ó ð!-ôHÐ-¨cÑ2ô ô
	˜Yô 	÷^4ò ^4r   