Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Benchmark/Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<OutputType>Exe</OutputType>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
<PackageReference Include="Microsoft.ML.OnnxRuntime.Gpu" Version="1.22.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\KokoroSharp\KokoroSharp.csproj" />
</ItemGroup>

</Project>
38 changes: 38 additions & 0 deletions Benchmark/Inference.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using BenchmarkDotNet.Attributes;

using KokoroSharp;
using KokoroSharp.Core;

using System;
using System.Collections.Generic;

namespace Benchmark;

[InProcess]
public class Inference {
const string text = "This is a performance benchmark of Kokoro.";
static readonly Dictionary<(KModel, bool UseCuda), KokoroModel> models = [];
static int[] tokens;
static KokoroVoice voice;

[GlobalSetup]
public void Setup() {
tokens = KokoroSharp.Processing.Tokenizer.Tokenize(text);
voice = KokoroVoiceManager.GetVoice("af_heart");
foreach (var model in Enum.GetValues<KModel>()) {
if (!KokoroTTS.IsDownloaded(model))
KokoroTTS.LoadModel(model).Dispose(); // downloads the model if not already present.
var options = new Microsoft.ML.OnnxRuntime.SessionOptions();
models[(model, false)] = new KokoroModel(KokoroTTS.ModelNamesMap[model], options);
var options2 = new Microsoft.ML.OnnxRuntime.SessionOptions();
options2.AppendExecutionProvider_CUDA(); // Use CUDA for GPU inference.
models[(model, true)] = new KokoroModel(KokoroTTS.ModelNamesMap[model], options2);
}
}

[ParamsAllValues]
public KModel Model { get; set; }

[Benchmark] public float[] CPU() => models[(Model, false)].Infer(tokens, voice!.Features);
[Benchmark] public float[] CUDA() => models[(Model, true)].Infer(tokens, voice!.Features);
}
3 changes: 3 additions & 0 deletions Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using BenchmarkDotNet.Running;

BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
16 changes: 16 additions & 0 deletions Benchmark/Tokenizer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using BenchmarkDotNet.Attributes;

using System.IO;
using System.Runtime.CompilerServices;

namespace Benchmark;

[SimpleJob]
public class Tokenizer {
static readonly string text = File.ReadAllText(Path.Join(GetMyPath(), "../../README.md"));
static string GetMyPath([CallerFilePath] string filePath = "") => filePath;

[Benchmark] public string PreprocessText() => KokoroSharp.Processing.Tokenizer.PreprocessText(text);
[Benchmark] public string Phonemize() => KokoroSharp.Processing.Tokenizer.Phonemize(text, "en-us");
[Benchmark] public int[] Tokenize() => KokoroSharp.Processing.Tokenizer.Tokenize(text);
}
6 changes: 6 additions & 0 deletions KokoroSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KokoroSharp", "KokoroSharp\
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KokoroSharp.Tests", "KokoroSharp.Tests\KokoroSharp.Tests.csproj", "{B0AB7C0B-D1C1-447C-9EB5-5CC9CB9E8943}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Benchmark", "Benchmark\Benchmark.csproj", "{FE74270D-B73E-4F9B-9467-2C225FD95FA1}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -21,6 +23,10 @@ Global
{B0AB7C0B-D1C1-447C-9EB5-5CC9CB9E8943}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B0AB7C0B-D1C1-447C-9EB5-5CC9CB9E8943}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B0AB7C0B-D1C1-447C-9EB5-5CC9CB9E8943}.Release|Any CPU.Build.0 = Release|Any CPU
{FE74270D-B73E-4F9B-9467-2C225FD95FA1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FE74270D-B73E-4F9B-9467-2C225FD95FA1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FE74270D-B73E-4F9B-9467-2C225FD95FA1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FE74270D-B73E-4F9B-9467-2C225FD95FA1}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
2 changes: 1 addition & 1 deletion KokoroSharp/HighLevel/KokoroLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
public enum KModel { float32, float16, int8 }

public partial class KokoroTTS {
static IReadOnlyDictionary<KModel, string> ModelNamesMap { get; } = new Dictionary<KModel, string>() {
internal static IReadOnlyDictionary<KModel, string> ModelNamesMap { get; } = new Dictionary<KModel, string>() {
{ float32, "kokoro.onnx" },
{ float16, "kokoro-quant.onnx" },
{ int8, "kokoro-quant-convinteger.onnx" },
Expand Down
2 changes: 1 addition & 1 deletion KokoroSharp/KokoroSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="KokoroSharp.Tests" />
<InternalsVisibleTo Include="KokoroSharp.Tests;Benchmark" />
</ItemGroup>

</Project>