Ensure JNI references are released when resizing inputs
Fixes #27202. PiperOrigin-RevId: 242223062
This commit is contained in:
parent
088e9ef4b6
commit
009fde6645
@ -101,7 +101,7 @@ void printDims(char* buffer, int max_size, int* dims, int num_dims) {
|
|||||||
|
|
||||||
// Checks whether there is any difference between dimensions of a tensor and a
|
// Checks whether there is any difference between dimensions of a tensor and a
|
||||||
// given dimensions. Returns true if there is difference, else false.
|
// given dimensions. Returns true if there is difference, else false.
|
||||||
bool areDimsDifferent(JNIEnv* env, TfLiteTensor* tensor, jintArray dims) {
|
bool AreDimsDifferent(JNIEnv* env, TfLiteTensor* tensor, jintArray dims) {
|
||||||
int num_dims = static_cast<int>(env->GetArrayLength(dims));
|
int num_dims = static_cast<int>(env->GetArrayLength(dims));
|
||||||
jint* ptr = env->GetIntArrayElements(dims, nullptr);
|
jint* ptr = env->GetIntArrayElements(dims, nullptr);
|
||||||
if (ptr == nullptr) {
|
if (ptr == nullptr) {
|
||||||
@ -109,16 +109,19 @@ bool areDimsDifferent(JNIEnv* env, TfLiteTensor* tensor, jintArray dims) {
|
|||||||
"Empty dimensions of input array.");
|
"Empty dimensions of input array.");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
bool is_different = false;
|
||||||
if (tensor->dims->size != num_dims) {
|
if (tensor->dims->size != num_dims) {
|
||||||
return true;
|
is_different = true;
|
||||||
}
|
} else {
|
||||||
for (int i = 0; i < num_dims; ++i) {
|
for (int i = 0; i < num_dims; ++i) {
|
||||||
if (ptr[i] != tensor->dims->data[i]) {
|
if (ptr[i] != tensor->dims->data[i]) {
|
||||||
return true;
|
is_different = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
env->ReleaseIntArrayElements(dims, ptr, JNI_ABORT);
|
env->ReleaseIntArrayElements(dims, ptr, JNI_ABORT);
|
||||||
return false;
|
return is_different;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO(yichengfan): evaluate the benefit to use tflite verifier.
|
// TODO(yichengfan): evaluate the benefit to use tflite verifier.
|
||||||
@ -446,7 +449,7 @@ Java_org_tensorflow_lite_NativeInterpreterWrapper_resizeInput(
|
|||||||
}
|
}
|
||||||
// check whether it is resizing with the same dimensions.
|
// check whether it is resizing with the same dimensions.
|
||||||
TfLiteTensor* target = interpreter->tensor(input_idx);
|
TfLiteTensor* target = interpreter->tensor(input_idx);
|
||||||
bool is_changed = areDimsDifferent(env, target, dims);
|
bool is_changed = AreDimsDifferent(env, target, dims);
|
||||||
if (is_changed) {
|
if (is_changed) {
|
||||||
TfLiteStatus status = interpreter->ResizeInputTensor(
|
TfLiteStatus status = interpreter->ResizeInputTensor(
|
||||||
interpreter->inputs()[idx], convertJIntArrayToVector(env, dims));
|
interpreter->inputs()[idx], convertJIntArrayToVector(env, dims));
|
||||||
|
Loading…
Reference in New Issue
Block a user