(1/2) Make TorchScript Preserve Fully Qualified Class Name for Python Exceptions: backend change

Summary: Reland for D33282878 (911d527b87) . Land backend change first to maintain FC. Will wait for 2 weeks after this diff is in. And than land the front-end change in next diff.

Test Plan:
test in next diff

time buck test mode/dev-nosan fblearner/flow/projects/langtech/translation:tests -- test_e2e_base_training

Reviewed By: gmagogsfm

Differential Revision: D33342547

fbshipit-source-id: b3dee9a4bdfd78103848c12629e5fccafdd621e3
(cherry picked from commit ae1935f1af755180e5607e870ff365dc17061e4a)
This commit is contained in:
Pavithran Ramachandran
2022-01-26 18:51:40 -08:00
committed by PyTorch MergeBot
parent 027c0d7f8e
commit bf69a61293
18 changed files with 527 additions and 163 deletions

View File

@ -15,6 +15,7 @@
#include <torch/csrc/jit/ir/constants.h>
#include <torch/csrc/jit/ir/ir.h>
#include <torch/csrc/jit/jit_log.h>
#include <torch/csrc/jit/mobile/promoted_prim_ops.h>
#include <torch/csrc/jit/runtime/exception_message.h>
#include <torch/csrc/jit/runtime/graph_executor.h>
#include <torch/csrc/jit/runtime/instruction.h>
@ -634,6 +635,81 @@ struct InterpreterStateImpl : c10::intrusive_ptr_target {
isinstance(stack, types);
}
INST_NEXT;
case INST(TUPLE_INDEX): {
INST_GUARD;
tupleIndex(stack);
}
INST_NEXT;
case INST(RAISE_EXCEPTION): {
INST_GUARD;
raiseExceptionWithMessage(stack);
}
INST_NEXT;
case INST(UNCHECKED_CAST): {
INST_GUARD;
noop(stack);
}
INST_NEXT;
case INST(__IS__): {
INST_GUARD;
is(stack);
}
INST_NEXT;
case INST(UN_INITIALIZED): {
INST_GUARD;
unInitialized(stack);
}
INST_NEXT;
case INST(__ISNOT__): {
INST_GUARD;
isNot(stack);
}
INST_NEXT;
case INST(FORMAT): {
INST_GUARD;
format(stack, inst.X);
}
INST_NEXT;
case INST(DEVICE): {
INST_GUARD;
device(stack);
}
INST_NEXT;
case INST(DTYPE): {
INST_GUARD;
dtype(stack);
}
INST_NEXT;
case INST(DIM): {
INST_GUARD;
dim(stack);
}
INST_NEXT;
case INST(__NOT__): {
INST_GUARD;
_not(stack);
}
INST_NEXT;
case INST(DICT_INDEX): {
INST_GUARD;
dictIndex(stack);
}
INST_NEXT;
case INST(TO_LIST): {
INST_GUARD;
toList(stack);
}
INST_NEXT;
case INST(NUM_TO_TENSOR): {
INST_GUARD;
numToTensorScalar(stack);
}
INST_NEXT;
case INST(IS_CUDA): {
INST_GUARD;
isCuda(stack);
}
INST_NEXT;
case INST(FORK): {
INST_GUARD;
// Move inputs to a separate stack
@ -714,10 +790,19 @@ struct InterpreterStateImpl : c10::intrusive_ptr_target {
}
throw;
}
bool is_jit_exception = dynamic_cast<JITException*>(&e);
auto* 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);
handleError(ExceptionMessage(e), is_jit_exception, not_implemented_error);
c10::optional<std::string> python_class_name;
if (jit_exception) {
python_class_name = jit_exception->getPythonClassName();
}
handleError(
ExceptionMessage(e),
(bool)jit_exception,
not_implemented_error,
python_class_name);
return false;
}
}
@ -736,15 +821,18 @@ struct InterpreterStateImpl : c10::intrusive_ptr_target {
void handleError(
const ExceptionMessage& msg,
bool is_jit_exception,
c10::NotImplementedError* not_implemented_error) {
c10::NotImplementedError* not_implemented_error,
c10::optional<std::string> python_class_name) {
std::ostringstream ss;
std::string class_name =
python_class_name ? *python_class_name : "RuntimeError";
ss << "The following operation failed in the TorchScript interpreter.\n";
formatStackTrace(ss);
ss << "RuntimeError: " << msg << "\n";
ss << class_name << ": " << msg << "\n";
if (future_) {
future_->setError(std::make_exception_ptr(Future::FutureError(ss.str())));
} else if (is_jit_exception) {
throw JITException(ss.str());
throw JITException(ss.str(), python_class_name);
} else if (not_implemented_error) {
throw c10::NotImplementedError(
ss.str(),