Skip to content

Commit 4852d1b

Browse files
authored
Merge pull request #7 from barrypitman/codex/update-onnxruntime-preset-for-openvino-y3mnbc
Add OpenVINO Execution Provider integration to ONNX Runtime Java preset and sample
2 parents ac6638f + 0500b0c commit 4852d1b

4 files changed

Lines changed: 138 additions & 13 deletions

File tree

onnxruntime/cppbuild.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ if [[ "$EXTENSION" == *gpu ]]; then
2929
fi
3030

3131
ONNXRUNTIME=1.26.0
32-
OPENVINO=2026.1.0
3332

3433
mkdir -p "$PLATFORM$EXTENSION"
3534
cd "$PLATFORM$EXTENSION"
@@ -75,8 +74,7 @@ esac
7574

7675
case $PLATFORM in
7776
linux-x86_64|macosx-arm64|windows-x86_64)
78-
"$PYTHON_BIN_PATH" -m pip install openvino==$OPENVINO
79-
OPENVINO_CMAKE_DIR=$("$PYTHON_BIN_PATH" -c "import pathlib, openvino; print(pathlib.Path(openvino.__file__).parent / 'cmake')")
77+
export OPENVINO_CMAKE_DIR="$TOP_PATH/openvino/cppbuild/$PLATFORM/runtime/cmake"
8078
export OPENVINO_FLAGS="--use_openvino CPU --cmake_extra_defines OpenVINO_DIR=$OPENVINO_CMAKE_DIR"
8179
;;
8280
esac

onnxruntime/pom.xml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
<properties>
1818
<onnxruntime.path>${basedir}/cppbuild/${javacpp.platform}${javacpp.platform.extension}/onnxruntime</onnxruntime.path>
19+
<openvino.link.path>${basedir}/../openvino/cppbuild/${javacpp.platform}/runtime/lib/intel64/</openvino.link.path>
1920
</properties>
2021

2122
<dependencies>
@@ -24,6 +25,11 @@
2425
<artifactId>dnnl</artifactId>
2526
<version>3.11-${project.parent.version}</version>
2627
</dependency>
28+
<dependency>
29+
<groupId>org.bytedeco</groupId>
30+
<artifactId>openvino</artifactId>
31+
<version>2026.1.0-${project.parent.version}</version>
32+
</dependency>
2733
<dependency>
2834
<groupId>org.bytedeco</groupId>
2935
<artifactId>javacpp</artifactId>
@@ -52,12 +58,18 @@
5258
<artifactId>dnnl-platform</artifactId>
5359
<version>3.11-${project.parent.version}</version>
5460
</dependency>
61+
<dependency>
62+
<groupId>org.bytedeco</groupId>
63+
<artifactId>openvino-platform</artifactId>
64+
<version>2026.1.0-${project.parent.version}</version>
65+
</dependency>
5566
</dependencies>
5667
<configuration>
5768
<encoding>UTF-8</encoding>
5869
<classPaths>
5970
<classPath>${basedir}/../opencl/target/classes/</classPath>
6071
<classPath>${basedir}/../dnnl/target/classes/</classPath>
72+
<classPath>${basedir}/../openvino/target/classes/</classPath>
6173
<classPath>${project.build.outputDirectory}</classPath>
6274
</classPaths>
6375
<includePaths>
@@ -67,6 +79,7 @@
6779
<includePath>${basedir}/../dnnl/target/classes/org/bytedeco/dnnl/include/</includePath>
6880
<includePath>${basedir}/../dnnl/src/main/resources/org/bytedeco/dnnl/include/</includePath>
6981
<includePath>${basedir}/../dnnl/cppbuild/${javacpp.platform}/include/</includePath>
82+
<includePath>${basedir}/../openvino/cppbuild/${javacpp.platform}/runtime/include/</includePath>
7083
<includePath>${basedir}/cppbuild/${javacpp.platform}${javacpp.platform.extension}/include/onnxruntime/core/session/</includePath>
7184
<includePath>${basedir}/cppbuild/${javacpp.platform}${javacpp.platform.extension}/include/onnxruntime/</includePath>
7285
<includePath>${basedir}/cppbuild/${javacpp.platform}${javacpp.platform.extension}/include/</includePath>
@@ -76,6 +89,7 @@
7689
<linkPaths>
7790
<linkPath>${basedir}/../opencl/cppbuild/${javacpp.platform}/lib/</linkPath>
7891
<linkPath>${basedir}/../dnnl/cppbuild/${javacpp.platform}/lib/</linkPath>
92+
<linkPath>${openvino.link.path}</linkPath>
7993
<linkPath>${basedir}/cppbuild/${javacpp.platform}${javacpp.platform.extension}/lib/</linkPath>
8094
</linkPaths>
8195
<preloadPaths>
@@ -87,13 +101,15 @@
87101
<buildResource>/${javacpp.platform.library.path}/</buildResource>
88102
<buildResource>/org/bytedeco/opencl/${javacpp.platform}/</buildResource>
89103
<buildResource>/org/bytedeco/dnnl/${javacpp.platform}/</buildResource>
104+
<buildResource>/org/bytedeco/openvino/${javacpp.platform}/</buildResource>
90105
</buildResources>
91106
<includeResources>
92107
<includeResource>/${javacpp.platform.library.path}/include/</includeResource>
93108
<includeResource>/org/bytedeco/opencl/include/</includeResource>
94109
<includeResource>/org/bytedeco/opencl/${javacpp.platform}/include/</includeResource>
95110
<includeResource>/org/bytedeco/dnnl/include/</includeResource>
96111
<includeResource>/org/bytedeco/dnnl/${javacpp.platform}/include/</includeResource>
112+
<includeResource>/org/bytedeco/openvino/${javacpp.platform}/runtime/include/</includeResource>
97113
</includeResources>
98114
<linkResources>
99115
<linkResource>/${javacpp.platform.library.path}/</linkResource>
@@ -161,4 +177,31 @@
161177
</plugins>
162178
</build>
163179

180+
<profiles>
181+
<profile>
182+
<id>macosx-arm64</id>
183+
<activation>
184+
<property>
185+
<name>javacpp.platform</name>
186+
<value>macosx-arm64</value>
187+
</property>
188+
</activation>
189+
<properties>
190+
<openvino.link.path>${basedir}/../openvino/cppbuild/${javacpp.platform}/runtime/lib/arm64/Release/</openvino.link.path>
191+
</properties>
192+
</profile>
193+
<profile>
194+
<id>windows-x86_64</id>
195+
<activation>
196+
<property>
197+
<name>javacpp.platform</name>
198+
<value>windows-x86_64</value>
199+
</property>
200+
</activation>
201+
<properties>
202+
<openvino.link.path>${basedir}/../openvino/cppbuild/${javacpp.platform}/runtime/lib/intel64/Release/</openvino.link.path>
203+
</properties>
204+
</profile>
205+
</profiles>
206+
164207
</project>
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright (C) 2026 JavaCPP Presets contributors
2+
// Licensed either under the Apache License, Version 2.0, or (at your option)
3+
// under the terms of the GNU General Public License as published by
4+
// the Free Software Foundation (subject to the "Classpath" exception),
5+
// either version 2, or any later version.
6+
7+
import java.util.Arrays;
8+
import java.util.Base64;
9+
10+
import org.bytedeco.javacpp.BytePointer;
11+
import org.bytedeco.javacpp.Loader;
12+
import org.bytedeco.javacpp.PointerPointer;
13+
import org.bytedeco.onnxruntime.Env;
14+
import org.bytedeco.onnxruntime.MemoryInfo;
15+
import org.bytedeco.onnxruntime.RunOptions;
16+
import org.bytedeco.onnxruntime.Session;
17+
import org.bytedeco.onnxruntime.SessionOptions;
18+
import org.bytedeco.onnxruntime.StringStringMap;
19+
import org.bytedeco.onnxruntime.Value;
20+
import org.bytedeco.onnxruntime.ValueVector;
21+
22+
import static org.bytedeco.onnxruntime.global.onnxruntime.*;
23+
24+
public class OpenVINOExecutionProviderSample {
25+
private static final String IDENTITY_MODEL_BASE64 =
26+
"CAgSFmphdmFjcHAtcHJlc2V0cy1zYW1wbGU6VgoZCgVpbnB1dBIGb3V0cHV0IghJZGVudGl0eRIOaWRlbnRpdHlfZ3JhcGhaEwoFaW5wdXQSCgoICAESBAoCCANiFAoGb3V0cHV0EgoKCAgBEgQKAggDQgIQDQ==";
27+
28+
public static void main(String[] args) throws Exception {
29+
String deviceType = args.length > 0 ? args[0] : "CPU";
30+
31+
// Load the OpenVINO preset first so that ONNX Runtime's OpenVINO provider can resolve
32+
// the OpenVINO and TBB native libraries bundled by the openvino-platform dependency.
33+
Loader.load(org.bytedeco.openvino.global.openvino.class);
34+
Loader.load(org.bytedeco.onnxruntime.global.onnxruntime.class);
35+
36+
System.out.println("Available ONNX Runtime providers: " + GetAvailableProviders());
37+
38+
Env env = new Env(ORT_LOGGING_LEVEL_WARNING, "openvino-ep-sample");
39+
SessionOptions sessionOptions = new SessionOptions();
40+
sessionOptions.SetIntraOpNumThreads(1);
41+
sessionOptions.SetGraphOptimizationLevel(ORT_ENABLE_EXTENDED);
42+
43+
StringStringMap openvinoOptions = new StringStringMap();
44+
openvinoOptions.put(new BytePointer("device_type"), deviceType);
45+
sessionOptions.AppendExecutionProvider_OpenVINO_V2(openvinoOptions);
46+
System.out.println("Appended OpenVINO execution provider with device_type=" + deviceType);
47+
48+
byte[] modelBytes = Base64.getDecoder().decode(IDENTITY_MODEL_BASE64);
49+
BytePointer modelData = new BytePointer(modelBytes);
50+
Session session = new Session(env, modelData, modelBytes.length, sessionOptions);
51+
52+
float[] input = new float[] {1.25f, -2.5f, 3.75f};
53+
long[] shape = new long[] {3};
54+
MemoryInfo memoryInfo = MemoryInfo.CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
55+
Value inputTensor = Value.CreateTensorFloat(memoryInfo.asOrtMemoryInfo(), input, input.length, shape, shape.length);
56+
57+
BytePointer inputName = new BytePointer("input");
58+
BytePointer outputName = new BytePointer("output");
59+
PointerPointer inputNames = new PointerPointer(1).put(0, inputName);
60+
PointerPointer outputNames = new PointerPointer(1).put(0, outputName);
61+
ValueVector outputs = session.Run(new RunOptions(), inputNames, inputTensor, 1, outputNames, 1);
62+
float[] output = new float[input.length];
63+
outputs.get(0).GetTensorMutableDataFloat().get(output);
64+
65+
System.out.println("Input: " + Arrays.toString(input));
66+
System.out.println("Output: " + Arrays.toString(output));
67+
if (!Arrays.equals(input, output)) {
68+
throw new AssertionError("The OpenVINO EP identity model output did not match the input");
69+
}
70+
System.out.println("OpenVINO EP identity inference succeeded.");
71+
}
72+
}

onnxruntime/samples/pom.xml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,33 @@
1515
<version>1.26.0-1.5.14-SNAPSHOT</version>
1616
</dependency>
1717

18-
<!-- Additional dependencies required to use CUDA and cuDNN -->
1918
<dependency>
2019
<groupId>org.bytedeco</groupId>
21-
<artifactId>onnxruntime-platform-gpu</artifactId>
22-
<version>1.26.0-1.5.14-SNAPSHOT</version>
23-
</dependency>
24-
25-
<!-- Additional dependencies to use bundled CUDA and cuDNN -->
26-
<dependency>
27-
<groupId>org.bytedeco</groupId>
28-
<artifactId>cuda-platform-redist-cudnn</artifactId>
29-
<version>13.2-9.21-1.5.14-SNAPSHOT</version>
20+
<artifactId>openvino-platform</artifactId>
21+
<version>2026.1.0-1.5.14-SNAPSHOT</version>
3022
</dependency>
3123

3224
</dependencies>
25+
<profiles>
26+
<profile>
27+
<id>cuda</id>
28+
<dependencies>
29+
<!-- Additional dependencies required to use CUDA and cuDNN -->
30+
<dependency>
31+
<groupId>org.bytedeco</groupId>
32+
<artifactId>onnxruntime-platform-gpu</artifactId>
33+
<version>1.26.0-1.5.14-SNAPSHOT</version>
34+
</dependency>
35+
36+
<!-- Additional dependencies to use bundled CUDA and cuDNN -->
37+
<dependency>
38+
<groupId>org.bytedeco</groupId>
39+
<artifactId>cuda-platform-redist-cudnn</artifactId>
40+
<version>13.2-9.21-1.5.14-SNAPSHOT</version>
41+
</dependency>
42+
</dependencies>
43+
</profile>
44+
</profiles>
3345
<build>
3446
<sourceDirectory>.</sourceDirectory>
3547
</build>

0 commit comments

Comments
 (0)