|
| 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 | + |
| 10 | +package org.elasticsearch.benchmark.vector.scorer; |
| 11 | + |
| 12 | +import org.apache.lucene.codecs.lucene104.Lucene104ScalarQuantizedVectorsFormat; |
| 13 | +import org.apache.lucene.codecs.lucene104.QuantizedByteVectorValues; |
| 14 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 15 | +import org.apache.lucene.search.VectorScorer; |
| 16 | +import org.apache.lucene.util.VectorUtil; |
| 17 | +import org.apache.lucene.util.hnsw.RandomVectorScorer; |
| 18 | +import org.apache.lucene.util.hnsw.UpdateableRandomVectorScorer; |
| 19 | +import org.apache.lucene.util.quantization.OptimizedScalarQuantizer; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.concurrent.ThreadLocalRandom; |
| 24 | + |
| 25 | +import static org.elasticsearch.benchmark.vector.scorer.ScalarOperations.applyI4Corrections; |
| 26 | +import static org.elasticsearch.benchmark.vector.scorer.ScalarOperations.dotProductI4SinglePacked; |
| 27 | +import static org.elasticsearch.simdvec.internal.vectorization.VectorScorerTestUtils.unpackNibbles; |
| 28 | + |
| 29 | +public class Int4BenchmarkUtils { |
| 30 | + |
| 31 | + /** |
| 32 | + * In-memory implementation of {@link QuantizedByteVectorValues} for int4 (PACKED_NIBBLE) benchmarks. |
| 33 | + * Stores pre-quantized packed nibble vectors with synthetic corrective terms. |
| 34 | + */ |
| 35 | + static class InMemoryInt4QuantizedByteVectorValues extends QuantizedByteVectorValues { |
| 36 | + |
| 37 | + private final int dims; |
| 38 | + private final byte[][] packedVectors; |
| 39 | + private final OptimizedScalarQuantizer.QuantizationResult[] correctiveTerms; |
| 40 | + private final float[] centroid; |
| 41 | + private final float centroidDP; |
| 42 | + private final OptimizedScalarQuantizer quantizer; |
| 43 | + |
| 44 | + InMemoryInt4QuantizedByteVectorValues( |
| 45 | + int dims, |
| 46 | + byte[][] packedVectors, |
| 47 | + OptimizedScalarQuantizer.QuantizationResult[] correctiveTerms, |
| 48 | + float[] centroid, |
| 49 | + float centroidDP |
| 50 | + ) { |
| 51 | + this.dims = dims; |
| 52 | + this.packedVectors = packedVectors; |
| 53 | + this.correctiveTerms = correctiveTerms; |
| 54 | + this.centroid = centroid; |
| 55 | + this.centroidDP = centroidDP; |
| 56 | + this.quantizer = new OptimizedScalarQuantizer(VectorSimilarityFunction.DOT_PRODUCT); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public int dimension() { |
| 61 | + return dims; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public int size() { |
| 66 | + return packedVectors.length; |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public byte[] vectorValue(int ord) throws IOException { |
| 71 | + return packedVectors[ord]; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public OptimizedScalarQuantizer.QuantizationResult getCorrectiveTerms(int vectorOrd) throws IOException { |
| 76 | + return correctiveTerms[vectorOrd]; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public OptimizedScalarQuantizer getQuantizer() { |
| 81 | + return quantizer; |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding getScalarEncoding() { |
| 86 | + return Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding.PACKED_NIBBLE; |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public float[] getCentroid() throws IOException { |
| 91 | + return centroid; |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public float getCentroidDP() throws IOException { |
| 96 | + return centroidDP; |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public VectorScorer scorer(float[] query) throws IOException { |
| 101 | + return null; |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public InMemoryInt4QuantizedByteVectorValues copy() throws IOException { |
| 106 | + return new InMemoryInt4QuantizedByteVectorValues(dims, packedVectors, correctiveTerms, centroid, centroidDP); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private static class ScalarScorer implements UpdateableRandomVectorScorer { |
| 111 | + private final QuantizedByteVectorValues values; |
| 112 | + private final int dims; |
| 113 | + private final VectorSimilarityFunction similarityFunction; |
| 114 | + |
| 115 | + private byte[] queryUnpacked; |
| 116 | + private OptimizedScalarQuantizer.QuantizationResult queryCorrections; |
| 117 | + |
| 118 | + ScalarScorer(QuantizedByteVectorValues values, VectorSimilarityFunction similarityFunction) { |
| 119 | + this.values = values; |
| 120 | + this.dims = values.dimension(); |
| 121 | + this.similarityFunction = similarityFunction; |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public float score(int node) throws IOException { |
| 126 | + byte[] packed = values.vectorValue(node); |
| 127 | + int rawDot = dotProductI4SinglePacked(queryUnpacked, packed); |
| 128 | + var nodeCorrections = values.getCorrectiveTerms(node); |
| 129 | + return applyI4Corrections(rawDot, dims, nodeCorrections, queryCorrections, values.getCentroidDP(), similarityFunction); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public int maxOrd() { |
| 134 | + return values.size(); |
| 135 | + } |
| 136 | + |
| 137 | + @Override |
| 138 | + public void setScoringOrdinal(int node) throws IOException { |
| 139 | + byte[] packed = values.vectorValue(node); |
| 140 | + queryUnpacked = unpackNibbles(packed, dims); |
| 141 | + queryCorrections = values.getCorrectiveTerms(node); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + static QuantizedByteVectorValues createI4QuantizedVectorValues(int dims, byte[][] packedVectors) { |
| 146 | + var random = ThreadLocalRandom.current(); |
| 147 | + var correctiveTerms = new OptimizedScalarQuantizer.QuantizationResult[packedVectors.length]; |
| 148 | + for (int i = 0; i < packedVectors.length; i++) { |
| 149 | + correctiveTerms[i] = new OptimizedScalarQuantizer.QuantizationResult( |
| 150 | + random.nextFloat(-1f, 1f), |
| 151 | + random.nextFloat(-1f, 1f), |
| 152 | + random.nextFloat(-1f, 1f), |
| 153 | + random.nextInt(0, dims * 15) |
| 154 | + ); |
| 155 | + } |
| 156 | + float[] centroid = new float[dims]; |
| 157 | + for (int i = 0; i < dims; i++) { |
| 158 | + centroid[i] = random.nextFloat(); |
| 159 | + } |
| 160 | + float centroidDP = random.nextFloat(); |
| 161 | + return new InMemoryInt4QuantizedByteVectorValues(dims, packedVectors, correctiveTerms, centroid, centroidDP); |
| 162 | + } |
| 163 | + |
| 164 | + static UpdateableRandomVectorScorer createI4ScalarScorer( |
| 165 | + QuantizedByteVectorValues values, |
| 166 | + VectorSimilarityFunction similarityFunction |
| 167 | + ) { |
| 168 | + return new ScalarScorer(values, similarityFunction); |
| 169 | + } |
| 170 | + |
| 171 | + static RandomVectorScorer createI4ScalarQueryScorer( |
| 172 | + QuantizedByteVectorValues values, |
| 173 | + VectorSimilarityFunction similarityFunction, |
| 174 | + float[] queryVector |
| 175 | + ) throws IOException { |
| 176 | + int dims = values.dimension(); |
| 177 | + OptimizedScalarQuantizer quantizer = values.getQuantizer(); |
| 178 | + float[] centroid = values.getCentroid(); |
| 179 | + Lucene104ScalarQuantizedVectorsFormat.ScalarEncoding encoding = values.getScalarEncoding(); |
| 180 | + |
| 181 | + byte[] queryQuantized = new byte[encoding.getDiscreteDimensions(dims)]; |
| 182 | + float[] queryCopy = Arrays.copyOf(queryVector, queryVector.length); |
| 183 | + if (similarityFunction == VectorSimilarityFunction.COSINE) { |
| 184 | + VectorUtil.l2normalize(queryCopy); |
| 185 | + } |
| 186 | + var queryCorrections = quantizer.scalarQuantize(queryCopy, queryQuantized, encoding.getQueryBits(), centroid); |
| 187 | + float centroidDP = values.getCentroidDP(); |
| 188 | + |
| 189 | + return new RandomVectorScorer.AbstractRandomVectorScorer(values) { |
| 190 | + @Override |
| 191 | + public float score(int node) throws IOException { |
| 192 | + byte[] packed = values.vectorValue(node); |
| 193 | + int rawDot = dotProductI4SinglePacked(queryQuantized, packed); |
| 194 | + var nodeCorrections = values.getCorrectiveTerms(node); |
| 195 | + return applyI4Corrections(rawDot, dims, nodeCorrections, queryCorrections, centroidDP, similarityFunction); |
| 196 | + } |
| 197 | + }; |
| 198 | + } |
| 199 | +} |
0 commit comments