Avoid overflow in CUDA reduction scans - #32137
Conversation
There was a problem hiding this comment.
Pull request overview
Improves CUDA reduction scan arithmetic near INT_MAX to prevent index overflow and invalid output writes.
Changes:
- Adds overflow-safe scan helpers.
- Refactors scalar and matrix-row reduction loops.
- Adds boundary arithmetic tests.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
reduction_functions.cu |
Uses safe scan advancement and validates output columns. |
reduction_utils.cuh |
Adds scan validation and advancement helpers. |
reduction_functions_test.cc |
Tests scan arithmetic near INT_MAX. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Tianlei Wu (tianleiwu)
left a comment
There was a problem hiding this comment.
The overflow-safe runtime loop changes look sound, but both Linux CUDA and TensorRT CI currently fail while compiling the new unit-test include. Details are inline.
Pull request was closed
Tianlei Wu (tianleiwu)
left a comment
There was a problem hiding this comment.
Reviewed the current head. The remaining-count checks avoid constructing overflowing absolute indices, and scan advancement only adds a step when the result stays below the limit. The block-uniform column loop keeps every thread participating in each synchronization while valid_col guards partial-block loads and writes. The host-compatible helper split resolves the earlier compile blocker, and all current CI checks pass. I do not see any remaining issues.
This pull request refactors and improves the CUDA reduction functions to make index calculations safer and more robust, especially for large tensor sizes. The main changes introduce utility functions to avoid integer overflow and clarify the logic for stepping through elements during reductions. Additionally, new tests are added to ensure correct behavior at boundary conditions.
Reduction logic improvements:
reduction_scan_delta_is_validandadvance_reduction_scan, inreduction_utils.cuhto encapsulate index validation and advancement logic for reduction scans, reducing the risk of integer overflow and improving code clarity.reduce_allandreduce_matrix_rows_kernelinreduction_functions.cuto use these new utility functions for safer and clearer iteration over elements and rows. [1] [2]Bug fixes and correctness:
reduce_matrix_rows_kernelwhere output writes could occur for invalid columns by adding avalid_colcheck before atomic writes.Testing improvements:
ScanIndexArithmeticAtIntMax, inreduction_functions_test.ccto verify correct behavior of the new utility functions at integer limit edge cases, ensuring robustness for very large tensors.reduction_utils.cuhin the test file.Code cleanup:
reduce_matrix_rows_kernelfor clarity.