|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.lucene.backward_codecs.lucene102; |
| 18 | + |
| 19 | +import static org.apache.lucene.backward_codecs.lucene102.Lucene102BinaryQuantizedVectorsFormat.QUERY_BITS; |
| 20 | +import static org.apache.lucene.index.VectorSimilarityFunction.COSINE; |
| 21 | +import static org.apache.lucene.index.VectorSimilarityFunction.EUCLIDEAN; |
| 22 | +import static org.apache.lucene.index.VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT; |
| 23 | +import static org.apache.lucene.util.quantization.OptimizedScalarQuantizer.transposeHalfByte; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import org.apache.lucene.codecs.hnsw.FlatVectorsScorer; |
| 27 | +import org.apache.lucene.index.KnnVectorValues; |
| 28 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 29 | +import org.apache.lucene.util.ArrayUtil; |
| 30 | +import org.apache.lucene.util.VectorUtil; |
| 31 | +import org.apache.lucene.util.hnsw.RandomVectorScorer; |
| 32 | +import org.apache.lucene.util.hnsw.RandomVectorScorerSupplier; |
| 33 | +import org.apache.lucene.util.quantization.OptimizedScalarQuantizer; |
| 34 | +import org.apache.lucene.util.quantization.OptimizedScalarQuantizer.QuantizationResult; |
| 35 | + |
| 36 | +/** Vector scorer over binarized vector values */ |
| 37 | +public class Lucene102BinaryFlatVectorsScorer implements FlatVectorsScorer { |
| 38 | + /** The delegate scorer for non-quantized vectors */ |
| 39 | + protected final FlatVectorsScorer nonQuantizedDelegate; |
| 40 | + |
| 41 | + /** Scaling factor for 4-bit quantization */ |
| 42 | + protected static final float FOUR_BIT_SCALE = 1f / ((1 << 4) - 1); |
| 43 | + |
| 44 | + /** |
| 45 | + * Construct a new scorer |
| 46 | + * |
| 47 | + * @param nonQuantizedDelegate the delegate scorer for non-quantized vectors |
| 48 | + */ |
| 49 | + public Lucene102BinaryFlatVectorsScorer(FlatVectorsScorer nonQuantizedDelegate) { |
| 50 | + this.nonQuantizedDelegate = nonQuantizedDelegate; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public RandomVectorScorerSupplier getRandomVectorScorerSupplier( |
| 55 | + VectorSimilarityFunction similarityFunction, KnnVectorValues vectorValues) |
| 56 | + throws IOException { |
| 57 | + throw new UnsupportedOperationException("Old codecs may only be used for reading"); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public RandomVectorScorer getRandomVectorScorer( |
| 62 | + VectorSimilarityFunction similarityFunction, KnnVectorValues vectorValues, float[] target) |
| 63 | + throws IOException { |
| 64 | + if (vectorValues instanceof BinarizedByteVectorValues binarizedVectors) { |
| 65 | + OptimizedScalarQuantizer quantizer = binarizedVectors.getQuantizer(); |
| 66 | + float[] centroid = binarizedVectors.getCentroid(); |
| 67 | + // We make a copy as the quantization process mutates the input |
| 68 | + float[] copy = ArrayUtil.copyOfSubArray(target, 0, target.length); |
| 69 | + if (similarityFunction == COSINE) { |
| 70 | + VectorUtil.l2normalize(copy); |
| 71 | + } |
| 72 | + target = copy; |
| 73 | + byte[] initial = new byte[target.length]; |
| 74 | + byte[] quantized = new byte[QUERY_BITS * binarizedVectors.discretizedDimensions() / 8]; |
| 75 | + QuantizationResult queryCorrections = |
| 76 | + quantizer.scalarQuantize(target, initial, (byte) 4, centroid); |
| 77 | + transposeHalfByte(initial, quantized); |
| 78 | + return new RandomVectorScorer.AbstractRandomVectorScorer(binarizedVectors) { |
| 79 | + @Override |
| 80 | + public float score(int node) throws IOException { |
| 81 | + return quantizedScore( |
| 82 | + quantized, queryCorrections, binarizedVectors, node, similarityFunction); |
| 83 | + } |
| 84 | + }; |
| 85 | + } |
| 86 | + return nonQuantizedDelegate.getRandomVectorScorer(similarityFunction, vectorValues, target); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public RandomVectorScorer getRandomVectorScorer( |
| 91 | + VectorSimilarityFunction similarityFunction, KnnVectorValues vectorValues, byte[] target) |
| 92 | + throws IOException { |
| 93 | + return nonQuantizedDelegate.getRandomVectorScorer(similarityFunction, vectorValues, target); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public String toString() { |
| 98 | + return "Lucene102BinaryFlatVectorsScorer(nonQuantizedDelegate=" + nonQuantizedDelegate + ")"; |
| 99 | + } |
| 100 | + |
| 101 | + static float quantizedScore( |
| 102 | + byte[] quantizedQuery, |
| 103 | + QuantizationResult queryCorrections, |
| 104 | + BinarizedByteVectorValues targetVectors, |
| 105 | + int targetOrd, |
| 106 | + VectorSimilarityFunction similarityFunction) |
| 107 | + throws IOException { |
| 108 | + byte[] binaryCode = targetVectors.vectorValue(targetOrd); |
| 109 | + float qcDist = VectorUtil.int4BitDotProduct(quantizedQuery, binaryCode); |
| 110 | + QuantizationResult indexCorrections = targetVectors.getCorrectiveTerms(targetOrd); |
| 111 | + float x1 = indexCorrections.quantizedComponentSum(); |
| 112 | + float ax = indexCorrections.lowerInterval(); |
| 113 | + // Here we assume `lx` is simply bit vectors, so the scaling isn't necessary |
| 114 | + float lx = indexCorrections.upperInterval() - ax; |
| 115 | + float ay = queryCorrections.lowerInterval(); |
| 116 | + float ly = (queryCorrections.upperInterval() - ay) * FOUR_BIT_SCALE; |
| 117 | + float y1 = queryCorrections.quantizedComponentSum(); |
| 118 | + float score = |
| 119 | + ax * ay * targetVectors.dimension() + ay * lx * x1 + ax * ly * y1 + lx * ly * qcDist; |
| 120 | + // For euclidean, we need to invert the score and apply the additional correction, which is |
| 121 | + // assumed to be the squared l2norm of the centroid centered vectors. |
| 122 | + if (similarityFunction == EUCLIDEAN) { |
| 123 | + score = |
| 124 | + queryCorrections.additionalCorrection() |
| 125 | + + indexCorrections.additionalCorrection() |
| 126 | + - 2 * score; |
| 127 | + return Math.max(1 / (1f + score), 0); |
| 128 | + } else { |
| 129 | + // For cosine and max inner product, we need to apply the additional correction, which is |
| 130 | + // assumed to be the non-centered dot-product between the vector and the centroid |
| 131 | + score += |
| 132 | + queryCorrections.additionalCorrection() |
| 133 | + + indexCorrections.additionalCorrection() |
| 134 | + - targetVectors.getCentroidDP(); |
| 135 | + if (similarityFunction == MAXIMUM_INNER_PRODUCT) { |
| 136 | + return VectorUtil.scaleMaxInnerProductScore(score); |
| 137 | + } |
| 138 | + return Math.max((1f + score) / 2f, 0); |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments