Skip to content

[rocSPARSE] Fix sctr/rocsparse_scatter integer overflow for large nnz (AISPARSE-652) - #11136

Merged
kliegeois merged 9 commits into
ROCm:developfrom
kliegeois:aisparse-652-sctr-large
Sep 22, 2026
Merged

kliegeois merged 9 commits into
ROCm:developfrom
kliegeois:aisparse-652-sctr-large

Conversation

@kliegeois

Copy link
Copy Markdown
Contributor

Summary

rocsparse_Xsctr and the generic rocsparse_scatter routine (which delegates to it) computed the element index in the device kernel using 32-bit arithmetic:

I idx = hipBlockIdx_x * BLOCKSIZE + hipThreadIdx_x;

hipBlockIdx_x * BLOCKSIZE is evaluated in 32-bit unsigned arithmetic before being widened to the (possibly 64-bit) index type I. For sparse vectors with nnz of 2^32 (~4.3 billion) or more entries, reachable with 64-bit indices, the block-index multiply overflows. The kernel maps one thread to one element with no grid-stride loop, so past the wrap point the tail of the vector is silently never scattered and no error is returned.

The fix casts the block index to the index type I before the multiply and iterates with a grid-stride loop; the launch grid is clamped to the device limit so a clamped grid still covers all nnz elements. This follows the grid-stride shape already used by doti_device.h.

Changes

  • sctr_device.h: cast hipBlockIdx_x/hipGridDim_x to I before the multiply and use a grid-stride loop over nnz.
  • rocsparse_sctr_impl.cpp: clamp the launch grid to maxGridSize[0].
  • CHANGELOG.md: document the fix under Resolved issues.
  • testing_sctr.cpp / test_sctr.yaml: add the sctr_extra regression test.

Test plan

  • New sctr_extra regression test drives rocsparse_scatter (64-bit index path) with nnz = 2^32 + 512, then checks that an element beyond the 2^32 boundary is scattered into y (y[1] == x_in). Before the fix this element is left untouched, so the test fails; after the fix it passes.
  • The case needs ~52 GB of device memory, so it is registered under the nightly category with a device_memory_gb: 55 guard and is skipped on smaller GPUs.

Made with Cursor

JIRA ID : AISPARSE-652

kliegeois and others added 2 commits August 19, 2026 16:53
The sctr kernel computed the element index as
`hipBlockIdx_x * BLOCKSIZE + hipThreadIdx_x`, which is evaluated in
32-bit unsigned arithmetic and wraps at 2^32 even when the index type I
is int64_t. Because the kernel mapped one thread to one element with no
grid-stride loop, elements past the wrap point were silently never
scattered.

Cast the block index to I before the multiply and add a grid-stride loop
over nnz so the kernel covers the full range regardless of grid size.
Clamp the launch grid.x against handle->properties.maxGridSize[0] so an
overflowing nnz cannot exceed the device grid limit. Fixes AC3/AC4 of
ROCM-28948 for rocsparse_Xsctr and rocsparse_scatter.

Co-authored-by: Cursor <cursoragent@cursor.com>
…AISPARSE-652)

Add the sctr_extra regression test driving the 64-bit-index path of
rocsparse_scatter with nnz = 2^32 + 512 and probing an element past the
2^32 boundary; before the fix the wrapped block index leaves it
unscattered. Guarded to the nightly category with a device_memory_gb
budget so it is skipped on smaller GPUs. Document the fix in CHANGELOG.

Co-authored-by: Cursor <cursoragent@cursor.com>
@therock-pr-bot

therock-pr-bot Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

✅ All Checks Passed — Ready for Review

Check Status Details
📝 PR Description ✅ Pass —
⛔ Forbidden Files ✅ Pass —
🧪 Unit Test ✅ Pass —
🔎 pre-commit ✅ Pass —
🚫 Draft PR 🔜 To Be Enabled —
🚩 Feature Flag 🔜 To Be Enabled —
📊 Code Coverage 🔜 To Be Enabled —
🤖 therock-pr-bot ✅ Pass —

🎉 All checks passed! This PR is ready for review.

📖 Need help? See the Policy FAQ for details on every check and how to fix failures.

🙋 Wish to Override Policy?

@therock-pr-bot

Copy link
Copy Markdown

🎉 All checks passed! This PR is ready for review.

@codecov

