Repository navigation
Conversation
|
Added 469d3ed: under
Measured with
🤖 Generated with Claude Code |
|
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. |
|
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. |
|
Numbers from a 64-core M5 Ultra 256 GB with the uniform 4-bit pack (
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 |
…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.
469d3ed to
8e7a115
Compare
|
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
Numbers so far (M3 Ultra,
Still owed: served tok/s at 1, 2 and 4 streams with |
|
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. |
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_DENSEis 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
simd_qmm_bitslane_qmmsimd_qmm_bits, native g128simd_qmm_bitsat 4 bits, native g128 (new)qmv_rows)qmv_quad_rows)Other details:
project()whenever either of the expert's linears is on the matrix kernel.simd_qmm_bitsalready 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_qmmreads 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):
What the table shows:
qmv_rowsre-reads the weight or loops by row, the matrix kernels win: 2 to 7 times at 4 to 16 rows.qmv_rowsstays 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.qmv_quad_rowsstays 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:
TF_GLM5_MODEL, first 8 layers past 4,096 keys): 2- to 16-row windows equal one-row steps.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.
qmv0.0054 to 0.0075.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.
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:
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:bench_openaisingle 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=Falsekeeps MLX's calls); not measured.Tests, M3 Ultra:
tests/test_glm5*tests/test_simd_qmm*The new
tests/test_glm5_dense_choice.pyhas 41 tests. On the previous head, 39 of them fail.Not run:
lane_qmmfor the 5-, 6- and 8-bit groups of 64.Checklist
"draft": false, a resumed prompt equals a fresh one, and concurrent equals solo. (Window tests and the real-checkpoint window test pass. Served--alone --serialat 1, 2 and 4 streams passes, so drafted equals"draft": falseand concurrent equals solo. Resumed against fresh hasn't been run separately.)