From 76b9106ccf5f92b9342b3b0eea21d3c19be6d80e Mon Sep 17 00:00:00 2001 From: Steven Huang <134844631+remote-controlled-man@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:42:04 +0800 Subject: [PATCH 1/2] fix(cache): include snapshot arrays in RAM cache capacity --- CHANGELOG.md | 1 + llama_cpp/llama_cache.py | 5 ++- tests/test_llama_cache.py | 78 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 tests/test_llama_cache.py diff --git a/CHANGELOG.md b/CHANGELOG.md index bfa7e917b..a92cb11b6 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): include snapshot arrays in RAM cache capacity by @remote-controlled-man - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds diff --git a/llama_cpp/llama_cache.py b/llama_cpp/llama_cache.py index 5fed2431a..41bda09bc 100644 --- a/llama_cpp/llama_cache.py +++ b/llama_cpp/llama_cache.py @@ -59,7 +59,10 @@ def __init__(self, capacity_bytes: int = (2 << 30)): @property def cache_size(self): - return sum([state.llama_state_size for state in self.cache_state.values()]) + return sum( + state.llama_state_size + state.scores.nbytes + state.input_ids.nbytes + for state in self.cache_state.values() + ) def _find_longest_prefix_key( self, diff --git a/tests/test_llama_cache.py b/tests/test_llama_cache.py new file mode 100644 index 000000000..fb81d3236 --- /dev/null +++ b/tests/test_llama_cache.py @@ -0,0 +1,78 @@ +import numpy as np +import pytest + +from llama_cpp import LlamaRAMCache, LlamaState + + +def test_ram_cache_counts_snapshot_buffers(): + state = LlamaState( + input_ids=np.array([1, 2, 3, 4], dtype=np.intc), + scores=np.zeros((4, 8), dtype=np.single), + n_tokens=4, + llama_state=b"state", + llama_state_size=5, + seed=0, + ) + cache = LlamaRAMCache(capacity_bytes=149) + cache[[1, 2, 3, 4]] = state + + # 5 native bytes, 16 token bytes, and 128 score bytes. + assert cache.cache_size == 149 + assert cache[[1, 2, 3, 4]] is state + + +def _state(token, columns=8): + return LlamaState( + input_ids=np.array([token], dtype=np.intc), + scores=np.zeros((1, columns), dtype=np.single), + n_tokens=1, + llama_state=b"state", + llama_state_size=5, + seed=0, + ) + + +@pytest.mark.parametrize("capacity, retained", [(40, False), (41, True), (42, True)]) +def test_ram_cache_snapshot_capacity_boundary(capacity, retained): + cache = LlamaRAMCache(capacity_bytes=capacity) + cache[[1]] = _state(1) + + assert ([1] in cache) is retained + assert cache.cache_size == (41 if retained else 0) + + +def test_ram_cache_evicts_least_recently_used_snapshot(): + cache = LlamaRAMCache(capacity_bytes=82) + first, second, third = (_state(token) for token in (1, 2, 3)) + cache[[1]] = first + cache[[2]] = second + assert cache[[1]] is first + + cache[[3]] = third + + assert [2] not in cache + assert cache[[1]] is first + assert cache[[3]] is third + assert cache.cache_size == 82 + + +def test_ram_cache_replacement_accounts_for_new_buffers(): + cache = LlamaRAMCache(capacity_bytes=82) + cache[[1]] = _state(1) + cache[[2]] = _state(2) + replacement = _state(1, columns=16) + + cache[[1]] = replacement + + assert [2] not in cache + assert cache[[1]] is replacement + assert cache.cache_size == 73 + + +@pytest.mark.parametrize("tokens", [[1], [1, 2]]) +def test_ram_cache_reuses_snapshot_within_capacity(tokens): + cache = LlamaRAMCache(capacity_bytes=41) + state = _state(1) + cache[[1]] = state + + assert cache[tokens] is state From 25dc079c613bb326e9a50c5f1b66a8f31da1eb0f Mon Sep 17 00:00:00 2001 From: Steven Huang <134844631+remote-controlled-man@users.noreply.github.com> Date: Fri, 27 Nov 2026 20:52:18 +0800 Subject: [PATCH 2/2] docs: link RAM cache capacity changelog to PR --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a92cb11b6..5853046a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -- fix(cache): include snapshot arrays in RAM cache capacity by @remote-controlled-man +- fix(cache): include snapshot arrays in RAM cache capacity by @remote-controlled-man in #2457 - fix: load cached pretrained models in offline mode by @emme1t in #2396 - fix(ci): install matching NVML headers for CUDA wheel builds