
    vh.                    L   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
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j,                  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Z.	 	 	 	 	 	 	 	 	 	 d/d Z/ej`                  jb                  f	 	 	 	 	 	 	 d0d!Z2d*d"Z3d(d#Z4	 	 	 	 	 	 d1d$Z5	 	 	 	 d2d%Z6y# e	$ r dZY w xY w)3    )annotationsN)	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/opt/face_recognition/venv/lib/python3.12/site-packages/jax/_src/compilation_cache.pyis_cache_usedr    6   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&   Z   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,   `   s     	--2D9r'   c                |    t        j                  dt        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().
  z9initialize_cache is deprecated; use set_cache_dir instead   
stacklevelr)   N)warningswarnDeprecationWarningr   r*   r+   s    r   initialize_cacher4   k   s-     --K"q2--2D9r'   c                      y)zDReturns the minimum size below which the entry should not be cached.r    r6   r'   r   default_min_cache_entry_sizer7   w   s    	
r'   c                 6    t         j                  j                  S N)r   enable_compilation_cacher$   r6   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*   r7   r   r&   )r%   cache_and_paths     r   _initialize_cacherB      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 r9   )r   r=   r$   r:   r6   r'   r   is_persistent_cache_enabledrD      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   rD   loggingWARNINGDEBUGr>   logr   rB   )r   log_prioritys     r   
_get_cacherL      sX     22'B'DGOO  
JJ|B##%^	-r'   c                    t         r%t        j                         }|j                  |       S t        j                  |       S r9   )	zstandardZstdCompressorcompresszlib)
executable
compressors     r   compress_executablerT      s3    ))+Jz**==$$r'   c                    t         r%t        j                         }|j                  |       S t        j                  |       S r9   )rN   ZstdDecompressor
decompressrQ   )rR   decompressors     r   decompress_executablerY      s3    --/L"":..??:&&r'   c                H    t        |       }|y|j                  |      }|duS )z)Checks if the executable is in the cache.NF)rL   get)r   r   cacheexecutable_and_times       r   is_executable_in_cacher^      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)rL   r>   r?   r[   rY   extract_executable_and_timedeserialize_executable)	r   compile_optionsr   executable_devicesr\   r]   serialized_executablecompile_timexla_executable_deserializeds	            r   get_executable_and_timerg      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|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       |j)                  | |       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 initializedzjNot 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$   rD   rG   rH   rI   rL   r>   rJ   serialize_executablecombine_executable_and_timerT   r@   lenr   r   compilation_cache_expect_pgler1   r2   put)r   module_namerR   r   re   rK   r\   rd   r]   min_entry_size
entry_sizes              r   put_executable_and_timerr      s1    006613 // }}  W
%
]
JJ|:;DF !66zB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 )NrN   rQ   )r   r[   rN   )moduledevicesrb   r   ignore_callbackss        r   get_cache_keyrw     s>     
*k
 
 17
 r'   c                 N    t        j                  dt        d       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.
  z(is_initialized is deprecated; do not user.   r/   )r1   r2   r3   r   r6   r'   r   is_initializedry   -  s!     --:"q2		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   r6   r'   r   reset_cacher}   :  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)rd   re   s     r   rk   rk   I  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)r]   s    r   r`   r`   W  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   )rR   bytesr   r   )r   r   r   r   )r   r   r   z5tuple[xla_client.LoadedExecutable | None, int | None])
r   r   ro   r   rR   zxla_client.LoadedExecutablere   r   r   r   )rt   z	ir.Moduleru   z
np.ndarrayrv   zcache_key.IgnoreCallbacksr   r   )rd   r   re   r   r   r   )r]   r   r   ztuple[bytes, int])7
__future__r   rG   	threadingr1   rQ   numpynprN   ImportError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,   r4   r7   r   rB   rD   rL   rT   rY   r^   rg   rr   IgnoreCallbacksNOrw   ry   r}   rk   r`   r6   r'   r   <module>r      s   #         ? #   ' 
		8	$ $ $  D   T  *9>>+ "%% x '!H1:	:
/!KF5
%')33:3,2.2.2. ,2.
 2. 
2.t 3<2K2K2N2N
 0 	"
P P03P
P000y	  )s   D D#"D#