perf(index): use dot distance for cosine in FlatFloatStorage#7777
Open
clearlove10-c wants to merge 1 commit into
Open
perf(index): use dot distance for cosine in FlatFloatStorage#7777clearlove10-c wants to merge 1 commit into
clearlove10-c wants to merge 1 commit into
Conversation
For IVF indexes with cosine distance, vectors are already normalized by the IVF transform pipeline (NormalizeTransformer), so cosine distance is mathematically equivalent to dot distance over normalized vectors. However, cosine still computes the L2 norm of each vector during distance calculation — redundant work that dot avoids. This converts Cosine to Dot in FlatFloatStorage::try_from_batch, consistent with how PQ and RQ already convert Cosine to L2 for the same reason. Adds tests verifying: - Cosine is converted to Dot - L2 and Dot are preserved unchanged - Partition serde round-trip adapts to the conversion
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: ASSERTIVE Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughFlat vector storage now normalizes cosine distance to dot distance for IVF-normalized vectors. Tests cover conversion, preservation of other distance types, cosine/dot equivalence, and serialized codec restoration. ChangesDistance normalization
Estimated code review effort: 2 (Simple) | ~10 minutes Possibly related issues
Suggested reviewers: 🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
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.
Problem
For IVF indexes with cosine distance, vectors are already normalized
by the IVF transform pipeline (NormalizeTransformer), so cosine
distance is mathematically equivalent to dot distance over normalized
vectors. However, cosine still computes the L2 norm of each vector
during distance calculation — redundant work that dot avoids.
Solution
Convert Cosine to Dot in
FlatFloatStorage::try_from_batch, consistentwith how PQ and RQ already convert Cosine to L2 for the same reason.
Benchmark
scripts below
my_ann_bench.py
Tests
test_try_from_batch_converts_cosine_to_dot— verifies Cosine → Dottest_try_from_batch_keeps_non_cosine_distance_types— verifies L2/Dot unchangedSummary by CodeRabbit