From 0d9b12fa43dae74f364c3c2483d26914f436182c Mon Sep 17 00:00:00 2001 From: Shang Zhizhou Date: Wed, 29 Dec 2021 13:46:23 +0800 Subject: [PATCH] fix reduce_max/reduce_min bug (#38478) --- 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 74d68572a3e..0c6cb3e05ee 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 caee7d9e5c2..5e413e80d71 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 @@ class ApiMaxTest(unittest.TestCase): 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): -- GitLab