Skip to content

FIX adaptive fixed-point scale for weighted float32-only cumsums - #24

Merged
ogrisel merged 1 commit into
fix-34813-cmat-weighted-int-scalefrom
cursor/adaptive-cmat-weight-scale-4d84
Aug 28, 2026
Merged

ogrisel merged 1 commit into
fix-34813-cmat-weighted-int-scalefrom
cursor/adaptive-cmat-weight-scale-4d84

Conversation

@ogrisel

@ogrisel ogrisel commented Aug 28, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to the weighted float32-only fix on fix-34813-cmat-weighted-int-scale (upstream scikit-learn#34827), addressing the failure mode discussed in review.

Why

A fixed round(weight * 1e6) scale works for O(1) sample weights but zeros out pre-normalized (sum≈1) or uniformly tiny weights once the mean weight drops below ~5e-7 (e.g. large n). That is the real mechanism behind the scikit-learn#34827 review concern, even though that review mis-attributed it to an in-function weight / weight.sum() step that the branch does not perform.

Approach

Keep absolute weighted counts (do not normalize then rescale). Choose the micro-unit scale from the mean weight:

  • scale ≈ 1e6 * n / weight.sum() so the average weight gets ~1e6 integer units
  • Cap with int64 headroom using the conservative bound n * max(weight)

For typical O(1) weights this recovers scale ≈ 1e6. For pre-normalized / tiny weights it raises the scale so values survive rounding.

Tests

Extended test_confusion_matrix_at_thresholds_float32_only_weighted_large_n with normalized and tiny kinds, including an assertion that the old fixed 1e6 scale would quantize all weights to zero. All four parametrizations pass locally on array_api_strict no_float64.

Review focus

  • Adaptive scale / int64 overflow cap
  • Whether float32 sum/max for scale selection is acceptable (order-of-magnitude only; counts still come from int64 cumsum)
  • Tolerances for large O(1) totals vs O(1)-normalized totals

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

Open in Web Open in Cursor 

Choose the micro-unit scale from the mean sample weight (capped by int64
headroom via n * max(weight)) so pre-normalized or uniformly tiny weights
do not round to zero under a fixed 1e6 scale, while O(1) weights keep the
previous micro-unit resolution. Extend the float32-only large-n regression
test to cover those failure modes.

Co-authored-by: Olivier Grisel <[email protected]>
@ogrisel
ogrisel marked this pull request as ready for review August 28, 2026 16:05
@ogrisel
ogrisel merged commit c6f51d5 into fix-34813-cmat-weighted-int-scale Aug 28, 2026
45 of 47 checks passed
@ogrisel
ogrisel deleted the cursor/adaptive-cmat-weight-scale-4d84 branch August 28, 2026 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants