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

Add square distance query variants to the vector distance benchmark #119219

Merged
merged 2 commits into from
Dec 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public class VectorScorerBenchmark {

RandomVectorScorer luceneDotScorerQuery;
RandomVectorScorer nativeDotScorerQuery;
RandomVectorScorer luceneSqrScorerQuery;
RandomVectorScorer nativeSqrScorerQuery;

@Setup
public void setup() throws IOException {
Expand Down Expand Up @@ -130,6 +132,8 @@ public void setup() throws IOException {
}
luceneDotScorerQuery = luceneScorer(values, VectorSimilarityFunction.DOT_PRODUCT, queryVec);
nativeDotScorerQuery = factory.getInt7SQVectorScorer(VectorSimilarityFunction.DOT_PRODUCT, values, queryVec).get();
luceneSqrScorerQuery = luceneScorer(values, VectorSimilarityFunction.EUCLIDEAN, queryVec);
nativeSqrScorerQuery = factory.getInt7SQVectorScorer(VectorSimilarityFunction.EUCLIDEAN, values, queryVec).get();

// sanity
var f1 = dotProductLucene();
Expand Down Expand Up @@ -157,6 +161,12 @@ public void setup() throws IOException {
if (q1 != q2) {
throw new AssertionError("query: lucene[" + q1 + "] != " + "native[" + q2 + "]");
}

var sqr1 = squareDistanceLuceneQuery();
var sqr2 = squareDistanceNativeQuery();
if (sqr1 != sqr2) {
throw new AssertionError("query: lucene[" + q1 + "] != " + "native[" + q2 + "]");
}
}

@TearDown
Expand Down Expand Up @@ -217,6 +227,16 @@ public float squareDistanceScalar() {
return 1 / (1f + adjustedDistance);
}

@Benchmark
public float squareDistanceLuceneQuery() throws IOException {
return luceneSqrScorerQuery.score(1);
}

@Benchmark
public float squareDistanceNativeQuery() throws IOException {
return nativeSqrScorerQuery.score(1);
}

QuantizedByteVectorValues vectorValues(int dims, int size, IndexInput in, VectorSimilarityFunction sim) throws IOException {
var sq = new ScalarQuantizer(0.1f, 0.9f, (byte) 7);
var slice = in.slice("values", 0, in.length());
Expand Down