|
| 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; |
| 11 | + |
| 12 | +import org.apache.lucene.index.VectorSimilarityFunction; |
| 13 | +import org.elasticsearch.common.logging.LogConfigurator; |
| 14 | +import org.elasticsearch.index.codec.vectors.OptimizedScalarQuantizer; |
| 15 | +import org.openjdk.jmh.annotations.Benchmark; |
| 16 | +import org.openjdk.jmh.annotations.BenchmarkMode; |
| 17 | +import org.openjdk.jmh.annotations.Fork; |
| 18 | +import org.openjdk.jmh.annotations.Level; |
| 19 | +import org.openjdk.jmh.annotations.Measurement; |
| 20 | +import org.openjdk.jmh.annotations.Mode; |
| 21 | +import org.openjdk.jmh.annotations.OutputTimeUnit; |
| 22 | +import org.openjdk.jmh.annotations.Param; |
| 23 | +import org.openjdk.jmh.annotations.Scope; |
| 24 | +import org.openjdk.jmh.annotations.Setup; |
| 25 | +import org.openjdk.jmh.annotations.State; |
| 26 | +import org.openjdk.jmh.annotations.Warmup; |
| 27 | + |
| 28 | +import java.util.concurrent.ThreadLocalRandom; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | + |
| 31 | +@BenchmarkMode(Mode.Throughput) |
| 32 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 33 | +@State(Scope.Benchmark) |
| 34 | +@Warmup(iterations = 3, time = 1) |
| 35 | +@Measurement(iterations = 5, time = 1) |
| 36 | +@Fork(value = 3) |
| 37 | +public class OptimizedScalarQuantizerBenchmark { |
| 38 | + static { |
| 39 | + LogConfigurator.configureESLogging(); // native access requires logging to be initialized |
| 40 | + } |
| 41 | + @Param({ "384", "702", "1024" }) |
| 42 | + int dims; |
| 43 | + |
| 44 | + float[] vector; |
| 45 | + float[] centroid; |
| 46 | + byte[] destination; |
| 47 | + |
| 48 | + @Param({ "1", "4", "7" }) |
| 49 | + byte bits; |
| 50 | + |
| 51 | + OptimizedScalarQuantizer osq = new OptimizedScalarQuantizer(VectorSimilarityFunction.DOT_PRODUCT); |
| 52 | + |
| 53 | + @Setup(Level.Iteration) |
| 54 | + public void init() { |
| 55 | + ThreadLocalRandom random = ThreadLocalRandom.current(); |
| 56 | + // random byte arrays for binary methods |
| 57 | + destination = new byte[dims]; |
| 58 | + vector = new float[dims]; |
| 59 | + centroid = new float[dims]; |
| 60 | + for (int i = 0; i < dims; ++i) { |
| 61 | + vector[i] = random.nextFloat(); |
| 62 | + centroid[i] = random.nextFloat(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Benchmark |
| 67 | + public byte[] scalar() { |
| 68 | + osq.scalarQuantize(vector, destination, bits, centroid); |
| 69 | + return destination; |
| 70 | + } |
| 71 | + |
| 72 | + @Benchmark |
| 73 | + @Fork(jvmArgsPrepend = { "--add-modules=jdk.incubator.vector" }) |
| 74 | + public byte[] vector() { |
| 75 | + osq.scalarQuantize(vector, destination, bits, centroid); |
| 76 | + return destination; |
| 77 | + } |
| 78 | +} |
0 commit comments