Skip to content

qmm_splitk dispatch causes 1.5-1.8x regression vs fp16 at M=96, M=128 on NAX hardware #3584

Closed

Description

@marian-gg

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 512

Option 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

  1. ima-helikoptaaa commented on May 28, 2026

    @ima-helikoptaaa

    @zcbenz @marian-gg
    Tried to reproduce the issue on the following hardware

    Hardware/software:

    • Apple M3 Max (Mac15,11), 36 GB
    • macOS 26.2, Xcode 26.2, Metal Toolchain 32023.864
    • MLX built from main @ 2165dc0

    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 NAX

    shape: N=2048, K=8192, group_size=64, bits=4
        M   fp16(ms)    qmm(ms)   qmm/fp16   split_k
       64      0.501      0.479      0.96x         4
       96      0.633      0.600      0.95x         2
      128      0.813      0.769      0.94x         2
      160      0.914      0.920      1.01x         1
      192      1.072      1.036      0.97x         1
      256      1.318      1.309      0.99x         1
    

    Benchmarking script

    import time
    import mlx.core as mx
    
    N, K = 2048, 8192
    GROUP_SIZE, BITS = 64, 4
    
    WARMUP_ITERS = 20
    TIMED_ITERS = 100
    REPEATS = 5
    
    MS = [64, 96, 128, 160, 192, 256]
    
    
    def compute_split_k(M, N, K, group_size):
        bm, bn = 32, 32
        n_tiles = (N + bn - 1) // bn
        m_tiles = (M + bm - 1) // bm
        current_tgs = n_tiles * m_tiles
        split_k = max(1, 512 // current_tgs)
        split_k = min(split_k, K // group_size)
        while split_k > 1 and (K % (split_k * group_size) != 0):
            split_k -= 1
        return split_k
    
    
    def time_min(fn):
        for _ in range(WARMUP_ITERS):
            mx.eval(fn())
        mx.synchronize()
    
        best = float("inf")
        for _ in range(REPEATS):
            mx.synchronize()
            t0 = time.perf_counter()
            for _ in range(TIMED_ITERS):
                mx.eval(fn())
            mx.synchronize()
            elapsed = (time.perf_counter() - t0) * 1000 / TIMED_ITERS
            best = min(best, elapsed)
        return best
    
    
    def main():
        w_fp16 = mx.random.normal((N, K)).astype(mx.float16)
        w_q, scales, biases = mx.quantize(w_fp16, group_size=GROUP_SIZE, bits=BITS)
        mx.eval(w_fp16, w_q, scales, biases)
    
        print("global warmup...")
        for M in MS:
            x = mx.random.normal((M, K)).astype(mx.float16)
            for _ in range(5):
                mx.eval(x @ w_fp16.T)
                mx.eval(
                    mx.quantized_matmul(
                        x, w_q, scales, biases,
                        transpose=True, group_size=GROUP_SIZE, bits=BITS,
                    )
                )
        mx.synchronize()
    
        print(f"\nshape: N={N}, K={K}, group_size={GROUP_SIZE}, bits={BITS}")
        print(f"{'M':>5} {'fp16(ms)':>10} {'qmm(ms)':>10} {'qmm/fp16':>10}  {'split_k':>8}")
        for M in MS:
            x = mx.random.normal((M, K)).astype(mx.float16)
            mx.eval(x)
    
            t_fp16 = time_min(lambda: x @ w_fp16.T)
            t_qmm = time_min(
                lambda: mx.quantized_matmul(
                    x, w_q, scales, biases,
                    transpose=True, group_size=GROUP_SIZE, bits=BITS,
                )
            )
            ratio = t_qmm / t_fp16
            sk = compute_split_k(M, N, K, GROUP_SIZE)
            marker = "  <-- regression" if ratio > 1.2 else ""
            print(f"{M:>5} {t_fp16:>10.3f} {t_qmm:>10.3f} {ratio:>9.2f}x  {sk:>8}{marker}")
    
    
    if __name__ == "__main__":
        main()
  2. pierre427 commented on Jul 7, 2026

    @pierre427
    Contributor

    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_tgs heuristic looks safe-to-helpful across these
    machines. 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=8192
    • group_size=64, bits=4
    • M in {64, 96, 128, 160, 192, 256}
    • compare fp16 x @ w_fp16.T with mx.quantized_matmul(..., transpose=True)

    To test the heuristic directly, I added a local bench-only override in
    qmm_splitk:

    int target_tgs = env::get_var("MLX_QMM_SPLITK_TARGET_TGS", 512);
    if (target_tgs <= 0) {
      return qmm(...);
    }
    int split_k = std::max(1, target_tgs / current_tgs);

    So:

    • unset/default: current behavior, target 512
    • positive value: retarget split-K to that threadgroup count
    • 0: force non-split qmm

    The harness refuses to run unless the rebuilt MLX binary contains the override
    symbol, to avoid silently measuring natural dispatch. Timing was:

    • warmup: 10
    • timed iterations: 200
    • repeats: 5
    • report: best-of
    • targets swept: 512, 384, 256, 128, 0 (0 shown as no-split)

    Important interpretation note: when all rows have split_k=1, differences
    between "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+b5404c9

    M best target split_k qmm ms qmm/fp16 vs no-split
    64 512 4 1.079 1.03x -2.4%
    96 512 2 1.485 1.04x -2.0%
    128 384 1 1.886 1.05x -0.3%
    160 512/384 tie 1 2.242 0.88x noise
    192 no-split 1 2.667 1.12x tie
    256 384 1 3.451 1.14x noise

    Takeaway: 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+1ac2fbac

    M best target split_k qmm ms qmm/fp16 vs no-split
    64 512 4 0.635 0.92x -7.8%
    96 128 1 0.843 0.92x -1.1%
    128 512 2 1.054 0.94x -2.0%
    160 no-split 1 1.243 0.92x tie/noise
    192 128 1 1.458 0.96x -0.2%
    256 no-split 1 1.848 0.96x tie

    Takeaway: 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+1ac2fbac

    M best target split_k qmm ms qmm/fp16 vs no-split
    64 512 4 0.319 0.80x -16.1%
    96 512 2 0.396 0.97x -5.7%
    128 512 2 0.468 0.98x -7.9%
    160 512 1 0.533 0.99x tie
    192 no-split 1 0.624 1.00x tie
    256 384 1 0.770 1.00x tie

    Takeaway: target 512 is clearly helpful at M=64/96/128 on this M5 Max run.
    Once split_k naturally drops to 1, everything ties.

    Overall conclusion

    For this FFN-down shape on current source builds:

    • I do not see the M=96/M=128 regression from the original MLX 0.31.2 report.
    • The current 512 / current_tgs heuristic is harmless on M2 and beneficial on
      M3/M5 where it creates actual split-K parallelism.
    • Lowering the target to 128/256 would remove useful split-K parallelism on M3
      and M5.
    • A blanket NAX bypass would also remove the useful M=64/96/128 wins seen here.

    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_TGS override plus the sweep above.

  3. zcbenz commented on Jul 7, 2026

    @zcbenz
    Member

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions