Skip to content

Commit fc00b21

Browse files
author
Pulkit Gupta
committed
Addressed comments
1 parent 8c9da62 commit fc00b21

File tree

3 files changed

+25
-63
lines changed

3 files changed

+25
-63
lines changed

lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.java

Lines changed: 17 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ abstract class OffHeapScalarQuantizedFloatVectorValues extends FloatVectorValues
7070
int quantizedComponentSum;
7171
final Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding;
7272
final float[] centroid;
73-
final boolean isQuerySide;
7473

7574
OffHeapScalarQuantizedFloatVectorValues(
7675
int dimension,
@@ -80,20 +79,6 @@ abstract class OffHeapScalarQuantizedFloatVectorValues extends FloatVectorValues
8079
VectorSimilarityFunction similarityFunction,
8180
FlatVectorsScorer vectorsScorer,
8281
IndexInput slice) {
83-
this(false, dimension, size, centroid, encoding, similarityFunction, vectorsScorer, slice);
84-
}
85-
86-
OffHeapScalarQuantizedFloatVectorValues(
87-
boolean isQuerySide,
88-
int dimension,
89-
int size,
90-
float[] centroid,
91-
Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding,
92-
VectorSimilarityFunction similarityFunction,
93-
FlatVectorsScorer vectorsScorer,
94-
IndexInput slice) {
95-
assert isQuerySide == false || encoding.isAsymmetric();
96-
this.isQuerySide = isQuerySide;
9782
this.dimension = dimension;
9883
this.size = size;
9984
this.similarityFunction = similarityFunction;
@@ -102,10 +87,7 @@ abstract class OffHeapScalarQuantizedFloatVectorValues extends FloatVectorValues
10287
this.centroid = centroid;
10388
this.correctiveValues = new float[3];
10489
this.encoding = encoding;
105-
int docPackedLength =
106-
isQuerySide
107-
? encoding.getQueryPackedLength(dimension)
108-
: encoding.getDocPackedLength(dimension);
90+
int docPackedLength = encoding.getDocPackedLength(dimension);
10991
this.byteSize = docPackedLength + (Float.BYTES * 3) + Integer.BYTES;
11092
this.byteBuffer = ByteBuffer.allocate(docPackedLength);
11193
this.vectorValue = new float[dimension];
@@ -142,15 +124,26 @@ public float[] vectorValue(int targetOrd) throws IOException {
142124
case SINGLE_BIT_QUERY_NIBBLE ->
143125
OptimizedScalarQuantizer.unpackBinary(byteValue, unpackedByteVectorValue);
144126
case UNSIGNED_BYTE, SEVEN_BIT -> {
145-
deQuantize(byteValue, vectorValue, encoding.getBits(), correctiveValues, centroid);
127+
deQuantize(
128+
byteValue,
129+
vectorValue,
130+
encoding.getBits(),
131+
correctiveValues[0],
132+
correctiveValues[1],
133+
centroid);
146134
lastOrd = targetOrd;
147135
return vectorValue;
148136
}
149137
}
150138

151139
// dequantize
152140
deQuantize(
153-
unpackedByteVectorValue, vectorValue, encoding.getBits(), correctiveValues, centroid);
141+
unpackedByteVectorValue,
142+
vectorValue,
143+
encoding.getBits(),
144+
correctiveValues[0],
145+
correctiveValues[1],
146+
centroid);
154147

155148
lastOrd = targetOrd;
156149
return vectorValue;
@@ -219,7 +212,7 @@ static OffHeapScalarQuantizedFloatVectorValues load(
219212
}
220213

