Skip to content

feat(qwen4_exp): TF_FLASH_DENSE=matrix runs every affine width on the matrix units before M5 - #149

Closed
gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:feat/flashnext-matrix-widths
Closed

gilby wants to merge 2 commits into
ashhart:mainfrom
gilby:feat/flashnext-matrix-widths

Conversation

@gilby

@gilby gilby commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Stacked on #148. The test checkpoint needs its n-gram scale support to load; the only new commit here is the last one.

Problem

On M1–M4, Flash Next's dense projections that aren't 4-bit/32 run through rows.qmv_rows, one row at a time at every window width (decode.py:project). Mixed checkpoints such as oMLX's oQ4e keep many dense layers at 5/6/8-bit in groups of 64 or 128, so they miss the matrix units.

The Qwen dense family already has a backend for exactly these widths, row_matmul.simd_qmm_backend():

  • simd_qmm for 4-bit;
  • simd_qmm_bits for 5/6/8-bit in groups of 64;
  • affine_rows for anything else;
  • each shape checked once.

Change

  • TF_FLASH_DENSE=matrix sends every quantized dense linear through that backend, at every row count. It's opt-in; the default stays rows.
  • Groups of 128 are read as two groups of 64 with the group's scale and bias. These are the same weights, the same thing the M5 lane path already does in _lane_project.
  • tests/test_qwen4_exp_matrix_widths.py covers 5/6/8-bit at groups 64 and 128, and 4-bit at 32 and 64. A 16-row window equals 16 one-row steps bit for bit, and stays within 2% of MLX's quantized matmul.

Checks (M3 Ultra)

Jundot/Qwen3.8-Flash-Next-oQ4e-mtp, MTP drafts 1 stream N=4 prefill 6.8K
default (rows) 83–111 tok/s 121–126 1,148
TF_FLASH_DENSE=matrix 89–130 140–168 1,175
  • Replies are correct (23×19, runnable code, a structured tool call). Drafted and concurrent replies equal serial ones.
  • Tests: Flash Next / n-gram / PLE / simd, 286 passed and 42 skipped, both with and without TF_FLASH_DENSE=matrix. The new test: 7 passed.
  • Routed experts still take qa_expert_* for non-4/32 formats; that's a separate change.
  • The default: I left it at rows so you can decide whether matrix should become the default on M1–M4 after your own measurements.

🤖 Generated with Claude Code

…ad of refusing it (ashhart#142)

oMLX's oQ checkpoints of Qwen3.8 Flash Next store each n-gram table's rows scaled up with one factor in
`ngram_embedding.weight_scale` (e.g. Jundot/Qwen3.8-Flash-Next-oQ4e-mtp: 0.000199). 0.5.0 refused any scale
other than 1, so those checkpoints did not load (ashhart#142).

- sanitize() collects each table's scale by embedding path (still refuses a scale other than 1 when called
  without the dict, as before).
- NGramEmbedding.table_scale and PleTables.scale carry it; embed.ple_lookup and NGramEmbedding's host and resident
  paths multiply the looked-up bf16 rows by it once (fp32 multiply, one bf16 rounding). The same function of each
  row on every path and at every row count, so drafted == undrafted and prompt chunking are unaffected; scale 1
  is the identity.
- tests/test_qwen4_exp_table_scale.py.

Jundot/Qwen3.8-Flash-Next-oQ4e-mtp (MTP drafts) on an M5 Ultra: loads (101.1 GiB resident); 437, runnable code and
a structured tool call; 122-186 tok/s single stream, N=4 223-249, 3,049 tok/s prefill at 6.9K; MTP accepted
53-64%; concurrent replies equal to serial ones.
… matrix units before M5

On M1-M4, Flash Next's dense projections that are not 4-bit/32 (oQ4e and other mixed checkpoints: 5/6/8-bit in
groups of 64 or 128) run rows.qmv_rows, one row at a time at any window width. The Qwen dense family already has a
matrix backend for these widths (row_matmul.simd_qmm_backend: simd_qmm for 4-bit, simd_qmm_bits for 5/6/8-bit in
groups of 64, affine_rows for the rest, each shape checked once).

TF_FLASH_DENSE=matrix sends every quantized dense linear through that backend at every row count; groups of 128
are read as two groups of 64 with the group's scale and bias (the same weights, as the M5 lane path does). Opt-in:
the default stays "rows".

tests/test_qwen4_exp_matrix_widths.py: 5/6/8-bit at groups of 64 and 128, 4-bit at 32 and 64 - a 16-row window
equals 16 one-row steps bit for bit, and stays within 2% of MLX's quantized matmul.

Jundot/Qwen3.8-Flash-Next-oQ4e-mtp (with the n-gram scale change) on an M3 Ultra, MTP drafts: 1 stream 89-130 tok/s
(rows: 83-111), N=4 140-168 (rows: 121-126), prefill 1,175 (1,148); replies drafted and concurrent equal serial ones.
Flash Next / n-gram / PLE / simd tests: 286 passed, 42 skipped, with and without TF_FLASH_DENSE=matrix.
@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby. Mixed checkpoints like oQ4e keep many dense layers at 5, 6 and 8 bits, and your M3 Ultra numbers show what the matrix units buy there: one stream 83-111 to 89-130 tok/s, four streams 121-126 to 140-168. Part of this landed in 0.6.0, out tonight, from our side: before M5, Flash Next's 5-, 6- and 8-bit projections of four or more rows now run on the matrix units with the per-row bits. Your numbers are against 0.5.0, so could you re-measure oQ4e on 0.6.0? What remains, most likely the one- to three-row windows and groups of 128, is what we'd take in 0.6.1. One change from the PR: when a faster path gives the same bits, we make it the default for the shapes it wins on rather than adding a switch, so we'd measure per width against the row kernels.

@gilby

gilby commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Re-measured on 0.6.0, as asked: M3 Ultra, Jundot/Qwen3.8-Flash-Next-oQ4e-mtp, MTP drafts, greedy, two runs of the same 4 prompts.

End to end

build 1 stream (tok/s) N=2 N=4 drafted and concurrent == serial
0.6.0 92–98 107 135–140 yes
0.6.0 + this PR, TF_FLASH_DENSE=matrix 88–98 115–124 150–152 yes

0.6.0's qmv_rows_mma closed the one-stream gap: 0.5.0 rows were 83–111 against 89–130 with this PR. This PR still adds ~8–11% at N=4 and ~7–16% at N=2.

This PR's commit (8d55431 here) cherry-picks cleanly onto 0.6.0. Its tests pass:

  • tests/test_qwen4_exp_matrix_widths.py: 7 passed.
  • The Flash Next, n-gram, PLE and simd tests: 656 passed, 81 skipped, with and without TF_FLASH_DENSE=matrix.

Per width against the row kernels

Each cell is qmv_rows on 0.6.0 (row loop below 4 rows, qmv_rows_mma from 4) / this PR's path (row_matmul.simd_qmm_backend, g128 read as 2×64), in µs per call with 8–24 different weights of the shape in flight. The shapes are oQ4e's; "in model" counts how many tensors of that shape the checkpoint has.

format N × K in model 1 row 2 3 4 8 16
5/g128 2560×6144 36 34.4 / 38.7 40.4 / 38.2 49.7 / 38.3 55.8 / 39.1 48.8 / 38.9 72.3 / 55.5
5/g128 6144×2560 26 31.7 / 38.3 39.4 / 38.5 49.9 / 38.0 53.0 / 39.2 50.5 / 40.6 76.2 / 59.7
8/g128 640×2560 98 17.9 / 20.4 18.1 / 19.3 19.0 / 19.5 29.8 / 22.6 25.6 / 19.9 28.5 / 25.5
8/g128 2560×640 49 22.6 / 20.2 18.4 / 19.6 19.0 / 19.6 20.1 / 19.1 24.5 / 19.7 35.8 / 27.3
5/g64 10240×2560 4 59.5 / 59.7 69.9 / 64.5 96.9 / 62.1 91.1 / 62.5 95.5 / 61.9 144.0 / 86.7
6/g64 12288×2560 5 69.8 / 73.7 84.5 / 73.6 110.3 / 73.3 114.7 / 73.3 116.3 / 71.6 176.5 / 97.6
8/g64 2560×6144 2 43.7 / 43.6 46.3 / 43.4 49.9 / 43.3 57.6 / 43.4 55.0 / 44.8 79.3 / 54.7
4/g64 10240×2560 26 54.1 / 55.4 67.8 / 63.4 88.3 / 63.9 74.9 / 63.2 78.4 / 64.0 121.0 / 81.9
8/g64 (lm_head) 248320×2560 1 1233 / 1254 1227 / 1250 1375 / 1252 1572 / 1260 1580 / 1254 2876 / 1393

What the table shows:

  • One row: the row loop is level or faster. At groups of 128 it wins by 10–20%, because this PR repeats each scale to read the group as two of 64, which doubles the scale bytes. A native g128 scale load in simd_qmm_bits would remove that.
  • Two and three rows: the row loop grows linearly and this PR's path stays flat. That is the gap you expected.
  • Four rows and up: qmv_rows_mma is 1.2–2× slower than simd_qmm_bits on these shapes, and at 4 rows it is often slower than the row loop at 3. So the remaining gap is not only the one- to three-row windows and g128; it also covers the verify widths qmv_rows_mma now takes.
  • Every cell was bit-checked: a 16-row window equals 16 one-row calls on both paths.

A question before I rework this into a default

"Same bits" can mean two things here:

  1. Row-count invariance. A row's bits never depend on the window, so drafted == serial and concurrent == solo. This PR has that.
  2. Identical to qmv_rows. This PR does not have that. simd_qmm_bits accumulates in another order, like the Qwen dense change in 0.4.0, so switching the default changes greedy replies against 0.6.0.
  • If (1) is enough, I'll rebase this as a per-(bits, group, N, K) default: the matrix backend where it wins at the verify widths, rows elsewhere, chosen once per linear from format and shape, and add a native g128 scale load so one-row g128 stops losing. No switch.
  • If you want (2), the lever is qmv_rows_mma itself, since it trails simd_qmm_bits from 4 rows up. I'm happy to look at that instead.

Which do you prefer?

🤖 Generated with Claude Code

@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks for re-measuring, @gilby, and for the per-width table: it answers the question better than we asked it.

(1) is enough. Our contract is that a row's bits never depend on the window or the other rows, so drafted == serial and concurrent == solo. A different accumulation order at the same precision is fine, as with the Qwen dense change in 0.4.0, on two conditions:

  • It accumulates at the same precision as qmv_rows, and we check the new order against an fp32 reference forward (KL, top-1, and perplexity on wikitext and code) before it becomes the default. We'll run that part.
  • No width gets slower: the per-(bits, group, N, K) choice takes the faster path per shape, and the native g128 scale load closes the one-row g128 gap.

So yes to the per-shape default with no switch. If you'd like to do that rework on 0.6.0, go ahead. Otherwise we'll rebase it ourselves in 0.6.1, keeping your commit, and post our M3 Ultra numbers here.

@ashhart

ashhart commented Oct 1, 2026

Copy link
Copy Markdown
Owner

Thanks again for the per-width table. Both PRs are ported onto 0.6.1 and pass their tests on an M3 Ultra, still behind the switches for now. Your table shows where the matrix path wins: two to sixteen rows, the verify and multi-stream widths. At one row it's level or slower, and slower at groups of 128. So the plan is the per-shape default we discussed, with no switch: the matrix path where it's faster, rows elsewhere, and a native g128 scale load. Before it becomes the default, we'll run the fp32-referenced quality check and served tok/s at one, two and four streams. If you've started that rework, push it to the PR and we'll take yours; otherwise we'll build it on top of your commits. We'll post the numbers here.

@ashhart

ashhart commented Oct 2, 2026

Copy link
Copy Markdown
Owner

This is in TensorFold 0.6.3, with your commit under your name in the release history: https://github.com/ashhart/TensorFold/releases/tag/v0.6.3

TF_FLASH_DENSE=matrix now runs every dense projection on the matrix units on Macs before M5, and the fused DeltaNet stack uses the matrix kernel by default.

Thank you, @gilby!

@ashhart ashhart closed this Oct 2, 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