Skip to content

Commit

Permalink
Adds a Java accessor for GetVersionString (#14876)
Browse files Browse the repository at this point in the history
### Description
Java part of #14873.
  • Loading branch information
Craigacp authored Mar 7, 2023
1 parent 5930e7e commit 150043f
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
26 changes: 25 additions & 1 deletion java/src/main/java/ai/onnxruntime/OnnxRuntime.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Licensed under the MIT License.
*/
package ai.onnxruntime;
Expand Down Expand Up @@ -84,6 +84,9 @@ final class OnnxRuntime {
/** The available runtime providers */
static EnumSet<OrtProvider> providers;

/** The version string. */
private static String version;

private OnnxRuntime() {}

/* Computes and initializes OS_ARCH_STR (such as linux-x64) */
Expand Down Expand Up @@ -124,6 +127,7 @@ private static String initOsArch() {
* Loads the native C library.
*
* @throws IOException If it can't write to disk to copy out the library from the jar file.
* @throws IllegalStateException If the native library failed to load.
*/
static synchronized void init() throws IOException {
if (loaded) {
Expand All @@ -139,7 +143,11 @@ static synchronized void init() throws IOException {
load(ONNXRUNTIME_LIBRARY_NAME);
load(ONNXRUNTIME_JNI_LIBRARY_NAME);
ortApiHandle = initialiseAPIBase(ORT_API_VERSION_11);
if (ortApiHandle == 0L) {
throw new IllegalStateException("Failed to load native library");
}
providers = initialiseProviders(ortApiHandle);
version = initialiseVersion();
loaded = true;
} finally {
if (tempDirectory != null) {
Expand All @@ -161,6 +169,15 @@ private static void cleanUp(File file) {
file.deleteOnExit();
}

/**
* Gets the native library version string.
*
* @return The version string.
*/
static String version() {
return version;
}

/**
* Extracts the CUDA provider library from the classpath resources if present, or checks to see if
* the CUDA provider library is in the directory specified by {@link #ONNXRUNTIME_NATIVE_PATH}.
Expand Down Expand Up @@ -433,4 +450,11 @@ private static EnumSet<OrtProvider> initialiseProviders(long ortApiHandle) {
* @return The array of providers
*/
private static native String[] getAvailableProviders(long ortApiHandle);

/**
* Gets the version string from the native library.
*
* @return The version string.
*/
private static native String initialiseVersion();
}
19 changes: 17 additions & 2 deletions java/src/main/java/ai/onnxruntime/OrtEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
* Licensed under the MIT License.
*/
package ai.onnxruntime;
Expand Down Expand Up @@ -271,9 +271,24 @@ public void setTelemetry(boolean sendTelemetry) throws OrtException {
setTelemetry(OnnxRuntime.ortApiHandle, nativeHandle, sendTelemetry);
}

/**
* Gets the native library version string.
*
* @return The version string.
*/
public String getVersion() {
return OnnxRuntime.version();
}

@Override
public String toString() {
return "OrtEnvironment(name=" + curLoggingName + ",logLevel=" + curLogLevel + ")";
return "OrtEnvironment(name="
+ curLoggingName
+ ",logLevel="
+ curLogLevel
+ ",version="
+ getVersion()
+ ")";
}

/**
Expand Down
17 changes: 16 additions & 1 deletion java/src/main/native/ai_onnxruntime_OnnxRuntime.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/*
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023 Oracle and/or its affiliates. All rights reserved.
* Licensed under the MIT License.
*/
#include <jni.h>
#include <assert.h>
#include "onnxruntime/core/session/onnxruntime_c_api.h"
#include "ai_onnxruntime_OnnxRuntime.h"
#include "OrtJniUtil.h"
Expand Down Expand Up @@ -54,3 +55,17 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxRuntime_getAvailableProvi
}
return providerArray;
}

/*
* Class: ai_onnxruntime_OnnxRuntime
* Method: initialiseVersion
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_ai_onnxruntime_OnnxRuntime_initialiseVersion
(JNIEnv * jniEnv, jclass clazz) {
(void)clazz; // required JNI parameter not needed by functions which don't access their host class.
const char* version = OrtGetApiBase()->GetVersionString();
assert(version != NULL);
jstring versionStr = (*jniEnv)->NewStringUTF(jniEnv, version);
return versionStr;
}
8 changes: 7 additions & 1 deletion java/src/test/java/ai/onnxruntime/InferenceTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, Oracle and/or its affiliates. All rights reserved.
* Licensed under the MIT License.
*/
package ai.onnxruntime;
Expand Down Expand Up @@ -72,6 +72,12 @@ public void environmentTest() {
assertSame(env, otherEnv);
}

@Test
public void testVersion() {
String version = env.getVersion();
Assertions.assertFalse(version.isEmpty());
}

@Test
public void createSessionFromPath() throws OrtException {
String modelPath = TestHelpers.getResourcePath("/squeezenet.onnx").toString();
Expand Down

0 comments on commit 150043f

Please sign in to comment.