Skip to content
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

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

Merged
merged 3 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmake/ext_java.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions java/ortx_jni.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
LIBRARY "onnxruntime_extensions4j_jni.dll"
EXPORTS
RegisterCustomOps @1
AddExternalCustomOp @2
GetActiveOrtAPIVersion @3
Java_ai_onnxruntime_extensions_Utils_getNativeExtensionOperatorRegister @4
8 changes: 8 additions & 0 deletions java/ortx_jni.ver
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
global:
RegisterCustomOps;
AddExternalCustomOp;
GetActiveOrtAPIVersion;
Java_ai_onnxruntime_extensions_Utils_getNativeExtensionOperatorRegister;
local: *;
};
7 changes: 6 additions & 1 deletion tutorials/demo4j/README.md
Original file line number Diff line number Diff line change
@@ -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.
5 changes: 3 additions & 2 deletions tutorials/demo4j/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
12 changes: 10 additions & 2 deletions tutorials/demo4j/app/src/main/java/demo4j/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

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;
Expand All @@ -19,8 +20,15 @@ public String inference(){
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)) {
Expand Down
Loading