diff --git a/paddle/fluid/operators/activation_op_npu.cc b/paddle/fluid/operators/activation_op_npu.cc index 5988b4e2fee69d9d510fcc2c2878826f31265f09..02ce817bcc8b2bf99a3e6bf11dea444b21b6529a 100644 --- a/paddle/fluid/operators/activation_op_npu.cc +++ b/paddle/fluid/operators/activation_op_npu.cc @@ -527,6 +527,39 @@ class CosGradNPUKernel : public framework::OpKernel { } }; +template +class AtanNPUKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + auto* x = ctx.Input("X"); + auto* out = ctx.Output("Out"); + auto place = ctx.GetPlace(); + out->mutable_data(place); + const auto& runner = NpuOpRunner("Atan", {*x}, {*out}, {}); + auto stream = + ctx.template device_context() + .stream(); + runner.Run(stream); + } +}; + +template +class AtanGradNPUKernel : public framework::OpKernel { + public: + void Compute(const framework::ExecutionContext& ctx) const override { + auto* dout = ctx.Input(framework::GradVarName("Out")); + auto* x = ctx.Input("X"); + auto* dx = ctx.Output(framework::GradVarName("X")); + auto place = ctx.GetPlace(); + dx->mutable_data(place); + auto stream = + ctx.template device_context() + .stream(); + const auto& runner_dx = NpuOpRunner("AtanGrad", {*x, *dout}, {*dx}, {}); + runner_dx.Run(stream); + } +}; + } // namespace operators } // namespace paddle @@ -648,3 +681,14 @@ REGISTER_OP_NPU_KERNEL( cos_grad, ops::CosGradNPUKernel, ops::CosGradNPUKernel); + +REGISTER_OP_NPU_KERNEL( + atan, ops::AtanNPUKernel, + ops::AtanNPUKernel); + +REGISTER_OP_NPU_KERNEL( + atan_grad, + ops::AtanGradNPUKernel, + ops::AtanGradNPUKernel); diff --git a/python/paddle/fluid/tests/unittests/npu/test_atan_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_atan_op_npu.py new file mode 100644 index 0000000000000000000000000000000000000000..a18b8a03075ef8c05bd90852ff006b95f8438b1f --- /dev/null +++ b/python/paddle/fluid/tests/unittests/npu/test_atan_op_npu.py @@ -0,0 +1,87 @@ +# 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 numpy as np +import unittest +import sys +sys.path.append("..") +from op_test import OpTest +import paddle +import paddle.fluid as fluid + +paddle.enable_static() +SEED = 1024 + + +class TestAtan(OpTest): + def setUp(self): + self.set_npu() + self.op_type = "atan" + self.place = paddle.NPUPlace(0) + + self.dtype = np.float32 + np.random.seed(SEED) + self.shape = [11, 17] + x = np.random.uniform(0.1, 1, self.shape).astype(self.dtype) + out = np.arctan(x) + + self.inputs = {'X': OpTest.np_dtype_to_fluid_dtype(x)} + self.outputs = {'Out': out} + + def set_attrs(self): + pass + + def set_npu(self): + self.__class__.use_npu = True + + def test_check_grad(self): + self.check_grad_with_place(self.place, ['X'], 'Out') + + def test_out_name(self): + with fluid.program_guard(fluid.Program()): + np_x = np.array([0.1]) + data = fluid.layers.data(name="X", shape=[1]) + out = paddle.atan(data, name='Y') + place = paddle.NPUPlace(0) + exe = fluid.Executor(place) + result, = exe.run(feed={"X": np_x}, fetch_list=[out]) + expected = np.arctan(np_x) + self.assertEqual(result, expected) + + def test_dygraph(self): + with fluid.dygraph.guard(paddle.NPUPlace(0)): + np_x = np.array([0.1]) + x = fluid.dygraph.to_variable(np_x) + z = paddle.atan(x).numpy() + z_expected = np.arctan(np_x) + self.assertEqual(z, z_expected) + + def test_check_output(self): + self.check_output_with_place(self.place) + + +class TestAtanShape(TestAtan): + def set_attrs(self): + self.shape = [12, 23, 10] + + +class TestAtanFloat16(TestAtan): + def set_attrs(self): + self.dtype = np.float16 + + +if __name__ == '__main__': + unittest.main()