Skip to content

perf(qwen3_5): prompt chunks on the M5 tensor units (27B 16.7K prefill 206 -> ~620 tok/s, decode bits unchanged) - #563

Closed
jkuepker wants to merge 5 commits into
ashhart:mainfrom
jkuepker:qwen35-nax-prefill
Closed

jkuepker wants to merge 5 commits into
ashhart:mainfrom
jkuepker:qwen35-nax-prefill

Conversation

@jkuepker

@jkuepker jkuepker commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Stacked on #520: the first two commits are #520's, and this change is the last three. Gate discussion: #562. My comments on #520 cite these commits by their earlier SHAs: fa3fb35, 2dae608 and 69bd50b are now f65530c, 9023b1d and 249f365. The code is identical; only the tool trailers were removed from the messages.

This is a draft until the PENDING receipt lines are filled in.

What this changes

Qwen3.5-family prompt chunks run on the M5 tensor units: projections of more than batch_rows rows go through MLX's NAX 4-bit qmm (affine_qmm_t_nax, the kernel Nemotron's prompt path already uses), and causal attention for those chunks goes through a NAX attention kernel adapted from Flash Next's tf_attn256_nax. Prompt chunks grow from 128 to 512 rows. Decode and draft windows (≤ 32 rows) keep the row decoder and the per-row attention, so their bits do not change. On Qwen3.8-27B 4-bit (M5 Max), cold prefill goes from 206 to about 620 tok/s at 16.7K tokens. Closes #562.

  • perf(qwen35): prompt rows through MLX's NAX 4-bit qmm: rows past batch_rows take the unsplit NAX qmm. It compiles while the weights load; if it or nax.h's layout check fails, every projection keeps the row decoder.
  • perf(qwen35): 512-row prompt chunks for windows up to 32K; warn without prompt kernels.
  • perf(qwen35): prompt-chunk attention on the tensor units: tf_q35_attn_nax (prefill/q35_attn.metal): 64 query rows a threadgroup, 32-key cache blocks, head size 256, fp32 online softmax. The per-row attention's partials are sized for batch_rows rows when it is present (3.2 GB → 0.2 GB at a 32K window), so 512-row chunks apply at every window.

Receipt

  • Environment: this branch on Qwen3.5 family: the geometry comes from the checkpoint, and Qwen3.8-27B is served #520's head 094cbaa; Zig 0.17.0; macOS 27.0 (26A428), Metal 32023.921; Apple M5 Max (40-core GPU, 128 GB); TensorFold/Qwen3.8-27B-MLX-4bit revision 22d8d538154e0e5b6f8dcb1fcc74a60a0798104c.

  • Exactness:

    • Decode and draft windows: the row decoder and per-row attention are unchanged, and the load-time wide-lane check (qualify.zig) admits 16-row lanes with no warning. bench_concurrent.py --alone --serial: PENDING.
    • Prompt rows (changed reduction order): tf-qwen35-prompt (added) fills the same prompt in 512-row vs 32-row chunks. At 2,000 tokens the last-row logits differ by at most 0.12 (|logit| ≤ 10.6), with the same argmax and a greedy continuation equal for 64 of 64 tokens. At 8,192 tokens: at most 0.08, same argmax, 64 of 64.
    • A 5,031-token greedy prompt gives the same 64-token reply on 094cbaa and on this branch, at 32K and 128K windows.
    • FP64 reference per projection class and for attention: PENDING.
  • Decode speed before and after: PENDING (bench_openai.py, alternating builds).

  • Prompt speed before and after (served, drafts off, fresh prompts, median of reps; ~1k / 4k / 16.7k tokens):

    Build Prompt tok/s
    094cbaa 240 / 239 / 206
    + NAX qmm, 512-row chunks 622 / 594 / 401
    + NAX attention (this branch) 645 / 676–705 / 619–627

    The last two rows were run alternately in one session. prefill_cold.py at 2k / 8k / 32k / 64k / 128k with --prompt-cache-gib 0: PENDING.

  • Quality: HumanEval (164, temperature 0, drafts on) 159/164 on 094cbaa and on this branch; median TTFT 0.77 → 0.31 s.

  • Tests run: PENDING (zig build test test-golden -Dcpu=apple_m1, tools/zig/lean_check.py against the base).

  • Not run, and why: M1–M4 GPUs (no access). They take the TF_SIMD_FRAGS fragment layout; the new kernel compiles there, and frags.check gates it at load, but it is unexercised. CUDA is unaffected (Metal-only change).

