Skip to content

Add binary prefiltering and candidate rescoring to flat search - #1623

Draft
xinyuwen2 wants to merge 1 commit into
mainfrom
wxy/flat_scan_prefilter
Draft

xinyuwen2 wants to merge 1 commit into
mainfrom
wxy/flat_scan_prefilter

Conversation

@xinyuwen2

Copy link
Copy Markdown
Contributor
  • Does this PR have a descriptive title that could go in our release notes?
  • Does this PR add any new dependencies?
  • Does this PR modify any existing APIs?
  • Is the change to the API backwards compatible?
  • Should this result in any changes to our documentation, either updating existing docs or adding new ones?

Reference Issues/PRs

What does this implement/fix? Briefly explain your changes.

Adds flat::binary_knn_search, an opt-in approximate search mode. It scans precomputed sign-bit signatures by Hamming distance, retains up to k × rescoring_factor candidate IDs, and asks the visitor to rescore those candidates with the original distance metric. Only the rescored distances determine the final top-k. Increasing rescoring_factor gives the second stage more candidates at the cost of additional vector reads and distance computations.

Any other comments?

Compared the two-stage search with exact flat::knn_search on two datasets of one million FP32 vectors each. All four configurations reached at least 99.9% mean Recall@K on the validation queries.

Dataset K rescoring_factor Vectors rescored Validation Recall@K Exact mean latency Two-stage mean latency Speedup
OpenAI embeddings (1536d) 100 39 3,900 (0.39%) 99.9531% 516.74 ms 27.36 ms 18.88×
OpenAI embeddings (1536d) 1000 27 27,000 (2.70%) 99.9195% 517.49 ms 51.97 ms 9.96×
Wikipedia/Cohere (768d) 100 2727 272,700 (27.27%) 99.9531% 252.08 ms 168.54 ms 1.50×
Wikipedia/Cohere (768d) 1000 386 386,000 (38.60%) 99.9047% 253.26 ms 214.50 ms 1.18×

We swept rescoring_factor on 256 calibration queries per dataset and measured latency on 128 separate validation queries, with two interleaved runs per query on a single CPU thread. Both paths used inner product. OpenAI queries excluded their own source vector.

The curves below show mean recall on the calibration queries. The lower panels zoom in on the 99.9% target. Diamonds mark the factors used for the latency comparison.
image

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 99.44444% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 92.29%. Comparing base (ad437bd) to head (248fcbb).

Files with missing lines Patch % Lines
diskann/src/flat/binary.rs 99.44% 1 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##             main    #1623    +/-   ##
========================================
  Coverage   92.28%   92.29%            
========================================
  Files         633      634     +1     
  Lines      133933   134113   +180     
========================================
+ Hits       123603   123776   +173     
- Misses      10330    10337     +7     
Flag Coverage Δ
miri 92.29% <99.44%> (+<0.01%) ⬆️
unittests 92.24% <99.44%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
diskann/src/flat/binary.rs 99.44% <99.44%> (ø)

... and 5 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants