Repository navigation
perf(nf12): share activation loads across column blocks in the Metal dot product - #4133
Merged
Merged
Conversation
…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).
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 #4128. The decode-plus-dot kernel loaded the activation values once per 16-column block, but
xdoes 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:
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:2048x2048512x20488192x20482048x81924096x4096128256x2048Ratio is previous head divided by this PR, so values above 1.0 are faster.
512x2048is 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.Ton the same resident input,scripts/benchmark_mlx_nf12_short_m.py:(N, K)2048x2048512x20488192x20482048x81924096x4096128256x2048How it was found
Two synthetic probes narrowed it down before any code was committed:
x[0]instead of its own row gave +68% at M=8 on4096x4096and +38% on128256x2048, so the activation loads are.Accumulator pressure was ruled out separately (folding all rows into two accumulators changed nothing).
Honest result
512x2048shape 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.128256x2048. Closing that needs a tile that stages fewer super-atoms per barrier.Correctness
_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.