From d47304e6d9f1c071ab85fe13c337210d99e386f7 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Thu, 30 Jul 2020 15:44:00 +0800 Subject: [PATCH] Refine paddle error stack format (#25790) * refine error stack format * polish compile traceback format * polish detail format --- paddle/fluid/framework/op_call_stack.cc | 18 +++--------------- paddle/fluid/framework/op_desc.cc | 2 +- paddle/fluid/platform/enforce.h | 8 ++++---- python/paddle/fluid/framework.py | 9 +++++++-- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/paddle/fluid/framework/op_call_stack.cc b/paddle/fluid/framework/op_call_stack.cc index 3a9b113ceac..80db35e0c39 100644 --- a/paddle/fluid/framework/op_call_stack.cc +++ b/paddle/fluid/framework/op_call_stack.cc @@ -35,26 +35,14 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, } std::ostringstream sout; - std::ostringstream sout_py_trace; // Step 1. Construct python call stack string if (callstack) { - sout_py_trace << "\n------------------------------------------\n"; - sout_py_trace << "Python Call Stacks (More useful to users):"; - sout_py_trace << "\n------------------------------------------\n"; + sout << "\n\n Compile Traceback (most recent call last):"; for (auto &line : *callstack) { - sout_py_trace << line; + sout << "\n " << line; } } - // Step 2. Insert python traceback into err_str_ - std::size_t found = exception->err_str_.rfind( - "\n----------------------\nError Message " - "Summary:\n----------------------\n"); - if (found != std::string::npos) { - exception->err_str_.insert(found, sout_py_trace.str()); - } else { - exception->err_str_.append(sout_py_trace.str()); - } - // Step 3. Construct final call stack & append error op name + // Step 2. Construct final call stack & append error op name sout << exception->err_str_; sout << " [operator < " << type << " > error]"; exception->err_str_ = sout.str(); diff --git a/paddle/fluid/framework/op_desc.cc b/paddle/fluid/framework/op_desc.cc index e490d571a69..66fe71a80a7 100644 --- a/paddle/fluid/framework/op_desc.cc +++ b/paddle/fluid/framework/op_desc.cc @@ -700,7 +700,7 @@ void OpDesc::InferShape(const BlockDesc &block) const { } infer_shape(&ctx); } catch (platform::EnforceNotMet &exception) { - framework::InsertCallStackInfo(Type(), attrs_, &exception); + framework::AppendErrorOpHint(Type(), &exception); throw std::move(exception); } catch (...) { std::rethrow_exception(std::current_exception()); diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index 475256826f3..9a3a639579b 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -232,16 +232,16 @@ inline std::string GetTraceBackString(StrType&& what, const char* file, static constexpr int TRACE_STACK_LIMIT = 100; std::ostringstream sout; - sout << "\n\n--------------------------------------------\n"; - sout << "C++ Call Stacks (More useful to developers):"; - sout << "\n--------------------------------------------\n"; + sout << "\n\n--------------------------------------\n"; + sout << "C++ Traceback (most recent call last):"; + sout << "\n--------------------------------------\n"; #if !defined(_WIN32) void* call_stack[TRACE_STACK_LIMIT]; auto size = backtrace(call_stack, TRACE_STACK_LIMIT); auto symbols = backtrace_symbols(call_stack, size); Dl_info info; int idx = 0; - for (int i = 0; i < size; ++i) { + for (int i = size - 1; i >= 0; --i) { if (dladdr(call_stack[i], &info) && info.dli_sname) { auto demangled = demangle(info.dli_sname); std::string path(info.dli_fname); diff --git a/python/paddle/fluid/framework.py b/python/paddle/fluid/framework.py index 1c28ecd3a8f..9ba1b33c739 100644 --- a/python/paddle/fluid/framework.py +++ b/python/paddle/fluid/framework.py @@ -1913,8 +1913,13 @@ class Operator(object): "`type` to initialized an Operator can not be None.") else: callstack_var_name = op_maker.kOpCreationCallstackAttrName() - op_attrs[callstack_var_name] = list( - reversed(traceback.format_stack()))[1:] + op_attrs[callstack_var_name] = [] + for frame in traceback.extract_stack(): + op_attrs[callstack_var_name].append( + ' File "{}", line {}, in {}'.format(frame[0], frame[1], + frame[2])) + op_attrs[callstack_var_name].append(' {}'.format(frame[ + 3])) self.desc.set_type(type) proto = OpProtoHolder.instance().get_op_proto(type) -- GitLab