Repository navigation
qwen27 metal: follow-up turns resume the full history whether or not the client keeps the reasoning - #2811
Conversation
devopsfortunato
left a comment
There was a problem hiding this comment.
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.
4cb6190 to
6fbaa25
Compare
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):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), maincc0bb1f67against this change:Every reply's hash is equal to main's in both modes.
Checks: the new
zig/src/native/qwen27_reply_cache_test.zigreproduces 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 fmtis clean, and the lean count is unchanged.Fixes #2791.