Apache Lucene open-source search software
Two bit quantization for dense vectors (#15564)
Benjamin Trentccf5b595bd5f
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 9709560a6e4..73a09784b01 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -220,6 +220,11 @@ New Features`Lucene104HnswScalarQuantizedVectorsFormat(ScalarEncoding.SINGLE_BIT_QUERY_NIBBLE, int, int)`(Ben Trent)+ * GITHUB#15564: Further extend `Lucene104ScalarQuantizedVectorsFormat` and `Lucene104HnswScalarQuantizedVectorsFormat` to+ allow asymmetric quantization with 2 bits and 4 bit queries. This can provide increased recall over single bit+ with little additional storage cost and latency.+ (Ben Trent)+* GITHUB#15415: Add fallback support to Lucene104ScalarQuantizedVectorsFormat getFloatVectorValues when there areno full-precision vectors present (Pulkit Gupta)diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorScorer.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorScorer.javaindex 22ba079b1f3..8888ed7e5b6 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorScorer.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorScorer.java@@ -75,9 +75,12 @@ public class Lucene104ScalarQuantizedVectorScorer implements FlatVectorsScorer {var targetCorrectiveTerms =quantizer.scalarQuantize(target, scratch, scalarEncoding.getQueryBits(), qv.getCentroid());- // for single bit query nibble, we need to transpose the nibbles for fast scoring comparisons+ // for asymmetric encodings with 4-bit query, we need to transpose the nibbles for fast+ // scoring comparisonsif (scalarEncoding- == Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding.SINGLE_BIT_QUERY_NIBBLE) {+ == Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding.SINGLE_BIT_QUERY_NIBBLE+ || scalarEncoding+ == Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding.DIBIT_QUERY_NIBBLE) {OptimizedScalarQuantizer.transposeHalfByte(scratch, targetQuantized);}return new RandomVectorScorer.AbstractRandomVectorScorer(qv) {@@ -198,9 +201,10 @@ public class Lucene104ScalarQuantizedVectorScorer implements FlatVectorsScorer {}OffHeapScalarQuantizedVectorValues.unpackNibbles(rawTargetVector, targetVector);}- case SINGLE_BIT_QUERY_NIBBLE -> {+ case SINGLE_BIT_QUERY_NIBBLE, DIBIT_QUERY_NIBBLE -> {throw new IllegalStateException(- "SINGLE_BIT_QUERY_NIBBLE encoding is not supported for symmetric quantization");+ values.getScalarEncoding().name()+ + " encoding is not supported for symmetric quantization");}}targetCorrectiveTerms = targetValues.getCorrectiveTerms(node);@@ -242,6 +246,7 @@ public class Lucene104ScalarQuantizedVectorScorer implements FlatVectorsScorer {case PACKED_NIBBLE -> VectorUtil.int4DotProductSinglePacked(quantizedQuery, quantizedDoc);case SINGLE_BIT_QUERY_NIBBLE ->VectorUtil.int4BitDotProduct(quantizedQuery, quantizedDoc);+ case DIBIT_QUERY_NIBBLE -> VectorUtil.int4DibitDotProduct(quantizedQuery, quantizedDoc);};OptimizedScalarQuantizer.QuantizationResult indexCorrections =targetVectors.getCorrectiveTerms(targetOrd);diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsFormat.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsFormat.javaindex 8015751fa42..334ce565eb8 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsFormat.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsFormat.java@@ -136,7 +136,16 @@ public class Lucene104ScalarQuantizedVectorsFormat extends FlatVectorsFormat {* <p>This is the most space efficient encoding, and will produce an index 8x smaller than* {@link #UNSIGNED_BYTE}. However, this comes at the cost of accuracy.*/- SINGLE_BIT_QUERY_NIBBLE(3, (byte) 1, 1, (byte) 4, 4);+ SINGLE_BIT_QUERY_NIBBLE(3, (byte) 1, 1, (byte) 4, 4),+ /**+ * Each dimension is quantized to 2 bits (dibit) and packed into bytes. During query time, the+ * query vector is quantized to 4 bits per dimension.+ *+ * <p>This encoding produces an index 4x smaller than {@link #UNSIGNED_BYTE}, offering a balance+ * between the compression of {@link #SINGLE_BIT_QUERY_NIBBLE} and the accuracy of {@link+ * #PACKED_NIBBLE}.+ */+ DIBIT_QUERY_NIBBLE(4, (byte) 2, 2, (byte) 4, 4);public static ScalarEncoding fromNumBits(int bits) {for (ScalarEncoding encoding : values()) {diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.javaindex a579f588f4f..4206130d4c2 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/Lucene104ScalarQuantizedVectorsWriter.java@@ -22,6 +22,7 @@ import static org.apache.lucene.index.VectorSimilarityFunction.COSINE;import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;import static org.apache.lucene.util.RamUsageEstimator.shallowSizeOfInstance;import static org.apache.lucene.util.quantization.OptimizedScalarQuantizer.packAsBinary;+import static org.apache.lucene.util.quantization.OptimizedScalarQuantizer.transposeDibit;import static org.apache.lucene.util.quantization.OptimizedScalarQuantizer.transposeHalfByte;import java.io.Closeable;@@ -196,7 +197,7 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {byte[] vector =switch (encoding) {case UNSIGNED_BYTE, SEVEN_BIT -> scratch;- case PACKED_NIBBLE, SINGLE_BIT_QUERY_NIBBLE ->+ case PACKED_NIBBLE, SINGLE_BIT_QUERY_NIBBLE, DIBIT_QUERY_NIBBLE ->new byte[encoding.getDocPackedLength(scratch.length)];};for (int i = 0; i < fieldData.getVectors().size(); i++) {@@ -206,6 +207,7 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {switch (encoding) {case PACKED_NIBBLE -> OffHeapScalarQuantizedVectorValues.packNibbles(scratch, vector);case SINGLE_BIT_QUERY_NIBBLE -> OptimizedScalarQuantizer.packAsBinary(scratch, vector);+ case DIBIT_QUERY_NIBBLE -> OptimizedScalarQuantizer.transposeDibit(scratch, vector);case UNSIGNED_BYTE, SEVEN_BIT -> {}}vectorData.writeBytes(vector, vector.length);@@ -256,7 +258,7 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {byte[] vector =switch (encoding) {case UNSIGNED_BYTE, SEVEN_BIT -> scratch;- case PACKED_NIBBLE, SINGLE_BIT_QUERY_NIBBLE ->+ case PACKED_NIBBLE, SINGLE_BIT_QUERY_NIBBLE, DIBIT_QUERY_NIBBLE ->new byte[encoding.getDocPackedLength(scratch.length)];};for (int ordinal : ordMap) {@@ -266,6 +268,7 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {switch (encoding) {case PACKED_NIBBLE -> OffHeapScalarQuantizedVectorValues.packNibbles(scratch, vector);case SINGLE_BIT_QUERY_NIBBLE -> OptimizedScalarQuantizer.packAsBinary(scratch, vector);+ case DIBIT_QUERY_NIBBLE -> OptimizedScalarQuantizer.transposeDibit(scratch, vector);case UNSIGNED_BYTE, SEVEN_BIT -> {}}vectorData.writeBytes(vector, vector.length);@@ -412,8 +415,13 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {quantizationScratch,new byte[] {encoding.getBits(), encoding.getQueryBits()},centroid);- // pack and store document bit vector- packAsBinary(quantizationScratch[0], toIndex);+ // pack and store document vector based on encoding type+ switch (encoding) {+ case SINGLE_BIT_QUERY_NIBBLE -> packAsBinary(quantizationScratch[0], toIndex);+ case DIBIT_QUERY_NIBBLE -> transposeDibit(quantizationScratch[0], toIndex);+ case PACKED_NIBBLE, UNSIGNED_BYTE, SEVEN_BIT ->+ throw new IllegalArgumentException("Unsupported asymmetric encoding: " + encoding);+ }binarizedVectorData.writeBytes(toIndex, toIndex.length);binarizedVectorData.writeInt(Float.floatToIntBits(r[0].lowerInterval()));binarizedVectorData.writeInt(Float.floatToIntBits(r[0].upperInterval()));@@ -798,7 +806,7 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {this.packed =switch (encoding) {case UNSIGNED_BYTE, SEVEN_BIT -> this.quantized;- case PACKED_NIBBLE, SINGLE_BIT_QUERY_NIBBLE ->+ case PACKED_NIBBLE, SINGLE_BIT_QUERY_NIBBLE, DIBIT_QUERY_NIBBLE ->new byte[encoding.getDocPackedLength(quantized.length)];};this.centroid = centroid;@@ -873,6 +881,7 @@ public class Lucene104ScalarQuantizedVectorsWriter extends FlatVectorsWriter {switch (encoding) {case PACKED_NIBBLE -> OffHeapScalarQuantizedVectorValues.packNibbles(quantized, packed);case SINGLE_BIT_QUERY_NIBBLE -> OptimizedScalarQuantizer.packAsBinary(quantized, packed);+ case DIBIT_QUERY_NIBBLE -> OptimizedScalarQuantizer.transposeDibit(quantized, packed);case UNSIGNED_BYTE, SEVEN_BIT -> {}}}diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.javaindex e429079b6c0..ec3dbbea087 100644--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.java+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.java@@ -123,6 +123,8 @@ abstract class OffHeapScalarQuantizedFloatVectorValues extends FloatVectorValuesOffHeapScalarQuantizedVectorValues.unpackNibbles(byteValue, unpackedByteVectorValue);case SINGLE_BIT_QUERY_NIBBLE ->OptimizedScalarQuantizer.unpackBinary(byteValue, unpackedByteVectorValue);+ case DIBIT_QUERY_NIBBLE ->+ OptimizedScalarQuantizer.untransposeDibit(byteValue, unpackedByteVectorValue);case UNSIGNED_BYTE, SEVEN_BIT -> {deQuantize(byteValue,diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorUtilSupport.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorUtilSupport.javaindex 3ec646288cd..51d2ab5fbc6 100644--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorUtilSupport.java+++ b/lucene/core/src/java/org/apache/lucene/internal/vectorization/DefaultVectorUtilSupport.java@@ -285,19 +285,54 @@ final class DefaultVectorUtilSupport implements VectorUtilSupport {public static long int4BitDotProductImpl(byte[] q, byte[] d) {assert q.length == d.length * 4;+ return int4BitDotProductImpl(q, d, 0, d.length);+ }++ @Override+ public long int4DibitDotProduct(byte[] int4Quantized, byte[] dibitQuantized) {+ return int4DibitDotProductImpl(int4Quantized, dibitQuantized);+ }++ /**+ * Computes the dot product between a transposed 4-bit query vector and a transposed 2-bit+ * document vector. The dibit vector has 2 stripes (lower bits first, then upper bits), so the+ * scoring is two passes of int4-bit dot product with results shifted appropriately.+ *+ * @param q transposed 4-bit query vector (4 stripes, each of size q.length/4)+ * @param d transposed 2-bit document vector (2 stripes, each of size d.length/2)+ * @return the dot product+ */+ public static long int4DibitDotProductImpl(byte[] q, byte[] d) {+ assert q.length == d.length * 2;+ int stripeSize = d.length / 2;+ long ret0 = int4BitDotProductImpl(q, d, 0, stripeSize);+ long ret1 = int4BitDotProductImpl(q, d, stripeSize, stripeSize);++ return ret0 + (ret1 << 1);+ }++ /**+ * Helper method to compute int4-bit dot product with a specific stripe of the document vector.+ *+ * @param q transposed 4-bit query vector (4 stripes)+ * @param d transposed document vector+ * @param dOffset offset into d for the stripe to use+ * @param stripeSize size of each stripe+ * @return the dot product for this stripe+ */+ private static long int4BitDotProductImpl(byte[] q, byte[] d, int dOffset, int stripeSize) {long ret = 0;- int size = d.length;for (int i = 0; i < 4; i++) {int r = 0;long subRet = 0;- for (final int upperBound = d.length & -Integer.BYTES; r < upperBound; r += Integer.BYTES) {+ for (final int upperBound = stripeSize & -Integer.BYTES; r < upperBound; r += Integer.BYTES) {subRet +=Integer.bitCount(- (int) BitUtil.VH_NATIVE_INT.get(q, i * size + r)- & (int) BitUtil.VH_NATIVE_INT.get(d, r));+ (int) BitUtil.VH_NATIVE_INT.get(q, i * stripeSize + r)+ & (int) BitUtil.VH_NATIVE_INT.get(d, dOffset + r));}- for (; r < d.length; r++) {- subRet += Integer.bitCount((q[i * size + r] & d[r]) & 0xFF);+ for (; r < stripeSize; r++) {+ subRet += Integer.bitCount((q[i * stripeSize + r] & d[dOffset + r]) & 0xFF);}ret += subRet << i;}diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorUtilSupport.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorUtilSupport.javaindex f92a0b653ca..66a16469a5d 100644--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorUtilSupport.java+++ b/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorUtilSupport.java@@ -94,6 +94,19 @@ public interface VectorUtilSupport {*/long int4BitDotProduct(byte[] int4Quantized, byte[] binaryQuantized);+ /**+ * Compute the dot product between a quantized int4 vector and a dibit (2-bit) quantized vector.+ * It is assumed that the int4 quantized bits are packed in the byte array in the same way as the+ * {@link org.apache.lucene.util.quantization.OptimizedScalarQuantizer#transposeHalfByte(byte[],+ * byte[])} and that the dibit bits are packed the same way as {@link+ * org.apache.lucene.util.quantization.OptimizedScalarQuantizer#transposeDibit(byte[], byte[])}.+ *+ * @param int4Quantized half byte packed int4 quantized vector (4 stripes)+ * @param dibitQuantized dibit packed quantized vector (2 stripes)+ * @return the dot product+ */+ long int4DibitDotProduct(byte[] int4Quantized, byte[] dibitQuantized);+/*** Quantizes {@code vector}, putting the result into {@code dest}.*diff --git a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.javaindex a410d270753..3428899bb25 100644--- a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java+++ b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java@@ -295,6 +295,25 @@ public final class VectorUtil {return IMPL.int4BitDotProduct(q, d);}+ /**+ * Dot product computed over int4 (values between [0,15]) bytes and a dibit (2-bit) vector.+ *+ * <p>The int4 query vector is expected to be transposed (4 stripes, each containing one bit plane+ * of all dimensions). The dibit document vector is expected to be transposed (2 stripes: lower+ * bits first, then upper bits).+ *+ * @param q the int4 query vector (transposed, 4 stripes)+ * @param d the dibit document vector (transposed, 2 stripes)+ * @return the dot product+ */+ public static long int4DibitDotProduct(byte[] q, byte[] d) {+ if (q.length != d.length * 2) {+ throw new IllegalArgumentException(+ "vector dimensions incompatible: " + q.length + "!= " + 2 + " x " + d.length);+ }+ return IMPL.int4DibitDotProduct(q, d);+ }+/*** For xorBitCount we stride over the values as either 64-bits (long) or 32-bits (int) at a time.* On ARM Long::bitCount is not vectorized, and therefore produces less than optimal code, whendiff --git a/lucene/core/src/java/org/apache/lucene/util/quantization/OptimizedScalarQuantizer.java b/lucene/core/src/java/org/apache/lucene/util/quantization/OptimizedScalarQuantizer.javaindex 9f937043a13..23e2e7ab19f 100644--- a/lucene/core/src/java/org/apache/lucene/util/quantization/OptimizedScalarQuantizer.java+++ b/lucene/core/src/java/org/apache/lucene/util/quantization/OptimizedScalarQuantizer.java@@ -440,6 +440,86 @@ public class OptimizedScalarQuantizer {}}+ /**+ * Transpose a 2-bit (dibit) quantized vector into a byte array for efficient bitwise operations.+ * The result has 2 stripes: similar to {@link #transposeHalfByte}, but only for 2 bits+ *+ * @param vector the 2-bit quantized vector (values 0-3)+ * @param packed the byte array to store the transposed vector+ */+ public static void transposeDibit(byte[] vector, byte[] packed) {+ int limit = vector.length - 7;+ int i = 0;+ int index = 0;+ for (; i < limit; i += 8, index++) {+ int lowerByte =+ (vector[i] & 1) << 7+ | (vector[i + 1] & 1) << 6+ | (vector[i + 2] & 1) << 5+ | (vector[i + 3] & 1) << 4+ | (vector[i + 4] & 1) << 3+ | (vector[i + 5] & 1) << 2+ | (vector[i + 6] & 1) << 1+ | (vector[i + 7] & 1);+ int upperByte =+ ((vector[i] >> 1) & 1) << 7+ | ((vector[i + 1] >> 1) & 1) << 6+ | ((vector[i + 2] >> 1) & 1) << 5+ | ((vector[i + 3] >> 1) & 1) << 4+ | ((vector[i + 4] >> 1) & 1) << 3+ | ((vector[i + 5] >> 1) & 1) << 2+ | ((vector[i + 6] >> 1) & 1) << 1+ | ((vector[i + 7] >> 1) & 1);+ packed[index] = (byte) lowerByte;+ packed[index + packed.length / 2] = (byte) upperByte;+ }+ if (i == vector.length) {+ return;+ }+ int lowerByte = 0;+ int upperByte = 0;+ for (int j = 7; i < vector.length; j--, i++) {+ assert vector[i] >= 0 && vector[i] <= 3;+ lowerByte |= (vector[i] & 1) << j;+ upperByte |= ((vector[i] >> 1) & 1) << j;+ }+ packed[index] = (byte) lowerByte;+ packed[index + packed.length / 2] = (byte) upperByte;+ }++ /**+ * Untranspose a packed 2-bit (dibit) vector back to its original form. This is the reverse of+ * {@link #transposeDibit}.+ *+ * @param packed the packed/transposed byte array+ * @param vector the output vector where each byte will contain a 2-bit value (0-3)+ */+ public static void untransposeDibit(byte[] packed, byte[] vector) {+ int stripeSize = packed.length / 2;+ int limit = vector.length - 7;+ int i = 0;+ int index = 0;+ for (; i < limit; i += 8, index++) {+ byte lowerByte = packed[index];+ byte upperByte = packed[index + stripeSize];+ vector[i] = (byte) (((lowerByte >> 7) & 1) | (((upperByte >> 7) & 1) << 1));+ vector[i + 1] = (byte) (((lowerByte >> 6) & 1) | (((upperByte >> 6) & 1) << 1));+ vector[i + 2] = (byte) (((lowerByte >> 5) & 1) | (((upperByte >> 5) & 1) << 1));+ vector[i + 3] = (byte) (((lowerByte >> 4) & 1) | (((upperByte >> 4) & 1) << 1));+ vector[i + 4] = (byte) (((lowerByte >> 3) & 1) | (((upperByte >> 3) & 1) << 1));+ vector[i + 5] = (byte) (((lowerByte >> 2) & 1) | (((upperByte >> 2) & 1) << 1));+ vector[i + 6] = (byte) (((lowerByte >> 1) & 1) | (((upperByte >> 1) & 1) << 1));+ vector[i + 7] = (byte) ((lowerByte & 1) | ((upperByte & 1) << 1));+ }+ if (i < vector.length) {+ byte lowerByte = packed[index];+ byte upperByte = packed[index + stripeSize];+ for (int j = 7; i < vector.length; j--, i++) {+ vector[i] = (byte) (((lowerByte >> j) & 1) | (((upperByte >> j) & 1) << 1));+ }+ }+ }+private static double clamp(double x, double a, double b) {return Math.min(Math.max(x, a), b);}diff --git a/lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java b/lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.javaindex 390dc97e8f8..482ea4338b6 100644--- a/lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java+++ b/lucene/core/src/java25/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java@@ -1133,24 +1133,63 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {}static long int4BitDotProduct256(byte[] q, byte[] d) {+ return int4BitDotProduct256WithOffset(q, d, 0, d.length);+ }++ public static long int4BitDotProduct128(byte[] q, byte[] d) {+ return int4BitDotProduct128WithOffset(q, d, 0, d.length);+ }++ @Override+ public long int4DibitDotProduct(byte[] q, byte[] d) {+ assert q.length == d.length * 2;+ int stripeSize = d.length / 2;+ if (stripeSize >= 16) {+ if (VECTOR_BITSIZE >= 256) {+ return int4DibitDotProduct256(q, d);+ } else if (VECTOR_BITSIZE == 128) {+ return int4DibitDotProduct128(q, d);+ }+ }+ return DefaultVectorUtilSupport.int4DibitDotProductImpl(q, d);+ }++ static long int4DibitDotProduct256(byte[] q, byte[] d) {+ int stripeSize = d.length / 2;+ long ret0 = int4BitDotProduct256WithOffset(q, d, 0, stripeSize);+ long ret1 = int4BitDotProduct256WithOffset(q, d, stripeSize, stripeSize);+ return ret0 + (ret1 << 1);+ }++ static long int4DibitDotProduct128(byte[] q, byte[] d) {+ int stripeSize = d.length / 2;+ long ret0 = int4BitDotProduct128WithOffset(q, d, 0, stripeSize);+ long ret1 = int4BitDotProduct128WithOffset(q, d, stripeSize, stripeSize);+ return ret0 + (ret1 << 1);+ }++ private static long int4BitDotProduct256WithOffset(+ byte[] q, byte[] d, int dOffset, int stripeSize) {long subRet0 = 0;long subRet1 = 0;long subRet2 = 0;long subRet3 = 0;int i = 0;- if (d.length >= ByteVector.SPECIES_256.vectorByteSize() * 2) {- int limit = ByteVector.SPECIES_256.loopBound(d.length);+ if (stripeSize >= ByteVector.SPECIES_256.vectorByteSize() * 2) {+ int limit = ByteVector.SPECIES_256.loopBound(stripeSize);var sum0 = LongVector.zero(LongVector.SPECIES_256);var sum1 = LongVector.zero(LongVector.SPECIES_256);var sum2 = LongVector.zero(LongVector.SPECIES_256);var sum3 = LongVector.zero(LongVector.SPECIES_256);for (; i < limit; i += ByteVector.SPECIES_256.length()) {var vq0 = ByteVector.fromArray(BYTE_SPECIES_256, q, i).reinterpretAsLongs();- var vq1 = ByteVector.fromArray(BYTE_SPECIES_256, q, i + d.length).reinterpretAsLongs();- var vq2 = ByteVector.fromArray(BYTE_SPECIES_256, q, i + d.length * 2).reinterpretAsLongs();- var vq3 = ByteVector.fromArray(BYTE_SPECIES_256, q, i + d.length * 3).reinterpretAsLongs();- var vd = ByteVector.fromArray(BYTE_SPECIES_256, d, i).reinterpretAsLongs();+ var vq1 = ByteVector.fromArray(BYTE_SPECIES_256, q, i + stripeSize).reinterpretAsLongs();+ var vq2 =+ ByteVector.fromArray(BYTE_SPECIES_256, q, i + stripeSize * 2).reinterpretAsLongs();+ var vq3 =+ ByteVector.fromArray(BYTE_SPECIES_256, q, i + stripeSize * 3).reinterpretAsLongs();+ var vd = ByteVector.fromArray(BYTE_SPECIES_256, d, dOffset + i).reinterpretAsLongs();sum0 = sum0.add(vq0.and(vd).lanewise(VectorOperators.BIT_COUNT));sum1 = sum1.add(vq1.and(vd).lanewise(VectorOperators.BIT_COUNT));sum2 = sum2.add(vq2.and(vd).lanewise(VectorOperators.BIT_COUNT));@@ -1162,18 +1201,20 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {subRet3 += sum3.reduceLanes(VectorOperators.ADD);}- if (d.length - i >= ByteVector.SPECIES_128.vectorByteSize()) {+ if (stripeSize - i >= ByteVector.SPECIES_128.vectorByteSize()) {var sum0 = LongVector.zero(LongVector.SPECIES_128);var sum1 = LongVector.zero(LongVector.SPECIES_128);var sum2 = LongVector.zero(LongVector.SPECIES_128);var sum3 = LongVector.zero(LongVector.SPECIES_128);- int limit = ByteVector.SPECIES_128.loopBound(d.length);+ int limit = ByteVector.SPECIES_128.loopBound(stripeSize);for (; i < limit; i += ByteVector.SPECIES_128.length()) {var vq0 = ByteVector.fromArray(BYTE_SPECIES_128, q, i).reinterpretAsLongs();- var vq1 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + d.length).reinterpretAsLongs();- var vq2 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + d.length * 2).reinterpretAsLongs();- var vq3 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + d.length * 3).reinterpretAsLongs();- var vd = ByteVector.fromArray(BYTE_SPECIES_128, d, i).reinterpretAsLongs();+ var vq1 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + stripeSize).reinterpretAsLongs();+ var vq2 =+ ByteVector.fromArray(BYTE_SPECIES_128, q, i + stripeSize * 2).reinterpretAsLongs();+ var vq3 =+ ByteVector.fromArray(BYTE_SPECIES_128, q, i + stripeSize * 3).reinterpretAsLongs();+ var vd = ByteVector.fromArray(BYTE_SPECIES_128, d, dOffset + i).reinterpretAsLongs();sum0 = sum0.add(vq0.and(vd).lanewise(VectorOperators.BIT_COUNT));sum1 = sum1.add(vq1.and(vd).lanewise(VectorOperators.BIT_COUNT));sum2 = sum2.add(vq2.and(vd).lanewise(VectorOperators.BIT_COUNT));@@ -1185,16 +1226,17 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {subRet3 += sum3.reduceLanes(VectorOperators.ADD);}// tail as bytes- for (; i < d.length; i++) {- subRet0 += Integer.bitCount((q[i] & d[i]) & 0xFF);- subRet1 += Integer.bitCount((q[i + d.length] & d[i]) & 0xFF);- subRet2 += Integer.bitCount((q[i + 2 * d.length] & d[i]) & 0xFF);- subRet3 += Integer.bitCount((q[i + 3 * d.length] & d[i]) & 0xFF);+ for (; i < stripeSize; i++) {+ subRet0 += Integer.bitCount((q[i] & d[dOffset + i]) & 0xFF);+ subRet1 += Integer.bitCount((q[i + stripeSize] & d[dOffset + i]) & 0xFF);+ subRet2 += Integer.bitCount((q[i + 2 * stripeSize] & d[dOffset + i]) & 0xFF);+ subRet3 += Integer.bitCount((q[i + 3 * stripeSize] & d[dOffset + i]) & 0xFF);}return subRet0 + (subRet1 << 1) + (subRet2 << 2) + (subRet3 << 3);}- public static long int4BitDotProduct128(byte[] q, byte[] d) {+ private static long int4BitDotProduct128WithOffset(+ byte[] q, byte[] d, int dOffset, int stripeSize) {long subRet0 = 0;long subRet1 = 0;long subRet2 = 0;@@ -1205,13 +1247,13 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {var sum1 = IntVector.zero(IntVector.SPECIES_128);var sum2 = IntVector.zero(IntVector.SPECIES_128);var sum3 = IntVector.zero(IntVector.SPECIES_128);- int limit = ByteVector.SPECIES_128.loopBound(d.length);+ int limit = ByteVector.SPECIES_128.loopBound(stripeSize);for (; i < limit; i += ByteVector.SPECIES_128.length()) {- var vd = ByteVector.fromArray(BYTE_SPECIES_128, d, i).reinterpretAsInts();+ var vd = ByteVector.fromArray(BYTE_SPECIES_128, d, dOffset + i).reinterpretAsInts();var vq0 = ByteVector.fromArray(BYTE_SPECIES_128, q, i).reinterpretAsInts();- var vq1 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + d.length).reinterpretAsInts();- var vq2 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + d.length * 2).reinterpretAsInts();- var vq3 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + d.length * 3).reinterpretAsInts();+ var vq1 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + stripeSize).reinterpretAsInts();+ var vq2 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + stripeSize * 2).reinterpretAsInts();+ var vq3 = ByteVector.fromArray(BYTE_SPECIES_128, q, i + stripeSize * 3).reinterpretAsInts();sum0 = sum0.add(vd.and(vq0).lanewise(VectorOperators.BIT_COUNT));sum1 = sum1.add(vd.and(vq1).lanewise(VectorOperators.BIT_COUNT));sum2 = sum2.add(vd.and(vq2).lanewise(VectorOperators.BIT_COUNT));@@ -1222,12 +1264,12 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {subRet2 += sum2.reduceLanes(VectorOperators.ADD);subRet3 += sum3.reduceLanes(VectorOperators.ADD);// tail as bytes- for (; i < d.length; i++) {- int dValue = d[i];+ for (; i < stripeSize; i++) {+ int dValue = d[dOffset + i];subRet0 += Integer.bitCount((dValue & q[i]) & 0xFF);- subRet1 += Integer.bitCount((dValue & q[i + d.length]) & 0xFF);- subRet2 += Integer.bitCount((dValue & q[i + 2 * d.length]) & 0xFF);- subRet3 += Integer.bitCount((dValue & q[i + 3 * d.length]) & 0xFF);+ subRet1 += Integer.bitCount((dValue & q[i + stripeSize]) & 0xFF);+ subRet2 += Integer.bitCount((dValue & q[i + 2 * stripeSize]) & 0xFF);+ subRet3 += Integer.bitCount((dValue & q[i + 3 * stripeSize]) & 0xFF);}return subRet0 + (subRet1 << 1) + (subRet2 << 2) + (subRet3 << 3);}diff --git a/lucene/core/src/test/org/apache/lucene/codecs/lucene104/TestLucene104ScalarQuantizedVectorsFormat.java b/lucene/core/src/test/org/apache/lucene/codecs/lucene104/TestLucene104ScalarQuantizedVectorsFormat.javaindex 43755f8f220..08a96b54bae 100644--- a/lucene/core/src/test/org/apache/lucene/codecs/lucene104/TestLucene104ScalarQuantizedVectorsFormat.java+++ b/lucene/core/src/test/org/apache/lucene/codecs/lucene104/TestLucene104ScalarQuantizedVectorsFormat.java@@ -194,6 +194,8 @@ public class TestLucene104ScalarQuantizedVectorsFormat extends BaseKnnVectorsForOffHeapScalarQuantizedVectorValues.packNibbles(scratch, expectedVector);case SINGLE_BIT_QUERY_NIBBLE ->OptimizedScalarQuantizer.packAsBinary(scratch, expectedVector);+ case DIBIT_QUERY_NIBBLE ->+ OptimizedScalarQuantizer.transposeDibit(scratch, expectedVector);}assertArrayEquals(expectedVector, qvectorValues.vectorValue(docIndexIterator.index()));var actualCorrections = qvectorValues.getCorrectiveTerms(docIndexIterator.index());