
    iM                     &   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y)    N)abstractmethod)defaultdict)Callable)	dataclass)AnyGenericOptional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    Z/var/www/html/engine/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  -Xc3h%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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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   r1   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_utilsr5   copydeepcopyr.   r   r   )r2   r3   r5   s      r   record_artifactz!PrecompileContext.record_artifactg   sG     	8] 	FJmmGC))*X\\*BC	 	 	s   6AAcache_entryr   c                 "    || j                   |<   y 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        |         }|j                  |       y)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   sI    
 c333XtC5@W5X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   )r2   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   )rB   rC   rI   r   r:   infor3   s          r   dump_debug_infoz!PrecompileContext.dump_debug_info   s     3>d2C
 . 4 4 6 	.C))+DDKx ''-	.
 *002 	8Hz"))(,,7	8 r   c                     | j                         \  }}|j                         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   sH    /2/G/G/I, *288: 	*JCeS)	*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	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   sT    2299N+&66-

 JJ,'
	 	'' )! 	( 	
 $&  . 4 4 6 	C->>!2 %4:,S1	4 (33'  @#PTUzz"!$U //!  , 0  %s   B77	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 ;=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   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   CL			8	$ !-71: !- !-H-c2 
	Y 	^4 ^4r   