§
    ŠŠtjb  ã                   óP  — 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dS )é    N)Úabstractmethod)Údefaultdict)ÚCallable)Ú	dataclass)ÚAnyÚGenericÚTypeVar)Ú
_BackendIdÚ_DynamoCacheEntryÚDynamoCacheÚPrecompileCacheEntryÚTc                   ód   — 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dS )
Ú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                 ó   — 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.
        N© ©Úselfs    ú^/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/torch/_dynamo/precompile_context.pyÚafter_deserializationz*BackendCacheArtifact.after_deserialization1   s	   € ð 	ˆó    Úedit_fn.Nc                 ó0   —  || j         ¦  «        | _         dS )z4
        Edit the contents of the artifact.
        N©r   )r   r   s     r   Úedit_contentsz"BackendCacheArtifact.edit_contents9   s   € ð �w˜tœ|Ñ,Ô,ˆŒˆˆr   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   r   r   r   r   r   r   r   r   r   r      s„   € € € € € € ðð ð" 
€H€H�HØ€L€L�Làð qð ð ð ñ „^ðð- X¨c°3¨hÔ%7ð -¸Dð -ð -ð -ð -ð -ð -r   r   c                   ó   — e Zd Zdefd„ZdS )Ú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dS )ÚBypassDynamoCacheEntryN)r   r    r!   r   r   r   r*   r*   E   s   € € € € € Ø€Dr   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d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.

    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                 ój   — | j                              ¦   «          | j                             ¦   «          d S r(   )r-   Úclearr.   )Úclss    r   r0   zPrecompileContext.clearb   s2   € àÔ%×+Ò+Ñ-Ô-Ð-ØÔ!×'Ò'Ñ)Ô)Ð)Ð)Ð)r   Úartifactc                 ó´   — ddl m}  |¦   «         5  t          j        |¦  «        | j        t          |j        ¦  «        <   ddd¦  «         dS # 1 swxY w Y   dS )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   s®   € ð 	8Ð7Ð7Ð7Ð7Ð7àˆ[‰]Œ]ð 	ð 	ÝFJÄmØñGô GˆCÔ)­*°X´\Ñ*BÔ*BÑCð	ð 	ð 	ñ 	ô 	ð 	ð 	ð 	ð 	ð 	ð 	ð 	øøøð 	ð 	ð 	ð 	ð 	ð 	s   ‘/AÁAÁAÚcache_entryr   c                 ó   — || j         |<   d S r(   )r.   )r1   r9   r   s      r   Úrecord_dynamo_cache_entryz+PrecompileContext.record_dynamo_cache_entryy   s   € ð *5ˆÔ! #Ñ&Ð&Ð&r   r   .c                 óœ   — || j         vrt          d|› d�¦  «        ‚| j         t          |¦  «                 }|                     |¦  «         dS )z:
        Edit the content of an existing artifact
        zKey z not found in artifactsN)r-   ÚAssertionErrorr
   r   )r1   r   r   r2   s       r   Úedit_artifactzPrecompileContext.edit_artifact   sY   € ð
 �cÔ3Ð3Ð3Ý Ð!D¨Ð!DÐ!DÐ!DÑEÔEÐEØÔ0µ¸C±´ÔAˆØ×Ò˜wÑ'Ô'Ð'Ð'Ð'r   c                 óR   — | 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ÔGÐGr   Údynamo_entriesÚbackend_artifactsc                 ó8  — t          t          ¦  «        }|                      ¦   «         D ]9\  }}|                     ¦   «         }||d<   |d                              |¦  «         Œ:|                     ¦   «         D ]"}|d                              |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   )rB   rC   rI   r   r9   Úinfor2   s          r   Údump_debug_infoz!PrecompileContext.dump_debug_info�   s¦   € õ 3>½dÑ2CÔ2Cˆ
Ø .× 4Ò 4Ñ 6Ô 6ð 	.ð 	.ÑˆC�Ø×)Ò)Ñ+Ô+ˆDØˆD�‰KØ�xÔ ×'Ò'¨Ñ-Ô-Ð-Ð-à)×0Ò0Ñ2Ô2ð 	8ð 	8ˆHØ�zÔ"×)Ò)¨(¬,Ñ7Ô7Ð7Ð7àÐr   c                 ó’   — |                       ¦   «         \  }}|                     ¦   «         D ]\  }}t          j        ||¦  «         Œ|S r(   )Úcreate_cache_entriesrH   r   Úwrite)r1   Úprecompile_cache_entriesrI   r   Úentrys        r   Úsave_to_dynamo_cachez&PrecompileContext.save_to_dynamo_cache¥   sT   € à/2×/GÒ/GÑ/IÔ/IÑ,Ð  *Ø2×8Ò8Ñ:Ô:ð 	*ð 	*‰JˆC�ÝÔ˜e SÑ)Ô)Ð)Ð)ØÐr   c                 ól  ‡‡— | j         }| j        }t          |¦  «        }t                               ||¦  «        }t          j        ||dœ¦  «        Št          j         	                    dd„ ˆfd„d¬¦  «         i }| 
                    ¦   «         D ]¢\  }}	 t          j        ||¦  «        }|�|||<   Œ## t          $ rs}	t                               d|d	¬
