Skip to content

Apache Lucene open-source search software

Vectorize the uint4 nibble unpack via VectorUtilSupport (#16631)

Jakub Slowinski8e3801b7655a
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 9da6884f163..a717eab2e77 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -489,6 +489,9 @@ Optimizations
* GITHUB#16297: Use LongHashSet for sparse ordinal sets in DocValuesRangeIterator so memory
scales with matching terms instead of segment cardinality. (Costin Leau)
+* GITHUB#16631: Vectorize the uint4 nibble unpack used by Lucene104ScalarQuantizedVectorsFormat
+ via VectorUtilSupport, vectors are now unpacked with the Panama Vector API. (Jakub Slowinski)
+
Bug Fixes
---------------------
diff --git a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilBenchmark.java b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilBenchmark.java
index fc26a06cfcd..00c12d548e0 100644
--- a/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilBenchmark.java
+++ b/lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/VectorUtilBenchmark.java
@@ -57,6 +57,7 @@ public class VectorUtilBenchmark {
private byte[] halfBytesAPacked;
private byte[] halfBytesB;
private byte[] halfBytesBPacked;
+ private byte[] halfBytesUnpackDest;
private byte[] int4QuantizedBit;
private byte[] binaryQuantized;
private byte[] int4QuantizedDibit;
@@ -101,6 +102,9 @@ public class VectorUtilBenchmark {
halfBytesBPacked = new byte[(size + 1) >> 1];
compressBytes(halfBytesB, halfBytesBPacked);
+
+ // destination for the unpack benchmarks
+ halfBytesUnpackDest = new byte[halfBytesAPacked.length * 2];
}
// random float arrays for float methods
@@ -287,6 +291,19 @@ public class VectorUtilBenchmark {
return VectorUtil.int4DibitDotProduct(int4QuantizedDibit, dibitQuantized);
}
+ @Benchmark
+ public byte[] binaryHalfByteUnpackScalar() {
+ VectorUtil.int4Unpack(halfBytesAPacked, halfBytesUnpackDest);
+ return halfBytesUnpackDest;
+ }
+
+ @Benchmark
+ @Fork(jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
+ public byte[] binaryHalfByteUnpackVector() {
+ VectorUtil.int4Unpack(halfBytesAPacked, halfBytesUnpackDest);
+ return halfBytesUnpackDest;
+ }
+
@Benchmark
public int binaryHalfByteDotProductSinglePackedScalar() {
int v = VectorUtil.int4DotProductSinglePacked(halfBytesA, halfBytesBPacked);
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedVectorValues.java b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedVectorValues.java
index 4ab62c49cf6..011a62c1e07 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedVectorValues.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedVectorValues.java
@@ -26,6 +26,7 @@ import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.VectorScorer;
import org.apache.lucene.store.IndexInput;
import org.apache.lucene.util.Bits;
+import org.apache.lucene.util.VectorUtil;
import org.apache.lucene.util.hnsw.RandomVectorScorer;
import org.apache.lucene.util.packed.DirectMonotonicReader;
import org.apache.lucene.util.quantization.OptimizedScalarQuantizer;
@@ -188,11 +189,7 @@ public abstract class OffHeapScalarQuantizedVectorValues extends QuantizedByteVe
/** Unpacks two 4-bit values per input byte, the reverse of {@link #packNibbles}. */
public static void unpackNibbles(byte[] packed, byte[] unpacked) {
- assert unpacked.length == packed.length * 2;
- for (int i = 0; i < packed.length; i++) {
- unpacked[i] = (byte) ((packed[i] >> 4) & 0x0F);
- unpacked[packed.length + i] = (byte) (packed[i] & 0x0F);
- }
+ VectorUtil.int4Unpack(packed, unpacked);
}
static OffHeapScalarQuantizedVectorValues load(
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.java
index cc11918baf0..252499a266e 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
@@ -218,6 +218,14 @@ final class DefaultVectorUtilSupport implements VectorUtilSupport {
return total;
}
+ @Override
+ public void int4Unpack(byte[] packed, byte[] unpacked) {
+ for (int i = 0; i < packed.length; i++) {
+ unpacked[i] = (byte) ((packed[i] >> 4) & 0x0F);
+ unpacked[packed.length + i] = (byte) (packed[i] & 0x0F);
+ }
+ }
+
@Override
public int int4DotProductBothPacked(byte[] a, byte[] b) {
int total = 0;
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.java
index 5569d77a337..6a74a26d054 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
@@ -56,6 +56,13 @@ public interface VectorUtilSupport {
/** Returns the dot product computed over unsigned half-bytes, both compressed. */
int int4DotProductBothPacked(byte[] a, byte[] b);
+ /**
+ * Unpacks a uint4 "packed" vector (two values per byte) into one value per byte: the high nibble
+ * of {@code packed[i]} lands at {@code unpacked[i]}, the low nibble at {@code
+ * unpacked[packed.length + i]}.
+ */
+ void int4Unpack(byte[] packed, byte[] unpacked);
+
/** Returns the dot product computed as though the bytes were unsigned. */
int uint8DotProduct(byte[] a, byte[] b);
diff --git a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
index 641cc420bf6..614d8aa8239 100644
--- a/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/util/VectorUtil.java
@@ -306,6 +306,18 @@ public final class VectorUtil {
return IMPL.int4DotProductSinglePacked(unpacked, packed);
}
+ /**
+ * Unpacks uint4 nibbles: the high nibble of {@code packed[i]} goes to {@code unpacked[i]}, the
+ * low nibble to {@code unpacked[packed.length + i]}.
+ */
+ public static void int4Unpack(byte[] packed, byte[] unpacked) {
+ if (unpacked.length != packed.length * 2) {
+ throw new IllegalArgumentException(
+ "vector dimensions differ: " + unpacked.length + " != 2 * " + packed.length);
+ }
+ IMPL.int4Unpack(packed, unpacked);
+ }
+
/**
* Dot product computed over uint4 (values between [0,15]) bytes. Both vectors are considered
* "packed" (i.e. every byte representing two values).
diff --git a/lucene/core/src/java25/org/apache/lucene/internal/vectorization/NativeVectorUtilSupport.java b/lucene/core/src/java25/org/apache/lucene/internal/vectorization/NativeVectorUtilSupport.java
index e35b472f71d..bad26257cbc 100644
--- a/lucene/core/src/java25/org/apache/lucene/internal/vectorization/NativeVectorUtilSupport.java
+++ b/lucene/core/src/java25/org/apache/lucene/internal/vectorization/NativeVectorUtilSupport.java
@@ -432,6 +432,11 @@ final class NativeVectorUtilSupport implements VectorUtilSupport {
: delegateVectorUtilSupport.int4DotProductBothPacked(a, b);
}
+ @Override
+ public void int4Unpack(byte[] packed, byte[] unpacked) {
+ delegateVectorUtilSupport.int4Unpack(packed, unpacked);
+ }
+
@Override
public int uint8DotProduct(byte[] a, byte[] b) {
return (uint8DotProduct$MH != null)
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.java
index 4ef2dca2512..92711e22fc4 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
@@ -72,6 +72,10 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {
private static final VectorSpecies<Short> SHORT_SPECIES;
private static final VectorSpecies<Byte> BYTE_SPECIES_128 = ByteVector.SPECIES_128;
private static final VectorSpecies<Byte> BYTE_SPECIES_256 = ByteVector.SPECIES_256;
+ // full register needed, no widening
+ private static final VectorSpecies<Byte> BYTE_SPECIES_FULL =
+ ByteVector.SPECIES_MAX.withShape(
+ VectorShape.forBitSize(PanamaVectorConstants.PREFERRED_VECTOR_BITSIZE));
static final int VECTOR_BITSIZE;
@@ -584,6 +588,24 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {
return sum;
}
+ @Override
+ public void int4Unpack(byte[] packed, byte[] unpacked) {
+ final int len = packed.length;
+ final int bound = BYTE_SPECIES_FULL.loopBound(len);
+ int i = 0;
+ for (; i < bound; i += BYTE_SPECIES_FULL.length()) {
+ ByteVector v = ByteVector.fromArray(BYTE_SPECIES_FULL, packed, i);
+ // LSHR is a logical shift within the byte lane, so the high nibble needs no mask.
+ v.lanewise(LSHR, 4).intoArray(unpacked, i);
+ v.lanewise(VectorOperators.AND, (byte) 0x0F).intoArray(unpacked, len + i);
+ }
+ // scalar tail
+ for (; i < len; i++) {
+ unpacked[i] = (byte) ((packed[i] >> 4) & 0x0F);
+ unpacked[len + i] = (byte) (packed[i] & 0x0F);
+ }
+ }
+
@Override
public int int4DotProductSinglePacked(byte[] unpacked, byte[] packed) {
return int4DotProductSinglePackedBody(new ArrayLoader(unpacked), new ArrayLoader(packed));
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 f34ab3ddbfc..519e05ce08c 100644
--- a/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
+++ b/lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java
@@ -715,4 +715,33 @@ public class TestVectorUtil extends LuceneTestCase {
}
return res;
}
+
+ public void testInt4Unpack() {
+ // Cover lengths below, at, and above a vector register, plus odd lengths that force the
+ // scalar tail of the vectorized implementation to run.
+ for (int packedLen : new int[] {0, 1, 2, 3, 7, 8, 15, 16, 17, 31, 32, 33, 63, 64, 2048}) {
+ byte[] packed = new byte[packedLen];
+ random().nextBytes(packed);
+ byte[] actual = new byte[packedLen * 2];
+ VectorUtil.int4Unpack(packed, actual);
+
+ // reference: the original scalar loop this replaced
+ byte[] expected = new byte[packedLen * 2];
+ for (int i = 0; i < packedLen; i++) {
+ expected[i] = (byte) ((packed[i] >> 4) & 0x0F);
+ expected[packedLen + i] = (byte) (packed[i] & 0x0F);
+ }
+ assertArrayEquals("packedLen=" + packedLen, expected, actual);
+
+ // every output nibble must be in [0,15]
+ for (byte b : actual) {
+ assertTrue("value out of uint4 range: " + b, b >= 0 && b <= 15);
+ }
+ }
+ }
+
+ public void testInt4UnpackRejectsBadLength() {
+ expectThrows(
+ IllegalArgumentException.class, () -> VectorUtil.int4Unpack(new byte[4], new byte[7]));
+ }
}