From 664c342ca02a657bf6fb9be2c12d9ab35c817a09 Mon Sep 17 00:00:00 2001 From: zhoukunsheng Date: Mon, 25 Mar 2019 19:07:04 +0800 Subject: [PATCH] test=develop split reduce_all_any_op.h into two files add unit test for reduce_all, reduce_any --- .../operators/reduce_ops/reduce_all_op.cc | 2 +- .../operators/reduce_ops/reduce_all_op.cu | 2 +- .../{reduce_all_any_op.h => reduce_all_op.h} | 9 +-- .../operators/reduce_ops/reduce_any_op.cc | 2 +- .../operators/reduce_ops/reduce_any_op.cu | 2 +- .../operators/reduce_ops/reduce_any_op.h | 29 ++++++++ .../fluid/tests/unittests/test_reduce_op.py | 72 +++++++++++++++++++ 7 files changed, 106 insertions(+), 12 deletions(-) rename paddle/fluid/operators/reduce_ops/{reduce_all_any_op.h => reduce_all_op.h} (77%) create mode 100644 paddle/fluid/operators/reduce_ops/reduce_any_op.h diff --git a/paddle/fluid/operators/reduce_ops/reduce_all_op.cc b/paddle/fluid/operators/reduce_ops/reduce_all_op.cc index af4de3eaab5..b087fbbb94c 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_all_op.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_all_op.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/operators/reduce_ops/reduce_all_any_op.h" +#include "paddle/fluid/operators/reduce_ops/reduce_all_op.h" REGISTER_REDUCE_OP(reduce_all); REGISTER_OP_CPU_KERNEL(reduce_all, diff --git a/paddle/fluid/operators/reduce_ops/reduce_all_op.cu b/paddle/fluid/operators/reduce_ops/reduce_all_op.cu index 7150221f433..bd94ba263d9 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_all_op.cu +++ b/paddle/fluid/operators/reduce_ops/reduce_all_op.cu @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/operators/reduce_ops/reduce_all_any_op.h" +#include "paddle/fluid/operators/reduce_ops/reduce_all_op.h" REGISTER_OP_CUDA_KERNEL(reduce_all, ops::ReduceKernel - void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) { - y->device(place) = x->any(dim); - } -}; - } // namespace operators } // namespace paddle diff --git a/paddle/fluid/operators/reduce_ops/reduce_any_op.cc b/paddle/fluid/operators/reduce_ops/reduce_any_op.cc index f65b2ddc6f4..d865dcb3c93 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_any_op.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_any_op.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/operators/reduce_ops/reduce_all_any_op.h" +#include "paddle/fluid/operators/reduce_ops/reduce_any_op.h" REGISTER_REDUCE_OP(reduce_any); REGISTER_OP_CPU_KERNEL(reduce_any, diff --git a/paddle/fluid/operators/reduce_ops/reduce_any_op.cu b/paddle/fluid/operators/reduce_ops/reduce_any_op.cu index 389166fec69..66f0c9997ea 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_any_op.cu +++ b/paddle/fluid/operators/reduce_ops/reduce_any_op.cu @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "paddle/fluid/operators/reduce_ops/reduce_all_any_op.h" +#include "paddle/fluid/operators/reduce_ops/reduce_any_op.h" REGISTER_OP_CUDA_KERNEL(reduce_any, ops::ReduceKernel + void operator()(const DeviceContext& place, X* x, Y* y, const Dim& dim) { + y->device(place) = x->any(dim); + } +}; + +} // namespace operators +} // namespace paddle diff --git a/python/paddle/fluid/tests/unittests/test_reduce_op.py b/python/paddle/fluid/tests/unittests/test_reduce_op.py index 8fc8125a773..65fc1453d8d 100644 --- a/python/paddle/fluid/tests/unittests/test_reduce_op.py +++ b/python/paddle/fluid/tests/unittests/test_reduce_op.py @@ -91,6 +91,78 @@ class TestProdOp(OpTest): self.check_grad(['X'], 'Out') +class TestAllOp(OpTest): + def setUp(self): + self.op_type = "reduce_all" + self.inputs = {'X': np.random.randint(0, 2, (5, 6, 10)).astype("bool")} + self.outputs = {'Out': self.inputs['X'].all()} + self.attrs = {'reduce_all': True} + + def test_check_output(self): + self.check_output() + + +class TestAllOpWithDim(OpTest): + def setUp(self): + self.op_type = "reduce_all" + self.inputs = {'X': np.random.randint(0, 2, (5, 6, 10)).astype("bool")} + self.attrs = {'dim': [1]} + self.outputs = {'Out': self.inputs['X'].all(axis=1)} + + def test_check_output(self): + self.check_output() + + +class TestAllOpWithKeepDim(OpTest): + def setUp(self): + self.op_type = "reduce_all" + self.inputs = {'X': np.random.randint(0, 2, (5, 6, 10)).astype("bool")} + self.attrs = {'dim': [1], 'keep_dim': True} + self.outputs = { + 'Out': np.expand_dims( + self.inputs['X'].all(axis=1), axis=1) + } + + def test_check_output(self): + self.check_output() + + +class TestAnyOp(OpTest): + def setUp(self): + self.op_type = "reduce_any" + self.inputs = {'X': np.random.randint(0, 2, (5, 6, 10)).astype("bool")} + self.outputs = {'Out': self.inputs['X'].any()} + self.attrs = {'reduce_all': True} + + def test_check_output(self): + self.check_output() + + +class TestAnyOpWithDim(OpTest): + def setUp(self): + self.op_type = "reduce_any" + self.inputs = {'X': np.random.randint(0, 2, (5, 6, 10)).astype("bool")} + self.attrs = {'dim': [1]} + self.outputs = {'Out': self.inputs['X'].any(axis=1)} + + def test_check_output(self): + self.check_output() + + +class TestAnyOpWithKeepDim(OpTest): + def setUp(self): + self.op_type = "reduce_any" + self.inputs = {'X': np.random.randint(0, 2, (5, 6, 10)).astype("bool")} + self.attrs = {'dim': [1], 'keep_dim': True} + self.outputs = { + 'Out': np.expand_dims( + self.inputs['X'].any(axis=1), axis=1) + } + + def test_check_output(self): + self.check_output() + + class Test1DReduce(OpTest): def setUp(self): self.op_type = "reduce_sum" -- GitLab