Repository navigation
perf(nf12): retune the MLX Metal kernels on the RDNA4 short-M shapes - #4120
Merged
Merged
Conversation
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
force-pushed
the
perf/mlx-nf12-m5-short-m
branch
from
November 15, 2026 10:18
77ffd1d to
4b4c330
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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*4plus 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 coalesceduint32loads and decode out of threadgroup memory.(lane, j)group = 3 shared words -> 8 BF16 values, 8 slots unrolled, escape test hoisted, one reduction at the end[k][out]threadgroup memory, thensimdgroupMMA; split-K fills the grid when 16-column tiles leave the GPU idle, a reduce pass folds partials + biasSplit-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
mainon the same checkpoint, best of nine trials. Dense BF16x @ W.Ton the same resident input is the reference:scripts/benchmark_mlx_nf12_short_m.pyreproduces 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:
m16n8k16MMA, 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.evalcosts ~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
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.N=48/128.