[nnc] Make our exceptions c10::Errors, get C++ stacktraces (#64332)

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

With this diff, if a compiler bug occurs (unlikely, I know!) we'll be able to get a c++ stacktrace leading to the exception, rather than just a terse message.  E.g.,
```
RuntimeError: UNSUPPORTED DTYPE
Exception raised from compilation_error at ../torch/csrc/jit/tensorexpr/exceptions.h:32 (most recent call first):
frame #0: c10::Error::Error(c10::SourceLocation, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >) + 0x6b (0x7f966659b2eb in /fsx/users/bertrand/c\
onda/envs/pytorch/lib/python3.8/site-packages/torch/lib/libc10.so)
frame #1: <unknown function> + 0x376f099 (0x7f966a195099 in /fsx/users/bertrand/conda/envs/pytorch/lib/python3.8/site-packages/torch/lib/libtorch_cuda.so)
frame #2: <unknown function> + 0x3763bf5 (0x7f966a189bf5 in /fsx/users/bertrand/conda/envs/pytorch/lib/python3.8/site-packages/torch/lib/libtorch_cuda.so)
frame #3: torch::jit::tensorexpr::CudaCodeGen::Initialize() + 0xdd8 (0x7f966a193368 in /fsx/users/bertrand/conda/envs/pytorch/lib/python3.8/site-packages/torch/lib/libtorch_cuda\
.so)
```

Test Plan: Imported from OSS

Reviewed By: huiguoo

Differential Revision: D30745610

Pulled By: bertmaher

fbshipit-source-id: a1cfaa7364ef4120de834e9cbe57ced1d082ab4e
This commit is contained in:
Bert Maher
2021-09-04 20:29:44 -07:00
committed by Facebook GitHub Bot
parent 6cac7ca980
commit 18b2751ea1
2 changed files with 41 additions and 27 deletions

View File

@ -26,66 +26,78 @@ namespace torch {
namespace jit {
namespace tensorexpr {
class unsupported_dtype : public std::runtime_error {
TORCH_API std::string buildErrorMessage(const std::string& s);
class compilation_error : public c10::Error {
public:
explicit unsupported_dtype() : std::runtime_error("UNSUPPORTED DTYPE") {}
explicit compilation_error(const std::string& err)
: c10::Error(
{
__func__,
__FILE__,
static_cast<uint32_t>(__LINE__),
},
buildErrorMessage(err)) {}
};
class unsupported_dtype : public compilation_error {
public:
explicit unsupported_dtype() : compilation_error("UNSUPPORTED DTYPE") {}
explicit unsupported_dtype(const std::string& err)
: std::runtime_error("UNSUPPORTED DTYPE: " + err) {}
: compilation_error("UNSUPPORTED DTYPE: " + err) {}
};
class out_of_range_index : public std::runtime_error {
class out_of_range_index : public compilation_error {
public:
explicit out_of_range_index() : std::runtime_error("OUT OF RANGE INDEX") {}
explicit out_of_range_index() : compilation_error("OUT OF RANGE INDEX") {}
explicit out_of_range_index(const std::string& err)
: std::runtime_error("OUT OF RANGE INDEX: " + err) {}
: compilation_error("OUT OF RANGE INDEX: " + err) {}
};
class unimplemented_lowering : public std::runtime_error {
class unimplemented_lowering : public compilation_error {
public:
explicit unimplemented_lowering()
: std::runtime_error("UNIMPLEMENTED LOWERING") {}
: compilation_error("UNIMPLEMENTED LOWERING") {}
explicit unimplemented_lowering(ExprPtr expr)
: std::runtime_error("UNIMPLEMENTED LOWERING: " + std::to_string(expr)) {}
: compilation_error("UNIMPLEMENTED LOWERING: " + std::to_string(expr)) {}
explicit unimplemented_lowering(StmtPtr stmt)
: std::runtime_error("UNIMPLEMENTED LOWERING: " + std::to_string(stmt)) {}
: compilation_error("UNIMPLEMENTED LOWERING: " + std::to_string(stmt)) {}
};
class malformed_input : public std::runtime_error {
class malformed_input : public compilation_error {
public:
explicit malformed_input() : std::runtime_error("MALFORMED INPUT") {}
explicit malformed_input() : compilation_error("MALFORMED INPUT") {}
explicit malformed_input(const std::string& err)
: std::runtime_error("MALFORMED INPUT: " + err) {}
: compilation_error("MALFORMED INPUT: " + err) {}
explicit malformed_input(ExprPtr expr)
: std::runtime_error("MALFORMED INPUT: " + std::to_string(expr)) {}
: compilation_error("MALFORMED INPUT: " + std::to_string(expr)) {}
explicit malformed_input(const std::string& err, ExprPtr expr)
: std::runtime_error(
: compilation_error(
"MALFORMED INPUT: " + err + " - " + std::to_string(expr)) {}
explicit malformed_input(StmtPtr stmt)
: std::runtime_error("MALFORMED INPUT: " + std::to_string(stmt)) {}
: compilation_error("MALFORMED INPUT: " + std::to_string(stmt)) {}
explicit malformed_input(const std::string& err, StmtPtr stmt)
: std::runtime_error(
: compilation_error(
"MALFORMED INPUT: " + err + " - " + std::to_string(stmt)) {}
};
class malformed_ir : public std::runtime_error {
class malformed_ir : public compilation_error {
public:
explicit malformed_ir() : std::runtime_error("MALFORMED IR") {}
explicit malformed_ir() : compilation_error("MALFORMED IR") {}
explicit malformed_ir(const std::string& err)
: std::runtime_error("MALFORMED IR: " + err) {}
: compilation_error("MALFORMED IR: " + err) {}
explicit malformed_ir(ExprPtr expr)
: std::runtime_error("MALFORMED IR: " + std::to_string(expr)) {}
: compilation_error("MALFORMED IR: " + std::to_string(expr)) {}
explicit malformed_ir(const std::string& err, ExprPtr expr)
: std::runtime_error(
: compilation_error(
"MALFORMED IR: " + err + " - " + std::to_string(expr)) {}
explicit malformed_ir(StmtPtr stmt)
: std::runtime_error("MALFORMED IR: " + std::to_string(stmt)) {}
: compilation_error("MALFORMED IR: " + std::to_string(stmt)) {}
explicit malformed_ir(const std::string& err, StmtPtr stmt)
: std::runtime_error(
: compilation_error(
"MALFORMED IR: " + err + " - " + std::to_string(stmt)) {}
};
TORCH_API std::string buildErrorMessage(const std::string& s);
} // namespace tensorexpr
} // namespace jit
} // namespace torch

View File

@ -476,11 +476,13 @@ bool LoopNest::vectorize(ForPtr f) {
normalize(to<For>(new_f));
new_f = FlattenIndexes(new_f);
new_f = v.vectorize(to<For>(new_f));
} catch (std::runtime_error& e) {
} catch (compilation_error& e) {
// We clone f before vectorizing. So, any partial vectorization will
// have modified the clone. In case of an exception, we can continue
// using f.
new_f = f;
} catch (std::runtime_error& e) {
new_f = f;
}
if (new_f != f) {