From d55120d77f0ed541ef092d1fce95f8ad0a6bb3e8 Mon Sep 17 00:00:00 2001 From: liym27 <33742067+liym27@users.noreply.github.com> Date: Thu, 18 Mar 2021 14:39:21 +0800 Subject: [PATCH] [NPU] Support testing grad of NPU ops in OpTest (#31697) --- .../npu/test_elementwise_add_op_npu.py | 48 +++++++++---------- .../tests/unittests/npu/test_pow_op_npu.py | 9 ++-- .../tests/unittests/npu/test_slice_op_npu.py | 4 ++ .../paddle/fluid/tests/unittests/op_test.py | 10 +++- 4 files changed, 38 insertions(+), 33 deletions(-) diff --git a/python/paddle/fluid/tests/unittests/npu/test_elementwise_add_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_elementwise_add_op_npu.py index 47da4fdb23ec4..6a82157faaec4 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_elementwise_add_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_elementwise_add_op_npu.py @@ -64,28 +64,28 @@ def init_axis(self): def test_check_output(self): self.check_output_with_place(self.place, check_dygraph=False) - # TODO(ascendrc): Test grad op after it is implemented. - # def test_check_grad_normal(self): - # self.check_grad_with_place( - # self.place, ['X', 'Y'], - # 'Out', - # max_relative_error=0.006, - # check_dygraph=False) - # - # def test_check_grad_ingore_x(self): - # self.check_grad_with_place( - # self.place, ['Y'], - # 'Out', - # no_grad_set=set("X"), - # max_relative_error=0.006, - # check_dygraph=False) - # - # def test_check_grad_ingore_y(self): - # self.check_grad_with_place( - # self.place, ['X'], - # 'Out', - # no_grad_set=set("Y"), - # max_relative_error=0.006,check_dygraph=False) + def test_check_grad_normal(self): + self.check_grad_with_place( + self.place, ['X', 'Y'], + 'Out', + max_relative_error=0.006, + check_dygraph=False) + + def test_check_grad_ingore_x(self): + self.check_grad_with_place( + self.place, ['Y'], + 'Out', + no_grad_set=set("X"), + max_relative_error=0.006, + check_dygraph=False) + + def test_check_grad_ingore_y(self): + self.check_grad_with_place( + self.place, ['X'], + 'Out', + no_grad_set=set("Y"), + max_relative_error=0.006, + check_dygraph=False) @unittest.skipIf(not paddle.is_compiled_with_npu(), @@ -133,10 +133,6 @@ def test_static(self): True, msg="z_value = {}, but expected {}".format(z_value, z_expected)) - def test_backward(self): - # TODO(ascendrc): Test backward after add grad npu op implemented. - pass - @unittest.skipIf(not paddle.is_compiled_with_npu(), "core is not compiled with NPU") diff --git a/python/paddle/fluid/tests/unittests/npu/test_pow_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_pow_op_npu.py index add2a1932b1aa..8c67766b31184 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_pow_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_pow_op_npu.py @@ -52,12 +52,9 @@ def init_dtype(self): def test_check_output(self): self.check_output_with_place(self.place, check_dygraph=False) - # TODO(ascendrc): Add grad test - # def test_check_grad(self): - # if self.dtype == np.float16: - # return - # self.check_grad(['X'], 'Out') - # + def test_check_grad(self): + self.check_grad_with_place( + self.place, ['X'], 'Out', check_dygraph=False) @unittest.skipIf(not paddle.is_compiled_with_npu(), diff --git a/python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py index 3e8f424129a0b..1e30bb0078213 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py @@ -62,6 +62,10 @@ def set_npu(self): def test_check_output(self): self.check_output_with_place(self.place, check_dygraph=False) + def test_check_grad_normal(self): + self.check_grad_with_place( + self.place, ['Input'], 'Out', check_dygraph=False) + @unittest.skipIf(not paddle.is_compiled_with_npu(), "core is not compiled with NPU") diff --git a/python/paddle/fluid/tests/unittests/op_test.py b/python/paddle/fluid/tests/unittests/op_test.py index efce2e770b1b1..ae8f64987898d 100644 --- a/python/paddle/fluid/tests/unittests/op_test.py +++ b/python/paddle/fluid/tests/unittests/op_test.py @@ -1416,9 +1416,17 @@ def check_grad_with_place(self, if not type(output_names) is list: output_names = [output_names] + # FIXME: Replace numeric_place with place to calculate numeric_grads. + # NOTE(liym27): There is an unknown error when call op.run() on NPUPlace, which + # needs to be fixed. + if self.__class__.use_npu == True: + numeric_place = paddle.CPUPlace() + else: + numeric_place = place + numeric_grads = user_defined_grads or [ get_numeric_gradient( - place, + numeric_place, self.scope, self.op, self.inputs,