|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the "Elastic License |
| 4 | + * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side |
| 5 | + * Public License v 1"; you may not use this file except in compliance with, at |
| 6 | + * your election, the "Elastic License 2.0", the "GNU Affero General Public |
| 7 | + * License v3.0 only", or the "Server Side Public License, v 1". |
| 8 | + */ |
| 9 | +package org.elasticsearch.simdvec; |
| 10 | + |
| 11 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 12 | +import org.apache.lucene.store.IndexInput; |
| 13 | +import org.apache.lucene.util.VectorUtil; |
| 14 | + |
| 15 | +import java.io.IOException; |
| 16 | + |
| 17 | +import static org.apache.lucene.index.VectorSimilarityFunction.EUCLIDEAN; |
| 18 | +import static org.apache.lucene.index.VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT; |
| 19 | + |
| 20 | +/** |
| 21 | + * Scorer for 7 bit quantized vectors stored in a {@link IndexInput}. |
| 22 | + * Queries are expected to be quantized using 7 bits as well. |
| 23 | + * */ |
| 24 | +public class ES92Int7VectorsScorer { |
| 25 | + |
| 26 | + public static final int BULK_SIZE = 16; |
| 27 | + protected static final float SEVEN_BIT_SCALE = 1f / ((1 << 7) - 1); |
| 28 | + |
| 29 | + /** The wrapper {@link IndexInput}. */ |
| 30 | + protected final IndexInput in; |
| 31 | + protected final int dimensions; |
| 32 | + |
| 33 | + private final float[] lowerIntervals = new float[BULK_SIZE]; |
| 34 | + private final float[] upperIntervals = new float[BULK_SIZE]; |
| 35 | + private final int[] targetComponentSums = new int[BULK_SIZE]; |
| 36 | + private final float[] additionalCorrections = new float[BULK_SIZE]; |
| 37 | + |
| 38 | + /** Sole constructor, called by sub-classes. */ |
| 39 | + public ES92Int7VectorsScorer(IndexInput in, int dimensions) { |
| 40 | + this.in = in; |
| 41 | + this.dimensions = dimensions; |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * compute the quantize distance between the provided quantized query and the quantized vector |
| 46 | + * that is read from the wrapped {@link IndexInput}. |
| 47 | + */ |
| 48 | + public long int7DotProduct(byte[] b) throws IOException { |
| 49 | + int total = 0; |
| 50 | + for (int i = 0; i < dimensions; i++) { |
| 51 | + total += in.readByte() * b[i]; |
| 52 | + } |
| 53 | + return total; |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * compute the quantize distance between the provided quantized query and the quantized vectors |
| 58 | + * that are read from the wrapped {@link IndexInput}. The number of quantized vectors to read is |
| 59 | + * determined by {code count} and the results are stored in the provided {@code scores} array. |
| 60 | + */ |
| 61 | + public void int7DotProductBulk(byte[] b, int count, float[] scores) throws IOException { |
| 62 | + for (int i = 0; i < count; i++) { |
| 63 | + scores[i] = int7DotProduct(b); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Computes the score by applying the necessary corrections to the provided quantized distance. |
| 69 | + */ |
| 70 | + public float score( |
| 71 | + byte[] q, |
| 72 | + float queryLowerInterval, |
| 73 | + float queryUpperInterval, |
| 74 | + int queryComponentSum, |
| 75 | + float queryAdditionalCorrection, |
| 76 | + VectorSimilarityFunction similarityFunction, |
| 77 | + float centroidDp |
| 78 | + ) throws IOException { |
| 79 | + float score = int7DotProduct(q); |
| 80 | + in.readFloats(lowerIntervals, 0, 3); |
| 81 | + int addition = in.readInt(); |
| 82 | + return applyCorrections( |
| 83 | + queryLowerInterval, |
| 84 | + queryUpperInterval, |
| 85 | + queryComponentSum, |
| 86 | + queryAdditionalCorrection, |
| 87 | + similarityFunction, |
| 88 | + centroidDp, |
| 89 | + lowerIntervals[0], |
| 90 | + lowerIntervals[1], |
| 91 | + addition, |
| 92 | + lowerIntervals[2], |
| 93 | + score |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * compute the distance between the provided quantized query and the quantized vectors that are |
| 99 | + * read from the wrapped {@link IndexInput}. |
| 100 | + * |
| 101 | + * <p>The number of vectors to score is defined by {@link #BULK_SIZE}. The expected format of the |
| 102 | + * input is as follows: First the quantized vectors are read from the input,then all the lower |
| 103 | + * intervals as floats, then all the upper intervals as floats, then all the target component sums |
| 104 | + * as shorts, and finally all the additional corrections as floats. |
| 105 | + * |
| 106 | + * <p>The results are stored in the provided scores array. |
| 107 | + */ |
| 108 | + public void scoreBulk( |
| 109 | + byte[] q, |
| 110 | + float queryLowerInterval, |
| 111 | + float queryUpperInterval, |
| 112 | + int queryComponentSum, |
| 113 | + float queryAdditionalCorrection, |
| 114 | + VectorSimilarityFunction similarityFunction, |
| 115 | + float centroidDp, |
| 116 | + float[] scores |
| 117 | + ) throws IOException { |
| 118 | + int7DotProductBulk(q, BULK_SIZE, scores); |
| 119 | + in.readFloats(lowerIntervals, 0, BULK_SIZE); |
| 120 | + in.readFloats(upperIntervals, 0, BULK_SIZE); |
| 121 | + in.readInts(targetComponentSums, 0, BULK_SIZE); |
| 122 | + in.readFloats(additionalCorrections, 0, BULK_SIZE); |
| 123 | + for (int i = 0; i < BULK_SIZE; i++) { |
| 124 | + scores[i] = applyCorrections( |
| 125 | + queryLowerInterval, |
| 126 | + queryUpperInterval, |
| 127 | + queryComponentSum, |
| 128 | + queryAdditionalCorrection, |
| 129 | + similarityFunction, |
| 130 | + centroidDp, |
| 131 | + lowerIntervals[i], |
| 132 | + upperIntervals[i], |
| 133 | + targetComponentSums[i], |
| 134 | + additionalCorrections[i], |
| 135 | + scores[i] |
| 136 | + ); |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Computes the score by applying the necessary corrections to the provided quantized distance. |
| 142 | + */ |
| 143 | + public float applyCorrections( |
| 144 | + float queryLowerInterval, |
| 145 | + float queryUpperInterval, |
| 146 | + int queryComponentSum, |
| 147 | + float queryAdditionalCorrection, |
| 148 | + VectorSimilarityFunction similarityFunction, |
| 149 | + float centroidDp, |
| 150 | + float lowerInterval, |
| 151 | + float upperInterval, |
| 152 | + int targetComponentSum, |
| 153 | + float additionalCorrection, |
| 154 | + float qcDist |
| 155 | + ) { |
| 156 | + float ax = lowerInterval; |
| 157 | + // Here we assume `lx` is simply bit vectors, so the scaling isn't necessary |
| 158 | + float lx = (upperInterval - ax) * SEVEN_BIT_SCALE; |
| 159 | + float ay = queryLowerInterval; |
| 160 | + float ly = (queryUpperInterval - ay) * SEVEN_BIT_SCALE; |
| 161 | + float y1 = queryComponentSum; |
| 162 | + float score = ax * ay * dimensions + ay * lx * (float) targetComponentSum + ax * ly * y1 + lx * ly * qcDist; |
| 163 | + // For euclidean, we need to invert the score and apply the additional correction, which is |
| 164 | + // assumed to be the squared l2norm of the centroid centered vectors. |
| 165 | + if (similarityFunction == EUCLIDEAN) { |
| 166 | + score = queryAdditionalCorrection + additionalCorrection - 2 * score; |
| 167 | + return Math.max(1 / (1f + score), 0); |
| 168 | + } else { |
| 169 | + // For cosine and max inner product, we need to apply the additional correction, which is |
| 170 | + // assumed to be the non-centered dot-product between the vector and the centroid |
| 171 | + score += queryAdditionalCorrection + additionalCorrection - centroidDp; |
| 172 | + if (similarityFunction == MAXIMUM_INNER_PRODUCT) { |
| 173 | + return VectorUtil.scaleMaxInnerProductScore(score); |
| 174 | + } |
| 175 | + return Math.max((1f + score) / 2f, 0); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments