You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[REVIEW] Fix int32 overflow in mutual_info_score and clip negative results - #3221
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.
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]>
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.
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: 4
Check name
Status
Explanation
Title check
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
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
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
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
Included review availability: This review used your included allowance. Your plan provides up to 12 included reviews per hour; 11 remain after this review.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_scoreandv_measurewere affected throughmutual_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
intrange. These fail without the fix.intproduct; it is now computed in double precision.Drafted with Claude Code (Fable 5.2).