|
85 | 85 | #include <torch/csrc/utils/tensor_qschemes.h>
|
86 | 86 | #include <torch/csrc/utils/verbose.h>
|
87 | 87 |
|
| 88 | +#include <c10/util/Logging.h> |
| 89 | +#include <torch/csrc/profiler/combined_traceback.h> |
| 90 | +#include <sstream> |
| 91 | + |
88 | 92 | #ifdef USE_DISTRIBUTED
|
89 | 93 | #ifdef USE_C10D
|
90 | 94 | #include <torch/csrc/distributed/autograd/python_autograd.h>
|
@@ -141,6 +145,34 @@ static PyObject* THPModule_initExtension(
|
141 | 145 | PyObject* _unused,
|
142 | 146 | PyObject* shm_manager_path) {
|
143 | 147 | HANDLE_TH_ERRORS
|
| 148 | +#if !defined(FBCODE_CAFFE2) |
| 149 | + if (torch::get_cpp_stacktraces_enabled() && !torch::get_disable_addr2line()) { |
| 150 | + c10::SetStackTraceFetcher([]() -> std::string { |
| 151 | + auto tb = torch::CapturedTraceback::gather(false, false, true); |
| 152 | + LOG(WARNING) |
| 153 | + << "symbolizing C++ stack trace for exception; if this hangs, rerun with TORCH_DISABLE_ADDR2LINE=1..." |
| 154 | + << std::endl; |
| 155 | + auto s_tbs = torch::symbolize({tb.get()}); |
| 156 | + std::stringstream oss; |
| 157 | + oss << "C++ CapturedTraceback:" << std::endl; |
| 158 | + const auto& s_tb = s_tbs.tracebacks.at(0); |
| 159 | + for (auto idx : c10::irange(s_tb.size())) { |
| 160 | + // Skip the first few frames: |
| 161 | + // #1 torch::CapturedTraceback::gather(bool, bool, bool) |
| 162 | + // #2 THPModule_initExtension |
| 163 | + // #3 THPModule_initExtension(_object*, _object*)::{lambda()#1} |
| 164 | + if (idx <= 3) { |
| 165 | + continue; |
| 166 | + } |
| 167 | + auto frame_id = s_tb[idx]; |
| 168 | + const auto& frame = s_tbs.all_frames.at(frame_id); |
| 169 | + oss << "#" << idx << " " << frame.funcname << " from " << frame.filename |
| 170 | + << ":" << frame.lineno << std::endl; |
| 171 | + } |
| 172 | + return oss.str(); |
| 173 | + }); |
| 174 | + } |
| 175 | +#endif |
144 | 176 | if (!THPUtils_checkString(shm_manager_path)) {
|
145 | 177 | THPUtils_setError(
|
146 | 178 | "initialization error - expected bytes/string object as shm_manager_path!");
|
|
0 commit comments