Skip to content

[REVIEW] Fix int32 overflow in mutual_info_score and clip negative results - #3221

Open
jpintar wants to merge 4 commits into
NVIDIA:mainfrom
jpintar:improve_mutual_info_score
Open

jpintar wants to merge 4 commits into
NVIDIA:mainfrom
jpintar:improve_mutual_info_score

Conversation

@jpintar

@jpintar jpintar commented Nov 17, 2026

Copy link
Copy Markdown

Closes #3220

Overflow of the marginal products

The mutual information kernel multiplied the row and column sums of the contingency matrix as 32-bit integers. The product overflows once it exceeds $2^{31} - 1$ (e.g. two clusters of more than about 46,341 samples each), which gave NaN or silently wrong scores. homogeneity_score, completeness_score and v_measure were affected through mutual_info_score.

The product is now evaluated in double precision, and the zero check tests the two marginals separately instead of testing their product.

Negative results

The mutual information is non-negative, but for nearly independent labelings rounding errors can make the sum of its terms slightly negative (e.g. -1.5e-15). The result is now clipped at zero, as scikit-learn does.

Tests

  • Added test cases with few large clusters (200,000 samples with 2 labels, 500,000 samples with 5 labels), for which the marginal products exceed the int range. These fail without the fix.
  • The reference value of the existing test was computed with the same int product; it is now computed in double precision.
  • Added a test with nearly independent labelings for which the unclipped result is negative.

Drafted with Claude Code (Fable 5.2).

Jura Pintar and others added 2 commits November 17, 2026 01:13
The mutual information kernel multiplied the row and column sums of the
contingency matrix as 32-bit integers. The product overflows once it
exceeds 2^31 - 1 (e.g. two clusters of about 46,000 samples each),
giving wrong or NaN scores. Evaluate the product in double precision.

Add test cases with few large clusters, and compute the test reference
in double precision as well.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
Mutual information is non-negative, but rounding errors can make
the sum of its terms slightly negative for nearly independent
labelings (e.g. -1.5e-15). Clip the result at zero, as scikit-learn
does.

Add a test with nearly independent labelings for which the unclipped
result is negative.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 17, 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 17, 2026 •

Copy link
Copy Markdown

Explain Change →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

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: 4f707352-b4e5-41f4-a176-32801e974c70
Commits

Reviewing files that changed from the base of the PR and between 8285908 and 9c06730.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • Trigger review

No actionable comments were generated in the recent review.

Summary

Summary by CodeRabbit

  • Bug Fixes
    • Improved mutual information score accuracy for large datasets by avoiding overflow in intermediate calculations. Scores for nearly independent labels are now kept at or above zero, with improved handling of cases where marginal counts are zero. These changes help produce more reliable results across varied label counts and dataset sizes.

mutual_info_score now evaluates marginal products as doubles, checks each marginal separately, and clamps the normalized result to zero or above. Tests update the reference calculation and add larger inputs and nearly independent labelings.

Changes: Mutual information score

Layer / File(s) Summary
Correct mutual information calculation
cpp/include/raft/stats/detail/mutual_info_score.cuh
The kernel checks each marginal separately and evaluates their product as a double. The normalized result is clamped to at least zero.
Validate large and nearly independent inputs
cpp/tests/stats/mutual_info_score.cu
The reference calculation uses double-precision marginal products. Added cases cover larger arrays and check that nearly independent labelings produce nonnegative scores within 0.000001 of zero.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: cjnolet, achirkin, bdice

Merge Risk: Minimal · previous review 193a3

The change fixes marginal-product overflow and prevents negative mutual-information scores without changing the API. Regression cases cover both behaviors. No blocking code issue remains; normal checks and required PR labels still need completion.

Security architecture risk: Minimal · up to 193a3

The change corrects overflow and negative rounding results in an existing calculation. Its effects propagate through existing callers, with no identified expansion of access, authority, or security exposure.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the two changes in the PR: the int32 overflow fix in the mutual information score and clipping of negative results. The raw summary confirms both changes in mutual_info_score.cuh and t…
Description check Passed The description explains the overflow in the marginal product, the switch to double precision, the clipping of negative results, and the new tests. These match the raw summary of changes to mutual_inf…
Linked Issues check Passed Directly linked issue #3220 requires that the marginal-count product in raft::stats::mutual_info_score not overflow int32, including the case where the product wraps to zero and the guard drops a term…
Out of Scope Changes check Passed The diff contains three kinds of change: the overflow fix in mutual_info_kernel (in scope for #3220), the new std::max clip of the MI result to zero in mutual_info_score, and a comment spelling fix pl…
Full details: Title check

Explanation

The title names the two changes in the PR: the int32 overflow fix in the mutual information score and clipping of negative results. The raw summary confirms both changes in mutual_info_score.cuh and the tests, so the title matches the changeset.

Full details: Description check

Explanation

The description explains the overflow in the marginal product, the switch to double precision, the clipping of negative results, and the new tests. These match the raw summary of changes to mutual_info_score.cuh and the test file.

Full details: Linked Issues check

Explanation

Directly linked issue #3220 requires that the marginal-count product in raft::stats::mutual_info_score not overflow int32, including the case where the product wraps to zero and the guard drops a term. At the reviewed head, mutual_info_kernel in cpp/include/raft/stats/detail/mutual_info_score.cuh computes log(double(a[i]) * double(b[j])) instead of log(double(a[i] * b[j])). The zero guard now tests a[i] != 0 and b[j] != 0 separately, so a wrapped product can no longer cause a term to be dropped. The product is evaluated in double precision, so the overflow path in the issue is removed. The PR summary reports added tests with 200,000 and 500,000 samples where the marginal products exceed the int range, and a reference value computed in double precision. Those test changes were read from the summary only, not from the test source. The issue's expected behavior (MI of a labeling with itself equals its entropy for all sample sizes) is consistent with the fixed formula. homogeneity_score, completeness_score and v_measure use this kernel and are fixed by the same change. No coding requirement from #3220 remains unmet in the reviewed code.

Full details: Out of Scope Changes check

Explanation

The diff contains three kinds of change: the overflow fix in mutual_info_kernel (in scope for #3220), the new std::max clip of the MI result to zero in mutual_info_score, and a comment spelling fix plus an added <algorithm> include. The clip is not requested by #3220, and the PR objectives state that negative-result clipping is not requested in this issue. The clip is still connected to the same function and result, and the PR adds a test for it (nearly independent labelings), so it is not an unrelated change. The comment fix and include support the edited code. No change was found that is unrelated to mutual_info_score. This assessment does not cover cuML or other repository files, which were not inspected.

Finishing Touches

Generate unit tests (beta)

  • 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: 4d8cfe6
  • Layers: Repository YAML
  • Review profile: CHILL
  • Plan: Enterprise
  • Run ID: fedac093-6926-4f0a-aa40-d776d436a320
Commits

Reviewing files that changed from the base of the PR and between 4d8cfe6 and 193a374.

Files selected for processing: 2

  • cpp/include/raft/stats/detail/mutual_info_score.cuh
  • cpp/tests/stats/mutual_info_score.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.

@viclafargue viclafargue 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! LGTM

@viclafargue viclafargue added bug Something isn't working non-breaking Non-breaking change labels Nov 26, 2026
@viclafargue

Copy link
Copy Markdown
Contributor

/merge

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

Labels

bug Something isn't working non-breaking Non-breaking change

Projects

Status: Approved

Development

Successfully merging this pull request may close these issues.

[BUG] mutual_info_score overflows int32 when multiplying marginal counts

3 participants