Skip to content

[CUDA] Looped row reductions across blocks - #4916

Merged
nastya236 merged 2 commits into
ml-explore:mainfrom
pierreablin:cuda-row-reduce-accum
Nov 20, 2026
Merged

nastya236 merged 2 commits into
ml-explore:mainfrom
pierreablin:cuda-row-reduce-accum

Conversation

@pierreablin

Copy link
Copy Markdown
Contributor

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:

x = (mx.random.normal((1, 4096, 8, 1024))).astype(mx.bfloat16)
y = mx.sum(x, axis=(0, 1, 3))

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 of mx.sum(x, axis=(0, 1, 3)) mostly solves the issue.

  • ☑️ I understand it is strictly prohibited to use AI to write PR description
  • AI usage disclosure: Claude found the optimization, ran the tests, wrote the kernels. I double checked the output and the timings.

…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.
@pierreablin

Copy link
Copy Markdown
Contributor Author

ping @nastya236 :)

@pierreablin

Copy link
Copy Markdown
Contributor Author

Here are some additional timings to show non-regression and major improvements in some cases (Claude wrote this table). Arrays of float32, times on a H100, timing a compiled x.sum(axis=(0, 2)). Times in µs, median of 51
evals.

shape size splits (introduced in the PR) main (µs) PR (µs) PR / main (ratio)
(32, 2, 4) 1 KiB not split 60.0 59.5 0.99
(64, 2, 4) 2 KiB 4 77.9 49.4 0.63
(32, 2048, 64) 16 MiB not split 59.4 59.1 0.99
(64, 1024, 32) 8 MiB 4 77.5 63.2 0.82
(65536, 2, 4) 2 MiB 2048 12,380 83.0 0.007
(4096, 8, 1024) 128 MiB 256 4,044 118.6 0.029

Splits: the number of blocks that reduce each output.

I timed these twice, the numbers agree within 2%.

@nastya236

nastya236 commented Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Thank you for the fix! We would need to apply the same fix for Metal at some point.

@nastya236
nastya236 self-requested a review November 20, 2026 13:18
@nastya236
nastya236 merged commit bd2d1fb into ml-explore:main Nov 20, 2026
24 checks passed
rcfa pushed a commit to cubiculum-ai/mlx that referenced this pull request Nov 26, 2026
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.

2 participants