Repository navigation
Description
TL;DR
mx.quantized_matmul is 1.5–1.8× slower than mx.matmul (fp16) at M=96 and
M=128 on common transformer FFN-down shapes (N=2048, K=8192) on M5 Max NAX
hardware. At M ≥ 160 it's at parity (~1.0× of fp16) as expected. The cause is
qmm_splitk's aggressive split-K threshold: it forces a split-K reduction
even when the underlying NAX qmm_t_nax would be faster as a single kernel
launch.
Fix candidate: bump the 512 threshold in mlx/backend/metal/quantized.cpp:793
(or remove split-K from the NAX path entirely — NAX qmm is already fast at
small M).
Reproducer
See bench/bench_splitk_boundary.py in this repo. Sweeps M on FFN-down shape,
prints split_k value and timing ratios.
Observed on M5 Max (g17s, NAX), macOS 26.5, MLX 0.31.2:
M= 64 fp16: 0.300 ms qmm: 0.307 ms qmm/fp16: 1.02x (split_k=4)
M= 96 fp16: 0.265 ms qmm: 0.418 ms qmm/fp16: 1.58x (split_k=2) ← regression
M= 128 fp16: 0.272 ms qmm: 0.486 ms qmm/fp16: 1.79x (split_k=2) ← regression
M= 160 fp16: 0.354 ms qmm: 0.354 ms qmm/fp16: 1.00x (split_k=1)
M= 192 fp16: 0.341 ms qmm: 0.349 ms qmm/fp16: 1.02x (split_k=1)
M= 256 fp16: 0.349 ms qmm: 0.381 ms qmm/fp16: 1.09x (split_k=1)
Root cause
In mlx/backend/metal/quantized.cpp:787-805:
void qmm_splitk(...) {
int bm = 32, bn = 32;
int n_tiles = (N + bn - 1) / bn;
int m_tiles = (M + bm - 1) / bm;
int current_tgs = n_tiles * m_tiles;
int split_k = std::max(1, 512 / current_tgs);
...
if (split_k <= 1) {
return qmm(...); // fall back to non-split kernel
}
// ... else allocate intermediate buffer, launch split-K kernel, sum-reduce
}For FFN-down (N=2048):
n_tiles = 64- M=64 →
m_tiles=2,current_tgs=128,split_k=4 - M=96 →
m_tiles=3,current_tgs=192,split_k=2 - M=128 →
m_tiles=4,current_tgs=256,split_k=2 - M=160 →
m_tiles=5,current_tgs=320,split_k=1(integer div: 512/320=1) - M=192+ →
split_k=1
The split-K path allocates an intermediate [split_k, M, N] buffer and then
runs a sum-along-axis-0 reduce after the matmul. On the NAX-capable path,
qmm_t_nax is so fast that this extra allocation + reduce step is more
overhead than the parallelism gain. The split_k = 2 cases at M=96, M=128
clearly demonstrate this: the regular qmm() would run in ~half the time.
Suggested fix
Option 1 (minimal): bump the threshold so split-K only triggers in extreme
small-N cases:
int split_k = std::max(1, 128 / current_tgs); // was 512Option 2 (NAX-aware): bypass split-K entirely on NAX:
if (metal::is_nax_available() && transpose) {
return qmm(x, w, scales, biases, out, true, group_size, bits, M, N, K, d, s, mode);
}Option 3 (full re-tuning): empirically sweep the threshold per (N, K) shape on
representative hardware. The 512 was likely tuned for the pre-NAX kernel.
Impact
This regression hits exactly the prefill lengths most chatbot prompts produce
(roughly 96-160 tokens after system prompt + short user message). Fixing it
recovers ~1.5× prefill throughput at that operating point for free, on any
NAX-capable Apple Silicon (M3/M4/M5 Pro+).
Hardware tested
Apple M5 Max (applegpu_g17s), 128 GB unified memory, macOS 26.5, Xcode 26.5
Metal Toolchain 32023.883.
Reference
This was found while building a custom NAX-accelerated fused dequant+matmul
kernel for testing. The custom kernel doesn't use split-K and lands at
1.39× faster than mx.quantized_matmul at M=128 on the same shape,
specifically because it avoids this dispatch overhead. See
bench/bench_splitk_boundary.py and bench/bench_smollm2_real.py in this
repo for full reproducers.
Activity
ima-helikoptaaa commentedon May 28, 2026
@zcbenz @marian-gg
Tried to reproduce the issue on the following hardware
Hardware/software:
No regression found,
Option 2 (NAX-aware): bypass split-K entirely on NAX, risks regressing on M3/M4. Will be safer to do sweep on each category of machine as M3, M4 and M5 all have different implementations of NAXBenchmarking script
pierre427 commentedon Jul 7, 2026
I took a pass at the "similar heuristics" angle suggested from #3791:
#3791 (comment)
Short version: on current source builds, I do not reproduce the original
M=96/M=128 regression on M2, M3 Pro, or M5 Max for the #3584 FFN-down shape.
The existing
512 / current_tgsheuristic looks safe-to-helpful across thesemachines. I would not bypass split-K for NAX or lower the target based on
these runs.
Method
I used the same shape as the issue:
N=2048,K=8192group_size=64,bits=4M in {64, 96, 128, 160, 192, 256}x @ w_fp16.Twithmx.quantized_matmul(..., transpose=True)To test the heuristic directly, I added a local bench-only override in
qmm_splitk:So:
5120: force non-splitqmmThe harness refuses to run unless the rebuilt MLX binary contains the override
symbol, to avoid silently measuring natural dispatch. Timing was:
512, 384, 256, 128, 0(0shown asno-split)Important interpretation note: when all rows have
split_k=1, differencesbetween "targets" are just repeated measurements of the same non-split path and
should be treated as noise/tie-breaks, not heuristic wins.
Results
Apple M2 (
applegpu_g14g)MLX:
0.32.0.dev20260707+b5404c9Takeaway: split-K only meaningfully engages at M=64/96, and the win is tiny
(-2 to -2.4%). At M>=128 the useful interpretation is essentially tie/noise.
Apple M3 Pro (
applegpu_g15s)MLX:
0.32.0.dev20260707+1ac2fbacTakeaway: target 512 gives a real win at M=64 and a small win at M=128.
The M=96 "best" row is split_k=1 and within noise. M>=160 is split_k=1
tie/noise.
Apple M5 Max (
applegpu_g17s)MLX:
0.32.0.dev20260706+1ac2fbacTakeaway: target 512 is clearly helpful at M=64/96/128 on this M5 Max run.
Once
split_knaturally drops to 1, everything ties.Overall conclusion
For this FFN-down shape on current source builds:
512 / current_tgsheuristic is harmless on M2 and beneficial onM3/M5 where it creates actual split-K parallelism.
and M5.
So my read is: if this was a real regression in 0.31.2, it may already have
changed on current source, or it may depend on a narrower build/toolchain state.
Before changing the heuristic, I would first try to reproduce the original
numbers on exactly 0.31.2 versus current main on the same M5 system.
Happy to share the small harness/patch if useful; it is just a local
MLX_QMM_SPLITK_TARGET_TGSoverride plus the sweep above.zcbenz commentedon Jul 7, 2026
Thank you for measuring the performance, this is very helpful and saves a lot of efforts for us 🙏 ! I'm closing this issue since it is very likely no longer relevant.