Skip to content

qmm_splitk's 512-threadgroup target routes M<=32 transposed qmm to the non-NAX split-K kernel on M5 (M=33 is 21% faster than M=32) #4198

Closed

Description

@pierre427

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 initial split_k=2, which survives the K-alignment adjustments when K is divisible by 128 — so the split-K path is taken; split_k <= 1 would fall back to qmm (#L1150-L1153).
  • The split-K kernel is plain Metal (#L1178-L1188); the qmm it displaces would hit the NAX gate (#L1039-L1041).
  • At M=33, m_tiles=2 → split_k=1 → NAX qmm_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

Activity

  1. zcbenz commented on Aug 16, 2026

    @zcbenz
    Member

    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.

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