Skip to content

perf(nf12): retune the MLX Metal kernels on the RDNA4 short-M shapes - #4120

Merged
Qubitium merged 1 commit into
mainfrom
perf/mlx-nf12-m5-short-m
Nov 15, 2026
Merged

Qubitium merged 1 commit into
mainfrom
perf/mlx-nf12-m5-short-m

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Retunes the NF12 MLX Metal kernels on the same (N, K) shapes and M values as the RDNA4 short-M work (docs/nf12-short-m-dispatch.md), with the emphasis on the low M values that local inference actually runs: M=1, 4, 8 (M=16 kept for parity with the CUDA/ROCm tables).

The old Metal kernels decoded every value from scattered 1-byte loads — four meta bytes at lane*16 + j*4 plus one low byte per value. Each of those is its own memory transaction, so the packed stream was read at about a quarter of what the device can sustain. A super-atom is 1536 contiguous bytes covering 16 rows by 64 K, so both packed paths now stage it with coalesced uint32 loads and decode out of threadgroup memory.

Path Tokens What runs
GEMV 1 16 output columns per threadgroup; one (lane, j) group = 3 shared words -> 8 BF16 values, 8 slots unrolled, escape test hoisted, one reduction at the end
Fused tile 2..16 same staged decode into [k][out] threadgroup memory, then simdgroup MMA; split-K fills the grid when 16-column tiles leave the GPU idle, a reduce pass folds partials + bias
Decode + matmul >16 unchanged: the padded MMA tile would re-read the packed stream per token block

Split-K is capped so the extra pass stays cheaper than the idle it removes, and both packed paths read the checkpoint bytes exactly once per output block.

Measured

M5 Ultra, interleaved A/B against main on the same checkpoint, best of nine trials. Dense BF16 x @ W.T on the same resident input is the reference:

Shape M main this PR dense vs main
2048x2048 1 31.5 us 24.8 us 13.5 1.27x
2048x2048 4 42.3 us 32.2 us 13.2 1.31x
8192x2048 1 43.2 us 35.9 us 19.9 1.20x
8192x2048 4 124.0 us 66.7 us 24.9 1.86x
2048x8192 8 122.3 us 71.1 us 33.9 1.72x
4096x4096 4 121.1 us 66.8 us 24.4 1.81x

scripts/benchmark_mlx_nf12_short_m.py reproduces the full RDNA4 shape set on Metal (--shapes, --rows, --json-out).

Honest result

NF12 still loses to dense BF16 at these shapes on this GPU. The docs record the reason with measurements rather than leaving it as a guess:

  • a 6.3 MB packed read with no decode at all costs 13.7 us (459 GB/s) in this grid shape,
  • an elementwise pass over the same bytes reaches 745 GB/s, and dense BF16 reaches ~1.1 TB/s at large sizes,
  • the tiled layout is built for m16n8k16 MMA, so every value needs a decode step dense BF16 does not have.

At small M the decode costs about as much as the weight traffic NF12 saves (1.5 B vs 2 B per weight is only a 1.33x ceiling). Closing the gap needs the hand-scheduled decode the CUDA/HIP kernels use, not another launch-plan change. The one place NF12 "wins" (128256x2048) is where the dense baseline is itself launch-bound, and the doc says so instead of claiming a decode advantage.

Measurement note: mx.eval costs ~240 us per call on this host regardless of the work it waits for, so per-call synchronization is unusable here. Each trial enqueues a bounded number of calls and evaluates once; both sides of every ratio use the same harness. Absolute values are steady-state throughput, not single-shot latency.

Correctness

  • Unrolled decode validated at 1/2/4/8/16 rows against the exact codec decode in FP64.
  • Fused tile validated at 2/3/4/8/15/16 rows, including escaped groups.
  • Row-count dispatch pinned: model(x) is bit-identical to the GEMV at 1 row, to the fused tile at 2/8/16, and to decode+matmul at 17.
  • FP16/BF16 dtypes and arbitrary leading dimensions preserved; the prefill/reduce split-K path checked at N=48/128.
python -m pytest -q tests/test_mlx_nf12.py tests/test_mlx_macaq.py
33 passed in 1.24s

python -m pytest -q tests/test_mlx_dense_methods.py tests/test_mlx_fp8_packed.py \
  tests/test_mlx_gptq_formats.py tests/test_mlx_qqq.py tests/test_mlx_splitk_dispatch.py \
  tests/test_mlx_awq_group16.py tests/test_mlx_gguf.py
1 failed, 405 passed, 80 skipped   # pre-existing: bitsandbytes not installed on this host

python -m ruff check gptqmodel/nn_modules/qlinear/mlx_nf12.py \
  scripts/benchmark_mlx_nf12_short_m.py tests/test_mlx_nf12.py
All checks passed!

git diff --check
(no output)

Measure the Metal path on the same (N, K) shapes and M values as the RDNA4
table, and retune the kernels against them.

The old Metal kernels decoded every value from scattered 1-byte loads: four
meta bytes at lane*16 + j*4 and one low byte per value. Each of those is a
separate memory transaction, so the packed stream was read at roughly a
quarter of what the device can do. A super-atom is 1536 contiguous bytes
covering 16 rows by 64 K, so both packed paths now stage it with coalesced
uint32 loads and decode out of threadgroup memory.

- M=1 runs a GEMV with 16 output columns per threadgroup. One (lane, j) group
  is three shared words that expand to eight BF16 values, the eight slots are
  unrolled so every shift and mask is a constant, the escape test is hoisted
  out of the value loop, and the two row sums a thread owns are reduced once
  at the end instead of once per super-atom.
- M=2..16 runs the fused tile with the same staged decode, plus a split-K pass
  that fills the grid when the 16-column tile count would leave the GPU idle,
  and a reduce pass that folds the partials and applies the bias. Splitting is
  capped so the extra pass stays cheaper than the idle it removes.
- M>16 keeps the single decode plus matmul.

Measured on an M5 Ultra against the same checkpoint on main, interleaved A/B,
best of nine trials (dense BF16 x @ W.T on the same resident input):

  shape            M    main      this    dense
  2048x2048        1    31.5 us   24.8    13.5
  2048x2048        4    42.3      32.2    13.2
  8192x2048        1    43.2      35.9    19.9
  8192x2048        4   124.0      66.7    24.9
  2048x8192        8   122.3      71.1    33.9
  4096x4096        4   121.1      66.8    24.4

scripts/benchmark_mlx_nf12_short_m.py reproduces the full RDNA4 shape set on
Metal. NF12 still loses to dense BF16 at these shapes on this GPU, and the
docs record why with numbers: a 6.3 MB packed read with no decode at all costs
13.7 us (459 GB/s) in this grid shape, while an elementwise pass over the same
bytes reaches 745 GB/s. The tiled layout is built for m16n8k16 MMA, so every
value needs a decode step dense BF16 does not, and at small M the decode costs
as much as the traffic it saves. Closing that needs a hand-scheduled decode,
not another launch-plan change.

Tests cover the unrolled decode at 1/2/4/8/16 rows, the fused tile at 2/3/4/8/
15/16 rows against the exact codec decode, escaped groups, dtype preservation,
and the row-count dispatch.
@Qubitium
Qubitium force-pushed the perf/mlx-nf12-m5-short-m branch from 77ffd1d to 4b4c330 Compare November 15, 2026 10:18
@Qubitium
Qubitium merged commit 4000d2a into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/mlx-nf12-m5-short-m branch November 15, 2026 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant