
    uki/                       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m	Z	 e		 d dlZnd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 d d	lmZ d d
lmZ  ej0                  e      Zdaded<   daded<   daded<   daded<    ej@                         Z! e"       Z#ded<   d'dZ$d(dZ%d)dZ&d)dZ'd*dZ(d+dZ)d)dZ*d+dZ+d,dZ,d-dZ-d-dZ.d.dZ/	 	 	 	 d/d Z0	 	 	 	 	 	 	 	 	 	 d0d!Z1ejd                  jf                  f	 	 	 	 	 	 	 d1d"Z4d+d#Z5d)d$Z6	 	 	 	 	 	 d2d%Z7	 	 	 	 d3d&Z8y# e
$ r dZ	Y w xY w# e
$ r dZY w xY w)4    )annotationsN)zstd)	cache_key)config)
monitoring)CacheInterface)
xla_client)ir)LRUCacheCacheInterface | None_cacheFbool_cache_initialized_cache_checked_cache_usedzset[str]_UNSUPPORTED_RUNTIMESc                x   t         5  t        rt        cddd       S 	 ddd       t         5  t        sgdag d}t               st	        j
                  d       n2| j                  |v r$t        | dd      rt	        j
                  d       dat        cddd       S 	 ddd       y# 1 sw Y   xY w# 1 sw Y   yxY w)zCheck if cache is used and report adoption metrics one-time per task.
  The cache may be initialized during the first call to this function.
  NT)tpugpucpuneuronz*/jax/compilation_cache/task_disabled_cache!supports_executable_serializationz(/jax/compilation_cache/tasks_using_cacheF)_cache_initialized_mutexr   r   _is_cache_enabledr   record_eventplatformgetattr)backendsupported_platformss     U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/compilation_cache.pyis_cache_usedr!   @   s           n <  LM


1
1gBDI JK% ( 
1 ( 
s   B$A$B0$B-0B9c                T    t         j                  j                  }t        | |      | fS )z1Returns the file cache and the path to the cache.)max_size)r   compilation_cache_max_sizevaluer   )pathr#   s     r    get_file_cacher'   d   s%    ..44(	$	*D	00    c                D    t         j                   j                  d|        y)a  
  Sets the persistent compilation cache directory.

  After calling this, jit-compiled functions are saved to `path`, so they
  do not need be recompiled if the process is restarted or otherwise run again.
  This also tells Jax where to look for compiled functions before compiling.
  jax_compilation_cache_dirNr   updater&   s    r    set_cache_dirr.   j   s     	--2D9r(   c                D    t         j                   j                  d|        y)z
  This API is deprecated; use set_cache_dir instead.

  Set the path. To take effect, should be called prior to any calls to
  get_executable_and_time() and put_executable_and_time().
  r*   Nr+   r-   s    r    initialize_cacher0   u   s     	--2D9r(   c                      y)zDReturns the minimum size below which the entry should not be cached.r    r2   r(   r    default_min_cache_entry_sizer3      s    	
r(   c                 6    t         j                  j                  S N)r   enable_compilation_cacher%   r2   r(   r    r   r      s    		(	(	.	..r(   c                    t         5  t        r
	 d d d        y t        j                  j                  } | s
	 d d d        y t               st        j                  d       	 d d d        y dat        j                  j                  dk(  r(t        j                  j                  dt                      t        J d       t        |       }|t        j                  d       n|\  a} t        j                  d|        d d d        y # 1 sw Y   y xY w)Nz%_initialize_cache: cache is disabled!Tr   )jax_persistent_cache_min_entry_size_bytesz'The cache has already been initialized!z/_initialize_cache: cache initialization failed!z.Initialized persistent compilation cache at %s)r   r   r   compilation_cache_dirr%   r   loggerdebug%persistent_cache_min_entry_size_bytesr,   r3   r   r'   )r&   cache_and_paths     r    _initialize_cacher>      s       KK K 3399DK K ll:;K K  3399Q>mmF79; >DDD>#D)NllDE#lfdllCTJ=K K Ks   DD D'BDDc                 r    t         j                  j                  d uxr t         j                  j                  S r5   )r   r9   r%   r6   r2   r(   r    is_persistent_cache_enabledr@      s/    

&
&
,
,D
8 4--335r(   c                    | j                   t        v rLt               rt        j                  nt        j
                  }t        j                  |d| j                          y t        
t                t        S )Nz#_get_cache: Unsupported runtime: %s)
runtime_typer   r@   loggingWARNINGDEBUGr:   logr   r>   )r   log_prioritys     r    
_get_cacherH      sX     22'B'DGOO  
JJ|B##%^	-r(   c                    t         rt        j                  |       S t        r%t        j                         }|j                  |       S t	        j                  |       S r5   )r   compress	zstandardZstdCompressorzlib)
executable
compressors     r    compress_executablerP      sE    	==$$))+Jz**==$$r(   c                    t         rt        j                  |       S t        r%t        j                         }|j                  |       S t	        j                  |       S r5   )r   
decompressrK   ZstdDecompressorrM   )rN   decompressors     r    decompress_executablerU      sE    	??:&&--/L"":..??:&&r(   c                H    t        |       }|y|j                  |      }|duS )z)Checks if the executable is in the cache.NF)rH   get)r   r   cacheexecutable_and_times       r    is_executable_in_cacherZ      s0    
W
%
] 		),	D	((r(   c                    t        |      }|t        j                  d       y|j                  |       }|yt	        |      }t        |      \  }}|j                  |||      }||fS )zZReturns the cached executable and its compilation time if present, or None
  otherwise.
  z:get_executable_and_time: cache is disabled/not initialized)NN)rH   r:   r;   rW   rU   extract_executable_and_timedeserialize_executable)	r   compile_optionsr   executable_devicesrX   rY   serialized_executablecompile_timexla_executable_deserializeds	            r    get_executable_and_timerc      s     W
%
]
LLMN		), -.AB(C)% ' > >/!B	$l	22r(   c                   t         j                  j                  rt               rt        j
                  nt        j                  }t        |      }|t        j                  |d|        yt        |d      st        j                  dk\  r|j                         }n|j                  |      }t        ||      }t!        |      }t         j"                  j                  }	t%        |      }
|
|	k  rt        j                  |d| |
|	       yt        j                  |d||        t'        j(                  d       t         j*                  j                  rt-        j.                  d|  d	       |j1                  | |       y)
zbAdds the 'executable' and its compilation time to the cache, possibly
  evicting older entries.
  NzVNot writing persistent cache entry with key %r since cache is disabled/not initialized	serializei  zjNot writing persistent cache entry with key %r since its size (%d bytes) is less than threshold (%d bytes)z6Writing %s to persistent compilation cache with key %rz#/jax/compilation_cache/cache_missesz PERSISTENT CACHE WRITE with key z, this is unexpected because JAX_COMPILATION_CACHE_EXPECT_PGLE is set. The execution that populated the cache may lack coverage, https://docs.jax.dev/en/latest/persistent_compilation_cache.html may help debug why this has happened)r   explain_cache_missesr%   r@   rC   rD   rE   rH   r:   rF   hasattrr	   _versionre   serialize_executablecombine_executable_and_timerP   r<   lenr   r   compilation_cache_expect_pglewarningswarnput)r   module_namerN   r   ra   rG   rX   r`   rY   min_entry_size
entry_sizes              r    put_executable_and_timers      sW    006613 // }}  W
%
]
JJ|:;DF Z%)<)<)C&002#88D3\++,?@??EE.&'*. 
JJ|	89BJ
 JJ|GI' AB++11
 mm,YK 8- -. 
IIi,-r(   c                P    t        j                  | |||t        d|      S d|      S )NrK   rM   )r   rW   rK   )moduledevicesr^   r   ignore_callbackss        r    get_cache_keyrx   +  s>     
*k
 
 17
 r(   c                     t               S )z
  Deprecated.

  Return whether the cache is enabled. Initialization can be deferred, so
  initialized status is not checked. The name is retained for backwards
  compatibility.
  )r   r2   r(   r    is_initializedrz   <  s     
	r(   c                     t         j                  dt        t        j                  nd       dat        5  dadadaddd       y# 1 sw Y   yxY w)z*Get back to pristine, uninitialized state.zResetting cache at %s.Nz<empty>F)r:   infor   _pathr   r   r   r   r2   r(   r    reset_cacher~   G  sO     	++&%1v||yB& NK  s   AAc                @    t        |      j                  dd      | z   S )a  Given the serialized executable and the compilation time, produce a cache
  entry in the format shown below.

  The cache entry is of the form:
  Byte:     0    1    2    3    4 ...
  Content:  compilation time    serialized executable
            (big-endian int)
     big	byteorder)intto_bytes)r`   ra   s     r    rj   rj   V  s%     
\		#	#A	#	7:O	OOr(   c                @    | dd t         j                  | dd d      fS )a#  Given the cache entry in the format shown below, extract the serialized
  executable and the compilation time.

  The cache entry 'executable_and_time' is of the form:
  Byte:     0    1    2    3    4 ...
  Content:  compilation time    serialized executable
            (big-endian int)
  r   Nr   r   )r   
from_bytes)rY   s    r    r\   r\   d  s4     
QR	 #.."1 #1 #0 
0 0r(   )r   zxla_client.Clientreturnr   )r&   strr   z!tuple[CacheInterface, str] | None)r   None)r   r   )r   r   )r   r   )rN   bytesr   r   )r   r   r   r   )r   r   r   z5tuple[xla_client.LoadedExecutable | None, int | None])
r   r   rp   r   rN   zxla_client.LoadedExecutablera   r   r   r   )ru   z	ir.Modulerv   z
np.ndarrayrw   zcache_key.IgnoreCallbacksr   r   )r`   r   ra   r   r   r   )rY   r   r   ztuple[bytes, int])9
__future__r   rC   	threadingrm   rM   numpynpcompressionr   ImportErrorrK   jax._srcr   r   r   $jax._src.compilation_cache_interfacer   jax._src.libr	   jax._src.lib.mlirr
   jax._src.lru_cacher   	getLogger__name__r:   r   __annotations__r   r   r   Lockr   setr   r!   r'   r.   r0   r3   r   r>   r@   rH   rP   rU   rZ   rc   rs   IgnoreCallbacksNOrx   rz   r~   rj   r\   r2   r(   r    <module>r      s   #      < )    ? #   ' 
		8	$ $ $  D   T  *9>>+ "%% x '!H1::
/!KF5
%')33:3,5.5.5. ,5.
 5. 
5.z 3<2K2K2N2N
 0 	"P P03P
P000Q
  	$ 
 Is"   D$ D2 $D/.D/2D=<D=