未验证 提交 d55120d7 编写于 作者: L liym27 提交者: GitHub

[NPU] Support testing grad of NPU ops in OpTest (#31697)

上级 e4247120
...@@ -64,28 +64,28 @@ class TestElementwiseAddOp(OpTest): ...@@ -64,28 +64,28 @@ class TestElementwiseAddOp(OpTest):
def test_check_output(self): def test_check_output(self):
self.check_output_with_place(self.place, check_dygraph=False) 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):
# def test_check_grad_normal(self): self.check_grad_with_place(
# self.check_grad_with_place( self.place, ['X', 'Y'],
# self.place, ['X', 'Y'], 'Out',
# 'Out', max_relative_error=0.006,
# max_relative_error=0.006, check_dygraph=False)
# check_dygraph=False)
# def test_check_grad_ingore_x(self):
# def test_check_grad_ingore_x(self): self.check_grad_with_place(
# self.check_grad_with_place( self.place, ['Y'],
# self.place, ['Y'], 'Out',
# 'Out', no_grad_set=set("X"),
# no_grad_set=set("X"), max_relative_error=0.006,
# max_relative_error=0.006, check_dygraph=False)
# check_dygraph=False)
# def test_check_grad_ingore_y(self):
# def test_check_grad_ingore_y(self): self.check_grad_with_place(
# self.check_grad_with_place( self.place, ['X'],
# self.place, ['X'], 'Out',
# 'Out', no_grad_set=set("Y"),
# no_grad_set=set("Y"), max_relative_error=0.006,
# max_relative_error=0.006,check_dygraph=False) check_dygraph=False)
@unittest.skipIf(not paddle.is_compiled_with_npu(), @unittest.skipIf(not paddle.is_compiled_with_npu(),
...@@ -133,10 +133,6 @@ class TestAddAPI(unittest.TestCase): ...@@ -133,10 +133,6 @@ class TestAddAPI(unittest.TestCase):
True, True,
msg="z_value = {}, but expected {}".format(z_value, z_expected)) 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(), @unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU") "core is not compiled with NPU")
......
...@@ -52,12 +52,9 @@ class TestPow(OpTest): ...@@ -52,12 +52,9 @@ class TestPow(OpTest):
def test_check_output(self): def test_check_output(self):
self.check_output_with_place(self.place, check_dygraph=False) self.check_output_with_place(self.place, check_dygraph=False)
# TODO(ascendrc): Add grad test def test_check_grad(self):
# def test_check_grad(self): self.check_grad_with_place(
# if self.dtype == np.float16: self.place, ['X'], 'Out', check_dygraph=False)
# return
# self.check_grad(['X'], 'Out')
#
@unittest.skipIf(not paddle.is_compiled_with_npu(), @unittest.skipIf(not paddle.is_compiled_with_npu(),
......
...@@ -62,6 +62,10 @@ class TestSliceOp(OpTest): ...@@ -62,6 +62,10 @@ class TestSliceOp(OpTest):
def test_check_output(self): def test_check_output(self):
self.check_output_with_place(self.place, check_dygraph=False) 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(), @unittest.skipIf(not paddle.is_compiled_with_npu(),
"core is not compiled with NPU") "core is not compiled with NPU")
......
...@@ -1416,9 +1416,17 @@ class OpTest(unittest.TestCase): ...@@ -1416,9 +1416,17 @@ class OpTest(unittest.TestCase):
if not type(output_names) is list: if not type(output_names) is list:
output_names = [output_names] 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 [ numeric_grads = user_defined_grads or [
get_numeric_gradient( get_numeric_gradient(
place, numeric_place,
self.scope, self.scope,
self.op, self.op,
self.inputs, self.inputs,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册