Repository navigation
fix(qwen4exp): prompt prefill cuts only at restore points at least 4096 tokens apart - #828
Merged
Merged
Conversation
…96 tokens apart qwen4exp ends a prompt forward at every restore point, so a prefix-cache hit and a miss see the same cuts. With a cut at every chat boundary, an agent-shaped prompt (50 short messages) prefilled in forwards of about 300 tokens, each paying the full expert read. Restore points are now spaced by ModelBackend::restore_point_spacing(): the first boundary (system/tools head), then each boundary at least that many tokens past the last kept one (spaced_restore_points). The rule is prefix-stable, so a checkpoint saved at one point is a restore point of every later turn, and a prompt's text does not depend on what the cache holds. reserve_inline_snap moves its capture down to the deepest spaced point. qwen4exp spaces them 4096 tokens apart; every other backend keeps every boundary (0). gfx1151, Strix only, main vs this commit: - 50 messages, 14,628 tokens, cold: 272 to 879 tok/s (55.1 s to 18.2 s); resend 1.5 s to 3.1 s (the checkpoint now sits at most about 4K tokens back); resend text identical - one 16K message: cold 1,331 vs 1,306 tok/s, resend 1.5 vs 1.6 s - quality bench 6/6; test_server_unit 647/647; the qwen4exp prefix-cache model test passes Co-Authored-By: Claude Opus 5.5 <[email protected]>
davide221
commented
Oct 8, 2026
davide221
left a comment
Contributor
Author
There was a problem hiding this comment.
Code review
No issues found. Checked for bugs and logic errors in the changed code.
Reviewed b89cb371 · 0 candidate issues checked, kept at ≥70% confidence · add the code-review label to review again
3 tasks done
This was referenced Oct 9, 2026
davide221
added a commit
that referenced
this pull request
Oct 11, 2026
…me, late system messages work (#835) * fix(server): the prefix cache keeps each turn's end, for every request at once Three things kept conversations from reusing their own prefix. - qwen4exp spaces its restore points 4096 tokens apart (#828), and a capture moved down to the deepest spaced point. A chat shorter than that never cached past its system prompt, an agent re-read up to 4K tokens on every turn, and a follow-up question on a 130K-token document re-read all of it (103 s). A backend with spaced restore points now saves the end of the prompt's last message, the point the next turn extends; the extra cut costs one forward over the generation prompt. Other cuts still move down to a spaced point. - The prefix cache held one capture reservation at a time, so the requests prefilling beside a capturing one saved nothing. Each prefilling request can now hold one: a capture in flight keeps its slot and its bytes out of the other selections, and the same prefix is not captured twice. - Concurrent serving never pruned the snapshots a new one supersedes. It now prunes them as each capture lands, as single-sequence serving does, and frees their payloads. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(server): requests decoding beside a long prompt keep half the time While one request prefilled a 130K-token document, a person chatting beside it got one token per 4096-token chunk: a 300-token reply took about 200 s. - StepPlanLimits::prefill_time_share: while requests decode, a prompt longer than one step's chunk takes at most that share of the time; after each of its chunks the decoding requests step alone for the rest. qwen4exp sets 0.5, since a chunk pays the whole expert read and a shorter one would not let them run faster. A prompt that fits in one chunk (an agent's next turn) is not held back. - A prompt that fits in one step runs before longer ones, alone, so a question asked beside a long document waits for one short forward instead of the document's next chunk. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(server): Claude Code requests no longer fail, replies start without blank lines - Claude Code 2.1 sends its environment as a system message after the user's first one. Qwen3.8-Flash-Next's template raises on a system message that is not first, so every Claude Code request failed with HTTP 500 and was retried 10 times. A template now gets a late system or developer message as a user turn, in place, so the prompt before it stays the same and cached. - A template that rejects the messages answers 400, which clients do not retry like a server failure. - Claude Code asks for thinking type "adaptive", which turned thinking off. It now means the model's default, as when the field is absent. - A reply no longer starts with the newlines a template puts after </think>; apps showed them as blank lines on top. Spaces stay, since they can indent the first line. Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(server): usage reports the prompt tokens the prefix cache served Codex showed 0 cached input tokens for every turn. Chat Completions usage now carries prompt_tokens_details.cached_tokens and Responses usage input_tokens_details.cached_tokens, where OpenAI's clients read them, from the same count as usage.timings.cached_prefix_tokens. Anthropic usage is unchanged: its input_tokens and cache fields add up to the context, which Claude Code would count twice. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(server): a late system message stays one where the template takes it The Claude Code fix rendered every system or developer message after the first as a user turn, for every Jinja template. A template that accepts system messages anywhere now gets them as they are; only when the template rejects the messages and a late system message is among them is it rendered again with those as user turns. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(server): every model keeps each turn's end, and the turn before it for edits Only Qwen3.8-Flash-Next saved a turn at the end of its last message. Every other model saved before it, so a follow-up question re-read the message it followed: Qwen 3.8 27B read a 118K-token document again in 532 s, and now restores it and answers in 4 s. - Every backend saves the prompt up to the end of its last message, the point the next turn extends. A conversation's first turn still saves its system head, so new conversations on the same system prompt restore it, unless the head is short beside a long message (as tool requests do). - A turn's new snapshot keeps the previous one while the resident budget holds it: a person who edits their last message resumes there. Eviction drops these kept turns before any conversation's newest one. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(ggml-cuda): paged attention keeps its partials within 256 MiB Paged attention split the context into one partition per 1024 tokens whatever the batch, and each partition keeps partials for every output row. A 2048-row prompt chunk 118K tokens deep asked for more memory than the R9700 had left beside Qwen 3.8 27B's weights and KV cache, and the server aborted: a long document sent to Qwen 3.8 27B on its own crashed the engine 79 s in. The partials now take at most 256 MiB. A chunk with that many rows covers the context with fewer, longer partitions, its rows giving it the parallelism: the same document now prefills in 524 s, as in 512-row chunks (530 s). Co-Authored-By: Claude Opus 5.5 <[email protected]> * feat(qwen35): requests decoding beside a long prompt keep half the time A step that carries a prompt chunk decodes without the DFlash drafter, one token per request. Beside a 118K-token document, a person chatting with Qwen 3.8 27B got 0.6 tok/s for the 9 minutes the document took. - While requests decode, a prompt of more than 8192 new tokens takes half the time (StepPlanLimits::prefill_time_share, as Qwen3.8-Flash-Next does), and the steps between its chunks decode with the drafter: the person gets 46 to 57 tok/s, and the document answers after 543 s instead of 530 s. - A prompt is long by the new tokens it brought (StepPlanLimits::time_share_prompt_tokens; by default more than one step's chunk), so it shares the time from its first chunk to its last. An agent's next step stays under 8192 and keeps every step: four agents at once finish in 117 s, against 153 s when their steps shared the time. Co-Authored-By: Claude Opus 5.5 <[email protected]> * perf(qwen4exp): an agent's next step no longer gives up time to decoding requests A prompt beside decoding requests shared the time while more than one granule (4096 tokens) of it was left, so an agent's next step after a large tool result waited for the others' replies. It now shares the time only when it brings more than 8192 new tokens, as on Qwen 3.8 27B: a document, not an agent's step. Four agents at once on Qwen3.8-Flash-Next: time to first token 3.29 s mean and 5.95 s at most, against 3.79 s and 11.37 s; the person chatting beside a 130K-token document still decodes at 19 tok/s. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: mrciffa <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since #809, Qwen3.8-Flash-Next ends a prompt forward at every chat boundary, so a prefix-cache hit and a miss see the same cuts. On an agent-shaped prompt (many short messages) that means forwards of about 300 tokens, each paying the full routed-expert read, and cold prefill drops to about a fifth of the single-message rate.
This keeps the guarantee and cuts less often:
ModelBackend::restore_point_spacing()(default 0, next tosnapshot_granularity()): restore points are the first boundary (the system/tools head) plus each boundary at least that many tokens past the last kept one.spaced_restore_points()in the prefix cache.prefix_restore_points()takes the spacing, so every backend that cuts at restore points goes through the same function.reserve_inline_snapplaces its capture at the deepest spaced point.Results
Strix Halo, UD-Q4_K_XL, main vs this branch:
A resend re-prefills at most about 4K tokens past the last kept checkpoint, which is the cost of cutting less often.
Validation
test_server_unit647/647, with a new spacing/snap test that derives the expected cut from the prompt's boundaries.🤖 Generated with Claude Code