diff --git a/paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc b/paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc index 268d17ca924d6972d8eef8e10ac14b87a15c9ae3..db44df1e6fde4daec7e827b0c1a30e17c016af7b 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 e0aa8d277f7d8d264fa5cc8a749a4a19b3f409c3..0ae2678d10b47c8998882e3ee00d177e86236a06 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')