From 8ff42223b752aa834f3d621c31f7d466f7650f44 Mon Sep 17 00:00:00 2001 From: shangzhizhou Date: Mon, 27 Dec 2021 15:52:55 +0800 Subject: [PATCH] fix reduce_max/reduce_min bug --- paddle/fluid/operators/reduce_ops/reduce_op.h | 7 ++++--- python/paddle/fluid/tests/unittests/test_max_op.py | 9 +++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/paddle/fluid/operators/reduce_ops/reduce_op.h b/paddle/fluid/operators/reduce_ops/reduce_op.h index 74d68572a3e41..0c6cb3e05eedc 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_op.h +++ b/paddle/fluid/operators/reduce_ops/reduce_op.h @@ -241,11 +241,12 @@ class ReduceKernel : public framework::OpKernel { framework::proto::VarType::Type cast_out_dtype; // The dims has full dim, set the reduce_all is True - const auto& input_dim_size = context.Input("X")->dims().size(); + const int& input_dim_size = context.Input("X")->dims().size(); std::set dims_set(dims.begin(), dims.end()); bool full_dim = true; - for (auto i = 0; i < input_dim_size; i++) { - if (dims_set.find(i) == dims_set.end()) { + for (int i = 0; i < input_dim_size; i++) { + if (dims_set.find(i) == dims_set.end() && + dims_set.find(i - input_dim_size) == dims_set.end()) { full_dim = false; break; } diff --git a/python/paddle/fluid/tests/unittests/test_max_op.py b/python/paddle/fluid/tests/unittests/test_max_op.py index caee7d9e5c2ba..5e413e80d7143 100644 --- a/python/paddle/fluid/tests/unittests/test_max_op.py +++ b/python/paddle/fluid/tests/unittests/test_max_op.py @@ -98,6 +98,15 @@ def test_big_dimension(self): self.assertEqual((np_z1 == z_expected).all(), True) self.assertEqual((np_z2 == z_expected).all(), True) + def test_all_negative_axis(self): + paddle.disable_static() + x = paddle.rand(shape=[2, 2]) + np_x = x.numpy() + z1 = paddle.max(x, axis=(-2, -1)) + np_z1 = z1.numpy() + z_expected = np.array(np.max(np_x, axis=(0, 1))) + self.assertEqual((np_z1 == z_expected).all(), True) + class TestOutDtype(unittest.TestCase): def test_max(self):