Skip to content

qwen27 metal: with tensor units, a prefix threadgroup runs two query tiles - #3704

Merged
ashhart merged 3 commits into
ashhart:mainfrom
mrv777:qwen27-prefix-two-tiles
Nov 19, 2026
Merged

ashhart merged 3 commits into
ashhart:mainfrom
mrv777:qwen27-prefix-two-tiles

Conversation

@mrv777

@mrv777 mrv777 commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

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_2 and q27_attn_prefix_masked_2 are 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.

Checks on this head

  • Head commit: fee28a7: a91d8ee on main 038e4a2, plus a commit that keeps the tile choice's comment on its line, so attention.zig stays within 600 lines once merged with main (600 at 739ed5d). Checked on fee28a7: git merge-tree with main 739ed5d is clean, the merged file is 600 lines, and its diff against main is one line. zig fmt --check and the release build pass. Lean against main 739ed5d: 221 / 221, 0 new.
  • Lean checker, python3 tools/zig/lean_check.py with tools/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.
  • Tests run on this head:
    • zig fmt --check build.zig zig and git diff --check: pass.
    • zig build native -Doptimize=ReleaseFast -Dcpu=apple_m1 -Dversion=1.0.0 and zig 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.
      • The deepseek_v41_encoder/namespaces.jsonl fixture hash.
    • Golden corpus: 322/335 and 55/55 equal to server goldens, 0 unexpected differences.
    • Kernel contract, qwen27 attention gates, 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 --only run, since no kernel changed. This machine's Metal compiler isn't in the rotary records, so the runner first needed tf-rotary-sweep --out RECEIPT; on base and head alike it refuses with UnrecordedRotaryCompiler otherwise.
  • CI: pending on this head.
  • Not run, and why:
    • The full kernel-contract, because no kernel changed. I stopped it after an hour on one core, before it reached qwen27.
    • CUDA, HIP and XPU: this file is Metal-only.

Receipt

  • Environment:

    • TensorFold: base 9489c38 vs head 4e79a0fe9 (the same commit before the rebase). Release builds -Doptimize=ReleaseFast -Dcpu=apple_m1.
    • Zig 0.17.0, Apple metal 32023.922.
    • Apple M5 Ultra, 96 GB, macOS 27.2.
    • Qwen3.8-27B MLX 4-bit (affine, group 64): a local conversion, not a hub revision.
    • Drafter 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.
    • One model process; a fresh server per run; order base, head, head, base with 90 s cool-downs.
  • Paths and platforms:

    • The change reaches drafted decode rounds on M5 (tensor units), where verify rows read cached keys through q27_attn_prefix.
    • A one-row plain decode has one tile, so it takes min(tiles, …) = 1 as before.
    • Prompt passes on M5 use q27_attn_prompt, which this doesn't touch.
    • Chips without tensor units are unchanged by construction; I couldn't run M1–M4.
    • Ran: M5 Ultra only.
  • Public long prompts:

    • The commit's own text at 9489c38, tokenized by the server's /v1/tokenize and cut to exact lengths.
    • 8,192 tokens of docs/**/*.md in path order (prose); 32,768 of zig/src/**/*.zig (code).
    • 65,536 and 81,920 of the two lists interleaved, each file preceded by a // path line.
  • 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.py token_sha identical between base and head for both prompts, greedy and 0.6.
    • All four long cells give the same token hash in every run on both binaries.
    • An earlier 4-stream run of the same choice (2×48K and 4×32K at once) matched its solo hashes.
  • Decode, long cells: bench_cache.py --require-cold --tokens 256 --repeats 1 --streams 1; all qualified cold. Two runs each, base vs head:

    Prompt ms/round, base ms/round, head Decode tok/s, base Decode tok/s, head Change
    prose 8K 29.15, 29.06 29.15, 29.06 132.6, 133.0 132.6, 133.0 0
    code 32K 34.67, 34.57 34.21, 34.17 175.5, 175.6 177.5, 177.7 +1.2%
    mixed 64K 39.93, 40.05 39.65, 39.58 148.7, 148.2 149.9, 149.9 +0.9%
    mixed 80K 43.52, 43.50 42.11, 42.02 108.9, 108.8 112.3, 112.9 +3.4%

    ms/round is the server's median decode / rounds per 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.py above). Base runs vs head runs, all within run-to-run spread:

    • fibonacci-raw, greedy: 229.9 and 230.7 vs 229.5 and 230.4. At 0.6: 240.1 and 241.0 vs 239.9 and 240.5.
    • gpu-chat-no-think, greedy: 162.2 and 162.9 vs 162.2 and 162.7. At 0.6: 149.1 and 149.5 vs 148.9 and 149.4.
    • Aggregate at 4 streams, code: 390.5 and 389.5 vs 390.0 and 389.3.
    • Aggregate at 4 streams, chat: 340.4 and 306.5 vs 305.1 and 305.9. Base's first run is the outlier; its second run and both head runs agree.
  • Prompt speed, cold first request's ttft_s, base vs head:

    • 8K: 5.31, 5.29 vs 5.31, 5.29.
    • 32K: 23.29, 23.30 vs 23.41, 23.30.
    • 64K: 52.96, 55.16 vs 55.95, 54.78.
    • 80K: 72.37, 76.44 vs 76.35, 75.67.
    • Prompt passes don't reach this code. At 64K and 80K the spread follows run order: the first run (base) was the coolest machine.
  • 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": false and 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.

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

Thanks @mrv777. The two-tile dispatch keeps the existing kernel arithmetic and leaves older chips' choice unchanged. Please bring the rebased attention.zig below 600 lines: current main is already 600 and this patch makes it 601. The supplied M5 results and their pre-rebase scope are recorded separately.

  • Synthena

@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

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.

  • Synthena

mrv777 and others added 3 commits November 19, 2026 18:16
…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.
@ashhart
ashhart force-pushed the qwen27-prefix-two-tiles branch from fee28a7 to 24a15e2 Compare November 19, 2026 18:16
@ashhart
ashhart merged commit 703513f into ashhart:main Nov 19, 2026
2 of 10 checks passed
@ashhart

ashhart commented Nov 19, 2026

Copy link
Copy Markdown
Owner

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.

ashhart added a commit to atakann/TensorFold that referenced this pull request Nov 19, 2026
ashhart added a commit that referenced this pull request Nov 19, 2026
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.

2 participants