fix(vector/search): select per-searcher top-k by distance, not underflow-prone similarity (#933) - #934
Merged
Merged
Conversation
…low-prone similarity (closes #933)
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.
Summary
Fixes the in-searcher sibling of #927's defect 2: per-segment searchers sorted and truncated their results by
similaritydescending, butdistance_to_similarity'sexp(-d)(Euclidean/Manhattan) underflows to0.0f32at long range — collapsing every distant candidate into a tie whose unstable-sort order decided top-k membership arbitrarily (DotProduct similarly saturates viaclamp(0,1)). Discovered by #931's frozen-behavior oracle: a 20-doc single-segment grid corpus returned[0, 1, 2, 3, 10]where brute force says[0, 1, 2, 3, 4]. The fan-out layer was fixed by #927/#928; the drop here happens earlier, at the per-segment truncate, where the fan-out cannot recover it.Changes
All five per-searcher sort sites (distance is already in the same tuple/struct at each) now order ascending by
distancewith adoc_idtiebreak, exactly matching the fan-out's #927 convention:hnsw/searcher.rs: graph-pathfinal_resultssort (the repro site) + linear-fallback candidate sortflat/searcher.rs: field-filtered + unfiltered scan sortsivf/searcher.rs: probed-cluster candidate sortmin_similarityfiltering and every reported similarity value are unchanged — only ordering/truncation stops flowing through the underflow-prone conversion.Tests
searcher_distance_sort_test.rs: HNSW / Flat / IVF pinned against exact brute-force top-k on the underflow-triggering grid fixture (discovery caseq = 0.2+ mid/far queries; IVF probes all clusters so the expectation is exact).[0, 1, 2, 3, 10]corruption.Verification
cargo fmt --checkandcargo clippy --all-targets -- -D warningsclean on stable and 1.97.0cargo test -p laurus --lib: 1258 passed;--tests: all 65 binaries ok — no order-dependent test affected (quantized and distance orders agree wherever similarity does not underflow)cargo check -p laurus-wasm --target wasm32-unknown-unknown: cleanCloses #933. Same defect class as #927 defect 2 (fan-out layer, PR #928). Found by and unblocks #931 (#650 rerank-pipeline campaign PR-1).