Skip to content

fixed packaging of onnxruntime java module for Mac OSX with Apple M1 Chip #12325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

pwittchen
Copy link

Description: Fixed packaging of onnxruntime java module for Mac OSX with Apple M1 Chip

Motivation and Context

@Craigacp
Copy link
Contributor

I would prefer to fix the packaging, which I thought should have been dealt with by the change here - https://github.com/microsoft/onnxruntime/pull/10981/files#r834505949.

@pwittchen
Copy link
Author

Mentioned PR was merged on 26th of March, but I (and other people) still can encounter that bug, so I guess more changes are needed. I'm not familiar with cmake configuration, so I'm not sure how to fix it in that place.

@snnn
Copy link
Member

snnn commented Jul 26, 2022

On macOS arm, what's the value of "os.arch" property for Java applications?

@pwittchen
Copy link
Author

pwittchen commented Jul 26, 2022

@snnn

On my basic Macbook Air M1:

System.getProperty("os.arch", "generic");

prints:

"aarch64"

so I guess change may be required both in cmake (already done) and Java code (this PR).

@Craigacp
Copy link
Contributor

Craigacp commented Jul 26, 2022

We should only need to change one of the location where the binary is placed by the build system and the location the Java loader looks, changing both is likely to cause the same problem.

If we build from source on an M1 Mac does it build the jar with it in osx-aarch64 or with osx-arm64? It might be the packaging that makes the jar for Maven Central that's causing the problem.

@pwittchen
Copy link
Author

pwittchen commented Jul 26, 2022

I downloaded jar file from the Maven Central repository here: https://search.maven.org/artifact/com.microsoft.onnxruntime/onnxruntime/1.12.0/jar and looked inside.

In the path ai/onnxruntime/native, we have directories:

  • linux-aarch64
  • linux-x64
  • osx-arm64
  • win-x64

Contents of the osx-arm64 looks like that:

/libonnxruntime.dylib.dSYM           (dir
/libonnxruntime4j_jni.dylib.dSYM     (dir)
 libonnxruntime.dylib                (size: 19200K)
 libonnxruntime4j_jni.dylib          (size: 96376)

It looks fine, but maybe something is wrong with the directories contents?

I also extracted my onnxruntime.jar, which I compiled from the source on Macbook M1 on 31st of March 2022 from the source and it works fine.

What is interesting, in my custom file in the path ai/onnxruntime/native I have:

  • osx-aarch64

and contents of this directory looks like that:

  • libonnxruntime.dylib (size: 16122K)
  • libonnxruntime4j_jni.dylib (size: 79815)

so the directory has different name and generated *.dylib are different (or compiled differently). In addition, I don't have directories with .dSYM posftix

Clearly, there are differences between my old *.jar file and *.jar file pushed to the Maven Central repository.

I even mentioned path in my compiled *.jar file, which was working correctly for my project on Mac with M1 Chip here: #11054 (comment) , so I guess there should be a directory named osx-aarch instead of osx-arm64 (or additionally) in order to make it work properly on Mac with M1. At least, I can conclude that from my tests.

I see two solutions:

  • this PR
  • changing output directory name in java artifact from osx-arm64 to osx-aarch64 (but where can I do that? maybe I will experiment with this later via cmake)

Please note:
It was compiling correctly on March 31st to osx-aarch64 directory and I tested it on Mac, so the bug was probably introduced in the code after 31st of March this year.

@Craigacp
Copy link
Contributor

Craigacp commented Jul 26, 2022

If the one compiled from source has the right structure (i.e. with a osx-aarch64) directory, then it means the packaging pipeline that outputs to Maven Central is putting the binary in the wrong place, so we should fix that.

I can run some tests on my personal M1 machine tonight. I have been using it for development of the JNI code recently without issue, which is why I suspect the packaging pipeline in Azure.

@pwittchen
Copy link
Author

Ok, thanks. I will wait for the update.

@Craigacp
Copy link
Contributor

Yeah, the v1.12.0 tag builds a jar containing osx-aarch64. So it's definitely the packaging pipeline. I had a look through the yaml and powershell that glues it together but I couldn't see any place where the folder name would be changed. @snnn any idea where I should look?

@snnn snnn closed this Jul 27, 2022
@snnn
Copy link
Member

snnn commented Jul 27, 2022

The root yaml which produces jars is https://github.com/microsoft/onnxruntime/blob/master/tools/ci_build/github/azure-pipelines/c-api-noopenmp-packaging-pipelines.yml .
It includes:java-api-artifacts-package-and-publish-steps-posix.yml

In the second yaml file, we call a script java_copy_strip_binary.sh in a way like:

/tools/ci_build/github/linux/java_copy_strip_binary.sh -r /Users/runner/work/1/b -c Release -a onnxruntime-java-osx-arm64 -l libonnxruntime.dylib -n libonnxruntime4j_jni.dylib -v 1.12.0 -h osx-arm64

sample output from the script:

+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ BINARY_DIR=/Users/runner/work/1/b
+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ BUILD_CONFIG=Release
+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ ARTIFACT_NAME=onnxruntime-java-osx-arm64
+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ LIB_NAME=libonnxruntime.dylib
+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ NATIVE_LIB_NAME=libonnxruntime4j_jni.dylib
+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ VERSION_NUMBER=1.12.0
+ getopts r:a:l:n:c:h:v: parameter_Option
+ case "${parameter_Option}" in
+ ARCH=osx-arm64
+ getopts r:a:l:n:c:h:v: parameter_Option
+ EXIT_CODE=1
+ uname -a
+ echo 'Version: 1.12.0'
+ [[ libonnxruntime.dylib == *.dylib ]]
+ [[ osx-arm64 == \o\s\x\-\x\8\6\_\6\4 ]]
+ NATIVE_FOLDER=ai/onnxruntime/native/osx-arm64
+ mkdir -p /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64

+ [[ libonnxruntime.dylib == *.dylib ]]
+ dsymutil /Users/runner/work/1/b/Release/libonnxruntime.dylib -o /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM
warning: no debug symbols in executable (-arch arm64)
+ cp /Users/runner/work/1/b/Release/libonnxruntime.dylib /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib
+ strip -S /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib
+ dsymutil /Users/runner/work/1/b/Release/libonnxruntime4j_jni.dylib -o /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM
warning: no debug symbols in executable (-arch arm64)
+ cp /Users/runner/work/1/b/Release/libonnxruntime4j_jni.dylib /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib
+ strip -S /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib
+ cp /Users/runner/work/1/b/Release/libcustom_op_library.dylib /Users/runner/work/1/b/onnxruntime-java-osx-arm64
+ find /Users/runner/work/1/b/onnxruntime-java-osx-arm64 -ls
12893961788        0 drwxr-xr-x    4 runner           staff                 128 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64
12893961789        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai
12893961790        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime
12893961791        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native
12893961792        0 drwxr-xr-x    6 runner           staff                 192 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64
12893961808      192 -rwxr-xr-x    1 runner           staff               96376 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib
12893961801        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM
12893961802        0 drwxr-xr-x    4 runner           staff                 128 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents
12893961803        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources
12893961804        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/DWARF
12893961806       40 -rw-r--r--    1 runner           staff               16542 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Resources/DWARF/libonnxruntime4j_jni.dylib
12893961805        8 -rw-r--r--    1 runner           staff                 655 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime4j_jni.dylib.dSYM/Contents/Info.plist
12893961793        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM
12893961794        0 drwxr-xr-x    4 runner           staff                 128 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents
12893961795        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Resources
12893961796        0 drwxr-xr-x    3 runner           staff                  96 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Resources/DWARF
12893961798    13328 -rw-r--r--    1 runner           staff             6819998 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Resources/DWARF/libonnxruntime.dylib
12893961797        8 -rw-r--r--    1 runner           staff                 649 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib.dSYM/Contents/Info.plist
12893961800    38408 -rwxr-xr-x    1 runner           staff            19663912 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/ai/onnxruntime/native/osx-arm64/libonnxruntime.dylib
12893961809      112 -rwxr-xr-x    1 runner           staff               55719 Jul 18 12:12 /Users/runner/work/1/b/onnxruntime-java-osx-arm64/libcustom_op_library.dylib
+ rm -fr /Users/runner/work/1/b/onnxruntime-java-osx-arm64/jar

@RandySheriffH
Copy link
Contributor

@pwittchen
Copy link
Author

pwittchen commented Aug 4, 2022

Thanks. I checked library from this patch in my project on Mac with M1 and it works fine. It works on Linux too.

@pwittchen pwittchen deleted the fix-m1-mac-packaging branch August 4, 2022 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants