From 57e6cdd1cd288388bae637f38e434ec2ef102ba4 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Thu, 5 Aug 2021 15:32:25 +0800 Subject: [PATCH 1/5] add size npu op --- paddle/fluid/operators/size_op_npu.cc | 60 +++++++++ .../tests/unittests/npu/test_size_op_npu.py | 125 ++++++++++++++++++ 2 files changed, 185 insertions(+) create mode 100644 paddle/fluid/operators/size_op_npu.cc create mode 100644 python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py diff --git a/paddle/fluid/operators/size_op_npu.cc b/paddle/fluid/operators/size_op_npu.cc new file mode 100644 index 0000000000000..0e62ff4243938 --- /dev/null +++ b/paddle/fluid/operators/size_op_npu.cc @@ -0,0 +1,60 @@ +// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include +#include + +#include "paddle/fluid/operators/mul_op.h" +#include "paddle/fluid/operators/npu_op_runner.h" + +namespace paddle { +namespace operators { + +template +class SizeNPUKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + VLOG(3) << "Ensure the op execute on NPU" << std::endl; + auto* x = ctx.Input("Input"); + auto* out = ctx.Output("Out"); + + framework::NPUAttributeMap attr_input = {}; + // set attrs if have + if (ctx.HasAttr("out_type")) { + attr_input["out_type"] = ctx.Attr("out_type"); + } + + out->mutable_data(ctx.GetPlace()); + const auto& runner = NpuOpRunner("Size", {*x}, {*out}, attr_input); + auto stream = + ctx.template device_context() + .stream(); + runner.Run(stream); + } +}; + +} // namespace operators +} // namespace paddle + +namespace ops = paddle::operators; + +REGISTER_OP_NPU_KERNEL( + size, ops::SizeNPUKernel, + ops::SizeNPUKernel, + ops::SizeNPUKernel, + ops::SizeNPUKernel, + ops::SizeNPUKernel, + ops::SizeNPUKernel); + diff --git a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py new file mode 100644 index 0000000000000..6a7579205cb01 --- /dev/null +++ b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py @@ -0,0 +1,125 @@ +# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import unittest +import numpy as np +import sys +sys.path.append("..") +import paddle +import paddle.fluid as fluid +from op_test import OpTest + +paddle.enable_static() + + +class TestSizeOp(OpTest): + def setUp(self): + self.set_npu() + self.place = paddle.NPUPlace(0) + + self.op_type = "size" + self.shape = [] + self.config() + input = np.zeros(self.shape, dtype='int32') + self.inputs = {'Input': input} + self.outputs = {'Out': np.array([np.size(input)], dtype='int64')} + + def config(self): + pass + + def test_check_output(self): + self.check_output_with_place(self.place) + + def set_npu(self): + self.__class__.use_npu = True + + +class TestRank1Tensor(TestSizeOp): + def config(self): + self.shape = [2] + + +class TestRank2Tensor(TestSizeOp): + def config(self): + self.shape = [2, 3] + + +class TestRank3Tensor(TestSizeOp): + def config(self): + self.shape = [2, 3, 100] + + +class TestLargeTensor(TestSizeOp): + def config(self): + self.shape = [2**10] + + +class TestSizeAPI(unittest.TestCase): + def setUp(self): + self.set_npu() + self.place = paddle.NPUPlace(0) + + def set_npu(self): + self.__class__.use_npu = True + + def test_size_static(self): + main_program = fluid.Program() + startup_program = fluid.Program() + with fluid.program_guard(main_program, startup_program): + shape1 = [2, 1, 4, 5] + shape2 = [1, 4, 5] + x_1 = paddle.fluid.data(shape=shape1, dtype='int32', name='x_1') + x_2 = paddle.fluid.data(shape=shape2, dtype='int32', name='x_2') + input_1 = np.random.random(shape1).astype("int32") + input_2 = np.random.random(shape2).astype("int32") + out_1 = paddle.fluid.layers.size(x_1) + out_2 = paddle.fluid.layers.size(x_2) + exe = paddle.static.Executor(place=self.place) + res_1, res_2 = exe.run(feed={ + "x_1": input_1, + "x_2": input_2, + }, + fetch_list=[out_1, out_2]) + assert (np.array_equal( + res_1, np.array([np.size(input_1)]).astype("int64"))) + assert (np.array_equal( + res_2, np.array([np.size(input_2)]).astype("int64"))) + + def test_size_imperative(self): + paddle.disable_static(self.place) + input_1 = np.random.random([2, 1, 4, 5]).astype("int32") + input_2 = np.random.random([1, 4, 5]).astype("int32") + x_1 = paddle.to_tensor(input_1) + x_2 = paddle.to_tensor(input_2) + out_1 = paddle.fluid.layers.size(x_1) + out_2 = paddle.fluid.layers.size(x_2) + assert (np.array_equal(out_1.numpy().item(0), np.size(input_1))) + assert (np.array_equal(out_2.numpy().item(0), np.size(input_2))) + paddle.enable_static() + + def test_error(self): + main_program = fluid.Program() + startup_program = fluid.Program() + with fluid.program_guard(main_program, startup_program): + + def test_x_type(): + shape = [1, 4, 5] + input_1 = np.random.random(shape).astype("int32") + out_1 = paddle.fluid.layers.size(input_1) + + self.assertRaises(TypeError, test_x_type) + + +if __name__ == '__main__': + unittest.main() From 8dc72917c4ffda648a5850ba7ae8646b6dec3ce2 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Fri, 6 Aug 2021 11:05:09 +0800 Subject: [PATCH 2/5] modify support data type --- paddle/fluid/operators/size_op_npu.cc | 5 ++--- .../fluid/tests/unittests/npu/test_size_op_npu.py | 10 ++++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) mode change 100644 => 100755 python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py diff --git a/paddle/fluid/operators/size_op_npu.cc b/paddle/fluid/operators/size_op_npu.cc index 0e62ff4243938..aa1f4c745e55c 100644 --- a/paddle/fluid/operators/size_op_npu.cc +++ b/paddle/fluid/operators/size_op_npu.cc @@ -31,8 +31,8 @@ class SizeNPUKernel : public framework::OpKernel { framework::NPUAttributeMap attr_input = {}; // set attrs if have - if (ctx.HasAttr("out_type")) { - attr_input["out_type"] = ctx.Attr("out_type"); + if (ctx.HasAttr("index")) { + attr_input["index"] = ctx.Attr("index"); } out->mutable_data(ctx.GetPlace()); @@ -57,4 +57,3 @@ REGISTER_OP_NPU_KERNEL( ops::SizeNPUKernel, ops::SizeNPUKernel, ops::SizeNPUKernel); - diff --git a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py old mode 100644 new mode 100755 index 6a7579205cb01..61ad3381de9e1 --- a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py @@ -27,11 +27,13 @@ class TestSizeOp(OpTest): def setUp(self): self.set_npu() self.place = paddle.NPUPlace(0) + # self.place = paddle.CPUPlace() + self.dtype = 'int32' self.op_type = "size" - self.shape = [] + self.shape = [1, 2] self.config() - input = np.zeros(self.shape, dtype='int32') + input = np.zeros(self.shape, dtype=self.dtype) self.inputs = {'Input': input} self.outputs = {'Out': np.array([np.size(input)], dtype='int64')} @@ -48,21 +50,25 @@ def set_npu(self): class TestRank1Tensor(TestSizeOp): def config(self): self.shape = [2] + self.dtype = 'double' class TestRank2Tensor(TestSizeOp): def config(self): self.shape = [2, 3] + self.dtype = 'float' class TestRank3Tensor(TestSizeOp): def config(self): self.shape = [2, 3, 100] + self.dtype = 'float16' class TestLargeTensor(TestSizeOp): def config(self): self.shape = [2**10] + self.dtype = 'bool' class TestSizeAPI(unittest.TestCase): From a7e135072fa6e9d46305238c1e18d9eaa5987963 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Sun, 15 Aug 2021 20:49:26 +0800 Subject: [PATCH 3/5] no longer use NPU size OP --- paddle/fluid/operators/size_op_npu.cc | 26 ++++++++-------- .../tests/unittests/npu/test_size_op_npu.py | 31 +++++++++++-------- 2 files changed, 31 insertions(+), 26 deletions(-) mode change 100644 => 100755 paddle/fluid/operators/size_op_npu.cc diff --git a/paddle/fluid/operators/size_op_npu.cc b/paddle/fluid/operators/size_op_npu.cc old mode 100644 new mode 100755 index aa1f4c745e55c..b5ff4af0133e4 --- a/paddle/fluid/operators/size_op_npu.cc +++ b/paddle/fluid/operators/size_op_npu.cc @@ -25,22 +25,22 @@ template class SizeNPUKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - VLOG(3) << "Ensure the op execute on NPU" << std::endl; auto* x = ctx.Input("Input"); auto* out = ctx.Output("Out"); - - framework::NPUAttributeMap attr_input = {}; - // set attrs if have - if (ctx.HasAttr("index")) { - attr_input["index"] = ctx.Attr("index"); - } - out->mutable_data(ctx.GetPlace()); - const auto& runner = NpuOpRunner("Size", {*x}, {*out}, attr_input); - auto stream = - ctx.template device_context() - .stream(); - runner.Run(stream); + + Tensor cpu_tensor; + auto cpu_data = + cpu_tensor.mutable_data(out->dims(), platform::CPUPlace()); + cpu_data[0] = x->numel(); + TensorCopy(cpu_tensor, ctx.GetPlace(), out); + + // framework::NPUAttributeMap attr_input = {}; + // const auto& runner = NpuOpRunner("Size", {*x}, {*out}, attr_input); + // auto stream = + // ctx.template device_context() + // .stream(); + // runner.Run(stream); } }; diff --git a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py index 61ad3381de9e1..0ce8b50f20741 100755 --- a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py @@ -28,17 +28,16 @@ def setUp(self): self.set_npu() self.place = paddle.NPUPlace(0) # self.place = paddle.CPUPlace() - self.dtype = 'int32' - self.op_type = "size" - self.shape = [1, 2] + self.config() input = np.zeros(self.shape, dtype=self.dtype) self.inputs = {'Input': input} - self.outputs = {'Out': np.array([np.size(input)], dtype='int64')} + self.outputs = {'Out': np.array([np.size(input)], dtype=np.int64)} def config(self): - pass + self.shape = [1, 2] + self.dtype = np.int32 def test_check_output(self): self.check_output_with_place(self.place) @@ -47,28 +46,34 @@ def set_npu(self): self.__class__.use_npu = True -class TestRank1Tensor(TestSizeOp): +class TestSizeOp1(TestSizeOp): def config(self): self.shape = [2] - self.dtype = 'double' + self.dtype = np.float64 -class TestRank2Tensor(TestSizeOp): +class TestSizeOp2(TestSizeOp): def config(self): self.shape = [2, 3] - self.dtype = 'float' + self.dtype = np.float32 -class TestRank3Tensor(TestSizeOp): +class TestSizeOp3(TestSizeOp): def config(self): self.shape = [2, 3, 100] - self.dtype = 'float16' + self.dtype = np.float16 -class TestLargeTensor(TestSizeOp): +class TestSizeOp4(TestSizeOp): def config(self): self.shape = [2**10] - self.dtype = 'bool' + self.dtype = np.bool + + +class TestSizeOp5(TestSizeOp): + def config(self): + self.shape = [2, 3, 4, 5] + self.dtype = np.int64 class TestSizeAPI(unittest.TestCase): From 855eb18ec3d3ac5dca17ecdea37a92b59a38faf1 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 16 Aug 2021 14:22:45 +0800 Subject: [PATCH 4/5] remove useless comments, add test case --- paddle/fluid/operators/size_op_npu.cc | 11 +++-------- .../fluid/tests/unittests/npu/test_size_op_npu.py | 9 +++++++-- 2 files changed, 10 insertions(+), 10 deletions(-) mode change 100755 => 100644 paddle/fluid/operators/size_op_npu.cc diff --git a/paddle/fluid/operators/size_op_npu.cc b/paddle/fluid/operators/size_op_npu.cc old mode 100755 new mode 100644 index b5ff4af0133e4..023a94c995850 --- a/paddle/fluid/operators/size_op_npu.cc +++ b/paddle/fluid/operators/size_op_npu.cc @@ -33,14 +33,9 @@ class SizeNPUKernel : public framework::OpKernel { auto cpu_data = cpu_tensor.mutable_data(out->dims(), platform::CPUPlace()); cpu_data[0] = x->numel(); - TensorCopy(cpu_tensor, ctx.GetPlace(), out); - - // framework::NPUAttributeMap attr_input = {}; - // const auto& runner = NpuOpRunner("Size", {*x}, {*out}, attr_input); - // auto stream = - // ctx.template device_context() - // .stream(); - // runner.Run(stream); + TensorCopy(cpu_tensor, ctx.GetPlace(), + ctx.template device_context(), out); + ctx.template device_context().Wait(); } }; diff --git a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py index 0ce8b50f20741..5ade23703d72b 100755 --- a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py @@ -27,7 +27,6 @@ class TestSizeOp(OpTest): def setUp(self): self.set_npu() self.place = paddle.NPUPlace(0) - # self.place = paddle.CPUPlace() self.op_type = "size" self.config() @@ -72,7 +71,13 @@ def config(self): class TestSizeOp5(TestSizeOp): def config(self): - self.shape = [2, 3, 4, 5] + self.shape = [7, 8, 9, 10] + self.dtype = np.int64 + + +class TestSizeOp6(TestSizeOp): + def config(self): + self.shape = [] self.dtype = np.int64 From bcdd990db067edbf5c9496f79a86322adc9156a7 Mon Sep 17 00:00:00 2001 From: 0x45f Date: Mon, 16 Aug 2021 15:12:40 +0800 Subject: [PATCH 5/5] fix copyright, remove useless include --- paddle/fluid/operators/size_op_npu.cc | 3 --- python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-) diff --git a/paddle/fluid/operators/size_op_npu.cc b/paddle/fluid/operators/size_op_npu.cc index 023a94c995850..4e9c2ec482e92 100644 --- a/paddle/fluid/operators/size_op_npu.cc +++ b/paddle/fluid/operators/size_op_npu.cc @@ -12,9 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include -#include - #include "paddle/fluid/operators/mul_op.h" #include "paddle/fluid/operators/npu_op_runner.h" diff --git a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py index 5ade23703d72b..80721cbd66a55 100755 --- a/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_size_op_npu.py @@ -1,4 +1,4 @@ -# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved. +# Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License.