未验证 提交 cabfb4a7 编写于 作者: L Liu-xiandong 提交者: GitHub

[NPU] Support npu kernel for atan and atan_grad op, test=develop (#34658)

* fix npu compile error, test=develop

* [NPU] Support npu kernel for atan and atan_grad op, test=develop

* [NPU] Support npu kernel for atan and atan_grad op, test=develop
Co-authored-by: Nqili93 <qili93@qq.com>
上级 47d81b09
......@@ -527,6 +527,39 @@ class CosGradNPUKernel : public framework::OpKernel<T> {
}
};
template <typename DeviceContext, typename T>
class AtanNPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto* x = ctx.Input<Tensor>("X");
auto* out = ctx.Output<Tensor>("Out");
auto place = ctx.GetPlace();
out->mutable_data<T>(place);
const auto& runner = NpuOpRunner("Atan", {*x}, {*out}, {});
auto stream =
ctx.template device_context<paddle::platform::NPUDeviceContext>()
.stream();
runner.Run(stream);
}
};
template <typename DeviceContext, typename T>
class AtanGradNPUKernel : public framework::OpKernel<T> {
public:
void Compute(const framework::ExecutionContext& ctx) const override {
auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
auto* x = ctx.Input<Tensor>("X");
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
auto place = ctx.GetPlace();
dx->mutable_data<T>(place);
auto stream =
ctx.template device_context<paddle::platform::NPUDeviceContext>()
.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<paddle::platform::NPUDeviceContext, float>,
ops::CosGradNPUKernel<paddle::platform::NPUDeviceContext,
paddle::platform::float16>);
REGISTER_OP_NPU_KERNEL(
atan, ops::AtanNPUKernel<paddle::platform::NPUDeviceContext, float>,
ops::AtanNPUKernel<paddle::platform::NPUDeviceContext,
paddle::platform::float16>);
REGISTER_OP_NPU_KERNEL(
atan_grad,
ops::AtanGradNPUKernel<paddle::platform::NPUDeviceContext, float>,
ops::AtanGradNPUKernel<paddle::platform::NPUDeviceContext,
paddle::platform::float16>);
# 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()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册