Repository navigation
qwen27 metal: with tensor units, a prefix threadgroup runs two query tiles - #3704
Conversation
|
Thanks @mrv777. The two-tile dispatch keeps the existing kernel arithmetic and leaves older chips' choice unchanged. Please bring the rebased
|
|
Thank you @mrv777. On today's main the rebased attention.zig stays at 600 lines, so the size ask is answered. The two-tile dispatch keeps the existing arithmetic and older chips unchanged. Nothing further is needed from you on this PR.
|
…tiles On an M5 a decode round's cached-key attention ran six 16-row query tiles in one threadgroup on a long cache (four below 64 chunks), so each chunk was read once for six tiles. Two tiles a threadgroup gives the GPU more threadgroups and finishes the prefix pass sooner at every context measured; the kernel, its chunks and the merge order are unchanged, so every row's sums are the same. Chips without tensor units keep six and four.
…ttention.zig within 600 lines
fee28a7 to
24a15e2
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
On a chip with tensor units (M5), a drafted decode round reads its cached keys through
q27_attn_prefix, and the host put six 16-row query tiles in each threadgroup on a long cache (four below 64 chunks), so each key chunk was read once for six tiles. With this change those chips run two tiles a threadgroup: three times as many threadgroups, each reading its chunk for two tiles. The kernel, the chunk boundaries and the merge order are unchanged, so every row's sums are the same and tokens are identical. Chips without tensor units keep six and four; today they don't take the prefix kernel at all, and #3677 would change that, so this leaves their choice to measurements on those chips.The change is one line in
Attention.treeRows(zig/src/families/qwen27/attention.zig).q27_attn_prefix_2andq27_attn_prefix_masked_2are already built and registered, and served whenever a round has two tiles.Profiled first (
TF_Q27_PROFILE, one greedy request, M5 Ultra), the attention class per round, by tiles a threadgroup: 1: 10.12 ms, 2: 10.02, 3: 10.10, 4: 10.98, 6 (main): 11.29 at 48K; 1: 21.44, 2: 20.57, 3: 20.86, 4: 21.56, 6: 21.92 at 80K. A larger decode chunk (1,024–4,096 keys past 8K) didn't help and changes tokens, so the cost isn't the partials and merge.atc board,atc whoon the file, and the related 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 and qwen27 metal: before M5, decode rounds read cached keys through the prefix kernels, their results in the simdgroup-matrix layout #3677 were checked; qwen27 metal: before M5, decode rounds read cached keys through the prefix kernels, their results in the simdgroup-matrix layout #3677 doesn't touchattention.zig.atc overlap --json(rebased on 038e4a2, 2026-11-17): no overlapping pull requests found, but the result is incomplete (otherPullRequestsLimit,pullFilesUnavailable,pullRequestsLimit,timeLimit). The one textual conflict is a local probe worktree of mine that edits the same line; it isn't published.Checks on this head
attention.zigstays within 600 lines once merged with main (600 at 739ed5d). Checked on fee28a7:git merge-treewith main 739ed5d is clean, the merged file is 600 lines, and its diff against main is one line.zig fmt --checkand the release build pass. Lean against main 739ed5d: 221 / 221, 0 new.python3 tools/zig/lean_check.pywithtools/zig/lean_new.py: 221 on the base, 221 on this head, 0 new. Measured on the pre-rebase pair 9489c38 / 4e79a0fe9; the rebase brought in no change to this file.zig fmt --check build.zig zigandgit diff --check: pass.zig build native -Doptimize=ReleaseFast -Dcpu=apple_m1 -Dversion=1.0.0andzig build dev-tools -Dcpu=apple_m1: pass.zig build test test-golden -Dcpu=apple_m1: 1163 pass, 12 skip, 1 fail in the unit-test run, plus a second failing run. Both failures also happen on unmodified 038e4a2, so they're inherited:families.glm.wide.test.a failed compile releases every pipeline built before it…: expected 52, found 64.deepseek_v41_encoder/namespaces.jsonlfixture hash.tf-kernel-contract --rotary-accuracy RECEIPT --only "qwen27 attention":SUMMARY 22 kernels: 22 pass gates 1-4, 0 fail, 0 known findings; 4 controls: 4 red as planted, 0 missed; 0 limit problems, exit 0. This is a diagnostic--onlyrun, since no kernel changed. This machine's Metal compiler isn't in the rotary records, so the runner first neededtf-rotary-sweep --out RECEIPT; on base and head alike it refuses withUnrecordedRotaryCompilerotherwise.kernel-contract, because no kernel changed. I stopped it after an hour on one core, before it reached qwen27.Receipt
Environment:
-Doptimize=ReleaseFast -Dcpu=apple_m1.z-lab/Qwen3.8-27B-DFlash2@ 50307d4c4cde6860d4eee73e2547cd786fe8e8a4, default--drafter-bits.serve --drafter z-lab/Qwen3.8-27B-DFlash2 --parallel 4 --context 98304 --max-tokens 16384 --prompt-cache-gib 0, otherwise defaults.Paths and platforms:
q27_attn_prefix.min(tiles, …)= 1 as before.q27_attn_prompt, which this doesn't touch.Public long prompts:
/v1/tokenizeand cut to exact lengths.docs/**/*.mdin path order (prose); 32,768 ofzig/src/**/*.zig(code).// pathline.Exactness, every run, base and head:
bench_concurrent.py --levels 1,4 --alone --serial --temperatures 0 --tokens 256 --reps 3: alone 3/3 and 12/12 equal for code and chat; serial ("draft": false) 1/1 equal for each.bench_openai.pytoken_shaidentical between base and head for both prompts, greedy and 0.6.Decode, long cells:
bench_cache.py --require-cold --tokens 256 --repeats 1 --streams 1; all qualified cold. Two runs each, base vs head:ms/round is the server's median
decode / roundsper request; tokens per round are the same on both.Decode, short prompts and concurrency (
bench_openai.py --tokens 256 --reps 3 --temperatures 0,0.6;bench_concurrent.pyabove). Base runs vs head runs, all within run-to-run spread:Prompt speed, cold first request's
ttft_s, base vs head:Licence of any new weights or drafter: n/a.
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. There's no new test: the change selects an existing registered variant, whose contract gates pass above, and the arithmetic is unchanged.
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. Drafted equals"draft": falseand concurrent equals solo, both shown above. I didn't run a resumed-prompt check: the change doesn't reach the prompt or cache paths.No precision traded for speed. If the bits change, the description says which sums change and why. The bits don't change.
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. Plain one-row decode takes the same tile count as before, so drafted vs drafts-off can only improve.