benwtrent commented on code in PR #15415:
URL: https://github.com/apache/lucene/pull/15415#discussion_r2519362543


##########
lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.java:
##########
@@ -0,0 +1,427 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.codecs.lucene104;
+
+import static 
org.apache.lucene.util.quantization.OptimizedScalarQuantizer.deQuantize;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.lucene90.IndexedDISI;
+import org.apache.lucene.codecs.lucene95.HasIndexSlice;
+import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration;
+import org.apache.lucene.index.FloatVectorValues;
+import org.apache.lucene.index.VectorSimilarityFunction;
+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.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.packed.DirectMonotonicReader;
+import org.apache.lucene.util.quantization.OptimizedScalarQuantizer;
+
+/**
+ * Reads quantized vector values from the index input and returns float vector 
values after
+ * dequantizing them.
+ *
+ * <p>This class provides functionality to read quantized vectors which are 
stored in the index, and
+ * then dequantize them back to float vectors with some precision loss. The 
implementation is based
+ * on {@code OffHeapScalarQuantizedVectorValues} with modifications to the 
{@code vectorValue()}
+ * method to return float vectors after dequantizing the vectors.
+ *
+ * <p>Usage: This class is used for read-only indexes where full-precision 
float vectors have been
+ * dropped from the index to save storage space. Full-precision vectors can be 
removed from an index
+ * using a method as implemented in {@code
+ * TestLucene104ScalarQuantizedVectorsFormat.simulateEmptyRawVectors()}.
+ *
+ * @lucene.internal
+ */
+abstract class OffHeapScalarQuantizedFloatVectorValues extends 
FloatVectorValues
+    implements HasIndexSlice {
+
+  final int dimension;
+  final int size;
+  final VectorSimilarityFunction similarityFunction;
+  final FlatVectorsScorer vectorsScorer;
+
+  final IndexInput slice;
+  final float[] vectorValue;
+  final byte[] byteValue;
+  final ByteBuffer byteBuffer;
+  final byte[] unpackedByteVectorValue;
+  final int byteSize;
+  private int lastOrd = -1;
+  final float[] correctiveValues;
+  int quantizedComponentSum;
+  final Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding;
+  final float[] centroid;
+  final boolean isQuerySide;
+
+  OffHeapScalarQuantizedFloatVectorValues(
+      int dimension,
+      int size,
+      float[] centroid,
+      Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding,
+      VectorSimilarityFunction similarityFunction,
+      FlatVectorsScorer vectorsScorer,
+      IndexInput slice) {
+    this(false, dimension, size, centroid, encoding, similarityFunction, 
vectorsScorer, slice);
+  }
+
+  OffHeapScalarQuantizedFloatVectorValues(
+      boolean isQuerySide,

Review Comment:
   We should never allow this "querySide" thing. Even as it is now, it wouldn't 
work.



##########
lucene/CHANGES.txt:
##########
@@ -176,6 +176,9 @@ New Features
    
`Lucene104HnswScalarQuantizedVectorsFormat(ScalarEncoding.SINGLE_BIT_QUERY_NIBBLE,
 int, int)`
     (Ben Trent)
 
+ * GITHUB#15415: Add fallback support to Lucene104ScalarQuantizedVectorsFormat 
getFloatVectorValues when there are
+   no full-precision vectors present

Review Comment:
   Please add your name for posterity :)



##########
lucene/core/src/java/org/apache/lucene/codecs/lucene104/OffHeapScalarQuantizedFloatVectorValues.java:
##########
@@ -0,0 +1,427 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.lucene.codecs.lucene104;
+
+import static 
org.apache.lucene.util.quantization.OptimizedScalarQuantizer.deQuantize;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import org.apache.lucene.codecs.hnsw.FlatVectorsScorer;
+import org.apache.lucene.codecs.lucene90.IndexedDISI;
+import org.apache.lucene.codecs.lucene95.HasIndexSlice;
+import org.apache.lucene.codecs.lucene95.OrdToDocDISIReaderConfiguration;
+import org.apache.lucene.index.FloatVectorValues;
+import org.apache.lucene.index.VectorSimilarityFunction;
+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.hnsw.RandomVectorScorer;
+import org.apache.lucene.util.packed.DirectMonotonicReader;
+import org.apache.lucene.util.quantization.OptimizedScalarQuantizer;
+
+/**
+ * Reads quantized vector values from the index input and returns float vector 
values after
+ * dequantizing them.
+ *
+ * <p>This class provides functionality to read quantized vectors which are 
stored in the index, and
+ * then dequantize them back to float vectors with some precision loss. The 
implementation is based
+ * on {@code OffHeapScalarQuantizedVectorValues} with modifications to the 
{@code vectorValue()}
+ * method to return float vectors after dequantizing the vectors.
+ *
+ * <p>Usage: This class is used for read-only indexes where full-precision 
float vectors have been
+ * dropped from the index to save storage space. Full-precision vectors can be 
removed from an index
+ * using a method as implemented in {@code
+ * TestLucene104ScalarQuantizedVectorsFormat.simulateEmptyRawVectors()}.
+ *
+ * @lucene.internal
+ */
+abstract class OffHeapScalarQuantizedFloatVectorValues extends 
FloatVectorValues
+    implements HasIndexSlice {
+
+  final int dimension;
+  final int size;
+  final VectorSimilarityFunction similarityFunction;
+  final FlatVectorsScorer vectorsScorer;
+
+  final IndexInput slice;
+  final float[] vectorValue;
+  final byte[] byteValue;
+  final ByteBuffer byteBuffer;
+  final byte[] unpackedByteVectorValue;
+  final int byteSize;
+  private int lastOrd = -1;
+  final float[] correctiveValues;
+  int quantizedComponentSum;
+  final Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding;
+  final float[] centroid;
+  final boolean isQuerySide;
+
+  OffHeapScalarQuantizedFloatVectorValues(
+      int dimension,
+      int size,
+      float[] centroid,
+      Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding,
+      VectorSimilarityFunction similarityFunction,
+      FlatVectorsScorer vectorsScorer,
+      IndexInput slice) {
+    this(false, dimension, size, centroid, encoding, similarityFunction, 
vectorsScorer, slice);
+  }
+
+  OffHeapScalarQuantizedFloatVectorValues(
+      boolean isQuerySide,
+      int dimension,
+      int size,
+      float[] centroid,
+      Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding,
+      VectorSimilarityFunction similarityFunction,
+      FlatVectorsScorer vectorsScorer,
+      IndexInput slice) {
+    assert isQuerySide == false || encoding.isAsymmetric();
+    this.isQuerySide = isQuerySide;
+    this.dimension = dimension;
+    this.size = size;
+    this.similarityFunction = similarityFunction;
+    this.vectorsScorer = vectorsScorer;
+    this.slice = slice;
+    this.centroid = centroid;
+    this.correctiveValues = new float[3];
+    this.encoding = encoding;
+    int docPackedLength =
+        isQuerySide
+            ? encoding.getQueryPackedLength(dimension)
+            : encoding.getDocPackedLength(dimension);
+    this.byteSize = docPackedLength + (Float.BYTES * 3) + Integer.BYTES;
+    this.byteBuffer = ByteBuffer.allocate(docPackedLength);
+    this.vectorValue = new float[dimension];
+    this.byteValue = byteBuffer.array();
+    this.unpackedByteVectorValue = new byte[dimension];
+  }
+
+  @Override
+  public int dimension() {
+    return dimension;
+  }
+
+  @Override
+  public int size() {
+    return size;
+  }
+
+  @Override
+  public float[] vectorValue(int targetOrd) throws IOException {
+    if (lastOrd == targetOrd) {
+      return vectorValue;
+    }
+
+    // read quantized byte vector, correctiveValues and quantizedComponentSum
+    slice.seek((long) targetOrd * byteSize);
+    slice.readBytes(byteBuffer.array(), byteBuffer.arrayOffset(), 
byteValue.length);
+    slice.readFloats(correctiveValues, 0, 3);
+    quantizedComponentSum = slice.readInt();
+
+    // unpack bytes
+    switch (encoding) {
+      case PACKED_NIBBLE ->
+          OffHeapScalarQuantizedVectorValues.unpackNibbles(byteValue, 
unpackedByteVectorValue);
+      case SINGLE_BIT_QUERY_NIBBLE ->
+          OptimizedScalarQuantizer.unpackBinary(byteValue, 
unpackedByteVectorValue);
+      case UNSIGNED_BYTE, SEVEN_BIT -> {
+        deQuantize(byteValue, vectorValue, encoding.getBits(), 
correctiveValues, centroid);
+        lastOrd = targetOrd;
+        return vectorValue;
+      }
+    }

Review Comment:
   If this was "query side" it wouldn't work. Consequently, I think this query 
side thing should go away. 
   
   I think this piece is great if we always assume document quantized bits.



##########
lucene/core/src/java/org/apache/lucene/util/quantization/OptimizedScalarQuantizer.java:
##########
@@ -236,6 +236,37 @@ public QuantizationResult scalarQuantize(
         sumQuery);
   }
 
+  /**
+   * Dequantizes a quantized byte vector back to float values.
+   *
+   * <p>This method reconstructs float vectors from their quantized byte 
representation using linear
+   * interpolation between the corrective range [a, b] and adding back the 
centroid offset.
+   *
+   * @param quantized the quantized byte vector to dequantize
+   * @param dequantized the output array to store dequantized float values
+   * @param bits the number of bits used for quantization
+   * @param correctiveValues array containing [a, b, unused] where a=min, 
b=max of quantization
+   *     range
+   * @param centroid the centroid vector that was subtracted during 
quantization
+   * @return the dequantized float array (same as dequantized parameter)
+   */
+  public static float[] deQuantize(
+      byte[] quantized,
+      float[] dequantized,
+      byte bits,
+      float[] correctiveValues,

Review Comment:
   maybe make this specifically `lower` `upper` thing instead of this array? 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to