221214
/** Dense off-heap scalar quantized vector values */
222-
static class DenseOffHeapVectorValues extends OffHeapScalarQuantizedFloatVectorValues {
215+
private static class DenseOffHeapVectorValues extends OffHeapScalarQuantizedFloatVectorValues {
223216
DenseOffHeapVectorValues(
224217
int dimension,
225218
int size,
@@ -231,38 +224,11 @@ static class DenseOffHeapVectorValues extends OffHeapScalarQuantizedFloatVectorV
231224
super(dimension, size, centroid, encoding, similarityFunction, vectorsScorer, slice);
232225
}
233226

234-
DenseOffHeapVectorValues(
235-
boolean isQuerySide,
236-
int dimension,
237-
int size,
238-
float[] centroid,
239-
Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding,
240-
VectorSimilarityFunction similarityFunction,
241-
FlatVectorsScorer vectorsScorer,
242-
IndexInput slice) {
243-
super(
244-
isQuerySide,
245-
dimension,
246-
size,
247-
centroid,
248-
encoding,
249-
similarityFunction,
250-
vectorsScorer,
251-
slice);
252-
}
253-
254227
@Override
255228
public OffHeapScalarQuantizedFloatVectorValues.DenseOffHeapVectorValues copy()
256229
throws IOException {
257230
return new OffHeapScalarQuantizedFloatVectorValues.DenseOffHeapVectorValues(
258-
isQuerySide,
259-
dimension,
260-
size,
261-
centroid,
262-
encoding,
263-
similarityFunction,
264-
vectorsScorer,
265-
slice.clone());
231+
dimension, size, centroid, encoding, similarityFunction, vectorsScorer, slice.clone());
266232
}
267233

268234
@Override
@@ -272,7 +238,6 @@ public Bits getAcceptOrds(Bits acceptDocs) {
272238

273239
@Override
274240
public VectorScorer scorer(float[] target) throws IOException {
275-
assert isQuerySide == false;
276241
OffHeapScalarQuantizedFloatVectorValues.DenseOffHeapVectorValues copy = copy();
277242
DocIndexIterator iterator = copy.iterator();
278243
RandomVectorScorer scorer =
@@ -316,7 +281,6 @@ private static class SparseOffHeapVectorValues extends OffHeapScalarQuantizedFlo
316281
IndexInput slice)
317282
throws IOException {
318283
super(dimension, size, centroid, encoding, similarityFunction, vectorsScorer, slice);
319-
assert isQuerySide == false;
320284
this.configuration = configuration;
321285
this.dataIn = dataIn;
322286
this.ordToDoc = configuration.getDirectMonotonicReader(dataIn);
@@ -326,7 +290,6 @@ private static class SparseOffHeapVectorValues extends OffHeapScalarQuantizedFlo
326290
@Override
327291
public OffHeapScalarQuantizedFloatVectorValues.SparseOffHeapVectorValues copy()
328292
throws IOException {
329-
assert isQuerySide == false;
330293
return new OffHeapScalarQuantizedFloatVectorValues.SparseOffHeapVectorValues(
331294
configuration,
332295
dimension,
@@ -369,7 +332,6 @@ public DocIndexIterator iterator() {
369332

370333
@Override
371334
public VectorScorer scorer(float[] target) throws IOException {
372-
assert isQuerySide == false;
373335
OffHeapScalarQuantizedFloatVectorValues.SparseOffHeapVectorValues copy = copy();
374336
DocIndexIterator iterator = copy.iterator();
375337
RandomVectorScorer scorer =
@@ -388,6 +350,7 @@ public DocIdSetIterator iterator() {
388350
}
389351
}
390352

353+
/** Empty vector values */
391354
private static class EmptyOffHeapVectorValues extends OffHeapScalarQuantizedFloatVectorValues {
392355
EmptyOffHeapVectorValues(
393356
int dimension,
@@ -401,7 +364,6 @@ private static class EmptyOffHeapVectorValues extends OffHeapScalarQuantizedFloa
401364
similarityFunction,
402365
vectorsScorer,
403366
null);
404-
assert isQuerySide == false;
405367
}
406368

407369
@Override

lucene/core/src/java/org/apache/lucene/util/quantization/OptimizedScalarQuantizer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -245,23 +245,22 @@ public QuantizationResult scalarQuantize(
245245
* @param quantized the quantized byte vector to dequantize
246246
* @param dequantized the output array to store dequantized float values
247247
* @param bits the number of bits used for quantization
248-
* @param correctiveValues array containing [a, b, unused] where a=min, b=max of quantization
249-
* range
248+
* @param lowerCorrectiveValue min value of quantization range
249+
* @param upperCorrectiveValue max value of quantization range
250250
* @param centroid the centroid vector that was subtracted during quantization
251251
* @return the dequantized float array (same as dequantized parameter)
252252
*/
253253
public static float[] deQuantize(
254254
byte[] quantized,
255255
float[] dequantized,
256256
byte bits,
257-
float[] correctiveValues,
257+
float lowerCorrectiveValue,
258+
float upperCorrectiveValue,
258259
float[] centroid) {
259-
float a = correctiveValues[0];
260-
float b = correctiveValues[1];
261260
int nSteps = (1 << bits) - 1;
262-
double step = (b - a) / nSteps;
261+
double step = (upperCorrectiveValue - lowerCorrectiveValue) / nSteps;
263262
for (int h = 0; h < quantized.length; h++) {
264-
double xi = (double) (quantized[h] & 0xFF) * step + a;
263+
double xi = (double) (quantized[h] & 0xFF) * step + lowerCorrectiveValue;
265264
dequantized[h] = (float) (xi + centroid[h]);
266265
}
267266
return dequantized;

lucene/core/src/test/org/apache/lucene/util/quantization/TestOptimizedScalarQuantizer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public void testQuantizationQuality() {
6666
destination,
6767
dequantized,
6868
bit,
69-
new float[] {result.lowerInterval(), result.upperInterval()},
69+
result.lowerInterval(),
70+
result.upperInterval(),
7071
centroid);
7172
float mae = 0;
7273
for (int k = 0; k < dims; ++k) {

0 commit comments

Comments
 (0)