Skip to content

Commit

Permalink
fix divide zero bug for paddle.all (#51088)
Browse files Browse the repository at this point in the history
  • Loading branch information
1180300923 authored Mar 2, 2023
1 parent 77c9c90 commit 2bcd393
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
7 changes: 7 additions & 0 deletions paddle/phi/kernels/reduce_all_kernel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ void AllKernel(const Context& dev_ctx,
const std::vector<int64_t>& dims,
bool keep_dim,
DenseTensor* out) {
auto x_dim = x.dims();
for (int i = 0; i < x_dim.size(); i++) {
PADDLE_ENFORCE_LT(0,
x_dim[i],
errors::InvalidArgument(
"The dims of Input(X) should be greater than 0."));
}
bool reduce_all = recompute_reduce_all(x, dims);
AllRawKernel<T>(dev_ctx, x, dims, keep_dim, reduce_all, out);
}
Expand Down
12 changes: 12 additions & 0 deletions python/paddle/fluid/tests/unittests/test_reduce_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -1250,6 +1250,18 @@ def test_dygraph(self):
paddle.enable_static()


class TestAllZeroError(unittest.TestCase):
def test_errors(self):
with paddle.fluid.dygraph.guard():

def test_0_size():
array = np.array([], dtype=np.float32)
x = paddle.to_tensor(np.reshape(array, [0, 0, 0]), dtype='bool')
paddle.all(x, axis=1)

self.assertRaises(ValueError, test_0_size)


if __name__ == '__main__':
paddle.enable_static()
unittest.main()

0 comments on commit 2bcd393

Please sign in to comment.