Fix _EfficientKMeans cluster reduction to use intra-cluster errors (fixes #2698)#2700
Merged
TobyRoseman merged 1 commit intoMay 21, 2026
Merged
Conversation
`_EfficientKMeans.fit` reduces `n_clusters` when removing the
smallest-population centroid still keeps the per-point RMSE under
`error_bnd`. The candidate `reduce_inertia` was being computed from
`reduce_cluster_centers_.sum()` (the sum of the remaining centroid
coordinates), so:
* the RMSE comparison against `error_bnd` was meaningless;
* when data pushed the centroid sum negative, `sqrt(negative / N)`
silently produced NaN and `NaN < error_bnd` evaluated false, so
no cluster was ever reduced.
This patch sums the per-point min distance errors instead, matching
the pattern already used a few lines above for `cur_inertia`:
cur_inertia = min_error.sum() # line 247 (unchanged)
reduce_inertia = reduce_min_error.sum() # line 262 (this change)
A regression test covers the all-negative-data case from the issue.
Fixes apple#2698
Collaborator
NehalBhandari
approved these changes
May 21, 2026
Collaborator
|
Thanks @LeSingh1 |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
_EfficientKMeans.fitreducesn_clusterswhen removing the smallest-population centroid still keeps the per-point RMSE undererror_bnd. The candidatereduce_inertiawas being computed fromreduce_cluster_centers_.sum()(the sum of the remaining centroid coordinates), which is the wrong quantity:reduce_inertia / Nis meant to be a mean squared error, but summing centroid coordinates produces a value with no consistent sign or magnitude relative toerror_bnd._torch.sqrt(negative / N)silently producesNaN.NaN < self.error_bndevaluates toFalse, so cluster reduction is skipped even when it should fire.Fix
Sum the per-point min distance errors instead, mirroring the pattern already used a few lines above for
cur_inertia:This is the fix suggested by the reporter in #2698.
Verification
Added
coremltools/test/optimize/torch/palettization/test_efficient_kmeans.pycovering the all-negative-data case from the issue. Without the patch the test fails withassert 3 == 2; with the patch it passes:Issue
Fixes #2698