Skip to content

feat(glm5_next): each decode linear takes the matrix or the row kernels by its format and shape, no switch - #154

Closed
gilby wants to merge 5 commits into
ashhart:mainfrom
gilby:feat/glm-matrix-projections
Closed

gilby wants to merge 5 commits into
ashhart:mainfrom
gilby:feat/glm-matrix-projections

Conversation

@gilby

@gilby gilby commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

What this changes

GLM-5.3-Flash's decode linears now take the matrix kernels or the row kernels by their format and shape, with no switch (TF_GLM_DENSE is gone). linear.choose(bits, group, N, K) decides once per linear and never from the rows, so every row count of a linear runs the same arithmetic. Before M5, groups of 128 are read natively, with one scale load a group.

The per-shape rule

linear before M5 M5
5-, 6- and 8-bit, groups of 64 simd_qmm_bits lane_qmm
5-, 6- and 8-bit, groups of 128 simd_qmm_bits, native g128 rows
4-bit, groups of 128 simd_qmm_bits at 4 bits, native g128 (new) rows
4-bit, groups of 64 rows (qmv_rows) rows
4- or 8-bit g64 with 64 or 128 inputs (KDA's f_b / g_b) rows (qmv_quad_rows) rows
float32 activations, CPU MLX's calls MLX's calls

Other details:

  • Split linears: a linear stacked from parts of different formats gives each part its own kernel, at one row too.
  • Fused MoE: it takes its shared expert from project() whenever either of the expert's linears is on the matrix kernel.
  • Native g128: simd_qmm_bits already loaded one scale per group of 128 at 5 to 8 bits. reads() now also admits 4-bit codes when a caller asks by name. BITS, fits() and the Qwen dense backend are unchanged. On M5, lane_qmm reads groups of 32 or 64, so groups of 128 stay on rows there.

Why these choices (M3 Ultra, µs per call over 3 to 26 weights of the shape, rows → matrix, interleaved, minimum of 5):

format N × K (GLM-5.3-Flash-oQ4e-mtp) 1 row 2 4 8 16 choice
5/g64 24896 × 4096 (KDA in) 141 → 144 170 → 160 346 → 155 664 → 153 1235 → 259 matrix
6/g64 16384 × 1536 (MLA q_b) 48 → 63 72 → 65 136 → 65 226 → 65 372 → 88 matrix
8/g64 24896 × 4096 (KDA in) 176 → 185 188 → 195 293 → 199 585 → 194 1046 → 257 matrix
8/g64 4096 × 4096 (shared gate/up) 34 → 40 41 → 43 47 → 40 91 → 39 172 → 53 matrix
8/g64 154880 × 4096 (head) 1015 → 1022 1168 → 1097 1939 → 1076 4272 → 1164 7971 → 1589 matrix
8/g64 8192 × 128 (f_b / g_b) 9.2 → 12.8 10.7 → 13.0 11.5 → 13.4 14.5 → 15.7 19.8 → 14.1 rows
5/g64 8192 × 128 (f_b / g_b) 12.9 → 15.7 28.8 → 18.9 37.6 → 21.3 61.4 → 18.0 131.8 → 18.4 matrix
4/g64 4096 × 4096 (synthetic) 24 → 27 30 → 46 45 → 47 69 → 46 139 → 57 rows
4/g128 4096 × 4096 (synthetic, native) 23 → 31 36 → 39 60 → 37 97 → 37 177 → 58 matrix
8/g128 4096 × 4096 (synthetic, native) 40 → 43 61 → 44 104 → 40 149 → 42 218 → 56 matrix

What the table shows:

  • One row: the row kernels lead by 0 to 30%.
  • From two rows: where qmv_rows re-reads the weight or loops by row, the matrix kernels win: 2 to 7 times at 4 to 16 rows.
  • The decode path's windows: with MTP drafts every target forward is a verify window (up to 4 rows, more in concurrent rounds), so these are the widths the choice is made for.
  • 4-bit groups of 64: qmv_rows stays ahead to 3 rows and level at 4, so it stays on rows. From 8 rows the matrix kernel wins, a candidate if concurrent rounds matter more.
  • f_b / g_b at 8 bits: qmv_quad_rows stays ahead to 8 rows.

Receipt

  • Environment: TensorFold v0.6.5 (609ca41) plus this branch at 8e7a115; MLX 0.32.3; Apple M3 Ultra, 80-core GPU; GLM-5.3-Flash-oQ4e-mtp (oMLX oQ4e: 554 tensors at 5, 6 or 8 bits, all in groups of 64).

  • Exactness:

    • Load-time window check: passes, exact window 16 rows.
    • Real-checkpoint test (TF_GLM5_MODEL, first 8 layers past 4,096 keys): 2- to 16-row windows equal one-row steps.
    • Per-shape tests: every chosen kernel was checked for row invariance at 1 to 16 rows.
  • Quality against an fp32 reference:

    • Per linear, on the checkpoint's own weights: mean |error| / rms of the fp32 product, 16 rows with an outlier channel.

      • 8-bit: matrix and rows are identical, 0.0012 to 0.0018.
      • 5- and 6-bit: matrix gives 0.0016 to 0.0017, MLX's one-row qmv 0.0054 to 0.0075.
      • So the matrix kernels are as accurate or more accurate at every shape.
    • First 12 layers plus the head: compared against the same layers with float32 activations, over 8 verify windows of 4 rows after two synthetic 1,024-token prompts.

      kernels mean KL max KL top-1 vs fp32
      rows 0.0025 / 0.0015 0.017 / 0.008 0.94 / 0.97
      per shape 0.0013 / 0.0015 0.008 / 0.008 0.94 / 1.00

      The full-depth KL, top-1 and perplexity check you described is still yours to run.

  • Decode speed:

    • Load-time forward ms by window width on a quiet GPU. Rows from one start, per shape from two:

      kernels 1 2 4 8 16
      rows (0.6.2) 26.8 32.8 46.4 81.7 150.7
      per shape 28.3 to 28.6 34.9 to 35.0 44.0 to 44.1 64.7 to 64.9 113.3 to 116.0
    • Served tok/s at 1, 2 and 4 streams (bench_concurrent, 256 tokens a stream, 2 reps a cell, aggregate tok/s). The baseline is the 0.6.2 release, which has the row kernels. This branch ran on v0.6.5 together with Mac prefill: keep the chunk start before a long history's boundary (--tail-checkpoint-tokens) #287 and feat(gemma4): the dense 31B layout and oQ per-layer mixed widths on the Mac decode kernels #360, which don't touch GLM's decode linears. The GPU was quiet for 60 s before each arm, and the arms ran interleaved, rows → per shape → rows → per shape. Each cell gives the two rounds:

      streams code T=0 chat T=0 code T=1 chat T=1
      1 57.7, 57.5 → 58.2, 58.0 48.4, 49.0 → 47.4, 47.4 51.3, 53.2 → 55.3, 55.2 45.2, 45.0 → 47.2, 47.7
      2 82.3, 85.2 → 92.4, 87.8 61.6, 63.9 → 73.6, 72.1 64.3, 64.7 → 70.9, 71.5 55.0, 57.9 → 64.9, 65.7
      4 84.0, 87.3 → 110.7, 113.1 69.5, 72.0 → 91.5, 91.4 71.4, 69.0 → 89.7, 88.2 65.1, 66.8 → 80.6, 79.3

      bench_openai single stream (median of 3) gives the same picture. Code T=0 went from 58.5, 58.5 to 58.1, 55.3, chat T=0 from 48.8, 48.7 to 47.2, 47.3, code T=1 from 53.4, 53.7 to 55.3, 56.1, and chat T=1 from 47.4, 45.9 to 46.0, 44.5.

      So at four streams the per-shape choice is 25 to 32% faster, and at two streams 3 to 17% faster. At one stream it's level, with chat about 2 to 3% behind, which matches the row kernels' lead at one row.

    • Served exactness: bench_concurrent --levels 1,2,4 --alone --serial, code and chat at T=0 and T=1, 0 failed. Every reply equals its request alone and "draft": false.

    • Greedy replies against 0.6.2 (rows), 3 prompts: one is identical. The other two first differ inside the reasoning at a near-tie, at character 144 and character 397, and both replies stay coherent. Bits change here as expected, since the linears that move to the matrix kernels sum in another order.

  • Prompt speed: the prefill path is untouched (rows_exact=False keeps MLX's calls); not measured.

  • Tests, M3 Ultra:

    test files passed skipped failed
    tests/test_glm5* 199 2 0
    tests/test_simd_qmm* 78 0 0

    The new tests/test_glm5_dense_choice.py has 41 tests. On the previous head, 39 of them fail.

  • Not run:

    • A resumed prompt against a fresh one, as a separate served check.
    • M5: no free M5 for GLM. The M5 rule keeps what the first commit measured there, lane_qmm for the 5-, 6- and 8-bit groups of 64.

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. (Window tests and the real-checkpoint window test pass. Served --alone --serial at 1, 2 and 4 streams passes, so drafted equals "draft": false and concurrent equals solo. Resumed against fresh hasn't been run separately.)
  • No precision traded for speed. Bits change for the linears that move to the matrix kernels: their 64-input group sums accumulate in another order at fp32, as in the Qwen dense change in 0.4.0.
  • Prompt processing is no slower than the last release. (Prefill code unchanged; not measured.)
  • 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.
  • No personal data, machine names, internal hosts or local paths. No AI attribution lines.

@gilby

gilby commented Sep 30, 2026

Copy link
Copy Markdown
Contributor Author

Added 469d3ed: under TF_GLM_DENSE=matrix, the fused MoE's shared expert now runs on the matrix units too.

  • How: moe_rows(..., shared_out=) takes the shared expert's output from the MoE block, computed through project(), and always takes the split path; the routed experts stay fused.
  • Tests: the fused MoE and the unfused block now agree under matrix, so the two skips from the first commit are gone. tests/test_glm5* passes with no skips in both modes: M3 Ultra 142 passed / 7 skipped, M5 Ultra 141 / 8.

Measured with GLM-5.3-Flash-oQ4e-mtp:

  • no speed change on either chip. M3 Ultra: 1 stream 48–54 tok/s, N=4 78. M5 Ultra: 1 stream 68–74, N=4 120.
  • So the 8-bit shared expert was never a kernel bottleneck; the remaining gap to uniform 4-bit is the 8-bit weights' bytes.
  • The value of this commit is consistency: one arithmetic for the shared expert on every path.

🤖 Generated with Claude Code

@ashhart

ashhart commented Sep 30, 2026

Copy link
Copy Markdown
Owner

Thanks, @gilby, the GLM side of #149. Mixed checkpoints like oQ4e keep GLM-5.3-Flash's attention, KDA and shared-expert projections at 5 to 8 bits, and before M5 those stay on one-row arithmetic, so the matrix units are worth having there too. Choosing the kernel once per linear from its format and shape, never from the input, is the right rule: every row count takes the same kernel. It came in after today's 0.6.0 cutoff, so it goes into 0.6.1 beside #149, and we'll measure both the same way: per width against the row kernels, and as the default wherever it gives the same bits and never runs slower.

@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.

@tpischke

tpischke commented Oct 2, 2026

Copy link
Copy Markdown
Contributor

Numbers from a 64-core M5 Ultra 256 GB with the uniform 4-bit pack (Vontra/GLM-5.3-Flash-MLX-4bit-MTP), this branch at 469d3ed, MLX 0.32.3, drafts on, short prompts, 1,500 tokens per stream, total t/s:

streams TF_GLM_DENSE=rows TF_GLM_DENSE=matrix
1 65.7, 80.4 70.5, 70.1
2 94.6 90.8
4 115.8, 115.3 117.0, 117.5

So on uniform 4-bit and 64 GPU cores the matrix path is level at four streams, slightly behind at two, and inside the noise at one (the two rows runs differ more from each other than from matrix). No quality check done. Happy to rerun on a rebased branch, or with a mixed-bit pack, if that helps decide the per-shape default.

@ashhart ashhart mentioned this pull request Oct 3, 2026
gilby added 5 commits October 5, 2026 22:11
…jections on the matrix units

GLM-5.3-Flash's decode projections keep MLX's one-row arithmetic: MLX's quantized_matmul for one row, qmv_rows (or
a row loop) for a window. Mixed-bit checkpoints (oMLX oQ4e keeps attention, KDA, indexer, MLA and shared-expert
projections at 8-bit, some at 5/6) therefore never reach the matrix units the Qwen dense family uses since 0.4.0.

TF_GLM_DENSE=matrix (opt-in; default "rows" is unchanged) sends every rows_exact projection in project() -- one row
included -- and KDA's small f_b/g_b projections on the decode path through one kernel family per chip: before M5
the Qwen dense backend (row_matmul.simd_qmm_backend: simd_qmm / simd_qmm_bits / affine_rows, each shape checked
once), on M5 lane_qmm (weights tiled once). Groups of 128 are read as two groups of 64 (the same weights). The CPU
and prefill (rows_exact=False) keep MLX's calls. The fused MoE (its shared expert), fused KDA, MLA absorb and the
routers are unchanged: each is already one kernel at every row count.

Two tests compare the fused MoE (MLX's one-row bits for the shared expert) against the unfused block, whose
projections this mode moves; they are skipped under TF_GLM_DENSE=matrix.

GLM-5.3-Flash-oQ4e-mtp on an M3 Ultra, MTP drafts: the load-time window check passes (exact window 16 rows);
forward ms by width 1/4/8/16 rows 27.5/43.8/66.1/113.5 (rows: 26.6/46.3/81.4/~150); 1 stream 47.5-53.9 tok/s
(rows 46.5-48.0), N=4 77-80 (65.4); MTP accepted 72-82%; drafted and concurrent replies equal serial ones.
tests/test_glm5*: 142 passed, 7 skipped (rows); 139 passed, 10 skipped (matrix).
…_qmm does not cover

Decided once per linear from its format and shape (lane_qmm.supports with a bf16 probe), never from the input,
so every row count takes the same kernel. Without it, linears lane_qmm does not read (fp32 scales, groups it
does not take, K or N off its multiples) gave row-dependent bits on the M5.
…xpert on the matrix units

moe_rows takes a shared_out [R, D] computed by the caller (then always the split path: shared apart, routed fused,
the same combine); under TF_GLM_DENSE=matrix the MoE block passes its shared expert computed through project(),
so it runs on simd_qmm/simd_qmm_bits (M1-M4) or lane_qmm (M5) at every row count, as the unfused block does. The
fused-vs-row-by-row tests hold again under matrix (their C1 skips are removed; the 8-bit shared-expert test passes
the block the shared output as serving does).
…cale load a group

simd_qmm reads 4-bit weights in groups of 32 or 64 only, so a 4-bit group of 128 reached the matrix
kernels as two groups of 64 with every scale and bias repeated. simd_qmm_bits already loads one scale
for a group of 128; reads() admits 4-bit codes (two words a lane) when a caller asks for them by name.
BITS, and so fits() and the Qwen dense backend, are unchanged.
…ls by its format and shape, no switch

Replaces TF_GLM_DENSE. linear.choose(bits, group, N, K) decides once per linear, never from the rows,
so every row count of a linear takes the same arithmetic and drafted == serial, concurrent == solo:
- before M5: 5-, 6- and 8-bit linears in groups of 64 or 128, and 4-bit in groups of 128, take
  simd_qmm / simd_qmm_bits; groups of 128 are read natively, one scale load a group.
- 4-bit groups of 64 keep qmv_rows, and 4- or 8-bit linears of 64 or 128 inputs (KDA's f_b / g_b)
  keep qmv_quad_rows: both share reads across a window and stay ahead up to verify widths.
- on M5: 5-, 6- and 8-bit groups of 64 take lane_qmm; the rest keep the row kernels.
- float32 activations and the CPU keep MLX's calls.
A split linear (parts of different formats) gives each part its own kernel at one row too. The fused
MoE takes its shared expert from project() when either of its linears is on the matrix kernel.
@gilby
gilby force-pushed the feat/glm-matrix-projections branch from 469d3ed to 8e7a115 Compare October 5, 2026 21:12
@gilby gilby changed the title feat(glm5_next): TF_GLM_DENSE=matrix runs the decode path's dense projections on the matrix units feat(glm5_next): each decode linear takes the matrix or the row kernels by its format and shape, no switch Oct 5, 2026
@gilby

gilby commented Oct 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, @ashhart. I've pushed the rework, rebased onto v0.6.5. Since it's a rebase, the branch was force-pushed; the three earlier commits are unchanged apart from conflicts.

What's new

  • fb46e19: simd_qmm_bits reads 4-bit codes in groups of 128 with one scale load per group (5 to 8 bits already did).
  • 8e7a115:
    • TF_GLM_DENSE is gone. linear.choose(bits, group, N, K) picks the kernel once per linear, never from the rows.
    • Before M5, the matrix kernels take the 5-, 6- and 8-bit linears and 4-bit in groups of 128, with groups of 128 read natively.
    • Two kinds stay on rows, where their shared-read kernels lead up to the verify widths: 4-bit in groups of 64 (qmv_rows), and KDA's 128-input f_b / g_b at 8 bits (qmv_quad_rows).
    • On M5, lane_qmm takes the 5-, 6- and 8-bit groups of 64.

Numbers so far (M3 Ultra, GLM-5.3-Flash-oQ4e-mtp; per-shape table in the description)

  • Kernels: at one row the row kernels lead by up to 30%. From two rows the matrix kernels win wherever qmv_rows re-reads or loops, by 2 to 7 times at 4 to 16 rows.
  • Load-time forward by width, 1 / 8 / 16 rows: 28.4 / 64.7 / 113.2 ms, against 26.6 / 81.4 / ~150 for the row kernels.
  • fp32 reference:
    • Per linear on the checkpoint's weights, the matrix kernels are as accurate or more accurate everywhere. 8-bit is identical; at 5 and 6 bits the mean error is 3 to 4.6 times lower than MLX's one-row qmv.
    • On the first 12 layers against float32 activations, mean KL is 0.0013 / 0.0015 against rows' 0.0025 / 0.0015.
  • Exactness: the real-checkpoint window test passes at 2 to 16 rows.
  • Tests: tests/test_glm5* 199 passed, 2 skipped.

Still owed: served tok/s at 1, 2 and 4 streams with --alone --serial. The GPU was shared with another long job in this window, and runs swung 3 to 5 times, so I'll post them from a quiet one. I couldn't run M5 either. @tpischke, a rerun on the M5 Ultra at this head would help, with both your uniform 4-bit pack and a mixed one. The uniform pack keeps 4-bit groups of 64 on rows, so it should match TF_GLM_DENSE=rows.

@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @gilby. Deciding the kernel once per linear, from its format and shape and never from the input, is the right rule, and your tables show where it pays: verify windows and concurrent rounds.

The Python engine is frozen (#286), so this won't merge there, and GLM-5.3-Flash isn't on the Zig engine yet. The rule itself is already how the Zig engine is built. Nemotron's decode projections run on the tensor units at every row count, one row included, and the Kimi K3 kernels give a row the same bits alone or inside a window. When GLM-5.3-Flash comes over, its mixed-bit projections start from that rule, and the oQ4e checkpoint you measured is the test case, with your credit.

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

3 participants