jpountz commented on code in PR #13288: URL: https://github.com/apache/lucene/pull/13288#discussion_r1562108834
########## lucene/core/src/java/org/apache/lucene/codecs/hnsw/FlatBitVectorsScorer.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.hnsw; + +import java.io.IOException; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.util.Bits; +import org.apache.lucene.util.VectorUtil; +import org.apache.lucene.util.hnsw.RandomAccessVectorValues; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; + +/** A bit vector scorer for scoring byte vectors. */ +public class FlatBitVectorsScorer implements FlatVectorsScorer { + @Override + public RandomVectorScorerSupplier getRandomVectorScorerSupplier( + VectorSimilarityFunction similarityFunction, RandomAccessVectorValues vectorValues) + throws IOException { + assert vectorValues instanceof RandomAccessVectorValues.Bytes; + if (vectorValues instanceof RandomAccessVectorValues.Bytes byteVectorValues) { + return new BitRandomVectorScorerSupplier(byteVectorValues); + } + throw new IllegalArgumentException( + "vectorValues must be an instance of RandomAccessVectorValues.Bytes"); + } + + @Override + public RandomVectorScorer getRandomVectorScorer( + VectorSimilarityFunction similarityFunction, + RandomAccessVectorValues vectorValues, + float[] target) + throws IOException { + throw new IllegalArgumentException("bit vectors do not support float[] targets"); + } + + @Override + public RandomVectorScorer getRandomVectorScorer( + VectorSimilarityFunction similarityFunction, + RandomAccessVectorValues vectorValues, + byte[] target) + throws IOException { + assert vectorValues instanceof RandomAccessVectorValues.Bytes; + if (vectorValues instanceof RandomAccessVectorValues.Bytes byteVectorValues) { + return new BitRandomVectorScorer(byteVectorValues, target); + } + throw new IllegalArgumentException( + "vectorValues must be an instance of RandomAccessVectorValues.Bytes"); + } + + static class BitRandomVectorScorer implements RandomVectorScorer { + private final RandomAccessVectorValues.Bytes vectorValues; + private final int bitDimensions; + private final byte[] query; + + BitRandomVectorScorer(RandomAccessVectorValues.Bytes vectorValues, byte[] query) { + this.query = query; + this.bitDimensions = vectorValues.dimension() * 8; Review Comment: nit: reuse the Byte.SIZE constant? ########## lucene/core/src/java/module-info.java: ########## @@ -72,7 +73,8 @@ org.apache.lucene.codecs.lucene90.Lucene90DocValuesFormat; provides org.apache.lucene.codecs.KnnVectorsFormat with org.apache.lucene.codecs.lucene99.Lucene99HnswVectorsFormat, - org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat; + org.apache.lucene.codecs.lucene99.Lucene99HnswScalarQuantizedVectorsFormat, + org.apache.lucene.codecs.lucene99.Lucene99HnswBitVectorsFormat; Review Comment: could we have it in lucene/codecs instead? ########## lucene/codecs/src/java/org/apache/lucene/codecs/simpletext/SimpleTextKnnVectorsReader.java: ########## @@ -472,9 +472,8 @@ private void readVector(byte[] value) throws IOException { } @Override - public BytesRef vectorValue(int targetOrd) throws IOException { - binaryValue.bytes = values[curOrd]; - return binaryValue; + public byte[] vectorValue(int targetOrd) throws IOException { + return values[curOrd]; Review Comment: Unrelated to your change, but it's surprising that this is using `curOrd` and not `targetOrd`. ########## lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.hnsw; + +import java.io.IOException; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.util.hnsw.RandomAccessVectorValues; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; +import org.apache.lucene.util.quantization.RandomAccessQuantizedByteVectorValues; +import org.apache.lucene.util.quantization.ScalarQuantizedRandomVectorScorer; +import org.apache.lucene.util.quantization.ScalarQuantizedRandomVectorScorerSupplier; +import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity; +import org.apache.lucene.util.quantization.ScalarQuantizer; + +/** On-heap scalar quantized implementation of {@link FlatVectorsScorer}. */ Review Comment: Similar comment here about "on-heap". ########## lucene/core/src/java/org/apache/lucene/codecs/hnsw/DefaultFlatVectorScorer.java: ########## @@ -0,0 +1,67 @@ +/* + * 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.hnsw; + +import java.io.IOException; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.util.hnsw.RandomAccessVectorValues; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; + +/** On-heap implementation of {@link FlatVectorsScorer}. */ Review Comment: It's unclear to me why these docs say on-heap, it seems like this works with arbitrary impls of `RandomAccessVectorValues`, including ones that store data off-heap? ########## lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java: ########## @@ -0,0 +1,92 @@ +/* + * 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.hnsw; + +import java.io.IOException; +import org.apache.lucene.index.VectorSimilarityFunction; +import org.apache.lucene.util.hnsw.RandomAccessVectorValues; +import org.apache.lucene.util.hnsw.RandomVectorScorer; +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; +import org.apache.lucene.util.quantization.RandomAccessQuantizedByteVectorValues; +import org.apache.lucene.util.quantization.ScalarQuantizedRandomVectorScorer; +import org.apache.lucene.util.quantization.ScalarQuantizedRandomVectorScorerSupplier; +import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity; +import org.apache.lucene.util.quantization.ScalarQuantizer; + +/** On-heap scalar quantized implementation of {@link FlatVectorsScorer}. */ +public class ScalarQuantizedVectorScorer implements FlatVectorsScorer { + + private final FlatVectorsScorer nonQuantizedDelegate; + + public ScalarQuantizedVectorScorer(FlatVectorsScorer flatVectorsScorer) { + nonQuantizedDelegate = flatVectorsScorer; + } + + @Override + public RandomVectorScorerSupplier getRandomVectorScorerSupplier( + VectorSimilarityFunction similarityFunction, RandomAccessVectorValues vectorValues) + throws IOException { + if (vectorValues instanceof RandomAccessQuantizedByteVectorValues quantizedByteVectorValues) { + return new ScalarQuantizedRandomVectorScorerSupplier( + similarityFunction, + quantizedByteVectorValues.getScalarQuantizer(), + quantizedByteVectorValues); + } + return nonQuantizedDelegate.getRandomVectorScorerSupplier(similarityFunction, vectorValues); Review Comment: can you add a comment that explains when we hit this `else` case? ########## lucene/core/src/java/org/apache/lucene/util/FixedBitSet.java: ########## @@ -128,9 +128,9 @@ public FixedBitSet(int numBits) { } /** - * Creates a new FixedBitSet using the provided long[] array as backing store. The storedBits array - * must be large enough to accommodate the numBits specified, but may be larger. In that case the - * 'extra' or 'ghost' bits must be clear (or they may provoke spurious side-effects) + * Creates a new FixedBitSet using the provided long[] array as backing store. The storedBits + * array must be large enough to accommodate the numBits specified, but may be larger. In that + * case the 'extra' or 'ghost' bits must be clear (or they may provoke spurious side-effects) Review Comment: Merging `main` should hopefully remove this bit from the diff. -- 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: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org