microsoft / onnxruntime

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

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

Open xydrolase opened 2 years ago

xydrolase commented 2 years ago

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:

https://github.com/microsoft/onnxruntime/blob/f3dcbf539aa410f31a0e4bce1807f79331fe1c1f/java/src/main/java/ai/onnxruntime/OnnxRuntime.java#L111-L112

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

To Reproduce

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.

pwittchen commented 2 years ago

I created PR which is probably addressing this issue in #12325 and is related to the discussion in #11054.

xydrolase commented 2 years ago

I created PR which is probably addressing this issue in #12325 and is related to the discussion in #11054.

Thanks for the pointers! The PR changes look good. Is there a SNAPSHOT version that we can use for testing?

Craigacp commented 2 years ago

If you build from source it packages the library correctly, there's something off with the packaging pipeline that makes the Maven Central jar.