From 02912ce7f2d06e6bd4e3df82a6182652452bd0bd Mon Sep 17 00:00:00 2001 From: oyjxer <1728722986@qq.com> Date: Sat, 20 Mar 2021 11:10:07 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90NPU=E3=80=91Fix=20npu=20kernel=20eleme?= =?UTF-8?q?ntwise=5Fdiv=5Fgrad=20=20(#31753)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../elementwise/elementwise_div_op_npu.cc | 10 +++++-- .../npu/test_elementwise_div_op_npu.py | 26 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc b/paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc index 268d17ca924..db44df1e6fd 100644 --- a/paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc +++ b/paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc @@ -110,9 +110,15 @@ class ElementwiseDivGradNPUKernel : public framework::OpKernel { if (dy) { dy->mutable_data(place); - Tensor y_grad_w(x->type()); + Tensor neg_out(y->type()); + neg_out.mutable_data(y->dims(), place); + auto neg_out_runner = NpuOpRunner("Neg", {*out}, + {neg_out}, {}); + neg_out_runner.Run(stream); + + Tensor y_grad_w(y->type()); y_grad_w.mutable_data(y->dims(), place); - auto y_grad_w_runner = NpuOpRunner("Mul", {*out, y_power}, + auto y_grad_w_runner = NpuOpRunner("Div", {neg_out, *y}, {y_grad_w}, {}); y_grad_w_runner.Run(stream); diff --git a/python/paddle/fluid/tests/unittests/npu/test_elementwise_div_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_elementwise_div_op_npu.py index e0aa8d277f7..0ae2678d10b 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_elementwise_div_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_elementwise_div_op_npu.py @@ -56,12 +56,24 @@ class TestElementwiseDiv(OpTest): def test_check_output(self): self.check_output_with_place(self.place, check_dygraph=False) - # TODO(ascendrc): Div grad test - # def test_check_grad(self): - # if self.dtype == np.float16: - # return - # self.check_grad(['X'], 'Out') - # + def test_check_grad_normal(self): + self.check_grad_with_place( + self.place, ['X', 'Y'], + 'Out', + max_relative_error=0.007, + check_dygraph=False) + + def test_check_grad_ingore_x(self): + self.check_grad_with_place( + self.place, ['Y'], + 'Out', + max_relative_error=0.007, + no_grad_set=set("X"), + check_dygraph=False) + + def test_check_grad_ingore_y(self): + self.check_grad_with_place( + self.place, ['X'], 'Out', no_grad_set=set("Y"), check_dygraph=False) @unittest.skipIf(not paddle.is_compiled_with_npu(), @@ -123,7 +135,7 @@ class TestElementwiseDivNet(unittest.TestCase): e = paddle.multiply(a, b) f = paddle.multiply(c, d) f.stop_gradient = True - g = paddle.divide(e, f) + g = fluid.layers.elementwise_div(e, f) fc_1 = fluid.layers.fc(input=g, size=128) prediction = fluid.layers.fc(input=fc_1, size=2, act='softmax') -- GitLab