|
| 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 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.benchmark.vector; |
| 10 | + |
| 11 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 12 | +import org.apache.lucene.store.Directory; |
| 13 | +import org.apache.lucene.store.IOContext; |
| 14 | +import org.apache.lucene.store.IndexInput; |
| 15 | +import org.apache.lucene.store.IndexOutput; |
| 16 | +import org.apache.lucene.store.MMapDirectory; |
| 17 | +import org.apache.lucene.util.quantization.ScalarQuantizedVectorSimilarity; |
| 18 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 19 | +import org.elasticsearch.core.IOUtils; |
| 20 | +import org.elasticsearch.vec.VectorScorer; |
| 21 | +import org.elasticsearch.vec.VectorScorerFactory; |
| 22 | +import org.openjdk.jmh.annotations.Benchmark; |
| 23 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 24 | +import org.openjdk.jmh.annotations.Fork; |
| 25 | +import org.openjdk.jmh.annotations.Measurement; |
| 26 | +import org.openjdk.jmh.annotations.Mode; |
| 27 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 28 | +import org.openjdk.jmh.annotations.Param; |
| 29 | +import org.openjdk.jmh.annotations.Scope; |
| 30 | +import org.openjdk.jmh.annotations.Setup; |
| 31 | +import org.openjdk.jmh.annotations.State; |
| 32 | +import org.openjdk.jmh.annotations.TearDown; |
| 33 | +import org.openjdk.jmh.annotations.Warmup; |
| 34 | + |
| 35 | +import java.io.IOException; |
| 36 | +import java.nio.file.Files; |
| 37 | +import java.util.concurrent.ThreadLocalRandom; |
| 38 | +import java.util.concurrent.TimeUnit; |
| 39 | + |
| 40 | +import static org.elasticsearch.vec.VectorSimilarityType.DOT_PRODUCT; |
| 41 | +import static org.elasticsearch.vec.VectorSimilarityType.EUCLIDEAN; |
| 42 | + |
| 43 | +@Fork(value = 1, jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 44 | +@Warmup(iterations = 3, time = 3) |
| 45 | +@Measurement(iterations = 5, time = 3) |
| 46 | +@BenchmarkMode(Mode.Throughput) |
| 47 | +@OutputTimeUnit(TimeUnit.MICROSECONDS) |
| 48 | +@State(Scope.Thread) |
| 49 | +/** |
| 50 | + * Benchmark that compares various scalar quantized vector similarity function |
| 51 | + * implementations;: scalar, lucene's panama-ized, and Elasticsearch's native. |
| 52 | + * Run with ./gradlew -p benchmarks run --args 'VectorScorerBenchmark' |
| 53 | + */ |
| 54 | +public class VectorScorerBenchmark { |
| 55 | + |
| 56 | + static { |
| 57 | + LogConfigurator.configureESLogging(); // native access requires logging to be initialized |
| 58 | + } |
| 59 | + |
| 60 | + @Param({ "96", "768", "1024" }) |
| 61 | + int dims; |
| 62 | + int size = 2; // there are only two vectors to compare |
| 63 | + |
| 64 | + Directory dir; |
| 65 | + IndexInput in; |
| 66 | + VectorScorerFactory factory; |
| 67 | + |
| 68 | + byte[] vec1; |
| 69 | + byte[] vec2; |
| 70 | + float vec1Offset; |
| 71 | + float vec2Offset; |
| 72 | + float scoreCorrectionConstant; |
| 73 | + |
| 74 | + ScalarQuantizedVectorSimilarity luceneDotScorer; |
| 75 | + ScalarQuantizedVectorSimilarity luceneSqrScorer; |
| 76 | + VectorScorer nativeDotScorer; |
| 77 | + VectorScorer nativeSqrScorer; |
| 78 | + |
| 79 | + @Setup |
| 80 | + public void setup() throws IOException { |
| 81 | + var optionalVectorScorerFactory = VectorScorerFactory.instance(); |
| 82 | + if (optionalVectorScorerFactory.isEmpty()) { |
| 83 | + String msg = "JDK=[" |
| 84 | + + Runtime.version() |
| 85 | + + "], os.name=[" |
| 86 | + + System.getProperty("os.name") |
| 87 | + + "], os.arch=[" |
| 88 | + + System.getProperty("os.arch") |
| 89 | + + "]"; |
| 90 | + throw new AssertionError("Vector scorer factory not present. Cannot run the benchmark. " + msg); |
| 91 | + } |
| 92 | + factory = optionalVectorScorerFactory.get(); |
| 93 | + scoreCorrectionConstant = 1f; |
| 94 | + vec1 = new byte[dims]; |
| 95 | + vec2 = new byte[dims]; |
| 96 | + |
| 97 | + ThreadLocalRandom.current().nextBytes(vec1); |
| 98 | + ThreadLocalRandom.current().nextBytes(vec2); |
| 99 | + vec1Offset = ThreadLocalRandom.current().nextFloat(); |
| 100 | + vec2Offset = ThreadLocalRandom.current().nextFloat(); |
| 101 | + |
| 102 | + dir = new MMapDirectory(Files.createTempDirectory("nativeScalarQuantBench")); |
| 103 | + try (IndexOutput out = dir.createOutput("vector.data", IOContext.DEFAULT)) { |
| 104 | + out.writeBytes(vec1, 0, vec1.length); |
| 105 | + out.writeInt(Float.floatToIntBits(vec1Offset)); |
| 106 | + out.writeBytes(vec2, 0, vec2.length); |
| 107 | + out.writeInt(Float.floatToIntBits(vec2Offset)); |
| 108 | + } |
| 109 | + in = dir.openInput("vector.data", IOContext.DEFAULT); |
| 110 | + |
| 111 | + luceneDotScorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity( |
| 112 | + VectorSimilarityFunction.DOT_PRODUCT, |
| 113 | + scoreCorrectionConstant |
| 114 | + ); |
| 115 | + luceneSqrScorer = ScalarQuantizedVectorSimilarity.fromVectorSimilarity(VectorSimilarityFunction.EUCLIDEAN, scoreCorrectionConstant); |
| 116 | + nativeDotScorer = factory.getScalarQuantizedVectorScorer(dims, size, scoreCorrectionConstant, DOT_PRODUCT, in).get(); |
| 117 | + nativeSqrScorer = factory.getScalarQuantizedVectorScorer(dims, size, scoreCorrectionConstant, EUCLIDEAN, in).get(); |
| 118 | + |
| 119 | + // sanity |
| 120 | + var f1 = dotProductLucene(); |
| 121 | + var f2 = dotProductNative(); |
| 122 | + var f3 = dotProductScalar(); |
| 123 | + if (f1 != f2) { |
| 124 | + throw new AssertionError("lucene[" + f1 + "] != " + "native[" + f2 + "]"); |
| 125 | + } |
| 126 | + if (f1 != f3) { |
| 127 | + throw new AssertionError("lucene[" + f1 + "] != " + "scalar[" + f3 + "]"); |
| 128 | + } |
| 129 | + // square distance |
| 130 | + f1 = squareDistanceLucene(); |
| 131 | + f2 = squareDistanceNative(); |
| 132 | + f3 = squareDistanceScalar(); |
| 133 | + if (f1 != f2) { |
| 134 | + throw new AssertionError("lucene[" + f1 + "] != " + "native[" + f2 + "]"); |
| 135 | + } |
| 136 | + if (f1 != f3) { |
| 137 | + throw new AssertionError("lucene[" + f1 + "] != " + "scalar[" + f3 + "]"); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + @TearDown |
| 142 | + public void teardown() throws IOException { |
| 143 | + IOUtils.close(dir, in); |
| 144 | + } |
| 145 | + |
| 146 | + @Benchmark |
| 147 | + public float dotProductLucene() { |
| 148 | + return luceneDotScorer.score(vec1, vec1Offset, vec2, vec2Offset); |
| 149 | + } |
| 150 | + |
| 151 | + @Benchmark |
| 152 | + public float dotProductNative() throws IOException { |
| 153 | + return nativeDotScorer.score(0, 1); |
| 154 | + } |
| 155 | + |
| 156 | + @Benchmark |
| 157 | + public float dotProductScalar() { |
| 158 | + int dotProduct = 0; |
| 159 | + for (int i = 0; i < vec1.length; i++) { |
| 160 | + dotProduct += vec1[i] * vec2[i]; |
| 161 | + } |
| 162 | + float adjustedDistance = dotProduct * scoreCorrectionConstant + vec1Offset + vec2Offset; |
| 163 | + return (1 + adjustedDistance) / 2; |
| 164 | + } |
| 165 | + |
| 166 | + // -- square distance |
| 167 | + |
| 168 | + @Benchmark |
| 169 | + public float squareDistanceLucene() { |
| 170 | + return luceneSqrScorer.score(vec1, vec1Offset, vec2, vec2Offset); |
| 171 | + } |
| 172 | + |
| 173 | + @Benchmark |
| 174 | + public float squareDistanceNative() throws IOException { |
| 175 | + return nativeSqrScorer.score(0, 1); |
| 176 | + } |
| 177 | + |
| 178 | + @Benchmark |
| 179 | + public float squareDistanceScalar() { |
| 180 | + int squareDistance = 0; |
| 181 | + for (int i = 0; i < vec1.length; i++) { |
| 182 | + int diff = vec1[i] - vec2[i]; |
| 183 | + squareDistance += diff * diff; |
| 184 | + } |
| 185 | + float adjustedDistance = squareDistance * scoreCorrectionConstant; |
| 186 | + return 1 / (1f + adjustedDistance); |
| 187 | + } |
| 188 | +} |
0 commit comments