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

Expand scalar quantization with adding half-byte (int4) quantization #13197

Merged
merged 24 commits into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ New Features
This may improve paging logic especially when large segments are merged under memory pressure.
(Uwe Schindler, Chris Hegarty, Robert Muir, Adrien Grand)

* GITHUB#13197: Expand support for new scalar bit levels for HNSW vectors. This includes 4-bit vectors and an option
to compress them to gain a 50% reduction in memory usage. (Ben Trent)

Improvements
---------------------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ public class VectorUtilBenchmark {

private byte[] bytesA;
private byte[] bytesB;
private byte[] halfBytesA;
private byte[] halfBytesB;
private float[] floatsA;
private float[] floatsB;

@Param({"1", "128", "207", "256", "300", "512", "702", "1024"})
@Param({"1024"})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't we keep the other options too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this was a mistake to commit this change, I was benchmarking :/

int size;

@Setup(Level.Iteration)
Expand All @@ -51,6 +53,14 @@ public void init() {
bytesB = new byte[size];
random.nextBytes(bytesA);
random.nextBytes(bytesB);
// random half byte arrays for binary methods
// this means that all values must be between 0 and 15
halfBytesA = new byte[size];
halfBytesB = new byte[size];
for (int i = 0; i < size; ++i) {
halfBytesA[i] = (byte) random.nextInt(16);
halfBytesB[i] = (byte) random.nextInt(16);
}

// random float arrays for float methods
floatsA = new float[size];
Expand Down Expand Up @@ -94,6 +104,17 @@ public int binarySquareVector() {
return VectorUtil.squareDistance(bytesA, bytesB);
}

@Benchmark
public int binaryHalfByteScalar() {
return VectorUtil.int4DotProduct(halfBytesA, halfBytesB);
}

@Benchmark
@Fork(jvmArgsPrepend = {"--add-modules=jdk.incubator.vector"})
public int binaryHalfByteVector() {
return VectorUtil.int4DotProduct(halfBytesA, halfBytesB);
}

@Benchmark
public float floatCosineScalar() {
return VectorUtil.cosine(floatsA, floatsB);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public final class Lucene99HnswScalarQuantizedVectorsFormat extends KnnVectorsFo

/** Constructs a format using default graph construction parameters */
public Lucene99HnswScalarQuantizedVectorsFormat() {
this(DEFAULT_MAX_CONN, DEFAULT_BEAM_WIDTH, DEFAULT_NUM_MERGE_WORKER, null, null);
this(
DEFAULT_MAX_CONN, DEFAULT_BEAM_WIDTH, DEFAULT_NUM_MERGE_WORKER, (byte) 7, true, null, null);
}

/**
Expand All @@ -75,7 +76,7 @@ public Lucene99HnswScalarQuantizedVectorsFormat() {
* @param beamWidth the size of the queue maintained during graph construction.
*/
public Lucene99HnswScalarQuantizedVectorsFormat(int maxConn, int beamWidth) {
this(maxConn, beamWidth, DEFAULT_NUM_MERGE_WORKER, null, null);
this(maxConn, beamWidth, DEFAULT_NUM_MERGE_WORKER, (byte) 7, true, null, null);
}

/**
Expand All @@ -85,6 +86,11 @@ public Lucene99HnswScalarQuantizedVectorsFormat(int maxConn, int beamWidth) {
* @param beamWidth the size of the queue maintained during graph construction.
* @param numMergeWorkers number of workers (threads) that will be used when doing merge. If
* larger than 1, a non-null {@link ExecutorService} must be passed as mergeExec
* @param bits the number of bits to use for scalar quantization (must be between 1 and 8,
* inclusive)
* @param compress whether to compress the vectors, if true, the vectors that are quantized with
* lte 4 bits will be compressed into a single byte. If false, the vectors will be stored as
* is. This provides a trade-off of memory usage and speed.
* @param confidenceInterval the confidenceInterval for scalar quantizing the vectors, when `null`
* it is calculated based on the vector field dimensions.
* @param mergeExec the {@link ExecutorService} that will be used by ALL vector writers that are
Expand All @@ -94,6 +100,8 @@ public Lucene99HnswScalarQuantizedVectorsFormat(
int maxConn,
int beamWidth,
int numMergeWorkers,
byte bits,
boolean compress,
Float confidenceInterval,
ExecutorService mergeExec) {
super("Lucene99HnswScalarQuantizedVectorsFormat");
Expand Down Expand Up @@ -127,7 +135,8 @@ public Lucene99HnswScalarQuantizedVectorsFormat(
} else {
this.mergeExec = null;
}
this.flatVectorsFormat = new Lucene99ScalarQuantizedVectorsFormat(confidenceInterval);
this.flatVectorsFormat =
new Lucene99ScalarQuantizedVectorsFormat(confidenceInterval, bits, compress);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public final class Lucene99ScalarQuantizedVectorsFormat extends FlatVectorsForma
static final String NAME = "Lucene99ScalarQuantizedVectorsFormat";

static final int VERSION_START = 0;
static final int VERSION_CURRENT = VERSION_START;
static final int VERSION_ADD_BITS = 1;
static final int VERSION_CURRENT = VERSION_ADD_BITS;
static final String META_CODEC_NAME = "Lucene99ScalarQuantizedVectorsFormatMeta";
static final String VECTOR_DATA_CODEC_NAME = "Lucene99ScalarQuantizedVectorsFormatData";
static final String META_EXTENSION = "vemq";
Expand All @@ -55,18 +56,27 @@ public final class Lucene99ScalarQuantizedVectorsFormat extends FlatVectorsForma
*/
final Float confidenceInterval;

final byte bits;
final boolean compress;

/** Constructs a format using default graph construction parameters */
public Lucene99ScalarQuantizedVectorsFormat() {
this(null);
this(null, 7, true);
}

/**
* Constructs a format using the given graph construction parameters.
*
* @param confidenceInterval the confidenceInterval for scalar quantizing the vectors, when `null`
* it is calculated based on the vector field dimensions.
* it is calculated dynamically.
* @param bits the number of bits to use for scalar quantization (must be between 1 and 8,
* inclusive)
* @param compress whether to compress the vectors, if true, the vectors that are quantized with
* lte 4 bits will be compressed into a single byte. If false, the vectors will be stored as
* is. This provides a trade-off of memory usage and speed.
*/
public Lucene99ScalarQuantizedVectorsFormat(Float confidenceInterval) {
public Lucene99ScalarQuantizedVectorsFormat(
Float confidenceInterval, int bits, boolean compress) {
if (confidenceInterval != null
&& (confidenceInterval < MINIMUM_CONFIDENCE_INTERVAL
|| confidenceInterval > MAXIMUM_CONFIDENCE_INTERVAL)) {
Expand All @@ -78,7 +88,12 @@ public Lucene99ScalarQuantizedVectorsFormat(Float confidenceInterval) {
+ "; confidenceInterval="
+ confidenceInterval);
}
if (bits < 1 || bits > 8) {
throw new IllegalArgumentException("bits must be between 1 and 8; bits=" + bits);
}
this.bits = (byte) bits;
this.confidenceInterval = confidenceInterval;
this.compress = compress;
}

public static float calculateDefaultConfidenceInterval(int vectorDimension) {
Expand All @@ -92,6 +107,10 @@ public String toString() {
+ NAME
+ ", confidenceInterval="
+ confidenceInterval
+ ", bits="
+ bits
+ ", compress="
+ compress
+ ", rawVectorFormat="
+ rawVectorFormat
+ ")";
Expand All @@ -100,7 +119,7 @@ public String toString() {
@Override
public FlatVectorsWriter fieldsWriter(SegmentWriteState state) throws IOException {
return new Lucene99ScalarQuantizedVectorsWriter(
state, confidenceInterval, rawVectorFormat.fieldsWriter(state));
state, confidenceInterval, bits, compress, rawVectorFormat.fieldsWriter(state));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public Lucene99ScalarQuantizedVectorsReader(
Lucene99ScalarQuantizedVectorsFormat.VERSION_CURRENT,
state.segmentInfo.getId(),
state.segmentSuffix);
readFields(meta, state.fieldInfos);
readFields(meta, versionMeta, state.fieldInfos);
} catch (Throwable exception) {
priorE = exception;
} finally {
Expand All @@ -102,13 +102,14 @@ public Lucene99ScalarQuantizedVectorsReader(
}
}

private void readFields(ChecksumIndexInput meta, FieldInfos infos) throws IOException {
private void readFields(ChecksumIndexInput meta, int versionMeta, FieldInfos infos)
throws IOException {
for (int fieldNumber = meta.readInt(); fieldNumber != -1; fieldNumber = meta.readInt()) {
FieldInfo info = infos.fieldInfo(fieldNumber);
if (info == null) {
throw new CorruptIndexException("Invalid field number: " + fieldNumber, meta);
}
FieldEntry fieldEntry = readField(meta);
FieldEntry fieldEntry = readField(meta, versionMeta);
validateFieldEntry(info, fieldEntry);
fields.put(info.name, fieldEntry);
}
Expand All @@ -126,8 +127,13 @@ static void validateFieldEntry(FieldInfo info, FieldEntry fieldEntry) {
+ fieldEntry.dimension);
}

// int8 quantized and calculated stored offset.
long quantizedVectorBytes = dimension + Float.BYTES;
final long quantizedVectorBytes;
if (fieldEntry.bits <= 4 && fieldEntry.compress) {
quantizedVectorBytes = ((dimension + 1) >> 1) + Float.BYTES;
} else {
// int8 quantized and calculated stored offset.
quantizedVectorBytes = dimension + Float.BYTES;
}
long numQuantizedVectorBytes = Math.multiplyExact(quantizedVectorBytes, fieldEntry.size);
if (numQuantizedVectorBytes != fieldEntry.vectorDataLength) {
throw new IllegalStateException(
Expand Down Expand Up @@ -209,6 +215,8 @@ public RandomVectorScorer getRandomVectorScorer(String field, float[] target) th
fieldEntry.ordToDoc,
fieldEntry.dimension,
fieldEntry.size,
fieldEntry.bits,
fieldEntry.compress,
fieldEntry.vectorDataOffset,
fieldEntry.vectorDataLength,
quantizedVectorData);
Expand Down Expand Up @@ -236,10 +244,10 @@ public long ramBytesUsed() {
return size;
}

private FieldEntry readField(IndexInput input) throws IOException {
private FieldEntry readField(IndexInput input, int versionMeta) throws IOException {
VectorEncoding vectorEncoding = readVectorEncoding(input);
VectorSimilarityFunction similarityFunction = readSimilarityFunction(input);
return new FieldEntry(input, vectorEncoding, similarityFunction);
return new FieldEntry(input, versionMeta, vectorEncoding, similarityFunction);
}

@Override
Expand All @@ -252,6 +260,8 @@ public QuantizedByteVectorValues getQuantizedVectorValues(String fieldName) thro
fieldEntry.ordToDoc,
fieldEntry.dimension,
fieldEntry.size,
fieldEntry.bits,
fieldEntry.compress,
fieldEntry.vectorDataOffset,
fieldEntry.vectorDataLength,
quantizedVectorData);
Expand All @@ -276,10 +286,13 @@ private static class FieldEntry implements Accountable {
final long vectorDataLength;
final ScalarQuantizer scalarQuantizer;
final int size;
final byte bits;
final boolean compress;
final OrdToDocDISIReaderConfiguration ordToDoc;

FieldEntry(
IndexInput input,
int versionMeta,
VectorEncoding vectorEncoding,
VectorSimilarityFunction similarityFunction)
throws IOException {
Expand All @@ -290,12 +303,29 @@ private static class FieldEntry implements Accountable {
dimension = input.readVInt();
size = input.readInt();
if (size > 0) {
float confidenceInterval = Float.intBitsToFloat(input.readInt());
float minQuantile = Float.intBitsToFloat(input.readInt());
float maxQuantile = Float.intBitsToFloat(input.readInt());
scalarQuantizer = new ScalarQuantizer(minQuantile, maxQuantile, confidenceInterval);
if (versionMeta < Lucene99ScalarQuantizedVectorsFormat.VERSION_ADD_BITS) {
int floatBits = input.readInt(); // confidenceInterval, unused
if (floatBits == -1) {
throw new CorruptIndexException(
"Missing confidence interval for scalar quantizer", input);
}
this.bits = (byte) 7;
this.compress = false;
float minQuantile = Float.intBitsToFloat(input.readInt());
float maxQuantile = Float.intBitsToFloat(input.readInt());
scalarQuantizer = new ScalarQuantizer(minQuantile, maxQuantile, (byte) 7);
} else {
input.readInt(); // confidenceInterval, unused
this.bits = input.readByte();
this.compress = input.readByte() == 1;
float minQuantile = Float.intBitsToFloat(input.readInt());
float maxQuantile = Float.intBitsToFloat(input.readInt());
scalarQuantizer = new ScalarQuantizer(minQuantile, maxQuantile, bits);
}
} else {
scalarQuantizer = null;
this.bits = (byte) 7;
this.compress = false;
}
ordToDoc = OrdToDocDISIReaderConfiguration.fromStoredMeta(input, size);
}
Expand Down