From 444c2852021291614c7a808c6ced135b37295299 Mon Sep 17 00:00:00 2001 From: pangyoki Date: Tue, 16 Mar 2021 19:44:47 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90NPU=E3=80=91Add=20TensorCopy=20to=20NP?= =?UTF-8?q?U=20kernel=20for=20reduce=5Fsum=20op=20=20(#31667)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * update unittest * add TensorCopy in npu grad kernel --- paddle/fluid/operators/reduce_ops/reduce_sum_op_npu.cc | 5 +++++ .../fluid/tests/unittests/npu/test_reduce_sum_op_npu.py | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/paddle/fluid/operators/reduce_ops/reduce_sum_op_npu.cc b/paddle/fluid/operators/reduce_ops/reduce_sum_op_npu.cc index 505e48054e7..e2cd7ca353c 100644 --- a/paddle/fluid/operators/reduce_ops/reduce_sum_op_npu.cc +++ b/paddle/fluid/operators/reduce_ops/reduce_sum_op_npu.cc @@ -83,6 +83,11 @@ class ReduceSumGradNPUKernel : public framework::OpKernel { Tensor out_grad_tmp(out_grad->type()); out_grad_tmp.Resize(out_dims); out_grad_tmp.mutable_data(ctx.GetPlace()); + framework::TensorCopy( + *out_grad, ctx.GetPlace(), + ctx.template device_context(), + &out_grad_tmp); + out_grad_tmp.Resize(out_dims); auto runner = NpuOpRunner("BroadcastToD", {out_grad_tmp}, {*x_grad}, {{"shape", framework::vectorize(x->dims())}}); diff --git a/python/paddle/fluid/tests/unittests/npu/test_reduce_sum_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_reduce_sum_op_npu.py index caae0507c26..dea5141a024 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_reduce_sum_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_reduce_sum_op_npu.py @@ -102,7 +102,9 @@ class TestReduceSumNet(unittest.TestCase): label = paddle.static.data( name="label", shape=[2, 1], dtype='int64') - z = paddle.add(a, b) + a_1 = fluid.layers.fc(input=a, size=4, num_flatten_dims=2, act=None) + b_1 = fluid.layers.fc(input=b, size=4, num_flatten_dims=2, act=None) + z = paddle.add(a_1, b_1) z_1 = self.set_reduce_sum_function(z) prediction = fluid.layers.fc(input=z_1, size=2, act='softmax') -- GitLab