o
    Iñhù1  ã                	   @  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 !¡ rƒd 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.getÚremote_fx_graph_cache_getFÚ!remote_fx_graph_cache_get_time_usT)Ú
phase_nameÚlog_pt2_compile_eventÚdynamo_compile_column_usÚlog_waitcounterzFbRemoteFxGraphCache.putÚremote_fx_graph_cache_putÚ!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"   úP/var/www/vscode/kcb/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#   r1   O   s   
ýþzRemoteCacheBackend.getc                 C  s<   z|   ||¡ t | j¡ W d S  ty   t | j¡ ‚ w r)   )r/   r0   Úputr   r2   r3   r.   r"   r"   r#   r6   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/   r1   r6   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"   r=   r"   r"   r#   Údecodeg   r,   zRemoteCacheSerde.decodeN)r-   r   r   r   )r-   r   r   r   )r   r9   r:   r   r>   r?   r"   r"   r"   r#   r<   b   s
    r<   Ú
JsonDataTyc                   @  s    e Zd Zd
dd„Zddd„Zd	S )ÚRemoteCacheJsonSerder-   r@   r   Úbytesc                 C  s   t t |¡dƒS )NÚascii)rB   ÚjsonÚdumpsr=   r"   r"   r#   r>   r   ó   zRemoteCacheJsonSerde.encodec                 C  s
   t  |¡S r)   )rD   Úloadsr=   r"   r"   r#   r?   u   s   
zRemoteCacheJsonSerde.decodeN)r-   r@   r   rB   )r-   rB   r   r@   ©r   r9   r:   r>   r?   r"   r"   r"   r#   rA   q   ó    
rA   c                   @  s    e Zd Zd	dd„Zd	dd„ZdS )
ÚRemoteCachePassthroughSerder-   r   r   c                 C  ó   |S r)   r"   r=   r"   r"   r#   r>   z   ó   z"RemoteCachePassthroughSerde.encodec                 C  rK   r)   r"   r=   r"   r"   r#   r?   }   rL   z"RemoteCachePassthroughSerde.decodeN)r-   r   r   r   rH   r"   r"   r"   r#   rJ   y   rI   rJ   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__rN   rO   rQ   )r!   rO   rQ   Ú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+   r0   r1   r   r   r2   r3   Ú_log_sample)r!   r%   ÚsampleÚresultr"   r"   r#   r1   §   s   þ
$÷zRemoteCache.getr5   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   rU   rV   r/   r0   r6   r   r   r2   r3   rW   ©r!   r%   r5   rX   r"   r"   r#   r6   ¶   s   þ"÷zRemoteCache.putr-   r   rX   úOptional[Sample]c                 C  ó   | j  |¡S r)   )rQ   r?   )r!   r-   rX   r"   r"   r#   Ú_decodeÃ   ó   zRemoteCache._decodeÚobjectc                 C  r\   r)   )rQ   r>   )r!   r5   rX   r"   r"   r#   Ú_encodeÇ   r^   zRemoteCache._encodec                 C  s   |   |¡ }r|  ||¡S d S r)   )Ú_backend_getr]   )r!   r%   rX   r-   r"   r"   r#   r+   Ì   s   zRemoteCache._getc                 C  r\   r)   )rO   r1   r*   r"   r"   r#   ra   Ô   r^   zRemoteCache._backend_getc                 C  s   |   ||¡}|  ||¡ d S r)   )r`   Ú_backend_put)r!   r%   r5   rX   r-   r"   r"   r#   r/   Ù   s   zRemoteCache._putc                 C  s   | j  ||¡ d S r)   )rO   r6   r.   r"   r"   r#   rb   à   s   zRemoteCache._backend_putc                 C  r(   r)   r"   r    r"   r"   r#   rV   å   rL   zRemoteCache._create_samplec                 C  r(   r)   r"   )r!   rX   r"   r"   r#   rW   é   rL   zRemoteCache._log_sample)rO   rP   rQ   rR   r   r   r8   )r%   r&   r5   r   r   r   )r-   r   rX   r[   r   r   )r5   r   rX   r[   r   r_   )r%   r&   rX   r[   r   r'   )r%   r&   r   r_   )r%   r&   r5   r   rX   r[   r   r   )r%   r&   r-   r_   r   r   )r   r[   )rX   r[   r   r   )r   r9   r:   rN   Ú__annotations__r$   r1   r6   r]   r`   r+   ra   r/   rb   rV   rW   r"   r"   r"   r#   rM   ˜   s   
 









