Skip to content

Commit e24e588

Browse files
committed
Add benchmark for Inference
1 parent ba45266 commit e24e588

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

Benchmark/Benchmark.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
<ItemGroup>
1212
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
13+
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.22.0" />
1314
</ItemGroup>
1415

1516
<ItemGroup>

Benchmark/Inference.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using BenchmarkDotNet.Attributes;
2+
3+
using KokoroSharp;
4+
using KokoroSharp.Core;
5+
6+
namespace Benchmark;
7+
8+
[InProcess]
9+
public class Inference {
10+
const string text = "This is a performance benchmark of Kokoro.";
11+
static readonly Dictionary<(KModel, bool UseCuda), KokoroModel> models = [];
12+
static int[]? tokens;
13+
static KokoroVoice? voice;
14+
15+
[GlobalSetup]
16+
public void Setup() {
17+
tokens = KokoroSharp.Processing.Tokenizer.Tokenize(text);
18+
voice = KokoroVoiceManager.GetVoice("af_heart");
19+
foreach (var model in Enum.GetValues<KModel>()) {
20+
if (!KokoroTTS.IsDownloaded(model))
21+
KokoroTTS.LoadModel(model).Dispose(); // downloads the model if not already present.
22+
var options = new Microsoft.ML.OnnxRuntime.SessionOptions();
23+
models[(model, false)] = new KokoroModel(KokoroTTS.ModelNamesMap[model], options);
24+
var options2 = new Microsoft.ML.OnnxRuntime.SessionOptions();
25+
options2.AppendExecutionProvider_CUDA(); // Use CUDA for GPU inference.
26+
models[(model, true)] = new KokoroModel(KokoroTTS.ModelNamesMap[model], options2);
27+
}
28+
}
29+
30+
[ParamsAllValues]
31+
public KModel Model { get; set; }
32+
33+
[Benchmark]
34+
public float[] CPU() {
35+
return models[(Model, false)].Infer(tokens, voice!.Features);
36+
}
37+
38+
[Benchmark]
39+
public float[] CUDA() {
40+
return models[(Model, true)].Infer(tokens, voice!.Features);
41+
}
42+
}

KokoroSharp/HighLevel/KokoroLoader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
public enum KModel { float32, float16, int8 }
99

1010
public partial class KokoroTTS {
11-
static IReadOnlyDictionary<KModel, string> ModelNamesMap { get; } = new Dictionary<KModel, string>() {
11+
internal static IReadOnlyDictionary<KModel, string> ModelNamesMap { get; } = new Dictionary<KModel, string>() {
1212
{ float32, "kokoro.onnx" },
1313
{ float16, "kokoro-quant.onnx" },
1414
{ int8, "kokoro-quant-convinteger.onnx" },

0 commit comments

Comments
 (0)