From fea0e294eea1a125ad1ec31f4e394c934c5d8d19 Mon Sep 17 00:00:00 2001 From: Chen Weihang Date: Wed, 25 Nov 2020 21:58:53 +0800 Subject: [PATCH] Hide the C++ stack by default and add hints (#29042) * default not show cpp statck & add hint * fix failed unittest * fix failed unittests --- paddle/fluid/framework/op_call_stack.cc | 7 +- paddle/fluid/framework/op_registry_test.cc | 4 +- paddle/fluid/platform/enforce.h | 11 ++- paddle/fluid/platform/errors_test.cc | 75 +++++++++---------- paddle/fluid/platform/flags.cc | 2 +- .../unittests/mkldnn/test_matmul_mkldnn_op.py | 12 ++- .../fluid/tests/unittests/test_prelu_op.py | 11 ++- 7 files changed, 64 insertions(+), 58 deletions(-) diff --git a/paddle/fluid/framework/op_call_stack.cc b/paddle/fluid/framework/op_call_stack.cc index 380ba74a1c..757095444c 100644 --- a/paddle/fluid/framework/op_call_stack.cc +++ b/paddle/fluid/framework/op_call_stack.cc @@ -25,11 +25,11 @@ std::string InsertIndentationIntoEachLine(const std::string &str) { std::ostringstream sout; size_t start_pos = 0; size_t end_pos = 0; - while ((end_pos = str.find("\n", start_pos)) != std::string::npos) { - sout << " " << str.substr(start_pos, end_pos + 1); + while ((end_pos = str.find_first_of("\n", start_pos)) != std::string::npos) { + sout << " " << str.substr(start_pos, end_pos - start_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(); } @@ -58,6 +58,7 @@ void InsertCallStackInfo(const std::string &type, const AttributeMap &attrs, sout << "\n " << line; } } + VLOG(1) << exception->error_str(); // Step 2. Construct final call stack & append error op name if (FLAGS_call_stack_level > 1) { sout << exception->what(); diff --git a/paddle/fluid/framework/op_registry_test.cc b/paddle/fluid/framework/op_registry_test.cc index 45fe66d7db..889b6b0c86 100644 --- a/paddle/fluid/framework/op_registry_test.cc +++ b/paddle/fluid/framework/op_registry_test.cc @@ -118,7 +118,7 @@ TEST(OpRegistry, IllegalAttr) { paddle::framework::OpRegistry::CreateOp(op_desc); } catch (paddle::platform::EnforceNotMet& err) { caught = true; - std::string msg = "OutOfRangeError"; + std::string msg = "OutOfRange"; std::string err_msg = err.what(); ASSERT_TRUE(err_msg.find(msg) != std::string::npos); } @@ -152,7 +152,7 @@ TEST(OpRegistry, CustomChecker) { paddle::framework::OpRegistry::CreateOp(op_desc); } catch (paddle::platform::EnforceNotMet& err) { caught = true; - std::string msg = "InvalidArgumentError"; + std::string msg = "InvalidArgument"; std::string err_msg = err.what(); ASSERT_TRUE(err_msg.find(msg) != std::string::npos); } diff --git a/paddle/fluid/platform/enforce.h b/paddle/fluid/platform/enforce.h index fc57d3a4d0..fb95b439b3 100644 --- a/paddle/fluid/platform/enforce.h +++ b/paddle/fluid/platform/enforce.h @@ -279,8 +279,15 @@ inline std::string GetErrorSumaryString(StrType&& what, const char* file, "Summary:\n----------------------\n"; } sout << string::Sprintf("%s (at %s:%d)", std::forward(what), file, - line) - << std::endl; + line); + 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(); } diff --git a/paddle/fluid/platform/errors_test.cc b/paddle/fluid/platform/errors_test.cc index a73c1ba3d3..712b67a654 100644 --- a/paddle/fluid/platform/errors_test.cc +++ b/paddle/fluid/platform/errors_test.cc @@ -20,32 +20,30 @@ limitations under the License. */ using namespace paddle::platform::errors; // NOLINT -#define CHECK_PADDLE_THROW(EFUNC) \ - do { \ - bool caught_exception = false; \ - try { \ - PADDLE_THROW((EFUNC)("paddle throw test.")); \ - } catch (paddle::platform::EnforceNotMet & error) { \ - caught_exception = true; \ - std::string ex_msg = error.what(); \ - EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle throw test.") != \ - std::string::npos); \ - } \ - EXPECT_TRUE(caught_exception); \ +#define CHECK_PADDLE_THROW(EFUNC) \ + do { \ + bool caught_exception = false; \ + try { \ + PADDLE_THROW((EFUNC)("paddle throw test.")); \ + } catch (paddle::platform::EnforceNotMet & error) { \ + caught_exception = true; \ + std::string ex_msg = error.what(); \ + EXPECT_TRUE(ex_msg.find("paddle throw test.") != std::string::npos); \ + } \ + EXPECT_TRUE(caught_exception); \ } while (0) -#define CHECK_PADDLE_ENFORCE(EFUNC) \ - do { \ - bool caught_exception = false; \ - try { \ - PADDLE_ENFORCE(false, (EFUNC)("paddle enforce test.")); \ - } catch (paddle::platform::EnforceNotMet & error) { \ - caught_exception = true; \ - std::string ex_msg = error.what(); \ - EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle enforce test.") != \ - std::string::npos); \ - } \ - EXPECT_TRUE(caught_exception); \ +#define CHECK_PADDLE_ENFORCE(EFUNC) \ + do { \ + bool caught_exception = false; \ + try { \ + PADDLE_ENFORCE(false, (EFUNC)("paddle enforce test.")); \ + } catch (paddle::platform::EnforceNotMet & error) { \ + caught_exception = true; \ + std::string ex_msg = error.what(); \ + EXPECT_TRUE(ex_msg.find("paddle enforce test.") != std::string::npos); \ + } \ + EXPECT_TRUE(caught_exception); \ } while (0) #define CHECK_PADDLE_ENFORCE_NOT_NULL(EFUNC) \ @@ -57,25 +55,24 @@ using namespace paddle::platform::errors; // NOLINT } catch (paddle::platform::EnforceNotMet & error) { \ caught_exception = true; \ std::string ex_msg = error.what(); \ - EXPECT_TRUE( \ - ex_msg.find(#EFUNC "Error: paddle enforce not null test.") != \ - std::string::npos); \ + EXPECT_TRUE(ex_msg.find("paddle enforce not null test.") != \ + std::string::npos); \ } \ EXPECT_TRUE(caught_exception); \ } while (0) -#define CHECK_PADDLE_ENFORCE_EQ(EFUNC) \ - do { \ - bool caught_exception = false; \ - try { \ - PADDLE_ENFORCE_EQ(1, 2, (EFUNC)("paddle enforce equal test.")); \ - } catch (paddle::platform::EnforceNotMet & error) { \ - caught_exception = true; \ - std::string ex_msg = error.what(); \ - EXPECT_TRUE(ex_msg.find(#EFUNC "Error: paddle enforce equal test.") != \ - std::string::npos); \ - } \ - EXPECT_TRUE(caught_exception); \ +#define CHECK_PADDLE_ENFORCE_EQ(EFUNC) \ + do { \ + bool caught_exception = false; \ + try { \ + PADDLE_ENFORCE_EQ(1, 2, (EFUNC)("paddle enforce equal test.")); \ + } catch (paddle::platform::EnforceNotMet & error) { \ + caught_exception = true; \ + std::string ex_msg = error.what(); \ + EXPECT_TRUE(ex_msg.find("paddle enforce equal test.") != \ + std::string::npos); \ + } \ + EXPECT_TRUE(caught_exception); \ } while (0) #define CHECK_ALL_PADDLE_EXCEPTION_MACRO(EFUNC) \ diff --git a/paddle/fluid/platform/flags.cc b/paddle/fluid/platform/flags.cc index ca71654b4a..378071964f 100644 --- a/paddle/fluid/platform/flags.cc +++ b/paddle/fluid/platform/flags.cc @@ -499,7 +499,7 @@ DEFINE_bool(use_mkldnn, false, "Use MKLDNN to run"); * message summary will be shown. */ DEFINE_int32( - call_stack_level, 2, + call_stack_level, 1, "Determine the call stack to print when error or exeception happens." // TODO(zhiqiu): implement logic of FLAGS_call_stack_level==0 // "If FLAGS_call_stack_level == 0, only the error message summary will be " diff --git a/python/paddle/fluid/tests/unittests/mkldnn/test_matmul_mkldnn_op.py b/python/paddle/fluid/tests/unittests/mkldnn/test_matmul_mkldnn_op.py index 11b453125d..9a5443eed1 100644 --- a/python/paddle/fluid/tests/unittests/mkldnn/test_matmul_mkldnn_op.py +++ b/python/paddle/fluid/tests/unittests/mkldnn/test_matmul_mkldnn_op.py @@ -437,7 +437,7 @@ class TestMatMulOpTransposeReshapeTransposeAxisNotSupportedException( def test_check_output(self): self.assertRaises(AttributeError, self.check_raise_error, - 'InvalidArgumentError: supported transpose axis ' + 'supported transpose axis ' 'for the fuse are {0, 2, 1, 3}') @@ -449,9 +449,8 @@ class TestMatMulOpTransposeReshapeTransposeRankNotSupportedException( self.out = np.matmul(self.x, self.y) def test_check_output(self): - self.assertRaises( - AttributeError, self.check_raise_error, - 'InvalidArgumentError: transpose_out supported rank is 4') + self.assertRaises(AttributeError, self.check_raise_error, + 'transpose_out supported rank is 4') class TestMatMulOpTransposeReshapeRankOfReshapeNotSupportedException( @@ -462,9 +461,8 @@ class TestMatMulOpTransposeReshapeRankOfReshapeNotSupportedException( self.out = np.matmul(self.x, self.y) def test_check_output(self): - self.assertRaises( - AttributeError, self.check_raise_error, - 'InvalidArgumentError: reshape_out supported rank is 3') + self.assertRaises(AttributeError, self.check_raise_error, + 'reshape_out supported rank is 3') if __name__ == "__main__": diff --git a/python/paddle/fluid/tests/unittests/test_prelu_op.py b/python/paddle/fluid/tests/unittests/test_prelu_op.py index f33b375029..e0db1bab3a 100644 --- a/python/paddle/fluid/tests/unittests/test_prelu_op.py +++ b/python/paddle/fluid/tests/unittests/test_prelu_op.py @@ -83,10 +83,12 @@ class TestFunctionalPReluAPI(unittest.TestCase): # The input type must be Variable. self.assertRaises(TypeError, F.prelu, x=1, weight=weight_fp32) # 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) # 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) @@ -100,7 +102,8 @@ class TestNNPReluAPI(unittest.TestCase): startup_program = paddle.static.Program() train_program = paddle.static.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() out = m(x) exe = paddle.static.Executor(self.place) @@ -296,7 +299,7 @@ class TestModeError(unittest.TestCase): try: y = prelu_t(x, 'any') except Exception as e: - assert (e.args[0].find('InvalidArgumentError') != -1) + assert (e.args[0].find('InvalidArgument') != -1) if __name__ == "__main__": -- GitLab