Repository navigation
prompt cache: Qwen3.8-27B on Metal keeps states at message starts behind TF_QWEN27_MESSAGE_STARTS (#3617) - #4932
Conversation
dfb88e9 to
02badbb
Compare
a81150e to
3ed03cf
Compare
|
I test-merged zig build test-prompt-cache-host test-message-starts-resume-host \
test-request-starts-turns-host -Dcpu=apple_m1 --summary allAll 854 tests passed: 785 prompt-cache, 34 resume and 35 request-start tests. That covers edits to a tool result with ChatML and the Qwen3.8 template, the switch-off control, and the existing request-start fixtures in the same trial tree. These are host and scripted-route checks. I did not rerun the 27B checkpoint's Metal timing or token comparison. Igor Lessio - AIFLOW LABS |
|
Thank you, @mrv777. The off path preserves the existing plan, and the new starts retain endpoint priority and reservation protection. The host checks cover tool-result edits through both templates. This is ready for controlled comparisons. Exact fresh/resumed tokens and state on the final tree remain, including the fallback when interior capture is unavailable. The shared host also serves SenseNova, so scope the switch or qualify that path before using it there.
|
|
@mrv777, our device check on an M3 Ultra, with Qwen3.8-27B and the switch off: prompt-fill tokens equal main's in all 8 rows, and speed is level with main at 1 and 4 streams. So the default path is unchanged. The opt-in path still needs the checks from the review:
|
|
Thanks, @ashhart, and thanks for the M3 Ultra off-path check. SenseNova-Loop. Resumed equals fresh, in tokens and state, on the final tree. I ran this change, with the scope, rebased onto main
Every resumed fingerprint and every reply's token IDs equal the fresh pass's, with and without DFlash2. The branch itself stays on |
|
Thank you, @mrv777. The switch now excludes SenseNova-Loop through the parsed checkpoint config. The reported 32 M5 runs cover fresh/resumed tokens and state, including bank refusal, split fallback and DFlash2. That closes the earlier asks; the M3 default-off result remains accepted. The current head merges cleanly and is ready to land after the usual final checks.
|
…ind TF_QWEN27_MESSAGE_STARTS (ashhart#3617) With TF_QWEN27_MESSAGE_STARTS=1 a chat request plans prompt states at up to four of its latest message starts (the exact token offsets ashhart#3679 records), and the rewind state moves back to the last user message's start, role header included. A later request that edits a tool result or replaces a message in that history resumes from that message's start instead of re-reading the whole prompt. The starts only take room the cache can already promise beside the request's own endpoint: they never displace another kept state, and under pressure the reservation trims them before the pass. Unset or 0 leaves every plan as on main. Raw prompts carry no starts and are unchanged.
…nts on main's plan (ashhart#3617) The Qwen27 Metal host also serves SenseNova-Loop. Its looped checkpoints now keep main's prompt-cache plan with the switch set, so the opt-in reaches Qwen3.8-27B only.
de46458 to
4043a6a
Compare
|
Thank you @mrv777. This is merged into main with your commits under your own name, so you're listed as a TensorFold contributor. It ships in the next release, credited to you in the changelog and the release notes. |
What this changes
Qwen3.8-27B on Metal can keep prompt states at a chat request's message starts, behind
TF_QWEN27_MESSAGE_STARTS=1(off by default). It builds on the starts #3679 already records: the store plans states at up to four of the request's latest message starts, and the rewind state moves back to the last user message's start, role header included. A later request that edits a tool result or replaces a message in that history then resumes from that message's start, where main re-reads the whole prompt. Unset or0leaves every plan exactly as on main.What is kept. A new planner step,
core/prompt_message_starts.zig, runs after the ladder inlookupUserAt. Candidates areRequest.message_startsstrictly between the resume point and the history end, at leastmin_gap(256) from the resume point, the history end, the other marks and each other. The latest are taken first, at mostRules.message_starts(4 for the 27B). The lane host scopes the request's starts onto the store around its lookup, as it does forsalt. No family code reads the starts, and the Metal pass already cuts at any planned mark.The rewind mark. With the switch on, an unplanned family's rewind sits at the last message start within
min_gapbeforerewind_len, which is the last user message's own start with its role header. A next turn that replaces that whole message, rather than editing its text, now resumes there. This is the point Synthena raised above.Scope. The Qwen27 Metal host also serves SenseNova-Loop. A looped checkpoint keeps main's plan with the switch set (
qwen27_resume.messageStarts(looped)), so the opt-in reaches Qwen3.8-27B only.Memory. The planner admits a start only into room that free space or redundant (superseded) states hold. The guarded reservation then gives non-endpoint marks only free room plus the resumed state's credit, so start states never displace another kept state. Under pressure they are trimmed before the pass (counted in
trimmed), and nothing else changes. A growing conversation plans none, because its resume point already sits at the previous reply's end.Issue: Refs Prompt cache: keep a state at every message start, in every family, by default (exact) #3617 (the Qwen3.8-27B Metal slice of step 3; default-on is step 4)
Stacked on: main
atc overlap --json: run after rebasing oned70f71aa. Three open PRs conflict on neighbouring lines: glm(metal): gate in-memory 4-bit/64 MTP draft head on GLM_DRAFT_HEAD_Q4 #4253 and CPU: Qwen3.5-2B served on the host's cores (zig build native-cpu), every operation in the kernel contract, judged against a float64 reference #2551 (registry and parser-test rows inenvironment.zig, its test anddocs/environment.md) and Implement GLP control vectors with a fused Metal kernel for Qwen3.8-27B #1835 (CHANGELOG.mdandqwen27_served.zig). Whichever lands second takes both. Seven are clean, and the rest unknown (ATC hit its PR limit).Checks on this head
de46458cf(baseed70f71aa; it merges cleanly with main at9961ca492);tools/pr_check.py --pr 4932 --base upstream/main: 0 errors, 0 warningspython3 tools/zig/lean_check.py, on the base and on this head: base 165, head 165, 0 new (tools/zig/lean_new.py)zig build test-prompt-cache-host -Dcpu=apple_m1: 785 passed (8 new inprompt_cache_message_starts_test.zig)zig build test-message-starts-resume-host -Dcpu=apple_m1(new step): 34 passed, 2 of them the newtool edit starts:tests. Real chat histories go throughopenai.chatInputandchat.prepare, through ChatML and through Qwen3.8's ownqwen38.jinja. An edit to the last of four tool results resumes at that tool message's recorded start, and the resumed hash equals a fresh pass. Unset resumes from 0.zig build test-qwen27-resume-host -Dcpu=apple_m1: 25 passed, 1 new: the switch's starts reach a plain checkpoint and never a looped one.test-request-starts-turns-host,test-qwen27-reply-cache-host,test-qwen27-startup-host,test-qwen27-keep-host,test-cache-store-host,test-conversation-cache-host,test-cache-progress-host,test-cache-spares-host,test-cache-replay-host,test-cache-agent-host,test-cache-decay-host: all pass.test-environment-host,test-environment-report-host,test-capability-env-host,test-env-defaults-host,test-profile: all pass.zig build test test-kernel-contract test-golden fuzz-cortheon -Dcpu=apple_m1(on the previous base; CI ran it green ona81150e85): the same three failures as on main at the base, and nothing new. These are sixstd.memcompile errors ("Only nullable and allowzero pointers can be set to zero"),mtp_pick.zig's missingbf16_topkmodule, and the testinfo prices a Qwen3.8-27B start from serve's charge. Golden: 0 unexpected differences.zig build: passeszig fmt --check build.zig zig,git diff --check: cleanaddfails the edit, budget, superseded-room and raw-prompt tests. Letting non-redundant states count as free fails the no-displacement test. Neuteringrewindfails both rewind tests.a81150e85and3ed03cf90; pending on this pushReceipt
Main against branch: the cells below, measured with main at
a0f809ea0against this change on that base, on the same machine, checkpoint and settings. The branch has since been rebased ontoed70f71aa. The rebases touched neighbouring registry rows and the changelog, and moved one test call into main's newqwen27_served_test.zig; the host checks above were re-run on the rebased head.Boxes it reaches: Qwen3.8-27B on Metal with
TF_QWEN27_MESSAGE_STARTS=1. With the switch unset no plan changes on any family:Rules.message_startsdefaults to 0, and only the 27B Metal host sets it, never for a looped (SenseNova-Loop) checkpoint.New switch?
TF_QWEN27_MESSAGE_STARTS, exactly0or1, default0. It is registered inplatform/environment.zigwith parser tests and documented indocs/environment.md, the recipe and the ledger. It has no profile row, like the otherTF_QWEN27_*cache and slot switches (TF_QWEN27_SLOT_RESUME,TF_QWEN27_METAL_KV_PAGES): it changes which states are kept, not any arithmetic, and the replies below are token-identical.Environment: TensorFold
a0f809ea0(main) and this change on it, Zig 0.17.0 ReleaseFast. Mac Studio M5 Ultra, 64-core GPU, 96 GB, macOS 27.2. Qwen3.8-27B MLX 4-bit (local conversion), DFlash2z-lab/Qwen3.8-27B-DFlash2at50307d4c.--parallel 1 --context 40960 --prompt-cache-gib 12(0for the cold controls),TF_PROMPT_ROWS=512, thinking off, one server start per cell.Paths and platforms the change reaches, which I ran and which I could not: the Qwen27 Metal lane host, with and without DFlash2, greedy and seeded, on an M5 Ultra. Not run: M1 to M4. The planner is shared core code, but no other family sets
message_starts.Exactness on the final tree, for the review: this change (with the SenseNova scope) rebased onto main
9961ca492, the same M5 Ultra, checkpoint and settings, 256-token long answers, two server starts per cell, 32 runs in all.Session.fingerprint()for the slot once its prompt pass ends, before the first decode round. That hashes every layer's committed DeltaNet state, the visible K/V rows and the last row's logits. The probe isn't in this branch.edit, 20,995 tokens reused) and resumed after only the preceding turn (prior, 20,961 reused), each against the fresh cache-off pass. Greedy and seeded (seed=7,t=0.7), with and without DFlash2. Without a drafter, the bank captures marks inside a chunk. With DFlash2, and in a second build that refuses the bank (theNoBufferfallback, forced), the pass splits its chunks at each mark instead.c13624874828…, the same in every cell, drafter on and off), and every reply's token IDs are equal (greedyf5bd5681a3…, seeded52fa3ce563…).Exactness on
a0f809ea0, as first posted: the fixture is the one from my earlier comment, rebuilt: fourread_fileresults of public repo docs, rendered to exactly 32,768 tokens, with the last result'sBUILD_MARKER=17edited to19.seed=7,t=0.7), with and without DFlash2. Greedy IDs are also equal across drafter on and off.19.9183534beb…, seeded350f58413e….--prompt-cache-gib 0and DFlash2, greedy, diverged at token 8. Runs 2 and 3 of that same cell matched everything above. I haven't chased it; I can open an issue if useful.tools/bench_concurrent.py --levels 1,4 --alone --serialwith the switch on: every request equal alone and serial at 1 and 4 streams. Itstoken_shavalues equal main's.Main against this branch, same machine and checkpoint: (seconds to first visible output, client-side)
=1--prompt-cache-gib 0)In the edit fixture the marker sits about 7,000 tokens into the last tool result, so a resume from that message's start still reads those 7,000 rows and the 4,700 after them. Where the edit is near a message's start, as in my earlier comment's fixture (68 tokens), the saving is close to the whole prefix.
Decode speed before and after, from
tools/bench_openai.py(ABBA,--parallel 4, DFlash2, greedy): code 215.8 / 216.9 tok/s on main against 217.1 / 216.9 on the branch; chat 152.8 / 153.4 against 153.5 / 153.1.bench_concurrentat 4 streams: code 385.4 / 384.7 against 383.2 / 382.8; chat 301.8 / 335.7 against 300.3 / 300.5, where main's second run is the outlier (its round-gap p95 was 33.8 ms, against 45–46 ms on every other run). These short prompts carry no planned starts, so the switch changes no plan in these cells.Prompt speed before and after: with
--prompt-cache-gib 0nothing is planned, so cold prompts are main's. With the cache on and free room, a cold 32K chat request keeps 6 states (about 10 GB) instead of main's 2 (5 GB). With DFlash2 each start that is not already a chunk end costs one more prompt pass, so expect about four passes (~0.1 s) plus four ~1 GB blits, roughly 0.5% of a 25 s pass. I couldn't resolve that on this Mac, which had other GPU work running: ABBA across fresh servers (4 and 6 pairs) and an in-process ABAB (8 pairs, cache flushed before each request, starts toggled per request in a measurement-only build) gave paired differences from -2.1 to +2.0 s, medians +0.1 to +0.5 s, with whole rounds at 39–40 s for every variant. Under pressure the reservation trims the starts and the pass is main's. If you'd rather have no extra passes, the starts could snap down to the pass's existing chunk ends, at the price of up toTF_PROMPT_ROWSrows on each resume. I didn't do that here.Licence of any new weights or drafter: n/a
Host replay: why four starts, and why only promised room
I replayed synthetic agent traces through the real store with the 27B's state sizes. The traces had 1, 3 and 6 interleaved sessions, 8, 12 and 16 GiB budgets and 12 seeds. Edit mixes were none, trimming an old tool result, swapping a note in the last user message, editing the last tool result, and a cold history followed by an edit.
Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo.--drafter, no slower than without it, greedy and sampled