Fix the windows API missing issue and Linux shared library size issue for Java packaging. (#774)

* Fix the java packaging issues

* add the jar path example for Linux build with a default configuration
This commit is contained in:
Wenbing Li 2024-07-29 16:03:58 -07:00 коммит произвёл GitHub
Родитель c3145b8f52
Коммит c9c11b4846
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
6 изменённых файлов: 45 добавлений и 5 удалений

Просмотреть файл

@ -54,6 +54,9 @@ file(GLOB onnxruntime_extensions4j_native_src
"${JAVA_ROOT}/src/main/native/*.h"
"${PROJECT_SOURCE_DIR}/include/*.h"
)
if(WIN32)
list(APPEND onnxruntime_extensions4j_native_src "${JAVA_ROOT}/ortx_jni.def")
endif()
# Build the JNI library
add_library(onnxruntime_extensions4j_jni SHARED ${onnxruntime_extensions4j_native_src})
@ -70,6 +73,15 @@ else()
target_link_libraries(onnxruntime_extensions4j_jni PRIVATE ortcustomops)
endif()
if(LINUX)
set_property(TARGET onnxruntime_extensions4j_jni APPEND_STRING PROPERTY LINK_FLAGS
" -Wl,--version-script -Wl,${JAVA_ROOT}/ortx_jni.ver")
# strip if not a debug build
if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug")
set_property(TARGET onnxruntime_extensions4j_jni APPEND_STRING PROPERTY LINK_FLAGS " -Wl,-s")
endif()
endif()
standardize_output_folder(onnxruntime_extensions4j_jni)
# Set platform and arch for packaging

6
java/ortx_jni.def Normal file
Просмотреть файл

@ -0,0 +1,6 @@
LIBRARY "onnxruntime_extensions4j_jni.dll"
EXPORTS
RegisterCustomOps @1
AddExternalCustomOp @2
GetActiveOrtAPIVersion @3
Java_ai_onnxruntime_extensions_Utils_getNativeExtensionOperatorRegister @4

8
java/ortx_jni.ver Normal file
Просмотреть файл

@ -0,0 +1,8 @@
{
global:
RegisterCustomOps;
AddExternalCustomOp;
GetActiveOrtAPIVersion;
Java_ai_onnxruntime_extensions_Utils_getNativeExtensionOperatorRegister;
local: *;
};

Просмотреть файл

@ -1,4 +1,9 @@
# How to start
1. copy JAR package from $REPO/out/$OS/RelWithDebInfo/java/build/libs/onnxruntime-extensions-${VERSION}.jar into app/libs folder.
1. correct the onnxruntime-extensions JAR location if needed, which is located at app/build.gradle: 16
`implementation fileTree(dir: '..\\..\\..\\out\\Windows\\java\\build\\libs', include: ['onnxruntime-extensions-0.*.0.jar'])
`
2. build and run this java project.

Просмотреть файл

@ -12,8 +12,9 @@ dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.7.2'
// onnxruntime and its extensions package
implementation 'com.microsoft.onnxruntime:onnxruntime:1.12.1'
implementation files('libs/onnxruntime-extensions-0.5.0.jar')
implementation 'com.microsoft.onnxruntime:onnxruntime:1.17.1'
implementation fileTree(dir: '..\\..\\..\\out\\Windows\\java\\build\\libs', include: ['onnxruntime-extensions-0.*.0.jar'])
// implementation fileTree(dir: '../../../out/Linux/RelWithDebInfo/java/build/libs', include: ['onnxruntime-extensions-0.*.0.jar'])
// This dependency is used by the application.
implementation 'com.google.guava:guava:30.1.1-jre'

Просмотреть файл

@ -5,6 +5,7 @@ package demo4j;
import java.util.Arrays;
import java.util.Map;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import ai.onnxruntime.*;
import ai.onnxruntime.extensions.OrtxPackage;
@ -19,8 +20,15 @@ public class App {
sess_opt.registerCustomOpLibrary(OrtxPackage.getLibraryPath());
/* do a quick inference on Bert Tokenizer custom ops with Ort */
var modelPath = Paths.get(this.getClass().getClassLoader().getResource("test_bert_tokenizer.onnx").getPath());
var session = env.createSession(modelPath.toString(), sess_opt);
var modelPath = "";
try {
modelPath = Paths.get(this.getClass().getClassLoader().getResource("test_bert_tokenizer.onnx").toURI()).toString();
} catch (URISyntaxException e) {
// handle the exception
e.printStackTrace();
}
var session = env.createSession(modelPath, sess_opt);
var t1 = OnnxTensor.createTensor(env, new String[]{"This is a test"});
var inputs = Map.of("text", t1);
try (var r = session.run(inputs)) {