Repository navigation
Description
Summary
On M5, transposed 4-bit quantized_matmul at N=6656 (gs64, B=1) runs ~21% slower for M=24..32 than for M=33..40. The M<=32 side dispatches qmm_t_splitk, which has no NAX variant; M>=33 dispatches the NAX qmm_t. The routing comes from the split-K heuristic's 512-threadgroup target:
- Initial
split_k = max(1, 512 / (ceil(N/32) * ceil(M/32)))(quantized.cpp#L1132-L1137). For N=6656 (208 column tiles), any M<=32 gives an initialsplit_k=2, which survives the K-alignment adjustments when K is divisible by 128 — so the split-K path is taken;split_k <= 1would fall back toqmm(#L1150-L1153). - The split-K kernel is plain Metal (#L1178-L1188); the
qmmit displaces would hit the NAX gate (#L1039-L1041). - At M=33,
m_tiles=2→split_k=1→ NAXqmm_t.
quantized.cpp is byte-identical between v0.32.0 and current main (36fde27); measurements below are from both.
Single-op measurement
M5 Max (128 GB), macOS 26.5.2. Chained 64 dependent ops per timed step, bf16 activations, 4-bit gs64, transpose=True, idle GPU, warmup + 6 reps. Band A is the affected shape; band B pads N past 256 column tiles so the initial split_k is 1 at every M (never split-K).
| µs/op | M=24 | 28 | 31 | 32 | 33 | 34 | 36 | 40 | M=33/M=32 |
|---|---|---|---|---|---|---|---|---|---|
| A: N=6656, main | 258 | 253 | 256 | 256 | 203 | 200 | 203 | 205 | 0.79 |
| A: N=6656, v0.32.0 | 252 | 252 | 252 | 247 | 194 | 194 | 194 | 196 | 0.79 |
| B: N=8224, main | 212 | 211 | 212 | 201 | 201 | 202 | 202 | 203 | 1.00 |
| B: N=8224, v0.32.0 | 212 | 211 | 213 | 200 | 201 | 202 | 201 | 202 | 1.00 |
Two things stand out: the 21% step at the m_tiles boundary exists only in band A, and at M<=32 the N=8224 op is absolutely faster (≈212 µs) than the N=6656 op (≈252-258 µs) despite computing 24% more output columns — the smaller matmul loses because it is routed to split-K.
Where this lands in model shapes
A 52-distinct-layer 30B-decoder-shaped stack (hidden 6656, intermediate 19968, vocab 202048, ~13.7 GB of 4-bit weights per step) on current main: step cost is 1.0-1.2x the M=1 cost through M=4, ~3.9-4.35x across M=16..32, and drops to 3.35x at M=33. On main the qmv/qmv_wide path covers M<=12 for these shapes (vector limit 13), so the split-K window is M=13..32 — where speculative-decoding verification blocks (M = draft+1) and small-batch decode operate.
Repro script (single-op probe)
import time
import mlx.core as mx
K, CHAIN, REPS = 6656, 64, 6
def qw(n, k):
w = mx.random.normal((n, k)).astype(mx.bfloat16)
q = mx.quantize(w, group_size=64, bits=4)
mx.eval(*q)
return q
for N in (6656, 8224):
wq = qw(N, K)
for M in (24, 28, 30, 31, 32, 33, 34, 36, 40):
x0 = mx.random.normal((M, K)).astype(mx.bfloat16)
mx.eval(x0)
def step(x):
for _ in range(CHAIN):
x = mx.quantized_matmul(
x, *wq, transpose=True, group_size=64, bits=4
)[:, :K]
return x
mx.eval(step(x0)); mx.synchronize()
t0 = time.perf_counter()
for _ in range(REPS):
mx.eval(step(x0))
mx.synchronize()
us = (time.perf_counter() - t0) / REPS / CHAIN * 1e6
print(f"N={N} M={M:2d} {us:7.1f} us/op")Proposed fix (happy to PR)
Bypass split-K when the fallback qmm would take the NAX path and m_tiles == 1, for the demonstrated band first (N around 6656..8192, K divisible by 128), with an N/K sweep on M5 hardware before any broader threshold change — at much smaller N the heuristic selects far more K partitions and may well be winning there. Can validate candidates on M5 Max, as with #3791.
Prior art
- [Performance]
qmvkernel: non-linear cost step at M=3 for large MLP shapes #3553 / qmv_wide: residual small-m cost on larger shapes — m=4–8 still 1.5–2.7x of m=1 on M4 Pro #3839 (closed) and 2-bit quantized_matmul loses its size advantage over 4-bit at M >= 3 #3852 (open) cover the qmv/qmv_wide small-M regime; on main the vector limit for these shapes is 13 (Raise qmv batch limit for large matrices on M5-class GPUs #3791), which keeps M<=12 out of the window reported here. - Use a 32-row block in qmm_t_nax when one block covers all of M #4171 changes NAX
qmm_ttiling for N>8192 — the band-B side. This report concerns the N<=8192 split-K routing. - qmm_splitk dispatch causes 1.5-1.8x regression vs fp16 at M=96, M=128 on NAX hardware #3584 (split-K on NAX): its no-regression sweep began at M=64 / N=2048 and did not cover the
m_tiles==1region wheresplit_k=2engages against a NAX-capableqmm.
Activity
zcbenz commentedon Aug 16, 2026
While having benchmarks like this is valuable we are unable to optimize for all possible cases, currently we are only focusing on real cases happened during inference and I'm closing this issue.