
    Vh                     <   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
mZ d dlmZ d dlmZmZmZ d dlmZ  ej(                  e      Z G d d	e      Z ej0                  d
       G d d             Zej0                   G d d             Z G d d      Zy)    N)Enum)OptionalUnion)
JsonDataTyRemoteCacheJsonSerde)	cache_dir)AppendingByteSerializerBytesReaderBytesWriter)
OrderedSetc                        e Zd ZdZdZdZdZdZy)CacheArtifactTypez
    Type of cache
    r            N)__name__
__module____qualname____doc__INDUCTORAUTOTUNEAOT_AUTOGRADPGO     E/home/dcms/DCMS/lib/python3.12/site-packages/torch/compiler/_cache.pyr   r      s     HHL
Cr   r   T)frozenc                       e Zd ZU dZeed<   eed<    ej                  d      Z	e
ed<   ededd d	d
fd       Zeded	d fd       Zy
)CacheArtifactzO
    Data for each cache artifact that will be serialized and deserialized
    typekeyF)reprcontentwriterclsreturnNc                     | j                  |j                  j                         | j                  |j                         | j                  |j                         y N)write_uint64r    value	write_strr!   write_bytesr#   )r$   r%   s     r   	serializezCacheArtifact.serialize*   s<    CHHNN+!3;;'r   readerc                     | j                         }| j                         }| j                         }t        t	        |      ||      S r(   )read_uint64read_str
read_bytesr   r   )r.   r    r!   r#   s       r   deserializezCacheArtifact.deserialize0   s?    !!#oo##%.t4c7CCr   )r   r   r   r   r   __annotations__strdataclassesfieldr#   bytesstaticmethodr   r-   r
   r3   r   r   r   r   r       sx     	H&[&&E2GU2(+ (O ( ( (
 DK DO D Dr   r   c                      e Zd ZU dZ ej
                  e      Zee   e	d<    ej
                  e      Z
ee   e	d<    ej
                  e      Zee   e	d<    ej
                  e      Zee   e	d<   dedd	fd
ZddZy	)	CacheInfozb
    Return value of serialization and deserialization for the purpose of
    instrumentation
    )default_factoryinductor_artifactsautotune_artifactsaot_autograd_artifactspgo_artifactsartifactr&   Nc                 `   |j                   t        j                  k(  r&| j                  j	                  |j
                         y |j                   t        j                  k(  r&| j                  j	                  |j
                         y |j                   t        j                  k(  r&| j                  j	                  |j
                         y |j                   t        j                  k(  r&| j                  j	                  |j
                         y t        j                  d|j                           y )NUnsupported artifact type )r    r   r   r=   appendr!   r   r>   r   r?   r   r@   logwarning)selfrA   s     r   addzCacheInfo.addD   s    ==-666##**8<<8]]/888##**8<<8]]/<<<''..x||<]]/333%%hll3KK4X]]ODEr   c                     | j                   j                          | j                  j                          | j                  j                          | j                  j                          y r(   )r=   clearr>   r?   r@   )rG   s    r   rJ   zCacheInfo.clearP   sJ    %%'%%'##))+  "r   r&   N)r   r   r   r   r6   r7   listr=   r5   r4   r>   r?   r@   r   rH   rJ   r   r   r   r;   r;   8   s    
 %6K$5$5d$KS	K$5K$5$5d$KS	K(9(9(9$(ODIO0{00FM49F
FM 
Fd 
F#r   r;   c            	       ,   e Zd ZU dZg Zee   ed<    e       Z	ee   ed<    e
ej                        Ze
e   ed<    e       Zeed<   edd	       Zed
ededeeef   ddfd       Zedefd       Zedeeeef      fd       Zededee   fd       Zy)CacheArtifactManageraQ  
    Lightweight manager class for collecting and processing cache artifacts for
    hot loading

    Intended Lifecycle:
    - Execute code via torch.compile, this will call
        CacheArtifactManager.record_artifact on each cache artifact
    - Call CacheArtifactManager.serialize to convert all the cache artifacts
        to portable format
    - Call CacheArtifactManager.deserialize to hot load the cache artifacts on
        a potentially different process

    NOTE: There's no FB/FC guarentees, results of cache artifacts will not be
          used unless code version matches.
    _new_cache_artifacts_seen_artifacts)serialize_fn_serializer_cache_infor&   Nc                     | j                   j                          | j                  j                          | j                  j                          | j                  j                          y r(   )rO   rJ   rP   rR   rS   r%   s    r   rJ   zCacheArtifactManager.clearu   sF      &&(!!#r   artifact_typer!   r#   c                    |t         j                  k(  r-t        |t              rJ t	               }|j                  |      }t        |t              sJ t        |||      }|| j                  v ryt        j                  dt        |             | j                  j                  |       | j                  j                  |       y)zg
        Called from each caching operation to record the artifact in this
        "mega" list
        NzRecording %s)r   r   
isinstancer8   r   encoder   rP   rE   debugr5   rO   rD   rH   )r%   rV   r!   r#   serderA   s         r   record_artifactz$CacheArtifactManager.record_artifact|   s     -666!'5111(*Ell7+G'5))) W=s***		.#h-0  ''1)r   c                 2    t        | j                        dk7  S )zG
        Have we seen new artifacts since last serialize call?
        r   )lenrO   rU   s    r   need_serializez#CacheArtifactManager.need_serialize   s    
 3++,11r   c                    | j                   D ]3  }t        j                  d|       | j                  j	                  |       5 	 t        j                  | j                        }| j                  j                  | j                          | j                  j                         }| j                   j                          ||fS # t        $ r t        j                  dd       Y yw xY w)z?
        Converts the "mega" list into portable format
        z
saving: %sz Failed to pickle cache artifactsTexc_infoN)rO   rE   rZ   rS   rH   copydeepcopyrR   extendto_bytesrJ   	ExceptionrF   )r%   rA   infoartifact_bytess       r   r-   zCacheArtifactManager.serialize   s    
 00 	*HIIlH-OO)	*		K ==1DOO""3#;#;< __557N$$**,!4'' 	KKK:TKJ	Ks   A;C    C#"C#serialized_artifactsc                    	 t        j                  | t        j                        }ddlm} ddl	m
} dd	lm} dd
lm}  |       }t               }|D ]  }t
        j!                  d|       |j#                  |       |j$                  t&        j(                  k(  r'|j+                  |j,                  |j.                         o|j$                  t&        j0                  k(  rOt2        j4                  j7                  t9               |j,                        }	|j;                  |	|j.                         |j$                  t&        j<                  k(  r(|j+                  |j,                  |j.                          |j$                  t&        j>                  k(  r# ||j,                  |j.                        }
|
^J t
        j                  d|j$                           |S # t        $ r t
        j                  dd       Y yw xY w)zR
        Converts the portable format back into various filesystem caches
        )deserialize_fnz#Failed to un-pickle cache artifactsTra   Nr   )write_local_impl)AOTAutogradCache)FxGraphCache)_LocalAutotuneCacheBackendzwriting: %srC   ) r	   to_listr   r3   rg   rE   rF   torch._dynamo.pgorm   -torch._functorch._aot_autograd.autograd_cachern   torch._inductor.codecachero   &torch._inductor.runtime.autotune_cacherp   r;   rZ   rH   r    r   r   _write_to_local_cacher!   r#   r   ospathjoinr   _putr   r   )rj   	artifactsrm   rn   ro   rp   autotune_cacherh   rA   r!   metas              r   r3   z CacheArtifactManager.deserialize   s   
	/77$]5N5NI 	7R:U35{! 	JHIImX.HHX}} 1 : ::228<<AQAQR"3"<"<<ggll9;=##C)9)9:"3"@"@@ 66x||XEUEUV"3"7"77'h6F6FG'''8HI	J  9  	KK=KM	s   %G  G<;G<rK   )r   r   r   r   rO   rL   r   r4   r   rP   r	   r-   rR   r;   rS   classmethodrJ   r   r5   r   r8   r   r\   boolr_   r   tupler9   r3   r   r   r   rN   rN   W   s   " 13$}-2 2<OZ.= ;R",,;K(7  '[K(    *(* * uj()	*
 
* *, 2t 2 2 (5	)9#:;  & $% $HY4G $ $r   rN   )rc   r6   loggingrw   enumr   typingr   r   torch._inductor.remote_cacher   r   %torch._inductor.runtime.runtime_utilsr   &torch.utils._appending_byte_serializerr	   r
   r   torch.utils._ordered_setr   	getLoggerr   rE   r   	dataclassr   r;   rN   r   r   r   <module>r      s       	  " I ; 
 0 g!  d#D D $D. # # #<| |r   