Repository navigation
qwen27 metal: before M5, decode rounds read cached keys through the prefix kernels, their results in the simdgroup-matrix layout - #3677
Conversation
|
Thanks @hobbesd, and thanks for the clear account of what was run and what was not. The change does what the description says. Before M5,
|
6a3b103 to
83e17cb
Compare
|
Thank you @hobbesd. We rebased onto current main, kept both prefix and join flags, and kept main's changelog structure. In Suite.collect we now call sharedPrefix the way Attn.init does, so the decode control's edit is checked on the pre-M5 source. The build and test results are below; we will run the served comparisons on our side and make the description explicit about prompt width and cold TTFT. Nothing else is needed from you.
|
83e17cb to
d1749b5
Compare
…g, answer and tool calls A tokenizer with <|start|>, <|message|>, <|eom|> and <|eot|> as single tokens and a chat template that writes to=self messages (Muse Spark Open's) replies as messages to recipients. self is reasoning_content; user, commentary and no recipient are content; any other recipient is a tool call whose <atem:function_calls> block becomes the <tool_call><function=...> markup tool_parse and tool_stream already read, so its arguments take the tools' schema types. While a reply streams, an unfinished header, a call message that has not closed and a partial marker are held back, so every split extends the last and the finished split extends them all; the finished reply feeds the call streamer once more, so a last call ended by the end token streams too. Harmony's tokenizers have no <|eom|> or <|eot|>, so gpt-oss replies keep their channel split; tests cover both formats whole and streamed.
…ompt's open message read, thinking off kept off A call message max_tokens cuts stays open as far as it was written, named by its header until its invoke names the tool, so tool_stream reads it as the reply's cut call, whole and streamed, as ashhart#1299 made a GLM call; content never shows its markup. A stop string inside a call keeps the call's text, as the other formats keep theirs. The reply goes on from the prompt's last unclosed <|start|> header, so a generation prompt that already opened to=self<|message|> keeps the reasoning after it; Muse Spark Open's template ends at <|start|>assistant, with or without reasoning_effort. Only calls to offered tools become <tool_call> markup: with no tools, or a tool not offered, a call message stays as the model wrote it, as DSML, LFM2 and Qwen's XML calls do. Thinking off no longer turns the split's reasoning on: self messages stay out of the reply, and the template, which has no thinking switch, hears its lowest reasoning effort rather than its default (xhigh). Raw completion prompts keep the model's text. Tests cut a call before its invoke names the tool, inside a value, a value's closer, a parameter's name, between parameters, after the invoke closed, and in a call's header, at strides 1, 3, 7 and whole; and cover an opened prompt, no tools, an unoffered tool and thinking off.
…e the last call; a reply with no markup streams as it comes
…espace back, as the finished reply strips it
With main's loop-guard stop, chat.zig plus the recipient-reply hook reached 603 lines, past lean's 600. flag (Python's bool(fields.get(key))) is a fields helper, so it moves there unchanged; chat.zig calls fields_mod.flag.
Move the existing probe body unchanged to a sibling module and keep its call through an alias.
…refix kernels, their results in the simdgroup-matrix layout
…the shared prefix before M5; comments and a test name only
…ask 2) The ashhart#3343 loop in Suite.collect passed each tensor-unit flag to controlEdit and at_.geometry, which now take the shared-prefix decision, so on the pre-M5 source the decode edit was never checked though the control is registered there. Call sharedPrefix as Attn.init does, so the decode control edit is checked against the pre-M5 source too.
d1749b5 to
b59c6bc
Compare
|
Thank you @hobbesd. 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 before M5 (no tensor units): today every row of a decode round reads its whole cached prefix through
q27_attn_tail. With this commit the cached keys go through the prefix kernels, as on an M5, with the kernels' result indexing rewritten into the simdgroup-matrix layout. Memory admission counts the same geometry. As built it also lets prompt passes take the frame's width before M5 (512 rows by default instead of 128); that is question 1 of #3604 and still open.TF_QWEN27_SHARED_PREFIX=0plans without the shared prefix, as today (not run served; the rewritten source and the layout check are built at start either way).The first commit is
cc1db32a9, the one you measured in #3604, unchanged and not rebased. The second (e1e9bcaa1) only rewords four docstrings and one test name that still described the tail-only path before M5. The third (6a3b103ed) adds theCHANGELOG.mdline. Nothing was built or run again for this pull request. Every build, test and measurement below is from 16 Nov, of this change over base077998f7d. All except the fmt, whitespace andpr_check.pyruns were made from the working tree before it was committed; the served binary is the one the commit builds (same MD5, in #3604). Since then I have only read main and #3661 and rungit merge-treetwice. The change, the measurements and this text were made by a coding agent (an LLM); none of it is written by hand.077998f7d, three commits. Main has moved on by 51 commits (699094f70when I looked);git merge-treeof the two is clean. The changelog line is added on this base, so it conflicts with main's Unreleased section.atc overlap --json: not run (nothing was run for this pull request). By hand I looked at contract: every chip checks each chip's qwen27 control edit matches its source once, so an M4 run sees a stale M5 edit #3343 and qwen27 metal: drafted window rows share the decode tail attention in pairs, before M5 #3661 only, not at the other open pull requests; families: Nemotron, GLM, Qwen 27B, MiniMax and Dory settings through environment.zig #3558, qwen3_5: CUDA and HIP settings through environment.zig, refused at startup by name #3520 and flashnext: settings through environment.zig, refused at startup by name #3512 also edit the three environment files.e9d629255). Its loop at the top ofSuite.collectinmetal_q27_attn_rows.zigcallscontrolEditandat_.geometrywith each tensor flag (false, then true). The first commit makescontrolEdit's second parameter the shared-prefix decision. The texts merge, but forfalsethe loop then finds no decode edit and so does not check the decode control's edit against the pre-M5 source, although it registers that control there; it should callsharedPrefixasAttn.initdoes. Not changed here, because the merged result is not built or run.git merge-treeagainst its head6aedede59conflicts insimd_attention.zigandenvironment_test.zig. I have not looked at how the two changes behave together.Checks on this head
6a3b103edc22c713e7d1ed6c2a6fe02cd838f791(cc1db32a9plus the comment commit and the changelog line)python3 tools/zig/lean_check.py, on the base and on this head: 221 and 221, 0 new.tools/pr_check.py --base origin/main --pr 3677on this head: 0 errors, 0 warnings.zig build test test-golden -Dcpu=apple_m1 --summary all: 874/874 steps, 14,492 of 14,605 tests passed, 113 skipped, 0 failed. That full count is from before three last edits that touch no kernel or engine source (a build-step description, a comment, one line of theforward-difftest tool). A rerun after the first two gave 874/874 steps again (460 tests run again: 445 passed, 15 skipped; the rest cached).cc1db32a9, which adds the third, was not run through the suite; on it onlyzig fmt --check build.zig zig,git diff --checkandtools/pr_check.pyran, all clean. On the comment commit,zig fmt --checkof the two files it touches andgit diff --checkare clean; the renamed test was not run."draft": false; a resumed prompt against a fresh one; a served run withTF_QWEN27_SHARED_PREFIX=0; a 64K history;tools/bench_openai.py,tools/bench_cache.pyandtools/bench_concurrent.py.Receipt
Main against branch: cells and numbers, or why they can't be run: M4 Max, 48 GB, 4-bit checkpoint with DFlash2,
--context 65536 --prompt-cache-gib 0 --no-thinking, one stream, greedy, order main, branch, branch, main, a fresh server per run, thermal statenominalbefore every request. The full table, the server lines of the four runs and the commands are in Qwen3.8-27B on Metal before M5: drafted decode rounds read cached keys through the prefix kernels (M4 Max, 4-bit with DFlash2: 57 to 73 tok/s over 10K of history, 26 to 55 over 34K, tokens equal) #3604; the two scripts and the thermal probe are in its first comment. Conditions as in Qwen3.8-27B on Metal before M5: drafted decode rounds read cached keys through the prefix kernels (M4 Max, 4-bit with DFlash2: 57 to 73 tok/s over 10K of history, 26 to 55 over 34K, tokens equal) #3604: other applications open, about 9.4 GiB of swap in use, page cache not dropped; main loggedmemory pressure: warningin both of its runs, the branch in neither; the 34K cold request ended in thermal statefairin all four. Startup stream and scratch reserve 7.49 GiB on the branch against 9.93 on main.Your M3 Ultra run of
cc1db32a9against its base is in your comment in #3604: served tokens identical, prompt speed +3.6 to +4.5%, decode after it +3.3% at 2K to +35.2% at 64K.Two cells are slower on the M4 Max, both still open (my read is in Qwen3.8-27B on Metal before M5: drafted decode rounds read cached keys through the prefix kernels (M4 Max, 4-bit with DFlash2: 57 to 73 tok/s over 10K of history, 26 to 55 over 34K, tokens equal) #3604; you called the first a hypothesis and the second open):
Boxes it reaches: Macs before M5 (M1 to M4) serving any checkpoint the qwen27 Metal family opens. Served: the 27B 4-bit only. The contract passed the 27B, GQA16 and 9B decode and prompt shapes on the M4 Max; the two-Mac rank geometry was not run. M5 already takes the prefix kernels and its source is untouched.
New switch? Default, accepted values, profile row:
TF_QWEN27_SHARED_PREFIX, exactly0or1, unset or blank is1;0keeps cached keys on the tail kernel before M5. Registered inplatform/environment.zigwith its test rows and a line indocs/environment.md. No profile row (see above).qwen27_disk.zighashestensorUnits()and not the switch (question 2 of the issue).Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision:
cc1db32a9against077998f7d; Zig 0.17.0,zig build native -Dcpu=apple_m1; Apple M4 Max, 48 GB, macOS 27.2.1 (26B131); the Xcode and Metal toolchain version was not recorded;TensorFold/Qwen3.8-27B-MLX-4bit@22d8d538154e0e5b6f8dcb1fcc74a60a0798104cwithz-lab/Qwen3.8-27B-DFlash2@50307d4c4cde6860d4eee73e2547cd786fe8e8a4.Paths and platforms the change reaches, which I ran and which I could not: decode rounds, memory admission and, as built, prompt pass width before M5 (with the default 512-row frame the branch's prompt passes are 512 rows wide instead of 128; question 1 of the issue, to be reconciled with qwen27 Metal (M1-M4): solo prompt passes take the frame's rows, not 128 (#2907) #2916 and qwen27 metal: M1-M4 size the default prompt frame to their 128-row passes, not 512 #2797 as you wrote). Ran: M4 Max. Could not: M1, M2, M3 and the other M4 variants, and the 8-bit checkpoint with its drafter (neither main nor the branch opened the drafter for it on 48 GB).
Exactness, from
tools/bench_concurrent.py --alone --serialortoken_shaagainst"draft": false: neither was run. What there is: the server'ssha=of each of the eight requests of a run is equal across the four runs (two of main, two of the branch), and so is the SHA-256 of the reply text of the seven streamed ones. Byte checks on the M4 Max:tf-qwen27-attention-fused(returns early on main without tensor units; runs before M5 with this commit): exit 0,552 plans, 462 chains byte-identical through the fused prefix kernel and the tail kernel, …, the same plans without a shared prefix with the same SHA-256; 274 plans of 5 to 8 streams likewise;642 rows … in 201 plans across 8192 … byte-identical to their one-row steps.tf-qwen27-attention-pages:PASS 21 rounds. The tool plans without a shared prefix before M5, so it does not reach the paged prefix kernel; one served run withTF_QWEN27_METAL_KV_PAGES=1gave the same token hashes as the unpaged runs on four requests (up to 3.8K of history).Main against this branch, same machine and checkpoint, posted before asking for a run on our machines (or why you can't): posted in Qwen3.8-27B on Metal before M5: drafted decode rounds read cached keys through the prefix kernels (M4 Max, 4-bit with DFlash2: 57 to 73 tok/s over 10K of history, 26 to 55 over 34K, tokens equal) #3604 on 16 Nov.
Decode speed before and after, from
tools/bench_openai.py: not run; the table is fromhistbench.py(first streamed token to the last; the server's owntok/slines agree within 0.1); the first-request rates and the first-token times are the server's owntok/sandttft.Prompt speed before and after:
tools/bench_cache.py --require-coldwas not run. Time to the first token of the three cold prompts in the same runs, main against the branch: 15.9 and 15.9 s against 15.6 and 15.6 s at 3.6K; 51.1 and 50.4 against 49.8 and 49.7 s at 10.4K; 213.9 and 207.0 against 199.5 and 199.5 s at 34K (the branch's passes are 512 rows wide, main's 128).Licence of any new weights or drafter, or "n/a": n/a, no weights are added or converted.
Kernel contract, only when a GPU kernel is new or changed
FAIL nonfinite control: qwen27 attention 27B decode rows with non-finite values unmasked: 29 of 30720 outputs differ in class; …,red at nonfinite, as planted.SUMMARYline and exit status: Metal,tf-kernel-contract --only qwen27on Apple M4 Max (tensor units false):SUMMARY 30 kernels: 30 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 red as planted, 0 missed; 0 limit problems; gates 105084 ms, limits 10 ms, exit 0. Run on base077998f7d, so without contract: every chip checks each chip's qwen27 control edit matches its source once, so an M4 run sees a stale M5 edit #3343's source check.Checklist
The title and description match the head commit, including the receipt and the stack.
New tests fail before the change, pass after it, and skip cleanly without their dependency. (One new unit test, for the prefix rewrite in
simd_attention.zig, and the switch's rows inenvironment_test.zig; neither was run against the base. The tail-rewrite test no longer checks the text before the tail kernel, since the prefix kernel there now changes.tf-qwen27-attention-fusednow runs before M5, and the contract's decode control is registered there.)Comments and docstrings are one line. No measurements or history in the source.
No personal data, machine names, internal hosts or local paths in the diff or its commits. No AI attribution lines.
Every token still goes through the lane rounds. No serial path, and nothing that needs drafts off.
Drafted output equals
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Not run by me.)No precision traded for speed. If the bits change, the description says which sums change and why. (By design the tail kernel's order. Shown byte for byte up to 8,705 keys on an M4 Max, by served tokens above; see Exactness.)
Decode and prompt processing are no slower than main at one stream and the widest admitted; with
--drafter, no slower than without it, greedy and sampled. (One stream, greedy only; two cells are slower, see above.)