Skip to content

fix(index): scale given centroids of cosine indexes to unit length - #260

Merged
gouyt13 merged 1 commit into
dev/0.9from
fix/cosine-centroids
Nov 20, 2026
Merged

gouyt13 merged 1 commit into
dev/0.9from
fix/cosine-centroids

Conversation

@gouyt13

@gouyt13 gouyt13 commented Nov 20, 2026

Copy link
Copy Markdown
Member

Closes #259.

Cosine indexes scale data and queries to unit length, but set_centroids (and the legacy construct) used the given centroids as they were. The codes are residuals against the centroids, so centroids far from the unit sphere ruined the estimates. Reproduction from #259, 4000 points in 64 dimensions, centroids of norm about 100, 5 bits:

Index Before: recall@10 After: recall@10
HNSW, raw centroids 0.001 0.943
IVF (flat routing), raw centroids 0.001 0.950

The "after" values equal those with centroids scaled by the caller. Indexes that train their own centroids give the same results as before.

Change

  • ivf::IVF and hnsw::HierarchicalNSW scale centroids given to set_centroids or construct to unit length, after check_coordinates, as they scale data. A zero centroid stays zero. IvfIndex, HnswIndex and the Python classes get this through set_centroids.
  • train keeps the centroids k-means computes from the scaled data (with the default training parameters, spherical k-means already returns unit-length centroids), so trained indexes are bit-identical to before.
  • The scaled centroids are what format 2 stores; docs/formats.md, the cosine section of usage.md, the release notes, the C++ comments and the Python docstrings say so.

Tests

  • C++ IvfCosineTest.ScalesGivenCentroidsToUnitLength and the given-centroid part of HnswCosineTest.MatchesInnerProductOverNormalizedVectors: a cosine index given centroids of norm about 100 matches an inner-product index given the scaled centroids, in search results and in the saved sections. The IVF construct path, whose rotation is drawn, checks the estimated distances against exact 1 - cos.
  • Python test_cosine_trained_or_given_centroids_match_inner_product passes unscaled centroids.
  • Without the scaling, the C++ tests fail; the IVF construct error rises from below 0.06 to about 19.

Checks run (Linux x86-64, g++ 14, native optimization off): C++ suite 616 passed, 1 skipped (environment); the new tests repeated 50 times; Python suite 1208 passed, 2 skipped, with the package rebuilt from this branch; mypy and Pyright tests; check-format.sh, full check-tidy.sh, check-python.sh, mkdocs build --strict.

Cosine indexes scale data and queries to unit length, but set_centroids
and construct used the given centroids as they were. The codes are
residuals against the centroids, so centroids far from the unit sphere,
such as the means of unscaled data, ruined the estimates: with centroids
of norm about 100, recall@10 fell to 0.001 at 5 bits.

ivf::IVF and hnsw::HierarchicalNSW now scale centroids given to
set_centroids or construct, after checking their coordinates, as they
scale data. train keeps the centroids k-means computes from the scaled
data, so trained indexes are unchanged. IvfIndex, HnswIndex and the
Python classes get the fix through set_centroids.

Closes #259.
@github-actions github-actions Bot added the release:fixed Release category managed from the PR title label Nov 20, 2026
@gouyt13
gouyt13 merged commit 9f07dd5 into dev/0.9 Nov 20, 2026
26 checks passed
@gouyt13
gouyt13 deleted the fix/cosine-centroids branch November 20, 2026 16:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fixed Release category managed from the PR title

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant