o
    Zh1                  	   @  s  U d dl mZ d dlZd dlZd dlZd dlZd dlZ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mZmZmZmZ d dlmZmZ d dlmZ d dlmZ d dlmZ zd dlZW n eyl   dZY nw eeZ e! rd d	l"m#Z$ e$Z#d
e%d< ne&e' Z#d
e%d< edZ(edZ)ej*eddddddZ+ej*eddddddZ,G dd dee( Z-G dd dee(e)f Z.eee/e0e1e2e3e1df e4d f  Z5G dd de.e5e6f Z7G dd de.e(e(f Z8G d d! d!ee( Z9G d"d# d#e-e6 Z:G d$d% d%e9e5 Z;G d&d' d'e;Z<G d(d) d)e;Z=G d*d+ d+e;Z>G d,d- d-e;Z?G d.d/ d/e;Z@dAd8d9ZAejBG d:d; d;ZCG d<d= d=ZDeD ZEejFdBd?d@ZGdS )C    )annotationsN)abstractmethod)AnyCallableGenericOptionalTypeVarUnion)override	TypeAlias)dynamo_timed)config)_WaitCounter)Sampler   r   _T_UzFbRemoteFxGraphCache.getZremote_fx_graph_cache_getFZ!remote_fx_graph_cache_get_time_usT)Z
phase_nameZlog_pt2_compile_eventZdynamo_compile_column_usZlog_waitcounterzFbRemoteFxGraphCache.putZremote_fx_graph_cache_putZ!remote_fx_graph_cache_put_time_usc                   @  sJ   e Zd ZdZdddZedd	d
ZedddZdddZdddZ	dS )RemoteCacheBackendz
    A backend implementation for accessing a remote/distributed cache.  Only
    works with bytes in/out.  For structured data use a RemoteCache.
    returnNonec                 C  s   dt | j | _d S )Nzbackend:)type__name___nameself r   K/var/www/auris/lib/python3.10/site-packages/torch/_inductor/remote_cache.py__init__D   s   zRemoteCacheBackend.__init__keystrOptional[_T]c                 C     d S Nr   r   r   r   r   r   _getG      zRemoteCacheBackend._getdatar   c                 C  r    r!   r   r   r   r%   r   r   r   _putK   r$   zRemoteCacheBackend._putc                 C  s<   z|  |}t| j| W |S  ty   t| j  w r!   )r#   cache_statsgetr   	Exception	exceptionr   r   valuer   r   r   r)   O   s   
zRemoteCacheBackend.getc                 C  s<   z|  || t| j W d S  ty   t| j  w r!   )r'   r(   putr   r*   r+   r&   r   r   r   r.   X   s   zRemoteCacheBackend.putNr   r   r   r   r   r   )r   r   r%   r   r   r   )
r   
__module____qualname____doc__r   r   r#   r'   r)   r.   r   r   r   r   r   >   s    

	r   c                   @  s(   e Zd Zed
ddZedddZd	S )RemoteCacheSerder%   r   r   r   c                 C  r    r!   r   r   r%   r   r   r   encodec   r$   zRemoteCacheSerde.encodec                 C  r    r!   r   r5   r   r   r   decodeg   r$   zRemoteCacheSerde.decodeN)r%   r   r   r   )r%   r   r   r   )r   r1   r2   r   r6   r7   r   r   r   r   r4   b   s
    r4   
JsonDataTyc                   @  s    e Zd Zd
ddZdddZd	S )RemoteCacheJsonSerder%   r8   r   bytesc                 C  s   t t|dS )Nascii)r:   jsondumpsr5   r   r   r   r6   r      zRemoteCacheJsonSerde.encodec                 C  s
   t |S r!   )r<   loadsr5   r   r   r   r7   u   s   
zRemoteCacheJsonSerde.decodeN)r%   r8   r   r:   )r%   r:   r   r8   r   r1   r2   r6   r7   r   r   r   r   r9   q       
r9   c                   @  s    e Zd Zd	ddZd	ddZdS )
RemoteCachePassthroughSerder%   r   r   c                 C     |S r!   r   r5   r   r   r   r6   z      z"RemoteCachePassthroughSerde.encodec                 C  rC   r!   r   r5   r   r   r   r7   }   rD   z"RemoteCachePassthroughSerde.decodeN)r%   r   r   r   r@   r   r   r   r   rB   y   rA   rB   c                   @  s   e Zd ZU dZded< d*d
dZd+ddZd,ddZd-ddZd.ddZ	d/ddZ
d0d d!Zd1d"d#Zd2d$d%Zd3d&d'Zd4d(d)ZdS )5RemoteCacheNz/Optional[Callable[[], RemoteCacheBackend[Any]]]backend_override_clsbackendRemoteCacheBackend[_U]serdeRemoteCacheSerde[_T, _U]r   r   c                 C  s*   | j j }d ur| | _n|| _|| _d S r!   )	__class__rF   rG   rI   )r   rG   rI   Zoverride_clsr   r   r   r      s   

