|  | 
|  | 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.internal.vectorization; | 
|  | 10 | + | 
|  | 11 | +import org.apache.lucene.index.VectorSimilarityFunction; | 
|  | 12 | +import org.apache.lucene.store.IndexInput; | 
|  | 13 | +import org.apache.lucene.util.BitUtil; | 
|  | 14 | +import org.apache.lucene.util.VectorUtil; | 
|  | 15 | +import org.apache.lucene.util.quantization.OptimizedScalarQuantizer; | 
|  | 16 | + | 
|  | 17 | +import java.io.IOException; | 
|  | 18 | + | 
|  | 19 | +import static org.apache.lucene.index.VectorSimilarityFunction.EUCLIDEAN; | 
|  | 20 | +import static org.apache.lucene.index.VectorSimilarityFunction.MAXIMUM_INNER_PRODUCT; | 
|  | 21 | + | 
|  | 22 | +/** Scorer for quantized vectors stored as an {@link IndexInput}. */ | 
|  | 23 | +public class ES91OSQVectorsScorer { | 
|  | 24 | + | 
|  | 25 | +    public static final int BULK_SIZE = 16; | 
|  | 26 | + | 
|  | 27 | +    protected static final float FOUR_BIT_SCALE = 1f / ((1 << 4) - 1); | 
|  | 28 | + | 
|  | 29 | +    /** The wrapper {@link IndexInput}. */ | 
|  | 30 | +    protected final IndexInput in; | 
|  | 31 | + | 
|  | 32 | +    protected final int length; | 
|  | 33 | +    protected final int dimensions; | 
|  | 34 | + | 
|  | 35 | +    protected final float[] lowerIntervals = new float[BULK_SIZE]; | 
|  | 36 | +    protected final float[] upperIntervals = new float[BULK_SIZE]; | 
|  | 37 | +    protected final int[] targetComponentSums = new int[BULK_SIZE]; | 
|  | 38 | +    protected final float[] additionalCorrections = new float[BULK_SIZE]; | 
|  | 39 | + | 
|  | 40 | +    /** Sole constructor, called by sub-classes. */ | 
|  | 41 | +    public ES91OSQVectorsScorer(IndexInput in, int dimensions) { | 
|  | 42 | +        this.in = in; | 
|  | 43 | +        this.dimensions = dimensions; | 
|  | 44 | +        this.length = OptimizedScalarQuantizer.discretize(dimensions, 64) / 8; | 
|  | 45 | +    } | 
|  | 46 | + | 
|  | 47 | +    /** | 
|  | 48 | +     * compute the quantize distance between the provided quantized query and the quantized vector | 
|  | 49 | +     * that is read from the wrapped {@link IndexInput}. | 
|  | 50 | +     */ | 
|  | 51 | +    public long quantizeScore(byte[] q) throws IOException { | 
|  | 52 | +        assert q.length == length * 4; | 
|  | 53 | +        final int size = length; | 
|  | 54 | +        long subRet0 = 0; | 
|  | 55 | +        long subRet1 = 0; | 
|  | 56 | +        long subRet2 = 0; | 
|  | 57 | +        long subRet3 = 0; | 
|  | 58 | +        int r = 0; | 
|  | 59 | +        for (final int upperBound = size & -Long.BYTES; r < upperBound; r += Long.BYTES) { | 
|  | 60 | +            final long value = in.readLong(); | 
|  | 61 | +            subRet0 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r) & value); | 
|  | 62 | +            subRet1 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r + size) & value); | 
|  | 63 | +            subRet2 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r + 2 * size) & value); | 
|  | 64 | +            subRet3 += Long.bitCount((long) BitUtil.VH_LE_LONG.get(q, r + 3 * size) & value); | 
|  | 65 | +        } | 
|  | 66 | +        for (final int upperBound = size & -Integer.BYTES; r < upperBound; r += Integer.BYTES) { | 
|  | 67 | +            final int value = in.readInt(); | 
|  | 68 | +            subRet0 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r) & value); | 
|  | 69 | +            subRet1 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r + size) & value); | 
|  | 70 | +            subRet2 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r + 2 * size) & value); | 
|  | 71 | +            subRet3 += Integer.bitCount((int) BitUtil.VH_LE_INT.get(q, r + 3 * size) & value); | 
|  | 72 | +        } | 
|  | 73 | +        for (; r < size; r++) { | 
|  | 74 | +            final byte value = in.readByte(); | 
|  | 75 | +            subRet0 += Integer.bitCount((q[r] & value) & 0xFF); | 
|  | 76 | +            subRet1 += Integer.bitCount((q[r + size] & value) & 0xFF); | 
|  | 77 | +            subRet2 += Integer.bitCount((q[r + 2 * size] & value) & 0xFF); | 
|  | 78 | +            subRet3 += Integer.bitCount((q[r + 3 * size] & value) & 0xFF); | 
|  | 79 | +        } | 
|  | 80 | +        return subRet0 + (subRet1 << 1) + (subRet2 << 2) + (subRet3 << 3); | 
|  | 81 | +    } | 
|  | 82 | + | 
|  | 83 | +    /** | 
|  | 84 | +     * compute the quantize distance between the provided quantized query and the quantized vectors | 
|  | 85 | +     * that are read from the wrapped {@link IndexInput}. The number of quantized vectors to read is | 
|  | 86 | +     * determined by {code count} and the results are stored in the provided {@code scores} array. | 
|  | 87 | +     */ | 
|  | 88 | +    public void quantizeScoreBulk(byte[] q, int count, float[] scores) throws IOException { | 
|  | 89 | +        for (int i = 0; i < count; i++) { | 
|  | 90 | +            scores[i] = quantizeScore(q); | 
|  | 91 | +        } | 
|  | 92 | +    } | 
|  | 93 | + | 
|  | 94 | +    /** | 
|  | 95 | +     * Computes the score by applying the necessary corrections to the provided quantized distance. | 
|  | 96 | +     */ | 
|  | 97 | +    public float score( | 
|  | 98 | +        OptimizedScalarQuantizer.QuantizationResult queryCorrections, | 
|  | 99 | +        VectorSimilarityFunction similarityFunction, | 
|  | 100 | +        float centroidDp, | 
|  | 101 | +        float lowerInterval, | 
|  | 102 | +        float upperInterval, | 
|  | 103 | +        int targetComponentSum, | 
|  | 104 | +        float additionalCorrection, | 
|  | 105 | +        float qcDist | 
|  | 106 | +    ) { | 
|  | 107 | +        float ax = lowerInterval; | 
|  | 108 | +        // Here we assume `lx` is simply bit vectors, so the scaling isn't necessary | 
|  | 109 | +        float lx = upperInterval - ax; | 
|  | 110 | +        float ay = queryCorrections.lowerInterval(); | 
|  | 111 | +        float ly = (queryCorrections.upperInterval() - ay) * FOUR_BIT_SCALE; | 
|  | 112 | +        float y1 = queryCorrections.quantizedComponentSum(); | 
|  | 113 | +        float score = ax * ay * dimensions + ay * lx * (float) targetComponentSum + ax * ly * y1 + lx * ly * qcDist; | 
|  | 114 | +        // For euclidean, we need to invert the score and apply the additional correction, which is | 
|  | 115 | +        // assumed to be the squared l2norm of the centroid centered vectors. | 
|  | 116 | +        if (similarityFunction == EUCLIDEAN) { | 
|  | 117 | +            score = queryCorrections.additionalCorrection() + additionalCorrection - 2 * score; | 
|  | 118 | +            return Math.max(1 / (1f + score), 0); | 
|  | 119 | +        } else { | 
|  | 120 | +            // For cosine and max inner product, we need to apply the additional correction, which is | 
|  | 121 | +            // assumed to be the non-centered dot-product between the vector and the centroid | 
|  | 122 | +            score += queryCorrections.additionalCorrection() + additionalCorrection - centroidDp; | 
|  | 123 | +            if (similarityFunction == MAXIMUM_INNER_PRODUCT) { | 
|  | 124 | +                return VectorUtil.scaleMaxInnerProductScore(score); | 
|  | 125 | +            } | 
|  | 126 | +            return Math.max((1f + score) / 2f, 0); | 
|  | 127 | +        } | 
|  | 128 | +    } | 
|  | 129 | + | 
|  | 130 | +    /** | 
|  | 131 | +     * compute the distance between the provided quantized query and the quantized vectors that are | 
|  | 132 | +     * read from the wrapped {@link IndexInput}. | 
|  | 133 | +     * | 
|  | 134 | +     * <p>The number of vectors to score is defined by {@link #BULK_SIZE}. The expected format of the | 
|  | 135 | +     * input is as follows: First the quantized vectors are read from the input,then all the lower | 
|  | 136 | +     * intervals as floats, then all the upper intervals as floats, then all the target component sums | 
|  | 137 | +     * as shorts, and finally all the additional corrections as floats. | 
|  | 138 | +     * | 
|  | 139 | +     * <p>The results are stored in the provided scores array. | 
|  | 140 | +     */ | 
|  | 141 | +    public void scoreBulk( | 
|  | 142 | +        byte[] q, | 
|  | 143 | +        OptimizedScalarQuantizer.QuantizationResult queryCorrections, | 
|  | 144 | +        VectorSimilarityFunction similarityFunction, | 
|  | 145 | +        float centroidDp, | 
|  | 146 | +        float[] scores | 
|  | 147 | +    ) throws IOException { | 
|  | 148 | +        quantizeScoreBulk(q, BULK_SIZE, scores); | 
|  | 149 | +        in.readFloats(lowerIntervals, 0, BULK_SIZE); | 
|  | 150 | +        in.readFloats(upperIntervals, 0, BULK_SIZE); | 
|  | 151 | +        for (int i = 0; i < BULK_SIZE; i++) { | 
|  | 152 | +            targetComponentSums[i] = Short.toUnsignedInt(in.readShort()); | 
|  | 153 | +        } | 
|  | 154 | +        in.readFloats(additionalCorrections, 0, BULK_SIZE); | 
|  | 155 | +        for (int i = 0; i < BULK_SIZE; i++) { | 
|  | 156 | +            scores[i] = score( | 
|  | 157 | +                queryCorrections, | 
|  | 158 | +                similarityFunction, | 
|  | 159 | +                centroidDp, | 
|  | 160 | +                lowerIntervals[i], | 
|  | 161 | +                upperIntervals[i], | 
|  | 162 | +                targetComponentSums[i], | 
|  | 163 | +                additionalCorrections[i], | 
|  | 164 | +                scores[i] | 
|  | 165 | +            ); | 
|  | 166 | +        } | 
|  | 167 | +    } | 
|  | 168 | +} | 
0 commit comments