Skip to content

Apache Lucene open-source search software

Fp16 re ranking and prefetch support (#16628)

Pulkit Gupta55fd2e75fb2b
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloat16VectorValues.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloat16VectorValues.java
index ab57500b386..3c8e1f67d6c 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloat16VectorValues.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene95/OffHeapFloat16VectorValues.java
@@ -91,6 +91,24 @@ public abstract class OffHeapFloat16VectorValues extends Float16VectorValues
return VectorEncoding.FLOAT16;
}
+ @Override
+ public void prefetch(final int[] ordsToPrefetch, int numOrds) throws IOException {
+ if (ordsToPrefetch == null) {
+ return;
+ }
+
+ int finalNumOrds = Math.min(numOrds, ordsToPrefetch.length);
+ if (finalNumOrds <= 1) {
+ return;
+ }
+
+ // calculate offset and prefetch immediately
+ for (int i = 0; i < finalNumOrds; i++) {
+ long offset = (long) ordsToPrefetch[i] * byteSize;
+ slice.prefetch(offset, byteSize);
+ }
+ }
+
public static OffHeapFloat16VectorValues load(
VectorSimilarityFunction vectorSimilarityFunction,
FlatVectorsScorer flatVectorsScorer,
diff --git a/lucene/core/src/java/org/apache/lucene/search/AbstractFullPrecisionVectorSimilarityValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/AbstractFullPrecisionVectorSimilarityValuesSource.java
new file mode 100644
index 00000000000..77e0231d3ee
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/search/AbstractFullPrecisionVectorSimilarityValuesSource.java
@@ -0,0 +1,132 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import org.apache.lucene.index.FieldInfo;
+import org.apache.lucene.index.KnnVectorValues;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.VectorSimilarityFunction;
+
+/**
+ * Base class for {@link DoubleValuesSource} implementations that compute vector similarity between
+ * a query vector and the raw full precision vectors of a KNN vector field. Subclasses supply the
+ * vector-type-specific access to the field values, leaving the common scoring flow here.
+ */
+abstract class AbstractFullPrecisionVectorSimilarityValuesSource extends DoubleValuesSource {
+
+ protected final String fieldName;
+ protected final VectorSimilarityFunction vectorSimilarityFunction;
+
+ protected AbstractFullPrecisionVectorSimilarityValuesSource(
+ String fieldName, VectorSimilarityFunction vectorSimilarityFunction) {
+ this.fieldName = fieldName;
+ this.vectorSimilarityFunction = vectorSimilarityFunction;
+ }
+
+ /** Returns the full precision similarity scores for documents in the given leaf. */
+ public DoubleValues getSimilarityScores(LeafReaderContext ctx) throws IOException {
+ return getValues(ctx, null);
+ }
+
+ @Override
+ public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
+ final KnnVectorValues vectorValues = getVectorValues(ctx);
+ if (vectorValues == null) {
+ checkField(ctx);
+ return DoubleValues.EMPTY;
+ }
+ final FieldInfo fi = ctx.reader().getFieldInfos().fieldInfo(fieldName);
+ if (fi.getVectorDimension() != queryDimension()) {
+ throw new IllegalArgumentException(
+ "Query vector dimension does not match field dimension: "
+ + queryDimension()
+ + " != "
+ + fi.getVectorDimension());
+ }
+
+ if (vectorSimilarityFunction == null) {
+ VectorScorer scorer = fullPrecisionRescorer(vectorValues);
+ if (scorer == null) {
+ return DoubleValues.EMPTY;
+ }
+ DocIdSetIterator iterator = scorer.iterator();
+ return new DoubleValues() {
+ @Override
+ public double doubleValue() throws IOException {
+ return scorer.score();
+ }
+
+ @Override
+ public boolean advanceExact(int doc) throws IOException {
+ return doc >= iterator.docID()
+ && (iterator.docID() == doc || iterator.advance(doc) == doc);
+ }
+ };
+ }
+ final KnnVectorValues.DocIndexIterator iterator = vectorValues.iterator();
+ return new DoubleValues() {
+ @Override
+ public double doubleValue() throws IOException {
+ return compareToQuery(vectorValues, iterator.index());
+ }
+
+ @Override
+ public boolean advanceExact(int doc) throws IOException {
+ return doc >= iterator.docID() && (iterator.docID() == doc || iterator.advance(doc) == doc);
+ }
+ };
+ }
+
+ /**
+ * Returns the full precision vector values for the field, or {@code null} if the field is absent.
+ */
+ protected abstract KnnVectorValues getVectorValues(LeafReaderContext ctx) throws IOException;
+
+ /** Raises the appropriate error when the field has no vector values of the expected type. */
+ protected abstract void checkField(LeafReaderContext ctx);
+
+ /** Returns the dimension of the query vector. */
+ protected abstract int queryDimension();
+
+ /**
+ * Returns a {@link VectorScorer} that scores the query vector against the full precision vectors,
+ * or {@code null} if no scorer is available.
+ */
+ protected abstract VectorScorer fullPrecisionRescorer(KnnVectorValues vectorValues)
+ throws IOException;
+
+ /** Compares the query vector against the field vector at the given ordinal. */
+ protected abstract double compareToQuery(KnnVectorValues vectorValues, int ord)
+ throws IOException;
+
+ @Override
+ public boolean needsScores() {
+ return false;
+ }
+
+ @Override
+ public DoubleValuesSource rewrite(IndexSearcher reader) throws IOException {
+ return this;
+ }
+
+ @Override
+ public boolean isCacheable(LeafReaderContext ctx) {
+ return true;
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
index a6750cbd986..f7099484106 100644
--- a/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
+++ b/lucene/core/src/java/org/apache/lucene/search/DoubleValuesSource.java
@@ -288,6 +288,21 @@ public abstract class DoubleValuesSource implements SegmentCacheable {
return new FloatVectorSimilarityValuesSource(queryVector, vectorField).getValues(ctx, null);
}
+ /**
+ * Returns a DoubleValues instance for computing the vector similarity score per document against
+ * the float16 query vector
+ *
+ * @param ctx the context for which to return the DoubleValues
+ * @param queryVector float16 query vector
+ * @param vectorField knn float16 field name
+ * @return DoubleValues instance
+ * @throws IOException if an {@link IOException} occurs
+ */
+ public static DoubleValues similarityToQueryVector(
+ LeafReaderContext ctx, short[] queryVector, String vectorField) throws IOException {
+ return new Float16VectorSimilarityValuesSource(queryVector, vectorField).getValues(ctx, null);
+ }
+
/**
* Creates a DoubleValuesSource that wraps a generic NumericDocValues field. Assumes no monotonic
* direction by default (Monotonicity.NONE).
diff --git a/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityValuesSource.java
new file mode 100644
index 00000000000..72b41ebbb0d
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/search/Float16VectorSimilarityValuesSource.java
@@ -0,0 +1,71 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Objects;
+import org.apache.lucene.index.Float16VectorValues;
+import org.apache.lucene.index.LeafReaderContext;
+
+/**
+ * A {@link DoubleValuesSource} which computes the vector similarity scores between the query vector
+ * and the {@link org.apache.lucene.document.KnnFloat16VectorField} for documents.
+ */
+class Float16VectorSimilarityValuesSource extends VectorSimilarityValuesSource {
+
+ private final short[] queryVector;
+
+ public Float16VectorSimilarityValuesSource(short[] vector, String fieldName) {
+ super(fieldName);
+ this.queryVector = vector;
+ }
+
+ @Override
+ public VectorScorer getScorer(LeafReaderContext ctx) throws IOException {
+ final Float16VectorValues vectorValues = ctx.reader().getFloat16VectorValues(fieldName);
+ if (vectorValues == null) {
+ Float16VectorValues.checkField(ctx.reader(), fieldName);
+ return null;
+ }
+ return vectorValues.scorer(queryVector);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(fieldName, Arrays.hashCode(queryVector));
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+ if (obj == null || getClass() != obj.getClass()) return false;
+ Float16VectorSimilarityValuesSource other = (Float16VectorSimilarityValuesSource) obj;
+ return Objects.equals(fieldName, other.fieldName)
+ && Arrays.equals(queryVector, other.queryVector);
+ }
+
+ @Override
+ public String toString() {
+ return "Float16VectorSimilarityValuesSource(fieldName="
+ + fieldName
+ + " queryVector="
+ + Arrays.toString(queryVector)
+ + ")";
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloat16VectorSimilarityValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloat16VectorSimilarityValuesSource.java
new file mode 100644
index 00000000000..1a851e69158
--- /dev/null
+++ b/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloat16VectorSimilarityValuesSource.java
@@ -0,0 +1,116 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.io.IOException;
+import java.util.Arrays;
+import java.util.Objects;
+import org.apache.lucene.index.Float16VectorValues;
+import org.apache.lucene.index.KnnVectorValues;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.VectorSimilarityFunction;
+
+/**
+ * A {@link DoubleValuesSource} that computes vector similarity between a query vector and the raw
+ * half-precision (FLOAT16) vectors indexed in the provided {@link
+ * org.apache.lucene.document.KnnFloat16VectorField} in documents.
+ */
+public class FullPrecisionFloat16VectorSimilarityValuesSource
+ extends AbstractFullPrecisionVectorSimilarityValuesSource {
+
+ private final short[] queryVector;
+
+ /**
+ * Creates a {@link DoubleValuesSource} that returns the vector similarity score between the
+ * provided query vector and the field for documents.
+ *
+ * @param vector the query vector
+ * @param fieldName the field name of the {@link org.apache.lucene.document.KnnFloat16VectorField}
+ * @param vectorSimilarityFunction the vector similarity function to use
+ */
+ public FullPrecisionFloat16VectorSimilarityValuesSource(
+ short[] vector, String fieldName, VectorSimilarityFunction vectorSimilarityFunction) {
+ super(fieldName, vectorSimilarityFunction);
+ this.queryVector = vector;
+ }
+
+ /**
+ * Creates a {@link DoubleValuesSource} that returns the vector similarity score between the
+ * provided query vector and the field for documents, using the similarity function configured for
+ * the field.
+ *
+ * @param vector the query vector
+ * @param fieldName the field name of the {@link org.apache.lucene.document.KnnFloat16VectorField}
+ */
+ public FullPrecisionFloat16VectorSimilarityValuesSource(short[] vector, String fieldName) {
+ this(vector, fieldName, null);
+ }
+
+ @Override
+ protected KnnVectorValues getVectorValues(LeafReaderContext ctx) throws IOException {
+ return ctx.reader().getFloat16VectorValues(fieldName);
+ }
+
+ @Override
+ protected void checkField(LeafReaderContext ctx) {
+ Float16VectorValues.checkField(ctx.reader(), fieldName);
+ }
+
+ @Override
+ protected int queryDimension() {
+ return queryVector.length;
+ }
+
+ @Override
+ protected VectorScorer fullPrecisionRescorer(KnnVectorValues vectorValues) throws IOException {
+ return ((Float16VectorValues) vectorValues).rescorer(queryVector);
+ }
+
+ @Override
+ protected double compareToQuery(KnnVectorValues vectorValues, int ord) throws IOException {
+ return vectorSimilarityFunction.compare(
+ queryVector, ((Float16VectorValues) vectorValues).vectorValue(ord));
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(fieldName, Arrays.hashCode(queryVector), vectorSimilarityFunction);
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj) return true;
+ if (obj == null || getClass() != obj.getClass()) return false;
+ FullPrecisionFloat16VectorSimilarityValuesSource other =
+ (FullPrecisionFloat16VectorSimilarityValuesSource) obj;
+ return Objects.equals(fieldName, other.fieldName)
+ && Objects.equals(vectorSimilarityFunction, other.vectorSimilarityFunction)
+ && Arrays.equals(queryVector, other.queryVector);
+ }
+
+ @Override
+ public String toString() {
+ return "FullPrecisionFloat16VectorSimilarityValuesSource(fieldName="
+ + fieldName
+ + " vectorSimilarityFunction="
+ + vectorSimilarityFunction
+ + " queryVector="
+ + Arrays.toString(queryVector)
+ + ")";
+ }
+}
diff --git a/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java b/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java
index f7097e9d261..fcacd6118b2 100644
--- a/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java
+++ b/lucene/core/src/java/org/apache/lucene/search/FullPrecisionFloatVectorSimilarityValuesSource.java
@@ -20,26 +20,24 @@ package org.apache.lucene.search;
import java.io.IOException;
import java.util.Arrays;
import java.util.Objects;
-import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.index.KnnVectorValues;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.VectorSimilarityFunction;
/**
- * A {@link DoubleValuesSource} that computes vector similarity between a query vector and raw full
- * precision vectors indexed in provided {@link org.apache.lucene.document.KnnFloatVectorField} in
- * documents.
+ * A {@link DoubleValuesSource} that computes vector similarity between a query vector and the raw
+ * full-precision vectors indexed in the provided {@link
+ * org.apache.lucene.document.KnnFloatVectorField} in documents.
*/
-public class FullPrecisionFloatVectorSimilarityValuesSource extends DoubleValuesSource {
+public class FullPrecisionFloatVectorSimilarityValuesSource
+ extends AbstractFullPrecisionVectorSimilarityValuesSource {
private final float[] queryVector;
- private final String fieldName;
- private VectorSimilarityFunction vectorSimilarityFunction;
/**
- * Creates a {@link DoubleValuesSource} that returns vector similarity score between provided
- * query vector and field for documents.
+ * Creates a {@link DoubleValuesSource} that returns the vector similarity score between the
+ * provided query vector and the field for documents.
*
* @param vector the query vector
* @param fieldName the field name of the {@link org.apache.lucene.document.KnnFloatVectorField}
@@ -47,15 +45,14 @@ public class FullPrecisionFloatVectorSimilarityValuesSource extends DoubleValues
*/
public FullPrecisionFloatVectorSimilarityValuesSource(
float[] vector, String fieldName, VectorSimilarityFunction vectorSimilarityFunction) {
+ super(fieldName, vectorSimilarityFunction);
this.queryVector = vector;
- this.fieldName = fieldName;
- this.vectorSimilarityFunction = vectorSimilarityFunction;
}
/**
- * Creates a {@link DoubleValuesSource} that returns vector similarity score between provided
- * query vector and field for documents. Uses the configured vector similarity function for the
- * field.
+ * Creates a {@link DoubleValuesSource} that returns the vector similarity score between the
+ * provided query vector and the field for documents, using the similarity function configured for
+ * the field.
*
* @param vector the query vector
* @param fieldName the field name of the {@link org.apache.lucene.document.KnnFloatVectorField}
@@ -64,69 +61,30 @@ public class FullPrecisionFloatVectorSimilarityValuesSource extends DoubleValues
this(vector, fieldName, null);
}
- /** Sugar to fetch full precision similarity score values */
- public DoubleValues getSimilarityScores(LeafReaderContext ctx) throws IOException {
- return getValues(ctx, null);
+ @Override
+ protected KnnVectorValues getVectorValues(LeafReaderContext ctx) throws IOException {
+ return ctx.reader().getFloatVectorValues(fieldName);
}
@Override
- public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
- final FloatVectorValues vectorValues = ctx.reader().getFloatVectorValues(fieldName);
- if (vectorValues == null) {
- FloatVectorValues.checkField(ctx.reader(), fieldName);
- return DoubleValues.EMPTY;
- }
- final FieldInfo fi = ctx.reader().getFieldInfos().fieldInfo(fieldName);
- if (fi.getVectorDimension() != queryVector.length) {
- throw new IllegalArgumentException(
- "Query vector dimension does not match field dimension: "
- + queryVector.length
- + " != "
- + fi.getVectorDimension());
- }
-
- if (vectorSimilarityFunction == null) {
- VectorScorer scorer = vectorValues.rescorer(queryVector);
- if (scorer == null) {
- return DoubleValues.EMPTY;
- }
- DocIdSetIterator iterator = scorer.iterator();
- return new DoubleValues() {
- @Override
- public double doubleValue() throws IOException {
- return scorer.score();
- }
-
- @Override
- public boolean advanceExact(int doc) throws IOException {
- return doc >= iterator.docID()
- && (iterator.docID() == doc || iterator.advance(doc) == doc);
- }
- };
- }
- final KnnVectorValues.DocIndexIterator iterator = vectorValues.iterator();
- return new DoubleValues() {
- @Override
- public double doubleValue() throws IOException {
- return vectorSimilarityFunction.compare(
- queryVector, vectorValues.vectorValue(iterator.index()));
- }
+ protected void checkField(LeafReaderContext ctx) {
+ FloatVectorValues.checkField(ctx.reader(), fieldName);
+ }
- @Override
- public boolean advanceExact(int doc) throws IOException {
- return doc >= iterator.docID() && (iterator.docID() == doc || iterator.advance(doc) == doc);
- }
- };
+ @Override
+ protected int queryDimension() {
+ return queryVector.length;
}
@Override
- public boolean needsScores() {
- return false;
+ protected VectorScorer fullPrecisionRescorer(KnnVectorValues vectorValues) throws IOException {
+ return ((FloatVectorValues) vectorValues).rescorer(queryVector);
}
@Override
- public DoubleValuesSource rewrite(IndexSearcher reader) throws IOException {
- return this;
+ protected double compareToQuery(KnnVectorValues vectorValues, int ord) throws IOException {
+ return vectorSimilarityFunction.compare(
+ queryVector, ((FloatVectorValues) vectorValues).vectorValue(ord));
}
@Override
@@ -150,14 +108,9 @@ public class FullPrecisionFloatVectorSimilarityValuesSource extends DoubleValues
return "FullPrecisionFloatVectorSimilarityValuesSource(fieldName="
+ fieldName
+ " vectorSimilarityFunction="
- + vectorSimilarityFunction.name()
+ + vectorSimilarityFunction
+ " queryVector="
+ Arrays.toString(queryVector)
+ ")";
}
-
- @Override
- public boolean isCacheable(LeafReaderContext ctx) {
- return true;
- }
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/RescoreTopNQuery.java b/lucene/core/src/java/org/apache/lucene/search/RescoreTopNQuery.java
index 523d194c0e8..b481ebf2927 100644
--- a/lucene/core/src/java/org/apache/lucene/search/RescoreTopNQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/RescoreTopNQuery.java
@@ -150,6 +150,23 @@ public class RescoreTopNQuery extends Query {
return new RescoreTopNQuery(in, valuaSource, n);
}
+ /**
+ * Utility method to create a new RescoreTopNQuery which uses half-precision (FLOAT16) vectors for
+ * rescoring.
+ *
+ * @param in the inner Query to rescore
+ * @param targetVector the target vector to compute score, as FLOAT16 bit patterns
+ * @param field the vector field to compute score
+ * @param n the number of results to keep
+ * @return the RescoreTopNQuery
+ */
+ public static Query createFullPrecisionRescorerQuery(
+ Query in, short[] targetVector, String field, int n) {
+ DoubleValuesSource valuesSource =
+ new FullPrecisionFloat16VectorSimilarityValuesSource(targetVector, field);
+ return new RescoreTopNQuery(in, valuesSource, n);
+ }
+
/**
* Creates a {@code RescoreTopNQuery} that computes top N results using multi-vector similarity
* comparisons against a late interaction field.
diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene95/TestOffHeapVectorValues.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene95/TestOffHeapVectorValues.java
index 8e57dac3abe..abb6017ad07 100644
--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene95/TestOffHeapVectorValues.java
+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene95/TestOffHeapVectorValues.java
@@ -35,6 +35,11 @@ public class TestOffHeapVectorValues extends LuceneTestCase {
OffHeapFloatVectorValues floatValues = createTestFloatVectorValues(floatIndexInput);
floatValues.prefetch(new int[] {1, 2, 5}, 2);
assertEquals(2, floatIndexInput.getPrefetchCount());
+
+ CountingIndexInput float16IndexInput = new CountingIndexInput();
+ OffHeapFloat16VectorValues float16Values = createTestFloat16VectorValues(float16IndexInput);
+ float16Values.prefetch(new int[] {1, 2, 5}, 2);
+ assertEquals(2, float16IndexInput.getPrefetchCount());
}
public void testPrefetchVectorValuesWithLessThanOneOrds() throws IOException {
@@ -51,6 +56,13 @@ public class TestOffHeapVectorValues extends LuceneTestCase {
assertEquals(0, floatIndexInput.getPrefetchCount());
floatValues.prefetch(new int[] {1}, 1);
assertEquals(0, floatIndexInput.getPrefetchCount());
+
+ CountingIndexInput float16IndexInput = new CountingIndexInput();
+ OffHeapFloat16VectorValues float16Values = createTestFloat16VectorValues(float16IndexInput);
+ float16Values.prefetch(null, 0);
+ assertEquals(0, float16IndexInput.getPrefetchCount());
+ float16Values.prefetch(new int[] {1}, 1);
+ assertEquals(0, float16IndexInput.getPrefetchCount());
}
public void testPrefetchBoundedByArrayLength() throws IOException {
@@ -65,6 +77,11 @@ public class TestOffHeapVectorValues extends LuceneTestCase {
OffHeapFloatVectorValues floatValues = createTestFloatVectorValues(floatIndexInput);
floatValues.prefetch(new int[] {1, 2}, 5);
assertEquals(2, floatIndexInput.getPrefetchCount());
+
+ CountingIndexInput float16IndexInput = new CountingIndexInput();
+ OffHeapFloat16VectorValues float16Values = createTestFloat16VectorValues(float16IndexInput);
+ float16Values.prefetch(new int[] {1, 2}, 5);
+ assertEquals(2, float16IndexInput.getPrefetchCount());
}
private OffHeapByteVectorValues createTestByteVectorValues(IndexInput indexInput)
@@ -79,6 +96,12 @@ public class TestOffHeapVectorValues extends LuceneTestCase {
100, 100, indexInput, 4, null, VectorSimilarityFunction.EUCLIDEAN);
}
+ private OffHeapFloat16VectorValues createTestFloat16VectorValues(IndexInput indexInput)
+ throws IOException {
+ return new OffHeapFloat16VectorValues.DenseOffHeapVectorValues(
+ 100, 100, indexInput, 2, null, VectorSimilarityFunction.EUCLIDEAN);
+ }
+
private static final class CountingIndexInput extends IndexInput {
AtomicInteger counter;
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestFullPrecisionFloat16VectorSimilarityValuesSource.java b/lucene/core/src/test/org/apache/lucene/search/TestFullPrecisionFloat16VectorSimilarityValuesSource.java
new file mode 100644
index 00000000000..d113c6ae993
--- /dev/null
+++ b/lucene/core/src/test/org/apache/lucene/search/TestFullPrecisionFloat16VectorSimilarityValuesSource.java
@@ -0,0 +1,211 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.search;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+import org.apache.lucene.codecs.Codec;
+import org.apache.lucene.codecs.KnnVectorsFormat;
+import org.apache.lucene.codecs.lucene104.Lucene104HnswScalarQuantizedVectorsFormat;
+import org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat;
+import org.apache.lucene.document.Document;
+import org.apache.lucene.document.Field;
+import org.apache.lucene.document.IntField;
+import org.apache.lucene.document.KnnFloat16VectorField;
+import org.apache.lucene.index.DirectoryReader;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.IndexWriter;
+import org.apache.lucene.index.LeafReaderContext;
+import org.apache.lucene.index.StoredFields;
+import org.apache.lucene.index.VectorSimilarityFunction;
+import org.apache.lucene.store.Directory;
+import org.apache.lucene.tests.util.LuceneTestCase;
+import org.apache.lucene.tests.util.TestUtil;
+import org.apache.lucene.util.TestVectorUtil;
+import org.apache.lucene.util.quantization.QuantizedByteVectorValues.ScalarEncoding;
+import org.junit.Before;
+
+public class TestFullPrecisionFloat16VectorSimilarityValuesSource extends LuceneTestCase {
+
+ private Codec savedCodec;
+
+ private static final String KNN_FIELD = "knnField";
+ private static final int NUM_VECTORS = 100;
+ private static final int VECTOR_DIMENSION = 8;
+
+ KnnVectorsFormat format;
+ int bits;
+
+ @Before
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ bits = random().nextBoolean() ? 4 : 8;
+ format = getKnnFormat(bits);
+ savedCodec = Codec.getDefault();
+ Codec.setDefault(getCodec());
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ Codec.setDefault(savedCodec); // restore
+ super.tearDown();
+ }
+
+ protected Codec getCodec() {
+ return TestUtil.alwaysKnnVectorsFormat(format);
+ }
+
+ private KnnVectorsFormat getKnnFormat(int bits) {
+ return new Lucene104HnswScalarQuantizedVectorsFormat(
+ ScalarEncoding.fromNumBits(bits),
+ Lucene99HnswVectorsFormat.DEFAULT_MAX_CONN,
+ Lucene99HnswVectorsFormat.DEFAULT_BEAM_WIDTH,
+ 1,
+ null);
+ }
+
+ // TODO: incredibly slow
+ @Nightly
+ public void testFullPrecisionVectorSimilarityDVS() throws Exception {
+ List<short[]> vectors = new ArrayList<>();
+ int numVectors = atLeast(NUM_VECTORS);
+ int numSegments = random().nextInt(2, 10);
+ final VectorSimilarityFunction indexingSimilarityFunction =
+ VectorSimilarityFunction.values()[
+ random().nextInt(VectorSimilarityFunction.values().length)];
+
+ try (Directory dir = newDirectory()) {
+ int id = 0;
+
+ // index some 4 bit quantized vectors
+ try (IndexWriter w =
+ new IndexWriter(
+ dir,
+ newIndexWriterConfig().setCodec(TestUtil.alwaysKnnVectorsFormat(getKnnFormat(4))))) {
+ for (int j = 0; j < numSegments; j++) {
+ for (int i = 0; i < numVectors; i++) {
+ Document doc = new Document();
+ if (random().nextInt(100) < 30) {
+ // skip vector for some docs to create sparse vector field
+ doc.add(new IntField("has_vector", 0, Field.Store.YES));
+ } else {
+ short[] vector = TestVectorUtil.randomVectorFloat16(VECTOR_DIMENSION);
+ vectors.add(vector);
+ doc.add(new IntField("id", id++, Field.Store.YES));
+ doc.add(new KnnFloat16VectorField(KNN_FIELD, vector, indexingSimilarityFunction));
+ doc.add(new IntField("has_vector", 1, Field.Store.YES));
+ }
+ w.addDocument(doc);
+ w.flush();
+ }
+ }
+ // add a segment with no vectors
+ for (int i = 0; i < 100; i++) {
+ Document doc = new Document();
+ doc.add(new IntField("has_vector", 0, Field.Store.YES));
+ w.addDocument(doc);
+ }
+ w.flush();
+ }
+
+ // index some 8 bit quantized vectors
+ try (IndexWriter w =
+ new IndexWriter(
+ dir,
+ newIndexWriterConfig().setCodec(TestUtil.alwaysKnnVectorsFormat(getKnnFormat(8))))) {
+ for (int j = 0; j < numSegments; j++) {
+ for (int i = 0; i < numVectors; i++) {
+ Document doc = new Document();
+ if (random().nextInt(100) < 30) {
+ // skip vector for some docs to create sparse vector field
+ doc.add(new IntField("has_vector", 0, Field.Store.YES));
+ } else {
+ short[] vector = TestVectorUtil.randomVectorFloat16(VECTOR_DIMENSION);
+ vectors.add(vector);
+ doc.add(new IntField("id", id++, Field.Store.YES));
+ doc.add(new KnnFloat16VectorField(KNN_FIELD, vector, indexingSimilarityFunction));
+ doc.add(new IntField("has_vector", 1, Field.Store.YES));
+ }
+ w.addDocument(doc);
+ w.flush();
+ }
+ }
+ // add a segment with no vectors
+ for (int i = 0; i < 100; i++) {
+ Document doc = new Document();
+ doc.add(new IntField("has_vector", 0, Field.Store.YES));
+ w.addDocument(doc);
+ }
+ w.flush();
+ }
+
+ short[] queryVector = TestVectorUtil.randomVectorFloat16(VECTOR_DIMENSION);
+ VectorSimilarityFunction rerankSimilarityFunction;
+ try (IndexReader reader = DirectoryReader.open(dir)) {
+ for (LeafReaderContext ctx : reader.leaves()) {
+ DoubleValues fpSimValues;
+ if (random().nextBoolean()) {
+ rerankSimilarityFunction =
+ VectorSimilarityFunction.values()[
+ random().nextInt(VectorSimilarityFunction.values().length)];
+ fpSimValues =
+ new FullPrecisionFloat16VectorSimilarityValuesSource(
+ queryVector, KNN_FIELD, rerankSimilarityFunction)
+ .getSimilarityScores(ctx);
+ } else {
+ fpSimValues =
+ new FullPrecisionFloat16VectorSimilarityValuesSource(queryVector, KNN_FIELD)
+ .getSimilarityScores(ctx);
+ rerankSimilarityFunction = indexingSimilarityFunction;
+ }
+ DoubleValues quantizedSimValues =
+ DoubleValuesSource.similarityToQueryVector(ctx, queryVector, KNN_FIELD);
+ // validate when segment has no vectors
+ if (fpSimValues == DoubleValues.EMPTY || quantizedSimValues == DoubleValues.EMPTY) {
+ assertEquals(fpSimValues, quantizedSimValues);
+ assertNull(ctx.reader().getFloat16VectorValues(KNN_FIELD));
+ continue;
+ }
+ StoredFields storedFields = ctx.reader().storedFields();
+ VectorScorer quantizedScorer =
+ ctx.reader().getFloat16VectorValues(KNN_FIELD).scorer(queryVector);
+ DocIdSetIterator disi = quantizedScorer.iterator();
+ while (disi.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
+ int doc = disi.docID();
+ fpSimValues.advanceExact(doc);
+ quantizedSimValues.advanceExact(doc);
+ int idValue =
+ Integer.parseInt(Objects.requireNonNull(storedFields.document(doc).get("id")));
+ short[] docVector = vectors.get(idValue);
+ assert docVector != null : "Vector for id " + idValue + " not found";
+ // validate full precision vector scores
+ double expectedFpScore = rerankSimilarityFunction.compare(queryVector, docVector);
+ double actualFpScore = fpSimValues.doubleValue();
+ assertEquals(expectedFpScore, actualFpScore, 1e-5);
+ // validate quantized vector scores
+ double expectedQScore = quantizedScorer.score();
+ double actualQScore = quantizedSimValues.doubleValue();
+ assertEquals(expectedQScore, actualQScore, 1e-5);
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestRescoreTopNQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestRescoreTopNQuery.java
index cddb1a76318..328b2c4d8d9 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestRescoreTopNQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestRescoreTopNQuery.java
@@ -29,6 +29,7 @@ import org.apache.lucene.codecs.lucene104.Lucene104HnswScalarQuantizedVectorsFor
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
+import org.apache.lucene.document.KnnFloat16VectorField;
import org.apache.lucene.document.KnnFloatVectorField;
import org.apache.lucene.document.LateInteractionField;
import org.apache.lucene.index.DirectoryReader;
@@ -161,6 +162,91 @@ public class TestRescoreTopNQuery extends LuceneTestCase {
}
}
+ // TODO: incredibly slow
+ @Nightly
+ public void testRescoreFieldFloat16() throws Exception {
+ Map<Integer, short[]> vectors = new HashMap<>();
+
+ Random random = random();
+
+ int numVectors = atLeast(NUM_VECTORS);
+ int numSegments = random.nextInt(2, 10);
+
+ // Step 1: Index random FLOAT16 vectors in quantized format
+ try (IndexWriter writer = new IndexWriter(directory, config)) {
+ for (int j = 0; j < numSegments; j++) {
+ for (int i = 0; i < numVectors; i++) {
+ short[] vector = randomFloat16Vector(VECTOR_DIMENSION, random);
+ Document doc = new Document();
+ int id = j * numVectors + i;
+ doc.add(new IntField("id", id, Field.Store.YES));
+ doc.add(new KnnFloat16VectorField(FIELD, vector, VECTOR_SIMILARITY_FUNCTION));
+ writer.addDocument(doc);
+ vectors.put(id, vector);
+ }
+ writer.flush();
+ }
+ }
+
+ // Step 2: Run an approximate FLOAT16 KNN query, then rescore with full-precision FLOAT16
+ // vectors
+ try (IndexReader reader = DirectoryReader.open(directory)) {
+ IndexSearcher searcher = new IndexSearcher(reader);
+ short[] targetVector = randomFloat16Vector(VECTOR_DIMENSION, random);
+ int k = 10;
+ double oversample = random.nextFloat(1.5f, 3.0f);
+
+ KnnFloat16VectorQuery knnQuery =
+ new KnnFloat16VectorQuery(FIELD, targetVector, k + (int) (k * oversample));
+
+ Query query =
+ RescoreTopNQuery.createFullPrecisionRescorerQuery(knnQuery, targetVector, FIELD, k);
+ TopDocs topDocs = searcher.search(query, k);
+
+ // Step 3: Verify that TopDocs scores match similarity with the raw FLOAT16 vectors
+ for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
+ Document retrievedDoc = searcher.storedFields().document(scoreDoc.doc);
+ int id = retrievedDoc.getField("id").numericValue().intValue();
+ short[] docVector = vectors.get(id);
+ assert docVector != null : "Vector for id " + id + " not found";
+ float expectedScore = VECTOR_SIMILARITY_FUNCTION.compare(targetVector, docVector);
+ Assert.assertEquals(
+ "Score does not match expected similarity for doc ord: " + scoreDoc.doc + ", id: " + id,
+ expectedScore,
+ scoreDoc.score,
+ 1e-5);
+ }
+ }
+ }
+
+ public void testMissingDoubleValuesFloat16() throws IOException {
+ Random random = random();
+
+ try (IndexWriter writer = new IndexWriter(directory, config)) {
+ short[] vector = randomFloat16Vector(VECTOR_DIMENSION, random);
+ Document doc = new Document();
+ doc.add(new KnnFloat16VectorField(FIELD, vector, VECTOR_SIMILARITY_FUNCTION));
+ writer.addDocument(doc);
+ }
+
+ try (IndexReader reader = DirectoryReader.open(directory)) {
+ IndexSearcher searcher = new IndexSearcher(reader);
+ short[] targetVector = randomFloat16Vector(VECTOR_DIMENSION, random);
+ int k = 1;
+
+ KnnFloat16VectorQuery knnQuery = new KnnFloat16VectorQuery(FIELD, targetVector, k);
+
+ Query query =
+ RescoreTopNQuery.createFullPrecisionRescorerQuery(knnQuery, targetVector, "field-1", k);
+ TopDocs topDocs = searcher.search(query, k);
+
+ // The rescoring field is invalid, so the score should be 0
+ for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
+ Assert.assertEquals("Score must be 0 for missing DoubleValues", 0, scoreDoc.score, 1e-5);
+ }
+ }
+ }
+
public void testLateInteractionQuery() throws Exception {
final String LATE_I_FIELD = "li_vector";
final String KNN_FIELD = "knn_vector";
@@ -249,4 +335,12 @@ public class TestRescoreTopNQuery extends LuceneTestCase {
}
return vector;
}
+
+ private short[] randomFloat16Vector(int dimension, Random random) {
+ short[] vector = new short[dimension];
+ for (int i = 0; i < dimension; i++) {
+ vector[i] = Float.floatToFloat16(random.nextFloat());
+ }
+ return vector;
+ }
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java b/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
index 519e05ce08c..2bcb4705a68 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
@@ -231,6 +231,15 @@ public class TestVectorUtil extends LuceneTestCase {
return v;
}
+ public static short[] randomVectorFloat16(int dim) {
+ short[] v = new short[dim];
+ Random random = random();
+ for (int i = 0; i < dim; i++) {
+ v[i] = Float.floatToFloat16(random.nextFloat());
+ }
+ return v;
+ }
+
private static byte[] randomVectorBytes() {
BytesRef v = TestUtil.randomBinaryTerm(random(), TestUtil.nextInt(random(), 1, 100));
// clip at -127 to avoid overflow