diff --git a/CHANGELOG.md b/CHANGELOG.md index 687567323..d48712249 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- fix(cache): evict disk cache states one at a time in LRU order by @DiadkoShmek in #2400 - fix(index): include available SHA256 hashes in wheel links by @hakarrr in #2409 - fix(server): close tokenizer config files after reading by @cloudgpuapp in #2422 - fix(cache): preserve disk cache entries on lookup and evict least-recently-used states by @remote-controlled-man in #2423 diff --git a/llama_cpp/llama_cache.py b/llama_cpp/llama_cache.py index 6ae8cfa2f..5fed2431a 100644 --- a/llama_cpp/llama_cache.py +++ b/llama_cpp/llama_cache.py @@ -1,4 +1,5 @@ import sys +import time from abc import ABC, abstractmethod from typing import ( Optional, @@ -146,7 +147,10 @@ def __contains__(self, key: Sequence[int]) -> bool: def __setitem__(self, key: Sequence[int], value: "llama_cpp.llama.LlamaState"): print("LlamaDiskCache.__setitem__: called", file=sys.stderr) key = tuple(key) - self.cache[key] = value - print("LlamaDiskCache.__setitem__: set", file=sys.stderr) - self.cache.cull() + # Public cull() evicts ten entries at once, regardless of cull_limit. + with self.cache._transact(retry=True) as (sql, cleanup): + self.cache[key] = value + print("LlamaDiskCache.__setitem__: set", file=sys.stderr) + while self.cache_size > self.capacity_bytes and len(self.cache) > 0: + self.cache._cull(time.time(), sql, cleanup, limit=1) print("LlamaDiskCache.__setitem__: trim", file=sys.stderr)