|
| 1 | +using BenchmarkDotNet.Attributes; |
| 2 | +using BenchmarkDotNet.Engines; |
| 3 | +using Bogus; |
| 4 | +using Microsoft.Extensions.AI; |
| 5 | +using Microsoft.Extensions.DependencyInjection; |
| 6 | +using System.Numerics.Tensors; |
| 7 | + |
| 8 | +namespace Akade.IndexedSet.Benchmarks; |
| 9 | +#pragma warning disable AkadeIndexedSetEXP0003 // The api for the vector indices is experimental |
| 10 | + |
| 11 | +[MemoryDiagnoser] |
| 12 | +[DisassemblyDiagnoser] |
| 13 | +[SimpleJob(BenchmarkDotNet.Jobs.RuntimeMoniker.Net90)] |
| 14 | +[JsonExporter] |
| 15 | +public class VectorBenchmarks |
| 16 | +{ |
| 17 | + private List<Product> _largeProductCollection = []; |
| 18 | + private List<Product> _smallProductCollection = []; |
| 19 | + private readonly Consumer _consumer = new(); |
| 20 | + |
| 21 | + private IndexedSet<Product> _indexedSetLarge = null!; |
| 22 | + private IndexedSet<Product> _indexedSetSmall = null!; |
| 23 | + |
| 24 | + [GlobalSetup] |
| 25 | + public async Task SetupAsync() |
| 26 | + { |
| 27 | + ServiceCollection services = new(); |
| 28 | + services.AddBertOnnxEmbeddingGenerator( |
| 29 | + onnxModelPath: "../../../../../../../../Akade.IndexedSet.Tests/Models/BgeMicroV2/model.onnx", |
| 30 | + vocabPath: "../../../../../../../../Akade.IndexedSet.Tests/Models/BgeMicroV2/vocab.txt"); |
| 31 | + |
| 32 | + |
| 33 | + using ServiceProvider sp = services.BuildServiceProvider(); |
| 34 | + |
| 35 | + var generator = sp.GetRequiredService<IEmbeddingGenerator<string, Embedding<float>>>(); |
| 36 | + |
| 37 | + |
| 38 | + Randomizer.Seed = new Random(42); |
| 39 | + |
| 40 | + var productFaker = new Faker<Product>().CustomInstantiator(x => new(x.Commerce.ProductName())); |
| 41 | + |
| 42 | + _largeProductCollection = productFaker.Generate(10_000); |
| 43 | + GeneratedEmbeddings<Embedding<float>> embeddings = await generator.GenerateAsync(_largeProductCollection.Select(x => x.Name)); |
| 44 | + for (int i = 0; i < _largeProductCollection.Count; i++) |
| 45 | + { |
| 46 | + _largeProductCollection[i].Embedding = embeddings[i]; |
| 47 | + } |
| 48 | + |
| 49 | + _smallProductCollection = _largeProductCollection.Take(100).ToList(); |
| 50 | + |
| 51 | + _indexedSetLarge = _largeProductCollection.ToIndexedSet() |
| 52 | + .WithVectorIndex(x => x.Embedding!.Vector.Span) |
| 53 | + .Build(); |
| 54 | + |
| 55 | + _indexedSetSmall = _smallProductCollection.ToIndexedSet() |
| 56 | + .WithVectorIndex(x => x.Embedding!.Vector.Span) |
| 57 | + .Build(); |
| 58 | + } |
| 59 | + |
| 60 | + [Benchmark] |
| 61 | + [BenchmarkCategory("large")] |
| 62 | + public void NearestNeighbor_Large_Linq() |
| 63 | + { |
| 64 | + Product queryProduct = _largeProductCollection[0]; |
| 65 | + _largeProductCollection |
| 66 | + .OrderBy(x => 1 - TensorPrimitives.CosineSimilarity(queryProduct.Embedding!.Vector.Span, x.Embedding!.Vector.Span)) |
| 67 | + .Take(10) |
| 68 | + .Consume(_consumer); |
| 69 | + } |
| 70 | + |
| 71 | + [Benchmark] |
| 72 | + [BenchmarkCategory("large")] |
| 73 | + public void NearestNeighbor_Large_IndexedSet() |
| 74 | + { |
| 75 | + Product queryProduct = _largeProductCollection[0]; |
| 76 | + _indexedSetLarge.ApproximateNearestNeighbors(x => x.Embedding!.Vector.Span, queryProduct.Embedding!.Vector.Span, 10) |
| 77 | + .Consume(_consumer); |
| 78 | + } |
| 79 | + |
| 80 | + [Benchmark] |
| 81 | + [BenchmarkCategory("small")] |
| 82 | + public void NearestNeighbor_Small_Linq() |
| 83 | + { |
| 84 | + Product queryProduct = _smallProductCollection[0]; |
| 85 | + _smallProductCollection |
| 86 | + .OrderBy(x => 1 - TensorPrimitives.CosineSimilarity(queryProduct.Embedding!.Vector.Span, x.Embedding!.Vector.Span)) |
| 87 | + .Take(10) |
| 88 | + .Consume(_consumer); |
| 89 | + } |
| 90 | + |
| 91 | + [Benchmark] |
| 92 | + [BenchmarkCategory("small")] |
| 93 | + public void NearestNeighbor_Small_IndexedSet() |
| 94 | + { |
| 95 | + Product queryProduct = _smallProductCollection[0]; |
| 96 | + _indexedSetSmall.ApproximateNearestNeighbors(x => x.Embedding!.Vector.Span, queryProduct.Embedding!.Vector.Span, 10) |
| 97 | + .Consume(_consumer); |
| 98 | + } |
| 99 | + |
| 100 | + public record class Product(string Name) |
| 101 | + { |
| 102 | + public Embedding<float>? Embedding { get; set; } |
| 103 | + } |
| 104 | +} |
0 commit comments