Skip to content

[CUB] Use one item per thread when cub::DeviceTransform fills values over 512 bytes - #11919

Open
SamMausberg wants to merge 1 commit into
NVIDIA:mainfrom
SamMausberg:fix/transform-fill-large-value
Open

SamMausberg wants to merge 1 commit into
NVIDIA:mainfrom
SamMausberg:fix/transform-fill-large-value

Conversation

@SamMausberg

Copy link
Copy Markdown
Contributor

Description

closes #11758

As suggested in the issue, fills of values larger than 512 bytes now use one item per thread (TransformVectorizedPolicy{128, 1, 1}) on all architectures. With 16 items per thread, 1 KiB values already needed 17392 bytes of stack per thread on sm_90 and sm_120, which the driver reserves for every thread the device can hold. Up to 512 bytes, the existing tuning needs at most 120 bytes of stack and stays as it is, so all types in the fill benchmark keep their policy. Comparing the policy selector's output before and after for 17 compute capabilities, 0 to 2 inputs and value sizes from 1 byte to 96 KiB shows no other change.

For struct { unsigned char b[N]; }, main → this PR. Stack from cuobjdump --dump-resource-usage; memory (the repro from the issue) and fill throughput measured on an RTX 5070 Ti with CUDA 13.2:

N stack, sm_90 stack, sm_120 device memory fill throughput
1 KiB 17392 → 0 B 17392 → 0 B 1682 → 2 MiB 104 → 206 GB/s
4 KiB 69616 → 7000 B 69616 → 3816 B 7040 → 290 MiB 40 → 153 GB/s

For larger values, one item per thread still needs stack that grows with the value size, 3.7 to 12.8 KiB per thread for 4 KiB values. The prefetch kernel needs no stack for 4 KiB values. It writes 1-byte aligned types byte by byte, though, at 25 GB/s for 1 KiB values. Transform with large inputs stages them the same way and could get the same limit in a follow-up.

The new test DeviceTransform::Fill large power-of-two type checks the policy for sm_75 to sm_120, which fails on main, and fills 2 KiB values, below the 4096-byte kernel parameter limit of CTK 12.0. All cub.test.device.transform* tests pass on the RTX 5070 Ti with gcc 13.3, and the test file builds without warnings with clang 18, gcc 12 and clang 21 as host compilers. Not tested: MSVC, NVHPC, CTK 12.x.

Checklist

  • New or existing tests cover these changes.
  • The documentation is up to date with these changes.

…over 512 bytes

The fill tuning of the vectorized transform kernel stages up to 16
values per thread. Above 512 bytes, these can spill to local memory
(17 KiB per thread for 1 KiB values on sm_90+), which the driver
reserves for every thread the device can hold, e.g. about 7 GiB for a
fill of 4 KiB values on an RTX 5070 Ti. Such values now get one item
per thread.

Fixes NVIDIA#11758
@copy-pr-bot

copy-pr-bot Bot commented Oct 7, 2026

Copy link
Copy Markdown
Contributor

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@coderabbitai

coderabbitai Bot commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

📝 Summary

Summary by CodeRabbit

  • Performance
    • Improved fill operations for outputs with elements larger than 512 bytes by adjusting how work is distributed across threads. This tuning applies to large-element fills while leaving other fill configurations and the default policy unchanged.
  • Testing
    • Added coverage verifying fill results and the selected policy across several supported compute capability levels.

Walkthrough

For fill operations with output elements larger than 512 bytes, the vectorized policy now uses one item per thread. A new test checks policy selection across five compute capabilities and verifies filled values for two output sizes.

Changes

Large-value fill tuning

Layer / File(s) Summary
Large-value fill policy and validation
cub/cub/device/dispatch/tuning/tuning_transform.cuh, cub/test/catch2_test_device_transform.cu
For output elements larger than 512 bytes, the vectorized policy selects one item per thread. The new test checks this selection across five compute capabilities and verifies fill results for 100 and 10,000 elements.

Assessment against linked issues

Objective Addressed Explanation
Reduce excessive per-thread staging for large power-of-two fill values [#11758] ❓ The policy reduces staging to one item per thread for values larger than 512 bytes. The test does not measure device-memory reservation, and one staged value still scales with the value size.

Suggested reviewers: bernhardmgruber

Fixed issue severity: <fixed_issue_severity>Medium</fixed_issue_severity>

Priority: ➖ Normal

Change: Bug fix

Merge Risk: 🔵 Low · up to f450e

Large fills currently use one item per thread, but the test does not assert that the vectorized algorithm is selected. Add that assertion so a policy regression cannot restore stack growth without failing the test.

  • Autopilot · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Comment @coderabbitai help to get the list of available commands.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
cub/test/catch2_test_device_transform.cu (1)

588-598: 🚀 Performance & Scalability | 🔵 Trivial | ⚡ Quick win

suggestion: Assert the vectorized algorithm as well as the item count.

The test checks items_per_thread on a separately constructed selector, then checks only the output values from DeviceTransform::Fill. A different Fill policy can therefore pass the test if it produces the same values. Assert algorithm == TransformAlgorithm::vectorized in the selector check to protect the intended large-fill policy.

Suggested fix
-    REQUIRE(policy_selector_t{}(cuda::compute_capability{cc}).vectorized.items_per_thread == 1);
+    const auto policy = policy_selector_t{}(cuda::compute_capability{cc});
+    REQUIRE(policy.algorithm == TransformAlgorithm::vectorized);
+    REQUIRE(policy.vectorized.items_per_thread == 1);

ℹ️ Review info
⚙️ Run configuration
  • Configuration used: Repository: NVIDIA/cccl/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: b45eb014-c1d1-49a1-8da3-85aace60372d
📥 Commits

Reviewing files that changed from the base of the PR and between bcfb6a0 and f450e7c.

📒 Files selected for processing (2)
  • cub/cub/device/dispatch/tuning/tuning_transform.cuh
  • cub/test/catch2_test_device_transform.cu

Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.

@bernhardmgruber bernhardmgruber left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Fix is a bit crude, since I think we should probably scale the items per thread as the value types get bigger, but it's good enough.

@bernhardmgruber

Copy link
Copy Markdown
Contributor

/ok to test f450e7c

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: In Review

Development

Successfully merging this pull request may close these issues.

[BUG]: cub::DeviceTransform fill with a large power-of-two value type reserves gigabytes of local memory

2 participants