Skip to content

Commit f9f29e7

Browse files
authored
Android: improve error diagnostics for LlmModule and exceptions (pytorch#19092)
Add cause-chaining constructor to ExecutorchRuntimeException so wrapped exceptions preserve the original cause in the stack trace. Restore detailed native error messages in LlmModule.load() — the null runner case now reports the model_type_category and valid values instead of a generic message. Load failures now throw from JNI with the specific error code and description. This commit was authored with the help of Claude.
1 parent 7b5dcc1 commit f9f29e7

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

extension/android/executorch_android/src/main/java/org/pytorch/executorch/ExecutorchRuntimeException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ public ExecutorchRuntimeException(int errorCode, String details) {
161161
this.errorCode = errorCode;
162162
}
163163

164+
public ExecutorchRuntimeException(int errorCode, String details, Throwable cause) {
165+
super(ErrorHelper.formatMessage(errorCode, details), cause);
166+
this.errorCode = errorCode;
167+
}
168+
164169
/** Returns the numeric error code from {@code runtime/core/error.h}. */
165170
public int getErrorCode() {
166171
return errorCode;

extension/android/jni/jni_layer_llama.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <cstdint>
1111
#include <cstring>
1212
#include <memory>
13+
#include <sstream>
1314
#include <string>
1415
#include <unordered_map>
1516
#include <vector>
@@ -594,21 +595,19 @@ class ExecuTorchLlmJni : public facebook::jni::HybridClass<ExecuTorchLlmJni> {
594595

595596
jint load() {
596597
if (!runner_) {
597-
ET_LOG(
598-
Error,
599-
"ExecuTorchLlmJni::load() called but runner_ is null. "
600-
"The model runner was not created or failed to initialize due to a "
601-
"previous configuration or initialization error. "
602-
"Model type category: %d.",
603-
model_type_category_);
598+
std::stringstream ss;
599+
ss << "Model runner was not created. model_type_category="
600+
<< model_type_category_
601+
<< ". Valid values: " << MODEL_TYPE_CATEGORY_LLM << " (LLM), "
602+
<< MODEL_TYPE_CATEGORY_MULTIMODAL << " (Multimodal)";
603+
executorch::jni_helper::throwExecutorchException(
604+
static_cast<uint32_t>(Error::InvalidState), ss.str().c_str());
604605
return static_cast<jint>(Error::InvalidState);
605606
}
606607
const auto load_result = static_cast<jint>(runner_->load());
607608
if (load_result != static_cast<jint>(Error::Ok)) {
608-
ET_LOG(
609-
Error,
610-
"ExecuTorchLlmJni::load() failed in runner_->load() with error code %d.",
611-
static_cast<int>(load_result));
609+
executorch::jni_helper::throwExecutorchException(
610+
static_cast<uint32_t>(load_result), "Failed to load model runner");
612611
}
613612
return load_result;
614613
}

0 commit comments

Comments
 (0)