From d43e4047ff57b84693a3b94886bd8a11d9c2bb66 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 15 May 2020 15:24:30 +0800 Subject: [PATCH] refine err msg, test=develop (#24550) --- paddle/fluid/operators/sum_op.cc | 7 +++++++ python/paddle/fluid/tests/unittests/test_sum_op.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/paddle/fluid/operators/sum_op.cc b/paddle/fluid/operators/sum_op.cc index 1c59fd99ba..39d6a99204 100644 --- a/paddle/fluid/operators/sum_op.cc +++ b/paddle/fluid/operators/sum_op.cc @@ -114,6 +114,13 @@ class SumOp : public framework::OperatorWithKernel { framework::LibraryType library{framework::LibraryType::kPlain}; framework::DataLayout layout{framework::DataLayout::kAnyLayout}; + PADDLE_ENFORCE_GT(x_vars.size(), 0, platform::errors::InvalidArgument( + "Input[X] should not be empty")); + + PADDLE_ENFORCE_NOT_NULL( + x_vars[0], platform::errors::NotFound( + "Input var[%s] should not be nullptr", x_vars_name[0])); + if (x_vars[0]->IsType()) { int dtype = -1; for (size_t idx = 0; idx < x_vars.size(); ++idx) { diff --git a/python/paddle/fluid/tests/unittests/test_sum_op.py b/python/paddle/fluid/tests/unittests/test_sum_op.py index 8af9c52266..c0cd88a0a6 100644 --- a/python/paddle/fluid/tests/unittests/test_sum_op.py +++ b/python/paddle/fluid/tests/unittests/test_sum_op.py @@ -298,6 +298,20 @@ class TestRaiseSumsError(unittest.TestCase): self.assertRaises(TypeError, test_out_dtype) +class TestSumOpError(unittest.TestCase): + def test_errors(self): + def test_empty_list_input(): + with fluid.dygraph.guard(): + fluid.core.ops.sum([]) + + def test_list_of_none_input(): + with fluid.dygraph.guard(): + fluid.core.ops.sum([None]) + + self.assertRaises(Exception, test_empty_list_input) + self.assertRaises(Exception, test_list_of_none_input) + + create_test_sum_fp16_class(TestSelectedRowsSumOp) create_test_sum_fp16_class(TestLoDTensorAndSelectedRowsOp) -- GitLab