未验证 提交 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) {
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();
......
......@@ -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);
}
......
......@@ -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<StrType>(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();
}
......
......@@ -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) \
......
......@@ -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 "
......
......@@ -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__":
......
......@@ -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__":
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册