Ë
    L²j@  ã                   ó"  — 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
 d dlmZmZmZ d dlZd dlmZmZmZmZ 	  ed«      Z ej,                  e«      Ze
 G d	„ d
ee   «      «       Z G d„ dee   «      Z G d„ de«      Z G d„ d«      Zy)é    N)Úabstractmethod)Údefaultdict)ÚCallable)Ú	dataclass)ÚAnyÚGenericÚTypeVar)Ú
_BackendIdÚ_DynamoCacheEntryÚDynamoCacheÚPrecompileCacheEntryÚTc                   óV   — e Zd ZU dZeed<   eed<   edefd„«       Z	de
def   ddfd	„Zy)
ÚBackendCacheArtifacta@  
    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                  ó   — 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.
        N© ©Úselfs    ú_/var/www/html/ia-enhanced/venv/lib/python3.12/site-packages/torch/_dynamo/precompile_context.pyÚafter_deserializationz*BackendCacheArtifact.after_deserialization1   s   € ð 	ó    Úedit_fn.Nc                 ó2   —  || j                   «      | _         y)z4
        Edit the contents of the artifact.
        N©r   )r   r   s     r   Úedit_contentsz"BackendCacheArtifact.edit_contents9   s   € ñ ˜tŸ|™|Ó,ˆ�r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   r   r   r   r   r   r   r   r   r   r      sH   … ñð" 
ƒHØƒLàð qò ó ðð- X¨c°3¨hÑ%7ð -¸Dô -r   r   c                   ó   — e Zd Zdefd„Zy)ÚEagerCacheArtifactr   c                 ó   — | j                   S ©Nr   r   s    r   r   z(EagerCacheArtifact.after_deserializationA   s   € Ø�|‰|Ðr   N)r   r    r!   r   r   r   r   r   r&   r&   @   s   „ ð sô r   r&   c                   ó   — e Zd Zy)ÚBypassDynamoCacheEntryN)r   r    r!   r   r   r   r*   r*   E   s   „ Ør   r*   c            	       ó|  — e Zd ZU dZi Zeeee   f   e	d<   i Z
eeef   e	d<   edd„«       Zedee   ddfd„«       Zed	ed
eddfd„«       Zed
ededef   ddfd„«       Zed
edee   dz  fd„«       Zedeeef   deeee   f   deeef   fd„«       Zedeeef   fd„«       Zedeeeef   eeef   f   fd„«       Zy)Ú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.

    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   — | j                   j                  «        | j                  j                  «        y r(   )r-   Úclearr.   )Úclss    r   r0   zPrecompileContext.clearb   s&   € à×%Ñ%×+Ñ+Ô-Ø×!Ñ!×'Ñ'Õ)r   Úartifactc                 ó²   — ddl m}  |«       5  t        j                  |«      | j                  t        |j                  «      <   ddd«       y# 1 sw Y   yxY w)zQ
        Records a backend artifact to be used with dynamo cache entries
        r   )Úno_dispatchN)Útorch.utils._mode_utilsr4   ÚcopyÚdeepcopyr-   r
   r   )r1   r2   r4   s      r   Úrecord_artifactz!PrecompileContext.record_artifactg   sG   € õ 	8á‹]ñ 	ÜFJÇmÁmØóGˆC×)Ñ)¬*°X·\±\Ó*BÑC÷	÷ 	ñ 	ús   Ž6AÁAÚcache_entryr   c                 ó"   — || j                   |<   y r(   )r.   )r1   r9   r   s      r   Úrecord_dynamo_cache_entryz+PrecompileContext.record_dynamo_cache_entryy   s   € ð *5ˆ×!Ñ! #Ò&r   r   .c                 óˆ   — || j                   v sJ d|› d�«       ‚| j                   t        |«         }|j                  |«       y)z:
        Edit the content of an existing artifact
        zKey z not found in artifactsN)r-   r
   r   )r1   r   r   r2   s       r   Úedit_artifactzPrecompileContext.edit_artifact   sI   € ð
 �c×3Ñ3Ñ3ÐX°t¸C¸5Ð@WÐ5XÓXÐ3Ø×0Ñ0´¸C³ÑAˆØ×Ñ˜wÕ'r   c                 óL   — | j                   j                  t        |«      d«      S )zK
        Return the backend cache artifact with the associated key
        N)r-   Úgetr
   )r1   r   s     r   Úserialize_artifact_by_keyz+PrecompileContext.serialize_artifact_by_keyˆ   s!   € ð
 ×,Ñ,×0Ñ0´¸C³À$ÓGÐGr   Údynamo_entriesÚbackend_artifactsc                 ó  — t        t        «      }| j                  «       D ].  \  }}|j                  «       }||d<   |d   j	                  |«       Œ0 |j                  «       D ]   }|d   j	                  |j                  «       Œ" |S )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   )rA   rB   rH   r   r9   Úinfor2   s          r   Údump_debug_infoz!PrecompileContext.dump_debug_info�   sŽ   € ô 3>¼dÓ2Cˆ
Ø .× 4Ñ 4Ó 6ò 	.ÑˆC�Ø×)Ñ)Ó+ˆDØˆD�‰KØ�xÑ ×'Ñ'¨Õ-ð	.ð
 *×0Ñ0Ó2ò 	8ˆHØ�zÑ"×)Ñ)¨(¯,©,Õ7ð	8ð Ðr   c                 óˆ   — | j                  «       \  }}|j                  «       D ]  \  }}t        j                  ||«       Œ |S r(   )Úcreate_cache_entriesrG   r   Úwrite)r1   Úprecompile_cache_entriesrH   r   Úentrys        r   Úsave_to_dynamo_cachez&PrecompileContext.save_to_dynamo_cache¤   sH   € à/2×/GÑ/GÓ/IÑ,Ð  *Ø2×8Ñ8Ó:ò 	*‰JˆC�Ü×Ñ˜e SÕ)ð	*àÐr   c                 ó\  ‡‡— | j                   }| j                  }t        |«      }t        j	                  ||«      }t        j                  ||dœ«      Št        j                  j                  dd„ ˆfd„d¬«       i }|j                  «       D ]#  \  }}	 t        j                  ||«      }|�|||<   Œ% ||fS # t        $ rk}	t        j                  d|d	¬
«       |	}
t        j                  |t!        |
«      dœ«      Št        j                  j                  dd„ ˆfd„¬«       Y d}	~	Œ™d}	~	ww xY w)zŒ
        Grabs all the cache entries in the precompile context and
        stitches them together into full PrecompileCacheEntries.
        )Únum_entriesÚ	artifactsr2   c                  ó   — dddœS )NÚdynamo_cache_entriesÚjson©ÚnameÚencodingr   r   r   r   ú<lambda>z8PrecompileContext.create_cache_entries.<locals>.<lambda>Ã   s   € Ø.Ø"ñ!€ r   c                  ó   •— ‰ S r(   r   )Ú	debug_strs   €r   r\   z8PrecompileContext.create_cache_entries.<locals>.<lambda>Ç   s   ø€ ˜y€ r   F)Úmetadata_fnÚ
payload_fnÚexpect_trace_idNzFailed to create cache entry %sT)Úexc_info)r   Úerrorc                  ó   — dddœS )NÚdynamo_cache_exceptionrX   rY   r   r   r   r   r\   z8PrecompileContext.create_cache_entries.<locals>.<lambda>à   s   € Ø 8Ø$*ñ)€ r   c                  ó   •— ‰ S r(   r   )Údatas   €r   r\   z8PrecompileContext.create_cache_entries.<locals>.<lambda>ä   s   ø€  t€ r   )r_   r`   )r.   r-   Úlenr,   rL   rX   ÚdumpsÚtorchÚ_loggingÚtrace_structuredrG   r   Úfrom_cache_entryÚ	ExceptionÚloggerÚwarningr#   )r1   rA   rB   Únum_artifactsrH   rP   r   r9   ÚresultÚerc   rg   r^   s              @@r   rN   z&PrecompileContext.create_cache_entries«   sT  ù€ ð ×2Ñ2ˆØ×9Ñ9Ðä˜NÓ+ˆä&×6Ñ6ØÐ-ó
ˆ
ô —J‘Jà,Ø'ñó
ˆ	ô 	�‰×'Ñ'Øñó )Ø!ð 	(ô 	
ð $&Ð à .× 4Ñ 4Ó 6ò 	ÑˆC�ðÜ-×>Ñ>ØÐ!2ó�ð Ð%Ø4:Ð,¨SÑ1øð	ð4 (¨Ð3Ð3øô' ò Ü—‘Ð@À#ÐPT�ÔUà�Ü—z‘zà"Ü!$ U£ñó�ô —‘×/Ñ/Øñ!ó  ,ð 0ô ô ûð%ús   ÂB7Â7	D+Ã A!D&Ä&D+)r   N)r   r    r!   r"   r-   Údictr
   r   r   r$   r.   r#   r   Úclassmethodr0   r8   r;   r   r=   r@   ÚstaticmethodrL   rR   Útupler   rN   r   r   r   r,   r,   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Ð4HÈÑ4MÐPTÑ4Tò Hó ðHð ðØ˜SÐ"3Ð3Ñ4ðà 
Ð,@ÀÑ,EÐ EÑFðð 
ˆc�3ˆh‰òó ðð( ð T¨#¨s¨(¡^ò ó ðð ð;4à	ˆt�CÐ-Ð-Ñ.°°S¸#°X±Ð>Ñ	?ò;4ó ñ;4r   r,   )r6   rX   ÚloggingÚabcr   Úcollectionsr   Úcollections.abcr   Údataclassesr   Útypingr   r   r	   rj   Útorch._dynamo.packager
   r   r   r   r   Ú	getLoggerr   ro   r   r&   rn   r*   r,   r   r   r   ú<module>r€      s™   ðÛ Û Û Ý Ý #Ý $Ý !ß (Ñ (ã ÷ó ðñ ˆCƒL€Ø	ˆ×	Ñ	˜8Ó	$€ð ô!-˜7 1™:ó !-ó ð!-ôHÐ-¨cÑ2ô ô
	˜Yô 	÷^4ò ^4r   