-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Refactor bulk quantization writing into a unified class #130354
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
Merged
elasticsearchmachine
merged 3 commits into
elastic:main
from
benwtrent:ivf-bulk-writer-refactor
Jul 1, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
104 changes: 104 additions & 0 deletions
104
server/src/main/java/org/elasticsearch/index/codec/vectors/DiskBBQBulkWriter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| package org.elasticsearch.index.codec.vectors; | ||
|
|
||
| import org.apache.lucene.index.FloatVectorValues; | ||
| import org.apache.lucene.store.IndexOutput; | ||
| import org.apache.lucene.util.hnsw.IntToIntFunction; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| import static org.elasticsearch.index.codec.vectors.BQVectorUtils.discretize; | ||
| import static org.elasticsearch.index.codec.vectors.BQVectorUtils.packAsBinary; | ||
|
|
||
| /** | ||
| * Base class for bulk writers that write vectors to disk using the BBQ encoding. | ||
| * This class provides the structure for writing vectors in bulk, with specific | ||
| * implementations for different bit sizes strategies. | ||
| */ | ||
| public abstract class DiskBBQBulkWriter { | ||
| protected final int bulkSize; | ||
| protected final OptimizedScalarQuantizer quantizer; | ||
| protected final IndexOutput out; | ||
| protected final FloatVectorValues fvv; | ||
|
|
||
| protected DiskBBQBulkWriter(int bulkSize, OptimizedScalarQuantizer quantizer, FloatVectorValues fvv, IndexOutput out) { | ||
| this.bulkSize = bulkSize; | ||
| this.quantizer = quantizer; | ||
| this.out = out; | ||
| this.fvv = fvv; | ||
| } | ||
|
|
||
| public abstract void writeOrds(IntToIntFunction ords, int count, float[] centroid) throws IOException; | ||
|
|
||
| private static void writeCorrections(OptimizedScalarQuantizer.QuantizationResult[] corrections, IndexOutput out) throws IOException { | ||
| for (OptimizedScalarQuantizer.QuantizationResult correction : corrections) { | ||
| out.writeInt(Float.floatToIntBits(correction.lowerInterval())); | ||
| } | ||
| for (OptimizedScalarQuantizer.QuantizationResult correction : corrections) { | ||
| out.writeInt(Float.floatToIntBits(correction.upperInterval())); | ||
| } | ||
| for (OptimizedScalarQuantizer.QuantizationResult correction : corrections) { | ||
| int targetComponentSum = correction.quantizedComponentSum(); | ||
| assert targetComponentSum >= 0 && targetComponentSum <= 0xffff; | ||
| out.writeShort((short) targetComponentSum); | ||
| } | ||
| for (OptimizedScalarQuantizer.QuantizationResult correction : corrections) { | ||
| out.writeInt(Float.floatToIntBits(correction.additionalCorrection())); | ||
| } | ||
| } | ||
|
|
||
| private static void writeCorrection(OptimizedScalarQuantizer.QuantizationResult correction, IndexOutput out) throws IOException { | ||
| out.writeInt(Float.floatToIntBits(correction.lowerInterval())); | ||
| out.writeInt(Float.floatToIntBits(correction.upperInterval())); | ||
| out.writeInt(Float.floatToIntBits(correction.additionalCorrection())); | ||
| int targetComponentSum = correction.quantizedComponentSum(); | ||
| assert targetComponentSum >= 0 && targetComponentSum <= 0xffff; | ||
| out.writeShort((short) targetComponentSum); | ||
| } | ||
|
|
||
| public static class OneBitDiskBBQBulkWriter extends DiskBBQBulkWriter { | ||
| private final byte[] binarized; | ||
| private final byte[] initQuantized; | ||
| private final OptimizedScalarQuantizer.QuantizationResult[] corrections; | ||
|
|
||
| public OneBitDiskBBQBulkWriter(int bulkSize, OptimizedScalarQuantizer quantizer, FloatVectorValues fvv, IndexOutput out) { | ||
| super(bulkSize, quantizer, fvv, out); | ||
| this.binarized = new byte[discretize(fvv.dimension(), 64) / 8]; | ||
| this.initQuantized = new byte[fvv.dimension()]; | ||
| this.corrections = new OptimizedScalarQuantizer.QuantizationResult[bulkSize]; | ||
| } | ||
|
|
||
| @Override | ||
| public void writeOrds(IntToIntFunction ords, int count, float[] centroid) throws IOException { | ||
| int limit = count - bulkSize + 1; | ||
| int i = 0; | ||
| for (; i < limit; i += bulkSize) { | ||
| for (int j = 0; j < bulkSize; j++) { | ||
| int ord = ords.apply(i + j); | ||
| float[] fv = fvv.vectorValue(ord); | ||
| corrections[j] = quantizer.scalarQuantize(fv, initQuantized, (byte) 1, centroid); | ||
| packAsBinary(initQuantized, binarized); | ||
| out.writeBytes(binarized, binarized.length); | ||
| } | ||
| writeCorrections(corrections, out); | ||
| } | ||
| // write tail | ||
| for (; i < count; ++i) { | ||
| int ord = ords.apply(i); | ||
| float[] fv = fvv.vectorValue(ord); | ||
| OptimizedScalarQuantizer.QuantizationResult correction = quantizer.scalarQuantize(fv, initQuantized, (byte) 1, centroid); | ||
| packAsBinary(initQuantized, binarized); | ||
| out.writeBytes(binarized, binarized.length); | ||
| writeCorrection(correction, out); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We were actually writing all vectors TWICE in the tail of postings. This (just a tad) negatively impacted recall. This is also why the skew index test started failing as the testing value there apparently relied on this :/