Checklist

  • 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. (pending bench_concurrent.py)
  • No precision traded for speed. If the bits change, the description says which sums change and why. (prompt-row sums change to MLX's NAX tile order; FP64 reference pending)
  • Prompt processing is no slower than the last release.
  • The description says which platforms I ran, Metal M1 to M5 and CUDA, and which I could not.
  • New tests fail before the change, pass after it, and skip cleanly without their dependency.
  • Comments and docstrings are one line. No measurements or history in the source. (pending the lean check)
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

BobClawblaw and others added 5 commits October 8, 2026 07:31
…7B is served

The family had the 2B's shape as comptime constants and as constexpr lines in each kernel. The kernels are now
templates whose dimensions the loader prepends as macros when it compiles them; the seven generated projection files
were one body with different constants and become one, compiled once per distinct shape of the checkpoint with the row
decoder's split-K and tile rules kept as code. config.zig reads the dimensions and matches them against a two-entry
table, the pinned 2B and Qwen3.8-27B (hidden 5,120, 64 layers, 48 DeltaNet value heads over 16 key heads, 24 query
heads over 4, a separate output head, bf16 A_log). Buffers, strides, grids and thread counts come from that geometry;
two were invisible on the 2B, where hidden and both mixers' rows are all 2,048: the query buffer takes the wider of the
DeltaNet and attention rows, and the attention partial kernel gets a simdgroup per query head of a group.

The 2B is bit-identical to main (tf-qwen35-forward logits on 300- and 777-token fixtures), passes its 49 exactness
checks and loads the same pipelines. The 27B passes the same 49 checks; against stock mlx-lm its top-1 agreement
matches mlx-lm's own one-row versus chunked agreement at every length (NLL within 0.002); behind the server 120 of 120
concurrent replies equal solo and 72 of 72 solo equal plain, 36 tok/s a stream and 104-108 at eight, 16 GiB RSS.
The fidelity tool falls back to the repository's docs for its third corpus now that the Python engine's left the tree.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
Projections of more than batch_rows rows (prompt chunks, never a decode or
draft window) dispatch affine_qmm_t_nax from the prompt-chunk library
Nemotron already compiles, reading each weight tile once per 64 rows
instead of once per row. Only the unsplit form is taken, so a row's bits
depend on its chunk's row count alone; DeltaNet a/b (48 outputs) and the
head's last row keep the row decoder. The library compiles while the
weights load; if it fails to compile or nax.h's layout check fails, every
projection keeps the row decoder. Decode and draft windows are untouched.

Prompt rows in chunks wider than 32 now carry MLX's accumulation order,
not the row decoder's: logits move by about one bf16 step, and a prompt
filled in wide chunks no longer matches the same prompt fed in 32-row
chunks bit for bit (tf-qwen35-prompt, 2,048 synthetic tokens: last-row
logits within 0.0625 at |logit| 10.5, same argmax, 130,283/248,320
bit-equal; greedy continuations agree for 33 of 64 tokens). Nemotron's
prompt path already works this way. A 5,031-token README prompt gave the
same 64-token greedy reply as the row decoder (sha 8cffa918cbdc).

27B 4-bit on M5 Max, 1k / 4k / 16.7k prompt (localeval, drafts off):
240 / 239 / 206 -> 638 / 549 / 395 tok/s. Server load time unchanged
(3.4-3.7 s).
…ut prompt kernels

With the NAX qmm, wider chunks feed it bigger tiles: 27B on M5 Max,
1k / 4k / 16.7k prompt 638 / 549 / 395 -> 698 / 615 / 416 tok/s, the
same 5,031-token greedy reply. Attention's partial buffers grow with
rows x context (512 rows at 32K: 3.2 GB), so longer windows keep 128.
Without prompt kernels the chunk stays 128 and the server warns that
prompts take the row decoder's projections.
Prompt segments wider than batch_rows run causal attention through
tf_q35_attn_nax (prefill/q35_attn.metal), Flash Next's tf_attn256_nax at
Qwen's layouts: 64 query rows a threadgroup per query head, 32-key cache
blocks read through the tensor units, head size 256, fp32 online softmax,
written [rows, heads, 256] for qwen35_attention_gate. Decode and draft
windows keep the per-row partials and merge. The kernel compiles with the
prompt kernels while the weights load; a failed compile logs a warning
and keeps the per-row attention.

The per-row attention's partial buffers are now sized for batch_rows rows
when the prompt attention is available (512-row chunks at 32K: 3.2 GB of
partials -> 0.2 GB), so 512-row chunks no longer stop at 32K windows.

27B 4-bit on M5 Max, localeval drafts off, 1k / 4k / 16.7k prompt:
622 / 594 / 401 -> 645 / 676-716 / 619-638 tok/s; decode unchanged.
1024-row chunks were 1-2% faster than 512 and are not taken.
tf-qwen35-prompt (512-row vs 32-row fills): 2,000 and 8,192 tokens, same
argmax, greedy continuations equal for 64 of 64 tokens; the 5,031-token
probe reply is unchanged (sha 8cffa918cbdc) at 32K and 128K windows.
@ashhart

ashhart commented Oct 10, 2026

Copy link
Copy Markdown
Owner

Thank you, @jkuepker. Tripling the 27B's prefill on an M5 Max with HumanEval unchanged is a big result, and the profile that led you there (94% of a prompt pass in the row decoder's projections) was exactly the right question.

Two things stand between this and landing. The 27B now runs on its own engine in 1.0.3 (34c4424), so the change needs to live in that family rather than qwen3_5. The second is exactness. Rows in segments wider than 32 go through NAX and narrower ones through the row decoder, so a row's bits depend on how the prompt was cut; your 512-against-32 fill shows it. Our rule is that a prompt row gives the same bits however the prompt is chunked, so a resumed conversation equals a fresh one. Sending every prompt row through the NAX path whatever the chunk width, and leaving decode on the row kernels, would meet it.

We'd like this on the 27B engine: its prefill trails mlx_lm in 1.0.3 and your numbers beat it. If you want to take it there, we'll run the M5 gate. If not, we'll port it ourselves with you credited.

@ashhart ashhart closed this Oct 10, 2026
@jkuepker

Copy link
Copy Markdown
Contributor Author

Thanks, I'll take it to qwen27.

Since opening this I replaced MLX's NAX qmm with a kernel of our own. The 4-bit weights enter the tensor-unit multiply as exact integers, and each group's scale and bias are applied in fp32, which is the row decoder's arithmetic. Against an FP64 reference on real 27B rows (all 64 layers), every projection class's error equals the row decoder's. MLX's kernel was 1.13–1.51× worse on mean error. A row's bits also no longer depend on its chunk's row count.

The prompt attention needed a matching fix. The tensor unit narrows an fp32 P, so P now goes in as two bf16 terms.

On the qwen3_5 engine this gives back part of the speed: 16K cold prefill is 189 → 595 tok/s, against 669 with MLX's rounding.

In qwen27 I'll route every prompt row through it whatever the chunk width, and leave decode on the row kernels. Then I'll measure against 1.0.4 and mlx_lm. I'll open a new PR when it's ready.

@jkuepker

Copy link
Copy Markdown
Contributor Author

Follow-up: there's nothing left to port. 1.0.3's tf_lane_reg already feeds the 4-bit codes to the tensor units as exact integers, applies scale and bias in fp32, and gives chunk-independent prompt bits. On an M5 Max it beats my kernel at every length: cold prefill at 2K/16K/128K is 1018/699/384 tok/s against 669/595/225.

I also tried three restructurings of tf_lane_reg that keep its bits: grid order, one code unpack shared across row blocks, and one wider tensor op. All three were slower. The one change that came out of this is #573, which the 27B engine needs before it can load on macOS 27.

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.

Qwen3.5 family: prompt chunks on the M5 tensor units (27B prefill about 3x), and the gate for the changed prompt-row arithmetic

3 participants