¦  «         |	}
t          j        |t!          |
¦  «        dœ¦  «        Št          j         	                    dd„ ˆfd„¬¦  «         Y d}	~	Œ›d}	~	ww xY w||fS )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_exceptionrY   rZ   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`   ra   )r.   r-   Úlenr,   rM   rY   ÚdumpsÚtorchÚ_loggingÚtrace_structuredrH   r   Úfrom_cache_entryÚ	ExceptionÚloggerÚwarningr#   )r1   rB   rC   Únum_artifactsrI   rQ   r   r9   ÚresultÚerd   rh   r_   s              @@r   rO   z&PrecompileContext.create_cache_entries¬   s²  øø€ ð Ô2ˆØÔ9Ðå˜NÑ+Ô+ˆå&×6Ò6ØÐ-ñ
ô 
ˆ
õ ”Jà,Ø'ðð ñ
ô 
ˆ	õ 	Œ×'Ò'Øðð ð )Ð(Ð(Ð(Ø!ð 	(ñ 	
ô 	
ð 	
ð $&Ð à .× 4Ò 4Ñ 6Ô 6ð 	ð 	ÑˆC�ðÝ-Ô>ØÐ!2ñô �ð Ð%Ø4:Ð,¨SÑ1øøÝð ð ð Ý—’Ð@À#ÐPT�ÑUÔUÐUà�Ý”zà"Ý!$ U¡¤ðð ñô �õ ”×/Ò/Øð!ð !ð  ,˜|˜|˜|ð 0ñ ô ð ð ����øøøøð%øøøð& (¨Ð3Ð3s   ÂB2Â2
D/Â<A)D*Ä*D/)r   N)r   r    r!   r"   r-   Údictr
   r   r   r$   r.   r#   r   Úclassmethodr0   r8   r;   r   r>   rA   ÚstaticmethodrM   rS   Útupler   rO   r   r   r   r,   r,   I   s?  € € € € € € ðð ð$ NPÐ˜t JÐ0DÀSÔ0IÐ$IÔJÐOÐOÑOð ;=Ð˜4 Ð%6Ð 6Ô7Ð<Ð<Ñ<àð*ð *ð *ñ „[ð*ð ðà& sÔ+ðð 
ðð ð ñ „[ðð" ð5Ø+ð5Ø25ð5à	ð5ð 5ð 5ñ „[ð5ð
 ð( ð (¨h°s¸C°xÔ.@ð (ÀTð (ð (ð (ñ „[ð(ð ðH¨Cð HÐ4HÈÔ4MÐPTÑ4Tð Hð Hð Hñ „[ðHð ðØ˜SÐ"3Ð3Ô4ðà 
Ð,@ÀÔ,EÐ EÔFðð 
ˆc�3ˆhŒðð ð ñ „\ðð( ð T¨#¨s¨(¤^ð ð ð ñ „[ðð ð;4à	ˆt�CÐ-Ð-Ô.°°S¸#°X´Ð>Ô	?ð;4ð ;4ð ;4ñ „[ð;4ð ;4ð ;4r   r,   )r6   rY   ÚloggingÚabcr   Úcollectionsr   Úcollections.abcr   Údataclassesr   Útypingr   r   r	   rk   Útorch._dynamo.packager
   r   r   r   r   Ú	getLoggerr   rp   r   r&   ro   r*   r,   r   r   r   ú<module>r�      sÂ  ðØ €€€Ø €€€Ø €€€Ø Ð Ð Ð Ð Ð Ø #Ð #Ð #Ð #Ð #Ð #Ø $Ð $Ð $Ð $Ð $Ð $Ø !Ð !Ð !Ð !Ð !Ð !Ø (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (Ð (à €€€ðð ð ð ð ð ð ð ð ð ð ð ðð €GˆC�L„L€Ø	ˆÔ	˜8Ñ	$Ô	$€ð ð!-ð !-ð !-ð !-ð !-˜7 1œ:ñ !-ô !-ñ „ð!-ðHð ð ð ð Ð-¨cÔ2ñ ô ð ð
	ð 	ð 	ð 	ð 	˜Yñ 	ô 	ð 	ð_4ð _4ð _4ð _4ð _4ñ _4ô _4ð _4ð _4ð _4r   