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 74 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 http://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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ Additional dockerfiles can be found [here](./dockerfiles).
* [C](docs/C_API.md)
* [C#](docs/CSharp_API.md)
* [C++](./include/onnxruntime/core/session/onnxruntime_cxx_api.h)
* [Java](docs/Java_API.md)
* [Ruby](https://github.com/ankane/onnxruntime) (external project)

### Official Builds
Expand All @@ -107,6 +108,7 @@ system.
* Version: **CUDA 10.0** and **cuDNN 7.6**
* Older ONNX Runtime releases: used **CUDA 9.1** and **cuDNN 7.1** - please refer to [prior release notes](https://github.com/microsoft/onnxruntime/releases) for more details.
* Python binaries are compatible with **Python 3.5-3.7**. See [Python Dev Notes](./docs/Python_Dev_Notes.md). If using `pip` to be download the Python binaries, run `pip install --upgrade pip` prior to downloading.
* The Java API is compatible with **Java 8-13**.
* Certain operators makes use of system locales. Installation of the **English language package** and configuring `en_US.UTF-8 locale` is required.
* For Ubuntu install [language-pack-en package](https://packages.ubuntu.com/search?keywords=language-pack-en)
* Run the following commands:
Expand Down
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()

110 changes: 110 additions & 0 deletions cmake/onnxruntime_java.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# 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" "-encoding" "UTF-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/)

if (WIN32)
add_custom_command(TARGET onnxruntime4j_jni POST_BUILD COMMAND ${CMAKE_COMMAND} -E copy "$<TARGET_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_FILE_NAME:onnxruntime>
DEPENDS onnxruntime4j
COMMENT "Rebuilding Java archive ${_JAVA_TARGET_OUTPUT_NAME}"
VERBATIM
)
else ()
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
)
endif()

create_javadoc(onnxruntime4j_javadoc
FILES ${onnxruntime4j_src}
DOCTITLE "Onnx Runtime Java API"
WINDOWTITLE "OnnxRuntime-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 ${CMAKE_CURRENT_BINARY_DIR}/java-tests/protobuf-java-3.10.0.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 docs/Java_API.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# ONNX Runtime Java API
The ONNX runtime provides a Java binding for running inference on ONNX models on a JVM, using Java 8 or newer.

Two jar files are created during the build process, one contains the onnxruntime shared library, the JNI binding and the Java class files, and the other only contains the class files. By default the shared libraries are loaded from the classpath in a folder called `/lib`, if you wish to have them load from `java.library.path` then supply `-DORT_LOAD_FROM_LIBRARY_PATH` to the JVM at runtime.

## Sample Code

The unit tests contain several examples of loading models, inspecting input/output node shapes and types, as well as constructing tensors for scoring.

* [../java/src/test/java/ai/onnxruntime/InferenceTest.java#L66](../java/src/test/java/ai/onnxruntime/InferenceTest.java#L66)

## Getting Started
Here is simple tutorial for getting started with running inference on an existing ONNX model for a given input data. The model is typically trained using any of the well-known training frameworks and exported into the ONNX format.
Note the code presented below uses syntax available from Java 10 onwards. The Java 8 syntax is similar but more verbose.
To start a scoring session, first create the `OrtEnvironment`, then open a session using the `OrtSession` class, passing in the file path to the model as a parameter.

var env = OrtEnvironment.getEnvironment();
var session = env.createSession("model.onnx",new OrtSession.SessionOptions());

Once a session is created, you can execute queries using the `run` method of the `OrtSession` object.
At the moment we support `OnnxTensor` inputs, and models can produce `OnnxTensor`, `OnnxSequence` or `OnnxMap` outputs. The latter two are more likely when scoring models produced by frameworks like scikit-learn.
The run call expects a `Map<String,OnnxTensor>` where the keys match input node names stored in the model. These can be viewed by calling `session.getInputNames()` or `session.getInputInfo()` on an instantiated session.
The run call produces a `Result` object, which contains a `Map<String,OnnxValue>` representing the output. The `Result` object is `AutoCloseable` and can be used in a try-with-resources statement to
prevent references from leaking out. Once the `Result` object is closed, all it's child `OnnxValue`s are closed too.

OnnxTensor t1,t2;
var inputs = Map.of("name1",t1,"name2",t2);
try (var results = session.run(inputs)) {
// manipulate the results
}

You can load your input data into OnnxTensor objects in several ways. The most efficient way is to use a `java.nio.Buffer`, but it's possible to use multidimensional arrays too. If constructed using arrays the arrays must not be ragged.

FloatBuffer sourceData; // assume your data is loaded into a FloatBuffer
long[] dimensions; // and the dimensions of the input are stored here
var tensorFromBuffer = OnnxTensor.createTensor(env,sourceData,dimensions);

float[][] sourceArray = new float[28][28]; // assume your data is loaded into a float array
var tensorFromArray = OnnxTensor.createTensor(env,sourceArray);

Here is a [complete sample program](../java/sample/ScoreMNIST.java) that runs inference on a pretrained MNIST model.

## Running on a GPU or with another provider (Optional)
To enable other execution providers like GPUs simply turn on the appropriate flag on SessionOptions when creating an OrtSession.

int gpuDeviceId = 0; // The GPU device ID to execute on
var sessionOptions = new OrtSession.SessionOptions();
sessionOptions.addCUDA(gpuDeviceId);
var session = environment.createSession("model.onnx", sessionOptions);

The execution providers are preferred in the order they were enabled.

## API Reference

The Javadoc is available [here](https://microsoft.github.io/onnxruntime/java/index.html).

Loading