Skip to content

Commit bd220a5

Browse files
authored
Refactor VectorScorerFactoryTests to Int7SQVectorScorerFactoryTests.java (#130620)
This commit refactors VectorScorerFactoryTests to Int7SQVectorScorerFactoryTests, in order to make space for other vector scorer benchmarks, namely float32.
1 parent 72815c2 commit bd220a5

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

libs/simdvec/src/test/java/org/elasticsearch/simdvec/AbstractVectorTestCase.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
package org.elasticsearch.simdvec;
1111

12-
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
1312
import org.elasticsearch.test.ESTestCase;
1413
import org.junit.BeforeClass;
1514

@@ -62,17 +61,9 @@ public static String platformMsg() {
6261
return "JDK=" + jdkVersion + ", os=" + osName + ", arch=" + arch;
6362
}
6463

65-
/** Computes the score using the Lucene implementation. */
66-
public static float luceneScore(
67-
VectorSimilarityType similarityFunc,
68-
byte[] a,
69-
byte[] b,
70-
float correction,
71-
float aOffsetValue,
72-
float bOffsetValue
73-
) {
74-
var scorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityType.of(similarityFunc), correction, (byte) 7);
75-
return scorer.score(a, aOffsetValue, b, bOffsetValue);
64+
// Support for passing on-heap arrays/segments to native
65+
protected static boolean supportsHeapSegments() {
66+
return Runtime.version().feature() >= 22;
7667
}
7768

7869
/** Converts a float value to a byte array. */

libs/simdvec/src/test/java/org/elasticsearch/simdvec/VectorScorerFactoryTests.java renamed to libs/simdvec/src/test/java/org/elasticsearch/simdvec/Int7SQVectorScorerFactoryTests.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier;
2323
import org.apache.lucene.util.hnsw.UpdateableRandomVectorScorer;
2424
import org.apache.lucene.util.quantization.QuantizedByteVectorValues;
25+
import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity;
2526
import org.apache.lucene.util.quantization.ScalarQuantizer;
2627

2728
import java.io.IOException;
@@ -47,8 +48,7 @@
4748
import static org.hamcrest.Matchers.equalTo;
4849
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
4950

50-
// @com.carrotsearch.randomizedtesting.annotations.Repeat(iterations = 100)
51-
public class VectorScorerFactoryTests extends AbstractVectorTestCase {
51+
public class Int7SQVectorScorerFactoryTests extends AbstractVectorTestCase {
5252

5353
// bounds of the range of values that can be seen by int7 scalar quantized vectors
5454
static final byte MIN_INT7_VALUE = 0;
@@ -107,7 +107,7 @@ void testSimpleImpl(long maxChunkSize) throws IOException {
107107
scorer.setScoringOrdinal(0);
108108
assertThat(scorer.score(1), equalTo(expected));
109109

110-
if (Runtime.version().feature() >= 22) {
110+
if (supportsHeapSegments()) {
111111
var qScorer = factory.getInt7SQVectorScorer(VectorSimilarityType.of(sim), values, query1).get();
112112
assertThat(qScorer.score(1), equalTo(expected));
113113
}
@@ -229,11 +229,11 @@ void testRandomSupplier(long maxChunkSize, Function<Integer, byte[]> byteArraySu
229229
}
230230

231231
public void testRandomScorer() throws IOException {
232-
testRandomScorerImpl(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, VectorScorerFactoryTests.FLOAT_ARRAY_RANDOM_FUNC);
232+
testRandomScorerImpl(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, Int7SQVectorScorerFactoryTests.FLOAT_ARRAY_RANDOM_FUNC);
233233
}
234234

235235
public void testRandomScorerMax() throws IOException {
236-
testRandomScorerImpl(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, VectorScorerFactoryTests.FLOAT_ARRAY_MAX_FUNC);
236+
testRandomScorerImpl(MMapDirectory.DEFAULT_MAX_CHUNK_SIZE, Int7SQVectorScorerFactoryTests.FLOAT_ARRAY_MAX_FUNC);
237237
}
238238

239239
public void testRandomScorerChunkSizeSmall() throws IOException {
@@ -461,6 +461,19 @@ QuantizedByteVectorValues vectorValues(int dims, int size, IndexInput in, Vector
461461
return new OffHeapQuantizedByteVectorValues.DenseOffHeapVectorValues(dims, size, sq, false, sim, null, slice);
462462
}
463463

464+
/** Computes the score using the Lucene implementation. */
465+
public static float luceneScore(
466+
VectorSimilarityType similarityFunc,
467+
byte[] a,
468+
byte[] b,
469+
float correction,
470+
float aOffsetValue,
471+
float bOffsetValue
472+
) {
473+
var scorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityType.of(similarityFunc), correction, (byte) 7);
474+
return scorer.score(a, aOffsetValue, b, bOffsetValue);
475+
}
476+
464477
RandomVectorScorerSupplier luceneScoreSupplier(QuantizedByteVectorValues values, VectorSimilarityFunction sim) throws IOException {
465478
return new Lucene99ScalarQuantizedVectorScorer(null).getRandomVectorScorerSupplier(sim, values);
466479
}

0 commit comments

Comments
 (0)