未验证 提交 fea0e294 编写于 作者: C Chen Weihang 提交者: GitHub

Hide the C++ stack by default and add hints (#29042)

* default not show cpp statck & add hint

* fix failed unittest

* fix failed unittests
上级 b1274ac3
...@@ -25,11 +25,11 @@ std::string InsertIndentationIntoEachLine(const std::string &str) { ...@@ -25,11 +25,11 @@ std::string InsertIndentationIntoEachLine(const std::string &str) {
std::ostringstream sout; std::ostringstream sout;
size_t start_pos = 0; size_t start_pos = 0;
size_t end_pos = 0; size_t end_pos = 0;
while ((end_pos = str.find("\n", start_pos)) != std::string::npos) { while ((end_pos = str.find_first_of("\n", start_pos)) != std::string::npos) {
sout << " " << str.substr(start_pos, end_pos + 1); sout << " " << str.substr(start_pos, end_pos - start_pos + 1);
start_pos = end_pos + 1; start_pos = end_pos + 1;
} }
sout << " " << str.substr(start_pos, end_pos); sout << " " << str.substr(start_pos, end_pos - start_pos + 1);
return sout.str(); return sout.str();
} }
...@@ -58,6 +58,7 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, ...@@ -58,6 +58,7 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs,
sout << "\n " << line; sout << "\n " << line;
} }
} }
VLOG(1) << exception->error_str();
// Step 2. Construct final call stack & append error op name // Step 2. Construct final call stack & append error op name
if (FLAGS_call_stack_level > 1) { if (FLAGS_call_stack_level > 1) {
sout << exception->what(); sout << exception->what();
......
...@@ -118,7 +118,7 @@ TEST(OpRegistry, IllegalAttr) { ...@@ -118,7 +118,7 @@ TEST(OpRegistry, IllegalAttr) {
paddle::framework::OpRegistry::CreateOp(op_desc); paddle::framework::OpRegistry::CreateOp(op_desc);
} catch (paddle::platform::EnforceNotMet& err) { } catch (paddle::platform::EnforceNotMet& err) {
caught = true; caught = true;
std::string msg = "OutOfRangeError"; std::string msg = "OutOfRange";
std::string err_msg = err.what(); std::string err_msg = err.what();
ASSERT_TRUE(err_msg.find(msg) != std::string::npos); ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
} }
...@@ -152,7 +152,7 @@ TEST(OpRegistry, CustomChecker) { ...@@ -152,7 +152,7 @@ TEST(OpRegistry, CustomChecker) {
paddle::framework::OpRegistry::CreateOp(op_desc); paddle::framework::OpRegistry::CreateOp(op_desc);
} catch (paddle::platform::EnforceNotMet& err) { } catch (paddle::platform::EnforceNotMet& err) {
caught = true; caught = true;
std::string msg = "InvalidArgumentError"; std::string msg = "InvalidArgument";
std::string err_msg = err.what(); std::string err_msg = err.what();
ASSERT_TRUE(err_msg.find(msg) != std::string::npos); ASSERT_TRUE(err_msg.find(msg) != std::string::npos);
} }
......
...@@ -279,8 +279,15 @@ inline std::string GetErrorSumaryString(StrType&& what, const char* file, ...@@ -279,8 +279,15 @@ inline std::string GetErrorSumaryString(StrType&& what, const char* file,
"Summary:\n----------------------\n"; "Summary:\n----------------------\n";
} }
sout << string::Sprintf("%s (at %s:%d)", std::forward<StrType>(what), file, sout << string::Sprintf("%s (at %s:%d)", std::forward<StrType>(what), file,
line) line);
<< std::endl; if (FLAGS_call_stack_level < 2) {
// NOTE(chenweihang): if no C++ backtrace, give a hint to tell users
// how to show C++ backtrace, this hint only show in 2.0-rc verison,
// and will be removed in 2.0 official version
sout << "\n [Hint: If need to show C++ stacktraces, please set "
"`FlAGS_call_stack_level=2`.]";
}
sout << std::endl;
return sout.str(); return sout.str();
} }
......
...@@ -20,32 +20,30 @@ limitations under the License. */ ...@@ -20,32 +20,30 @@ limitations under the License. */
using namespace paddle::platform::errors; // NOLINT using namespace paddle::platform::errors; // NOLINT
#define CHECK_PADDLE_THROW(EFUNC) \ #define CHECK_PADDLE_THROW(EFUNC) \
do { \ do { \
bool caught_exception = false; \ bool caught_exception = false; \
try { \ try { \
PADDLE_THROW((EFUNC)("paddle throw test.")); \ PADDLE_THROW((EFUNC)("paddle throw test.")); \
} catch (paddle::platform::EnforceNotMet & error) { \ } catch (paddle::platform::EnforceNotMet & error) { \
caught_exception = true; \ caught_exception = true; \
std::string ex_msg = error.what(); \ std::string ex_msg = error.what(); \
EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle throw test.") != \ EXPECT_TRUE(ex_msg.find("paddle throw test.") != std::string::npos); \
std::string::npos); \ } \
} \ EXPECT_TRUE(caught_exception); \
EXPECT_TRUE(caught_exception); \
} while (0) } while (0)
#define CHECK_PADDLE_ENFORCE(EFUNC) \ #define CHECK_PADDLE_ENFORCE(EFUNC) \
do { \ do { \
bool caught_exception = false; \ bool caught_exception = false; \
try { \ try { \
PADDLE_ENFORCE(false, (EFUNC)("paddle enforce test.")); \ PADDLE_ENFORCE(false, (EFUNC)("paddle enforce test.")); \
} catch (paddle::platform::EnforceNotMet & error) { \ } catch (paddle::platform::EnforceNotMet & error) { \
caught_exception = true; \ caught_exception = true; \
std::string ex_msg = error.what(); \ std::string ex_msg = error.what(); \
EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle enforce test.") != \ EXPECT_TRUE(ex_msg.find("paddle enforce test.") != std::string::npos); \
std::string::npos); \ } \
} \ EXPECT_TRUE(caught_exception); \
EXPECT_TRUE(caught_exception); \
} while (0) } while (0)
#define CHECK_PADDLE_ENFORCE_NOT_NULL(EFUNC) \ #define CHECK_PADDLE_ENFORCE_NOT_NULL(EFUNC) \
...@@ -57,25 +55,24 @@ using namespace paddle::platform::errors; // NOLINT ...@@ -57,25 +55,24 @@ using namespace paddle::platform::errors; // NOLINT
} catch (paddle::platform::EnforceNotMet & error) { \ } catch (paddle::platform::EnforceNotMet & error) { \
caught_exception = true; \ caught_exception = true; \
std::string ex_msg = error.what(); \ std::string ex_msg = error.what(); \
EXPECT_TRUE( \ EXPECT_TRUE(ex_msg.find("paddle enforce not null test.") != \
ex_msg.find(#EFUNC "Error: paddle enforce not null test.") != \ std::string::npos); \
std::string::npos); \
} \ } \
EXPECT_TRUE(caught_exception); \ EXPECT_TRUE(caught_exception); \
} while (0) } while (0)
#define CHECK_PADDLE_ENFORCE_EQ(EFUNC) \ #define CHECK_PADDLE_ENFORCE_EQ(EFUNC) \
do { \ do { \
bool caught_exception = false; \ bool caught_exception = false; \
try { \ try { \
PADDLE_ENFORCE_EQ(1, 2, (EFUNC)("paddle enforce equal test.")); \ PADDLE_ENFORCE_EQ(1, 2, (EFUNC)("paddle enforce equal test.")); \
} catch (paddle::platform::EnforceNotMet & error) { \ } catch (paddle::platform::EnforceNotMet & error) { \
caught_exception = true; \ caught_exception = true; \
std::string ex_msg = error.what(); \ std::string ex_msg = error.what(); \
EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle enforce equal test.") != \ EXPECT_TRUE(ex_msg.find("paddle enforce equal test.") != \
std::string::npos); \ std::string::npos); \
} \ } \
EXPECT_TRUE(caught_exception); \ EXPECT_TRUE(caught_exception); \
} while (0) } while (0)
#define CHECK_ALL_PADDLE_EXCEPTION_MACRO(EFUNC) \ #define CHECK_ALL_PADDLE_EXCEPTION_MACRO(EFUNC) \
......
...@@ -499,7 +499,7 @@ DEFINE_bool(use_mkldnn, false, "Use MKLDNN to run"); ...@@ -499,7 +499,7 @@ DEFINE_bool(use_mkldnn, false, "Use MKLDNN to run");
* message summary will be shown. * message summary will be shown.
*/ */
DEFINE_int32( DEFINE_int32(
call_stack_level, 2, call_stack_level, 1,
"Determine the call stack to print when error or exeception happens." "Determine the call stack to print when error or exeception happens."
// TODO(zhiqiu): implement logic of FLAGS_call_stack_level==0 // TODO(zhiqiu): implement logic of FLAGS_call_stack_level==0
// "If FLAGS_call_stack_level == 0, only the error message summary will be " // "If FLAGS_call_stack_level == 0, only the error message summary will be "
......
...@@ -437,7 +437,7 @@ class TestMatMulOpTransposeReshapeTransposeAxisNotSupportedException( ...@@ -437,7 +437,7 @@ class TestMatMulOpTransposeReshapeTransposeAxisNotSupportedException(
def test_check_output(self): def test_check_output(self):
self.assertRaises(AttributeError, self.check_raise_error, self.assertRaises(AttributeError, self.check_raise_error,
'InvalidArgumentError: supported transpose axis ' 'supported transpose axis '
'for the fuse are {0, 2, 1, 3}') 'for the fuse are {0, 2, 1, 3}')
...@@ -449,9 +449,8 @@ class TestMatMulOpTransposeReshapeTransposeRankNotSupportedException( ...@@ -449,9 +449,8 @@ class TestMatMulOpTransposeReshapeTransposeRankNotSupportedException(
self.out = np.matmul(self.x, self.y) self.out = np.matmul(self.x, self.y)
def test_check_output(self): def test_check_output(self):
self.assertRaises( self.assertRaises(AttributeError, self.check_raise_error,
AttributeError, self.check_raise_error, 'transpose_out supported rank is 4')
'InvalidArgumentError: transpose_out supported rank is 4')
class TestMatMulOpTransposeReshapeRankOfReshapeNotSupportedException( class TestMatMulOpTransposeReshapeRankOfReshapeNotSupportedException(
...@@ -462,9 +461,8 @@ class TestMatMulOpTransposeReshapeRankOfReshapeNotSupportedException( ...@@ -462,9 +461,8 @@ class TestMatMulOpTransposeReshapeRankOfReshapeNotSupportedException(
self.out = np.matmul(self.x, self.y) self.out = np.matmul(self.x, self.y)
def test_check_output(self): def test_check_output(self):
self.assertRaises( self.assertRaises(AttributeError, self.check_raise_error,
AttributeError, self.check_raise_error, 'reshape_out supported rank is 3')
'InvalidArgumentError: reshape_out supported rank is 3')
if __name__ == "__main__": if __name__ == "__main__":
......
...@@ -83,10 +83,12 @@ class TestFunctionalPReluAPI(unittest.TestCase): ...@@ -83,10 +83,12 @@ class TestFunctionalPReluAPI(unittest.TestCase):
# The input type must be Variable. # The input type must be Variable.
self.assertRaises(TypeError, F.prelu, x=1, weight=weight_fp32) self.assertRaises(TypeError, F.prelu, x=1, weight=weight_fp32)
# The input dtype must be float16, float32, float64. # The input dtype must be float16, float32, float64.
x_int32 = paddle.fluid.data(name='x_int32', shape=[2, 3], dtype='int32') x_int32 = paddle.fluid.data(
name='x_int32', shape=[2, 3], dtype='int32')
self.assertRaises(TypeError, F.prelu, x=x_int32, weight=weight_fp32) self.assertRaises(TypeError, F.prelu, x=x_int32, weight=weight_fp32)
# support the input dtype is float16 # support the input dtype is float16
x_fp16 = paddle.fluid.data(name='x_fp16', shape=[2, 3], dtype='float16') x_fp16 = paddle.fluid.data(
name='x_fp16', shape=[2, 3], dtype='float16')
F.prelu(x=x_fp16, weight=weight_fp32) F.prelu(x=x_fp16, weight=weight_fp32)
...@@ -100,7 +102,8 @@ class TestNNPReluAPI(unittest.TestCase): ...@@ -100,7 +102,8 @@ class TestNNPReluAPI(unittest.TestCase):
startup_program = paddle.static.Program() startup_program = paddle.static.Program()
train_program = paddle.static.Program() train_program = paddle.static.Program()
with paddle.static.program_guard(train_program, startup_program): with paddle.static.program_guard(train_program, startup_program):
x = paddle.fluid.data(name='X', shape=self.x_np.shape, dtype='float32') x = paddle.fluid.data(
name='X', shape=self.x_np.shape, dtype='float32')
m = paddle.nn.PReLU() m = paddle.nn.PReLU()
out = m(x) out = m(x)
exe = paddle.static.Executor(self.place) exe = paddle.static.Executor(self.place)
...@@ -296,7 +299,7 @@ class TestModeError(unittest.TestCase): ...@@ -296,7 +299,7 @@ class TestModeError(unittest.TestCase):
try: try:
y = prelu_t(x, 'any') y = prelu_t(x, 'any')
except Exception as e: except Exception as e:
assert (e.args[0].find('InvalidArgumentError') != -1) assert (e.args[0].find('InvalidArgument') != -1)
if __name__ == "__main__": if __name__ == "__main__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册