|
| 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 | +} |
0 commit comments