From 5a622c29064e858eb72884aafeb75a7bd5e041a1 Mon Sep 17 00:00:00 2001 From: Tomasz Patejko Date: Mon, 21 May 2018 07:36:10 +0200 Subject: [PATCH] MKL elementwise add backward: Initial implementation with vector copy --- paddle/fluid/operators/elementwise_add_op.cc | 6 +++--- paddle/fluid/operators/elementwise_add_op.h | 19 ++++++++++++++++--- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/paddle/fluid/operators/elementwise_add_op.cc b/paddle/fluid/operators/elementwise_add_op.cc index d2c2053713..c1ddc1824b 100644 --- a/paddle/fluid/operators/elementwise_add_op.cc +++ b/paddle/fluid/operators/elementwise_add_op.cc @@ -25,6 +25,6 @@ REGISTER_OP_CPU_KERNEL( REGISTER_OP_CPU_KERNEL( elementwise_add_grad, ops::ElementwiseAddGradKernel, - ops::ElementwiseAddGradKernel, - ops::ElementwiseAddGradKernel, - ops::ElementwiseAddGradKernel); + ops::ElementwiseAddGradKernel); +// ops::ElementwiseAddGradKernel, +// ops::ElementwiseAddGradKernel); diff --git a/paddle/fluid/operators/elementwise_add_op.h b/paddle/fluid/operators/elementwise_add_op.h index d75d86c242..5984f4aef1 100644 --- a/paddle/fluid/operators/elementwise_add_op.h +++ b/paddle/fluid/operators/elementwise_add_op.h @@ -98,9 +98,22 @@ class ElementwiseAddGradKernel : public framework::OpKernel { auto* dx = ctx.Output(framework::GradVarName("X")); auto* dy = ctx.Output(framework::GradVarName("Y")); int axis = ctx.Attr("axis"); - ElemwiseGradCompute, IdentityGrad>( - ctx, *x, *y, *out, *dout, axis, dx, dy, IdentityGrad(), - IdentityGrad()); + + if (platform::is_cpu_place(ctx.GetPlace()) && (x->dims() == y->dims())) { + auto blas = math::GetBlas(ctx); + + if (dx) + dx->mutable_data(ctx.GetPlace()); + if (dy) + dy->mutable_data(ctx.GetPlace()); + + blas.VCOPY(dout->numel(), dout->data(), dx->data()); + blas.VCOPY(dout->numel(), dout->data(), dy->data()); + } else { + ElemwiseGradCompute, IdentityGrad>( + ctx, *x, *y, *out, *dout, axis, dx, dy, IdentityGrad(), + IdentityGrad()); + } } }; -- GitLab