Skip to content

[DiskBBQ] Compute only higher three bits for skipping #131462

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,33 @@ public void scoreFromMemorySegmentOnlyVector(Blackhole bh) throws IOException {
}
}

@Benchmark
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
public void scoreThreeUpperBitsFromMemorySegmentOnlyVector(Blackhole bh) throws IOException {
for (int j = 0; j < numQueries; j++) {
in.seek(0);
for (int i = 0; i < numVectors; i++) {
float qDist = scorer.quantizeScoreThreeUpperBit(binaryQueries[j]);
in.readFloats(corrections, 0, corrections.length);
int addition = Short.toUnsignedInt(in.readShort());
float score = scorer.score(
result.lowerInterval(),
result.upperInterval(),
result.quantizedComponentSum(),
result.additionalCorrection(),
VectorSimilarityFunction.EUCLIDEAN,
centroidDp,
corrections[0],
corrections[1],
addition,
corrections[2],
qDist
);
bh.consume(score);
}
}
}

@Benchmark
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
public void scoreFromMemorySegmentOnlyVectorBulk(Blackhole bh) throws IOException {
Expand Down Expand Up @@ -199,6 +226,35 @@ public void scoreFromMemorySegmentOnlyVectorBulk(Blackhole bh) throws IOExceptio
}
}

@Benchmark
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
public void scoreThreeUpperBitsFromMemorySegmentOnlyVectorBulk(Blackhole bh) throws IOException {
for (int j = 0; j < numQueries; j++) {
in.seek(0);
for (int i = 0; i < numVectors; i += 16) {
scorer.quantizeScoreThreeUpperBitBulk(binaryQueries[j], ES91OSQVectorsScorer.BULK_SIZE, scratchScores);
for (int k = 0; k < ES91OSQVectorsScorer.BULK_SIZE; k++) {
in.readFloats(corrections, 0, corrections.length);
int addition = Short.toUnsignedInt(in.readShort());
float score = scorer.score(
result.lowerInterval(),
result.upperInterval(),
result.quantizedComponentSum(),
result.additionalCorrection(),
VectorSimilarityFunction.EUCLIDEAN,
centroidDp,
corrections[0],
corrections[1],
addition,
corrections[2],
scratchScores[k]
);
bh.consume(score);
}
}
}
}

@Benchmark
@Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" })
public void scoreFromMemorySegmentAllBulk(Blackhole bh) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,54 @@ public long quantizeScore(byte[] q) throws IOException {
return subRet0 + (subRet1 << 1) + (subRet2 << 2) + (subRet3 << 3);
}

public long quantizeScoreThreeUpperBit(byte[] q) throws IOException {
assert q.length == length * 4;
final int size = length;
long subRet1 = 0;
long subRet2 = 0;
long subRet3 = 0;
int r = 0;
for (final int upperBound = size & -Long.BYTES; r < upperBound; r += Long.BYTES) {
final long value = in.readLong();
subRet1 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r + size) & value);
subRet2 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r + 2 * size) & value);
subRet3 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r + 3 * size) & value);
}
for (final int upperBound = size & -Integer.BYTES; r < upperBound; r += Integer.BYTES) {
final int value = in.readInt();
subRet1 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r + size) & value);
subRet2 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r + 2 * size) & value);
subRet3 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r + 3 * size) & value);
}
for (; r < size; r++) {
final byte value = in.readByte();
subRet1 += Integer.bitCount((q[r + size] & value) & 0xFF);
subRet2 += Integer.bitCount((q[r + 2 * size] & value) & 0xFF);
subRet3 += Integer.bitCount((q[r + 3 * size] & value) & 0xFF);
}
return (subRet1 << 1) + (subRet2 << 2) + (subRet3 << 3);
}

public long quantizeScoreLowerBit(byte[] q) throws IOException {
assert q.length == length * 4;
final int size = length;
long subRet = 0;
int r = 0;
for (final int upperBound = size & -Long.BYTES; r < upperBound; r += Long.BYTES) {
final long value = in.readLong();
subRet += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r) & value);
}
for (final int upperBound = size & -Integer.BYTES; r < upperBound; r += Integer.BYTES) {
final int value = in.readInt();
subRet += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r) & value);
}
for (; r < size; r++) {
final byte value = in.readByte();
subRet += Integer.bitCount((q[r] & value) & 0xFF);
}
return subRet;
}

/**
* compute the quantize distance between the provided quantized query and the quantized vectors
* that are read from the wrapped {@link IndexInput}. The number of quantized vectors to read is
Expand All @@ -91,6 +139,12 @@ public void quantizeScoreBulk(byte[] q, int count, float[] scores) throws IOExce
}
}

public void quantizeScoreThreeUpperBitBulk(byte[] q, int count, float[] scores) throws IOException {
for (int i = 0; i < count; i++) {
scores[i] = quantizeScoreThreeUpperBit(q);
}
}

/**
* Computes the score by applying the necessary corrections to the provided quantized distance.
*/
Expand Down
Loading