codecov Bot commented Aug 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #11136      +/-   ##
===========================================
+ Coverage    69.69%   70.78%   +1.09%     
===========================================
  Files         2932     2849      -83     
  Lines       481712   468248   -13464     
  Branches     69452    69174     -278     
===========================================
- Hits        335700   331422    -4278     
+ Misses      121811   112813    -8998     
+ Partials     24201    24013     -188     
Flag Coverage Δ *Carryforward flag
TensileLite-CPP 48.80% <ø> (+0.01%) ⬆️ Carriedforward from 3e981de
TensileLite-Unit 76.65% <ø> (+0.01%) ⬆️ Carriedforward from 3e981de
hipBLAS 90.62% <ø> (ø) Carriedforward from 3e981de
hipBLASLt 35.28% <ø> (ø) Carriedforward from 3e981de
hipCUB 82.68% <ø> (ø) Carriedforward from 3e981de
hipDNN 86.98% <ø> (-<0.01%) ⬇️ Carriedforward from 3e981de
hipFFT 54.81% <ø> (ø) Carriedforward from 3e981de
hipRAND 76.12% <ø> (ø) Carriedforward from 3e981de
hipSOLVER 68.96% <ø> (ø) Carriedforward from 3e981de
hipSPARSE 86.99% <ø> (ø) Carriedforward from 3e981de
rocBLAS 48.31% <ø> (ø) Carriedforward from 3e981de
rocFFT 46.27% <ø> (ø) Carriedforward from 3e981de
rocRAND 57.42% <ø> (ø) Carriedforward from 3e981de
rocSOLVER 76.83% <ø> (ø) Carriedforward from 3e981de
rocSPARSE 74.76% <100.00%> (+0.06%) ⬆️
rocThrust 93.15% <ø> (+14.81%) ⬆️ Carriedforward from 3e981de

*This pull request uses carry forward flags. Click here to find out more.

Files with missing lines Coverage Δ
...csparse/library/src/level1/rocsparse_sctr_impl.cpp 94.00% <100.00%> (+0.38%) ⬆️

... and 90 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

- use rocsparse::min to clamp the grid size instead of an if-branch
- name the grid-stride start index (gid) for readability
- move the large-nnz regression test from sctr_extra to scatter_extra
  (it exercises the generic rocsparse_scatter API)

Co-authored-by: Cursor <cursoragent@cursor.com>
Comment thread projects/rocsparse/CHANGELOG.md Resolved

@dso-amd dso-amd 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.

LGTM (see my suggestions on the changelog entry).

kliegeois and others added 2 commits September 15, 2026 11:20
…ix (AISPARSE-652)

Applies the dso-amd review suggestion on projects/rocsparse/CHANGELOG.md
verbatim: reword the 'Resolved issues' entry for rocsparse_Xsctr /
rocsparse_scatter to the shorter, standardized overflow description.

Co-authored-by: Cursor <cursoragent@cursor.com>
…nable (AISPARSE-652)

The large-nnz regression test built a sparse vector descriptor with
size = 2 and nnz = 2^32 + 512. rocsparse_create_spvec_descr rejects
nnz > size with rocsparse_status_invalid_size, so the test could never
pass; it went unnoticed because its device_memory_gb guard kept it from
being instantiated on every machine it had been run on so far.

Size the dense vector to nnz so the descriptor is valid, and raise
device_memory_gb to 70 to cover the now three nnz-sized device
allocations (32 GiB indices + 16 GiB values + 16 GiB dense vector).

Verified by running the test body at a scaled-down nnz: it passes with
the fixed kernel and fails when the kernel is made to drop the tail of
the sparse vector, so the probe is load-bearing.

Co-authored-by: Cursor <cursoragent@cursor.com>
kliegeois and others added 3 commits September 18, 2026 17:00
…PARSE-652)

The snap date for rocSPARSE 5.1.0 has passed, so this fix belongs in
the next version. Open "## (Unreleased) rocSPARSE 5.2.0" and move only
this PR's Resolved issues entry there. The 5.1.0 section is left
otherwise unchanged so the net diff against develop stays additive.
@kliegeois
kliegeois merged commit f55facc into ROCm:develop Sep 22, 2026
128 of 129 checks passed
@assistant-librarian

Copy link
Copy Markdown
Contributor

TheRock Submodule Bump Activity

Newest first

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants