
    WjM                     T   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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Optional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    e/home/longshao/multi-rider-rag/.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     wt|,,r   )__name__
__module____qualname____doc__str__annotations__r   r   r   r   r   r   r   r   r   r   r      s          " 
HHHLLLq    ^-Xc3h%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e                  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   r1   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_utilsr5   copydeepcopyr.   r   r   )r2   r3   r5   s      r   record_artifactz!PrecompileContext.record_artifactg   s     	877777[]] 	 	FJmG GC)*X\*B*BC	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   /AAAcache_entryr   c                     || j         |<   d S r)   )r/   )r2   r:   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          |                   }|                    |           dS )z:
        Edit the content of an existing artifact
        zKey z not found in artifactsN)r.   r   r   )r2   r   r   r3   s       r   edit_artifactzPrecompileContext.edit_artifact   sY    
 c33335XC5X5X5X3330CAw'''''r   c                 R    | j                             t          |          d          S )zK
        Return the backend cache artifact with the associated key
        N)r.   getr   )r2   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   r:   infor3   s          r   dump_debug_infoz!PrecompileContext.dump_debug_info   s     3>d2C2C
 . 4 4 6 6 	. 	.C))++DDKx ''----)0022 	8 	8Hz"))(,7777r   c                     |                                  \  }}|                                D ]\  }}t          j        ||           |S r)   )create_cache_entriesrH   r   write)r2   precompile_cache_entriesrI   r   entrys        r   save_to_dynamo_cachez&PrecompileContext.save_to_dynamo_cache   sT    /2/G/G/I/I, *288:: 	* 	*JCeS))))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	artifactsr3   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$   )r2   rB   rC   num_artifactsrI   rQ   r   r:   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   classmethodr1   r9   r<   r   r>   r	   rA   staticmethodrM   rS   tupler   rO   r   r   r   r-   r-   I   s?         $ NPtJ0DS0I$IJOOO ;=4%6 67<<<* * * [* &s+ 
   [" 5+5255	5 5 5 [5
 ( (hsCx.@ (T ( ( ( [( HC HH=QRU=V4W H H H [H S"334
,@,E EF 
c3h   \( T#s(^    [ ;4	tC--.S#X>	?;4 ;4 ;4 [;4 ;4 ;4r   r-   )r7   rY   loggingabcr   collectionsr   collections.abcr   dataclassesr   typingr   r   r	   r
   rk   torch._dynamo.packager   r   r   r   r   	getLoggerr    rp   r   r'   ro   r+   r-   r   r   r   <module>r      s            # # # # # # $ $ $ $ $ $ ! ! ! ! ! ! 2 2 2 2 2 2 2 2 2 2 2 2             GCLL		8	$	$ !- !- !- !- !-71: !- !- !-H    -c2   
	 	 	 	 	Y 	 	 	^4 ^4 ^4 ^4 ^4 ^4 ^4 ^4 ^4 ^4r   