Kyojin: open-source engine that runs 100 GB-class open models (GLM-5.3, Qwen3.8, DeepSeek V4.1) on one desktop machine. AMD Strix Halo, NVIDIA RTX and Apple Mac. OpenAI API, built on ExLlamaV3.
Python amdapple-siliconcudadeepseekexl3exllamav3glminferencellmllm-inferencelocal-llmnvidiaopenai-apiquantizationqwenrocmstrix-halo
Recurrent checkpoint cache: spare the shared-prefix anchors when evicting
YanissAmzd9b3f8d56d0a
diff --git a/exllamav3/cache/recurrent.py b/exllamav3/cache/recurrent.pyindex 93e1236..c346bfa 100644--- a/exllamav3/cache/recurrent.py+++ b/exllamav3/cache/recurrent.py@@ -20,6 +20,10 @@ def note_freed(nbytes: int):malloc_trim()+ANCHOR_MIN_POS = 2048+ANCHOR_MAX_POS = int(os.environ.get("EXL3_RC_ANCHOR_MAX", "16384")) # 0 = plain LRU++class RecurrentCache(OrderedDict):def __init__(self,@@ -145,7 +149,8 @@ class RecurrentCache(OrderedDict):popped = self.pop(popped_key)self.metrics["stash_evictions_stranded"] += 1else:- popped_key, popped = self.popitem(last = False)+ popped_key = self._lru_victim()+ popped = self.pop(popped_key)if pt is not None:page = pt.referenced_pages.get(popped_key) or pt.unreferenced_pages.get(popped_key)if page is not None and page.kv_position == PAGE_SIZE:@@ -162,6 +167,26 @@ class RecurrentCache(OrderedDict):self.update_total_size()+ def _lru_victim(self):+ """+ Oldest checkpoint, sparing the shared-prefix anchors (power-of-two positions up to ANCHOR_MAX_POS: 2K, 4K, 8K, 16K) while+ other checkpoints exist and the anchors hold at most half of the cache: a long session churns through its own prompt-end+ checkpoints and would otherwise push out the one at the end of the system prompt, so a compacted or new session sharing+ that prompt resumed from zero although its K/V pages were still cached.+ """+ anchors = 0+ victim = None+ for k, v in OrderedDict.items(self):+ pos = v.get("position", 0) if isinstance(v, dict) else 0+ if ANCHOR_MIN_POS <= pos <= ANCHOR_MAX_POS and pos & (pos - 1) == 0:+ anchors += v["checkpoint_size"]+ elif victim is None:+ victim = k+ if victim is None or anchors > self.max_size // 2:+ return next(iter(OrderedDict.keys(self)))+ return victim++def drop(self, key) -> None:"""Remove one checkpoint (rolling-checkpoint cleanup)."""popped = self.pop(key, None)diff --git a/tests/test_rc_anchor.py b/tests/test_rc_anchor.pynew file mode 100644index 0000000..49702c6--- /dev/null+++ b/tests/test_rc_anchor.py@@ -0,0 +1,47 @@+"""RecurrentCache eviction spares the shared-prefix anchors (power-of-two positions 2K..16K) while other checkpoints exist. CPU only."""+import os+os.environ.setdefault("CUDA_VISIBLE_DEVICES", "")+from types import SimpleNamespace+from exllamav3.cache import recurrent+from exllamav3.cache.recurrent import RecurrentCache+++def make(max_entries):+ model = SimpleNamespace(loaded_tp=False)+ c = RecurrentCache(model, max_size=max_entries * 100)+ return c+++def st(pos):+ return {"position": pos, "checkpoint_size": 100}+++def test_anchors_survive_lru():+ c = make(6)+ for i, pos in enumerate([2048, 4096, 8192, 9472, 20480, 40960]):+ c.put(("k", i), None, stashed=st(pos))+ for j in range(10): # a long session keeps adding prompt-end checkpoints+ c.put(("n", j), None, stashed=st(50000 + 256 * j))+ kept = sorted(v["position"] for v in c.values())+ assert 2048 in kept and 4096 in kept and 8192 in kept, kept+ assert len(kept) == 6+++def test_anchors_do_not_fill_the_cache():+ c = make(4)+ for i, pos in enumerate([2048, 4096, 8192, 16384]):+ c.put(("a", i), None, stashed=st(pos))+ c.put(("n", 0), None, stashed=st(60000))+ c.put(("n", 1), None, stashed=st(60256))+ kept = sorted(v["position"] for v in c.values())+ assert kept[-2:] == [60000, 60256] or 60256 in kept, kept # anchors beyond half the cache go by plain LRU+ assert len(kept) == 4+++def test_off_is_plain_lru(monkeypatch):+ monkeypatch.setattr(recurrent, "ANCHOR_MAX_POS", 0)+ c = make(3)+ for i, pos in enumerate([2048, 4096, 8192]):+ c.put(("a", i), None, stashed=st(pos))+ c.put(("n", 0), None, stashed=st(60000))+ assert 2048 not in [v["position"] for v in c.values()]