diff --git a/paddle/fluid/operators/elementwise/elementwise_mul_op_npu.cc b/paddle/fluid/operators/elementwise/elementwise_mul_op_npu.cc index cecb62d3580f9e84afa965424c6e8af20cda5dc5..08df6d4e27af0a79123f26ad2064ee0203cc1b28 100644 --- a/paddle/fluid/operators/elementwise/elementwise_mul_op_npu.cc +++ b/paddle/fluid/operators/elementwise/elementwise_mul_op_npu.cc @@ -12,6 +12,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ +#ifdef PADDLE_WITH_ASCEND_CL #include #include @@ -58,17 +59,21 @@ class ElementwiseMulGradNPUKernel : public framework::OpKernel { auto place = ctx.GetPlace(); - dx->mutable_data(place); - dy->mutable_data(place); - auto stream = ctx.template device_context() .stream(); - auto dx_runner = NpuOpRunner("Mul", {*dout, *y}, {*dx}, {}); - dx_runner.Run(stream); - auto dy_runner = NpuOpRunner("Mul", {*x, *dout}, {*dy}, {}); - dy_runner.Run(stream); + if (dx) { + dx->mutable_data(place); + auto dx_runner = NpuOpRunner("Mul", {*dout, *y}, {*dx}, {}); + dx_runner.Run(stream); + } + + if (dy) { + dy->mutable_data(place); + auto dy_runner = NpuOpRunner("Mul", {*x, *dout}, {*dy}, {}); + dy_runner.Run(stream); + } } }; @@ -88,3 +93,4 @@ REGISTER_OP_NPU_KERNEL( ops::ElementwiseMulGradNPUKernel, ops::ElementwiseMulGradNPUKernel); +#endif diff --git a/python/paddle/fluid/tests/unittests/npu/test_elementwise_mul_op_npu.py b/python/paddle/fluid/tests/unittests/npu/test_elementwise_mul_op_npu.py index d0baa5877f224bbc8c0e505457b004918aaf6448..9bfb7e033e7ea454223c683877bb30f02506be75 100644 --- a/python/paddle/fluid/tests/unittests/npu/test_elementwise_mul_op_npu.py +++ b/python/paddle/fluid/tests/unittests/npu/test_elementwise_mul_op_npu.py @@ -122,6 +122,7 @@ class TestElementwiseMulNet(unittest.TestCase): e = paddle.multiply(a, b) f = paddle.multiply(c, d) + f.stop_gradient = True g = paddle.multiply(e, f) fc_1 = fluid.layers.fc(input=g, size=128)