elementwise_mul_op_npu.cc 5.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2021 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. */

#include "paddle/fluid/operators/elementwise/elementwise_mul_op.h"
16
#include "paddle/fluid/operators/elementwise/elementwise_npu.h"
17 18 19 20 21 22
#include "paddle/fluid/operators/npu_op_runner.h"

namespace paddle {
namespace operators {

using Tensor = framework::Tensor;
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
using NPUDeviceContext = platform::NPUDeviceContext;

template <typename T>
static void ReduceDims(const framework::ExecutionContext& ctx,
                       const aclrtStream& stream, const int axis,
                       const framework::DDim& ddims,
                       const framework::DDim& brd_ddims, const Tensor& in,
                       Tensor* out) {
  std::vector<int64_t> axes;
  int64_t brd_size = brd_ddims.size();
  int64_t org_size = ddims.size();
  // int64_t diff = brd_dims.size() - dims.size();
  for (int64_t i = 0; i < brd_size; ++i) {
    if (i < axis || i >= org_size + axis) {
      axes.push_back(i);
      continue;
    }
    if (brd_ddims[i] > ddims[i - axis]) {
      axes.push_back(i);
    }
  }
  // LOG(INFO) << "axes = " << framework::make_ddim(axes).to_str();
  out->mutable_data<T>(ctx.GetPlace());
  const auto& runner = NpuOpRunner("ReduceSumD", {in}, {*out},
                                   {{"axes", axes}, {"keep_dims", false}});
  runner.Run(stream);
}

template <typename T>
52 53 54
class ElementwiseMulNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
55
    auto& dev_ctx = ctx.template device_context<NPUDeviceContext>();
56 57 58
    auto* x = ctx.Input<Tensor>("X");
    auto* y = ctx.Input<Tensor>("Y");
    auto* out = ctx.Output<Tensor>("Out");
59 60 61 62 63 64 65 66 67 68 69 70 71
    out->mutable_data<T>(ctx.GetPlace());

    int axis = ctx.Attr<int>("axis");

    bool direct_compute = false;
    auto x_dims = x->dims();
    auto y_dims = y->dims();
    axis = (axis == -1 ? std::abs(x_dims.size() - y_dims.size()) : axis);
    if (x_dims.size() >= y_dims.size()) {
      direct_compute = x_dims.size() == (y_dims.size() + axis);
    } else {
      direct_compute = y_dims.size() == (x_dims.size() + axis);
    }
72

73
    auto stream = ctx.template device_context<NPUDeviceContext>().stream();
74

75 76 77 78 79 80 81 82 83
    if (direct_compute) {
      const auto& runner = NpuOpRunner("Mul", {*x, *y}, {*out}, {});
      runner.Run(stream);
    } else {
      Tensor trans_x, trans_y;
      NpuElementWiseOpBroadcast<T>(dev_ctx, x, y, axis, &trans_x, &trans_y);
      const auto& runner = NpuOpRunner("Mul", {trans_x, trans_y}, {*out}, {});
      runner.Run(stream);
    }
84 85 86
  }
};

87
template <typename T>
88 89 90
class ElementwiseMulGradNPUKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
91
    auto& dev_ctx = ctx.template device_context<NPUDeviceContext>();
92 93 94 95 96
    auto* x = ctx.Input<Tensor>("X");
    auto* y = ctx.Input<Tensor>("Y");
    auto* dout = ctx.Input<Tensor>(framework::GradVarName("Out"));
    auto* dx = ctx.Output<Tensor>(framework::GradVarName("X"));
    auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y"));
97
    int axis = ctx.Attr<int>("axis");
98

99 100
    axis = (axis == -1 ? std::abs(x->dims().size() - y->dims().size()) : axis);
    auto stream = ctx.template device_context<NPUDeviceContext>().stream();
101

102 103
    Tensor trans_x, trans_y;
    NpuElementWiseOpBroadcast<T>(dev_ctx, x, y, axis, &trans_x, &trans_y);
104 105

    if (dx) {
106 107 108 109 110 111 112 113 114 115 116 117 118 119
      if (dx->dims() == dout->dims()) {
        dx->mutable_data<T>(ctx.GetPlace());
        const auto& runner_dx = NpuOpRunner("Mul", {*dout, trans_y}, {*dx}, {});
        runner_dx.Run(stream);
      } else {
        Tensor dx_temp(x->type());
        dx_temp.Resize(trans_x.dims());
        dx_temp.mutable_data<T>(ctx.GetPlace());
        const auto& runner_dx =
            NpuOpRunner("Mul", {*dout, trans_y}, {dx_temp}, {});
        runner_dx.Run(stream);
        ReduceDims<T>(ctx, stream, axis, dx->dims(), trans_x.dims(), dx_temp,
                      dx);
      }
120 121
    }
    if (dy) {
122 123 124 125 126 127 128 129 130 131 132 133 134 135
      if (dy->dims() == dout->dims()) {
        dy->mutable_data<T>(ctx.GetPlace());
        const auto& runner_dy = NpuOpRunner("Mul", {trans_x, *dout}, {*dy}, {});
        runner_dy.Run(stream);
      } else {
        Tensor dy_temp(y->type());
        dy_temp.Resize(trans_y.dims());
        dy_temp.mutable_data<T>(ctx.GetPlace());
        const auto& runner_dy =
            NpuOpRunner("Mul", {trans_x, *dout}, {dy_temp}, {});
        runner_dy.Run(stream);
        ReduceDims<T>(ctx, stream, axis, dy->dims(), trans_y.dims(), dy_temp,
                      dy);
      }
136 137 138 139 140 141 142 143 144
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

145 146
REGISTER_OP_NPU_KERNEL(elementwise_mul, ops::ElementwiseMulNPUKernel<float>,
                       ops::ElementwiseMulNPUKernel<paddle::platform::float16>);
147 148

REGISTER_OP_NPU_KERNEL(
149 150
    elementwise_mul_grad, ops::ElementwiseMulGradNPUKernel<float>,
    ops::ElementwiseMulGradNPUKernel<paddle::platform::float16>);