diff --git a/paddle/fluid/operators/sum_op.cc b/paddle/fluid/operators/sum_op.cc index 1c59fd99ba64dc530c93d49eb6dfddc381478f85..39d6a992043072ed98bf35a060b99233dd927bc7 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 8af9c52266a70b8e55c5fa1ae285de16ebd7a7ac..c0cd88a0a6aa044d9ffeda6b18358daae5f43830 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)