[JIT] Add a flag to rethrow caught exception in jit interpreter (#63073)

Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/63073

It turned out that it's less than ideal to print out verbose stacktrace in exception messages in high-QPS services (see the related task) with a non-significant failure rate due to the truncation of long stacktrace which results in losing the original exception message thrown from native code. It is actually desirable to retain only the message of the original exception directly thrown from native code in such a usecase.

This change adds a new flag `torch_jit_disable_exception_stacktrace` to the pytorch jit interpreter to suppress stacktrace in the messages of exception thrown from the interpreter.

Reviewed By: Krovatkin

Differential Revision: D30241792

fbshipit-source-id: c340225c69286663cbd857bd31ba6f1736b1ac4c
This commit is contained in:
Don Jang
2021-08-13 08:37:23 -07:00
committed by Facebook GitHub Bot
parent 32b6104f37
commit 61b49c8e41
3 changed files with 72 additions and 5 deletions

View File

@ -43,6 +43,11 @@ using torch::distributed::autograd::DistAutogradContainer;
#include <utility>
#include <vector>
C10_DEFINE_bool(
torch_jit_enable_rethrow_caught_exception,
false,
"enable rethrowing caught exception");
namespace torch {
namespace jit {
@ -708,13 +713,16 @@ struct InterpreterStateImpl : c10::intrusive_ptr_target {
push(stack, IValue());
try {
f.run(stack);
} catch (std::exception& e) {
std::ostringstream ss;
ss << "The following operation failed in the TorchScript interpreter.\n";
formatStackTrace(ss);
ss << "RuntimeError: " << ExceptionMessage(e) << "\n";
} catch (std::exception& _) {
// TODO(T98048876): Handle `_` correctly.
}
}
if (FLAGS_torch_jit_enable_rethrow_caught_exception) {
if (future_) {
future_->setError(std::make_exception_ptr(e));
}
throw;
}
bool is_jit_exception = dynamic_cast<JITException*>(&e);
// Janky af. See https://github.com/pytorch/pytorch/issues/54612
auto* not_implemented_error = dynamic_cast<c10::NotImplementedError*>(&e);