Skip to content

Fix gather/scatter fast division bug - #3059

Merged
rapids-bot[bot] merged 5 commits into
NVIDIA:mainfrom
huuanhhuyn:bug_3055
Jun 29, 2026
Merged

Fix gather/scatter fast division bug#3059
rapids-bot[bot] merged 5 commits into
NVIDIA:mainfrom
huuanhhuyn:bug_3055

Conversation

@huuanhhuyn

@huuanhhuyn huuanhhuyn commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

raft::matrix::gather/scatter crashes at int32 boundary even when it advertises int64 type -> issue 3055. This PR fixes this issue.

int64 is still not supported for performance reason with int128.

closes #3055

@copy-pr-bot

copy-pr-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

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.

@tarang-jain tarang-jain 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! Thanks for the fix.

@tarang-jain

Copy link
Copy Markdown
Contributor

Actually, can we add a test for this diff? We could either add a test for FastIntDiv directly, or we can test the raft::matrix::gather when N * D exceeds INT_MAX. I am more inclined to have tests for both -- testing FastIntDiv should be quick where we just compare with the C++ operators ( / and %). But the more important one is to test the raft::matrix::gather for those larger matrices.

@huuanhhuyn
huuanhhuyn requested a review from a team as a code owner June 26, 2026 08:23
Comment thread cpp/tests/matrix/gather.cu Outdated
inplace_inputs_i64);
GATHER_TEST((GatherTest<false, false, true, float, int64_t, int64_t>),
GatherInplaceTestCI64I64,
inplace_inputs_i64_i32_max); // slow test, 8GB allocation, reproduces https://github.com/rapidsai/raft/issues/3055

@huuanhhuyn huuanhhuyn Jun 26, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@tfeher @tarang-jain Only float type is possible here (8GB allocation, 100ms). I would suggest to not have it. I already added a regression test for the fix in fast_int_div.cu

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.

I think it is fine to spend 100ms on this.

@tfeher tfeher 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.

Thanks Huy for the fix, LGTM!

Comment thread cpp/tests/matrix/gather.cu Outdated
inplace_inputs_i64);
GATHER_TEST((GatherTest<false, false, true, float, int64_t, int64_t>),
GatherInplaceTestCI64I64,
inplace_inputs_i64_i32_max); // slow test, 8GB allocation, reproduces https://github.com/rapidsai/raft/issues/3055

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.

I think it is fine to spend 100ms on this.

@tfeher

tfeher commented Jun 26, 2026

Copy link
Copy Markdown
Contributor

/ok to test 9acb86f

@aamijar
aamijar requested review from a team as code owners June 26, 2026 21:37
@aamijar

aamijar commented Jun 26, 2026

Copy link
Copy Markdown
Member

/ok to test 33ec5d7

@aamijar

aamijar commented Jun 26, 2026

Copy link
Copy Markdown
Member

/ok to test b18e428

@aamijar

aamijar commented Jun 27, 2026

Copy link
Copy Markdown
Member

/ok to test dc2ede3

- gather/scatter crashes at int32 boundary even when it advertises int64 type. This change fixes this issue.
- int64 is still not supported for performance reason with int128.
@huuanhhuyn

Copy link
Copy Markdown
Contributor Author

/ok to test 9f3d2cb

@huuanhhuyn

Copy link
Copy Markdown
Contributor Author

/ok to test d87dcdd

@huuanhhuyn

Copy link
Copy Markdown
Contributor Author

/merge

1 similar comment
@tfeher

tfeher commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

/merge

@tfeher tfeher 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

@tfeher

tfeher commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

/merge

1 similar comment
@gforsyth

Copy link
Copy Markdown
Contributor

/merge

@rapids-bot
rapids-bot Bot merged commit 30cb3e0 into NVIDIA:main Jun 29, 2026
79 checks passed
@aamijar

aamijar commented Jun 29, 2026

Copy link
Copy Markdown
Member

Hello @huuanhhuyn, going forward let's remember to build cuvs and build cuml at the least to ensure that raft doesn't cause any breakages. This PR is causing some breakages currently.

rapids-bot Bot pushed a commit to NVIDIA/cuml that referenced this pull request Jun 29, 2026
Add explicit template parameters for FastIntDiv, follow up to NVIDIA/raft#3059.

Authors:
  - Anupam (https://github.com/aamijar)

Approvers:
  - Jim Crist-Harif (https://github.com/jcrist)

URL: #8299
@huuanhhuyn
huuanhhuyn deleted the bug_3055 branch June 30, 2026 07:24
irina-resh-nvda added a commit to irina-resh-nvda/cuvs that referenced this pull request Jul 15, 2026
…around)

The shuffle_dataset path used an out-of-place gather into a temporary buffer to
work around an illegal memory access in raft's in-place gather overload when
n_rows * row_len exceeded 2^31 (32-bit index overflow).

That bug is now fixed upstream in raft (NVIDIA/raft#3059, closes #3055), which
the cuvs raft pin now includes. Revert to the in-place gather to drop the extra
full-size temporary allocation and copy.
rapids-bot Bot pushed a commit that referenced this pull request Aug 11, 2026
#3093)

This PR allows FastIntDiv to support the full 64-bit range values. Within the 32-bit range values, the fast implementation is selected. When exceeds 32-bit, it fallbacks during runtime to the normal division. The overhead of runtime check is measured to be negligible.

FastIntDiv is used by in-place gather() where number of columns (divisor) is set once and the / and % divisions are called a lot more frequent to obtain row and col numbers from the given flat index. Therefore, the benchmark compares the fast int division against the normal division at 100 divisors and 1 million numerators for 32-bit and 64-bit range.
```
----------------------------------------------------------------------------------------
Benchmark                                              Time             CPU   Iterations
----------------------------------------------------------------------------------------
fast_int_div_i32/FastIntDiv/int32/manual_time      0.138 ms        0.893 ms         5086
native_div_i32/NativeIntDiv/int32/manual_time      0.186 ms        0.941 ms         3755
fast_int_div_i64/FastIntDiv/int64/manual_time      0.762 ms         1.52 ms          921
native_div_i64/NativeIntDiv/int64/manual_time      0.742 ms         1.50 ms          948
```
@mfoerste4 Original version without the current changes and [this PR](#3059) -> Only negligible runtime overhead (~4%) is added.
```
----------------------------------------------------------------------------------------
Benchmark                                              Time             CPU   Iterations
----------------------------------------------------------------------------------------
fast_int_div_i32/FastIntDiv/int32/manual_time      0.131 ms        0.223 ms         5371
native_div_i32/NativeIntDiv/int32/manual_time      0.180 ms        0.273 ms         3884
```

The [gather() bench](https://github.com/huuanhhuyn/raft/blob/fc9681c1821fc4a1559b8970a8e814f17569f84a/cpp/bench/prims/matrix/gather.cu) gives a similar comparison. The added runtime check is only a negligible overhead

Authors:
  - https://github.com/huuanhhuyn

Approvers:
  - Malte Förster (https://github.com/mfoerste4)
  - Divye Gala (https://github.com/divyegala)

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

Labels

breaking Breaking change bug Something isn't working

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

[BUG] raft::matrix::gather (in-place overload): illegal memory access when N*D ≥ 2^31 (32-bit index overflow)

6 participants