Skip to content

Commit

Permalink
[NPU] Support testing grad of NPU ops in OpTest (PaddlePaddle#31697)
Browse files Browse the repository at this point in the history
  • Loading branch information
liym27 authored and frankwhzhang committed Apr 12, 2021
1 parent 993235b commit af99906
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 3 additions & 6 deletions python/paddle/fluid/tests/unittests/npu/test_pow_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions python/paddle/fluid/tests/unittests/npu/test_slice_op_npu.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
10 changes: 9 additions & 1 deletion python/paddle/fluid/tests/unittests/op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1449,9 +1449,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,
Expand Down

0 comments on commit af99906

Please sign in to comment.