From 4f0c3278ff031f3c59878c8f3c03b86e79917986 Mon Sep 17 00:00:00 2001 From: ronnywang <524019753@qq.com> Date: Wed, 22 Sep 2021 19:27:39 +0800 Subject: [PATCH] [NPU] add randperm_op_npu (#35763) * add randperm_op_npu * fix test_set_value_op_npu --- paddle/fluid/operators/randperm_op_npu.cc | 23 ++++ paddle/fluid/operators/utils.h | 5 + .../unittests/npu/test_randperm_op_npu.py | 122 ++++++++++++++++++ .../unittests/npu/test_set_value_op_npu.py | 2 +- 4 files changed, 151 insertions(+), 1 deletion(-) create mode 100644 paddle/fluid/operators/randperm_op_npu.cc create mode 100644 python/paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py diff --git a/paddle/fluid/operators/randperm_op_npu.cc b/paddle/fluid/operators/randperm_op_npu.cc new file mode 100644 index 00000000000..a16c0d905a5 --- /dev/null +++ b/paddle/fluid/operators/randperm_op_npu.cc @@ -0,0 +1,23 @@ +/* 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 "paddle/fluid/operators/randperm_op.h" +#include "paddle/fluid/framework/op_registry.h" + +template +using kernel = + paddle::operators::RandpermKernel; + +REGISTER_OP_NPU_KERNEL(randperm, kernel, kernel, kernel, + kernel); diff --git a/paddle/fluid/operators/utils.h b/paddle/fluid/operators/utils.h index 912d538d5e9..770369e64f4 100644 --- a/paddle/fluid/operators/utils.h +++ b/paddle/fluid/operators/utils.h @@ -114,6 +114,11 @@ inline T GetValue(const framework::Tensor* x) { if (!platform::is_cpu_place(x->place())) { framework::Tensor cpu_x; framework::TensorCopy(*x, platform::CPUPlace(), &cpu_x); +#ifdef PADDLE_WITH_ASCEND_CL + platform::DeviceContextPool& pool = platform::DeviceContextPool::Instance(); + const platform::DeviceContext* dev_ctx = pool.Get(x->place()); + dev_ctx->Wait(); +#endif value = cpu_x.data()[0]; } else { value = x->data()[0]; diff --git a/python/paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py new file mode 100644 index 00000000000..4ec353c55de --- /dev/null +++ b/python/paddle/fluid/tests/unittests/npu/test_randperm_op_npu.py @@ -0,0 +1,122 @@ +# 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. + +from __future__ import print_function + +import unittest +import numpy as np +import sys +sys.path.append("..") +from op_test import OpTest +import paddle +import paddle.fluid.core as core +from paddle.static import program_guard, Program +from test_randperm_op import check_randperm_out, error_msg, convert_dtype + +paddle.enable_static() + + +class TestRandpermOp(OpTest): + """ Test randperm op.""" + + def setUp(self): + self.set_npu() + self.op_type = "randperm" + self.n = 200 + self.dtype = "int64" + + self.inputs = {} + self.outputs = {"Out": np.zeros((self.n)).astype(self.dtype)} + self.init_attrs() + self.attrs = { + "n": self.n, + "dtype": convert_dtype(self.dtype), + } + + def set_npu(self): + self.__class__.use_npu = True + + def _get_places(self): + return [paddle.NPUPlace(0)] + + def init_attrs(self): + pass + + def test_check_output(self): + self.check_output_customized(self.verify_output) + + def verify_output(self, outs): + out_np = np.array(outs[0]) + self.assertTrue( + check_randperm_out(self.n, out_np), msg=error_msg(out_np)) + + +class TestRandpermOpN(TestRandpermOp): + def init_attrs(self): + self.n = 10000 + + +class TestRandpermOpInt32(TestRandpermOp): + def init_attrs(self): + self.dtype = "int32" + + +class TestRandpermOpFloat32(TestRandpermOp): + def init_attrs(self): + self.dtype = "float32" + + +class TestRandpermOpFloat64(TestRandpermOp): + def init_attrs(self): + self.dtype = "float64" + + +class TestRandpermOpError(unittest.TestCase): + def test_errors(self): + with program_guard(Program(), Program()): + self.assertRaises(ValueError, paddle.randperm, -3) + self.assertRaises(TypeError, paddle.randperm, 10, 'int8') + + +class TestRandpermAPI(unittest.TestCase): + def test_out(self): + n = 10 + place = paddle.NPUPlace(0) + with program_guard(Program(), Program()): + x1 = paddle.randperm(n) + x2 = paddle.randperm(n, 'float32') + + exe = paddle.static.Executor(place) + res = exe.run(fetch_list=[x1, x2]) + + self.assertEqual(res[0].dtype, np.int64) + self.assertEqual(res[1].dtype, np.float32) + self.assertTrue(check_randperm_out(n, res[0])) + self.assertTrue(check_randperm_out(n, res[1])) + + +class TestRandpermImperative(unittest.TestCase): + def test_out(self): + paddle.disable_static(paddle.NPUPlace(0)) + n = 10 + for dtype in ['int32', np.int64, 'float32', 'float64']: + data_p = paddle.randperm(n, dtype) + data_np = data_p.numpy() + self.assertTrue( + check_randperm_out(n, data_np), msg=error_msg(data_np)) + paddle.enable_static() + + +if __name__ == "__main__": + unittest.main() diff --git a/python/paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py index e02a9dc446a..e819f422f2b 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_set_value_op_npu.py @@ -68,7 +68,7 @@ class TestSetValueApi(TestSetValueBase): return out def _run_dynamic(self): - paddle.disable_static() + paddle.disable_static(paddle.NPUPlace(0)) x = paddle.ones(shape=self.shape, dtype=self.dtype) self._call_setitem(x) out = x.numpy() -- GitLab