Add java API for AddSessionConfigEntry (#5241)

* Add session option config entry API for java

* Java format

* Add extra test verification

* Address PR comments

* Update comments

Co-authored-by: gwang0000 <62914304+gwang0000@users.noreply.github.com>
This commit is contained in:
Guoyu Wang 2020-09-22 14:51:39 -07:00 committed by GitHub
parent 8dceebda0e
commit e30530d9ea
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 55 additions and 0 deletions

View file

@ -7,6 +7,7 @@ package ai.onnxruntime;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
@ -495,12 +496,15 @@ public class OrtSession implements AutoCloseable {
private final List<Long> customLibraryHandles;
private Map<String, String> configEntries;
private boolean closed = false;
/** Create an empty session options. */
public SessionOptions() {
nativeHandle = createOptions(OnnxRuntime.ortApiHandle);
customLibraryHandles = new ArrayList<>();
configEntries = new LinkedHashMap<String, String>();
}
/** Closes the session options, releasing any memory acquired. */
@ -675,6 +679,25 @@ public class OrtSession implements AutoCloseable {
customLibraryHandles.add(customHandle);
}
/**
* Adds a single session configuration entry as a pair of strings.
*
* @param configKey The config key string.
* @param configValue The config value string.
* @throws OrtException If there was an error in native code.
*/
public void addConfigEntry(String configKey, String configValue) throws OrtException {
checkClosed();
addConfigEntry(OnnxRuntime.ortApiHandle, nativeHandle, configKey, configValue);
configEntries.put(configKey, configValue);
}
/** Returns an unmodifiable view of the map contains all session configuration entries. */
public Map<String, String> getConfigEntries() {
checkClosed();
return Collections.unmodifiableMap(configEntries);
}
/**
* Add CUDA as an execution backend, using device 0.
*
@ -844,6 +867,10 @@ public class OrtSession implements AutoCloseable {
private native void closeOptions(long apiHandle, long nativeHandle);
private native void addConfigEntry(
long apiHandle, long nativeHandle, String configKey, String configValue)
throws OrtException;
/*
* To use additional providers, you must build ORT with the extra providers enabled. Then call one of these
* functions to enable them in the session:

View file

@ -295,6 +295,23 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_closeC
(*jniEnv)->ReleaseLongArrayElements(jniEnv,libraryHandles,handles,JNI_ABORT);
}
/*
* Class: ai_onnxruntime_OrtSession_SessionOptions
* Method: addConfigEntry
* Signature: (JJLjava/lang/String;Ljava/lang/String;)V
*/
JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_addConfigEntry
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong optionsHandle, jstring configKey, jstring configValue) {
(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;
const char* configKeyStr = (*jniEnv)->GetStringUTFChars(jniEnv, configKey, NULL);
const char* configValueStr = (*jniEnv)->GetStringUTFChars(jniEnv, configValue, NULL);
checkOrtStatus(jniEnv,api,api->AddSessionConfigEntry(options, configKeyStr, configValueStr));
(*jniEnv)->ReleaseStringUTFChars(jniEnv, configKey, configKeyStr);
(*jniEnv)->ReleaseStringUTFChars(jniEnv, configValue, configValueStr);
}
/*
* Class: ai_onnxruntime_OrtSession_SessionOptions
* Method: addCPU

View file

@ -852,6 +852,17 @@ public class InferenceTest {
options.setLoggerId("monkeys");
options.setSessionLogLevel(OrtLoggingLevel.ORT_LOGGING_LEVEL_FATAL);
options.setSessionLogVerbosityLevel(5);
Map<String, String> configEntries = options.getConfigEntries();
assertTrue(configEntries.isEmpty());
options.addConfigEntry("key", "value");
assertEquals("value", configEntries.get("key"));
try {
options.addConfigEntry("", "invalid key");
fail("Add config entry with empty key should have failed");
} catch (OrtException e) {
assertTrue(e.getMessage().contains("Config key is empty"));
assertEquals(OrtException.OrtErrorCode.ORT_INVALID_ARGUMENT, e.getCode());
}
try (OrtSession session = env.createSession(modelPath, options)) {
String inputName = session.getInputNames().iterator().next();
Map<String, OnnxTensor> container = new HashMap<>();