
    ukiU                        U d dl mZ d dlZd dlZd dlZd dlmZ d dlZdZde	d<   	 d dlZd dlmZ d dlmZ  ej                   e      ZdZd	Zdd
Z G d de      Zy# e
$ r Y 9w xY w)    )annotationsN)Anyz
Any | Nonefilelockpath)CacheInterfacez-cachez-atimec                0    | j                  d      xs d| vS )Nzfile://z://)
startswithr   s    M/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/_src/lru_cache.py_is_local_filesystemr   '   s    		#	8uD'88    c                  <    e Zd ZdZddddZddZddZdddd	Zy
)LRUCachea;  Bounded cache with least-recently-used (LRU) eviction policy.

  This implementation includes cache reading, writing and eviction
  based on the LRU policy.

  Notably, when ``max_size`` is set to -1, the cache eviction
  is disabled, and the LRU cache functions as a normal cache
  without any size limitations.
  
   )lock_timeout_secsc                  t        |      st        j                  st        d      t        j                  |      x| _        | _        | j
                  j                  dd       |dk7  | _        | j                  rt        t        d      || _
        || _        | j
                  dz  | _        t        |      r%t        j                  | j                        | _        yt        j                  | j                        | _        yy)a_  Args:

      path: The path to the cache directory.
      max_size: The maximum size of the cache in bytes. Caching will be
        disabled if this value is set to ``0``. A special value of ``-1``
        indicates no limit, allowing the cache size to grow indefinitely.
      lock_timeout_secs: (optional) The timeout for acquiring a file lock.
    z`Please install the `etils[epath]` package to specify a cache directory on a non-local filesystemT)parentsexist_okNzMPlease install the `filelock` package to set `jax_compilation_cache_max_size`z	.lockfile)r   pathlibepath_installedRuntimeErrorPathr   _pathmkdireviction_enabledr   max_sizer   	lock_pathFileLocklockSoftFileLock)selfr   r   r   s       r   __init__zLRUCache.__init__6   s      %g.E.E{||$\\$//DI
IIOOD4O0$ND		jkkdm0dyy;.dn	d	#%%dnn5	))$..9	 r   c                   |st        d      | j                  | t         z  }| j                  r&| j                  j                  | j                         	 |j                         sAt        j                  d|       	 | j                  r| j                  j                          yyt        j                  d|       |j                         }| j                  rLt        j                         j                  dd      }| j                  | t         z  }|j!                  |       || j                  r| j                  j                          S S # | j                  r| j                  j                          w w xY w)zRetrieves the cached value for the given key.

    Args:
      key: The key for which the cache value is retrieved.

    Returns:
      The cached data as bytes if available; ``None`` otherwise.
    key cannot be emptytimeoutzCache miss for key: NzCache hit for key:    little)
ValueErrorr   _CACHE_SUFFIXr   r    acquirer   existsloggerdebugrelease
read_bytestimetime_nsto_bytes_ATIME_SUFFIXwrite_bytes)r"   key
cache_pathval	timestamp
atime_paths         r   getzLRUCache.getT   sB    ,--u]O44J
ii 6 67 +C734 
				 
 ll(01!!#c			LLN++Ax8	YYC5!88
y) 
				 
				 
s   (E )BE )E;c                   |st        d      | j                  rMt        |      | j                  kD  r5d|dt        |       d| j                   d}t	        j
                  |       y| j                  | t         z  }| j                  r&| j                  j                  | j                         	 |j                         r)	 | j                  r| j                  j                          yy| j                  t        |             |j                  |       | j                  rLt        j                          j#                  d	d
      }| j                  | t$         z  }|j                  |       | j                  r| j                  j                          yy# | j                  r| j                  j                          w w xY w)zAdds a new entry to the cache.

    If a cache item with the same key already exists, no action
    will be taken, even if the value is different.

    Args:
      key: The key under which the data will be stored.
      val: The data to be stored.
    r%   zCache value for key z	 of size z) bytes exceeds the maximum cache size of z bytesNr&   additional_sizer(   r)   )r*   r   lenr   warningswarnr   r+   r    r,   r   r-   r0   _evict_if_neededr6   r2   r3   r4   r5   )r"   r7   r9   msgr8   r:   r;   s          r   putzLRUCache.puty   s~    ,-- SDMM!9#C7)CH: >**.--@cmmCu]O44J
ii 6 67				 
				 
 CH5S!			LLN++Ax8	YYC5!88
y) 
				 
				 
s   1F *BF )F?r   r>   c                  | j                   syg }d}| j                  j                  dt               D ]  }|j	                         }t
        j                  s|j                  n|j                  }|j                  j                  t              }| j                  | t         z  }t        j                  |j                         d      }	||z  }t        j                   ||	||f        | j"                  |z
  }
||
kD  rt        j$                  |      \  }	}}t&        j)                  d|||
       | j                  | t         z  }| j                  | t         z  }|j+                          |j+                          ||z  }||
kD  ryy)a8  Evicts the least recently used items from the cache if necessary
    to ensure the cache does not exceed its maximum size.

    Args:
      additional_size: The size of the new entry being added to the cache.
        This is included to account for the new entry when checking if
        eviction is needed.
    Nr   *r)   zGEvicting cache entry %r: file size %d bytes, target cache size %d bytes)r   r   globr+   statr   r   st_sizelengthnameremovesuffixr5   int
from_bytesr1   heapqheappushr   heappopr.   r/   unlink)r"   r?   hdir_sizer8   	file_stat	file_sizer7   r;   
file_atimetarget_sizes              r   rC   zLRUCache._evict_if_needed   so       %'AHiinnq%89 6
//#i
 ,3+B+B)##	HXHXiOO((7c99#}o66j>>*"7"7"98Dj)hnnQS)456 --/1K [
 #(==#3 j#yll 014iN 99#}o66j99#}o66j)h [
 r   N)r   strr   rN   r   zfloat | None)r7   rZ   returnzbytes | None)r7   rZ   r9   bytesr[   None)r?   rN   r[   r]   )__name__
__module____qualname____doc__r#   r<   rE   rC    r   r   r   r   +   s)     UW :<#J(T 89 -r   r   )r   rZ   r[   bool)
__future__r   rP   loggingr2   typingr   rA   r   __annotations__ImportErrorjax._srcr   r   $jax._src.compilation_cache_interfacer   	getLoggerr^   r.   r+   r5   r   r   rb   r   r   <module>rl      sx    #     *  % ?			8	$ 9e~ e#  s   A A$#A$