Skip to content

Wrong native library directory name for M1 Mac in the Java package #12324

@xydrolase

Description

@xydrolase

Describe the bug
In the release notes of 1.12.0, it was stated that M1 Mac support is now provided for the onnxruntime Java package.

Inspecting the published JAR file on Maven Repository: https://mvnrepository.com/artifact/com.microsoft.onnxruntime/onnxruntime/1.12.0, indeed there are now libonnxruntime and libonnxruntime_jni native libraries pre-built for osx-arm64:

$ unzip -l onnxruntime-1.12.0.jar

Archive:  onnxruntime-1.12.0.jar
  Length      Date    Time    Name
---------  ---------- -----   ----
        0  07-22-2022 01:14   ai/
        0  07-22-2022 01:14   ai/onnxruntime/
     1275  07-21-2022 21:47   ai/onnxruntime/MapInfo.class
        0  07-22-2022 01:14   ai/onnxruntime/native/
        0  07-22-2022 01:14   ai/onnxruntime/native/linux-aarch64/
 14166576  07-22-2022 01:14   ai/onnxruntime/native/linux-aarch64/libonnxruntime.so
    67760  07-22-2022 01:14   ai/onnxruntime/native/linux-aarch64/libonnxruntime4j_jni.so
        0  07-22-2022 01:14   ai/onnxruntime/native/linux-x64/
 15200880  07-22-2022 01:14   ai/onnxruntime/native/linux-x64/libonnxruntime.so
    59592  07-22-2022 01:14   ai/onnxruntime/native/linux-x64/libonnxruntime4j_jni.so
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/
 19661016  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/
      649  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Info.plist
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Resources/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Resources/DWARF/
  6815902  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Resources/DWARF/libonnxruntime.dylib
    96376  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/
      655  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Info.plist
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/DWARF/
    16542  07-22-2022 01:14   ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/DWARF/libonnxruntime4j_jni.dylib
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/
 22081032  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib.dSYM/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib.dSYM/Contents/
      649  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib.dSYM/Contents/Info.plist
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib.dSYM/Contents/Resources/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib.dSYM/Contents/Resources/DWARF/
  6877342  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime.dylib.dSYM/Contents/Resources/DWARF/libonnxruntime.dylib
    62680  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib.dSYM/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib.dSYM/Contents/
      655  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib.dSYM/Contents/Info.plist
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/
        0  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/DWARF/
    16542  07-22-2022 01:14   ai/onnxruntime/native/osx-x64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/DWARF/libonnxruntime4j_jni.dylib
        0  07-21-2022 21:54   ai/onnxruntime/native/win-x64/
  8245760  07-21-2022 21:54   ai/onnxruntime/native/win-x64/onnxruntime.dll
203665408  07-21-2022 21:54   ai/onnxruntime/native/win-x64/onnxruntime.pdb
    58368  07-21-2022 21:54   ai/onnxruntime/native/win-x64/onnxruntime4j_jni.dll
   700416  07-21-2022 21:54   ai/onnxruntime/native/win-x64/onnxruntime4j_jni.pdb
      932  07-21-2022 21:47   ai/onnxruntime/NodeInfo.class
     2031  07-21-2022 21:47   ai/onnxruntime/OnnxJavaType$1.class

Notice that the native library for M1 Mac is located under the resource directory: ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib. In OnnxRuntime.java implementation, however, the os/arch name constructed by the initOsArch method is osx-aarch64:

} else if (arch.startsWith("aarch64")) {
detectedArch = "aarch64";

Therefore, when running onnxruntime 1.12.0 on a M1 Mac (which has the aarch64 arch):

@ System.getProperty("os.arch", "generic") 
res0: String = "aarch64"

the OrtEnvironment class cannot be initialized due to the failure of loading the native library:

[info]   java.lang.UnsatisfiedLinkError: no onnxruntime in java.library.path: [/Users/foo/Library/Java/Extensions, /Library/Java/Extensions, /Network/Library/Java/Extensions, /System/Library/Java/Extensions, /usr/lib/java, .]
[info]   at java.base/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2670)
[info]   at java.base/java.lang.Runtime.loadLibrary0(Runtime.java:830)
[info]   at java.base/java.lang.System.loadLibrary(System.java:1873)
[info]   at ai.onnxruntime.OnnxRuntime.load(OnnxRuntime.java:338)
[info]   at ai.onnxruntime.OnnxRuntime.init(OnnxRuntime.java:139)
[info]   at ai.onnxruntime.OrtEnvironment.<clinit>(OrtEnvironment.java:29)

Urgency
none

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): macOS Monterey 12.5 (M1 Chip)
  • ONNX Runtime installed from (source or binary): Binary
  • ONNX Runtime version: 1.12.0
  • Python version: N/A
  • Visual Studio version (if applicable):
  • GCC/Compiler version (if compiling from source):
  • CUDA/cuDNN version:
  • GPU model and memory:

To Reproduce

  • Describe steps/code to reproduce the behavior.
    Simply calling the static method ai.onnxruntime.OrtEnvironment.getEnvironment() with onnxruntime 1.12.0 Java package on a M1 Mac.

Expected behavior
My theory is that this error can be resolved by renaming the directory name from osx-arm64 to osx-aarch64 for the directory containing the native library.

Noticeably, the native library for Linux ARM is located under the directory: ai/onnxruntime/native/linux-aarch64. Therefore, the same treatment shall be applied to osx libraries as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleissues that have not been addressed in a while; categorized by a bot

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions