Skip to content

Commit

Permalink
HANDLE_TH_ERRORS: Move exception translation out of line (pytorch#69974)
Browse files Browse the repository at this point in the history
Summary:
I've noticed that the `HANDLE_TH_ERRORS` macros are actually very expensive in terms of compile time.  Moving the bulk of the catch statements out of line using a lippincott function significantly improves compile times and object file binary sizes. For just the generated autograd bindings, this halves serial build time from 8 minutes to 4 and binary size is more than halved for most files with the biggest difference being `python_variable_methods.cpp` which went from 126 MB to 43 MB.

Pull Request resolved: pytorch#69974

Reviewed By: mruberry

Differential Revision: D33160899

Pulled By: albanD

fbshipit-source-id: fc35fa86f69ffe5a0752557be30b438c8564e998
  • Loading branch information
peterbell10 authored and facebook-github-bot committed Dec 16, 2021
1 parent 9ff8c49 commit 96fe82a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
9 changes: 9 additions & 0 deletions torch/csrc/Exceptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ static std::string formatMessage(const char *format, va_list fmt_args) {
return std::string(error_buf);
}

void translate_exception_to_python(const std::exception_ptr &e_ptr) {
try {
TORCH_INTERNAL_ASSERT(e_ptr, "translate_exception_to_python "
"called with invalid exception pointer");
std::rethrow_exception(e_ptr);
}
CATCH_ALL_ERRORS(return)
}

IndexError::IndexError(const char *format, ...) {
va_list fmt_args;
va_start(fmt_args, format);
Expand Down
13 changes: 11 additions & 2 deletions torch/csrc/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ static inline void PyErr_SetString(PyObject* type, const std::string& message) {
catch (torch::jit::JITException & e) { \
throw; \
} \
CATCH_ALL_ERRORS(throw py::error_already_set())
catch (const std::exception & e) { \
torch::translate_exception_to_python(std::current_exception()); \
throw py::error_already_set(); \
}

#define END_HANDLE_TH_ERRORS_RET(retval) \
} \
Expand All @@ -141,7 +144,10 @@ static inline void PyErr_SetString(PyObject* type, const std::string& message) {
throw; \
} \
} \
CATCH_ALL_ERRORS(return retval)
catch (const std::exception & e) { \
torch::translate_exception_to_python(std::current_exception()); \
return retval; \
}

#define END_HANDLE_TH_ERRORS END_HANDLE_TH_ERRORS_RET(nullptr)

Expand Down Expand Up @@ -258,6 +264,9 @@ bool THPException_init(PyObject *module);

namespace torch {

// Set python current exception from a C++ exception
TORCH_PYTHON_API void translate_exception_to_python(const std::exception_ptr &);

TORCH_PYTHON_API std::string processErrorMsg(std::string str);

TORCH_PYTHON_API bool get_cpp_stacktraces_enabled();
Expand Down

0 comments on commit 96fe82a

Please sign in to comment.