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

Java API for onnxruntime #2215

Merged
merged 76 commits into from
Dec 10, 2019
Merged
Show file tree
Hide file tree
Changes from 54 commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
88f2f55
Added a Java API, plus unit tests ported from the C# API in v0.5.0.
Craigacp Oct 21, 2019
023fff3
Fixes for the JNI build on macOS.
Craigacp Oct 23, 2019
157a7f3
Adding two JVM startup flags which turn on additional debug logging o…
Craigacp Oct 28, 2019
46521e7
Fixing a typo and missing logging statement.
Craigacp Oct 28, 2019
72813bc
Handle flatten string array as input/output of onnx
Nov 3, 2019
5f590fb
Remove duplicated example
Nov 3, 2019
7c89d7d
Updating copyright statement to include license reference.
Craigacp Nov 4, 2019
bccfe61
Repackaging to ai.onnxruntime.
Craigacp Nov 4, 2019
14fa539
Renaming native code.
Craigacp Nov 4, 2019
e226c83
Small fixes to names & javadoc.
Craigacp Nov 4, 2019
6dfa0bc
Merge branch 'java-api-fix-string' of https://github.com/XciD/onnxrunt…
Craigacp Nov 4, 2019
94119e5
Fixing the issue where a multidimensional String is passed to createT…
Craigacp Nov 4, 2019
75df6db
Merge branch 'XciD-java-api-fix-string' into java-api
Craigacp Nov 4, 2019
c37c8fb
Comment and exception error message fixes for ONNXAllocator.
Craigacp Nov 4, 2019
6ede863
Tidying up the array creation via reflection.
Craigacp Nov 5, 2019
19e0c02
Refactoring the reshape method to reduce code size.
Craigacp Nov 13, 2019
a8d65a3
Fixes to make the JNI code compile with -Werror -Wunused-parameter.
Craigacp Nov 20, 2019
b018923
Initial pass at CMake integration for the Java build. Does not produc…
Craigacp Nov 20, 2019
0d5f92d
Fix for clang warning.
Craigacp Nov 20, 2019
607ac1e
More work on Java CMake build.
Craigacp Nov 20, 2019
078d6b5
Merge branch 'master' into java-api
Craigacp Nov 20, 2019
4fa9927
Change the java provider ifdefs to trigger on the values set by CMake.
Craigacp Nov 20, 2019
f5d45b6
Finished the Java/CMake integration for builds. Doesn't run the tests…
Craigacp Nov 20, 2019
bc9c17f
Making two jars, one with binaries, one without.
Craigacp Nov 25, 2019
d447bde
Adding the onnx Java protobuf file.
Craigacp Nov 26, 2019
1607412
Fixing the Java providers so they don't trigger the unused parameter …
Craigacp Nov 26, 2019
e1da6e8
Changes to the gradle build to support running the tests from CMake a…
Craigacp Nov 26, 2019
f32f522
Adding initial Java test support.
Craigacp Nov 26, 2019
db96bb9
Integration of the Java tests with ctest. All the Java tests are exec…
Craigacp Nov 26, 2019
e476701
Starting the renaming of the Java classes.
Craigacp Nov 26, 2019
c6810ac
Renaming the JNI parts of the Java API.
Craigacp Nov 26, 2019
11c2da7
Finishing the renaming of the Java classes and JNI code.
Craigacp Nov 26, 2019
aacd5a4
Updating the CMake to build the renamed Java files.
Craigacp Nov 27, 2019
b5c8e44
Refactoring OrtEnvironment to enforce that it's a singleton and autoc…
Craigacp Nov 27, 2019
d56d38d
OrtSession.run now accepts Map<String,OnnxTensor> and returns Map<Str…
Craigacp Dec 2, 2019
a17b953
Adding some java specific things to gitignore.
Craigacp Dec 2, 2019
9d9fbb4
Converting OrtSession.run() over to produce an AutoCloseable Result o…
Craigacp Dec 2, 2019
d0cd066
Enabling some disabled Java tests.
Craigacp Dec 2, 2019
df89715
Merge branch 'master' into java-api
Craigacp Dec 2, 2019
f4691ef
Renaming the JNI helper ONNXUtil.c to OrtJniUtil.c
Craigacp Dec 3, 2019
2387821
Minor cleanup of Java bits, adding toString to some classes, removing…
Craigacp Dec 3, 2019
fd79837
Adding Java method to control the telemetry.
Craigacp Dec 3, 2019
b664d89
Adding a method to get ByteBuffers (and other primitive buffers) out …
Craigacp Dec 4, 2019
5677541
Removing gradle project files.
Craigacp Dec 4, 2019
4e8fc9e
Removing old comments from OrtSession.SessionOptions.
Craigacp Dec 4, 2019
7836689
Slight rearrangement of OrtAllocator.close to make it not set the def…
Craigacp Dec 4, 2019
441cd55
Converting the SessionOptions.setSequentialExecution method over to e…
Craigacp Dec 4, 2019
c0d1438
Small javadoc update.
Craigacp Dec 4, 2019
f22b6bb
Fixing a bug in Java's convertExecutionMode function.
Craigacp Dec 4, 2019
2a195c5
Updating a java test after changing the execution mode to an enum.
Craigacp Dec 4, 2019
86080bb
Adding javadoc to cmake, fixing javadoc warnings.
Craigacp Dec 4, 2019
8bcfd92
Merge branch 'master' into java-api
Craigacp Dec 4, 2019
ff3e5db
Moving createTensor to OnnxTensor. Updating to use DNNL rather than M…
Craigacp Dec 5, 2019
4d03337
Adding more javadoc.
Craigacp Dec 5, 2019
9e2dc06
Small changes to javadoc and naming in OrtEnvironment.
Craigacp Dec 5, 2019
0c26d4e
Switching use_mkldnn for use_dnnl in linux-ci-pipeline-with-java.yml
Craigacp Dec 5, 2019
5b796cd
Adding --build_java to win-ci-pipeline.yml
Craigacp Dec 5, 2019
fd034b4
Adding file encoding to javac command.
Craigacp Dec 5, 2019
9ea9928
Fix for fp16 to float C conversion.
Craigacp Dec 5, 2019
ef2cdd3
Fixing the fp16 to float conversion again.
Craigacp Dec 5, 2019
5343746
Adding documentation for the Java API.
Craigacp Dec 5, 2019
bac2da2
Updating the cmake to give the javadoc window a title.
Craigacp Dec 5, 2019
443d653
Different fix for the javadoc window title. The create_javadoc comman…
Craigacp Dec 5, 2019
aef09cc
Updated Java CMake build to hopefully fix the windows issues.
Craigacp Dec 5, 2019
6a211ae
Replacing the copyright symbol with (c) to make the java files ASCII.
Craigacp Dec 6, 2019
f9f037c
Fix for nuphar on Java.
Craigacp Dec 6, 2019
9a0691c
Updating the Java tests to minimise the cascading errors issue.
Craigacp Dec 6, 2019
a8d0a8b
Adding a couple of new overloads to OnnxTensor.createTensor for deali…
Craigacp Dec 6, 2019
26ab79b
Merge branch 'master' into java-api
Craigacp Dec 6, 2019
40b1756
Adding a patch for Windows file paths in Java.
Craigacp Dec 9, 2019
ca4dcd2
added path to build dir before running test
shahasad Dec 9, 2019
4f1f1da
fix syntax error
shahasad Dec 9, 2019
399c847
Merge branch 'master' of https://github.com/Microsoft/onnxruntime int…
shahasad Dec 9, 2019
5b42825
try localising the path setting
shahasad Dec 9, 2019
b4a79e6
Merge branch 'master' into java-api
shahasad Dec 10, 2019
4d468c5
added an echo %PATH% after path setting
shahasad Dec 10, 2019
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,6 @@ onnxprofile_profile_test_*.json
/csharp/packages
/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.targets
/csharp/src/Microsoft.ML.OnnxRuntime/Microsoft.ML.OnnxRuntime.props
# Java specific ignores
java/src/main/native/ai_onnxruntime_*.h
java/.gradle
6 changes: 6 additions & 0 deletions cmake/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,11 @@ if (onnxruntime_BUILD_SERVER)
include(onnxruntime_server.cmake)
endif()