rM   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 )NÚTORCHINDUCTOR_REDIS_URLÚTORCHINDUCTOR_REDIS_HOSTÚ	localhostÚTORCHINDUCTOR_REDIS_PORTië  )ÚhostÚport)
Úsuperr$   ÚredisÚosÚenvironÚRedisÚfrom_urlre   r1   Úint)r!   rf   ©rS   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)   )re   r1   rn   Ú
exceptionsÚConnectionErrorÚ
isinstancerB   r4   r"   r"   r#   r+     s   üzRedisRemoteCacheBackend._getr-   rB   c                 C  s>   | j sd S z
| j  ||¡ W d S  tjjy   d | _ Y d S w r)   )re   Úsetrn   rv   rw   r.   r"   r"   r#   r/     s   ýzRedisRemoteCacheBackend._put©rf   r&   r   r   )r%   r&   r   ru   )r%   r&   r-   rB   r   r   )r   r9   r:   r;   re   rc   r$   r
   r+   r/   Ú__classcell__r"   r"   rt   r#   rd   í   s   
 rd   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 )ÚRedisRemoteCacherf   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)rS   rN   ÚtypingÚcastr   rB   rd   rA   rm   r$   Ú_key_fmt)r!   rf   rO   rQ   Úversionrt   r"   r#   r$   #  s   zRedisRemoteCache.__init__r%   c                 C  s   | j j|dS )N)r%   )r€   Úformatr*   r"   r"   r#   Ú_get_key0  s   zRedisRemoteCache._get_keyrX   r[   úOptional[JsonDataTy]c                   s   |   |¡}tƒ  ||¡S r)   )rƒ   rm   r+   )r!   r%   rX   rt   r"   r#   r+   3  s   
zRedisRemoteCache._getr5   r@   c                   s   |   |¡}tƒ  |||¡ d S r)   )rƒ   rm   r/   rZ   rt   r"   r#   r/   8  s   
zRedisRemoteCache._putrz   )r%   r&   r   r&   )r%   r&   rX   r[   r   r„   )r%   r&   r5   r@   rX   r[   r   r   )	r   r9   r:   r$   rƒ   r
   r+   r/   r{   r"   r"   rt   r#   r|   "  s    
r|   c                   @  ó   e Zd ZdS )ÚRemoteAutotuneCacheN©r   r9   r:   r"   r"   r"   r#   r†   >  ó    r†   c                   @  r…   )ÚRemoteBundledAutotuneCacheNr‡   r"   r"   r"   r#   r‰   B  rˆ   r‰   c                   @  r…   )ÚRemoteFxGraphCacheNr‡   r"   r"   r"   r#   rŠ   F  rˆ   rŠ   c                   @  r…   )ÚRemoteAOTAutogradCacheNr‡   r"   r"   r"   r#   r‹   J  rˆ   r‹   c                   @  r…   )ÚRemoteDynamoPGOCacheNr‡   r"   r"   r"   r#   rŒ   N  rˆ   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)Útorch._inductor.fb.remote_cacheÚgetattrÚ	_inductorÚfbÚremote_cacheÚsysÚmodulesr   r2   ÚlogÚwarning)r%   r   r   r   ÚtorchÚ	cache_clsÚ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   rs   ÚmissÚhitr6   r3   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¡   r6   r3   r    r"   r"   r#   Ú__str__q  s   &z_CacheStat.__str__N)r   r&   )	r   r9   r:   r¡   rc   r¢   r6   r3   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  rF   z_CacheStats.__init__r}   Únamer&   Úcountrs   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.hitr5   úOptional[object]c                 C  s$   |d u r|   |¡ d S |  |¡ d S r)   )r¡   r¢   )r!   r©   r5   r"   r"   r#   r1     s   z_CacheStats.getc                 C  r«   r)   )r¦   r6   r¬   r"   r"   r#   r6   ‡  r­   z_CacheStats.putc                 C  r«   r)   )r¦   r3   r¬   r"   r"   r#   r3   Š  r­   z_CacheStats.exceptionNr7   )r}   )r©   r&   rª   rs   r   r   )r©   r&   r5   r®   r   r   )
r   r9   r:   rc   r$   r¡   r¢   r1   r6   r3   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ÚStringIOr0   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‘   r7   )HÚ
__future__r   Úatexitr§   ÚdataclassesÚ	functoolsrD   r±   ro   r˜   r~   Úabcr   r   r   r   r   r   r	   Útyping_extensionsr
   r   Útorch._dynamo.utilsr   Útorch._inductorr   Útorch.monitorr   rn   ÚImportErrorÚ	getLoggerr   rš   r   Úrfe.scubadata.scubadata_py3r   ÚSample_rc   r   r_   r   r   ÚpartialÚremote_fx_cache_get_timedÚremote_fx_cache_put_timedr   r<   rs   Úfloatr&   rŽ   ÚdictÚlistr@   rB   rA   rJ   rM   rd   r|   r†   r‰   rŠ   r‹   rŒ   rŸ   Ú	dataclassr    r¥   r0   Úregisterr½   r"   r"   r"   r#   Ú<module>   s„     ÿ
úú
$
ÿU5

