Skip to content

Reject CUB block reduction for short (<128) partial reductions - #10076

Merged
leofang merged 6 commits into
cupy:mainfrom
seberg:cub-reject-small-reduce
Jul 22, 2026
Merged

leofang merged 6 commits into
cupy:mainfrom
seberg:cub-reject-small-reduce

Conversation

@seberg

@seberg seberg commented Jul 6, 2026 •

Copy link
Copy Markdown
Member

Small segments launch one block each with little work and are slower than the generic reduction kernel, so fall back for contiguous_size < 128.

Further forward segment_size to CUB's fixed-size int segmented to fix the same (and more) issue when doing CUB reductions as CUB reductions are otherwise slow for small fixed-size segments (our use-case).

This is a minimal version for gh-10075. Basically, I would like to make sure we have something for CuPy 14.2, but I am not yet convinced that bloating our custom _cub_reduction code is the right way to achieve good performance here (for one, because that code is never quite as fast as it could be).

See gh-10075 for a timing script that works on that branch. I think we are basically leaving a 2x performance on the table for these mid-sized ones, but we also leave perf on the table always.

Small segments launch one block each with little work and are slower
than the generic reduction kernel, so fall back for contiguous_size
< 128.

Further forward segment_size to CUB's fixed-size int segmented to fix
the same (and more) issue when doing CUB reductions as CUB reductions
are otherwise slow for small fixed-size segments (our use-case).
@seberg seberg added cat:performance Performance in terms of speed or memory consumption to-be-backported Pull-requests to be backported to stable branch labels Jul 6, 2026
@seberg seberg added this to the v14.2.0 milestone Jul 6, 2026
@leofang

leofang commented Jul 18, 2026

Copy link
Copy Markdown
Member

@seberg could you show the perf before/after this change? Can the #10075 script be used as is?

@leofang leofang self-assigned this Jul 18, 2026
@seberg

seberg commented Jul 19, 2026

Copy link
Copy Markdown
Member Author

Yeah, the script works, although it's messy/giving nonsense spurious extra info. But it still gives the idea:
reduction_axis_scan

I.e. the green line is what we have now. The violet/ret ones are the CUB paths previously. So you can see the green one follows orange (naive) and then switches over to cub for larger ones.

The blue one is the new sum-cub path that uses the fixed-width now. You can see that it is always very fast (previously it would continue the trend similar to the violet/red ones).

leofang
leofang previously approved these changes Jul 19, 2026

@leofang leofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

LGTM as the minimal fix for 14.2, thanks!

Checked while reviewing: the fixed-size DeviceSegmentedReduce overloads exist at the pinned CCCL with matching int argument types (v14 pins the same CCCL commit, so the backport compiles as-is); the pre-existing x.size <= INT_MAX guard covers the casts; contiguous_size == 0 is rejected before the new check; and the new test shapes keep every contiguous-axes combination >= 128 in both C and F order.

A few short test-side comments below, none blocking.

Comment thread cupy/_core/_cub_reduction.pyx Outdated
# For reductions the CUB block kernel launches one block
# per segment with very little work each, which is slower than the
# generic reduction kernel so use the naive kernel instead.
if contiguous_size < 128:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

nit: worth naming this threshold (module-level constant) — the test files now encode the same 128 in shapes/comments, and it's the first thing that'll get retuned later.

Comment thread cupy/cuda/cub.pyx Resolved
_accelerator.set_routine_accelerators([])

a = cupy.random.random((10, 10))
a = cupy.random.random((10, 128))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nothing tests the new < 128 rejection directly. Cheap companion here: (10, 64) + sum(axis=1) asserting times_called=0.

# CUB block-reduction path instead of the short-axis fallback.
@pytest.mark.parametrize(
"shape", [(10,), (10, 20), (10, 20, 30), (10, 20, 30, 40)]
"shape", [(128,), (128, 128), (128, 2, 128), (128, 2, 2, 128)]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These CUB classes (also in test_ndarray_reduction/test_search) now only exercise device_segmented_reduce with segments >= 128, but the new fixed-size CUB call is motivated by small segments — and it's the default path (CUPY_ACCELERATORS=cub). The device path has no minimum size, so keeping one small shape (e.g. (10, 20, 30)) for backend='device' only would keep small segments asserted.

@leofang

leofang commented Jul 20, 2026

Copy link
Copy Markdown
Member

Sorry @seberg, I guess I should have asked sooner: Does this address #8092 as well (I assume it does)? How's the perf comparison look like before/after this PR, using the script there?

@seberg

seberg commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Sorry a bit obnoxious to have everything on main, but compare the blue line for sum, the difference is massive:
reduction_axis_scan

So yeah, the issue is closed in a sense, we just have more to do to improve things for shorter reductions.

@seberg

seberg commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

/test mini

@seberg

seberg commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Moved the threshold into a module global and exposed to Python + used to tests.

"Reducing the tests" feels a bit annoying, but these tests actually check for CUB use so it makes no sense to test the original ones.
And... I think it should be fine to assume we have unrelated tests that do hit that the naive path is correct (worst case indirect ones).

@seberg

seberg commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Oops, this broke fusion, and moved thing to a slightly nicer place I think. Plus fixed the optimize test (I think).

/test mini

@seberg

seberg commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

/test mini

leofang
leofang previously approved these changes Jul 21, 2026

@leofang leofang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ran the reproducer from #8092 on this branch (68b1220) against its base (9771347) on three GPUs — RTX A6000 (sm_86), RTX 6000 Ada (sm_89), RTX PRO 6000 Blackwell Max-Q (sm_120) — CUDA 13.1, Linux x86_64, float32 sum(axis=1), 25 shapes (2**i, 2**(24-i)), CUB accelerators on/off.

  • The worst-case CUB-vs-generic slowdown is eliminated on all three GPUs: 116x / 69x / 236x → 1.00x. With this PR the CUB path is never slower than the generic kernel on any tested shape.
  • Short-segment rows improve dramatically, e.g. (16777216, 1): 24.7 ms → 0.21 ms (A6000), 12.6 ms → 0.18 ms (Ada), 18.3 ms → 0.058 ms (Blackwell).
  • Long-segment rows are unchanged on A6000/Ada. On Blackwell only, few-large-segment rows (e.g. (8, 2097152)) show a consistent ~3–5% penalty from the fixed-size segmented path — still 3–6x faster than the generic kernel there, so nothing user-visible reintroduced.
Full per-GPU tables (GPU-time means, µs; cub/raw = accelerators on/off; speedup = cub-base/cub-PR)
=== GPU0: NVIDIA RTX A6000  CC: 8.6 ===
shape                cub-base     cub-PR  speedup   raw-base     raw-PR  note
(1, 16777216)          1758.6     1729.1    1.02x    10588.7    10458.5
(2, 8388608)            864.9      850.3    1.02x     5429.8     5427.3
(4, 4194304)            444.2      441.5    1.01x     2739.7     2734.5
(8, 2097152)            233.6      229.8    1.02x     1383.3     1384.4
(16, 1048576)           134.6      134.9    1.00x      706.5      707.4
(32, 524288)            112.3      109.8    1.02x      371.9      367.4
(64, 262144)            111.1      109.2    1.02x      197.2      196.8
(128, 131072)           110.0      108.7    1.01x      123.8      123.6
(256, 65536)            109.5      109.2    1.00x      150.4      150.8
(512, 32768)            111.7      110.9    1.01x      131.9      132.1
(1024, 16384)           110.2      109.6    1.00x      121.5      122.0
(2048, 8192)            110.0      109.4    1.01x      117.0      117.3
(4096, 4096)            110.2      109.1    1.01x      117.6      117.5
(8192, 2048)            110.8      110.9    1.00x      122.8      123.3
(16384, 1024)           111.3      109.5    1.02x      167.7      170.2
(32768, 512)            111.4      110.5    1.01x      286.0      286.4
(65536, 256)            124.1      110.9    1.12x      266.7      267.3
(131072, 128)           220.5      111.9    1.97x      248.4      251.1
(262144, 64)            418.2      112.2    3.73x      234.6      237.3
(524288, 32)            819.1      121.7    6.73x      232.3      232.3
(1048576, 16)          1624.5      117.1   13.87x      214.8      214.7
(2097152, 8)           3246.3      133.4   24.33x      179.9      180.5
(4194304, 4)           6482.1      134.3   48.26x      166.1      166.7
(8388608, 2)          12411.1      162.3   76.45x      167.1      169.0
(16777216, 1)         24683.3      211.6  116.64x      212.2      214.5
worst cub/raw ratio: base 116.34x -> PR 1.00x

=== GPU1: NVIDIA RTX 6000 Ada Generation  CC: 8.9 ===
shape                cub-base     cub-PR  speedup   raw-base     raw-PR  note
(1, 16777216)           849.1      847.0    1.00x     3839.6     3836.5
(2, 8388608)            431.2      429.8    1.00x     2192.0     2187.1
(4, 4194304)            222.9      222.7    1.00x     1138.4     1139.5
(8, 2097152)            118.8      117.9    1.01x      579.0      580.1
(16, 1048576)            67.2       67.1    1.00x      299.0      297.3
(32, 524288)             42.1       41.3    1.02x      157.1      157.7
(64, 262144)             30.0       31.4    0.95x       87.8       88.2
(128, 131072)            27.5       28.2    0.97x       52.7       58.0
(256, 65536)             27.2       25.6    1.06x       37.3       36.6
(512, 32768)             27.0       26.7    1.01x       37.2       37.2
(1024, 16384)            27.7       27.5    1.00x       34.8       34.9
(2048, 8192)             27.1       25.3    1.07x       36.5       35.4
(4096, 4096)             27.8       26.0    1.07x       41.1       40.0
(8192, 2048)             28.6       27.1    1.05x       51.6       51.1
(16384, 1024)            33.5       32.8    1.02x       76.2       76.5
(32768, 512)             40.2       25.6    1.57x      126.6      128.8
(65536, 256)             59.0       27.9    2.11x      117.9      119.9
(131072, 128)            98.5       32.8    3.01x      110.0      110.5
(262144, 64)            178.1       38.9    4.57x      105.2      104.7
(524288, 32)            337.2       55.6    6.06x      104.3      104.6
(1048576, 16)           660.7       25.6   25.77x       96.9       97.3
(2097152, 8)           1303.6       28.6   45.50x       87.7       80.4
(4194304, 4)           2597.0       26.7   97.26x       69.6       69.2
(8388608, 2)           5812.3       73.6   78.94x       94.3       94.4
(16777216, 1)         12646.9      180.8   69.93x      184.3      183.5
worst cub/raw ratio: base 68.62x -> PR 1.00x

=== GPU2: NVIDIA RTX PRO 6000 Blackwell Max-Q Workstation Edition  CC: 12.0 ===
shape                cub-base     cub-PR  speedup   raw-base     raw-PR  note
(1, 16777216)           840.5      861.0    0.98x     4881.7     4880.1
(2, 8388608)            426.8      438.4    0.97x     2638.4     2635.8
(4, 4194304)            222.8      233.8    0.95x     1363.7     1378.6
(8, 2097152)            118.8      138.1    0.86x      718.5      725.5  (re-measured at 50 reps: ~124.4 µs, ~5%)
(16, 1048576)            67.5       70.9    0.95x      367.4      368.2
(32, 524288)             42.7       44.6    0.96x      192.7      193.4
(64, 262144)             31.1       31.9    0.97x      105.9      105.4
(128, 131072)            27.4       28.6    0.96x       62.3       62.2
(256, 65536)             26.3       27.4    0.96x       42.5       41.4
(512, 32768)             27.0       27.5    0.98x       32.4       33.0
(1024, 16384)            27.5       26.6    1.04x       33.2       31.8
(2048, 8192)             27.0       26.9    1.00x       34.3       34.9
(4096, 4096)             27.4       26.3    1.04x       37.9       37.7
(8192, 2048)             32.7       27.3    1.20x       45.5       44.4
(16384, 1024)            40.0       31.1    1.29x       66.0       61.8
(32768, 512)             51.4       26.9    1.92x       99.3       98.2
(65536, 256)             86.7       28.1    3.09x       94.0       92.9
(131072, 128)           148.2       30.5    4.85x       87.3       87.4
(262144, 64)            272.0       35.2    7.72x       83.7       83.9
(524288, 32)            519.8       50.4   10.30x       85.1       87.1
(1048576, 16)          1020.2       26.7   38.21x       80.7       80.6
(2097152, 8)           2025.3       29.3   69.17x       69.0       68.0
(4194304, 4)           4037.2       26.8  150.50x       61.1       60.8
(8388608, 2)           8321.4       34.5  241.08x       60.2       57.2
(16777216, 1)         18318.0       57.8  316.80x       77.5       67.1
worst cub/raw ratio: base 236.27x -> PR 1.00x

Comment thread tests/cupyx_tests/test_optimize.py Resolved
@seberg

seberg commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

/test mini

@leofang
leofang merged commit a6398b8 into cupy:main Jul 22, 2026
9 checks passed
@seberg
seberg deleted the cub-reject-small-reduce branch July 22, 2026 06:43
leofang added a commit that referenced this pull request Jul 23, 2026
Reject CUB block reduction for short (<128) partial reductions
@leofang leofang linked an issue Jul 23, 2026 that may be closed by this pull request
@kmaehashi kmaehashi modified the milestones: v14.2.0, v15.0.0a1 Aug 18, 2026
saladpalad pushed a commit to saladpalad/cupy that referenced this pull request Aug 24, 2026
Reject CUB block reduction for short (<128) partial reductions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cat:performance Performance in terms of speed or memory consumption prio:high to-be-backported Pull-requests to be backported to stable branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Batched cupy.sum on short reduction axes are slow

3 participants