Skip to content
Merged
Changes from 1 commit
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 @@ -11,7 +11,6 @@

import org.apache.lucene.codecs.hnsw.FlatVectorsReader;
import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FloatVectorValues;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.VectorSimilarityFunction;
import org.apache.lucene.search.KnnCollector;
Expand All @@ -20,10 +19,12 @@
import org.apache.lucene.util.VectorUtil;
import org.apache.lucene.util.hnsw.NeighborQueue;
import org.apache.lucene.util.quantization.OptimizedScalarQuantizer;
import org.elasticsearch.index.codec.vectors.reflect.OffHeapStats;
import org.elasticsearch.simdvec.ES91OSQVectorsScorer;
import org.elasticsearch.simdvec.ESVectorUtil;

import java.io.IOException;
import java.util.Map;
import java.util.function.IntPredicate;

import static org.apache.lucene.codecs.lucene102.Lucene102BinaryQuantizedVectorsFormat.QUERY_BITS;
Expand All @@ -38,7 +39,7 @@
* Default implementation of {@link IVFVectorsReader}. It scores the posting lists centroids using
* brute force and then scores the top ones using the posting list.
*/
public class DefaultIVFVectorsReader extends IVFVectorsReader {
public class DefaultIVFVectorsReader extends IVFVectorsReader implements OffHeapStats {
Copy link
Contributor

Choose a reason for hiding this comment

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

++ Implementing OffHeapStats is right, until we get to lucene 10.3.

private static final float FOUR_BIT_SCALE = 1f / ((1 << 4) - 1);

public DefaultIVFVectorsReader(SegmentReadState state, FlatVectorsReader rawVectorsReader) throws IOException {
Expand Down Expand Up @@ -163,57 +164,9 @@ static float int4QuantizedScore(
}
}

static class OffHeapCentroidFloatVectorValues extends FloatVectorValues {
private final int numCentroids;
private final IndexInput input;
private final int dimension;
private final float[] centroid;
private final long centroidByteSize;
private int ord = -1;

OffHeapCentroidFloatVectorValues(int numCentroids, IndexInput input, int dimension) {
this.numCentroids = numCentroids;
this.input = input;
this.dimension = dimension;
this.centroid = new float[dimension];
this.centroidByteSize = dimension + 3 * Float.BYTES + Short.BYTES;
}

@Override
public float[] vectorValue(int ord) throws IOException {
if (ord < 0 || ord >= numCentroids) {
throw new IllegalArgumentException("ord must be in [0, " + numCentroids + "]");
}
if (ord == this.ord) {
return centroid;
}
readQuantizedCentroid(ord);
return centroid;
}

private void readQuantizedCentroid(int centroidOrdinal) throws IOException {
if (centroidOrdinal == ord) {
return;
}
input.seek(numCentroids * centroidByteSize + (long) Float.BYTES * dimension * centroidOrdinal);
input.readFloats(centroid, 0, centroid.length);
ord = centroidOrdinal;
}

@Override
public int dimension() {
return dimension;
}

@Override
public int size() {
return numCentroids;
}

@Override
public FloatVectorValues copy() throws IOException {
return new OffHeapCentroidFloatVectorValues(numCentroids, input.clone(), dimension);
}
@Override
public Map<String, Long> getOffHeapByteSize(FieldInfo fieldInfo) {
return Map.of();
}

private static class MemorySegmentPostingsVisitor implements PostingVisitor {
Expand Down