mirror of
https://github.com/saymrwulf/onnxruntime.git
synced 2026-07-19 19:00:47 +00:00
[java] First part of the JNI error handling rewrite (#12013)
**Description**: This fixes error handling in the JNI code in OnnxMap, OnnxSequence, OnnxRuntime, RunOptions. SessionOptions and OrtEnvironment are correct as is. The bulk of the work will be in rewriting OnnxTensor, OnnxSparseTensor (after the merge of #10653) and OrtSession, along with the helper methods in OrtJniUtil. I plan to tackle those in separate PRs to reduce the amount of code to review. **Motivation and Context** - Why is this change required? What problem does it solve? The current native interop code doesn't return control to Java immediately on throwing an exception from an ORT error code, which can cause incorrect interactions with native ORT, and issues with exception propagation on the Java side. - If it fixes an open issue, please link to the issue here. Partial work towards solving #11451.
This commit is contained in:
parent
a6fd1a3b85
commit
e0ed9f0f2f
7 changed files with 419 additions and 293 deletions
|
|
@ -82,11 +82,11 @@ jint convertFromONNXDataFormat(ONNXTensorElementDataType type) {
|
|||
return 3;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16: // maps to c type int16_t
|
||||
return 4;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32: // maps to c type uint32_t
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32: // maps to c type uint32_t
|
||||
return 5;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32: // maps to c type int32_t
|
||||
return 6;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64: // maps to c type uint64_t
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64: // maps to c type uint64_t
|
||||
return 7;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64: // maps to c type int64_t
|
||||
return 8;
|
||||
|
|
@ -94,7 +94,7 @@ jint convertFromONNXDataFormat(ONNXTensorElementDataType type) {
|
|||
return 9;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT: // maps to c type float
|
||||
return 10;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE: // maps to c type double
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE: // maps to c type double
|
||||
return 11;
|
||||
case ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING: // maps to c++ type std::string
|
||||
return 12;
|
||||
|
|
@ -1029,19 +1029,23 @@ jint convertErrorCode(OrtErrorCode code) {
|
|||
}
|
||||
}
|
||||
|
||||
void checkOrtStatus(JNIEnv *jniEnv, const OrtApi * api, OrtStatus * status) {
|
||||
if (status == NULL) return;
|
||||
OrtErrorCode checkOrtStatus(JNIEnv *jniEnv, const OrtApi * api, OrtStatus * status) {
|
||||
if (status == NULL) {
|
||||
return ORT_OK;
|
||||
}
|
||||
const char* message = api->GetErrorMessage(status);
|
||||
OrtErrorCode errCode = api->GetErrorCode(status);
|
||||
size_t len = strlen(message)+1;
|
||||
char* copy = malloc(sizeof(char)*len);
|
||||
if (copy == NULL) {
|
||||
throwOrtException(jniEnv, 1, "Not enough memory");
|
||||
return;
|
||||
return ORT_FAIL;
|
||||
}
|
||||
memcpy(copy,message,len);
|
||||
int messageId = convertErrorCode(api->GetErrorCode(status));
|
||||
int messageId = convertErrorCode(errCode);
|
||||
api->ReleaseStatus(status);
|
||||
throwOrtException(jniEnv,messageId,copy);
|
||||
return errCode;
|
||||
}
|
||||
|
||||
jsize safecast_size_t_to_jsize(size_t v) {
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ jint throwOrtException(JNIEnv *env, int messageId, const char *message);
|
|||
|
||||
jint convertErrorCode(OrtErrorCode code);
|
||||
|
||||
void checkOrtStatus(JNIEnv * env, const OrtApi * api, OrtStatus * status);
|
||||
OrtErrorCode checkOrtStatus(JNIEnv * env, const OrtApi * api, OrtStatus * status);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
#include <jni.h>
|
||||
|
|
@ -12,21 +12,24 @@
|
|||
* Method: getStringKeys
|
||||
* Signature: (J)[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxMap_getStringKeys
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract key
|
||||
OrtValue* keys;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,0,allocator,&keys));
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxMap_getStringKeys(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
// Extract key
|
||||
OrtValue* keys;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, 0, allocator, &keys));
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java String array
|
||||
jobjectArray output = createStringArrayFromTensor(jniEnv, api, allocator, keys);
|
||||
|
||||
api->ReleaseValue(keys);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -34,20 +37,23 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxMap_getStringKeys
|
|||
* Method: getLongKeys
|
||||
* Signature: (JJ)[J
|
||||
*/
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxMap_getLongKeys
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract key
|
||||
OrtValue* keys;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,0,allocator,&keys));
|
||||
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxMap_getLongKeys(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
// Extract key
|
||||
OrtValue* keys;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, 0, allocator, &keys));
|
||||
if (code == ORT_OK) {
|
||||
jlongArray output = createLongArrayFromTensor(jniEnv, api, keys);
|
||||
|
||||
api->ReleaseValue(keys);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -55,21 +61,25 @@ JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxMap_getLongKeys
|
|||
* Method: getStringValues
|
||||
* Signature: (JJ)[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxMap_getStringValues
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,1,allocator,&values));
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxMap_getStringValues(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, 1, allocator, &values));
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java String array
|
||||
jobjectArray output = createStringArrayFromTensor(jniEnv, api, allocator, values);
|
||||
|
||||
api->ReleaseValue(values);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -77,20 +87,23 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxMap_getStringValues
|
|||
* Method: getLongValues
|
||||
* Signature: (JJ)[J
|
||||
*/
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxMap_getLongValues
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,1,allocator,&values));
|
||||
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxMap_getLongValues(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, 1, allocator, &values));
|
||||
if (code == ORT_OK) {
|
||||
jlongArray output = createLongArrayFromTensor(jniEnv, api, values);
|
||||
|
||||
api->ReleaseValue(values);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -98,20 +111,23 @@ JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxMap_getLongValues
|
|||
* Method: getFloatValues
|
||||
* Signature: (JJ)[F
|
||||
*/
|
||||
JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxMap_getFloatValues
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,1,allocator,&values));
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxMap_getFloatValues(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, 1, allocator, &values));
|
||||
if (code == ORT_OK) {
|
||||
jfloatArray output = createFloatArrayFromTensor(jniEnv, api, values);
|
||||
|
||||
api->ReleaseValue(values);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -119,20 +135,24 @@ JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxMap_getFloatValues
|
|||
* Method: getDoubleValues
|
||||
* Signature: (JJ)[D
|
||||
*/
|
||||
JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxMap_getDoubleValues
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,1,allocator,&values));
|
||||
|
||||
JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxMap_getDoubleValues(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
// Extract value
|
||||
OrtValue* values;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, 1, allocator, &values));
|
||||
if (code == ORT_OK) {
|
||||
jdoubleArray output = createDoubleArrayFromTensor(jniEnv, api, values);
|
||||
|
||||
api->ReleaseValue(values);
|
||||
|
||||
return output;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -140,9 +160,8 @@ JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxMap_getDoubleValues
|
|||
* Method: close
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_ai_onnxruntime_OnnxMap_close
|
||||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle) {
|
||||
(void) jniEnv; (void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
api->ReleaseValue((OrtValue*)handle);
|
||||
JNIEXPORT void JNICALL Java_ai_onnxruntime_OnnxMap_close(JNIEnv* jniEnv, jobject jobj, jlong apiHandle, jlong handle) {
|
||||
(void)jniEnv; (void)jobj; // Required JNI parameters not used by functions which don't access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
api->ReleaseValue((OrtValue*)handle);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 2020 Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2022 Oracle and/or its affiliates. All rights reserved.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
#include <jni.h>
|
||||
|
|
@ -12,11 +12,11 @@
|
|||
* Method: initialiseAPIBase
|
||||
* Signature: (I)J
|
||||
*/
|
||||
JNIEXPORT jlong JNICALL Java_ai_onnxruntime_OnnxRuntime_initialiseAPIBase
|
||||
(JNIEnv * jniEnv, jclass clazz, jint apiVersion) {
|
||||
(void) jniEnv; (void) clazz; // required JNI parameters not needed by functions which don't call back into Java.
|
||||
const OrtApi* ortPtr = OrtGetApiBase()->GetApi((uint32_t) apiVersion);
|
||||
return (jlong) ortPtr;
|
||||
JNIEXPORT jlong JNICALL Java_ai_onnxruntime_OnnxRuntime_initialiseAPIBase(JNIEnv* jniEnv, jclass clazz,
|
||||
jint apiVersion) {
|
||||
(void)jniEnv; (void)clazz; // required JNI parameters not needed by functions which don't call back into Java.
|
||||
const OrtApi* ortPtr = OrtGetApiBase()->GetApi((uint32_t)apiVersion);
|
||||
return (jlong)ortPtr;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -24,31 +24,33 @@ JNIEXPORT jlong JNICALL Java_ai_onnxruntime_OnnxRuntime_initialiseAPIBase
|
|||
* Method: getAvailableProviders
|
||||
* Signature: (J)[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxRuntime_getAvailableProviders
|
||||
(JNIEnv * jniEnv, jclass clazz, jlong apiHandle) {
|
||||
(void) jniEnv; (void) clazz; // required JNI parameters not needed by functions which don't call back into Java.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxRuntime_getAvailableProviders(JNIEnv* jniEnv, jclass clazz,
|
||||
jlong apiHandle) {
|
||||
(void)jniEnv; (void)clazz; // required JNI parameters not needed by functions which don't call back into Java.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
|
||||
char** providers = NULL;
|
||||
int numProviders = 0;
|
||||
|
||||
// Extract the provider array
|
||||
checkOrtStatus(jniEnv,api,api->GetAvailableProviders(&providers,&numProviders));
|
||||
jobjectArray providerArray = NULL;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetAvailableProviders(&providers, &numProviders));
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java String Array
|
||||
char* stringClassName = "java/lang/String";
|
||||
jclass stringClazz = (*jniEnv)->FindClass(jniEnv, stringClassName);
|
||||
providerArray = (*jniEnv)->NewObjectArray(jniEnv, numProviders, stringClazz, NULL);
|
||||
|
||||
// Convert to Java String Array
|
||||
char *stringClassName = "java/lang/String";
|
||||
jclass stringClazz = (*jniEnv)->FindClass(jniEnv, stringClassName);
|
||||
jobjectArray providerArray = (*jniEnv)->NewObjectArray(jniEnv,numProviders,stringClazz,NULL);
|
||||
for (int i = 0; i < numProviders; i++) {
|
||||
// Read out the provider name and convert it to a java.lang.String
|
||||
jstring provider = (*jniEnv)->NewStringUTF(jniEnv, providers[i]);
|
||||
(*jniEnv)->SetObjectArrayElement(jniEnv, providerArray, i, provider);
|
||||
}
|
||||
|
||||
for (int i = 0; i < numProviders; i++) {
|
||||
// Read out the provider name and convert it to a java.lang.String
|
||||
jstring provider = (*jniEnv)->NewStringUTF(jniEnv,providers[i]);
|
||||
(*jniEnv)->SetObjectArrayElement(jniEnv, providerArray, i, provider);
|
||||
// Release providers
|
||||
// if this fails we return immediately anyway
|
||||
checkOrtStatus(jniEnv, api, api->ReleaseAvailableProviders(providers, numProviders));
|
||||
providers = NULL;
|
||||
}
|
||||
|
||||
// Release providers
|
||||
checkOrtStatus(jniEnv,api,api->ReleaseAvailableProviders(providers,numProviders));
|
||||
providers = NULL;
|
||||
|
||||
return providerArray;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
#include <jni.h>
|
||||
|
|
@ -11,26 +11,32 @@
|
|||
* Method: getStringKeys
|
||||
* Signature: (JJI)[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStringKeys
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle, jint index) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,index,allocator,&element));
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStringKeys(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle, jint index) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jobjectArray output = NULL;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, index, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract keys from element
|
||||
OrtValue* keys;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(element,0,allocator,&keys));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(element, 0, allocator, &keys));
|
||||
|
||||
// Convert to Java String array
|
||||
jobjectArray output = createStringArrayFromTensor(jniEnv, api, allocator, keys);
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java String array
|
||||
output = createStringArrayFromTensor(jniEnv, api, allocator, keys);
|
||||
// Release if valid
|
||||
api->ReleaseValue(element);
|
||||
}
|
||||
|
||||
// Keys is valid, so release
|
||||
api->ReleaseValue(keys);
|
||||
api->ReleaseValue(element);
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -38,25 +44,32 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStringKeys
|
|||
* Method: getLongKeys
|
||||
* Signature: (JJI)[J
|
||||
*/
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongKeys
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle, jint index) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,index,allocator,&element));
|
||||
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongKeys(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle,
|
||||
jint index) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jlongArray output = NULL;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, index, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract keys from element
|
||||
OrtValue* keys;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(element,0,allocator,&keys));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(element, 0, allocator, &keys));
|
||||
|
||||
jlongArray output = createLongArrayFromTensor(jniEnv, api, keys);
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java long array
|
||||
output = createLongArrayFromTensor(jniEnv, api, keys);
|
||||
// Release if valid
|
||||
api->ReleaseValue(element);
|
||||
}
|
||||
|
||||
// Keys is valid, so release
|
||||
api->ReleaseValue(keys);
|
||||
api->ReleaseValue(element);
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -64,26 +77,32 @@ JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongKeys
|
|||
* Method: getStringValues
|
||||
* Signature: (JJI)[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStringValues
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle, jint index) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,index,allocator,&element));
|
||||
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStringValues(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle, jint index) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jobjectArray output = NULL;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, index, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract values from element
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(element,1,allocator,&values));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(element, 1, allocator, &values));
|
||||
|
||||
// Convert to Java String array
|
||||
jobjectArray output = createStringArrayFromTensor(jniEnv, api, allocator, values);
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java String array
|
||||
output = createStringArrayFromTensor(jniEnv, api, allocator, values);
|
||||
// Release if valid
|
||||
api->ReleaseValue(element);
|
||||
}
|
||||
|
||||
// values is valid, so release
|
||||
api->ReleaseValue(values);
|
||||
api->ReleaseValue(element);
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -91,25 +110,32 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStringValues
|
|||
* Method: getLongValues
|
||||
* Signature: (JJI)[J
|
||||
*/
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongValues
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle, jint index) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,index,allocator,&element));
|
||||
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongValues(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle, jint index) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jlongArray output = NULL;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, index, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract values from element
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(element,1,allocator,&values));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(element, 1, allocator, &values));
|
||||
|
||||
jlongArray output = createLongArrayFromTensor(jniEnv, api, values);
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java long array
|
||||
output = createLongArrayFromTensor(jniEnv, api, values);
|
||||
// Release if valid
|
||||
api->ReleaseValue(element);
|
||||
}
|
||||
|
||||
// values is valid, so release
|
||||
api->ReleaseValue(values);
|
||||
api->ReleaseValue(element);
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -117,25 +143,32 @@ JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongValues
|
|||
* Method: getFloatValues
|
||||
* Signature: (JJI)[F
|
||||
*/
|
||||
JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxSequence_getFloatValues
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle, jint index) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,index,allocator,&element));
|
||||
|
||||
JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxSequence_getFloatValues(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle, jint index) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jfloatArray output = NULL;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, index, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract values from element
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(element,1,allocator,&values));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(element, 1, allocator, &values));
|
||||
|
||||
jfloatArray output = createFloatArrayFromTensor(jniEnv, api,values);
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java float array
|
||||
output = createFloatArrayFromTensor(jniEnv, api, values);
|
||||
// Release if valid
|
||||
api->ReleaseValue(element);
|
||||
}
|
||||
|
||||
// values is valid, so release
|
||||
api->ReleaseValue(values);
|
||||
api->ReleaseValue(element);
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -143,25 +176,32 @@ JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxSequence_getFloatValues
|
|||
* Method: getDoubleValues
|
||||
* Signature: (JJI)[D
|
||||
*/
|
||||
JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxSequence_getDoubleValues
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle, jint index) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue((OrtValue*)handle,index,allocator,&element));
|
||||
|
||||
JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxSequence_getDoubleValues(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle, jint index) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jdoubleArray output = NULL;
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValue((OrtValue*)handle, index, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract values from element
|
||||
OrtValue* values;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(element,1,allocator,&values));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(element, 1, allocator, &values));
|
||||
|
||||
jdoubleArray output = createDoubleArrayFromTensor(jniEnv, api,values);
|
||||
if (code == ORT_OK) {
|
||||
// Convert to Java double array
|
||||
output = createDoubleArrayFromTensor(jniEnv, api, values);
|
||||
// Release if valid
|
||||
api->ReleaseValue(element);
|
||||
}
|
||||
|
||||
// values is valid, so release
|
||||
api->ReleaseValue(values);
|
||||
api->ReleaseValue(element);
|
||||
|
||||
return output;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -169,30 +209,43 @@ JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxSequence_getDoubleValues
|
|||
* Method: getStrings
|
||||
* Signature: (JJ)[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStrings
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtValue* sequence = (OrtValue*) handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStrings(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtValue* sequence = (OrtValue*)handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
checkOrtStatus(jniEnv,api,api->GetValueCount(sequence,&count));
|
||||
jobjectArray outputArray = NULL;
|
||||
|
||||
jclass stringClazz = (*jniEnv)->FindClass(jniEnv,"java/lang/String");
|
||||
jobjectArray outputArray = (*jniEnv)->NewObjectArray(jniEnv,safecast_size_t_to_jsize(count),stringClazz, NULL);
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValueCount(sequence, &count));
|
||||
if (code == ORT_OK) {
|
||||
jclass stringClazz = (*jniEnv)->FindClass(jniEnv, "java/lang/String");
|
||||
outputArray = (*jniEnv)->NewObjectArray(jniEnv, safecast_size_t_to_jsize(count), stringClazz, NULL);
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(sequence,(int)i,allocator,&element));
|
||||
|
||||
createStringFromStringTensor(jniEnv,api,allocator,element);
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(sequence, (int)i, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
jobject str = createStringFromStringTensor(jniEnv, api, allocator, element);
|
||||
if (str == NULL) {
|
||||
api->ReleaseValue(element);
|
||||
// bail out as exception has been thrown
|
||||
return NULL;
|
||||
}
|
||||
(*jniEnv)->SetObjectArrayElement(jniEnv, outputArray, (jsize)i, str);
|
||||
|
||||
api->ReleaseValue(element);
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return outputArray;
|
||||
}
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -200,39 +253,53 @@ JNIEXPORT jobjectArray JNICALL Java_ai_onnxruntime_OnnxSequence_getStrings
|
|||
* Method: getLongs
|
||||
* Signature: (JJ)[J
|
||||
*/
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongs
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtValue* sequence = (OrtValue*) handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
checkOrtStatus(jniEnv,api,api->GetValueCount(sequence,&count));
|
||||
JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongs(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtValue* sequence = (OrtValue*)handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jlongArray outputArray = NULL;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValueCount(sequence, &count));
|
||||
if (code == ORT_OK) {
|
||||
int64_t* values;
|
||||
checkOrtStatus(jniEnv,api,api->AllocatorAlloc(allocator,sizeof(int64_t)*count,(void**)&values));
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
code = checkOrtStatus(jniEnv, api, api->AllocatorAlloc(allocator, sizeof(int64_t) * count, (void**)&values));
|
||||
if (code == ORT_OK) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(sequence,(int)i,allocator,&element));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(sequence, (int)i, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract the values
|
||||
int64_t* arr;
|
||||
code = checkOrtStatus(jniEnv, api, api->GetTensorMutableData(element, (void**)&arr));
|
||||
if (code == ORT_OK) {
|
||||
values[i] = arr[0];
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
api->ReleaseValue(element);
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Extract the values
|
||||
int64_t* arr;
|
||||
checkOrtStatus(jniEnv,api,api->GetTensorMutableData(element,(void**)&arr));
|
||||
values[i] = arr[0];
|
||||
api->ReleaseValue(element);
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
api->ReleaseValue(element);
|
||||
outputArray = (*jniEnv)->NewLongArray(jniEnv, safecast_size_t_to_jsize(count));
|
||||
(*jniEnv)->SetLongArrayRegion(jniEnv, outputArray, 0, safecast_size_t_to_jsize(count), (jlong*)values);
|
||||
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
}
|
||||
|
||||
jlongArray outputArray = (*jniEnv)->NewLongArray(jniEnv,safecast_size_t_to_jsize(count));
|
||||
(*jniEnv)->SetLongArrayRegion(jniEnv, outputArray,0,safecast_size_t_to_jsize(count),(jlong*)values);
|
||||
|
||||
checkOrtStatus(jniEnv,api,api->AllocatorFree(allocator,values));
|
||||
|
||||
return outputArray;
|
||||
}
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -240,39 +307,53 @@ JNIEXPORT jlongArray JNICALL Java_ai_onnxruntime_OnnxSequence_getLongs
|
|||
* Method: getFloats
|
||||
* Signature: (JJ)[F
|
||||
*/
|
||||
JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxSequence_getFloats
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtValue* sequence = (OrtValue*) handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
checkOrtStatus(jniEnv,api,api->GetValueCount(sequence,&count));
|
||||
JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxSequence_getFloats(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle, jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtValue* sequence = (OrtValue*)handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jfloatArray outputArray = NULL;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValueCount(sequence, &count));
|
||||
if (code == ORT_OK) {
|
||||
float* values;
|
||||
checkOrtStatus(jniEnv,api,api->AllocatorAlloc(allocator,sizeof(float)*count,(void**)&values));
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
code = checkOrtStatus(jniEnv, api, api->AllocatorAlloc(allocator, sizeof(float) * count, (void**)&values));
|
||||
if (code == ORT_OK) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(sequence,(int)i,allocator,&element));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(sequence, (int)i, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract the values
|
||||
float* arr;
|
||||
code = checkOrtStatus(jniEnv, api, api->GetTensorMutableData(element, (void**)&arr));
|
||||
if (code == ORT_OK) {
|
||||
values[i] = arr[0];
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
api->ReleaseValue(element);
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Extract the values
|
||||
float* arr;
|
||||
checkOrtStatus(jniEnv,api,api->GetTensorMutableData(element,(void**)&arr));
|
||||
values[i] = arr[0];
|
||||
api->ReleaseValue(element);
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
api->ReleaseValue(element);
|
||||
outputArray = (*jniEnv)->NewFloatArray(jniEnv, safecast_size_t_to_jsize(count));
|
||||
(*jniEnv)->SetFloatArrayRegion(jniEnv, outputArray, 0, safecast_size_t_to_jsize(count), values);
|
||||
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
}
|
||||
|
||||
jfloatArray outputArray = (*jniEnv)->NewFloatArray(jniEnv,safecast_size_t_to_jsize(count));
|
||||
(*jniEnv)->SetFloatArrayRegion(jniEnv,outputArray,0,safecast_size_t_to_jsize(count),values);
|
||||
|
||||
checkOrtStatus(jniEnv,api,api->AllocatorFree(allocator,values));
|
||||
|
||||
return outputArray;
|
||||
}
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -280,39 +361,54 @@ JNIEXPORT jfloatArray JNICALL Java_ai_onnxruntime_OnnxSequence_getFloats
|
|||
* Method: getDoubles
|
||||
* Signature: (JJ)[D
|
||||
*/
|
||||
JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxSequence_getDoubles
|
||||
(JNIEnv *jniEnv, jobject jobj, jlong apiHandle, jlong handle, jlong allocatorHandle) {
|
||||
(void) jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtValue* sequence = (OrtValue*) handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*) allocatorHandle;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
checkOrtStatus(jniEnv,api,api->GetValueCount(sequence,&count));
|
||||
JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxSequence_getDoubles(JNIEnv* jniEnv, jobject jobj,
|
||||
jlong apiHandle, jlong handle,
|
||||
jlong allocatorHandle) {
|
||||
(void)jobj; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
OrtValue* sequence = (OrtValue*)handle;
|
||||
OrtAllocator* allocator = (OrtAllocator*)allocatorHandle;
|
||||
jdoubleArray outputArray = NULL;
|
||||
|
||||
// Get the element count of this sequence
|
||||
size_t count;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->GetValueCount(sequence, &count));
|
||||
if (code == ORT_OK) {
|
||||
double* values;
|
||||
checkOrtStatus(jniEnv,api,api->AllocatorAlloc(allocator,sizeof(double)*count,(void**)&values));
|
||||
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
code = checkOrtStatus(jniEnv, api, api->AllocatorAlloc(allocator, sizeof(double) * count, (void**)&values));
|
||||
if (code == ORT_OK) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
// Extract element
|
||||
OrtValue* element;
|
||||
checkOrtStatus(jniEnv,api,api->GetValue(sequence,(int)i,allocator,&element));
|
||||
code = checkOrtStatus(jniEnv, api, api->GetValue(sequence, (int)i, allocator, &element));
|
||||
if (code == ORT_OK) {
|
||||
// Extract the values
|
||||
double* arr;
|
||||
code = checkOrtStatus(jniEnv, api, api->GetTensorMutableData(element, (void**)&arr));
|
||||
if (code == ORT_OK) {
|
||||
values[i] = arr[0];
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
api->ReleaseValue(element);
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Extract the values
|
||||
double* arr;
|
||||
checkOrtStatus(jniEnv,api,api->GetTensorMutableData(element,(void**)&arr));
|
||||
values[i] = arr[0];
|
||||
api->ReleaseValue(element);
|
||||
} else {
|
||||
// bail out as exception has been thrown
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
api->ReleaseValue(element);
|
||||
outputArray = (*jniEnv)->NewDoubleArray(jniEnv, safecast_size_t_to_jsize(count));
|
||||
(*jniEnv)->SetDoubleArrayRegion(jniEnv, outputArray, 0, safecast_size_t_to_jsize(count), values);
|
||||
|
||||
checkOrtStatus(jniEnv, api, api->AllocatorFree(allocator, values));
|
||||
}
|
||||
|
||||
jdoubleArray outputArray = (*jniEnv)->NewDoubleArray(jniEnv,safecast_size_t_to_jsize(count));
|
||||
(*jniEnv)->SetDoubleArrayRegion(jniEnv,outputArray,0,safecast_size_t_to_jsize(count),values);
|
||||
|
||||
checkOrtStatus(jniEnv,api,api->AllocatorFree(allocator,values));
|
||||
|
||||
return outputArray;
|
||||
}
|
||||
return outputArray;
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -320,8 +416,10 @@ JNIEXPORT jdoubleArray JNICALL Java_ai_onnxruntime_OnnxSequence_getDoubles
|
|||
* Method: close
|
||||
* Signature: (J)V
|
||||
*/
|
||||
JNIEXPORT void JNICALL Java_ai_onnxruntime_OnnxSequence_close(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong handle) {
|
||||
(void) jniEnv; (void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
api->ReleaseValue((OrtValue*)handle);
|
||||
JNIEXPORT void JNICALL Java_ai_onnxruntime_OnnxSequence_close(JNIEnv* jniEnv, jobject jobj, jlong apiHandle,
|
||||
jlong handle) {
|
||||
(void)jniEnv;
|
||||
(void)jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*)apiHandle;
|
||||
api->ReleaseValue((OrtValue*)handle);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2020 Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2020, 2022, Oracle and/or its affiliates. All rights reserved.
|
||||
* Licensed under the MIT License.
|
||||
*/
|
||||
#include <jni.h>
|
||||
|
|
@ -18,7 +18,7 @@ JNIEXPORT jlong JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_createRun
|
|||
(void) jclazz; // Required JNI parameter not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtRunOptions* opts;
|
||||
checkOrtStatus(jniEnv,api,api->CreateRunOptions(&opts));
|
||||
checkOrtStatus(jniEnv, api, api->CreateRunOptions(&opts));
|
||||
return (jlong) opts;
|
||||
}
|
||||
|
||||
|
|
@ -31,7 +31,7 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_setLogLeve
|
|||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong nativeHandle, jint logLevel) {
|
||||
(void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsSetRunLogSeverityLevel((OrtRunOptions*) nativeHandle,logLevel));
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsSetRunLogSeverityLevel((OrtRunOptions*) nativeHandle, logLevel));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -44,7 +44,7 @@ JNIEXPORT jint JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_getLogLeve
|
|||
(void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
int logLevel;
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsGetRunLogSeverityLevel((OrtRunOptions*) nativeHandle,&logLevel));
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsGetRunLogSeverityLevel((OrtRunOptions*) nativeHandle, &logLevel));
|
||||
return (jint)logLevel;
|
||||
}
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_setLogVerb
|
|||
(JNIEnv * jniEnv, jobject jobj, jlong apiHandle, jlong nativeHandle, jint logLevel) {
|
||||
(void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsSetRunLogVerbosityLevel((OrtRunOptions*) nativeHandle,logLevel));
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsSetRunLogVerbosityLevel((OrtRunOptions*) nativeHandle, logLevel));
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -70,7 +70,7 @@ JNIEXPORT jint JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_getLogVerb
|
|||
(void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
int logLevel;
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsGetRunLogVerbosityLevel((OrtRunOptions*) nativeHandle,&logLevel));
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsGetRunLogVerbosityLevel((OrtRunOptions*) nativeHandle, &logLevel));
|
||||
return (jint)logLevel;
|
||||
}
|
||||
|
||||
|
|
@ -84,8 +84,8 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_setRunTag
|
|||
(void) jobj; // Required JNI parameters not needed by functions which don't need to access their host object.
|
||||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
const char* runTagStr = (*jniEnv)->GetStringUTFChars(jniEnv, runTag, NULL);
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsSetRunTag((OrtRunOptions*) nativeHandle, runTagStr));
|
||||
(*jniEnv)->ReleaseStringUTFChars(jniEnv,runTag,runTagStr);
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsSetRunTag((OrtRunOptions*) nativeHandle, runTagStr));
|
||||
(*jniEnv)->ReleaseStringUTFChars(jniEnv, runTag, runTagStr);
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -99,9 +99,13 @@ JNIEXPORT jstring JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_getRunT
|
|||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
const char* runTagStr;
|
||||
// This is a reference to the C str, and should not be freed.
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsGetRunTag((OrtRunOptions*)nativeHandle,&runTagStr));
|
||||
jstring runTag = (*jniEnv)->NewStringUTF(jniEnv,runTagStr);
|
||||
return runTag;
|
||||
OrtErrorCode code = checkOrtStatus(jniEnv, api, api->RunOptionsGetRunTag((OrtRunOptions*) nativeHandle, &runTagStr));
|
||||
if (code == ORT_OK) {
|
||||
jstring runTag = (*jniEnv)->NewStringUTF(jniEnv, runTagStr);
|
||||
return runTag;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
|
@ -115,9 +119,9 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024RunOptions_setTermina
|
|||
const OrtApi* api = (const OrtApi*) apiHandle;
|
||||
OrtRunOptions* runOptions = (OrtRunOptions*) nativeHandle;
|
||||
if (terminate) {
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsSetTerminate(runOptions));
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsSetTerminate(runOptions));
|
||||
} else {
|
||||
checkOrtStatus(jniEnv,api,api->RunOptionsUnsetTerminate(runOptions));
|
||||
checkOrtStatus(jniEnv, api, api->RunOptionsUnsetTerminate(runOptions));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -615,4 +615,3 @@ JNIEXPORT void JNICALL Java_ai_onnxruntime_OrtSession_00024SessionOptions_addROC
|
|||
throwOrtException(jniEnv,convertErrorCode(ORT_INVALID_ARGUMENT),"This binary was not compiled with ROCM support.");
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue