Skip to content

Commit

Permalink
Split Cuda to Windows + Linux and added Parallel Example
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Hanea committed Oct 8, 2024
1 parent fc1282f commit bc6ad14
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -359,8 +359,8 @@ MigrationBackup/
/Whisper.net.Runtime/*
!/Whisper.net.Runtime/Whisper.net.Runtime.targets

/Whisper.net.Runtime.Cuda/*
!/Whisper.net.Runtime.Cuda/Whisper.net.Runtime.Cuda.targets
/Whisper.net.Runtime.Cuda.*/*
!/Whisper.net.Runtime.Cuda.*/Whisper.net.Runtime.Cuda.*.targets

/Whisper.net.Runtime.Vulkan/*
!/Whisper.net.Runtime.Vulkan/Whisper.net.Runtime.Vulkan.targets
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ linux_x64_cuda:
cmake -S . -B build/linux-x64-cuda -DCMAKE_C_COMPILER=x86_64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=x86_64-linux-gnu-g++ -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=x86_64 -DGGML_CUDA=ON
cmake --build build/linux-x64-cuda --config $(BUILD_TYPE)
mkdir -p Whisper.net.Runtime.Cuda/linux-x64
cp build/linux-x64-cuda/whisper.cpp/src/libwhisper.so ./Whisper.net.Runtime.Cuda/linux-x64/libwhisper.so
cp build/linux-x64-cuda/whisper.cpp/ggml/src/libggml.so ./Whisper.net.Runtime.Cuda/linux-x64/libggml.so
cp build/linux-x64-cuda/whisper.cpp/src/libwhisper.so ./Whisper.net.Runtime.Cuda.Linux/linux-x64/libwhisper.so
cp build/linux-x64-cuda/whisper.cpp/ggml/src/libggml.so ./Whisper.net.Runtime.Cuda.Linux/linux-x64/libggml.so

linux_x64_openvino:
rm -rf build/linux-x64-openvino
Expand Down
19 changes: 19 additions & 0 deletions Whisper.net.Runtime.Cuda.Linux.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<package>
<metadata>
<id>Whisper.net.Runtime.Cuda.Linux</id>
<title>Whisper.Net.Runtime.Cuda.Linux</title>
<version>1.7.0</version>
<authors>Sandro Hanea</authors>
<owners>Sandro Hanea</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<summary>Linux Cuda dotnet runtimes for Whisper.</summary>
<description>Whisper.net.Runtime.Cuda.Linux contains the native runtime libraries to enable Whisper on .NET with Whisper.net and GPU support (Cuda) for Linux</description>
<tags>whisper;cuda;gpu;cublas;</tags>
<projectUrl>https://github.com/sandrohanea/whisper.net</projectUrl>
<license type="expression">MIT</license>
</metadata>
<files>
<file src="Whisper.net.Runtime.Cuda.Linux\**" target="build"/>
</files>
</package>
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup
Condition="$(TargetFramework.Contains('-windows')) == true or $(TargetFramework.Contains('-')) == false">
<None Visible="false" Include="$(MSBuildThisFileDirectory)win-x64\whisper.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>runtimes/cuda/win-x64/whisper.dll</TargetPath>
</None>
<None Visible="false" Include="$(MSBuildThisFileDirectory)win-x64\ggml.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>runtimes/cuda/win-x64/ggml.dll</TargetPath>
</None>
<ItemGroup>
<None Visible="false" Include="$(MSBuildThisFileDirectory)linux-x64\libwhisper.so">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
Expand Down
19 changes: 19 additions & 0 deletions Whisper.net.Runtime.Cuda.Windows.nuspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0"?>
<package>
<metadata>
<id>Whisper.net.Runtime.Cuda.Windows</id>
<title>Whisper.Net.Runtime.Cuda.Windows</title>
<version>1.7.0</version>
<authors>Sandro Hanea</authors>
<owners>Sandro Hanea</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<summary>Windows Cuda dotnet runtimes for Whisper.</summary>
<description>Whisper.net.Runtime.Cuda.Windows contains the native runtime libraries to enable Whisper on .NET with Whisper.net and GPU support (Cuda) on Windows.</description>
<tags>whisper;cuda;gpu;cublas;</tags>
<projectUrl>https://github.com/sandrohanea/whisper.net</projectUrl>
<license type="expression">MIT</license>
</metadata>
<files>
<file src="Whisper.net.Runtime.Cuda.Windows\**" target="build"/>
</files>
</package>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<None Visible="false" Include="$(MSBuildThisFileDirectory)win-x64\whisper.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>runtimes/cuda/win-x64/whisper.dll</TargetPath>
</None>
<None Visible="false" Include="$(MSBuildThisFileDirectory)win-x64\ggml.dll">
<Pack>true</Pack>
<PackageCopyToOutput>true</PackageCopyToOutput>
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<TargetPath>runtimes/cuda/win-x64/ggml.dll</TargetPath>
</None>
</ItemGroup>
</Project>
8 changes: 6 additions & 2 deletions Whisper.net.Runtime.Cuda.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@
<authors>Sandro Hanea</authors>
<owners>Sandro Hanea</owners>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<summary>Web Assembly dotnet runtimes for Whisper.</summary>
<summary>Cuda dotnet runtimes for Whisper.</summary>
<description>Whisper.net.Runtime.Cuda contains the native runtime libraries to enable Whisper on .NET with Whisper.net and GPU support (Cuda)</description>
<tags>whisper;cuda;gpu;cublas;</tags>
<projectUrl>https://github.com/sandrohanea/whisper.net</projectUrl>
<license type="expression">MIT</license>
<dependencies>
<dependency id="Whisper.net.Runtime.Cuda.Windows" version="1.7.0" />
<dependency id="Whisper.net.Runtime.Cuda.Linux" version="1.7.0" />
</dependencies>
</metadata>
<files>
<file src="Whisper.net.Runtime.Cuda\**" target="build"/>

</files>
</package>
5 changes: 4 additions & 1 deletion Whisper.net.sln
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,11 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "runtimes", "runtimes", "{CC
ProjectSection(SolutionItems) = preProject
Whisper.net.Runtime.CoreML.nuspec = Whisper.net.Runtime.CoreML.nuspec
Whisper.net.Runtime.CoreML\Whisper.net.Runtime.CoreML.targets = Whisper.net.Runtime.CoreML\Whisper.net.Runtime.CoreML.targets
Whisper.net.Runtime.Cuda.Linux.nuspec = Whisper.net.Runtime.Cuda.Linux.nuspec
Whisper.net.Runtime.Cuda.Linux\Whisper.net.Runtime.Cuda.Linux.targets = Whisper.net.Runtime.Cuda.Linux\Whisper.net.Runtime.Cuda.Linux.targets
Whisper.net.Runtime.Cuda.nuspec = Whisper.net.Runtime.Cuda.nuspec
Whisper.net.Runtime.Cuda\Whisper.net.Runtime.Cuda.targets = Whisper.net.Runtime.Cuda\Whisper.net.Runtime.Cuda.targets
Whisper.net.Runtime.Cuda.Windows.nuspec = Whisper.net.Runtime.Cuda.Windows.nuspec
Whisper.net.Runtime.Cuda.Windows\Whisper.net.Runtime.Cuda.Windows.targets = Whisper.net.Runtime.Cuda.Windows\Whisper.net.Runtime.Cuda.Windows.targets
Whisper.net.Runtime.nuspec = Whisper.net.Runtime.nuspec
Whisper.net.Runtime.OpenVino.nuspec = Whisper.net.Runtime.OpenVino.nuspec
Whisper.net.Runtime.OpenVino\Whisper.net.Runtime.OpenVino.targets = Whisper.net.Runtime.OpenVino\Whisper.net.Runtime.OpenVino.targets
Expand Down
2 changes: 1 addition & 1 deletion examples/NvidiaCuda/NvidiaCuda.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Whisper.net.Runtime.Cuda" Version="1.7.0" />
<PackageReference Include="Whisper.net" Version="1.7.0" />
<PackageReference Include="Whisper.net.Runtime.Cuda" Version="1.7.0" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 20 additions & 0 deletions examples/ParallelExecution/ParallelExecution.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFrameworks>net8.0; net472</TargetFrameworks>
<LangVersion>12</LangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Whisper.net.Runtime" Version="1.7.0" />
<PackageReference Include="Whisper.net" Version="1.7.0" />
</ItemGroup>

<ItemGroup>
<None Include="../TestData/kennedy.wav">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
66 changes: 66 additions & 0 deletions examples/ParallelExecution/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Licensed under the MIT license: https://opensource.org/licenses/MIT

using System;
using System.IO;
using System.Threading.Tasks;
using Whisper.net;
using Whisper.net.Ggml;
using Whisper.net.Logger;

public class Program
{
// This examples shows how to use Whisper.net to create a transcription from an audio file with 16Khz sample rate with 2 parallel executions.
public static async Task Main(string[] args)
{
var ggmlType = GgmlType.Base;
var modelFileName = "ggml-base.bin";

if (!File.Exists(modelFileName))
{
await DownloadModel(modelFileName, ggmlType);
}

// Optional logging from the native library
LogProvider.Instance.OnLog += (level, message) =>
{
Console.Write($"{level}: {message}");
};

// This section creates the whisperFactory object which is used to create the processor object.
using var whisperFactory = WhisperFactory.FromPath("ggml-base.bin");

var task1 = Task.Run(() => RunInParallel("Task1", "kennedy.wav", whisperFactory));
var task2 = Task.Run(() => RunInParallel("Task2", "kennedy.wav", whisperFactory));

// We wait both tasks and we'll see that the results are interleaved
await Task.WhenAll(task1, task2);
}

public static async Task RunInParallel(string name, string wavFileName, WhisperFactory whisperFactory)
{

// This section creates the processor object which is used to process the audio file, it uses language `auto` to detect the language of the audio file.
using var processor = whisperFactory.CreateBuilder()
.WithLanguage("auto")
.Build();

using var fileStream = File.OpenRead(wavFileName);

// This section processes the audio file and prints the results (start time, end time and text) to the console.
await foreach (var result in processor.ProcessAsync(fileStream))
{
Console.WriteLine($"{name} =====> {result.Start}->{result.End}: {result.Text}");

// Add some delay, otherwise we might get the results too fast
await Task.Delay(1000);
}
}

private static async Task DownloadModel(string fileName, GgmlType ggmlType)
{
Console.WriteLine($"Downloading Model {fileName}");
using var modelStream = await WhisperGgmlDownloader.GetGgmlModelAsync(ggmlType);
using var fileWriter = File.OpenWrite(fileName);
await modelStream.CopyToAsync(fileWriter);
}
}
6 changes: 6 additions & 0 deletions examples/Whisper.net.Examples.sln
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vulkan", "Vulkan\Vulkan.csp
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenVinoExample", "OpenVinoExample\OpenVinoExample.csproj", "{1657F744-D10E-4532-B2B5-DD03838235DD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParallelExecution", "ParallelExecution\ParallelExecution.csproj", "{4D1415B0-80BD-40D1-8D55-7CE3A3BABC1C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -98,6 +100,10 @@ Global
{1657F744-D10E-4532-B2B5-DD03838235DD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1657F744-D10E-4532-B2B5-DD03838235DD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1657F744-D10E-4532-B2B5-DD03838235DD}.Release|Any CPU.Build.0 = Release|Any CPU
{4D1415B0-80BD-40D1-8D55-7CE3A3BABC1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4D1415B0-80BD-40D1-8D55-7CE3A3BABC1C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4D1415B0-80BD-40D1-8D55-7CE3A3BABC1C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4D1415B0-80BD-40D1-8D55-7CE3A3BABC1C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 2 additions & 1 deletion nuget.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="public_nugget" value="https://api.nuget.org/v3/index.json" />
<add key="Local Whisper" value="C:\Projects\sandrohanea\whisper.net\nupkgs" />
</packageSources>
</configuration>
4 changes: 3 additions & 1 deletion windows-scripts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ function BuildWindowsBase() {

$runtimePath = "./Whisper.net.Runtime"
if ($Cuda) {
$runtimePath += ".Cuda"
$runtimePath += ".Cuda.Windows"
}

if ($Vulkan) {
Expand Down Expand Up @@ -163,6 +163,8 @@ function PackAll([Parameter(Mandatory = $true)] [string]$Version) {

dotnet pack Whisper.net/Whisper.net.csproj -p:Version=$Version -o ./nupkgs -c Release
nuget pack Whisper.net.Runtime.CoreML.nuspec -Version $Version -OutputDirectory ./nupkgs
nuget pack Whisper.net.Runtime.Cuda.Linux.nuspec -Version $Version -OutputDirectory ./nupkgs
nuget pack Whisper.net.Runtime.Cuda.Windows.nuspec -Version $Version -OutputDirectory ./nupkgs
nuget pack Whisper.net.Runtime.Cuda.nuspec -Version $Version -OutputDirectory ./nupkgs
nuget pack Whisper.net.Runtime.Vulkan.nuspec -Version $Version -OutputDirectory ./nupkgs
nuget pack Whisper.net.Runtime.OpenVino.nuspec -Version $Version -OutputDirectory ./nupkgs
Expand Down

0 comments on commit bc6ad14

Please sign in to comment.