|
| 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.graph.*; |
| 20 | +import io.github.jbellis.jvector.graph.similarity.BuildScoreProvider; |
| 21 | +import io.github.jbellis.jvector.util.Bits; |
| 22 | +import io.github.jbellis.jvector.vector.VectorSimilarityFunction; |
| 23 | +import io.github.jbellis.jvector.vector.VectorizationProvider; |
| 24 | +import io.github.jbellis.jvector.vector.types.VectorFloat; |
| 25 | +import io.github.jbellis.jvector.vector.types.VectorTypeSupport; |
| 26 | +import org.openjdk.jmh.annotations.*; |
| 27 | +import org.openjdk.jmh.infra.Blackhole; |
| 28 | +import org.slf4j.Logger; |
| 29 | +import org.slf4j.LoggerFactory; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.ArrayList; |
| 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 RandomVectorsBenchmark { |
| 43 | + private static final Logger log = LoggerFactory.getLogger(RandomVectorsBenchmark.class); |
| 44 | + private static final VectorTypeSupport VECTOR_TYPE_SUPPORT = VectorizationProvider.getInstance().getVectorTypeSupport(); |
| 45 | + private RandomAccessVectorValues ravv; |
| 46 | + private ArrayList<VectorFloat<?>> baseVectors; |
| 47 | + private ArrayList<VectorFloat<?>> queryVectors; |
| 48 | + private GraphIndexBuilder graphIndexBuilder; |
| 49 | + private GraphIndex graphIndex; |
| 50 | + int originalDimension; |
| 51 | + @Param({"1000", "10000", "100000", "1000000"}) |
| 52 | + int numBaseVectors; |
| 53 | + @Param({"10"}) |
| 54 | + int numQueryVectors; |
| 55 | + |
| 56 | + @Setup |
| 57 | + public void setup() throws IOException { |
| 58 | + originalDimension = 128; // Example dimension, can be adjusted |
| 59 | + |
| 60 | + baseVectors = new ArrayList<>(numBaseVectors); |
| 61 | + queryVectors = new ArrayList<>(numQueryVectors); |
| 62 | + |
| 63 | + for (int i = 0; i < numBaseVectors; i++) { |
| 64 | + VectorFloat<?> vector = createRandomVector(originalDimension); |
| 65 | + baseVectors.add(vector); |
| 66 | + } |
| 67 | + |
| 68 | + for (int i = 0; i < numQueryVectors; i++) { |
| 69 | + VectorFloat<?> vector = createRandomVector(originalDimension); |
| 70 | + queryVectors.add(vector); |
| 71 | + } |
| 72 | + |
| 73 | + // wrap the raw vectors in a RandomAccessVectorValues |
| 74 | + ravv = new ListRandomAccessVectorValues(baseVectors, originalDimension); |
| 75 | + |
| 76 | + // score provider using the raw, in-memory vectors |
| 77 | + BuildScoreProvider bsp = BuildScoreProvider.randomAccessScoreProvider(ravv, VectorSimilarityFunction.EUCLIDEAN); |
| 78 | + |
| 79 | + graphIndexBuilder = new GraphIndexBuilder(bsp, |
| 80 | + ravv.dimension(), |
| 81 | + 16, // graph degree |
| 82 | + 100, // construction search depth |
| 83 | + 1.2f, // allow degree overflow during construction by this factor |
| 84 | + 1.2f); // relax neighbor diversity requirement by this factor |
| 85 | + graphIndex = graphIndexBuilder.build(ravv); |
| 86 | + } |
| 87 | + |
| 88 | + private VectorFloat<?> createRandomVector(int dimension) { |
| 89 | + VectorFloat<?> vector = VECTOR_TYPE_SUPPORT.createFloatVector(dimension); |
| 90 | + for (int i = 0; i < dimension; i++) { |
| 91 | + vector.set(i, (float) Math.random()); |
| 92 | + } |
| 93 | + return vector; |
| 94 | + } |
| 95 | + |
| 96 | + @TearDown |
| 97 | + public void tearDown() throws IOException { |
| 98 | + baseVectors.clear(); |
| 99 | + queryVectors.clear(); |
| 100 | + graphIndexBuilder.close(); |
| 101 | + } |
| 102 | + |
| 103 | + @Benchmark |
| 104 | + public void testOnHeapRandomVectors(Blackhole blackhole) { |
| 105 | + var queryVector = SiftSmall.randomVector(originalDimension); |
| 106 | + // Your benchmark code here |
| 107 | + var searchResult = GraphSearcher.search(queryVector, |
| 108 | + 10, // number of results |
| 109 | + ravv, // vectors we're searching, used for scoring |
| 110 | + VectorSimilarityFunction.EUCLIDEAN, // how to score |
| 111 | + graphIndex, |
| 112 | + Bits.ALL); // valid ordinals to consider |
| 113 | + blackhole.consume(searchResult); |
| 114 | + } |
| 115 | +} |
0 commit comments