zRemoteCache.__init__r   r   r   c              	   C  s   t d 5 |  }z| ||}tt| j| W n ty,   t	t| j  w | 
| |W  d    S 1 s>w   Y  d S )Nzpytorch.remote_cache.get)r   guard_create_sampler#   r(   r)   r   r   r*   r+   _log_sample)r   r   sampleresultr   r   r   r)      s   
$zRemoteCache.getr-   r   c              	   C  s   t d : |d usJ |  }z| ||| tt| j W n ty2   t	t| j  w | 
| W d    d S 1 sCw   Y  d S )Nzpytorch.remote_cache.put)r   rL   rM   r'   r(   r.   r   r   r*   r+   rN   r   r   r-   rO   r   r   r   r.      s   "zRemoteCache.putr%   r   rO   Optional[Sample]c                 C     | j |S r!   )rI   r7   )r   r%   rO   r   r   r   _decode      zRemoteCache._decodeobjectc                 C  rS   r!   )rI   r6   )r   r-   rO   r   r   r   _encode   rU   zRemoteCache._encodec                 C  s   |  | }r| ||S d S r!   )_backend_getrT   )r   r   rO   r%   r   r   r   r#      s   zRemoteCache._getc                 C  rS   r!   )rG   r)   r"   r   r   r   rX      rU   zRemoteCache._backend_getc                 C  s   |  ||}| || d S r!   )rW   _backend_put)r   r   r-   rO   r%   r   r   r   r'      s   zRemoteCache._putc                 C  s   | j || d S r!   )rG   r.   r&   r   r   r   rY      s   zRemoteCache._backend_putc                 C  r    r!   r   r   r   r   r   rM      rD   zRemoteCache._create_samplec                 C  r    r!   r   )r   rO   r   r   r   rN      rD   zRemoteCache._log_sample)rG   rH   rI   rJ   r   r   r0   )r   r   r-   r   r   r   )r%   r   rO   rR   r   r   )r-   r   rO   rR   r   rV   )r   r   rO   rR   r   r   )r   r   r   rV   )r   r   r-   r   rO   rR   r   r   )r   r   r%   rV   r   r   )r   rR   )rO   rR   r   r   )r   r1   r2   rF   __annotations__r   r)   r.   rT   rW   r#   rX   r'   rY   rM   rN   r   r   r   r   rE      s   
 









