Skip to content

Commit

Permalink
Trying to find libomp
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrohanea committed Oct 13, 2024
1 parent 3c867a8 commit 176bcd5
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 11 deletions.
6 changes: 2 additions & 4 deletions .github/workflows/dotnet-maui.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ jobs:
run: |
dotnet workload install maui --ignore-failed-sources
dotnet workload install tvos --ignore-failed-sources
- name: Install xharness
run: dotnet tool install Microsoft.DotNet.XHarness.CLI --global --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-eng/nuget/v3/index.json --version "10.0.0-prerelease.24511.1"

- name: Restore dependencies
run: dotnet restore ./Whisper.net.Maui.Tests.sln

Expand Down Expand Up @@ -89,6 +86,7 @@ jobs:
script: xharness android test --app=./maui-build-artifacts/com.companyname.whisper.net.maui.tests-Signed.apk -p com.companyname.whisper.net.maui.tests -i com.companyname.whisper.net.maui.tests.AndroidMauiTestInstrumentation -o=./test-results/android

- name: Upload Maui Android xharness test results
if: ${{ always() }}
uses: actions/upload-artifact@v4
with:
name: xharness-android-test-results
Expand Down
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,9 @@ android_x64:
rm -rf build/android-x86_64
cmake $(CMAKE_PARAMETERS) -DCMAKE_ANDROID_ARCH_ABI=x86_64 -DCMAKE_SYSTEM_NAME=Android -DCMAKE_ANDROID_API=21 -DCMAKE_ANDROID_NDK=$(NDK) -S . -B build/android-x86_64
cmake --build build/android-x86_64
mkdir -p runtimes/Whisper.net.Runtime/android-x86_64
mkdir -p runtimes/Whisper.net.Runtime/android-x86_64 ls -l build/linux-x64/whisper.cpp/src/
ls -l build/linux-x64/whisper.cpp/ggml/src/
find build/linux-x64/whisper.cpp/ -name libomp.so
cp build/android-x86_64/whisper.cpp/src/libwhisper.so ./runtimes/Whisper.net.Runtime/android-x86_64/libwhisper.so
cp build/android-x86_64/whisper.cpp/ggml/src/libggml.so ./runtimes/Whisper.net.Runtime/android-x86_64/libggml.so

Expand Down
1 change: 1 addition & 0 deletions Whisper.net.Maui.Tests.sln
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "scripts", "scripts", "{52C6
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "runtimes", "runtimes", "{CC45AC94-3CD7-45D8-A847-2D9C06E1806A}"
ProjectSection(SolutionItems) = preProject
runtimes\Whisper.net.AllRuntimes.nuspec = runtimes\Whisper.net.AllRuntimes.nuspec
runtimes\Whisper.net.Runtime.CoreML.nuspec = runtimes\Whisper.net.Runtime.CoreML.nuspec
runtimes\Whisper.net.Runtime.CoreML\Whisper.net.Runtime.CoreML.targets = runtimes\Whisper.net.Runtime.CoreML\Whisper.net.Runtime.CoreML.targets
runtimes\Whisper.net.Runtime.Cuda.Linux.nuspec = runtimes\Whisper.net.Runtime.Cuda.Linux.nuspec
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
// Licensed under the MIT license: https://opensource.org/licenses/MIT


#if NET8_0_OR_GREATER
using System.Runtime.InteropServices;
using Whisper.net.Native;

