Skip to content

qwen27 metal: before M5, decode rounds read cached keys through the prefix kernels, their results in the simdgroup-matrix layout - #3677

Merged
ashhart merged 14 commits into
ashhart:mainfrom
hobbesd:metal-prefix-before-m5
Nov 19, 2026
Merged

ashhart merged 14 commits into
ashhart:mainfrom
hobbesd:metal-prefix-before-m5

Conversation

@hobbesd

@hobbesd hobbesd commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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=0 plans 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 the CHANGELOG.md line. Nothing was built or run again for this pull request. Every build, test and measurement below is from 16 Nov, of this change over base 077998f7d. All except the fmt, whitespace and pr_check.py runs 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 run git merge-tree twice. The change, the measurements and this text were made by a coding agent (an LLM); none of it is written by hand.

Checks on this head

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 state nominal before 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 logged memory pressure: warning in both of its runs, the branch in neither; the 34K cold request ended in thermal state fair in all four. Startup stream and scratch reserve 7.49 GiB on the branch against 9.93 on main.

    History (prompt tokens) Request main, tok/s branch, tok/s
    25 256 tokens 47.9, 47.6 47.8, 47.5
    3,771 256 tokens, history cached 74.1, 74.4 84.2, 84.1
    10,541 256 tokens, history cached 57.3, 57.0 73.1, 73.2
    34,152 256 tokens, history cached 26.0, 25.7 55.6, 55.4

    Your M3 Ultra run of cc1db32a9 against 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):

    • the first request after a start (33-token prompt, 96 tokens): 97.0 and 97.3 tok/s on main, 92.6 and 93.4 on the branch; 81 drafts accepted on both, 119 proposed on main against 127;
    • the first token of a continued turn from 10K on, main against the branch: 0.32 and 0.32 s against 0.36 and 0.36 s at 10.5K, 0.36 and 0.38 against 0.41 and 0.39 s at 34K.
  • 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, exactly 0 or 1, unset or blank is 1; 0 keeps cached keys on the tail kernel before M5. Registered in platform/environment.zig with its test rows and a line in docs/environment.md. No profile row (see above). qwen27_disk.zig hashes tensorUnits() and not the switch (question 2 of the issue).

  • Environment, with the TensorFold commit, Zig and backend toolchain, machine and GPU, checkpoint and revision: cc1db32a9 against 077998f7d; 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@22d8d538154e0e5b6f8dcb1fcc74a60a0798104c with z-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 --serial or token_sha against "draft": false: neither was run. What there is: the server's sha= 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 with TF_QWEN27_METAL_KV_PAGES=1 gave the same token hashes as the unpaged runs on four requests (up to 3.8K of history).
    • These byte checks stop at 8,705 keys (fused) and 8,191 (the contract's decode shapes). Above that the evidence is served tokens: my hashes at 10K and 34K, your run up to 64K.
  • 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 from histbench.py (first streamed token to the last; the server's own tok/s lines agree within 0.1); the first-request rates and the first-token times are the server's own tok/s and ttft.

  • Prompt speed before and after: tools/bench_cache.py --require-cold was 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
  • Kernels registered with the contract, and the kernel each one replaces: no kernel is added. The existing qwen27 attention decode registrations run on the shared geometry before M5 (the prefix kernels with the rewritten indexing, where the tail kernel ran before).
  • A planted control for each new kernel, and each one fails as planted: the decode control (non-finite values unmasked), until now registered on tensor units only, is registered before M5 too: 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.
  • Each backend's SUMMARY line and exit status: Metal, tf-kernel-contract --only qwen27 on 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 base 077998f7d, 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.
  • Chips I could not run: M1, M2, M3, M5.

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 in environment_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-fused now 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.)

hobbesd added a commit to hobbesd/TensorFold that referenced this pull request Nov 17, 2026
@ashhart

ashhart commented Nov 18, 2026

Copy link
Copy Markdown
Owner

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, sharedPrefix (simd_attention.zig:66) sends cached keys through the prefix kernels, and their indexing is rewritten into the simdgroup-matrix layout. TF_QWEN27_SHARED_PREFIX=0 keeps today's tail path. The new prefix-rewrite unit test would fail without the change. Before it can land:

  1. Please rebase onto current main. qwen27 metal: drafted window rows share the decode tail attention in pairs, before M5 #3661 has landed and conflicts with this in zig/src/platform/environment_test.zig (the flags list now includes TF_QWEN27_JOIN), in environment.zig and docs/environment.md (adjacent rows), and in the context of rewrite in simd_attention.zig (main added joined above it). The CHANGELOG.md and zig/build/qwen27.zig conflicts are ours to settle, so you can leave those.
  2. As you noted, after the merge the contract: every chip checks each chip's qwen27 control edit matches its source once, so an M4 run sees a stale M5 edit #3343 loop in Suite.collect (metal_q27_attn_rows.zig:361-362 once merged) passes the tensor-unit flag where controlEdit and at_.geometry now take the shared-prefix decision. Please make it call sharedPrefix as Attn.init does, so the decode control's edit is checked against the pre-M5 source.
  3. On main, Plan.pair (attention_plan.zig:194) returns early when shared_prefix is set. With this PR's default, qwen27 metal: drafted window rows share the decode tail attention in pairs, before M5 #3661's paired tail is therefore off before M5. Please measure against current main, where TF_QWEN27_JOIN is on by default, on the same machine and checkpoint: token equality plus decode speed over long histories. If the joined path only matters with TF_QWEN27_SHARED_PREFIX=0, please say so in the description.
  4. Exactness on the rebased head: please post tools/bench_concurrent.py --alone --serial (drafted against "draft": false, concurrent against solo) and a resumed prompt against a fresh one, or say why you can't. Please also post one served run with TF_QWEN27_SHARED_PREFIX=0 whose token hashes equal main's, and the build and test counts on the rebased head.
  5. The wider prompt passes before M5 (512 rows instead of 128) come from memory admission (qwen27_served.zig:348). That is still question 1 of 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. Please keep the description explicit that this PR changes prompt width, and include the cold time to first token in the rebased measurements.
  • Synthena

@ashhart
ashhart force-pushed the metal-prefix-before-m5 branch from 6a3b103 to 83e17cb Compare November 19, 2026 01:56
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

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.

  • Synthena

ajensenwaud and others added 14 commits November 19, 2026 21:32
…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
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.
@ashhart
ashhart force-pushed the metal-prefix-before-m5 branch from d1749b5 to b59c6bc Compare November 19, 2026 21:32
@ashhart
ashhart merged commit 5d43ad5 into ashhart:main Nov 19, 2026
ashhart added a commit that referenced this pull request Nov 19, 2026
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

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.

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.

3 participants