Repository navigation
fix(index): scale given centroids of cosine indexes to unit length - #260
Merged
Merged
Conversation
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.
This was referenced Nov 20, 2026
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.
Closes #259.
Cosine indexes scale data and queries to unit length, but
set_centroids(and the legacyconstruct) 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: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::IVFandhnsw::HierarchicalNSWscale centroids given toset_centroidsorconstructto unit length, aftercheck_coordinates, as they scale data. A zero centroid stays zero.IvfIndex,HnswIndexand the Python classes get this throughset_centroids.trainkeeps 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.docs/formats.md, the cosine section ofusage.md, the release notes, the C++ comments and the Python docstrings say so.Tests
IvfCosineTest.ScalesGivenCentroidsToUnitLengthand the given-centroid part ofHnswCosineTest.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 IVFconstructpath, whose rotation is drawn, checks the estimated distances against exact1 - cos.test_cosine_trained_or_given_centroids_match_inner_productpasses unscaled centroids.constructerror 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, fullcheck-tidy.sh,check-python.sh,mkdocs build --strict.