if (onnxruntime_BUILD_JAVA)
message(STATUS "Java Build is enabled")
include(onnxruntime_java.cmake)
endif()

# some of the tests rely on the shared libs to be
# built; hence the ordering
if (onnxruntime_BUILD_UNIT_TESTS)
Expand Down Expand Up @@ -756,3 +761,4 @@ if (onnxruntime_BUILD_CSHARP)
# set_property(GLOBAL PROPERTY VS_DOTNET_TARGET_FRAMEWORK_VERSION "netstandard2.0")
include(onnxruntime_csharp.cmake)
endif()

97 changes: 97 additions & 0 deletions cmake/onnxruntime_java.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
# Licensed under the MIT License.

#set(CMAKE_VERBOSE_MAKEFILE on)

# Setup Java compilation
include(FindJava)
find_package(Java REQUIRED)
find_package(JNI REQUIRED)
include(UseJava)
include_directories(${JNI_INCLUDE_DIRS})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c11")

set(JAVA_ROOT ${REPO_ROOT}/java)
set(CMAKE_JAVA_COMPILE_FLAGS "-source" "1.8" "-target" "1.8")
if (onnxruntime_RUN_ONNX_TESTS)
set(JAVA_DEPENDS onnxruntime ${test_data_target})
else()
set(JAVA_DEPENDS onnxruntime)
endif()

