kaivalnp commented on code in PR #17013: URL: https://github.com/apache/lucene/pull/17013#discussion_r4569499026
########## lucene/core/src/java25/org/apache/lucene/internal/vectorization/Lucene104MemorySegmentScalarQuantizedVectorScorer.java: ########## @@ -0,0 +1,448 @@ +/* + * 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.internal.vectorization; + +import java.io.IOException; +import java.lang.foreign.MemorySegment; +import java.lang.foreign.ValueLayout; +import java.nio.ByteOrder; +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; +import org.apache.lucene.codecs.lucene104.Lucene104ScalarQuantizedVectorScorer; +import org.apache.lucene.codecs.lucene104.OffHeapScalarQuantizedVectorValues; +import org.apache.lucene.index.KnnVectorValues; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.store.FilterIndexInput; +import org.apache.lucene.store.IndexInput; +import org.apache.lucene.store.MemorySegmentAccessInput; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; +import org.apache.lucene.util.hnsw.UpdateableRandomVectorScorer; +import org.apache.lucene.util.quantization.OptimizedScalarQuantizer.QuantizationResult; +import org.apache.lucene.util.quantization.QuantizedByteVectorValues; +import org.apache.lucene.util.quantization.QuantizedByteVectorValues.ScalarEncoding; + +/** + * A {@link Lucene104ScalarQuantizedVectorScorer} that scores vectors in place in the {@link + * MemorySegment} that holds them, several at a time in {@link RandomVectorScorer#bulkScore} so that + * their cache misses overlap, instead of copying each on heap first. Vectors that are not memory + * mapped in one segment are scored by the superclass. + */ +final class Lucene104MemorySegmentScalarQuantizedVectorScorer + extends Lucene104ScalarQuantizedVectorScorer { + + static final Lucene104MemorySegmentScalarQuantizedVectorScorer INSTANCE = + new Lucene104MemorySegmentScalarQuantizedVectorScorer( + Lucene99MemorySegmentFlatVectorsScorer.INSTANCE); + + private final FlatVectorsScorer nonQuantizedDelegate; + + private Lucene104MemorySegmentScalarQuantizedVectorScorer( + FlatVectorsScorer nonQuantizedDelegate) { + super(nonQuantizedDelegate); + this.nonQuantizedDelegate = nonQuantizedDelegate; + } + + @Override + public String toString() { + return "Lucene104MemorySegmentScalarQuantizedVectorScorer(nonQuantizedDelegate=" + + nonQuantizedDelegate + + ")"; + } + + @Override + public RandomVectorScorerSupplier getRandomVectorScorerSupplier( + VectorSimilarityFunction similarityFunction, KnnVectorValues vectorValues) + throws IOException { + if (vectorValues instanceof QuantizedByteVectorValues qv + && qv.getScalarEncoding().isAsymmetric() == false) { + Records records = Records.of(qv, similarityFunction); + if (records != null) { + return new OffHeapScorerSupplier(records, qv, qv); + } + } + return super.getRandomVectorScorerSupplier(similarityFunction, vectorValues); + } + + @Override + public RandomVectorScorerSupplier getRandomVectorScorerSupplier( + VectorSimilarityFunction similarityFunction, + QuantizedByteVectorValues scoringVectors, + QuantizedByteVectorValues targetVectors) + throws IOException { + if (targetVectors.getScalarEncoding().isAsymmetric()) { + Records records = Records.of(targetVectors, similarityFunction); + if (records != null) { + return new OffHeapScorerSupplier(records, targetVectors, scoringVectors); + } + } + return super.getRandomVectorScorerSupplier(similarityFunction, scoringVectors, targetVectors); + } + + @Override + protected RandomVectorScorer getQuantizedQueryScorer( + VectorSimilarityFunction similarityFunction, + QuantizedByteVectorValues qv, + byte[] quantizedQuery, + QuantizationResult queryCorrections) + throws IOException { + Records records = Records.of(qv, similarityFunction); + if (records != null) { + return QueryScorer.create(records, qv, quantizedQuery, queryCorrections); + } + return super.getQuantizedQueryScorer(similarityFunction, qv, quantizedQuery, queryCorrections); + } + + /** + * The records of one field in a segment: each is the packed vector followed by its corrective + * terms, three floats and an int. Holds values only, so one instance serves every scorer of a + * supplier and its copies. + * + * <p>The segment is that of the whole slice, like in {@link + * Lucene99MemorySegmentFlatVectorsScorer}. Scoring after the input is closed fails on the + * segment, not with an {@link org.apache.lucene.store.AlreadyClosedException}. + */ + private record Records( Review Comment: Right, this represents a quantized vector field (data, function, encoding, etc). I'll change to something like `QuantizedVectorField` and the actual memory segment to something like [`dataSeg`](https://github.com/apache/lucene/blob/781025d6fe32641b495c9dab2f43525f464f324d/lucene/core/src/java25/org/apache/lucene/internal/vectorization/MemorySegmentBulkVectorOps.java#L49). -- 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]
