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】Fix elementwise_div_grad op #31753

Merged
merged 2 commits into from
Mar 20, 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
10 changes: 8 additions & 2 deletions paddle/fluid/operators/elementwise/elementwise_div_op_npu.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,15 @@ class ElementwiseDivGradNPUKernel : public framework::OpKernel<T> {
if (dy) {
dy->mutable_data<T>(place);

Tensor y_grad_w(x->type());
Tensor neg_out(y->type());
neg_out.mutable_data<T>(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<T>(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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,24 @@ def init_dtype(self):
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

max_relative_error = 0.005 是否能过测试?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

精度只能到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(),
Expand Down Expand Up @@ -123,7 +135,7 @@ def _test(self, run_npu=True):
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')
Expand Down