# Specify the Java source files
set(onnxruntime4j_src
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/MapInfo.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/NodeInfo.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OnnxRuntime.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OnnxJavaType.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OnnxMap.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OnnxSequence.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OnnxTensor.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OnnxValue.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OrtAllocator.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OrtEnvironment.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OrtException.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OrtSession.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/OrtUtil.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/package-info.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/SequenceInfo.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/TensorInfo.java
${REPO_ROOT}/java/src/main/java/ai/onnxruntime/ValueInfo.java
)

# Build the jar and generate the native headers
add_jar(onnxruntime4j SOURCES ${onnxruntime4j_src} VERSION ${ORT_VERSION} GENERATE_NATIVE_HEADERS onnxruntime4j_generated DESTINATION ${REPO_ROOT}/java/src/main/native/)

# Specify the native sources (without the generated headers)
file(GLOB onnxruntime4j_native_src
"${REPO_ROOT}/java/src/main/native/*.c"
"${REPO_ROOT}/java/src/main/native/OrtJniUtil.h"
"${REPO_ROOT}/include/onnxruntime/core/session/*.h"
)

# Build the JNI library
add_library(onnxruntime4j_jni SHARED ${onnxruntime4j_native_src} ${onnxruntime4j_generated})
onnxruntime_add_include_to_target(onnxruntime4j_jni onnxruntime_session)
target_include_directories(onnxruntime4j_jni PRIVATE ${REPO_ROOT}/include ${REPO_ROOT}/java/src/main/native)
target_link_libraries(onnxruntime4j_jni PUBLIC ${JNI_LIBRARIES} onnxruntime onnxruntime4j_generated)

# Now the jar, jni binary and shared lib binary have been built, now to build the jar with the binaries added.

# This blob creates the new jar name
get_property(onnxruntime_jar_name TARGET onnxruntime4j PROPERTY JAR_FILE)
get_filename_component(onnxruntime_jar_abs ${onnxruntime_jar_name} ABSOLUTE)
get_filename_component(jar_path ${onnxruntime_jar_abs} DIRECTORY)
set(onnxruntime_jar_binaries_name "${jar_path}/onnxruntime4j-${ORT_VERSION}-with-binaries.jar")
set(onnxruntime_jar_binaries_platform "$<SHELL_PATH:${onnxruntime_jar_binaries_name}>")

# Copy the current jar
add_custom_command(TARGET onnxruntime4j_jni PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${onnxruntime_jar_name}
${onnxruntime_jar_binaries_platform})

# Make a temp directory to store the binaries
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory "${CMAKE_CURRENT_BINARY_DIR}/java-libs/lib")

# Copy the binaries
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_FILE:onnxruntime4j_jni>" ${CMAKE_CURRENT_BINARY_DIR}/java-libs/lib/)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_LINKER_FILE:onnxruntime>" ${CMAKE_CURRENT_BINARY_DIR}/java-libs/lib/)

# Update the with-binaries jar so it includes the binaries
add_custom_command(
TARGET onnxruntime4j_jni POST_BUILD
COMMAND ${Java_JAR_EXECUTABLE} -uf ${onnxruntime_jar_binaries_platform} -C ${CMAKE_CURRENT_BINARY_DIR}/java-libs lib/$<TARGET_FILE_NAME:onnxruntime4j_jni> -C ${CMAKE_CURRENT_BINARY_DIR}/java-libs lib/$<TARGET_LINKER_FILE_NAME:onnxruntime>
DEPENDS onnxruntime4j
COMMENT "Rebuilding Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
VERBATIM
)

create_javadoc(onnxruntime4j_javadoc
FILES ${onnxruntime4j_src}
DOCTITLE "Onnx Runtime Java API"
AUTHOR FALSE
USE TRUE
VERSION FALSE
)
36 changes: 35 additions & 1 deletion cmake/onnxruntime_unittests.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -800,7 +800,6 @@ list(APPEND onnxruntime_mlas_test_libs Threads::Threads)
target_link_libraries(onnxruntime_mlas_test PRIVATE ${onnxruntime_mlas_test_libs})
set_target_properties(onnxruntime_mlas_test PROPERTIES FOLDER "ONNXRuntimeTest")


add_library(custom_op_library SHARED ${REPO_ROOT}/onnxruntime/test/testdata/custom_op_library/custom_op_library.cc)
target_include_directories(custom_op_library PRIVATE ${REPO_ROOT}/include)
if(UNIX)
Expand All @@ -814,3 +813,38 @@ else()
# need to ignore the linker warning 4199, due to some global linker flags failing here
endif()
set_property(TARGET custom_op_library APPEND_STRING PROPERTY LINK_FLAGS ${ONNXRUNTIME_CUSTOM_OP_LIB_LINK_FLAG})

