Skip to content

server : purge the least recently used idle slot and keep it in the prompt cache - #176

Merged
dzannotti merged 4 commits into
halo-box:masterfrom
rcfa:pr/lru-purge
Nov 18, 2026
Merged

dzannotti merged 4 commits into
halo-box:masterfrom
rcfa:pr/lru-purge

Conversation

@rcfa

@rcfa rcfa commented Oct 14, 2026 •

Copy link
Copy Markdown

Scope first

Like #171, this is general server code, not Strix specific: ggml-org master has the same try_clear_idle_slots(), with
a TODO above it that names LRU selection and storing the slot to resume later ("smarter decision which slot to clear (LRU ...)", "move slot to level 2
cache instead of removing?", "instead of purging, try to store and resume later?"). I am opening it here because this
fork syncs from ggml-org directly now that halo-box/llama.cpp is archived, and because we have run it in production
with this fork's target model (Qwen3.8-Flash-Next, qwen4exp) since 2026-10-02. If you would rather not carry it here,
please close it; as an agent I cannot open it in ggml-org.

This PR was written by an AI agent (Claude Code), working for @rcfa; what was and was not verified is listed below.

Overview

With a unified KV pool (-kvu) that is full, try_clear_idle_slots() clears the idle slot with the lowest id and
discards its context. That is often the conversation that was used most recently, and it has to be prefilled again
when it comes back.

  1. server : purge the least recently used idle slot and save it to the prompt cache first. The purge picks the
    idle slot with the oldest t_last_used and saves it to the prompt cache before clearing it, as
    get_available_slot() already does. A conversation that comes back is restored instead of prefilled again.
    The gain needs --no-cache-idle-slots. With the default idle-slot caching the idle slots are saved and cleared
    when a task arrives, so try_clear_idle_slots() finds none to purge. The save also runs on the decode-failure
    path. It costs about 50 ms for 164 MiB of state and about 190 ms for 525 MiB (measured in the review, on Strix
    Halo).
  2. server : make room in a full unified KV pool before restoring from the prompt cache. When a returning
    conversation's restore finds no free cells, get_available_slot() cleared the slot and prefilled it again although
    its state was in the cache. It now purges idle slots (least recently used first, each saved to the cache) until the
    restore fits.
    A failed restore empties the slot's sequence, so the slot is cleared before the loop. The saves can evict the
    entry that is to be restored; the loop then stops.
    What this costs in the worst case: one synchronous save per step, up to n_slots - 1 of them, while the other
    slots wait, and if the entry still does not fit next to the busy slots, every idle slot has been purged and the
    request is prefilled anyway. The trade is that save against a full prefill of the conversation.

Related, not changed here: pinning a request to an empty slot with id_slot skips the prompt cache entirely
(ggml-org#28139), so a pinned request to a purged slot is still prefilled again. The tests below send the
returning requests unpinned.

Measurements

Device:     Ryzen AI 9 HX PRO 370 / Radeon 890M (gfx1150, Strix Point - NOT Strix Halo) / Minisforum N5 PRO, BIOS 1.05
Memory:     96 GB DDR5-5600 (2 x 48 GB SO-DIMM, ECC)
Kernel:     7.2.15+kali-amd64
Build:      CPU only: -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=OFF -DGGML_NATIVE=ON -DBUILD_SHARED_LIBS=ON (gcc 16.2.0)
Baseline:   093c552d7 (master, this branch's merge base)
Change:     e305bb838 (this branch's head)

Both arms were built and run in the same session, from the same source tree, and each binary's llama-server --version was checked against the commit it was meant to be.

Master vs this PR, server logic. CPU only (--device none), Qwen2.5-1.5B-Instruct Q4_K_M,
-c 16384 -np 3 -kvu --cache-ram 2048 --no-cache-idle-slots -t 8. B goes to slot 1, A to slot 0 (B is now the least
recently used), C to slot 2: 3 x 6000 tokens do not fit, one idle slot is purged. Then B and A come back, unpinned:

master this PR
slot purged when the pool filled 0 (A, the most recently used) 1 (B, the least recently used)
B comes back: tokens computed 1 of 6000 (it was never purged) 1 of 6000 (restored from the cache)
A comes back: tokens computed 6000 of 6000 (its state was discarded) 1 of 6000
restores that first found the pool full and made room 0 2

B's 16 greedy tokens are the same on both builds. Each of A's and B's returns on this PR first fails one restore of
164.5 MiB (6015 tokens, 28.0 KiB per token), then purges one idle slot and saves it, then restores; the purges in the
whole run are slots 1, 0, 2 on this PR and slots 0, 1 on master.

A different tree, for context only, not evidence for this PR: in production, with Flash-Next on Vulkan (these two
commits on master 02e2c2b plus #171 and #149, which this fork does not have - #149 adds the --cache-dir disk
prompt cache; IQ3_XXS, MTP, -c 262144 -np 4 -kvu --cache-dir): four conversations of 70,000 tokens each take turns
in a pool that holds three, 12 turns after a server restart. The first 3 turns restored into slots that were still
empty. In each of the other 9, the restore first found the pool full, purged the least recently used idle slot to the
(disk) prompt cache, and was restored:

per turn
tokens computed instead of 70,000 4 - 57
wall time, first 3 turns (restore, 32 decoded tokens) 4.6 - 5.5 s
wall time, other 9 turns (restore, purge with a ~3 GiB save, 32 decoded tokens) 7.9 - 16.6 s
the same turns prefilled from scratch (pinned requests, see ggml-org#28139 above) 467 - 488 s

Correctness

  • Master and this PR give the same greedy tokens for the returning conversation (table above).
  • No ggml op, model graph or compute path changes, so test-backend-ops, the corpus comparison and llama-bench do not
    apply.

Not verified

  • tests/unit/test_kv_keep_only_active.py: 5 of 5 pass on the head, 2 of 5 on master (the three new tests fail),
    CPU, under pytest 9.1.1. The runs need PORT=<free port>, because this host has something else on 8080. The rest
    of the tools/server pytest suite was not run: its other presets need model downloads we did not make.
  • Only Vulkan on gfx1150 (Strix Point), and CPU. Nothing on Strix Halo, ROCm, CUDA or Metal.
  • A draft restore that fails for lack of room: the path the review found by reading, and could not force either.
  • Non-unified KV (-kvu off) is unchanged by construction (try_clear_idle_slots() returns early); not exercised.

Generated with Claude Code

@gaetan-puleo

gaetan-puleo commented Oct 16, 2026 •

Copy link
Copy Markdown
Collaborator

@rcfa i rebased your prs

@dzannotti dzannotti added in-review We are reviewing. verifying Building and running the PR to verify its claims. and removed in-review We are reviewing. labels Nov 9, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

Requesting changes. The LRU purge works as described, and a returning conversation is restored instead of prefilled again. But the new retry loop can crash the server: we reproduced a pos_min == -1, but n_past > 0 abort on Vulkan, ROCm and CPU. Master does not crash on the same requests.

Direction

Accept after work. We want this change, and the fix for the abort is one line. The same function is still unchanged in ggml-org, and an open upstream PR (ggml-org#30970) rewrites try_clear_idle_slots(). So a maintainer will also decide whether we carry this here until upstream has an equivalent.

Asks

  1. Blocking. tools/server/server-context.cpp:2058-2068. A failed prompt_load() has already removed the slot's KV sequence, but ret->prompt keeps its old tokens. The loop then saves the LRU slot, and that save can evict the cache entry that the restore wants. The next prompt_load() finds no better entry and returns true without a restore. The slot then has tokens but an empty KV sequence, and the server aborts at line 4011. Call ret->prompt_clear() right after the first failed load, before the loop. We tested this change: the abort stops, and the scenario from your description still computes 1 token. It also stops try_clear_idle_slots() from picking ret itself.
  2. Blocking. Add a test. tools/server/tests/unit/test_kv_keep_only_active.py already sets kv_unified and cache_ram. Please cover the LRU purge with a restore (assert a small prompt_n on the returning request), and the eviction sequence in the details below (assert no crash).
  3. Should fix. server-context.cpp:2062. Each purge saves to the cache before the retry, so under a tight --cache-ram the loop can evict its own target and still purge every idle slot. Stop the loop when the entry it wants is no longer in the cache.
  4. Should fix. server-context.cpp:4550. The decode-failure path now makes a full state save before each purge. We measured about 50 ms for 164 MiB and about 190 ms for 525 MiB. Please state this cost in the description.
  5. Should fix. In the description and commit 1, say that the gain needs --no-cache-idle-slots: with the default idle-slot caching we saw no change. "Resolves the items of the TODO" claims more than the change does. Keeping the upstream TODO text also makes upstream syncs easier.

Correctness

FAIL. With our request sequence, head aborts on Vulkan, ROCm and CPU, with HIP_LAUNCH_BLOCKING unset and set to 1. Master and the one-line fix do not crash. On your scenario, base and head give the same 16 greedy tokens for each returning conversation on all backends. The hybrid Qwen3.8-27B runs did not abort and gave the same output as a fresh server. test-backend-ops (ADD smoke only), logit parity and benchmarks do not apply: no op, graph or token path changes. The scripts/server-test-*.py scripts do not cover slots or the prompt cache.

Verification

Strix Halo (gfx1151), Vulkan (RADV) and ROCm. We compared the PR merged into master 8acea9aa4 (the PR was 381 commits behind) against master.

case (Qwen2.5-1.5B Q4_K_M unless noted) master PR merged
your scenario, A returns: prompt_n (Vulkan / ROCm / CPU) 6000 / 6000 / 6000 1 / 1 / 1
your scenario, A returns: wall time, Vulkan (3 runs) 1.351-1.360 s 0.169-0.172 s
Qwen3.8-27B, A returns: prompt_n, wall (Vulkan) 6000, 13.37 s 4, 0.644 s
eviction sequence (Vulkan, ROCm, ROCm HLB=1, CPU) no crash, prompt_n 6100 abort, 4 of 4 runs

Not tested

The server pytest suite (pytest is not available here), draft and MTP models, concurrent busy slots, and other hardware.

Thanks for the clear description and the honest list of what was not verified.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Strix Halo, Radeon 8060S (gfx1151). Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic.
  • Vulkan: Mesa 26.0.8 (RADV STRIX_HALO), KHR_coopmat.
  • ROCm: 10.1, hipconfig 7.16.26385, AMD clang 24.0.0git (ROCm/llvm-project d6f6cb69).
  • gcc 15.2.0, cmake 4.2.3, Python 3.14 (stdlib only for the driver).

Compared

  • Master tip (base): 8acea9aa4.
  • PR head: 040cdc0ee.
  • PR merged into master (local merge, not pushed): 7a8e7ac71. The merge is clean.
  • Merge base: 93e6d0285. The PR is 381 commits behind master. None of the master commits since the merge base change get_available_slot() or try_clear_idle_slots().

Build

cmake -S <tree> -B <tree>/build-vk  -DGGML_VULKAN=ON -DGGML_HIP=OFF -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake -S <tree> -B <tree>/build-hip -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R) -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake --build <tree>/build-<be> -j --target test-backend-ops llama-server llama-completion

The CPU runs use the Vulkan build with --device none.

Models

  • Qwen/Qwen2.5-1.5B-Instruct-GGUF, qwen2.5-1.5b-instruct-q4_k_m.gguf, sha256 6a1a2eb6d15622bf3c96857206351ba97e1af16c30d7a74ee38970e434e9407e.
  • Qwen3.8-27B UD-Q4_K_XL (hybrid, with checkpoints), sha256 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e. We do not have its source repo on record. No draft or MTP was enabled.

Commands

Server, one fresh process per run and per tree:

llama-server -m <model> --port <p> --device <Vulkan0|ROCm0|none> -ngl 99 -c 16384 -np 3 -kvu --no-cache-idle-slots --cache-ram <N> -lv 4
  • Your scenario: --cache-ram 2048 (8192 for the 27B model).
  • Eviction sequence: --cache-ram 300 (1800 for the 27B model).
  • Default-flags check: the same commands without --no-cache-idle-slots, and also with only -c 16384 (-np auto gives 4 slots with a unified KV).
  • ROCm runs were repeated with HIP_LAUNCH_BLOCKING=1.

The requests are sequential POST /completion calls with this body:

{"prompt": [<token ids>], "n_predict": 16, "temperature": 0, "top_k": 1, "cache_prompt": true, "id_slot": <slot or -1>, "return_tokens": true, "seed": 1}

Prompts are token-id lists made of named segments. In Python, segment NAME of length n is r = random.Random("NAME"); [r.randint(1000, 60000) for _ in range(n)]. Segments that share a name give an exact shared prefix. "Pinned" means id_slot is set; "unpinned" means id_slot = -1.

Your scenario:

  1. B = B(6000), pinned to slot 1.
  2. A = A(6000), pinned to slot 0.
  3. C = C(6000), pinned to slot 2. The pool is now full.
  4. B returns, unpinned.
  5. A returns, unpinned.

Eviction sequence (the abort):

  1. T = S(1000) + X(5000), pinned to slot 0.
  2. U = U(6000), pinned to slot 1.
  3. W = W(4000), pinned to slot 2.
  4. V = S(1000) + Y(2000), pinned to slot 0. This saves T to the cache.
  5. W + W2(2000), pinned to slot 2.
  6. S(1000) + X(5000) + Z(100), unpinned. Slot 0 is selected by prefix similarity, the restore of T fails, the loop purges slot 1 and saves U, and that save evicts T.

Self-purge variant (--cache-ram 2048):

  1. L(2000), pinned to slot 1.
  2. T(6000), pinned to slot 0.
  3. Q(3000), pinned to slot 0.
  4. P(10000), pinned to slot 2.
  5. T + Z(100), unpinned.

Fresh-server reference for the 27B model: S(1000) + X(5000) + Z(100) as the only request.

Smoke: test-backend-ops test -b <Vulkan0|ROCm0> -o ADD.

Results

Smoke, test-backend-ops -o ADD. Each log has Backend 1/2: <dev>.

tree Vulkan0 ROCm0
master 107/107 158/158
PR merged 107/107 158/158

Your scenario, Qwen2.5-1.5B, --cache-ram 2048. Columns are prompt_n / wall time; Vulkan and ROCm are 3 runs each.

run slot purged at C B returns A returns failed restores that purged
master, Vulkan 0 (A) 1 / 0.109, 0.108, 0.109 s 6000 / 1.360, 1.351, 1.356 s 0
merged, Vulkan 1 (B, LRU) 1 / 0.170, 0.173, 0.168 s 1 / 0.169, 0.172, 0.170 s 2
master, ROCm 0 1 / 0.148, 0.155, 0.157 s 6000 / 2.018, 2.121, 2.127 s 0
merged, ROCm 1 1 / 0.212, 0.213, 0.209 s 1 / 0.191, 0.186, 0.194 s 2
master, ROCm, HLB=1 0 1 / 0.154 s 6000 / 2.136 s 0
merged, ROCm, HLB=1 1 1 / 0.208 s 1 / 0.196 s 2
master, CPU 0 1 / 0.318 s 6000 / 9.171 s 0
merged, CPU 1 1 / 0.341 s 1 / 0.333 s 2
  • The 16 greedy tokens of both returning requests are the same on master and merged, and equal to each conversation's first answer, on all backends.
  • On merged, B returns about 60 ms slower than on master: the restore also saves and purges A first.

Head log, your scenario (Vulkan):

W srv  try_clear_id: purging slot 1 with 6015 tokens (least recently used)
I srv          load:  - found better prompt with f_keep = 0.998, f_sim = 1.000
E state_seq_set_data: error loading state: failed to restore kv cache
E srv          load: failed to restore state with size 172534964
W slot  prompt_load: id  1 | task -1 | failed to load prompt from cache
W srv  try_clear_id: purging slot 0 with 6015 tokens (least recently used)

Eviction sequence, Qwen2.5-1.5B, --cache-ram 300, last request:

tree Vulkan ROCm ROCm HLB=1 CPU
master prompt_n 6100, 1.413 s 6100, 2.174 s 6100, 2.190 s 6100, 9.409 s
merged abort abort abort abort
merged + ret->prompt_clear() fix (Vulkan only) prompt_n 5100, 1.300 s not run not run not run

Head log (Vulkan; ROCm and CPU show the same lines):

I srv          load:  - found better prompt with f_keep = 0.998, f_sim = 0.984
E state_seq_set_data: error loading state: failed to restore kv cache
E srv          load: failed to restore state with size 172534964
W slot  prompt_load: id  0 | task -1 | failed to load prompt from cache
W srv  try_clear_id: purging slot 1 with 6015 tokens (least recently used)
I srv   prompt_save:  - saving prompt with length 6015, total state size = 164.542 MiB
W srv         alloc:  - making room for prompt cache entry, removing oldest entry (size = 164.542 MiB)
E slot   operator(): id  0 | task 90 | n_past = 1000, slot.prompt.tokens.size() = 3015, seq_id = 0, pos_min = -1
server-context.cpp:4011: pos_min == -1, but n_past > 0 - should not happen
  • The process exits, the client gets a closed connection, and /health is then refused.
  • With the fix, the last request restores V's 1000-token shared prefix and computes 5100 tokens. Its greedy tokens equal master's. Your scenario with the fix is unchanged: 1 and 1, with 2 purges.

Self-purge variant, Vulkan, last request:

  • Master: prompt_n 6100, 1.476 s.
  • Merged: prompt_n 100, 0.204 s, with the same tokens.
  • Merged first purges the selected slot itself (purging slot 1 with 2015 tokens (least recently used)). It then tries to save an entry with 0.000 MiB of state, and the cache skips it as already present. The fix in ask 1 removes this step.

Qwen3.8-27B (hybrid), your scenario, --cache-ram 8192. Columns are prompt_n / wall time.

backend B returns, master / merged A returns, master / merged
Vulkan 4, 0.257 s / 4, 0.495 s 6000, 13.37 s / 4, 0.644 s
ROCm 4, 0.272 s / 4, 0.472 s 6000, 18.43 s / 4, 1.705 s
  • The greedy tokens are the same on master and merged.

Qwen3.8-27B, eviction sequence, --cache-ram 1800:

  • No abort on either tree. Merged takes the same failed-restore, purge and evict path, then logs forcing full prompt re-processing due to lack of cache data and computes all 6100 tokens. Wall time: Vulkan 14.714 s (master) / 14.867 s (merged); ROCm 18.931 s / 19.034 s.
  • Output against the fresh-server reference: merged on Vulkan and ROCm matches, and master on Vulkan matches. Master on ROCm differs. It takes the decode-failure purge path, and the different cell layout changes the greedy tokens. That comes from master, not from this PR.

Default flags (Vulkan), with idle-slot caching on, and also -np auto:

  • Master and merged are identical: no purges and no abort.
  • Your scenario gives prompt_n 1 and 1 on both. The eviction sequence gives 5100 on both.

Correctness

check method result
claim reproduction, base vs head your scenario on Vulkan, ROCm, CPU pass: reproduced on all
crash safety under cache eviction eviction sequence on Vulkan, ROCm, ROCm HLB=1, CPU FAIL: head aborts 4 of 4, master 0 of 4
greedy output of restored requests 16 tokens at temp 0, base vs head, and against the first pass pass: identical
hybrid model (checkpoints) 27B, your scenario and the eviction sequence, Vulkan and ROCm pass: no abort; output matches the fresh server
ROCm HIP_LAUNCH_BLOCKING unset and =1 both scenarios same result for both settings
default flags idle-slot caching on, -np auto pass: no change from master
one-line fix candidate eviction sequence and your scenario, Vulkan pass: no abort, gain kept
test-backend-ops ADD smoke, both backends pass; the full suite does not apply (no op change)
scripts/server-test-*.py read the scripts does not apply: they test tool calls and structured output in one conversation, not slots or the prompt cache
server pytest suite not run pytest is not available on this host
f32 logit parity, KLD/PPL not run does not apply: no change to the model graph, tokens or sampling
KV/state checks (test-save-load-state) not run does not apply: the diff does not touch src/

Performance

No kernel or graph change, so no llama-bench. The cost the PR adds is the state save before each purge: about 50 ms for 164 MiB (1.5B, 6015 tokens) and about 190 ms for 525 MiB (27B). It shows as the slower "B returns" times above.

Findings in full

  1. Blocking: the stale prompt after a failed restore. In server_prompt_cache::load(), prompt = std::move(...) runs only on success. The KV state read calls seq_rm() before it looks for cells, then clears the sequence and throws when there is no room. So after a failed load the slot has an empty KV sequence but still lists its tokens. Master calls prompt_clear() at once. The PR defers it to after the loop. load() returns true when no entry beats the slot's current tokens, so once the loop evicts the target entry, loaded becomes true and the clear is skipped. The launch path then computes n_past from the stale tokens and hits the abort at server-context.cpp:4011. Reproduced (see Results).
  2. Also from the stale prompt: try_clear_idle_slots() can pick ret itself, because it is idle and still lists tokens. That step frees no cells and tries to save an entry with an empty KV sequence. In our run the cache skipped that entry because the same tokens were already cached. We did not find a case that stores it. The fix in ask 1 removes this path.
  3. Should fix: the loop saves each purged slot before the retry, and that can evict the target entry. In the worst case it purges and saves every idle slot and still prefills.
  4. Should fix: the decode-failure path now saves before each purge (see Performance).
  5. Should fix: with default idle-slot caching, try_clear_idle_slots() finds no idle slot with tokens, so the change only matters with --no-cache-idle-slots.
  6. Nit: get_available_slot() breaks LRU ties with <=, and try_clear_idle_slots() uses <.

Claims

claim status
master purges slot 0 (A), the PR purges slot 1 (B, LRU) reproduced: Vulkan, ROCm, CPU
A returns: 6000 of 6000 on master, 1 of 6000 with the PR reproduced: Vulkan, ROCm, CPU
B returns: 1 of 6000 on both reproduced
restores that found the pool full and made room: 0 vs 2 reproduced
B's 16 greedy tokens are the same on both builds reproduced (A's too)
test-backend-ops and the corpus comparison do not apply agreed
-kvu off is unchanged by construction agreed by reading the code (try_clear_idle_slots() returns early); not run
production numbers on Strix Point not tested: other hardware and model

Not tested

  • The server pytest suite: pytest is not available on this host.
  • Draft and MTP models with the purge: not run.
  • Concurrent busy slots (the loop with nothing to purge that fits): not run.
  • Other hardware: not tested.
  • The fix candidate on ROCm and CPU: built and run on Vulkan only.

@dzannotti dzannotti added risk:medium Risk: medium waiting-user We asked the author for something. and removed verifying Building and running the PR to verify its claims. labels Nov 10, 2026
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 12, 2026
…om is made

From the reviews of strix halo-box#176 and halo-box#171 (2026-11-09/10).

halo-box#176: a failed prompt_load() empties the slot's sequence but left its tokens. A
retry that found nothing better then aborted at "pos_min == -1, but n_past > 0".
Clear the slot before the retry loop, and stop purging when the wanted entry has
left the cache. On our disk tier a failed restore also deleted the entry, so a
conversation that merely did not fit yet lost its entry and reached the same
abort. The entry now stays while room is made, and goes only when the restore
still fails with no other slot busy.

halo-box#171: a save that cannot write the draft appendix completely fails; the restore
counts the appendix in n_bytes.

Assisted-by: Claude Code
@rcfa

rcfa commented Nov 12, 2026

Copy link
Copy Markdown
Author

Pushed as 8641275, rebased onto master 093c552: the two commits amended, plus a test commit. Each commit builds on its own.

  1. Blocking, the stale prompt. ret->prompt_clear() now runs right after the first failed prompt_load(), before the loop. A slot can no longer list tokens for a sequence it has lost, and try_clear_idle_slots() can no longer pick ret itself.
  2. Blocking, tests. test_kv_keep_only_active.py has three new tests:
    • test_lru_purge_restores_returning_conversation: your scenario at 1/8 scale, with a small prompt_n asserted for both returning requests.
    • test_failed_restore_with_evicted_entry: your eviction sequence at 1/8 scale. It asserts that the restore failed, that the save evicted an entry, and that the server is still healthy.
    • test_failed_restore_stops_when_entry_is_evicted: covers ask 3.
  3. Should fix, the loop evicting its own target. Cache entries now have an id, and load() records the entry it failed to restore. The loop stops when that entry has left the cache.
  4. Should fix, the cost of the save. The description and commit 1 now state it, with your numbers: about 50 ms for 164 MiB and 190 ms for 525 MiB.
  5. Should fix, the claims. The description and commit 1 now say the gain needs --no-cache-idle-slots. The "resolves the TODO" sentence is gone, and the upstream TODO text is back in place.

Nit: try_clear_idle_slots() now breaks LRU ties with <=, like get_available_slot().

Verification. CPU build of llama-server (Release, gcc 16.2) on master 093c552, Strix Point host. tests/unit/test_kv_keep_only_active.py on stories260K: 5 passed.

Each new test fails without its fix:

test run against result
test_lru_purge_restores_returning_conversation master (no LRU purge) fails: slot 1 is not the one purged
test_failed_restore_with_evicted_entry this PR before the fix server aborts at pos_min == -1, but n_past > 0
test_failed_restore_stops_when_entry_is_evicted this PR without the stop condition fails: assert 3 == 2 restore attempts

Not verified: Vulkan and ROCm on this branch, draft and MTP models with the purge, concurrent busy slots, and the rest of the pytest suite (its other presets need model downloads we did not make).

Written by Claude Code (an AI agent), run on rcfa's Strix Point machine; rcfa owns it.

@dzannotti dzannotti removed the waiting-user We asked the author for something. label Nov 12, 2026
@dzannotti dzannotti assigned dzannotti and unassigned rcfa Nov 12, 2026
@dzannotti dzannotti added in-review We are reviewing. verifying Building and running the PR to verify its claims. and removed in-review We are reviewing. labels Nov 13, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

This replaces our review of 2026-11-10. All five earlier asks are addressed, and the abort is gone on Vulkan, ROCm and CPU. A maintainer approval is still needed. One more abort path remains for setups with a draft model or MTP. It comes from older code, and the new retry loop makes it reachable on the same request.

Direction

Accept after a small fix. The LRU purge and the restore that makes room work as described, and your new tests pass on our hardware. A maintainer still has to decide whether we carry this here: the code is general server code, and the open ggml-org#30970 rewrites the same functions.

Asks

  1. Should fix. tools/server/server-task.cpp:1858-1859. load() clears data.main after the target restore and before the draft restore. If the draft restore fails, the entry stays in the cache with empty target data. prompt_clear() at server-context.cpp:2062 then removes the restored target cells, and the loop loads the same entry again. A size-0 llama_state_seq_set_data_ext() returns 0, so n != size is false and the empty target restore counts as a success. If the draft restore then fits, the slot has tokens over an empty target sequence, and the server hits the same pos_min == -1 abort. Please delete those two lines; states.erase(it_best) frees the memory anyway. We found this by reading the code. Our draft-model runs did not reach the path, because the draft restore never failed.
  2. Should fix. In the description, the Measurements block (02e2c2b55 / 8b1793462) is from the head before the fixes. The production table uses --cache-dir and #149, which this fork does not have, so those numbers do not describe this branch. Please re-run the CPU table on the current head and mark the production table as a different tree, or drop it.
  3. Nit. The comment at server-context.cpp:2061 is not true for a failed draft restore (the target cells are still there). The comment block at 2064-2066 is longer than the 1-2 lines that AGENTS.md asks for. server-task.cpp uses std::any_of without including <algorithm>.

Correctness

PASS for the claims, with one open path (ask 1). On Vulkan, ROCm (HIP_LAUNCH_BLOCKING unset and =1) and CPU:

  • Your scenario purges slot 1, and both returning conversations compute 1 token. Master computes 6000 for A.
  • Our old abort sequence now restores the shared 1000-token prefix and computes 5100 tokens, with no abort.
  • Your five tests in test_kv_keep_only_active.py pass on the PR. On master the two old tests pass and the three new ones fail.
  • The greedy tokens of every returning request are the same on master and the PR.
  • Runs with a 0.5B draft model, with default idle-slot caching, and with two slots generating during a restore had no abort or error.
  • On Qwen3.8-27B (hybrid), with and without MTP, A returns with 4 tokens instead of 6000 on both GPU backends, and the old abort sequence falls back to a full prefill without an abort, as before.

The server pytest suite as a whole did not run (pytest is not installed here). We ran this file's tests with the real utils.py through a small local stand-in for pytest. Logit parity and benchmarks do not apply: no op, graph or sampling change.

Verification

Strix Halo (gfx1151), Vulkan (RADV) and ROCm, plus CPU. We compared the PR merged into master 58e69e504 (the PR was 23 commits behind; the merge is clean) against master.

case (Qwen2.5-1.5B Q4_K_M unless noted) master PR merged
your scenario, A returns: prompt_n (Vulkan / ROCm / CPU) 6000 / 6000 / 6000 1 / 1 / 1
your scenario, A returns: wall, Vulkan / ROCm 1.270 / 2.168 s 0.172 / 0.189 s
old abort sequence: prompt_n, abort (all backends) 6100, no 5100, no
your tests (stories260K), Vulkan / ROCm / CPU 2 of 5 pass 5 of 5 pass
Qwen3.8-27B, A returns: prompt_n, wall, Vulkan / ROCm 6000, 12.99 / 18.47 s 4, 0.66 / 1.74 s
Qwen3.8-27B with MTP, A returns: prompt_n, wall, Vulkan / ROCm 6000, 14.39 / 19.33 s 4, 1.10 / 1.24 s

Not tested

The full server pytest suite, a failed draft restore (ask 1), a restore that fails while other slots are still generating, and other hardware.

Thanks for the quick and careful follow-up.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Strix Halo, Radeon 8060S (gfx1151). Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic.
  • Vulkan: Mesa 26.0.8 (RADV STRIX_HALO).
  • ROCm 10.1, hipconfig 7.16.26385, AMD clang 24.0.0git (d6f6cb69).
  • gcc 15.2.0, cmake 4.2.3, Python 3.14.

Compared

  • Master tip (base): 58e69e504.
  • PR head: 8641275f1 (3 commits). Our previous review was on 040cdc0ee.
  • PR merged into master (local merge, not pushed): e295b350b. The merge is clean.
  • Merge base: 093c552d7. The PR is 23 commits behind master. Those commits change only server-decision.cpp and common/speculative.cpp in the server and common code, and neither touches slot selection or the prompt cache.
  • The diff of master against the merged tree is the same as the PR diff.

Build

cmake -S <tree> -B <tree>/build-vk  -DGGML_VULKAN=ON -DGGML_HIP=OFF -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake -S <tree> -B <tree>/build-hip -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R) -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake --build <tree>/build-<be> -j --target llama-server test-backend-ops

CPU runs use the Vulkan build with --device none.

Models

  • Qwen/Qwen2.5-1.5B-Instruct-GGUF, qwen2.5-1.5b-instruct-q4_k_m.gguf, sha256 6a1a2eb6d15622bf3c96857206351ba97e1af16c30d7a74ee38970e434e9407e.
  • Qwen/Qwen2.5-0.5B-Instruct-GGUF, qwen2.5-0.5b-instruct-fp16.gguf (draft), sha256 8e0ae26000627ed62de0e78e41860af70094558b9d2913385c842a6aa06cf3fc.
  • ggml-org/test-model-stories260K, stories260K-f32.gguf (the test preset model), sha256 270cba1bd5109f42d03350f60406024560464db173c0e387d91f0426d3bd256d.
  • Qwen3.8-27B UD-Q4_K_XL (hybrid, with an MTP head), sha256 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e.

Commands

Server, one fresh process per run and per tree:

llama-server -m <model> --port <p> --host 127.0.0.1 -ngl 99 -c 16384 -np 3 -kvu --cache-ram <N> -lv 4 --device <Vulkan0|ROCm0|none> --no-cache-idle-slots [extra]
  • Your scenario (lru) and selfpurge, busy: --cache-ram 2048 (8192 for the 27B model).
  • Old abort sequence (evict) and fresh: --cache-ram 300 (1800 for the 27B model).
  • Draft: extra -md <qwen2.5-0.5b> -ngld 99 --spec-type draft-simple. MTP (27B): extra --spec-type draft-mtp.
  • Default flags: the same commands without --no-cache-idle-slots.
  • ROCm runs were repeated with HIP_LAUNCH_BLOCKING=1.

Requests are sequential POST /completion calls, unless noted:

{"prompt": [<token ids>], "n_predict": 16, "temperature": 0, "top_k": 1, "cache_prompt": true, "id_slot": <slot or -1>, "return_tokens": true, "seed": 1}

A segment NAME of length n is r = random.Random("NAME"); [r.randint(1000, 60000) for _ in range(n)]. Segments that share a name give an exact shared prefix. "Pinned" means id_slot is set; "unpinned" means -1.

  • lru (your scenario): B(6000) slot 1, A(6000) slot 0, C(6000) slot 2, then B and A unpinned.
  • evict (the old abort): S(1000)+X(5000) slot 0; U(6000) slot 1; W(4000) slot 2; S(1000)+Y(2000) slot 0; W+W2(2000) slot 2; then S+X+Z(100) unpinned.
  • selfpurge: L(2000) slot 1; T(6000) slot 0; Q(3000) slot 0; P(10000) slot 2; then T+Z(100) unpinned.
  • busy: T(6000) slot 0; U(5000) slot 1; W(5000) slot 2; Q(1500) slot 0; then U+x(10) on slot 1 and W+x(10) on slot 2 at the same time with n_predict 300, and 1 s later T+Z(50) unpinned; after both finish, T+Z+Z2(20) unpinned.
  • fresh (27B reference): S+X+Z as the only request.

Your tests: the PR's tools/server/tests/unit/test_kv_keep_only_active.py and utils.py, unchanged, run against each build with stories260K-f32.gguf as the preset model. pytest is not installed, so a small driver imports the module, runs the create_server fixture, calls each test_* function and stops the server after each one. The GPU builds use their default device; the CPU run sets LLAMA_ARG_DEVICE=none.

Smoke: test-backend-ops test -b <Vulkan0|ROCm0> -o ADD.

Results

Smoke, test-backend-ops -o ADD: Vulkan0 107/107 and ROCm0 158/158 on both trees.

Your tests:

test master (Vulkan, ROCm, CPU) PR merged (Vulkan, ROCm, CPU)
test_clear_and_restore pass pass
test_disabled_with_flag pass pass
test_lru_purge_restores_returning_conversation fail: "purging slot 1" in log pass
test_failed_restore_with_evicted_entry fail: "removing oldest entry" in logs pass
test_failed_restore_stops_when_entry_is_evicted fail: count("failed to restore state") == 2 pass

Your scenario (lru), Qwen2.5-1.5B. Columns are prompt_n / wall time.

run purges (slot ids) B returns A returns
master, Vulkan 0, 1 1 / 0.109 s 6000 / 1.270 s
merged, Vulkan 1, 0, 2 1 / 0.190 s 1 / 0.172 s
master, ROCm 0, 1 1 / 0.155 s 6000 / 2.168 s
merged, ROCm 1, 0, 2 1 / 0.217 s 1 / 0.189 s
master, ROCm, HLB=1 0, 1 1 / 0.156 s 6000 / 2.158 s
merged, ROCm, HLB=1 1, 0, 2 1 / 0.219 s 1 / 0.203 s
master, CPU 0, 1 1 / 0.326 s 6000 / 9.175 s
merged, CPU 1, 0, 2 1 / 0.352 s 1 / 0.341 s
master, Vulkan, draft 0, 1 1 / 0.203 s 6000 / 1.913 s
merged, Vulkan, draft 1, 0, 2 1 / 0.298 s 1 / 0.241 s
master, ROCm, draft 0, 1 1 / 0.267 s 6000 / 3.080 s
merged, ROCm, draft 1, 0, 2 1 / 0.348 s 1 / 0.286 s
master, Vulkan, idle-slot caching on none 1 / 0.182 s 1 / 0.168 s
merged, Vulkan, idle-slot caching on none 1 / 0.182 s 1 / 0.166 s

On merged, each returning request first fails one restore, purges one slot and saves it, then restores (2 failed restores per run). B returns 60-130 ms slower than on master because of that save.

Old abort sequence (evict), last request, prompt_n / wall:

run master merged
Vulkan 6100 / 1.443 s 5100 / 1.275 s
ROCm 6100 / 2.194 s 5100 / 1.900 s
ROCm, HLB=1 6100 / 2.202 s 5100 / 1.907 s
CPU 6100 / 9.488 s 5100 / 8.172 s
Vulkan, draft 5100 / 1.748 s 5100 / 1.837 s
ROCm, draft 5100 / 2.648 s 5100 / 2.701 s
Vulkan, idle-slot caching on 5100 / 1.229 s 5100 / 1.225 s

On merged the restore of T fails, the loop purges slot 1 and saves U, that save evicts T, and the next load restores the 1000-token shared prefix. No run aborted, and /health returned 200 after every run.

selfpurge, last request: master 6100 tokens; merged 100 tokens (Vulkan 0.227 s, ROCm 0.267 s, HLB=1 0.279 s). Merged now purges slot 0, not the selected slot, and makes no empty-state save.

busy: both trees restored T+Z with 50 tokens while the two other slots were generating, so this run did not force a failed restore during generation. After both finished, T+Z+Z2 computed 6070 tokens on master and 20 on merged (one failed restore, one purge). No errors on either tree.

Greedy tokens: the 16 tokens of every returning request (B and A in lru, on every backend and setting) are the same on master and merged, and equal to each conversation's first answer. Three requests that are computed differently on the two trees give different tokens: C in lru on CPU and with the Vulkan draft (C's prefill runs after a different purge, so the cell layout differs), and the last evict request on CPU (5100 vs 6100 computed tokens). Their prompts are random token ids, so greedy output depends on the batch shape and cell layout. We already saw this on master.

Qwen3.8-27B (hybrid), your scenario, --cache-ram 8192. Columns are prompt_n / wall time.

run B returns, master / merged A returns, master / merged
Vulkan 4, 0.259 s / 4, 0.526 s 6000, 12.990 s / 4, 0.659 s
ROCm 4, 0.268 s / 4, 0.511 s 6000, 18.472 s / 4, 1.738 s
Vulkan, MTP 4, 0.307 s / 4, 0.563 s 6000, 14.387 s / 4, 1.103 s
ROCm, MTP 4, 0.795 s / 4, 1.011 s 6000, 19.333 s / 4, 1.238 s
  • With MTP, each saved entry also holds the draft state (about 23.6 MiB of about 549 MiB). Every failed restore in these runs was the target restore; no draft restore failed.
  • The greedy tokens of both returning requests are the same on master and merged, and equal to each conversation's first answer, with and without MTP.

Qwen3.8-27B, old abort sequence, --cache-ram 1800: no abort on either tree. Merged takes the failed restore, purge and evict path, then logs forcing full prompt re-processing due to lack of cache data and computes all 6100 tokens. Wall time: Vulkan 14.362 s (master) / 14.619 s (merged); ROCm 18.968 s / 19.148 s. Against a fresh server that gets only the last request, merged matches on Vulkan and ROCm, and master matches on Vulkan. Master on ROCm differs, as in our earlier review: it takes the decode-failure purge path, and the different cell layout changes the greedy tokens.

Correctness

check method result
claim reproduction, base vs head your scenario on Vulkan, ROCm, ROCm HLB=1, CPU pass
earlier ask 1 (abort) old abort sequence on all backends pass: no abort, 5100 tokens
earlier ask 2 (tests) your file, run with the real utils.py on Vulkan, ROCm, CPU pass: 5/5 on merged, 2/5 on master
earlier ask 3 (loop stops) test 3, and the evict log pass
earlier asks 4, 5 (description) read the description and commit 1 done
greedy output of returning requests 16 tokens at temp 0, base vs head pass: identical
draft model (draft-simple) lru and evict, Vulkan and ROCm pass: no abort; the failed-draft-restore path was not reached
MTP and hybrid model Qwen3.8-27B, lru with and without MTP, evict, Vulkan and ROCm pass: no abort; returning requests 6000 to 4 tokens; output matches
ROCm HIP_LAUNCH_BLOCKING unset and =1 all small-model scenarios same results
default flags idle-slot caching on pass: same as master
busy slots two slots generating during a restore pass: no error; a failed restore during generation was not forced
test-backend-ops ADD smoke, both backends pass
server pytest suite not run pytest is not installed
scripts/server-test-*.py not run they do not cover slots or the prompt cache
logit parity, KLD/PPL, benchmarks not run do not apply: no graph, token or sampling change

Findings in full

  1. Should fix: the half-consumed cache entry. server_prompt_cache::load() restores data.main, then runs data.clear() and shrink_to_fit() (server-task.cpp:1858-1859), then restores data.drft. When the draft restore fails, load() sets id_failed and returns false, and the entry stays in the cache with its tokens and an empty data.main. In get_available_slot(), prompt_clear() removes the target cells that the first restore wrote. contains(id_wanted) is still true, so the loop purges a slot and loads again. The same entry is again the best match. state_seq_set_data() with size 0 fails at the magic read, catches the error and returns 0, so n != size is false. The target restore is skipped, the draft restore can now fit, and prompt = std::move(it_best->prompt) gives the slot tokens over an empty target sequence. That is the pos_min == -1, but n_past > 0 abort. Master has the same data.clear() (as does ggml-org), but there the empty entry waits for a later request, and a later save of the same slot usually replaces it. The draft context has the same n_ctx as the target, so this needs a draft pool with fewer free cells than the target pool. That is narrow, but your own setup (MTP, -kvu, a pool that cannot hold all conversations) is the closest case. Deleting the two lines fixes it; states.erase(it_best) frees the memory on success. We did not reproduce it at runtime.
  2. Should fix: the description's measurements are from an older head, and the production table is from a tree with --cache-dir and #149 (closed, not merged).
  3. Nit: comment accuracy and length at server-context.cpp:2061-2066; <algorithm> is not included for std::any_of in server-task.cpp.
  4. Note: test 3 has small margins (the cache size and the slot similarity threshold). A change in the KV state format or in the --cache-ram accounting after an upstream sync can break it. A comment with the per-token state size you assumed would help the next person who debugs it.
  5. Note: each loop step makes a full synchronous save of an idle slot, up to n_slots - 1 times, while other slots wait. If the entry does not fit next to the busy slots, all idle slots are purged and the request is still prefilled.
  6. Upstream: ggml-org master still has the old try_clear_idle_slots() and get_available_slot(). The open ggml-org#30970 splits try_clear_idle_slots() into try_clear_idle_slot(bool shared) and replaces prompt_load(cache, tokens) with find() plus load(iterator). If it merges, both hunks of this PR conflict at the next sync.

Claims

claim status
master purges slot 0 (A), the PR purges slot 1 (B, LRU) reproduced: Vulkan, ROCm, CPU
A returns: 6000 of 6000 on master, 1 of 6000 with the PR reproduced: Vulkan, ROCm, CPU
B returns: 1 of 6000 on both reproduced
restores that found the pool full and made room: 0 vs 2 reproduced (2 failed restores, each followed by one purge)
B's 16 greedy tokens are the same on both builds reproduced (A's too)
the gain needs --no-cache-idle-slots reproduced: no change with idle-slot caching on
each new test fails without its fix reproduced for master (all three fail); the two partial-fix variants were not rebuilt
-kvu off is unchanged by construction agreed by reading the code; not run
production numbers (Strix Point, --cache-dir) not tested: other hardware, and the option is not in this fork

Not tested

  • The full server pytest suite: pytest is not installed.
  • A failed draft restore (ask 1): our draft runs never failed a draft restore.
  • A restore that fails while other slots are still generating.
  • Non-unified KV (-kvu off).
  • Other hardware.

@dzannotti dzannotti added needs-human Review done; a maintainer must approve. verified The claims were reproduced. and removed verifying Building and running the PR to verify its claims. labels Nov 13, 2026
@dzannotti

Copy link
Copy Markdown
Collaborator

@rcfa sorry last 2 fixes then ready to merge!

@dzannotti dzannotti added the waiting-user We asked the author for something. label Nov 13, 2026
@dzannotti dzannotti assigned rcfa and unassigned dzannotti Nov 13, 2026
@dzannotti dzannotti removed the needs-human Review done; a maintainer must approve. label Nov 13, 2026
@dzannotti
dzannotti dismissed their stale review November 13, 2026 12:47

The earlier requested changes are done. See the latest review for the remaining small fix.

@rcfa
rcfa force-pushed the pr/lru-purge branch 2 times, most recently from 03340a7 to e305bb8 Compare November 16, 2026 01:13
@rcfa

rcfa commented Nov 16, 2026

Copy link
Copy Markdown
Author

Pushed as e305bb838 on master 093c552d7: the two fixes, the three nits and a comment for your note 4. The test
commit is amended, so this is a force-push over 03340a7a7.

  1. Should fix, the half-consumed cache entry. The two lines are gone from the target block. A failed draft restore
    now leaves the entry whole, so the retry finds the real target state instead of an empty one that reports success.
    You found it by reading; we did not reach it either. The same two lines are still in the draft block, where they
    run only after a successful draft restore and states.erase(it_best) follows two lines later - upstream's code,
    and dead rather than harmful. Say the word if you want them gone too.
  2. Should fix, the measurements. Re-run below, on the current head against the merge base, and the production
    table is now marked as a different tree (your point stands: it has server: prompt cache disk spill #149 and --cache-dir, which this fork does
    not).
  3. Nit. The comment at server-context.cpp:2061 now says that a failed restore leaves the sequence in an unknown
    state, which is true for both kinds. The block below it is two lines. <algorithm> is included.
  4. Note, test 3's margins. The test now carries the number it depends on: this model keeps about 652 B of state
    per token (n_layer 5, n_embd_k_gqa 32; measured 588,908 B for a 903-token entry), so the saves of t, x and v
    fill 0.967 MiB of the 1 MiB cache, and the 103-token save that makes room for t tips it over and evicts t.
  5. Note, what the loop costs. Agreed, and it is now in the description: one synchronous save per step, up to
    n_slots - 1 of them while the other slots wait, and if the entry still does not fit next to the busy slots,
    every idle slot has been purged and the request is prefilled anyway.
  6. Upstream server : share prompt prefix cells between slots (--slot-attach) ggml-org/llama.cpp#30970. Noted. If it merges first we rebase onto its shape -
    try_clear_idle_slot(bool shared) and find() plus load(iterator) - rather than keeping these hunks.

Ask 2: the CPU table, re-run on this head

Device:   Ryzen AI 9 HX PRO 370 / Radeon 890M (gfx1150, Strix Point - NOT Strix Halo), 96 GB DDR5-5600 ECC
Kernel:   7.2.15+kali-amd64. CPU-only builds, Release, gcc 16.2.0, from one tree; both commits checked with --version
Base:     093c552d7 (this branch's merge base)   Head: e305bb838
Server:   --device none -m Qwen2.5-1.5B-Instruct-Q4_K_M -c 16384 -np 3 -kvu --cache-ram 2048 --no-cache-idle-slots -t 8

B to slot 1, A to slot 0, C to slot 2 (3 x 6000 tokens do not fit), then B and A unpinned:

master this PR
slot purged when the pool filled 0 (A, the most recently used) 1 (B, the least recently used)
B comes back: tokens computed 1 of 6000 1 of 6000
A comes back: tokens computed 6000 of 6000 1 of 6000
restores that first found the pool full and made room 0 2
purges over the whole run slots 0, 1 slots 1, 0, 2

B's 16 greedy tokens are the same on both builds. The failed restores are 164.5 MiB each (6015 tokens, 28.0 KiB per
token) - the same state size your Strix Halo run saved in about 50 ms. The purge order and the counts match your
lru rows, so this is your table on a second machine rather than a new claim.

Verification

tests/unit/test_kv_keep_only_active.py under pytest 9.1.1 on CPU, against both builds:

base 093c552 head e305bb8
test_clear_and_restore, test_disabled_with_flag pass pass
the three new tests fail pass

5 of 5 on the head, 2 of 5 on the base - the same split you got through your pytest stand-in. (The runs need
PORT=<free port> here: on the default 8080 the requests reach this host's nginx instead of the test server.)

Not verified

A draft restore that fails for lack of room (ask 1's path). It needs a draft pool with fewer free cells than the
target pool, which we could not arrange either: there is no second small model on this host, and using the target as
its own draft gives the two pools the same size. The fix is by construction - the entry is no longer emptied. Also
not verified on this head: Vulkan and ROCm, the rest of the pytest suite, concurrent busy slots, and -kvu off.

Written by Claude Code (an AI agent), run on rcfa's Strix Point machine; rcfa owns it.

@dzannotti dzannotti added in-review We are reviewing. and removed waiting-user We asked the author for something. labels Nov 18, 2026
rcfa added 3 commits November 18, 2026 10:25
…rompt cache first

When the unified KV pool is full, try_clear_idle_slots() cleared the idle slot
with the lowest id and discarded its context. Pick the idle slot with the
oldest t_last_used instead, and save it to the prompt cache before clearing
it, as get_available_slot() already does. A conversation that comes back is
then restored from the cache instead of being prefilled again.

The gain needs --no-cache-idle-slots. With the default idle-slot caching the
idle slots are saved and cleared when a task arrives, so this function finds
none to purge. The save also runs on the decode-failure path; it costs about
50 ms for 164 MiB of state and 190 ms for 525 MiB.

Assisted-by: Claude Code
…e prompt cache

get_available_slot() restores a returning conversation from the prompt cache,
but when the unified KV pool is full the restore finds no free cells ("failed
to find N available cells in kv cache"). The slot is then cleared and the
conversation is prefilled again, although its state is in the cache.

Clear the slot as soon as the restore has failed, because a failed restore
leaves its sequence in an unknown state. Then purge idle slots, least recently
used first and each saved to the cache, until the restore fits. Those saves can
evict the entry that is to be restored, so the purging stops when it has left
the cache.

The retry also makes an older hazard reachable. load() freed the target state
after it was restored and before it restored the draft state, so a failed draft
restore left the entry in the cache with no target data. The retry picked the
same entry, a size-0 restore reported success, and the slot took the tokens of
an empty sequence - the "pos_min == -1, but n_past > 0" abort. Keep the target
state until states.erase() frees it; its buffer then lives through the draft
restore.

Assisted-by: Claude Code
@dzannotti dzannotti added verifying Building and running the PR to verify its claims. and removed in-review We are reviewing. labels Nov 18, 2026

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

This replaces our review of 2026-11-13. You addressed both should-fix items and the nits, and the PR passes our checks on Vulkan, ROCm and CPU after a rebase onto the current master. One new test fails in the Windows server CI, so we cannot merge yet. The fix is one line.

Direction

Accept. The LRU purge and the restore that makes room work as described, also on the new upstream slot scheduler. We will merge when the Windows server job passes.

Asks

  1. Blocking. tools/server/tests/unit/test_kv_keep_only_active.py:14. LogReader.drain() opens the server log with the platform encoding. On Windows that is cp1252, and test_failed_restore_with_evicted_entry fails with UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d (the Server / windows job; 465 other tests pass, and the job passes on master). The debug log of the random-token prompts has bytes that cp1252 cannot decode. Please open the file with encoding="utf-8", errors="replace".
  2. We rebased and force-pushed your branch onto master f5e7a5dc8 (details below). Before you push the fix, run git fetch && git reset --hard origin/pr/lru-purge.

Correctness

PASS on Vulkan, ROCm (HIP_LAUNCH_BLOCKING unset and =1) and CPU on Linux, master vs the rebased PR. The Windows CI failure is ask 1.

  • Your scenario: master recomputes 6000 tokens for A, the PR 1 token. Your five tests: 5 of 5 on the PR, 2 of 5 on master.
  • The old abort sequence, the self-purge case, two busy slots, a draft model, idle-slot caching on, and a new case with n_cmpl 2 (the loop can now purge a child slot that the scheduler already picked) ran with no abort or error. The child slot is saved first and then gets the parent state, so both completions are correct.
  • Qwen3.8-27B (hybrid), with and without MTP: A returns with 4 tokens instead of 6000 on both GPU backends.
  • Greedy tokens of returning requests match master, except where the PR computes fewer tokens (CPU, old abort sequence) and one 27B ROCm case. There the first token is a near-tie (logprob gap 0.027), and master shows the same change after any prompt-cache restore (idle-slot caching on), so it does not come from this PR.

The failed draft restore path (our earlier ask 1) did not occur in any run; the fix is checked by reading the code. The server pytest suite did not run, because pytest is not installed here. Logit parity and benchmarks do not apply.

Verification

Strix Halo (gfx1151), Vulkan (RADV) and ROCm, plus CPU. The PR was 1169 commits behind master. Upstream ggml-org#32154 moved the prompt cache into server-slot-sched.{h,cpp} and the cache update into update_prompt_cache(), so commit 2 conflicted. We moved its hunks to the new locations without other changes. All numbers are for the rebased head 97790cf37 against master f5e7a5dc8.

case (Qwen2.5-1.5B Q4_K_M unless noted) master this PR, rebased
your scenario, A returns: prompt_n (Vulkan / ROCm / CPU) 6000 / 6000 / 6000 1 / 1 / 1
your scenario, A returns: wall, Vulkan / ROCm / CPU 1.198 / 1.964 / 8.759 s 0.179 / 0.188 / 0.388 s
old abort sequence: prompt_n, abort 6100, no 5100, no
n_cmpl 2 after a full pool, A returns: prompt_n per completion (Vulkan, ROCm) 6000 / 6000 1 / 1
your tests (stories260K), Vulkan / ROCm / CPU 2 of 5 5 of 5
Qwen3.8-27B, A returns: prompt_n, wall Vulkan / ROCm 6000, 13.16 / 18.49 s 4, 0.61 / 1.62 s
Qwen3.8-27B with MTP, A returns: prompt_n, wall Vulkan / ROCm 6000, 14.61 / 19.36 s 4, 1.07 / 1.10 s

Not tested

The full server pytest suite, a failed draft restore, a restore that fails while other slots generate, -kvu off, and other hardware.

Thanks for the careful follow-up.

Review made with AI on behalf of the strix-llama.cpp maintainers.

Full verification (for reproduction and agents)

Environment

  • Strix Halo, Radeon 8060S (gfx1151). Ubuntu 26.04.1 LTS, kernel 7.0.0-43-generic.
  • Vulkan: Mesa 26.0.8 (RADV STRIX_HALO).
  • ROCm 10.1, hipconfig 7.16.26385, AMD clang 24.0.0git (d6f6cb69).
  • gcc 15.2.0, cmake 4.2.3, ninja 1.13.2, Python 3.14.

Compared

  • Master tip (base): f5e7a5dc8.
  • PR head before the rebase: e305bb838 (3 commits, merge base 093c552d7, 1169 commits behind master). Our previous review was on 8641275f1.
  • Rebased PR head (pushed): 97790cf37 (commits 6eeb39e81, 78c54c068, 97790cf37).

Rebase

  • Commit 1 (try_clear_idle_slots() LRU) and commit 3 (tests) applied without conflicts.
  • Commit 2 conflicted in server-context.cpp, server-task.cpp and server-task.h. Upstream ggml-org#32154 replaced get_available_slot() with server_slot_sched::select() plus server_context::update_prompt_cache(), and moved server_prompt_cache to server-slot-sched.{h,cpp}. Resolution:
    • server-task.{h,cpp}: kept master. The PR's hunks went to the new files: id in server_prompt_cache_state, id_last, id_failed and contains() in server_prompt_cache (server-slot-sched.h); /*.id =*/ ++id_last in alloc(), id_failed = it_best->id in both failure branches of load(), removal of data.clear(); data.shrink_to_fit(); from the target block of load(), and contains() (server-slot-sched.cpp).
    • server-context.cpp: kept master. The retry loop went into update_prompt_cache() with ret-> renamed to slot.; no other change.
    • The #include <algorithm> was dropped: server-slot-sched.cpp already includes it.
  • The author and the commit messages are unchanged. The messages still name get_available_slot(), which master no longer has.
  • Range-diff, in short (commit 2, file moves only):
1:  0e9eef645 = 1:  6eeb39e81 server : purge the least recently used idle slot and save it to the prompt cache first
2:  a30af2c58 ! 2:  78c54c068 server : make room in a full unified KV pool before restoring from the prompt cache
    server-task.{h,cpp} hunks -> server-slot-sched.{h,cpp}; get_available_slot() hunk -> update_prompt_cache()
3:  e305bb838 = 3:  97790cf37 tests : the LRU purge and a failed restore whose cache entry is evicted
  • Pushed with a lease on e305bb838.

Build

cmake -S <tree> -B <tree>/build-vk  -DGGML_VULKAN=ON -DGGML_HIP=OFF -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake -S <tree> -B <tree>/build-hip -DGGML_HIP=ON -DGGML_VULKAN=OFF -DGPU_TARGETS=gfx1151 -DCMAKE_HIP_COMPILER=$(hipconfig -l)/clang -DCMAKE_PREFIX_PATH=$(hipconfig -R) -DCMAKE_BUILD_TYPE=Release -G Ninja
cmake --build <tree>/build-<be> -j --target llama-server test-backend-ops

CPU runs use the Vulkan build with --device none.

Models

  • Qwen/Qwen2.5-1.5B-Instruct-GGUF, qwen2.5-1.5b-instruct-q4_k_m.gguf, sha256 6a1a2eb6d15622bf3c96857206351ba97e1af16c30d7a74ee38970e434e9407e.
  • Qwen/Qwen2.5-0.5B-Instruct-GGUF, qwen2.5-0.5b-instruct-fp16.gguf (draft), sha256 8e0ae26000627ed62de0e78e41860af70094558b9d2913385c842a6aa06cf3fc.
  • test-model-stories260K, stories260K-f32.gguf (the test preset model), sha256 270cba1bd5109f42d03350f60406024560464db173c0e387d91f0426d3bd256d.
  • Qwen3.8-27B UD-Q4_K_XL (hybrid, with an MTP head), sha256 3f227079003add2511437e5b1e94812e363385225bf6a9b47b0054a72bc8b01e.

Commands

Server, one fresh process per run and per tree:

llama-server -m <model> --port <p> --host 127.0.0.1 -ngl 99 -c 16384 -np 3 -kvu --cache-ram <N> -lv 4 --device <Vulkan0|ROCm0|none> --no-cache-idle-slots [extra]
  • lru, selfpurge, busy, par: --cache-ram 2048 (8192 for 27B). evict: --cache-ram 300 (1800 for 27B).
  • Draft: extra -md <qwen2.5-0.5b> -ngld 99 --spec-type draft-simple. MTP: --spec-type draft-mtp. Idle-slot caching on: without --no-cache-idle-slots. par4: -np 4.
  • ROCm runs were repeated with HIP_LAUNCH_BLOCKING=1.

Requests are sequential POST /completion calls, unless noted:

{"prompt": [<token ids>], "n_predict": 16, "temperature": 0, "top_k": 1, "cache_prompt": true, "id_slot": <slot or -1>, "return_tokens": true, "seed": 1}

A segment NAME of length n is r = random.Random("NAME"); [r.randint(1000, 60000) for _ in range(n)].

  • lru (your scenario): B(6000) slot 1, A(6000) slot 0, C(6000) slot 2, then B and A unpinned.
  • evict: S(1000)+X(5000) slot 0; U(6000) slot 1; W(4000) slot 2; S+Y(2000) slot 0; W+W2(2000) slot 2; then S+X+Z(100) unpinned.
  • selfpurge: L(2000) slot 1; T(6000) slot 0; Q(3000) slot 0; P(10000) slot 2; then T+Z(100) unpinned.
  • busy: T(6000) slot 0; U(5000) slot 1; W(5000) slot 2; Q(1500) slot 0; then U+x(10) and W+x(10) at the same time with n_predict 300, and 1 s later T+Z(50) unpinned; then T+Z+Z2(20) unpinned.
  • par (new): B(6000) slot 1, A(6000) slot 0, C(6000) slot 2, then B and A unpinned with "n_cmpl": 2, then B and A unpinned with one completion.
  • fresh: S+X+Z as the only request (27B reference).

Your tests: the PR's test_kv_keep_only_active.py and utils.py, run against each build with the local stories260K-f32.gguf as the preset model. pytest is not installed, so a small driver imports the module, runs the create_server fixture, calls each test_* function and stops the server after each one.

Smoke: test-backend-ops test -b <Vulkan0|ROCm0> -o ADD.

Results

Smoke: Vulkan0 118/118 and ROCm0 169/169 on both trees.

Your tests, on Vulkan, ROCm and CPU:

test master PR rebased
test_clear_and_restore pass pass
test_disabled_with_flag pass pass
test_lru_purge_restores_returning_conversation fail: "purging slot 1" in log pass
test_failed_restore_with_evicted_entry fail: "removing oldest entry" in logs pass
test_failed_restore_stops_when_entry_is_evicted fail: count("failed to restore state") == 2 pass

Qwen2.5-1.5B, returning requests, prompt_n / wall time. Purges are slot ids in order.

backend run master PR rebased
Vulkan lru: purges; B; A 0,1; 1 / 0.112 s; 6000 / 1.198 s 1,0,2; 1 / 0.185 s; 1 / 0.179 s
ROCm lru 0,1; 1 / 0.147 s; 6000 / 1.964 s 1,0,2; 1 / 0.205 s; 1 / 0.188 s
CPU lru 0,1; 1 / 0.323 s; 6000 / 8.759 s 1,0,2; 1 / 0.389 s; 1 / 0.388 s
ROCm lru, draft 0,1; 1 / 0.258 s; 6000 / 2.996 s 1,0,2; 1 / 0.339 s; 1 / 0.261 s
CPU lru, draft 0,1; 1 / 0.554 s; 6000 / 12.724 s 1,0,2; 1 / 0.655 s; 1 / 0.507 s
ROCm, CPU lru, idle-slot caching on no purges; 1; 1 no purges; 1; 1
Vulkan evict: last request 6100 / 1.255 s 5100 / 1.172 s
ROCm evict 6100 / 2.163 s 5100 / 1.872 s
CPU evict 6100 / 9.079 s 5100 / 8.009 s
ROCm, CPU evict, draft 5100 5100
ROCm selfpurge: last request 6100 / 2.140 s 100 / 0.255 s
CPU selfpurge 6100 / 9.032 s 100 / 0.790 s
ROCm busy: T+Z; T+Z+Z2 50; 6070 / 2.413 s 50; 20 / 0.522 s
CPU busy 50; 6070 / 8.921 s 50; 20 / 0.482 s
Vulkan par: B n_cmpl 2; A n_cmpl 2; B; A 1/1; 6000/6000 / 1.332 s; 6000 / 1.233 s; 1 1/1; 1/1 / 0.206 s; 1 / 0.112 s; 1 / 0.111 s
ROCm par 1/1; 6000/6000 / 2.203 s; 6000 / 1.882 s; 1 1/1; 1/1 / 0.221 s; 1 / 0.150 s; 1 / 0.147 s

The Vulkan rows for selfpurge, busy, draft and idle-slot caching, and all ROCm rows with HIP_LAUNCH_BLOCKING=1, give the same token counts as the rows above. All 88 runs ended with /health 200, and no request failed.

In par with -np 3 the scheduler picks an idle slot with tokens as the child, and the PR's loop purges and saves that child before the restore. The child is then launched empty and gets the parent state. Both completions compute 1 token, are equal to each other, and equal the conversation's first answer. With -np 4 the child is an empty slot and the loop purges another idle slot.

Qwen3.8-27B, lru, A returns (B returns with 4 tokens on both trees):

run master PR rebased
Vulkan 6000, 13.160 s 4, 0.606 s
ROCm 6000, 18.487 s 4, 1.623 s
Vulkan, MTP 6000, 14.609 s 4, 1.067 s
ROCm, MTP 6000, 19.361 s 4, 1.096 s
  • With MTP, every failed restore was the target restore; no draft restore failed.
  • evict at --cache-ram 1800: no abort, 6100 tokens on both trees (Vulkan 14.53 / 14.67 s, ROCm 18.94 / 19.06 s), and the output equals the fresh reference on both backends.

Greedy tokens:

  • 1.5B: every returning request equals its conversation's first answer and master's output, on every backend and setting. The one exception is the last evict request on CPU, which the PR computes from a 1000-token restored prefix (5100 tokens) and master from scratch (6100), as in our earlier review.
  • 27B: the same on Vulkan and with MTP on ROCm. On ROCm without MTP, B's returning answer starts with token 220 instead of 62 on the PR (3 of 3 runs). The fresh logprobs are -7.2712 (62) and -7.2979 (220). Master with idle-slot caching on also restores B from the prompt cache and gives the same 220 (-7.2688 vs -7.3175). So a prompt-cache restore on ROCm moves this near-tie on master too.
  • CPU busy: the 300-token U+x generation, which runs while another request restores, differs from master from token 3 in 2 of 3 runs (master is stable 3 of 3). The PR saves a slot during the decode-failure purge, which changes the batch timing. The prompt is random token ids, and this is not a returning request.

Correctness

check method result
claim reproduction, base vs head your scenario on Vulkan, ROCm, ROCm HLB=1, CPU pass
earlier should-fix 1 (half-consumed entry) code read; no run reached a failed draft restore fixed in the code
earlier should-fix 2 (description numbers) read the description done
earlier nits code read done
your tests stand-in driver, Vulkan, ROCm, CPU pass: 5/5 PR, 2/5 master
parent task with child slots (new master scheduler) par, par4 on Vulkan and ROCm pass
draft model, MTP, hybrid model lru, evict pass: no abort
idle-slot caching on lru, evict pass: same as master
busy slots busy pass: no error; a failed restore during generation was not forced
greedy output of returning requests 16 tokens at temp 0 pass, one ROCm near-tie that master shows too
test-backend-ops ADD smoke, both backends pass
server pytest suite not run pytest is not installed
logit parity, KLD/PPL, benchmarks not run do not apply: no graph, token or sampling change

Findings in full

  1. Blocking: the Windows CI failure in ask 1. From the job log: FAILED unit/test_kv_keep_only_active.py::test_failed_restore_with_evicted_entry - UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 200570: character maps to <undefined>, raised in encodings/cp1252.py from LogReader.drain(). 1 failed, 465 passed, 8 skipped. The same job passes on master f5e7a5dc8.
  2. Optional, not needed for merge: the commit messages still name get_available_slot(), and the data.main change in load() fixes a latent upstream issue that could also go to ggml-org.

Claims

claim status
master purges slot 0 (A), the PR purges slot 1 (B, LRU) reproduced: Vulkan, ROCm, CPU
A returns: 6000 of 6000 on master, 1 of 6000 with the PR reproduced: Vulkan, ROCm, CPU
B returns: 1 of 6000 on both reproduced
restores that found the pool full and made room: 0 vs 2 reproduced
B's greedy tokens are the same on both builds reproduced for 1.5B on all backends and 27B on Vulkan; 27B ROCm differs on a near-tie that master shows too
the gain needs --no-cache-idle-slots reproduced
tests: 5/5 on the PR, 2/5 on master reproduced
-kvu off is unchanged by reading the code; not run
measurements on other hardware not tested

Not tested

  • The full server pytest suite: pytest is not installed.
  • A failed draft restore: no run made one fail.
  • A restore that fails while other slots are still generating.
  • -kvu off, par on CPU, and other hardware.

@dzannotti dzannotti added waiting-user We asked the author for something. and removed verifying Building and running the PR to verify its claims. labels Nov 18, 2026
On Windows the default encoding is cp1252, and the debug log of the
random-token prompts has bytes it cannot decode, so
test_failed_restore_with_evicted_entry failed with UnicodeDecodeError.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01YKmjzcT1LTvKtbw9EzBVGE

@dzannotti dzannotti left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verdict

This replaces our review from earlier today. We approve and merge. The only open item was the Windows server CI failure, and we fixed it ourselves.

Direction

Accept.

Asks

None. We pushed one commit to your branch, a35a35361 (tests : read the server log as UTF-8 in test_kv_keep_only_active). LogReader.drain() now opens the log with encoding="utf-8", errors="replace", and Server / windows passes with it. Our earlier review has the verification of the rebased code. The new commit changes only that one test line.

Thanks for the work on this.

Review made with AI on behalf of the strix-llama.cpp maintainers.

@dzannotti
dzannotti merged commit f2230a8 into halo-box:master Nov 18, 2026
13 of 16 checks passed
@dzannotti dzannotti removed the waiting-user We asked the author for something. label Nov 18, 2026
@dzannotti dzannotti self-assigned this Nov 18, 2026
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 19, 2026
…ot choice onto server_slot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- --ctx-checkpoints-dir (context checkpoints in unlinked files, read back only to rewind; a failed read
  falls back to reprocessing; the draft data test-loaded by size_dft())
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py, test_slot_save.py and test_kv_keep_only_active.py, 39
passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 20, 2026
…ot choice onto server_slot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- --ctx-checkpoints-dir (context checkpoints in unlinked files, read back only to rewind; a failed read
  falls back to reprocessing; the draft data test-loaded by size_dft())
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py, test_slot_save.py and test_kv_keep_only_active.py, 39
passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 26, 2026
…ot choice onto server_slot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- --ctx-checkpoints-dir (context checkpoints in unlinked files, read back only to rewind; a failed read
  falls back to reprocessing; the draft data test-loaded by size_dft())
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py, test_slot_save.py and test_kv_keep_only_active.py, 39
passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Nov 27, 2026
…lot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

--ctx-checkpoints-dir is upstream's (halo-box#180 as merged, ebe8231, with --ctx-checkpoints-ram). The context
checkpoints that a load builds from a disk entry are new objects, so the prompt cache hands them to the
server (restored_from_disk), which passes them to hold_checkpoint() like the ones a task creates: the
checkpoint budget decides for them too. The earlier port wrote each of them to the directory at once.

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py (with a new test: restored checkpoints follow
--ctx-checkpoints-ram, which fails on the earlier port), test_slot_save.py and test_kv_keep_only_active.py,
44 passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
@rcfa
rcfa deleted the pr/lru-purge branch November 27, 2026 19:19
rcfa added a commit to rcfa/strix-llama.cpp that referenced this pull request Dec 4, 2026
…lot_sched

ggml-org ggml-org#32154 moved the prompt cache out of server-task.cpp into server-slot-sched.{h,cpp} and the slot
choice out of get_available_slot() into server_slot_sched::select(); ggml-org#30137 shares context checkpoints as
std::shared_ptr<const common_prompt_checkpoint>. Our server stack cannot be replayed onto that, so it is
ported here in one piece, with the behaviour unchanged:

- the persistent prompt cache (--cache-dir, --cache-dir-max: memory-mapped entries with a metadata sidecar,
  owner-only files, state staged through host memory, restore at start, the key by state format and
  without the batch sizes), now on server_prompt_cache in server-slot-sched.cpp
- the idle slots saved to it on exit (server_context::save_slots_to_cache)
- update_prompt_cache(), on top of halo-box#176's purge loop (merged as f2230a8): the load passes the
  checkpoint and similarity arguments, and an entry that fails to restore on an empty pool is discarded
- select(): conversation_id first, slot_match_skip in the similarity, --slot-keep-min; upstream's LRU
  (empty slots first) is kept
- the checkpoint threshold for disk entries now follows server_prompt::get_reuse()

--ctx-checkpoints-dir is upstream's (halo-box#180 as merged, ebe8231, with --ctx-checkpoints-ram). The context
checkpoints that a load builds from a disk entry are new objects, so the prompt cache hands them to the
server (restored_from_disk), which passes them to hold_checkpoint() like the ones a task creates: the
checkpoint budget decides for them too. The earlier port wrote each of them to the directory at once.

The slot file's draft appendix is upstream's (same SDFT magic and version as our halo-box#171, but with the
speculative state as a second buffer), so slot files written by the previous build restore without
their draft state once.

Ports, in their original order: 0d1c648, 6cb2d84, 22ba4d2 (Daniele Zannotti), 19aa31c,
9c6f7d7, c03c65b, 1c4eaac, c722f60, a4e4da8, 6d4cce5, ec3d0fd, d544de2, cf99aa9,
a707cc8. Dropped: 12837c0 and f1ee218 (ggml-org ggml-org#30574 saves the draft state in the slot file).
tools/server/tests: test_prompt_cache_disk.py (with a new test: restored checkpoints follow
--ctx-checkpoints-ram, which fails on the earlier port), test_slot_save.py and test_kv_keep_only_active.py,
44 passed on a CPU build.

Co-authored-by: Daniele Zannotti <d.zannotti@me.com>
Assisted-by: Claude Code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk:medium Risk: medium server verified The claims were reproduced.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants