elementwise_add_op_xpu.cc 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
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_XPU
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include <memory>
#include <string>
#include "paddle/fluid/operators/elementwise/elementwise_op.h"

#include "paddle/fluid/operators/elementwise/elementwise_xpu.h"
22
#include "paddle/fluid/platform/device/device_wrapper.h"
23 24 25 26

namespace paddle {
namespace operators {

27
template <typename T>
28
class ElementwiseAddXPUKernel : public framework::OpKernel<T> {
29 30
  using XPUType = typename XPUTypeTrait<T>::Type;

31
 public:
32
  void Compute(const framework::ExecutionContext& ctx) const override {
33 34 35
    XPUElementwise<T, XPUType>(ctx, xpu::broadcast_add<XPUType>);
  }
};
36

37
template <typename T>
38
class ElementwiseAddGradXPUKernel : public ElemwiseGradKernel<T> {
39 40
  using XPUType = typename XPUTypeTrait<T>::Type;

41
 public:
42
  void Compute(const framework::ExecutionContext& ctx) const override {
43
    ElemwiseGradKernel<T>::Compute(ctx);
44 45 46 47
    auto* x = ctx.Input<framework::Tensor>("X");
    auto* dz = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
    auto* dy = ctx.Output<framework::Tensor>(framework::GradVarName("Y"));
48 49 50 51
    const framework::DDim& dz_dims = dz->dims();
    int axis = ctx.Attr<int>("axis");

    const T* dz_data = dz->data<T>();
52 53
    auto& dev_ctx =
        ctx.template device_context<paddle::platform::XPUDeviceContext>();
54

55
    if (dx != nullptr) {
56
      T* dx_data = dx->mutable_data<T>(ctx.GetPlace());
57
      if (dx->dims() == dz_dims) {
58 59 60 61 62
        if (dx_data != dz_data) {
          framework::TensorCopy(
              *dz, ctx.GetPlace(),
              ctx.template device_context<platform::DeviceContext>(), dx);
        }
63
      } else {
64 65 66 67 68 69
        // For inplace strategy, dx will be stored in addr of dz, which makes
        // the result of dy wrong.
        if (dx->IsSharedBufferWith(*dz)) {
          dx->clear();
          dx->mutable_data<T>(x->dims(), ctx.GetPlace());
        }
70
        std::vector<int> reduce_dims = GetReduceDim(dx->dims(), dz_dims, axis);
71
        std::vector<int> dz_vector = phi::vectorize<int>(dz_dims);
72

73 74
        int ret = xpu::reduce_sum<XPUType>(
            dev_ctx.x_context(), reinterpret_cast<const XPUType*>(dz_data),
75 76
            reinterpret_cast<XPUType*>(dx_data), dz_vector, reduce_dims);
        PADDLE_ENFORCE_XDNN_SUCCESS(ret, "reduce_sum");
77 78 79 80
      }
    }

    if (dy != nullptr) {
81
      T* dy_data = dy->mutable_data<T>(ctx.GetPlace());
82
      if (dy->dims() == dz_dims) {
83 84 85 86 87
        if (dy_data != dz_data) {
          framework::TensorCopy(
              *dz, ctx.GetPlace(),
              ctx.template device_context<platform::DeviceContext>(), dy);
        }
88
      } else {
89
        std::vector<int> reduce_dims = GetReduceDim(dy->dims(), dz_dims, axis);
90
        std::vector<int> dz_vector = phi::vectorize<int>(dz_dims);
91 92
        int ret = xpu::reduce_sum<XPUType>(
            dev_ctx.x_context(), reinterpret_cast<const XPUType*>(dz_data),
93 94
            reinterpret_cast<XPUType*>(dy_data), dz_vector, reduce_dims);
        PADDLE_ENFORCE_XDNN_SUCCESS(ret, "reduce_sum");
95
      }
96
    }
97 98 99 100 101 102 103 104
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

105 106
REGISTER_OP_XPU_KERNEL(elementwise_add, ops::ElementwiseAddXPUKernel<float>,
                       ops::ElementwiseAddXPUKernel<paddle::platform::float16>);
107
REGISTER_OP_XPU_KERNEL(
108 109
    elementwise_add_grad, ops::ElementwiseAddGradXPUKernel<float>,
    ops::ElementwiseAddGradXPUKernel<paddle::platform::float16>);
110
#endif