Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: implement XTR for retrieving multivector #3437

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

BubbleCal
Copy link
Contributor

this PR introduces XTR, which can score the documents without the original multivector, so we don't need any IO op for searching on multivector.

it sets the minimum similarity as the estimated similarity for missed documents of single query vector.

Signed-off-by: BubbleCal <[email protected]>
@BubbleCal BubbleCal marked this pull request as ready for review February 8, 2025 08:10
@codecov-commenter
Copy link

codecov-commenter commented Feb 8, 2025

Codecov Report

Attention: Patch coverage is 79.35484% with 32 lines in your changes missing coverage. Please review.

Project coverage is 78.92%. Comparing base (2295324) to head (dc7cc24).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
rust/lance/src/io/exec/knn.rs 79.86% 24 Missing and 5 partials ⚠️
rust/lance/src/dataset/scanner.rs 72.72% 0 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3437      +/-   ##
==========================================
+ Coverage   78.88%   78.92%   +0.04%     
==========================================
  Files         251      251              
  Lines       92230    92364     +134     
  Branches    92230    92364     +134     
==========================================
+ Hits        72752    72897     +145     
+ Misses      16508    16495      -13     
- Partials     2970     2972       +2     
Flag Coverage Δ
unittests 78.92% <79.35%> (+0.04%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

.map(|v| distance_type.func()(q, v))
.min_by(|a, b| a.partial_cmp(b).unwrap())
.map(|v| 1.0 - distance_type.func()(q, v))
.max_by(|a, b| a.total_cmp(b))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changing these so that the flat search results can be the same with IVF_FLAT, so the numbers won't confuse users

_ => unreachable!(),
};

let mut knn_node = if q.refine_factor.is_some() || is_multivec {
let mut knn_node = if q.refine_factor.is_some() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't require refine for multivector search any more

@@ -1061,7 +1063,7 @@ mod tests {
let gt = multivec_ground_truth(&vectors, &query, k, params.metric_type);
let gt_set = gt.iter().map(|r| r.1).collect::<HashSet<_>>();

let recall = row_ids.intersection(&gt_set).count() as f32 / 10.0;
let recall = row_ids.intersection(&gt_set).count() as f32 / 100.0;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the recall was calculated incorrectly, the previous algo requires refine_factor=5 to reach good enough recall

Copy link
Contributor

@wjones127 wjones127 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something seems off in the algorithm, with how missed_similarities is handled. Could you address my comment, and also maybe write a unit tests that shows we get correct results? out of this?

Comment on lines +739 to +740
let row_ids = batch[ROW_ID].as_primitive::<UInt64Type>();
let dists = batch[DIST_COL].as_primitive::<Float32Type>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we are using values here, can we add a debug assert that there are non nulls?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

// at most, we will have k * refine_factor results for each query
let mut results = HashMap::with_capacity(k * refactor);
let mut missed_similarities = 0.0;
while let Some((min_sim, batch)) = reduced_inputs.try_next().await? {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand the algorithm in the paper deeply, but it seems odd to me that the order of the ANN queries matters. It appears that later batches will be adding a higher missed_similarities value. Is that intentional?

It looks like the output order of select_all isn't deterministic. https://docs.rs/futures/latest/futures/stream/fn.select_all.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's intentional, this is a little bit complicated, will add more comments about this:
considering we are updating the final results with a batch from a query vector, and for a row x:

  • if x exists in results but not batch: set min_sim as the estimated similarity, the contribution is min_sim
  • if x exists in both, then the contribution is sim in batch
  • if x exists in only batch, this means all queries before missed this row, this algo maintains missed_similarities as the sum of min_sim so far, so the contribution is missed_similarities + sim

Signed-off-by: BubbleCal <[email protected]>
@BubbleCal
Copy link
Contributor Author

Something seems off in the algorithm, with how missed_similarities is handled. Could you address my comment, and also maybe write a unit tests that shows we get correct results? out of this?

we have tests here https://github.com/lancedb/lance/pull/3437/files#diff-6de816b72e7c722316243c57df4f809ad34dc8581367c72335154dada48c40edL993

@BubbleCal BubbleCal requested a review from wjones127 February 11, 2025 07:35
Signed-off-by: BubbleCal <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants