Repository navigation
[CUDA] Looped row reductions across blocks - #4916
Merged
Merged
Conversation
…m when outputs are few A row reduction keeping a middle axis, e.g. a (B, T, C, D) array summed over (0, 1, 3) as the gradient of a per-channel scale is, ran one block per output and accumulated in the output type: with C = 8 outputs of 4M elements each, a bfloat16 sum was 98% off (the running sum stalls in half precision) and 30x slower than it needs to be on an H100. Row reductions now accumulate half precision sums and products in float32, as the CPU backend does since ml-explore#4387, and the looped one splits its rows over several blocks when it has few outputs, combining their float32 partial results in a second kernel.
Contributor
Author
|
ping @nastya236 :) |
Contributor
Author
|
Here are some additional timings to show non-regression and major improvements in some cases (Claude wrote this table). Arrays of
Splits: the number of blocks that reduce each output. I timed these twice, the numbers agree within 2%. |
Collaborator
|
Thank you for the fix! We would need to apply the same fix for Metal at some point. |
nastya236
self-requested a review
November 20, 2026 13:18
nastya236
approved these changes
Nov 20, 2026
1 task
rcfa
pushed a commit
to cubiculum-ai/mlx
that referenced
this pull request
Nov 26, 2026
(cherry picked from commit bd2d1fb)
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.
Take a reduction with a middle axis, for instance a
(B, T, C, D)array summed over(0, 1, 3). For instance, the gradient of a per-channel scale needs this.For instance:
On a h100, my timings give:
main: 5.49 ms
this pr: 0.115 ms
In float32, timings give:
main: 4.00 ms
this pr: 0.122 ms
Right now, main runs one block per output channel C, so there are 8 blocks. On the previous example, this PR splits each output's 4096 rows into 256 chunks of 16, leading to far more blocks (2048) and hence faster compute.
Let me know if I should add more numbers to the benchmarks !
Note: on main, doing
x.sum(axis=(0, 1)).sum(-1)instead ofmx.sum(x, axis=(0, 1, 3))mostly solves the issue.