microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
14.54k stars 2.91k forks source link

Onnxruntime NuGet package works on android apk builded with Mono but not with IL2CPP using Unity. #10427

Closed mblaszczyk97 closed 2 years ago

mblaszczyk97 commented 2 years ago

Describe the bug I'm using microsoft onnxruntime C# NuGet package with Unity. When I build app for android with Mono, it works as intended but if i change it to IL2CPP, following error happens:

2022.01.26 14:40:32.979 13461 13498 Info IL2CPP Locale pl-PL
2022.01.26 14:40:33.017 13461 13498 Error Unity MarshalDirectiveException: Cannot marshal P/Invoke call through delegate of type '.DOrtGetApi'
2022.01.26 14:40:33.017 13461 13498 Error Unity at Microsoft.ML.OnnxRuntime.NativeMethods..cctor () [0x00000] in <00000000000000000000000000000000>:0
2022.01.26 14:40:33.017 13461 13498 Error Unity at Microsoft.ML.OnnxRuntime.SessionOptions..ctor () [0x00000] in <00000000000000000000000000000000>:0
2022.01.26 14:40:33.017 13461 13498 Error Unity at SimpleOnnxTest.Start () [0x00000] in <00000000000000000000000000000000>:0
2022.01.26 14:40:33.017 13461 13498 Error Unity Rethrow as TypeInitializationException: The type initializer for 'Microsoft.ML.OnnxRuntime.NativeMethods' threw an exception.
2022.01.26 14:40:33.017 13461 13498 Error Unity at Microsoft.ML.OnnxRuntime.SessionOptions..ctor () [0x00000] in <00000000000000000000000000000000>:0
2022.01.26 14:40:33.017 13461 13498 Error Unity at SimpleOnnxTest.Start () [0x00000] in <00000000000000000000000000000000>:0
2022.01.26 14:40:33.017 13461 13498 Error Unity

Urgency None

System information

To Reproduce

  1. Import Microsoft.ML.OnnxRuntime 1.10.0 C# library in Unity app.
  2. Create call to some method in library. For example options.AppendExecutionProvider_Nnapi();
  3. Create build for android in Unity using IL2CPP.
  4. Run apk.

Expected behavior APK build is corectly loading OnnxRuntime library.

skottmckay commented 2 years ago

Not familiar with IL2CPP or Unity to know whether there is anything that could/should be updated on the ORT side. I added a reply on https://forum.unity.com/threads/mono-build-works-but-il2cpp-throws-error.1210260/ so hopefully a Unity dev can provide some insight.

skottmckay commented 2 years ago

As per https://forum.unity.com/threads/mono-build-works-but-il2cpp-throws-error.1210260/ the issue was on the Unity side and will be fixed in an upcoming release.

Cipher-zzz commented 2 years ago

@mblaszczyk97 Could you give me some ideas about how to install Onnxruntime NuGet into Unity? Thanks in advance!

mblaszczyk97 commented 2 years ago

@mblaszczyk97 Could you give me some ideas about how to install Onnx Runtime NuGet into Unity? Thanks in advance!

Hey. I was not responsible for compilation of Unity app but as far as I know Onnx Runtime library was compiled as any other additional library in C# for Unity. We used this blogpost - Add AI to mobile applications with Xamarin and ONNX Runtime and the newest NuGet package of Onnx Runtime. Hope this helps.

Cipher-zzz commented 2 years ago

@mblaszczyk97 Thank you!