Skip to content

Commit

Permalink
Fixed the perf issue on linux (#229)
Browse files Browse the repository at this point in the history
* fixed the perf issue on linux

* Renamed GetSystemInfo => GetRuntimeInfo

* Added comment about runtime info
  • Loading branch information
sandrohanea authored Oct 17, 2024
1 parent cda7d18 commit 278c39c
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 7 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,31 @@ wasm:

linux_x64:
rm -rf build/linux-x64
cmake -S . -B build/linux-x64 -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
cmake -S . -B build/linux-x64 -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_NATIVE=ON
cmake --build build/linux-x64 --config $(BUILD_TYPE)
mkdir -p runtimes/Whisper.net.Runtime/linux-x64
cp build/linux-x64/whisper.cpp/src/libwhisper.so ./runtimes/Whisper.net.Runtime/linux-x64/libwhisper.so
cp build/linux-x64/whisper.cpp/ggml/src/libggml.so ./runtimes/Whisper.net.Runtime/linux-x64/libggml.so

linux_arm64:
rm -rf build/linux-arm64
cmake -S . -B build/linux-arm64 -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64
cmake -S . -B build/linux-arm64 -DCMAKE_C_COMPILER=aarch64-linux-gnu-gcc -DCMAKE_CXX_COMPILER=aarch64-linux-gnu-g++ -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=aarch64 -DGGML_NATIVE=ON
cmake --build build/linux-arm64 --config $(BUILD_TYPE)
mkdir -p runtimes/Whisper.net.Runtime/linux-arm64
cp build/linux-arm64/whisper.cpp/src/libwhisper.so ./runtimes/Whisper.net.Runtime/linux-arm64/libwhisper.so
cp build/linux-arm64/whisper.cpp/ggml/src/libggml.so ./runtimes/Whisper.net.Runtime/linux-arm64/libggml.so

linux_arm:
rm -rf build/linux-arm
cmake -S . -B build/linux-arm -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=arm
cmake -S . -B build/linux-arm -DCMAKE_C_COMPILER=arm-linux-gnueabihf-gcc -DCMAKE_CXX_COMPILER=arm-linux-gnueabihf-g++ -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=arm -DGGML_NATIVE=ON
cmake --build build/linux-arm --config $(BUILD_TYPE)
mkdir -p runtimes/Whisper.net.Runtime/linux-arm
cp build/linux-arm/whisper.cpp/src/libwhisper.so ./runtimes/Whisper.net.Runtime/linux-arm/libwhisper.so
cp build/linux-arm/whisper.cpp/ggml/src/libggml.so ./runtimes/Whisper.net.Runtime/linux-arm/libggml.so

linux_x64_cuda:
rm -rf build/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 -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 -DGGML_NATIVE=ON
cmake --build build/linux-x64-cuda --config $(BUILD_TYPE)
mkdir -p runtimes/Whisper.net.Runtime.Cuda.Linux/linux-x64
cp build/linux-x64-cuda/whisper.cpp/src/libwhisper.so ./runtimes/Whisper.net.Runtime.Cuda.Linux/linux-x64/libwhisper.so
Expand Down Expand Up @@ -108,7 +108,7 @@ linux_arm_noavx:

linux_x64_openvino:
rm -rf build/linux-x64-openvino
cmake -S . -B build/linux-x64-openvino -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 -DWHISPER_OPENVINO=ON
cmake -S . -B build/linux-x64-openvino -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 -DWHISPER_OPENVINO=ON -DGGML_NATIVE=ON
cmake --build build/linux-x64-openvino --config $(BUILD_TYPE)
mkdir -p runtimes/Whisper.net.Runtime.OpenVino/linux-x64
cp build/linux-x64-openvino/whisper.cpp/src/libwhisper.so ./runtimes/Whisper.net.Runtime.OpenVino/linux-x64/libwhisper.so
Expand Down Expand Up @@ -167,15 +167,15 @@ ios_coreml:

maccatalyst_arm64:
rm -rf build/maccatalyst_arm64
cmake $(CMAKE_PARAMETERS) -DCMAKE_SYSTEM_PROCESSOR=arm -DCMAKE_HOST_SYSTEM_PROCESSOR=arm64 -DGGML_METAL=OFF -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_ARCHITECTURES="arm64" -DCMAKE_CXX_FLAGS="-target arm64-apple-ios13.1-macabi" -DCMAKE_C_FLAGS="-target arm64-apple-ios13.1-macabi" -S . -B build/maccatalyst_arm64
cmake $(CMAKE_PARAMETERS) -S . -B build/maccatalyst_arm64 -DCMAKE_SYSTEM_PROCESSOR=arm -DCMAKE_HOST_SYSTEM_PROCESSOR=arm64 -DGGML_METAL=OFF -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_ARCHITECTURES="arm64" -DCMAKE_CXX_FLAGS="-target arm64-apple-ios13.1-macabi" -DCMAKE_C_FLAGS="-target arm64-apple-ios13.1-macabi" -DGGML_NATIVE=ON
cmake --build build/maccatalyst_arm64
mkdir -p runtimes/Whisper.net.Runtime/maccatalyst
cp build/maccatalyst_arm64/whisper.cpp/src/libwhisper.dylib runtimes/Whisper.net.Runtime/maccatalyst/libwhisper.dylib
cp build/maccatalyst_arm64/whisper.cpp/ggml/src/libggml.dylib runtimes/Whisper.net.Runtime/maccatalyst/libggml.dylib

maccatalyst_arm64_coreml:
rm -rf build/maccatalyst-arm64-coreml
cmake $(COREML_SUPPORT) -DCMAKE_SYSTEM_PROCESSOR=arm -DCMAKE_HOST_SYSTEM_PROCESSOR=arm64 -DGGML_METAL=OFF -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_ARCHITECTURES="arm64" -DCMAKE_CXX_FLAGS="-target arm64-apple-ios13.1-macabi" -DCMAKE_C_FLAGS="-target arm64-apple-ios13.1-macabi" -S . -B build/maccatalyst-arm64-coreml
cmake $(COREML_SUPPORT) -S . -B build/maccatalyst-arm64-coreml -DCMAKE_SYSTEM_PROCESSOR=arm -DCMAKE_HOST_SYSTEM_PROCESSOR=arm64 -DGGML_METAL=OFF -DCMAKE_SYSTEM_NAME=Darwin -DCMAKE_OSX_ARCHITECTURES="arm64" -DCMAKE_CXX_FLAGS="-target arm64-apple-ios13.1-macabi" -DCMAKE_C_FLAGS="-target arm64-apple-ios13.1-macabi" -DGGML_NATIVE=ON
cmake --build build/maccatalyst-arm64-coreml
mkdir -p runtimes/Whisper.net.Runtime.CoreML/maccatalyst
cp build/maccatalyst-arm64-coreml/whisper.cpp/src/libwhisper.coreml.dylib runtimes/Whisper.net.Runtime.CoreML/maccatalyst/libwhisper.coreml.dylib
Expand Down
5 changes: 5 additions & 0 deletions Whisper.net/Internals/Native/INativeWhisper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ internal interface INativeWhisper : IDisposable
[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate void ggml_log_set(IntPtr logCallback, IntPtr user_data);

[UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public delegate IntPtr whisper_print_system_info();

whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State { get; }
whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State { get; }
whisper_free Whisper_Free { get; }
Expand All @@ -96,4 +99,6 @@ internal interface INativeWhisper : IDisposable
whisper_ctx_init_openvino_encoder_with_state Whisper_Ctx_Init_Openvino_Encoder_With_State { get; }

ggml_log_set Ggml_log_set { get; }

whisper_print_system_info WhisperPrintSystemInfo { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ internal class DllImportsNativeLibWhisper : INativeWhisper
[DllImport(ggmlLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void ggml_log_set(IntPtr logCallback, IntPtr user_data);

[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr whisper_print_system_info();

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -123,6 +126,8 @@ internal class DllImportsNativeLibWhisper : INativeWhisper

public INativeWhisper.ggml_log_set Ggml_log_set => ggml_log_set;

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ internal class DllImportsNativeWhisper : INativeWhisper
[DllImport(ggmlLibraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern void ggml_log_set(IntPtr logCallback, IntPtr user_data);

[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern IntPtr whisper_print_system_info();

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -123,6 +126,8 @@ internal class DllImportsNativeWhisper : INativeWhisper

public INativeWhisper.ggml_log_set Ggml_log_set => ggml_log_set;

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,9 @@ internal partial class LibraryImportInternalWhisper : INativeWhisper
[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial void ggml_log_set(IntPtr logCallback, IntPtr user_data);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_print_system_info();

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -122,6 +125,8 @@ internal partial class LibraryImportInternalWhisper : INativeWhisper

public INativeWhisper.ggml_log_set Ggml_log_set => ggml_log_set;

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ internal partial class LibraryImportLibWhisper : INativeWhisper
[LibraryImport(ggmlLibraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial void ggml_log_set(IntPtr logCallback, IntPtr user_data);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_print_system_info();

public INativeWhisper.whisper_init_from_file_with_params_no_state Whisper_Init_From_File_With_Params_No_State => whisper_init_from_file_with_params_no_state;

public INativeWhisper.whisper_init_from_buffer_with_params_no_state Whisper_Init_From_Buffer_With_Params_No_State => whisper_init_from_buffer_with_params_no_state;
Expand Down Expand Up @@ -123,6 +126,8 @@ internal partial class LibraryImportLibWhisper : INativeWhisper

public INativeWhisper.ggml_log_set Ggml_log_set => ggml_log_set;

public INativeWhisper.whisper_print_system_info WhisperPrintSystemInfo => whisper_print_system_info;

public void Dispose()
{

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public NativeLibraryWhisper(IntPtr whisperLibraryHandle, IntPtr ggmlLibraryHandl
Whisper_Log_Set = Marshal.GetDelegateForFunctionPointer<whisper_log_set>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_log_set)));
Whisper_Ctx_Init_Openvino_Encoder_With_State = Marshal.GetDelegateForFunctionPointer<whisper_ctx_init_openvino_encoder_with_state>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_ctx_init_openvino_encoder_with_state)));
Ggml_log_set = Marshal.GetDelegateForFunctionPointer<ggml_log_set>(NativeLibrary.GetExport(ggmlLibraryHandle, nameof(ggml_log_set)));
WhisperPrintSystemInfo = Marshal.GetDelegateForFunctionPointer<whisper_print_system_info>(NativeLibrary.GetExport(whisperLibraryHandle, nameof(whisper_print_system_info)));

this.whisperLibraryHandle = whisperLibraryHandle;
this.ggmlLibraryHandle = ggmlLibraryHandle;
Expand Down Expand Up @@ -84,6 +85,8 @@ public NativeLibraryWhisper(IntPtr whisperLibraryHandle, IntPtr ggmlLibraryHandl

public ggml_log_set Ggml_log_set { get; }

public whisper_print_system_info WhisperPrintSystemInfo { get; }

public void Dispose()
{
NativeLibrary.Free(whisperLibraryHandle);
Expand Down
21 changes: 21 additions & 0 deletions Whisper.net/WhisperFactory.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Licensed under the MIT license: https://opensource.org/licenses/MIT

using System.Runtime.InteropServices;
using Whisper.net.Internals.ModelLoader;
using Whisper.net.LibraryLoader;
using Whisper.net.Logger;
Expand Down Expand Up @@ -47,6 +48,26 @@ private WhisperFactory(IWhisperProcessorModelLoader loader, bool delayInit)
}
}

/// <summary>
/// Returns the information about the loaded native runtime.
/// </summary>
/// <remarks>
/// This information includes support of the features like AVX, AVX2, AVX512, CUDA, etc.
/// </remarks>
/// <exception cref="Exception"></exception>
public static string? GetRuntimeInfo()
{
if (!libraryLoaded.Value.IsSuccess)
{
throw new Exception($"Failed to load native whisper library. Error: {libraryLoaded.Value.ErrorMessage}");
}

var systemInfoPtr = libraryLoaded.Value.NativeWhisper!.WhisperPrintSystemInfo();
var systemInfoStr = Marshal.PtrToStringAnsi(systemInfoPtr);
Marshal.FreeHGlobal(systemInfoPtr);
return systemInfoStr;
}

/// <summary>
/// Creates a factory that uses the ggml model from a path in order to create <seealso cref="WhisperProcessorBuilder"/>.
/// </summary>
Expand Down

0 comments on commit 278c39c

Please sign in to comment.