Skip to content

Commit cf08087

Browse files
authored
Fix JNI reading mTypeCode instead of mData for scalar EValue inputs (pytorch#17603)
- The `execute_method` paths in `jni_layer.cpp` and `jni_layer_training.cpp` extracted Int/Double/Bool EValue data by re-reading the `mTypeCode` field instead of the actual `mData`. This meant every scalar input was silently replaced by its type code constant (e.g. Int always became 4, Double became 3.0, Bool was always true). - Call the Java accessor methods (`toInt`, `toDouble`, `toBool`) instead, matching the pattern already used for Tensor inputs via `toTensor()`.
1 parent b863992 commit cf08087

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

‎extension/android/jni/jni_layer.cpp‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -359,14 +359,17 @@ class ExecuTorchJni : public facebook::jni::HybridClass<ExecuTorchJni> {
359359
tensors.emplace_back(JEValue::JEValueToTensorImpl(jevalue));
360360
evalues.emplace_back(tensors.back());
361361
} else if (typeCode == JEValue::kTypeCodeInt) {
362-
int64_t value = jevalue->getFieldValue(typeCodeField);
363-
evalues.emplace_back(value);
362+
static const auto toIntMethod =
363+
JEValue::javaClassStatic()->getMethod<jlong()>("toInt");
364+
evalues.emplace_back(static_cast<int64_t>(toIntMethod(jevalue)));
364365
} else if (typeCode == JEValue::kTypeCodeDouble) {
365-
double value = jevalue->getFieldValue(typeCodeField);
366-
evalues.emplace_back(value);
366+
static const auto toDoubleMethod =
367+
JEValue::javaClassStatic()->getMethod<jdouble()>("toDouble");
368+
evalues.emplace_back(static_cast<double>(toDoubleMethod(jevalue)));
367369
} else if (typeCode == JEValue::kTypeCodeBool) {
368-
bool value = jevalue->getFieldValue(typeCodeField);
369-
evalues.emplace_back(value);
370+
static const auto toBoolMethod =
371+
JEValue::javaClassStatic()->getMethod<jboolean()>("toBool");
372+
evalues.emplace_back(static_cast<bool>(toBoolMethod(jevalue)));
370373
}
371374
}
372375

‎extension/android/jni/jni_layer_training.cpp‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,17 @@ class ExecuTorchTrainingJni
131131
tensors.emplace_back(JEValue::JEValueToTensorImpl(jevalue));
132132
evalues.emplace_back(tensors.back());
133133
} else if (typeCode == JEValue::kTypeCodeInt) {
134-
int64_t value = jevalue->getFieldValue(typeCodeField);
135-
evalues.emplace_back(value);
134+
static const auto toIntMethod =
135+
JEValue::javaClassStatic()->getMethod<jlong()>("toInt");
136+
evalues.emplace_back(static_cast<int64_t>(toIntMethod(jevalue)));
136137
} else if (typeCode == JEValue::kTypeCodeDouble) {
137-
double value = jevalue->getFieldValue(typeCodeField);
138-
evalues.emplace_back(value);
138+
static const auto toDoubleMethod =
139+
JEValue::javaClassStatic()->getMethod<jdouble()>("toDouble");
140+
evalues.emplace_back(static_cast<double>(toDoubleMethod(jevalue)));
139141
} else if (typeCode == JEValue::kTypeCodeBool) {
140-
bool value = jevalue->getFieldValue(typeCodeField);
141-
evalues.emplace_back(value);
142+
static const auto toBoolMethod =
143+
JEValue::javaClassStatic()->getMethod<jboolean()>("toBool");
144+
evalues.emplace_back(static_cast<bool>(toBoolMethod(jevalue)));
142145
}
143146
}
144147

0 commit comments

Comments
 (0)