Skip to content

Improve intermediate precision in exact 1D clustering - #5619

Open
rioyu123 wants to merge 1 commit into
facebookresearch:mainfrom
rioyu123:codex/kmeans1d-cost-precision
Open

rioyu123 wants to merge 1 commit into
facebookresearch:mainfrom
rioyu123:codex/kmeans1d-cost-precision

Conversation

@rioyu123

Copy link
Copy Markdown

Summary

Clustering1D::train_exact() can lose precision in two intermediate calculations:

  • The interval-cost prefix sums are stored as double, but x * x is evaluated as float first. The rounded squared values can change the partition selected by the dynamic program when the input has a large common offset.
  • Cluster means are accumulated in float. For example, a single cluster of 128 identical 1000001.0f values currently returns 1000000.125f instead of 1000001.0f.

Promote each value before squaring and accumulate the cluster sum in double. Centroids remain float; the SMAWK recurrence, cost-table types, public API, and asymptotic complexity are unchanged. Numerical results are not bit-identical: removing intermediate float rounding can change centroids even on well-conditioned inputs, and can also change the selected partition.

The regression tests use the public Clustering1D::train_exact() path. They cover a known eight-cluster partition before and after exactly representable positive/negative offsets, including float output rounding, and constant-valued clusters. This is a targeted precision fix: the cost table still uses float, and prefix-sum subtraction remains subject to cancellation.

Validation

  • Built the CPU-only generic faiss_test target on Linux against main 6bbb068ad8dab612b84304400846da1b14539085.
  • Both new tests fail with the unchanged implementation and pass with this patch.
  • Full public C++ CTest run: 277 passed, 5 skipped (SIMD-specific checks in this generic build), no failures.
  • Additional standalone checks compare small cases against an independent quadratic-time dynamic-programming reference, with an allowance for float centroid rounding.
  • Changed C++ lines/new test formatting and git diff --check pass.
  • No Python bindings (including tests/test_clustering.py), GPU tests, or Meta-internal tests were run.

@meta-cla meta-cla Bot added the CLA Signed label Sep 10, 2026

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant