fix: Fixed wrong distances and neighbours across backends - #82
Merged
Merged
Conversation
- faiss: compare against Metric.COSINE (the string comparison never matched), convert inner-product and squared-L2 scores to distances, use metric-aware range search radii, and drop results padded with index -1 - annoy: store the metric enum so cosine queries are normalized - voyager: add items with explicit ids (ids could be assigned out of order) and return non-squared Euclidean distances - hnsw: return non-squared Euclidean distances - pynndescent: only normalize query vectors for the cosine metric
…es unconverted Halving squared L2 only gives cosine distance for unit vectors, so zero vectors came out at distance 0.5 instead of 1 on hnsw/scalar/ivf_scalar/ivfpq indexes. These are now built with inner product where FAISS supports it. LSH returns Hamming distances, which cannot be converted, so they are returned as is. The contract test now covers scalar indexes, zero vectors and threshold membership.
…scent graphs on load - faiss: pq and ivfpqr stay on L2, where halving squared L2 only gives cosine distance for unit vectors. Zero vectors (stored or queried) now get cosine distance 1; stored zero vectors are tracked through inserts and save/load. - pynndescent: the saved (indices, distances) neighbour graph came back as one float array, so querying a loaded index failed. The original dtypes are restored on load, which also fixes indexes saved by earlier versions. - The save/load test now queries the loaded index.
Every reachable fix now fails a test when reverted: FAISS padding (IVF with small clusters), range-search radii (thresholds beyond 0.5 cosine / 1 Euclidean), zero vectors through insert and save/load, LSH Hamming distances, item-to-vector mapping (every stored vector queried for itself), and string metrics stored as enums.
Codecov Report✅ All modified and coverable lines are covered by tests.
... and 1 file with indirect coverage changes 🚀 New features to boost your workflow:
|
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.
This PR fixes many many bugs that I found while comparing Vicinity backends in SemHash. In no particular order:
query_thresholdcould miss items for larger thresholds.hnsw,scalar,ivf_scalarandivfpqindexes are now built with inner product for cosine, likeflatandivf.The tests now catch all of these cases. I also want to add some proper integration tests in a followup.