Repository navigation
perf(nf12): keep the MLX Metal decode in the dot product through 15 rows - #4128
Merged
Merged
Conversation
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.
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
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:
simd_shuffle_xorbutterfly tail((high + exp) << 24) | (low & 0x00FF0000)<< 8 | low << 16_TILE_MIN_OUT_FEATURESnow gates only the 16-row case:rows <= 15always takes the dot product,rows == 16takes the tile whenout_features >= 65536and the dot product otherwise.Measured
M5 Ultra, interleaved A/B against the previous head (
4b4c330), graph built outside the timed region:2048x2048512x20488192x20482048x81924096x4096128256x2048Ratio 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.Ton the same resident input,scripts/benchmark_mlx_nf12_short_m.py:(N, K)2048x2048512x20488192x20482048x81924096x4096128256x2048Benchmark 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.evaloverhead 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 indocs/nf12-short-m-dispatch.mdis regenerated with the new harness.Honest result
2048x2048M=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.128256x2048. Closing that needs a tile that stages fewer super-atoms per barrier, not a dispatch change.Correctness
_rowsat 1/2/4/8/9/12/16, the tile at 16 with the width gate forced on, decode+matmul at 17; each bit-identical tomodel(x).test_nf12_mlx_forward_matches_torchnow covers 12 rows as well, against the exact codec decode, with and without escaped groups.tests/test_mlx_method_selection.py::...[bitsandbytes]andtests/test_mlx.py::test_mlx_generatefail on this host for pre-existing reasons (bitsandbytes not installed, hub download), unrelated to this change.