Repository navigation
server : fix context checkpoint eviction on prompts shorter than checkpoint_min_step - #28302
Conversation
…list is full The spacing eviction in create_checkpoint() keeps the oldest checkpoint and erases every later one within checkpoint_min_step of it. For prompts shorter than checkpoint_min_step this drops the checkpoint at n_tokens - 4 that the next request resumes from, so hybrid/recurrent models re-prefill from the previous checkpoint instead. Apply the spacing rule only once the list is at n_ctx_checkpoints, and replace an existing checkpoint at the same n_tokens instead of appending a duplicate.
|
Hi @masterFoad, thanks for your contribution! Per our contribution guidelines, the automated PR checker found the following issue(s) that need your attention:
Please note that maintainers reserve the right to make final decisions on PRs. If you believe there is a mistake, please comment below. |
|
Can confirm this fixes a real production symptom independently. Running Qwen3.8-27B (hybrid attention + Mamba2/SSM architecture, GGUF general.architecture = qwen35) via ollama 0.33.3, every single conversation turn was forcing full prompt reprocessing regardless of prompt length — observed on prompts from a few hundred tokens up to ~6,241 tokens, all comfortably under the default 8192-token checkpoint_min_step. Logs showed checkpoints being created and then immediately erased ("erased invalidated context checkpoint") on the very next turn, exactly matching the eviction-logic bug described here. This patch would directly fix that for any hybrid/recurrent model workload with typical (sub-8K-token) conversation lengths — appreciate the fix, hope it gets reviewed soon. |
…list is full (ggml-org#28302) The spacing eviction in create_checkpoint() keeps the oldest checkpoint and erases every later one within checkpoint_min_step of it. For prompts shorter than checkpoint_min_step this drops the checkpoint at n_tokens - 4 that the next request resumes from, so hybrid/recurrent models re-prefill from the previous checkpoint instead. Apply the spacing rule only once the list is at n_ctx_checkpoints, and replace an existing checkpoint at the same n_tokens instead of appending a duplicate. (cherry picked from commit 5d806aa)
…list is full (ggml-org#28302) The spacing eviction in create_checkpoint() keeps the oldest checkpoint and erases every later one within checkpoint_min_step of it. For prompts shorter than checkpoint_min_step this drops the checkpoint at n_tokens - 4 that the next request resumes from, so hybrid/recurrent models re-prefill from the previous checkpoint instead. Apply the spacing rule only once the list is at n_ctx_checkpoints, and replace an existing checkpoint at the same n_tokens instead of appending a duplicate.
|
Hey there, sorry for commenting on a closed pull request, I have been trying to fix a similar issue as to this one by @landrewes33, but still not working, even on commit 434ddbb.
So I tried b10864, but no dice. Using the b10864 build, still produces these issues where "forcing full prompt re-processing due to lack of cache data (likely due to SWA or hybrid/recurrent memory," happens. I briefly believed that this would potentially be an issue with the quant I was using, specifically I did my best to try and figure out what the issue was with some other AI tools, trying new builds, different flags but these issues still appear, and changes my overall work to be 95% prompt processing, which is far from optimal. Setup specifics:
Trying |
…list is full (ggml-org#28302) The spacing eviction in create_checkpoint() keeps the oldest checkpoint and erases every later one within checkpoint_min_step of it. For prompts shorter than checkpoint_min_step this drops the checkpoint at n_tokens - 4 that the next request resumes from, so hybrid/recurrent models re-prefill from the previous checkpoint instead. Apply the spacing rule only once the list is at n_ctx_checkpoints, and replace an existing checkpoint at the same n_tokens instead of appending a duplicate.
The branch is far behind upstream master and the parts that a card other than this one needs had drifted. This ports the non-backend half of that sync, content-matched against upstream rather than replayed by hash: the parser and JSON schema chain (ggml-org#27764, ggml-org#28736, ggml-org#28742, which adds common/parsers/, common/json-schema.* and tests/test-json-schema.cpp and retires the three example scripts it replaces), the chat, grammar, jinja and server fixes (ggml-org#28302, ggml-org#28469, ggml-org#26885, ggml-org#28279, ggml-org#28511, ggml-org#28620, ggml-org#28817, ggml-org#28749), the qwen4exp RMSNorm and multiply fusion (ggml-org#28896, hand-ported because the norm tensors here load with a different shape), the GDN q/k l2 norm as flash-linear-attention defines it (ggml-org#28068), the ggml_prec_set_acc API that replaces ggml_mul_mat_set_prec and ggml_flash_attn_ext_set_prec at every call site, and the scheduler fixes for zero-sized expert ids (ggml-org#28739) and split inputs (ggml-org#28387). The GPU backend half of the same sync lands in the following two commits; this one alone does not build the CUDA or HIP backend, because their call sites move to the new precision API there. test-chat, test-json-schema, test-json-schema-to-grammar, test-grammar-integration and test-grammar-parser pass. Assisted-by: Claude Opus 5
Overview
This fixes
create_checkpoint()deleting the checkpoint a later rewind needs whenever the prompt is shorter thancheckpoint_min_step.create_checkpoint()currently runs a spacing eviction before appending the new checkpoint. Walking from the oldest entry, it removes checkpoints from another task whosen_tokensis withincheckpoint_min_step(8192 by default) of the previous surviving checkpoint. On prompts shorter than 8192 tokens, effectively every checkpoint falls within that range, so only the oldest one survives.This removes the recent checkpoint that the prompt-fill loop placed near the end of the prompt at
n_tokens - 4(checkpoint_offsets = {4 + n_ubatch, 4}, #20288). For hybrid/recurrent models, whose recurrent state cannot simply be rolled back, that recent checkpoint is the useful one when a later request rewinds the conversation.The functional change is about ten lines: apply the spacing rule only once the checkpoint list has reached
n_ctx_checkpoints, which already provides a hard capacity bound, and replace an existing checkpoint at the samen_tokensinstead of appending a duplicate.I found this while replaying a coding-agent transcript on
Tiel-Coder-35B-A3B-MTP-UD-IQ3_XXS.gguf(HF:peculiar-ragdoll/Tiel-Coder-35B-A3B-GGUF-MTP), a dynamic UD-IQ3_XXS re-quantization of Ornith-1.5-35B-A3B. It is a hybrid GDN MoE: 30 of its 40 trunk layers use Gated DeltaNet recurrent state and only 10 use full attention with a KV cache, so rewinding requires restoring the recurrent state from a checkpoint.The symptom was that reopening each session caused a few hundred already-cached tokens to be processed again, while the server log repeatedly printed
erasing context checkpoint too close.When it shows. Normal linear chat is unaffected: each turn resumes from the checkpoint created by the previous request before that checkpoint can be evicted. Switching between conversations is also unaffected because the prompt cache saves and restores the slot together with its checkpoints.
The problem appears when a request rewinds within a conversation after it has already advanced: editing or branching an earlier turn, agent harnesses that retry or compact a session, or reopening a session at an earlier point.
Tested on my 14inch MacBook Pro M5 24 GB and my 16inch MacBook Pro M1 Pro 32 GB.
Additional information
Stock Qwen3.5-9B Q4_K_M (Unsloth),
upstream/master = 7798007, M5, using 6 turns of ~150 tokens after a 390-token system prompt, then editing turn 4, continuing for two more turns, and finally regenerating the last answer. Generation was greedy with 120-token replies.prompt_nis the number of tokensllama-serveractually processed:prompt_nprompt_nBoth sides produced the same greedy output.
Same scenario on the M1 Pro, run as master, PR, then master again as an ordering control:
erasing context checkpoint too closelines in server logThe ordering control reproduces the original behavior after the PR run, which makes the edit-turn difference unlikely to be explained by thermal state or cache warming.
Tiel-Coder-35B-A3B-MTP-UD-IQ3_XXS (Ornith-1.5-35B-A3B, hybrid GDN MoE),
b10488tree, M5, using a fixed replay of a 3-session coding-agent transcript, 13 generations, and a ~5.6K-token system prompt, measured as the mean of 3 warm repetitions. Each repetition reopens the sessions at turn 1, which exercises the rewind case.The three session openers re-processed 260/254/272 tokens before the patch and 40/34/52 after it. Total warm re-processed tokens per repetition dropped from 2436 to 1776, and warm wall time from 18.25 to 17.12 s (−6.2%). In the same plain-chat edit-turn-4 scenario used above, re-processed tokens dropped from 715 to 26.
Memory. Master effectively retains 2 to 4 checkpoints on short prompts because the spacing pass removes the rest. After this change, a slot may retain up to
n_ctx_checkpointscheckpoints before the existing capacity rule starts evicting them.At the default limit of 32, that means up to 32 times the model's checkpoint state size: about 74 MiB per checkpoint on Tiel and 50 MiB on Qwen3.5-9B. The highest observed usage in the agent replay was 8 live checkpoints / 596 MiB after duplicate positions were removed, and the Qwen3.5-9B chat reached 13 live checkpoints.
--ctx-checkpointsalready provides an explicit bound. A byte-based budget in addition to the count would be a reasonable follow-up, but I would prefer to keep that separate from this eviction fix unless maintainers want it included here.An alternative would be to keep applying the spacing rule on every call but choose the most redundant checkpoint, for example the one closest to its neighbours, instead of allowing the oldest checkpoint to dominate the surviving set. I went with the smaller change here, but I am happy to reshape the policy if that is preferred.
Related: #22929 introduced
checkpoint_min_step; its discussion includes a report of prompt-cache performance dropping for sub-1000-token requests after the merge. #24055 ("context checkpoints always invalidated on hybrid/recurrent models", open) appears to describe the same underlying mechanism. See also #22384 and #20225 for earlier restore-path fixes.Test methodology
llama-serverusing/v1/chat/completions,temperature 0,top_k 1, andseed 0, recordingtimings.prompt_nandtimings.prompt_msfor every request. Master and PR were built from the same tree and build directory, withCMAKE_BUILD_TYPE=Releaseconfirmed on both.--ctx-size 16384 --batch-size 512 --ubatch-size 256 --flash-attn on --cache-type-k q8_0 --cache-type-v q8_0 --parallel 1 --jinja, with the defaults for--ctx-checkpoints(32),--checkpoint-min-step(8192), and prompt caching.Requirements