Skip to content

Commit 4fb9e51

Browse files
CraigacpDu Li
authored andcommitted
[Java] Initial Apple Silicon support (#5891)
* Rearranging checks in onnxruntime_mlas.cmake to pickup Apple Silicon. On an M1 Macbook Pro clang reports: $ clang -dumpmachine arm64-apple-darwin20.1.0 So the regex check needs to look for "arm64" first, as otherwise it matches 32-bit ARM and you get NEON compilation failures. * Adding Java side library loading support for Apple Silicon (and other aarch64 architectures). * Adding Qgemm fix from @tracysh * Fixes the java packaging on Windows. * Missed a check in the java platform detector. (cherry picked from commit 8b83c51)
1 parent 64dfdb2 commit 4fb9e51

File tree

4 files changed

+35
-6
lines changed

4 files changed

+35
-6
lines changed

cmake/onnxruntime_java.cmake

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,31 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Android")
100100
file(MAKE_DIRECTORY ${ANDROID_PACKAGE_OUTPUT_DIR})
101101
endif()
102102

103-
# Set platform and ach for packaging
103+
# Set platform and arch for packaging
104+
# Checks the names set by MLAS on non-Windows platforms first
104105
if (CMAKE_SYSTEM_NAME STREQUAL "Android")
105106
set(JNI_ARCH ${ANDROID_ABI})
106-
elseif (CMAKE_SIZEOF_VOID_P EQUAL "8")
107+
elseif (ARM64)
108+
set(JNI_ARCH aarch64)
109+
elseif (X86_64)
107110
set(JNI_ARCH x64)
111+
elseif (POWER)
112+
set(JNI_ARCH ppc64)
108113
else()
109-
message(FATAL_ERROR "Java is currently not supported for x86 architecture")
114+
# Now mirror the checks used with MSVC
115+
if(MSVC)
116+
if(onnxruntime_target_platform STREQUAL "ARM64")
117+
set(JNI_ARCH aarch64)
118+
elseif(onnxruntime_target_platform STREQUAL "x64")
119+
set(JNI_ARCH x64)
120+
else()
121+
# if everything else failed then we're on a 32-bit arch and Java isn't supported
122+
message(FATAL_ERROR "Java is currently not supported on 32-bit x86 architecture")
123+
endif()
124+
else()
125+
# if everything else failed then we're on a 32-bit arch and Java isn't supported
126+
message(FATAL_ERROR "Java is currently not supported on 32-bit x86 architecture")
127+
endif()
110128
endif()
111129

112130
if (WIN32)

cmake/onnxruntime_mlas.cmake

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,9 @@ else()
151151
OUTPUT_VARIABLE dumpmachine_output
152152
ERROR_QUIET
153153
)
154-
if(dumpmachine_output MATCHES "^arm.*")
154+
if(dumpmachine_output MATCHES "^arm64.*")
155+
set(ARM64 TRUE)
156+
elseif(dumpmachine_output MATCHES "^arm.*")
155157
set(ARM TRUE)
156158
elseif(dumpmachine_output MATCHES "^aarch64.*")
157159
set(ARM64 TRUE)

java/src/main/java/ai/onnxruntime/OnnxRuntime.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,15 @@ private static String initOsArch() {
5959
}
6060
String detectedArch = null;
6161
String arch = System.getProperty("os.arch", "generic").toLowerCase(Locale.ENGLISH);
62-
if (arch.indexOf("amd64") == 0 || arch.indexOf("x86_64") == 0) {
62+
if (arch.startsWith("amd64") || arch.startsWith("x86_64")) {
6363
detectedArch = "x64";
64-
} else if (arch.indexOf("x86") == 0) {
64+
} else if (arch.startsWith("x86")) {
65+
// 32-bit x86 is not supported by the Java API
6566
detectedArch = "x86";
67+
} else if (arch.startsWith("aarch64")) {
68+
detectedArch = "aarch64";
69+
} else if (arch.startsWith("ppc64")) {
70+
detectedArch = "ppc64";
6671
} else if (isAndroid()) {
6772
detectedArch = arch;
6873
} else {

onnxruntime/core/mlas/lib/aarch64/QgemmU8X8KernelNeon.S

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,11 @@ Abstract:
2323

2424
.equ .LGemmU8X8KernelFrame_ColumnSumBuffer, 0
2525
.equ .LGemmU8X8KernelFrame_DepthValue, 8
26+
#if defined(__APPLE__)
27+
.equ .LGemmU8X8KernelFrame_ZeroMode, 12
28+
#else
2629
.equ .LGemmU8X8KernelFrame_ZeroMode, 16
30+
#endif
2731

2832
.text
2933

0 commit comments

Comments
 (0)