Skip to content

perf(nf12): share activation loads across column blocks in the Metal dot product - #4133

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

Follow-up to #4128. The decode-plus-dot kernel loaded the activation values once per 16-column block, but x does not depend on the output column — every block needs the same values. At eight rows those per-step activation loads outnumbered the packed weight loads, which is why M=8 cost twice M=1 for identical weight traffic.

A threadgroup now owns several adjacent 16-column blocks and decodes all of them from one set of activation loads:

Tokens Blocks per threadgroup Why
1..4 4 8 accumulator pairs per thread; 64 columns per threadgroup
5..12 2 32 accumulator pairs; four blocks would spill
13..16 1 32+ pairs, and the dot product only runs at narrow outputs

The block count also shrinks with the output width (one block below N=512) so the threadgroup grid still covers the GPU.

Measured

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

Shape M=1 M=4 M=8 M=16
2048x2048 1.51x 1.35x 1.16x 1.00x
512x2048 0.99x 1.02x 0.96x 0.96x
8192x2048 1.51x 1.20x 1.08x 1.01x
2048x8192 1.29x 1.31x 1.34x 1.01x
4096x4096 1.70x 1.49x 1.32x 1.01x
128256x2048 1.04x 1.21x 1.23x 1.00x

Ratio is previous head divided by this PR, so values above 1.0 are faster. 512x2048 is flat because its output is too short for more than one block; M=16 is unchanged (one block, or 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 2.56x 2.61x 3.14x 0.70x
512x2048 1.29x 2.01x 1.99x 0.90x
8192x2048 2.19x 2.08x 2.62x 0.70x
2048x8192 2.08x 3.76x 4.09x 0.40x
4096x4096 2.53x 3.00x 3.38x 0.51x
128256x2048 1.38x 2.99x 4.29x 0.48x

How it was found

Two synthetic probes narrowed it down before any code was committed:

  • removing the decode entirely (constant weight, FMAs kept) changed the time by 0-8%, so the bit-twiddling is not the limit;
  • making every token row read x[0] instead of its own row gave +68% at M=8 on 4096x4096 and +38% on 128256x2048, so the activation loads are.

Accumulator pressure was ruled out separately (folding all rows into two accumulators changed nothing).

Honest result

  • M=1..8 now beats dense BF16 on every shape in the sweep: 1.29-2.56x at M=1, 2.01-3.76x at M=4, 1.99-4.29x at M=8.
  • The narrow 512x2048 shape gains nothing: its output is 32 column blocks, and two blocks per threadgroup would leave 16 threadgroups, which is below one wave. The block rule keeps it at one.
  • M=16 still loses (0.40-0.90x). The dot product pays 16 FMAs per weight and is ALU-bound there, while dense BF16 runs the same MACs on the matrix units and stays memory-bound. The fused tile is the right shape for that regime but is barrier-bound on this GPU, so it only wins at 128256x2048. Closing that needs a tile that stages fewer super-atoms per barrier.

Correctness

  • New test pins the block table (_row_blocks) and checks a 1024-wide layer against the exact codec decode at 1/4/8/12 rows, which exercises blocks=4 and blocks=2 end to end.
  • Verified separately that blocks 1/2/4 all produce bit-identical output to each other across widths 48..4096 and 1..16 rows, including escaped groups.
  • The existing dispatch, tile, split-K and dtype tests are unchanged and still pass.
python -m pytest -q tests/test_mlx_nf12.py tests/test_mlx_macaq.py
36 passed in 1.28s

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

git diff --check
(no output)

…dot product

The decode-plus-dot kernel was loading the activation values once per
16-column block, but `x` does not depend on the output column. At eight
rows those per-step activation loads outnumbered the packed loads, which is
why M=8 sat at twice the M=1 time for the same weight traffic.

A threadgroup now owns several adjacent 16-column blocks and decodes all of
them from one set of activation loads:

* four blocks for 1..4 tokens, two for 5..12, one for 13..16 (each block
  costs `rows` accumulator pairs per thread, and 64 pairs spills),
* the block count also shrinks with the output width (one block below
  N=512) so the threadgroup grid still covers the GPU.

Measured on an M5 Ultra against the previous head, interleaved, graph built
outside the timed region: 1.04-1.70x at M=1, 1.02-1.49x at M=4, 0.96-1.34x
at M=8. The narrow 512x2048 shape is flat because its output is too short
for more than one block. M=16 is unchanged (it takes one block or the tile).

Versus dense BF16 the sweep is now 1.29-2.56x at M=1, 2.01-3.76x at M=4 and
1.99-4.29x at M=8.

Removing the decode entirely, and swapping the x loads for a single row,
were both measured first: the decode is not the limit (0-8%), the activation
loads are (up to +68% at M=8).
@Qubitium
Qubitium merged commit 6f9fc71 into main Nov 15, 2026
6 checks passed
@Qubitium
Qubitium deleted the perf/mlx-nf12-m5-short-m branch November 15, 2026 11:18
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