rE   c                      sL   e Zd ZU dZdZded< d fd	d
ZedddZedddZ	  Z
S )RedisRemoteCacheBackendz?
    A Redis implementation of a remote/distributed cache.
    NzOptional[redis.Redis]_rediscache_idr   r   r   c                   s`   t    ts	d S dtjv rtjtjd | _d S tjtjddt	tjddd| _d S )NZTORCHINDUCTOR_REDIS_URLZTORCHINDUCTOR_REDIS_HOST	localhostZTORCHINDUCTOR_REDIS_PORTi  )hostport)
superr   redisosenvironZRedisZfrom_urlr\   r)   int)r   r]   rK   r   r   r      s   

z RedisRemoteCacheBackend.__init__r   Optional[bytes]c                 C  sT   | j sd S z| j |}W n tjjy   d | _ Y d S w |d u s(t|ts(J |S r!   )r\   r)   rb   
exceptionsConnectionError
isinstancer:   r,   r   r   r   r#     s   zRedisRemoteCacheBackend._getr%   r:   c                 C  s>   | j sd S z
| j || W d S  tjjy   d | _ Y d S w r!   )r\   setrb   rh   ri   r&   r   r   r   r'     s   zRedisRemoteCacheBackend._putr]   r   r   r   )r   r   r   rg   )r   r   r%   r:   r   r   )r   r1   r2   r3   r\   rZ   r   r
   r#   r'   __classcell__r   r   rf   r   r[      s   
 r[   c                      sL   e Zd Zd fddZddd	Zed fddZed fddZ  ZS )RedisRemoteCacher]   r   r   r   c                   sP   | j jrttt d }nt|}t }t 	|| d}d| d| | _
d S )N   zpt2:z	::{key}:c)rK   rF   typingcastr   r:   r[   r9   ra   r   _key_fmt)r   r]   rG   rI   versionrf   r   r   r   #  s   zRedisRemoteCache.__init__r   c                 C  s   | j j|dS )N)r   )rr   formatr"   r   r   r   _get_key0  s   zRedisRemoteCache._get_keyrO   rR   Optional[JsonDataTy]c                   s   |  |}t ||S r!   )ru   ra   r#   )r   r   rO   rf   r   r   r#   3  s   
zRedisRemoteCache._getr-   r8   c                   s   |  |}t ||| d S r!   )ru   ra   r'   rQ   rf   r   r   r'   8  s   
zRedisRemoteCache._putrl   )r   r   r   r   )r   r   rO   rR   r   rv   )r   r   r-   r8   rO   rR   r   r   )	r   r1   r2   r   ru   r
   r#   r'   rm   r   r   rf   r   rn   "  s    
rn   c                   @     e Zd ZdS )RemoteAutotuneCacheNr   r1   r2   r   r   r   r   rx   >      rx   c                   @  rw   )RemoteBundledAutotuneCacheNry   r   r   r   r   r{   B  rz   r{   c                   @  rw   )RemoteFxGraphCacheNry   r   r   r   r   r|   F  rz   r|   c                   @  rw   )RemoteAOTAutogradCacheNry   r   r   r   r   r}   J  rz   r}   c                   @  rw   )RemoteDynamoPGOCacheNry   r   r   r   r   r~   N  rz   r~   r   r   	is_fbcodeboolfb_cache_clsoss_cache_clsr   !Optional[RemoteCache[JsonDataTy]]c                 C  sh   z"|rdd l }t|jjj|}|| W S tjt }t||}|| W S  ty3   t	j
ddd Y d S w )Nr   zUnable to create a remote cacheT)exc_info)Ztorch._inductor.fb.remote_cachegetattrZ	_inductorZfbZremote_cachesysmodulesr   r*   logwarning)r   r   r   r   ZtorchZ	cache_clsZthis_moduler   r   r   create_cacheR  s   



r   c                   @  sH   e Zd ZU dZded< dZded< dZded< dZded< dd	d
ZdS )
_CacheStatr   re   misshitr.   r+   r   r   c              	   C  s&   d| j  d| j d| j d| j d	S )Nz{hit: z, miss: z, put: z, exception: })r   r   r.   r+   r   r   r   r   __str__q  s   &z_CacheStat.__str__N)r   r   )	r   r1   r2   r   rZ   r   r.   r+   r   r   r   r   r   r   j  s   
 r   c                   @  sZ   e Zd ZU ded< dddZddddZddddZdddZddddZddddZ	dS )_CacheStatszdict[str, _CacheStat]_statsr   r   c                 C  s   t t| _d S r!   )collectionsdefaultdictr   r   r   r   r   r   r   x  r>   z_CacheStats.__init__ro   namer   countre   c                 C     | j |  j|7  _d S r!   )r   r   r   r   r   r   r   r   r   {     z_CacheStats.missc                 C  r   r!   )r   r   r   r   r   r   r   ~  r   z_CacheStats.hitr-   Optional[object]c                 C  s$   |d u r|  | d S | | d S r!   )r   r   )r   r   r-   r   r   r   r)     s   z_CacheStats.getc                 C  r   r!   )r   r.   r   r   r   r   r.     r   z_CacheStats.putc                 C  r   r!   )r   r+   r   r   r   r   r+     r   z_CacheStats.exceptionNr/   )ro   )r   r   r   re   r   r   )r   r   r-   r   r   r   )
r   r1   r2   rZ   r   r   r   r)   r.   r+   r   r   r   r   r   u  s   
 

r   r   c                  C  s   t tjsd S dd l} |  }tjstd|d nt|d t	tj
 D ]\}}td| d| |d q&t d|  d S )Nr   z None)filez  z: zCache Metrics:%s)r   isEnabledForloggingINFOioStringIOr(   r   printsorteditemsinfogetvalue)r   outkvr   r   r   dump_cache_stats  s   
r   )
r   r   r   r   r   r   r   r   r   r   r/   )H
__future__r   atexitr   dataclasses	functoolsr<   r   rc   r   rp   abcr   r   r   r   r   r   r	   Ztyping_extensionsr
   r   Ztorch._dynamo.utilsr   Ztorch._inductorr   Ztorch.monitorr   rb   ImportError	getLoggerr   r   r   Zrfe.scubadata.scubadata_py3r   ZSample_rZ   r   rV   r   r   partialZremote_fx_cache_get_timedZremote_fx_cache_put_timedr   r4   re   floatr   r   dictlistr8   r:   r9   rB   rE   r[   rn   rx   r{   r|   r}   r~   r   	dataclassr   r   r(   registerr   r   r   r   r   <module>   s     

$
U5

