Skip to content

Run small reductions as a single task - #11957

Merged
copybara-service[bot] merged 1 commit into
google:masterfrom
JakeStevens:reduce-small-inline
Nov 18, 2026
Merged

copybara-service[bot] merged 1 commit into
google:masterfrom
JakeStevens:reduce-small-inline

Conversation

@JakeStevens

@JakeStevens JakeStevens commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

reduce-nd used one task per output row, so small static reductions such as an RMSNorm mean over [1,16,128] woke the thread pool to run 16 tasks of 128 elements. This tiles the innermost output dimension so each task reduces at least 4096 input elements. Small reductions then don't wake the pool (0.8-14.5 us -> ~0.1 us on M4 Max and Pixel 10), and large ones keep their parallelism. Before/after tables for contiguous and discontiguous reductions are in the commit message.

Tests: reduce-nd-test, with a new multithreaded multi-task test, passes on M4 Max and Pixel 10. static-reduce-test passes on M4 Max. static-reduce is added to the CMake subgraph benchmarks; it was already in Bazel.

Authored with Claude Code.

@fbarchard

Copy link
Copy Markdown
Collaborator

Thanks for the PR. Overall looks okay, but I'll run some checks

@fbarchard fbarchard left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor/optional: Middle-axis 3D reductions ([B, K, C] reducing axis 1) still wake the pool when B > 1:
Because 3d_tile_1d_dynamic only tiles range[2], a shape like [8, 16, 4] (512 elements total) reducing axis 1
normalizes to [1, 1, 1, 8, 16, 4] with range = [1, 8, 4]. Since range[1] = 8 > 1, pthreadpool still wakes the pool
to run 8 tasks of 64 elements each. So the comment/commit claim ("every task reduces at least 4096 input elements,
so small reductions don't wake the thread pool") strictly holds when range[0] == 1 && range[1] == 1 (innermost
contiguous reductions like [..., M, K] and outer-axis reductions like [K, C]), not when outer non-reduced axes
remain unmerged. This doesn't block the PR—fixing [B, K, C] would require 2D tiling (3d_tile_2d_dynamic)—but the
comment could note it applies along the innermost output dimension.

reduce-nd used one task per output row (contiguous) or per rd_width
columns (discontiguous), so a static mean over [1,16,128] woke the thread
pool to run 16 tasks of 128 elements each. Tile the innermost output
dimension so each task reduces at least 4096 input elements. Small
reductions then don't wake the thread pool, and large ones keep their
parallelism.

subgraph-static-reduce-bench, median of 10, us, before -> after. M4 Max:
Apple clang 17.0.0. Pixel 10: NDK r29 clang 21.0.0. No thread pinning.

| bench | M4 Max t4 | M4 Max t16 | Pixel 10 t4 | Pixel 10 t6 |
|---|---:|---:|---:|---:|
| Mean 1x8x128 (last axis) | 0.795 -> 0.071 | 12.1 -> 0.073 | 0.960 -> 0.084 | 1.41 -> 0.079 |
| Mean 1x16x128 (last axis) | 1.48 -> 0.100 | 14.5 -> 0.103 | 1.03 -> 0.123 | 1.57 -> 0.127 |
| Mean 1x2048x128 (last axis) | 9.96 -> 9.10 | 29.3 -> 28.5 | 8.48 -> 8.07 | 7.50 -> 7.39 |
| Mean 1x128x1024 (last axis) | 7.14 -> 7.23 | 26.6 -> 25.8 | 4.29 -> 4.14 | 4.36 -> 3.54 |
| Sum 64x1x256 (first axis) | 3.11 -> 2.89 | 20.5 -> 19.0 | 1.38 -> 1.23 | 1.75 -> 1.49 |
| Sum 64x1024x1 (first axis) | 7.25 -> 6.94 | 26.2 -> 25.0 | 4.06 -> 4.38 | 4.64 -> 4.29 |
| Max 64x1x256 (first axis) | 2.61 -> 2.54 | 18.9 -> 18.3 | 1.20 -> 1.12 | 1.54 -> 1.26 |
| Max 64x1024x1 (first axis) | 7.31 -> 7.03 | 25.8 -> 25.9 | 3.75 -> 3.89 | 3.68 -> 3.34 |

Single-threaded times are within 5% (noise). Multithreaded Pixel 10 runs
have a 3-10% coefficient of variation.
@JakeStevens

Copy link
Copy Markdown
Contributor Author

@fbarchard updated the comment to clarify about innermost output dimension, to keep this limited to what i've verified and benchmarked

@copybara-service
copybara-service Bot merged commit 9cbfeac into google:master Nov 18, 2026
8 checks passed
@JakeStevens
JakeStevens deleted the reduce-small-inline branch November 18, 2026 20:46
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.

3 participants