if (onnxruntime_BUILD_JAVA)
message(STATUS "Running Java tests")
# Build and run tests
set(onnxruntime4j_test_src
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/InferenceTest.java
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/TestHelpers.java
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/OnnxMl.java
${REPO_ROOT}/java/src/test/java/ai/onnxruntime/UtilTest.java
)

# Create test directories
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/java-tests/")
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/java-tests/results")

# Download test dependencies
if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar)
message("Downloading JUnit 5")
file(DOWNLOAD https://repo1.maven.org/maven2/org/junit/platform/junit-platform-console-standalone/1.5.2/junit-platform-console-standalone-1.5.2.jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar EXPECTED_HASH SHA1=8d937d2b461018a876836362b256629f4da5feb1)
endif()

if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar)
message("Downloading protobuf-java 3.10.0")
file(DOWNLOAD https://repo1.maven.org/maven2/com/google/protobuf/protobuf-java/3.10.0/protobuf-java-3.10.0.jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar EXPECTED_HASH SHA1=410b61dd0088aab4caa05739558d43df248958c9)
endif()

# Build the test jar
add_jar(onnxruntime4j_test SOURCES ${onnxruntime4j_test_src} VERSION ${ORT_VERSION} INCLUDE_JARS ${onnxruntime_jar_name} ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.jar)

add_dependencies(onnxruntime4j_test onnxruntime4j_jni onnxruntime4j)
get_property(onnxruntime_test_jar_name TARGET onnxruntime4j_test PROPERTY JAR_FILE)

# Run the tests with JUnit's console launcher
add_test(NAME java-api COMMAND ${Java_JAVA_EXECUTABLE} -jar ${CMAKE_CURRENT_BINARY_DIR}/java-tests/junit-platform-console-standalone-1.5.2.jar -cp ${onnxruntime_test_jar_name} -cp ${onnxruntime_jar_binaries_platform} --scan-class-path --fail-if-no-tests --reports-dir=${CMAKE_CURRENT_BINARY_DIR}/java-tests/results --disable-banner WORKING_DIRECTORY ${REPO_ROOT})
endif()
56 changes: 56 additions & 0 deletions java/src/main/java/ai/onnxruntime/MapInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
* Licensed under the MIT License.
*/
package ai.onnxruntime;

/**
* Describes an {@link OnnxMap} object or output node.
*/
public class MapInfo implements ValueInfo {

/**
* The number of entries in this map.
*/
public final int size;

/**
* The Java type of the keys.
*/
public final OnnxJavaType keyType;

/**
* The Java type of the values.
*/
public final OnnxJavaType valueType;

/**
* Construct a MapInfo with the specified key type and value type.
* The size is unknown and set to -1.
* @param keyType The Java type of the keys.
* @param valueType The Java type of the values.
*/
MapInfo(OnnxJavaType keyType, OnnxJavaType valueType) {
this.size = -1;
this.keyType = keyType;
this.valueType = valueType;
}

/**
* Construct a MapInfo with the specified size, key type and value type.
* @param size The size.
* @param keyType The Java type of the keys.
* @param valueType The Java type of the values.
*/
MapInfo(int size, OnnxJavaType keyType, OnnxJavaType valueType) {
this.size = size;
this.keyType = keyType;
this.valueType = valueType;
}

@Override
public String toString() {
String initial = size == -1 ? "MapInfo(size=UNKNOWN" : "MapInfo(size="+size;
return initial + ",keyType=" + keyType.toString() + ",valueType=" + valueType.toString() + ")";
}
}
52 changes: 52 additions & 0 deletions java/src/main/java/ai/onnxruntime/NodeInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright © 2019, Oracle and/or its affiliates. All rights reserved.
* Licensed under the MIT License.
*/
package ai.onnxruntime;

/**
* The info for an input or output node from an ONNX model.
*/
public class NodeInfo {

private final ValueInfo info;

private final String name;

/**
* Creates a node info object from the supplied name and value info.
*
* Called from native code.
* @param name The name of the node.
* @param info The ValueInfo for this node.
*/
public NodeInfo(String name, ValueInfo info) {
this.name = name;
this.info = info;
}

/**
* The name of the node.
* @return The name.
*/
public String getName() {
return name;
}

/**
* The type and shape information of this node.
* <p>
* WARNING: {@link MapInfo} and {@link SequenceInfo} instances returned by this type will have insufficient information in
* them as it's not available from the model without an example output.
* @return The information of the value.
*/
public ValueInfo getInfo() {
return info;
}

@Override
public String toString() {
return "NodeInfo(name="+name+",info="+info.toString()+")";
}

}
Loading