dotnet / maui

.NET MAUI is the .NET Multi-platform App UI, a framework for building native device applications spanning mobile, tablet, and desktop.
https://dot.net/maui
MIT License
21.83k stars 1.67k forks source link

OnnxRuntime GPU does not work in MAUI project #13860

Closed thuongmhh closed 1 year ago

thuongmhh commented 1 year ago

Description

I'm trying to make OnnxRuntime GPU library work with .NET MAUI.

I call the method SessionOptions.MakeSessionOptionWithCudaProvider() and got the OnnxRuntimeException: " LoadLibrary failed with error 126 when trying to load onnxruntime_providers_cuda.dll". I checked that dll exists in the output directory.

I can successfully use the library in the following cases:

For that reason, I think that this is a MAUI bug.

Steps to Reproduce

  1. Create a MAUI Project
  2. Add this package: https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime.Gpu/
  3. Call SessionOptions.MakeSessionOptionWithCudaProvider()

Link to public reproduction project repository

https://github.com/thuongmhh/OnnxRunimeGpuBug

Version with bug

6.0.312

Last version that worked well

Unknown/Other

Affected platforms

Windows

Affected platform versions

Windows 10.0.22621

Did you find any workaround?

Only the CPU version of OnnxRuntime work in MAUI project now.

Relevant log output

Microsoft.ML.OnnxRuntime.OnnxRuntimeException
HResult=0x80131500
Message=[ErrorCode:RuntimeException] D:\a_work\1\s\onnxruntime\core\session\provider_bridge_ort.cc:1103 onnxruntime::ProviderLibrary::Get [ONNXRuntimeError] : 1 : FAIL : LoadLibrary failed with error 126 "" when trying to load "D:\Projects...\bin\Debug\net7.0-windows10.0.19041.0\win10-x64\AppX\onnxruntime_providers_cuda.dll"

Source=Microsoft.ML.OnnxRuntime
StackTrace:
at Microsoft.ML.OnnxRuntime.NativeApiStatus.VerifySuccess(IntPtr nativeStatus)
at Microsoft.ML.OnnxRuntime.SessionOptions.AppendExecutionProvider_CUDA(Int32 deviceId)
at Microsoft.ML.OnnxRuntime.SessionOptions.MakeSessionOptionWithCudaProvider(Int32 deviceId)
at VitCon.RemoveBackground.App.MauiProgram.CreateMauiApp() in D:\Projects\RemoveBackground\VitCon.RemoveBackground.App\MauiProgram.cs:line 13
at VitCon.RemoveBackground.App.WinUI.App.CreateMauiApp() in D:\Projects\RemoveBackground\VitCon.RemoveBackground.App\Platforms\Windows\App.xaml.cs:line 22
at Microsoft.Maui.MauiWinUIApplication.OnLaunched(LaunchActivatedEventArgs args)
at Microsoft.UI.Xaml.Application.Microsoft.UI.Xaml.IApplicationOverrides.OnLaunched(LaunchActivatedEventArgs args)
at ABI.Microsoft.UI.Xaml.IApplicationOverrides.Do_Abi_OnLaunched_0(IntPtr thisPtr, IntPtr args)
drasticactions commented 1 year ago

@thuongmhh Did you try running this on a WinUI application without MAUI? If it works there, it should work with MAUI.

thuongmhh commented 1 year ago

Yes, I tried this on a WinUI application without MAUI, and it does work. It's strange that it doesn't work with MAUI. I contacted the developer of OnnxRuntime and they said that "I am not familiar with MAUI dll search order, but it seems to me that CUDA libraries can not be found from the app."

drasticactions commented 1 year ago

Can you make a reproduction app showing it working on WinUI without MAUI?

thuongmhh commented 1 year ago

@drasticactions: Sorry I misread your question. It works with WinForm, not WinUI. In WinUI project, it raises the same exception.

mattleibow commented 1 year ago

@thuongmhh based on your last comment, I opened this issue: https://github.com/microsoft/WindowsAppSDK/issues/3500

If you have that WinUI repro project lying about still, please go ahead and link to it so I can make sure the WinUI team has all the things they need!

mattleibow commented 1 year ago

Duplicate of https://github.com/microsoft/WindowsAppSDK/issues/3500