Skip to content

perf(nf12): keep the MLX Metal decode in the dot product through 15 rows - #4128

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

Qubitium merged 2 commits into
mainfrom
perf/mlx-nf12-m5-short-m

Conversation

@Qubitium

Copy link
Copy Markdown
Collaborator

Summary

Follow-up to #4120. That PR kept the decode in the dot product for M=1 and sent 2..16 to the fused MMA tile. On the M5 Ultra the dot product now wins at every width from 1 to 15 rows, and the tile only pays for itself at exactly 16 rows on a wide output, so the dispatch follows the measurement instead of the old split.

Three changes inside the decode-plus-dot kernel:

Change Why
16-lane simd_shuffle_xor butterfly tail the old tail ran 128 serial shared-memory adds on 16 of 128 threads, per token row; the butterfly is 8 shuffles, and the 8 KB threadgroup buffer disappears
((high + exp) << 24) | (low & 0x00FF0000) one shift less per weight than << 8 | low << 16
prefetch the next super-atom the three loads per step overlap the integer ALU instead of serializing behind it

_TILE_MIN_OUT_FEATURES now gates only the 16-row case: rows <= 15 always takes the dot product, rows == 16 takes the tile when out_features >= 65536 and the dot product otherwise.

Measured

M5 Ultra, interleaved A/B against the previous head (4b4c330), graph built outside the timed region:

Shape M=1 M=4 M=8 M=16
2048x2048 1.32x 2.33x 2.04x 1.25x
512x2048 1.25x 1.85x 1.66x 1.42x
8192x2048 1.12x 2.04x 1.78x 0.97x
2048x8192 1.56x 2.45x 1.71x 0.99x
4096x4096 1.28x 2.29x 1.62x 0.93x
128256x2048 1.21x 2.11x 1.80x 1.00x

Ratio is previous head divided by this PR, so values above 1.0 are faster. M=16 is neutral at wide outputs (the tile still runs there) and 1.25-1.42x where the dot product replaced the tile.

Full sweep vs dense BF16 x @ W.T on the same resident input, scripts/benchmark_mlx_nf12_short_m.py:

Shape (N, K) M=1 M=4 M=8 M=16
2048x2048 1.72x 1.90x 2.68x 0.69x
512x2048 1.34x 2.00x 2.09x 0.77x
8192x2048 1.45x 1.75x 2.41x 0.70x
2048x8192 1.68x 2.95x 2.96x 0.39x
4096x4096 1.50x 2.12x 2.59x 0.51x
128256x2048 1.30x 2.48x 3.40x 0.48x

Benchmark harness fix

The old harness built the calls inside the timer, so it measured about 2 us of Python lazy-graph cost per call plus ~5.5 us of amortized mx.eval overhead at 40 iterations. For 2-8 us kernels that is most of the number, and it compressed every ratio toward 1.0. The harness now prebuilds the nodes, times only the eval, batches enough calls to amortize the round-trip, and prints the dispatch floor it measures with a null kernel (1.2 us here). Numbers before/after this change are not directly comparable; the M5 table in docs/nf12-short-m-dispatch.md is regenerated with the new harness.

Honest result

  • M=1..8 beats dense BF16 on every shape in the sweep. The M=1 limit is the decode, not traffic: at 2048x2048 M=1 the kernel reads 6.3 MB in 4.6 us (~1.35 TB/s, the read-only floor for this access pattern) and the ~8.5 integer ops per weight saturate the ALU at 128 lanes/core.
  • A lookup-table decode was implemented and measured, then dropped: the extra gather per weight costs more than the shifts it removes on this GPU (2-24% slower).
  • M=16 still loses (0.39-0.77x). The dot product pays 16 FMAs per weight and becomes ALU-bound, while dense BF16 runs the same MACs on the matrix units and stays memory-bound. The tile is the right shape for that regime but is barrier-bound here, so it only wins at 128256x2048. Closing that needs a tile that stages fewer super-atoms per barrier, not a dispatch change.

Correctness

  • Row-count dispatch pinned: _rows at 1/2/4/8/9/12/16, the tile at 16 with the width gate forced on, decode+matmul at 17; each bit-identical to model(x).
  • test_nf12_mlx_forward_matches_torch now covers 12 rows as well, against the exact codec decode, with and without escaped groups.
  • Dense and escaped groups, FP16/BF16/FP32 activations, split-K prefill and the reduce pass all unchanged.
python -m pytest -q tests/test_mlx_nf12.py tests/test_mlx_macaq.py
35 passed in 1.28s

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)

tests/test_mlx_method_selection.py::...[bitsandbytes] and tests/test_mlx.py::test_mlx_generate fail on this host for pre-existing reasons (bitsandbytes not installed, hub download), unrelated to this change.

The decode-plus-dot kernel now owns 1..15 tokens at every output width and
the fused MMA tile only covers exactly 16 rows on a wide output, where its
matrix units can win back the FMA cost. Three changes inside that kernel:

* the per-column tail folds the 16 partial sums with a 16-lane
  `simd_shuffle_xor` butterfly instead of a shared-memory tree that ran 128
  serial adds on 16 of 128 threads, which also drops the 8 KB threadgroup
  buffer,
* the BF16 word is assembled as `((high + exp) << 24) | (low & 0x00FF0000)`,
  one shift less per weight than the `<< 8 | low << 16` form,
* the next super-atom is prefetched while the current one decodes, so the
  three loads per step overlap the integer ALU.

Measured on an M5 Ultra against the previous head, with the graph built
outside the timed region so Python's lazy-graph cost is not counted:
1.3x at M=1, 2.2x at M=4, 1.8x at M=8; M=16 is neutral at wide outputs and
1.25-1.42x where the dot product replaced the tile.

The benchmark harness is fixed the same way: it prebuilds the calls, batches
enough of them to amortize the ~240 us eval round-trip, and reports the
dispatch floor it measures with a null kernel. The old harness counted about
2 us of Python graph construction and 5.5 us of eval overhead per call, which
is why its small-shape ratios understated NF12.

docs/nf12-short-m-dispatch.md and docs/mlx-runtime.md carry the retuned M5
table and the new dispatch.
@Qubitium
Qubitium merged commit a8c7675 into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/mlx-nf12-m5-short-m branch November 15, 2026 10:59
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