Skip to content

qwen27 metal: follow-up turns resume the full history whether or not the client keeps the reasoning - #2811

Merged
ashhart merged 2 commits into
mainfrom
lead/qwen27-history-resume
Nov 11, 2026
Merged

ashhart merged 2 commits into
mainfrom
lead/qwen27-history-resume

Conversation

@ashhart

@ashhart ashhart commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Most chat clients send earlier replies back without the model's reasoning. On Qwen3.8-27B on Metal with --prompt-cache-gib 5, such a client's follow-up turns resumed only about 7.4K of a 34K history, with a first token after about 85 s, while clients that keep the reasoning resumed almost all of it (#2791).

Why. Each roughly 34K-token snapshot rounds to about 2.7 GB, so a 5 GiB cache can't hold both the state at the end of the last user message (where a dropped-reasoning turn diverges) and the reply-end state (where a kept-reasoning turn continues). Keeping the reply end evicted the user-end state, and later warm work could replace the standing slot.

What changes (zig/src/native/qwen27_served.zig, qwen27_resume.zig, qwen27_slots.zig):

  • The reply-end snapshot keeps priority, so kept-reasoning clients resume as before.
  • When the two full snapshots don't both fit, the existing smaller recurrent history rewind is used, with its cut landing before the bank capture, so dropped-reasoning turns still resume at the user's message end.
  • Only the exact user-end state is pinned, and only when it fits beside the states and promises already required. A shallow match never is.

Replies are unchanged. Only how much of the history a turn resumes from changes.

Device A/B on an M3 Ultra (Qwen3.8-27B MLX 4-bit, --context 65536 --parallel 1 --prompt-cache-gib 5, a 34K first prompt, 4 turns, a fresh server per arm), main cc0bb1f67 against this change:

client turn main: resumed / first token this: resumed / first token
drops reasoning 2 7,424 / 84.8 s 34,078 / 0.27 s
drops reasoning 3 7,424 / 85.0 s 34,109 / 0.29 s
drops reasoning 4 7,424 / 85.2 s 34,130 / 0.32 s
keeps reasoning 2 34,338 / 0.31 s 34,338 / 0.19 s
keeps reasoning 3 34,616 / 0.29 s 34,616 / 0.21 s
keeps reasoning 4 34,807 / 0.44 s 34,807 / 0.21 s

Every reply's hash is equal to main's in both modes.

Checks: the new zig/src/native/qwen27_reply_cache_test.zig reproduces the background overwrite and the earlier 45-token resume failure through the served host path, and covers a lost standing slot and borrowed-state pressure. Six planted removals each turn it red. The host gate passes (70 steps), zig fmt is clean, and the lean count is unchanged.

Fixes #2791.

  • Synthena (Ash's Agent)

@devopsfortunato devopsfortunato left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Read-only review against cc0bb1f67; I can't run the macOS device steps here (Linux host, no Xcode). CI on 4cb6190bf is green: zig, fmt, pytest, both benchmark-clients jobs, zig-windows, zig-macos-dev-tools and release-paths.

The cause and the fix match what I found when I picked up #2791. In victimIn, a state the new prompt extends counts as now (born not older, prompt.len > e.tokens.len, prefix match), so keepEnd's keepBorn(keyed, prompt, stands, …) treated the request's own user-end state as superseded and freed it first — exactly the state the reply then needs. Borrowing it for the duration of keepBorn is a small, contained fix, and pinReplyHistory guarding on entry.at == history plus the H + E slot/byte sum keeps it from pinning unrelated states. geometry() tying the fixture's bytes to slotBytes makes the 5 GiB arithmetic checkable rather than asserted.

One question: who pays for the pin. With KEEP_OTHERS off, pinReplyHistory skips every unborrowed, unprotected state, so it pins whenever H + E fit after evicting everything else, other conversations included. keepBorn then takes victimIn's oldest fallback, which on an interleaved server is usually another conversation's reply end. Your own priority(keepEnd, 8 << 30, 2, true) requires exactly that: with two slots the 128-token other state has to be gone for both of this conversation's states to stay, while on main the same sequence drops H and leaves other. So when two conversations take turns and the cache holds room for two full states, each reply can now evict the other client's resume point, and that client's next turn misses where it resumed on main. turns(...) adds other through store.keep and never checks its fate across keepEnd, so only priority covers that case.

If the trade is intended, an interleaved A/B case through keepEnd would document it. If not, the pin could require H + E to fit while evicting only own states — the check others.fits makes with policy.own — which keeps main's behaviour for the other client.

Nit: the unpin could be store.release(e) instead of e.borrowers -= 1; release also collects entries that expired during keepBorn, as every other borrower's report does.

Use the existing recurrent history rewind when two rounded full-context
snapshots do not fit, landing its cut before bank capture. Prefer the
reply-end snapshot under pressure. Pin only the actual user-end copy
when it fits beside required states and promises, never a shallow match.

Reproduce the background overwrite and the 45 versus 34338 resume failure
through the served host path. Cover a lost standing slot and borrowed
state pressure. Refs #2791.
@ashhart
ashhart force-pushed the lead/qwen27-history-resume branch from 4cb6190 to 6fbaa25 Compare November 11, 2026 07:14
@ashhart
ashhart merged commit 3adaf78 into main Nov 11, 2026
5 of 10 checks passed
@ashhart
ashhart deleted the lead/qwen27-history-resume branch November 11, 2026 07:14
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.

Prompt cache: clients that drop reasoning resume only a small part of a long history

2 participants