onnxruntime: Unable to load shared library 'onnxruntime' on MacOS (DllNotFoundException)

Crash when trying to load onnxruntime when running console application on osx with .net6 core and macos workload. Dll’s looks to be in the bin folder, but can’t be found/loaded. No macos versions available in the nuget package?

Reproduction steps: Create a new .netcore 6 console application. Add macOS workload and set target framework to ‘net6.0-macos’. Add Microsoft.ML and OnnxRuntime from nuget. Add some code to call .ApplyOnnxModel(…). -> Crash

Setup: macOS 11.6 (x64) Visual Studio Mac 2022 (preview 1->3) OnnxRuntime 1.9.0 (from nuget) .NetCore 6.0 with macOS workload

Callstack:

Unhandled exception. System.TypeInitializationException: The type initializer for 'Microsoft.ML.OnnxRuntime.NativeMethods' threw an exception.
 ---> System.DllNotFoundException: Unable to load shared library 'onnxruntime' or one of its dependencies. In order to help diagnose loading problems, consider setting the DYLD_PRINT_LIBRARIES environment variable: dlopen(libonnxruntime, 1): image not found
   at Microsoft.ML.OnnxRuntime.NativeMethods.OrtGetApiBase()
   at Microsoft.ML.OnnxRuntime.NativeMethods..cctor()
   --- End of inner exception stack trace ---
   at Microsoft.ML.OnnxRuntime.SessionOptions..ctor()
   at Microsoft.ML.Transforms.Onnx.OnnxModel..ctor(String modelFile, Nullable`1 gpuDeviceId, Boolean fallbackToCpu, Boolean ownModelFile, IDictionary`2 shapeDictionary, Int32 recursionLimit, Nullable`1 interOpNumThreads, Nullable`1 intraOpNumThreads)
   at Microsoft.ML.Transforms.Onnx.OnnxTransformer..ctor(IHostEnvironment env, Options options, Byte[] modelBytes)
   at Microsoft.ML.Transforms.Onnx.OnnxTransformer..ctor(IHostEnvironment env, String[] outputColumnNames, String[] inputColumnNames, String modelFile, Nullable`1 gpuDeviceId, Boolean fallbackToCpu, IDictionary`2 shapeDictionary, Int32 recursionLimit, Nullable`1 interOpNumThreads, Nullable`1 intraOpNumThreads)
   at Microsoft.ML.Transforms.Onnx.OnnxScoringEstimator..ctor(IHostEnvironment env, String[] outputColumnNames, String[] inputColumnNames, String modelFile, Nullable`1 gpuDeviceId, Boolean fallbackToCpu, IDictionary`2 shapeDictionary, Int32 recursionLimit, Nullable`1 interOpNumThreads, Nullable`1 intraOpNumThreads)
   at Microsoft.ML.OnnxCatalog.ApplyOnnxModel(TransformsCatalog catalog, String[] outputColumnNames, String[] inputColumnNames, String modelFile, Nullable`1 gpuDeviceId, Boolean fallbackToCpu)

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 29 (6 by maintainers)

Most upvoted comments

@niyazidageek I still think something is wrong with the NuGet package because the .dylib is in the bin directory but just not linking correctly. Luckily installing the onnxruntime via homebrew links it correctly machine-wide.

It’s enough to get some code running at least. 😅

I was able to work around this issue on macOS by using homebrew to install the onnxruntime dependency.

brew install onnxruntime

It lets me run workloads, but I would expect the package to bring down the latest .dll.

@leXorNet thanks for the workaround.

The packaging of the nuget needs to be fixed for osx anyways.

the lib must be placed in runtimes/osx-x64/native/, but is currently placed in runtimes/osx.10.14-x64/native

respective for arm: the lib must be placed in runtimes/osx-arm64/native/, but is currently placed in runtimes/osx.10.14-arm64/native

If somebody is interested:

  • MKL == Math Kernel Library
  • installing Microsoft.ML.Mkl.Redist package does not fix the issue, because it only contains a “libMklProxyNative.dylib” for osx; see nuget.info: Microsoft.ML.Mkl.Redist
  • installing Microsoft.ML.OnnxRuntime.MKLML is only a workaround for intel macs (or x64 .net runtime on arm mac), because it contains the necessary native lib libonnxruntime.dylib, but only for x64 not arm64, and it contains unnecessary blis nuget.info: Microsoft.ML.OnnxRuntime.MKLML image

I believe the culprit is this line in generate_nspec_for_native_nuget.py. This code is responsible for creating a nuspec that is then packaged, and it creates the native runtimes folders for MacOS in a way that is specific to 10.14, as recorded above by @juwens. If this were changed to just osx-%s, you’d get a package that works correctly for all OSX consumers (at least barring any actual OS requirements on the part of the native code). This would be in line with how the win and linux platforms are provided as general baselines as well.

To test this, users can manually copy the osx.10.14-x64 directory from the package runtimes in their nuget cache (~/.nuget/package/microsoft.ml.onnxruntime/1.10.0 is the default location) to a sibling folder called osx-x64, then purge their obj and bin directories and re-publish their applications. At this point the application should run. I verified this offline with @khalidabuhakmeh.

@khalidabuhakmeh Wow! Thank you for the solution!!! And also, installing previous version of onnxruntime helped me as well. I had 1.10.0, which was not working on Mac OS, however, 1.8.1 runs perfectly. Anyways, thank you!

.net 6 support was added in the most recent release - 1.12 - and net6.0-macos is included as a target framework.

@silveredgold maybe create a new issue with full details of your setup such as ORT version, which nuget packages you’re using, what framework/s you’re targeting etc.

@baronfel thanks, I was also searching for this line, but without success. Thanks for the PR 👌