namespace Whisper.net.Internals.Native.Implementations;
/// <summary>
/// This way of loading INativeWhisper is used on Android
/// </summary>
internal partial class LibraryImportNativeLibWhisper : INativeWhisper
{
const string libraryName = "libwhisper";
const string ggmlLibraryName = "libggml";

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_init_from_file_with_params_no_state(string path, WhisperContextParams whisperContextParams);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_init_from_buffer_with_params_no_state(IntPtr buffer, nuint buffer_size, WhisperContextParams whisperContextParams);

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

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

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

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial int whisper_full_with_state(IntPtr context, IntPtr state, WhisperFullParams parameters, IntPtr samples, int nSamples);

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

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial long whisper_full_get_segment_t0_from_state(IntPtr state, int index);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial long whisper_full_get_segment_t1_from_state(IntPtr state, int index);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial IntPtr whisper_full_get_segment_text_from_state(IntPtr state, int index);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial int whisper_full_n_tokens_from_state(IntPtr state, int index);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial float whisper_full_get_token_p_from_state(IntPtr state, int segmentIndex, int tokenIndex);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial int whisper_lang_max_id();

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial int whisper_lang_auto_detect_with_state(IntPtr context, IntPtr state, int offset_ms, int n_threads, IntPtr lang_probs);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial int whisper_pcm_to_mel_with_state(IntPtr context, IntPtr state, IntPtr samples, int nSamples, int nThreads);

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

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

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

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

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial void whisper_log_set(IntPtr logCallback, IntPtr user_data);

[LibraryImport(libraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial void whisper_ctx_init_openvino_encoder_with_state(IntPtr context, IntPtr state, IntPtr modelPath, IntPtr device, IntPtr cacheDir);

[LibraryImport(ggmlLibraryName, StringMarshalling = StringMarshalling.Utf8)]
public static partial void ggml_log_set(IntPtr logCallback, IntPtr user_data);

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;

public INativeWhisper.whisper_free Whisper_Free => whisper_free;

public INativeWhisper.whisper_free_params Whisper_Free_Params => whisper_free_params;

public INativeWhisper.whisper_full_default_params_by_ref Whisper_Full_Default_Params_By_Ref => whisper_full_default_params_by_ref;

public INativeWhisper.whisper_full_with_state Whisper_Full_With_State => whisper_full_with_state;

public INativeWhisper.whisper_full_n_segments_from_state Whisper_Full_N_Segments_From_State => whisper_full_n_segments_from_state;

public INativeWhisper.whisper_full_get_segment_t0_from_state Whisper_Full_Get_Segment_T0_From_State => whisper_full_get_segment_t0_from_state;

public INativeWhisper.whisper_full_get_segment_t1_from_state Whisper_Full_Get_Segment_T1_From_State => whisper_full_get_segment_t1_from_state;

public INativeWhisper.whisper_full_get_segment_text_from_state Whisper_Full_Get_Segment_Text_From_State => whisper_full_get_segment_text_from_state;

public INativeWhisper.whisper_full_n_tokens_from_state Whisper_Full_N_Tokens_From_State => whisper_full_n_tokens_from_state;

public INativeWhisper.whisper_full_get_token_p_from_state Whisper_Full_Get_Token_P_From_State => whisper_full_get_token_p_from_state;

public INativeWhisper.whisper_lang_max_id Whisper_Lang_Max_Id => whisper_lang_max_id;

public INativeWhisper.whisper_lang_auto_detect_with_state Whisper_Lang_Auto_Detect_With_State => whisper_lang_auto_detect_with_state;

public INativeWhisper.whisper_pcm_to_mel_with_state Whisper_PCM_To_Mel_With_State => whisper_pcm_to_mel_with_state;

public INativeWhisper.whisper_lang_str Whisper_Lang_Str => whisper_lang_str;

public INativeWhisper.whisper_init_state Whisper_Init_State => whisper_init_state;

public INativeWhisper.whisper_free_state Whisper_Free_State => whisper_free_state;

public INativeWhisper.whisper_full_lang_id_from_state Whisper_Full_Lang_Id_From_State => whisper_full_lang_id_from_state;

public INativeWhisper.whisper_log_set Whisper_Log_Set => whisper_log_set;

public INativeWhisper.whisper_ctx_init_openvino_encoder_with_state Whisper_Ctx_Init_Openvino_Encoder_With_State => whisper_ctx_init_openvino_encoder_with_state;

public INativeWhisper.ggml_log_set Ggml_log_set => ggml_log_set;

public void Dispose()
{

}
}

#endif
5 changes: 2 additions & 3 deletions Whisper.net/LibraryLoader/NativeLibraryLoader.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
// Licensed under the MIT license: https://opensource.org/licenses/MIT
using Whisper.net.Internals.Native.Implementations;
#if !IOS && !MACCATALYST && !TVOS && !ANDROID
#if !NETSTANDARD
using System.Runtime.Intrinsics.X86;
#endif

#if !IOS && !MACCATALYST && !TVOS && !ANDROID
using System.Runtime.InteropServices;
#endif

Expand All @@ -17,7 +16,7 @@ internal static LoadResult LoadNativeLibrary()
#if IOS || MACCATALYST || TVOS
return LoadResult.Success(new DllImportsNativeWhisper());
#elif ANDROID
return LoadResult.Success(new DllImportsNativeWhisper());
return LoadResult.Success(new LibraryImportNativeLibWhisper());
#else
// If the user has handled loading the library themselves, we don't need to do anything.
if (RuntimeOptions.Instance.BypassLoading
Expand Down
14 changes: 11 additions & 3 deletions tests/Whisper.net.Maui.Tests/MainPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed under the MIT license: https://opensource.org/licenses/MIT

using Whisper.net.Internals.Native.Implementations;
using Whisper.net.LibraryLoader;
using Whisper.net.Logger;

namespace Whisper.net.Maui.Tests;
Expand All @@ -22,10 +23,17 @@ private void OnCounterClicked(object sender, EventArgs e)

try
{
var dllImport = new DllImportsNativeWhisper();
LogProvider.InitializeLogging(dllImport);
var linuxLIbraryLoader = new LinuxLibraryLoader();
var openedLib = linuxLIbraryLoader.OpenLibrary("libggml.so", true);

if (openedLib == IntPtr.Zero)
{
var error = linuxLIbraryLoader.GetLastError();
CounterBtn.Text = error;
}

}
catch(Exception ex)
catch (Exception ex)
{
CounterBtn.Text = ex.Message;
// ignored
Expand Down

0 comments on commit 176bcd5

Please sign in to comment.