|
| 1 | +/* |
| 2 | + * Copyright DataStax, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.github.jbellis.jvector.bench; |
| 17 | + |
| 18 | +import io.github.jbellis.jvector.example.SiftSmall; |
| 19 | +import io.github.jbellis.jvector.example.util.SiftLoader; |
| 20 | +import io.github.jbellis.jvector.graph.*; |
| 21 | +import io.github.jbellis.jvector.graph.similarity.BuildScoreProvider; |
| 22 | +import io.github.jbellis.jvector.util.Bits; |
| 23 | +import io.github.jbellis.jvector.vector.VectorSimilarityFunction; |
| 24 | +import io.github.jbellis.jvector.vector.types.VectorFloat; |
| 25 | +import org.openjdk.jmh.annotations.*; |
| 26 | +import org.openjdk.jmh.infra.Blackhole; |
| 27 | +import org.slf4j.Logger; |
| 28 | +import org.slf4j.LoggerFactory; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +@BenchmarkMode(Mode.AverageTime) |
| 36 | +@OutputTimeUnit(TimeUnit.MILLISECONDS) |
| 37 | +@State(Scope.Thread) |
| 38 | +@Fork(1) |
| 39 | +@Warmup(iterations = 2) |
| 40 | +@Measurement(iterations = 5) |
| 41 | +@Threads(1) |
| 42 | +public class IndexConstructionWithStaticSetBenchmark { |
| 43 | + private static final Logger log = LoggerFactory.getLogger(IndexConstructionWithStaticSetBenchmark.class); |
| 44 | + private RandomAccessVectorValues ravv; |
| 45 | + private ArrayList<VectorFloat<?>> baseVectors; |
| 46 | + private ArrayList<VectorFloat<?>> queryVectors; |
| 47 | + private ArrayList<Set<Integer>> groundTruth; |
| 48 | + private BuildScoreProvider bsp; |
| 49 | + @Param({"16", "32", "64"}) |
| 50 | + private int M; // graph degree |
| 51 | + @Param({"10", "100"}) |
| 52 | + private int beamWidth; |
| 53 | + int originalDimension; |
| 54 | + |
| 55 | + @Setup |
| 56 | + public void setup() throws IOException { |
| 57 | + var siftPath = "siftsmall"; |
| 58 | + baseVectors = SiftLoader.readFvecs(String.format("%s/siftsmall_base.fvecs", siftPath)); |
| 59 | + queryVectors = SiftLoader.readFvecs(String.format("%s/siftsmall_query.fvecs", siftPath)); |
| 60 | + groundTruth = SiftLoader.readIvecs(String.format("%s/siftsmall_groundtruth.ivecs", siftPath)); |
| 61 | + log.info("base vectors size: {}, query vectors size: {}, loaded, dimensions {}", |
| 62 | + baseVectors.size(), queryVectors.size(), baseVectors.get(0).length()); |
| 63 | + originalDimension = baseVectors.get(0).length(); |
| 64 | + // wrap the raw vectors in a RandomAccessVectorValues |
| 65 | + ravv = new ListRandomAccessVectorValues(baseVectors, originalDimension); |
| 66 | + |
| 67 | + // score provider using the raw, in-memory vectors |
| 68 | + bsp = BuildScoreProvider.randomAccessScoreProvider(ravv, VectorSimilarityFunction.EUCLIDEAN); |
| 69 | + } |
| 70 | + |
| 71 | + @TearDown |
| 72 | + public void tearDown() throws IOException { |
| 73 | + baseVectors.clear(); |
| 74 | + queryVectors.clear(); |
| 75 | + groundTruth.clear(); |
| 76 | + } |
| 77 | + |
| 78 | + @Benchmark |
| 79 | + public void buildIndexBenchmark(Blackhole blackhole) throws IOException { |
| 80 | + // score provider using the raw, in-memory vectors |
| 81 | + try (final var graphIndexBuilder = new GraphIndexBuilder(bsp, ravv.dimension(), M, beamWidth, 1.2f, 1.2f)) { |
| 82 | + final var graphIndex = graphIndexBuilder.build(ravv); |
| 83 | + blackhole.consume(graphIndex); |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments