Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NPU] Support test grad of NPU ops in OpTest #31697

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
Expand Down