Skip to content

Commit

Permalink
[Java] Exposing SessionOptions.SetDeterministicCompute (#18998)
Browse files Browse the repository at this point in the history
### Description
Exposes `SetDeterministicCompute` in Java, added to the C API by #18944.

### Motivation and Context
Parity between C and Java APIs.
  • Loading branch information
Craigacp authored Sep 16, 2024
1 parent 02e00dc commit 6d7235b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
17 changes: 17 additions & 0 deletions java/src/main/java/ai/onnxruntime/OrtSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -942,6 +942,20 @@ public void setSymbolicDimensionValue(String dimensionName, long dimensionValue)
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.
Expand Down Expand Up @@ -1327,6 +1341,9 @@ private native void registerCustomOpsUsingFunction(

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;
Expand Down
13 changes: 13 additions & 0 deletions java/src/main/native/ai_onnxruntime_OrtSession_SessionOptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions java/src/test/java/ai/onnxruntime/InferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1263,6 +1263,7 @@ public void testExtraSessionOptions() throws OrtException, IOException {
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");
Expand Down

0 comments on commit 6d7235b

Please sign in to comment.