[Java] Exposing SessionOptions.SetDeterministicCompute (#18998)

### Description
Exposes `SetDeterministicCompute` in Java, added to the C API by #18944.

### Motivation and Context
Parity between C and Java APIs.
This commit is contained in:
Adam Pocock 2024-09-15 21:55:38 -04:00 committed by GitHub
parent 02e00dc023
commit 6d7235ba5a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 31 additions and 0 deletions

View file

@ -942,6 +942,20 @@ public class OrtSession implements AutoCloseable {
OnnxRuntime.ortApiHandle, nativeHandle, dimensionName, dimensionValue);
}
/**
* Set whether to use deterministic compute.
*
* <p>Default is false. If set to true, this will enable deterministic compute for GPU kernels
* where possible. Note that this most likely will have a performance cost.
*
* @param value Should the compute be deterministic?
* @throws OrtException If there was an error in native code.
*/
public void setDeterministicCompute(boolean value) throws OrtException {
checkClosed();
setDeterministicCompute(OnnxRuntime.ortApiHandle, nativeHandle, value);
}
/**
* Disables the per session thread pools. Must be used in conjunction with an environment
* containing global thread pools.
@ -1327,6 +1341,9 @@ public class OrtSession implements AutoCloseable {
private native void closeOptions(long apiHandle, long nativeHandle);
private native void setDeterministicCompute(
long apiHandle, long nativeHandle, boolean isDeterministic) throws OrtException;
private native void addFreeDimensionOverrideByName(
long apiHandle, long nativeHandle, String dimensionName, long dimensionValue)
throws OrtException;

View file

@ -259,6 +259,19 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_setSes
checkOrtStatus(jniEnv,api,api->SetSessionLogVerbosityLevel(options,logLevel));
}
/*
* Class: ai_onnxruntime_OrtSession_SessionOptions
* Method: setDeterministicCompute
* Signature: (JJZ)V
*/
JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_setDeterministicCompute
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong optionsHandle, jboolean isDeterministic) {
(void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
const OrtApi* api = (const OrtApi*)apiHandle;
OrtSessionOptions* options = (OrtSessionOptions*) optionsHandle;
checkOrtStatus(jniEnv,api,api->SetDeterministicCompute(options, isDeterministic));
}
/*
* Class: ai_onnxruntime_OrtSession_SessionOptions
* Method: registerCustomOpLibrary

View file

@ -1263,6 +1263,7 @@ public class InferenceTest {
options.setLoggerId("monkeys");
options.setSessionLogLevel(OrtLoggingLevel.ORT_LOGGING_LEVEL_FATAL);
options.setSessionLogVerbosityLevel(5);
options.setDeterministicCompute(true);
Map<String, String> configEntries = options.getConfigEntries();
assertTrue(configEntries.isEmpty());
options.addConfigEntry("key", "value");