Skip to content

Fix #98: engage CUDA prefix retention on Flash Next (keep cache entry one token early) - #143

Closed
Arminova wants to merge 1 commit into
ashhart:mainfrom
Arminova:fix/98-cuda-flashnext-prefix-retention
Closed

Arminova wants to merge 1 commit into
ashhart:mainfrom
Arminova:fix/98-cuda-flashnext-prefix-retention

Conversation

@Arminova

Copy link
Copy Markdown
Contributor

Fixes #98.

Problem

On the CUDA lane, Flash Next (qwen4_exp) remembered the whole prompt as its prefix-cache entry and matched only an exact token-prefix-extension. Flash Next is a reasoning model, so the next turn re-renders the assistant boundary (a stripped <think> plus two newlines), which changes the last token of the previous prompt. The stored full-prompt entry is then no longer a prefix of the next turn, _slot_for / _resume find nothing, cached = 0, and every turn pays a full prefill — exactly what #98 reports.

It reproduces only with thinking on (the default): with the reasoning boundary the last token diverges by exactly one token (…assistant\n → …assistant\n<think>\n\n…); with thinking off the full-prompt entry already matches.

Fix

The dense 27B (qwen3_5) already avoids this by keeping its cache entry one token early (entry_end). This ports the same idea to qwen4_exp:

  • decode.prefill gains keep_at: it forces a chunk boundary onto keep_at, captures the committed state there (a resume dict {state, tail}), then absorbs token keep_at into the MTP head exactly as a resume does. The kept state's bits match a fresh prefill(prompt[:keep_at]) and the current request is unchanged; it returns (first, kept) when keep_at is set.
  • engine.entry_end + both remember paths (concurrent MultiDecoder.admit and single-stream _decode) store prompt[:entry_end] with that early state. entry_end is applied on one GPU only, so the two-rank path (which hands rank 1 a fixed resume point) is untouched.

Output stays exact regardless: every drafted token is verified against the target and the reused KV rows come from the retained slot, so a wrong MTP resume tail could only lower draft acceptance, never change the reply.

Results

1× DGX Spark (GB10), Vontra/Qwen3.8-Flash-Next-MLX-4bit-MTP, --parallel 5 --context 262144 --kv-dtype int8, thinking on. Controlled A/B: clean v0.5.0 (worktree at 9cd52ab) vs the same + this patch, identical image and flags.

ctx prompt toks PP tok/s TG tok/s cold TTFT warm TTFT (this PR) speedup cached toks answer correct
2k 1,989 1,912 53.8 1.04 s 0.316 s 3× 1,988 yes
16k 15,782 2,214 55.2 7.13 s 0.193 s 37× 15,781 yes
64k 65,222 2,154 67.0 30.28 s 0.198 s 153× 65,221 yes
128k 131,166 1,985 61.1 66.09 s 0.205 s 323× 131,165 yes
200k 206,917 1,839 51.4 112.50 s 0.216 s 521× 206,916 yes
  • Before (stock v0.5.0, same config): cached = 0 and warm TTFT = cold TTFT (1×) at every size — full re-prefill each turn.
  • cached = prompt − 1 confirms the one-token-early entry.
  • Correctness: a fact buried at ~80% depth is retrieved correctly on the resumed turn at every size up to 200k (thinking on and off); output is deterministic on re-run; draft acceptance on a resumed turn is not degraded (57% vs 53% cold).
  • No regression: cold PP/TG match the unpatched build; tools/bench.py decode 69.3 code / 55.7 chat tok/s; tools/toolcheck.py OK.
  • Validated on both CUDA paths: concurrent (--parallel 4/5) and single-stream (--parallel 1).

Not in this change (follow-ups)

  • Message-start snapshots via resume_points / points (needs multi-stop capture in prefill), for reuse when an earlier turn is edited.
  • Resuming past the previous reply (this reuses up to the prompt end, one token early, like qwen3_5).
  • The two-rank path.

🤖 Generated with Claude Code

…ulti-turn resumes (#98)

Flash Next (qwen4_exp) on CUDA remembered the whole prompt as its prefix-cache
entry and matched only an exact token-prefix-extension. Because Flash Next is a
reasoning model, the next turn re-renders the assistant boundary (a stripped
<think> plus two newlines) so the last token of the previous prompt differs, the
stored full-prompt entry is no longer a prefix, _slot_for/_resume find nothing,
and every turn pays a full prefill (cached=0). This is what #98 reports.

The dense 27B (qwen3_5) already avoids this by keeping its entry one token early
(entry_end). Port the same idea to qwen4_exp:

- decode.prefill: add keep_at. It forces a chunk boundary onto keep_at, captures
  the committed state there (a resume dict {state, tail}), then absorbs token
  keep_at into the MTP head exactly as a resume does. The kept state's bits match
  a fresh prefill of prompt[:keep_at] and the current request is unchanged; it
  returns (first, kept) when keep_at is set.
- engine.entry_end + both remember paths (concurrent MultiDecoder.admit and the
  single-stream _decode) store prompt[:entry_end] with that early state.
  entry_end is applied on one GPU only, so the two-rank path (which hands rank 1
  a fixed resume point) is untouched.

Output stays exact regardless: every drafted token is verified against the target
and the reused KV rows come from the retained slot, so a wrong MTP resume tail
could only lower draft acceptance, never change the reply.

Follow-ups (not in this change): message-start snapshots via resume_points/points
(needs multi-stop capture in prefill), resuming past the previous reply, and the
two-rank path.

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@Arminova

Copy link
Copy Markdown
Contributor Author

Closing this as a duplicate of #124. I didn't see it when I opened this one, sorry for the noise.

#124 fixes the same thing more completely: it keeps the state inside the chunk (no extra forward), it handles the same prompt sent several times in a row (this PR misses every other identical resend), it covers --tp 2, and it comes with tests. Please go with #124.

@Arminova Arminova closed this Sep 30, 2026
@Arminova
Arminova deleted the fix/98-cuda-flashnext-prefix-retention branch September 30, 2026 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CUDA] prefix retention does not engage on the CUDA lane: re-sending an identical context still costs a full prefill

1 participant