Skip to content

[REVIEW] Preserve infinities in compensated sum reductions - #3233

Open
galleonli wants to merge 1 commit into
NVIDIA:mainfrom
galleonli:fix/nonfinite-compensated-sum
Open

galleonli wants to merge 1 commit into
NVIDIA:mainfrom
galleonli:fix/nonfinite-compensated-sum

Conversation

@galleonli

Copy link
Copy Markdown

A column with one infinity and 31 ones sums to NaN in raft::stats::sum. The running sum already has the right sign, but the rounding correction computes inf - inf. Adding that correction back turns the result into NaN.

This skips the correction when the partial sum is already nonfinite. Finite partial sums still use the same correction, and a column containing both signs of infinity still produces NaN. The regression test covers both matrix layouts and the short, medium and long reduction paths in float and double.

On a GTX 1660 SUPER, all 82 tests in stats/sum.cu passed. On finite inputs, the largest increase in median time across two timing runs was 2.3%, for a long contiguous FP32 column. That same case measured 3.2% faster in the other run.

Fixes #3232.

@copy-pr-bot

copy-pr-bot Bot commented Nov 28, 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.

@coderabbitai

coderabbitai Bot commented Nov 28, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes

    • Corrected compensated summation so results involving positive or negative infinity, opposing infinities, and overflow are handled consistently across supported reduction paths.
    • Preserved accurate sums for unaffected columns when other columns contain non-finite values.
  • Tests

    • Added coverage for non-finite column sums across floating-point types, memory layouts, and input sizes.

Reduction kernels now suppress compensation when a partial sum is non-finite. New typed tests check infinity, NaN, and overflow results across data types, layouts, and row counts.

Changes: Non-finite sum handling

Layer / File(s) Summary
Correction helper
cpp/include/raft/util/reduction.cuh
Added sum_correction. For types that support infinity, it returns zero when the sum is non-finite. For other types, it returns the correction unchanged.
Reduction kernel integration
cpp/include/raft/linalg/detail/coalesced_reduction-inl.cuh, cpp/include/raft/linalg/detail/strided_reduction.cuh
Thin, medium, thick, and strided reduction paths use sum_correction when combining sums with compensation.
Non-finite sum tests
cpp/tests/stats/sum.cu
Added float and double tests for both layouts and three row counts. The tests check infinity propagation, NaN results, overflow, and an unaffected column.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: mfoerste4, achirkin, divyegala

Merge Risk: Minimal · up to 89ae6

This change makes column sums with a single infinity return infinity instead of NaN, while opposing infinities still give NaN. Finite results are unchanged, and the author reports a small performance cost. No merge-blocking risk remains.

Security architecture risk: Minimal · up to 89ae6

The change prevents a rounding correction from turning a valid infinite sum into NaN. Finite partial sums still use the same correction, and the fix does not add a new way to invoke the reduction or access data.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: compensated sum reductions in raft::stats::sum now keep infinities instead of producing NaN. It matches the changeset and the linked objectives. It is specific and not…
Description check Passed The description states the bug (a column with one infinity and ones sums to NaN), the fix (skip the rounding correction when the partial sum is nonfinite), the regression test coverage, and the test a…
Linked Issues check Passed [#3232] The issue requires that a column with one positive infinity and otherwise ones sum to +inf, one negative infinity sum to -inf, and a column with both signs of infinity sum to NaN, for float an…
Out of Scope Changes check Passed All changed files relate to the fix. reduction.cuh adds the helper and its required <cuda/std/cmath> and <cuda/std/limits> includes. The two reduction kernels apply the helper at the compensatio…
Full details: Title check

Explanation

The title names the main change: compensated sum reductions in raft::stats::sum now keep infinities instead of producing NaN. It matches the changeset and the linked objectives. It is specific and not vague.

Full details: Description check

Explanation

The description states the bug (a column with one infinity and ones sums to NaN), the fix (skip the rounding correction when the partial sum is nonfinite), the regression test coverage, and the test and timing results. It directly describes the changeset.

Full details: Linked Issues check

Explanation

[#3232] The issue requires that a column with one positive infinity and otherwise ones sum to +inf, one negative infinity sum to -inf, and a column with both signs of infinity sum to NaN, for float and double. The PR adds raft::detail::sum_correction in cpp/include/raft/util/reduction.cuh. It returns the rounding correction only when the running sum is finite, and returns T{0} otherwise. The PR applies this helper in coalesced_reduction-inl.cuh (thin, medium, and thick add-reduction kernels) and in strided_reduction.cuh (stridedSummationKernel grid-reduction atomic add). With one infinity, the running sum stays infinite and the correction is skipped, so the result is +/-inf. With both signs, the running sum becomes NaN, so the result stays NaN. Finite sums keep the existing correction. The added tests in cpp/tests/stats/sum.cu cover float and double, row-major and column-major layouts, and 32, 1024, and 131072 rows, which map to the short, medium, and long paths. No coding requirement remains unmet. The issue's reproduction uses a 32-element float column, which the tests cover.

Full details: Out of Scope Changes check

Explanation

All changed files relate to the fix. reduction.cuh adds the helper and its required &lt;cuda/std/cmath&gt; and &lt;cuda/std/limits&gt; includes. The two reduction kernels apply the helper at the compensation points. The test file adds non-finite sum tests. The copyright line change in reduction.cuh is a minor, administrative edit to the same file. It does not change behavior and is low-risk. One behavior change is not covered by the issue: a finite input set that overflows to infinity now returns inf instead of NaN, because the correction is skipped. The issue does not require this case. The PR summary and test description do mention NaN for overflowing finite values, so the test expectations for that case should be checked against this behavior. This does not make a change out of scope.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
Fix failing CI checks

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: NVIDIA/raft
  • Branch: main
  • File: .coderabbit.yaml
  • Commit: b74966f
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: 640de6d0-f68a-4a12-8f6e-d8f965cf7e3a
Commits

Reviewing files that changed from the base of the PR and between b74966f and 89ae68f.

Files selected for processing: 4

  • cpp/include/raft/linalg/detail/coalesced_reduction-inl.cuh
  • cpp/include/raft/linalg/detail/strided_reduction.cuh
  • cpp/include/raft/util/reduction.cuh
  • cpp/tests/stats/sum.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.

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

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

[BUG] A column with one infinity sums to NaN in raft::stats::sum

1 participant