elementwise_xpu.h 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* 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. */
#pragma once
#ifdef PADDLE_WITH_XPU
T
taixiurong 已提交
16
#include <algorithm>
17
#include <string>
T
taixiurong 已提交
18 19 20
#include <tuple>
#include <utility>
#include <vector>
21 22
#include "paddle/fluid/framework/tensor.h"
#include "paddle/fluid/platform/place.h"
T
taixiurong 已提交
23
#include "xpu/refactor/math.h"
24

25 26 27
namespace paddle {
namespace operators {

T
taixiurong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
static std::pair<std::vector<int>, std::vector<int>> XPUDimsToBroadcastVector(
    const framework::DDim& x, const framework::DDim& y) {
  std::vector<int> x_v;
  std::vector<int> y_v;
  int y_size = y.size();
  for (int i = 0; i < y_size; ++i) {
    if (x[i] == y[i]) {
      x_v.push_back(y[i]);
      y_v.push_back(y[i]);
      continue;
    }
    x_v.push_back(1);
    x_v.push_back(x[i]);
    y_v.push_back(y[i] / x[i]);
    y_v.push_back(x[i]);
43
  }
T
taixiurong 已提交
44 45
  return std::make_pair(x_v, y_v);
}
46

T
taixiurong 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
static std::pair<std::vector<int>, std::vector<int>> XPUReducesAxisVector(
    const framework::DDim& x, const framework::DDim& y) {
  std::vector<int> x_vector;
  std::vector<int> axis_v;
  PADDLE_ENFORCE_GT(
      x.size(), 0, platform::errors::OutOfRange("x size is less 1, x shape is ",
                                                x.to_str()));
  PADDLE_ENFORCE_GT(
      y.size(), 0, platform::errors::OutOfRange("y size is less 1, y shape is ",
                                                y.to_str()));

  int y_nums = framework::product(y);
  x_vector = framework::vectorize<int>(x);
  if (y_nums == 1) {
    for (int i = 0; i < x.size(); ++i) {
      axis_v.push_back(i);
    }
    return std::make_pair(x_vector, axis_v);
  }
  int yidx = 0;
  for (size_t i = 0; i < x_vector.size(); ++i) {
    if (y[yidx] == 1) {
      axis_v.push_back(i);
      yidx++;
      continue;
    }
    if (x_vector[i] != y[yidx]) {
      axis_v.push_back(i);
      continue;
    }
    yidx++;
78
  }
T
taixiurong 已提交
79 80
  return std::make_pair(x_vector, axis_v);
}
81

T
taixiurong 已提交
82 83 84 85
template <typename T>
void XPUElementwise(
    const framework::ExecutionContext& ctx,
    std::function<int(xpu::Context*, const T*, const T*, T*, int)> func) {
86
  auto x_var = ctx.InputVar("X");
J
Jack Zhou 已提交
87 88
  PADDLE_ENFORCE_NE(x_var, nullptr, platform::errors::InvalidArgument(
                                        "Cannot get input Variable X"));
89 90 91 92
  PADDLE_ENFORCE_EQ(
      x_var->IsType<framework::LoDTensor>(), true,
      platform::errors::InvalidArgument(
          "XPU only support LoDTensor, Input(X) is not LoDTensor"));
93 94 95 96 97 98

  auto x = x_var->Get<framework::LoDTensor>();
  auto* y = ctx.Input<framework::LoDTensor>("Y");
  auto* z = ctx.Output<framework::LoDTensor>("Out");
  z->mutable_data<T>(ctx.GetPlace());
  auto x_dims = x.dims();
T
taixiurong 已提交
99 100 101 102
  auto y_dims = y->dims();
  int max_dim = std::max(x_dims.size(), y_dims.size());
  int axis = ctx.Attr<int>("axis");
  axis = (axis == -1 ? std::abs(x_dims.size() - y_dims.size()) : axis);
103

T
taixiurong 已提交
104 105 106 107 108 109 110 111 112
  PADDLE_ENFORCE_GE(
      axis, 0,
      platform::errors::InvalidArgument(
          "Axis should be great than or equal to 0, but received axis is %d.",
          axis));
  PADDLE_ENFORCE_LT(axis, max_dim,
                    platform::errors::InvalidArgument(
                        "Axis should be less than %d, but received axis is %d.",
                        max_dim, axis));
113

T
taixiurong 已提交
114 115 116 117 118 119 120
  std::vector<int> x_dims_array(max_dim);
  std::vector<int> y_dims_array(max_dim);
  std::vector<int> out_dims_array(max_dim);
  GetBroadcastDimsArrays(x_dims, y_dims, x_dims_array.data(),
                         y_dims_array.data(), out_dims_array.data(), max_dim,
                         axis);
  framework::DDim out_dim = framework::make_ddim(out_dims_array);
121 122 123 124

  const T* x_data = x.data<T>();
  const T* y_data = y->data<T>();
  T* z_data = z->data<T>();
T
taixiurong 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
  bool need_wait = false;
  framework::Tensor x_broadcast_tensor;
  framework::Tensor y_broadcast_tensor;
  auto& dev_ctx =
      ctx.template device_context<paddle::platform::XPUDeviceContext>();
  int ret = xpu::SUCCESS;
  // begin broadcast now
  if (x.numel() != z->numel()) {
    // broadcast x
    std::pair<std::vector<int>, std::vector<int>> bcast_v =
        XPUDimsToBroadcastVector(framework::make_ddim(x_dims_array), out_dim);

    ret = xpu::broadcast<T>(
        dev_ctx.x_context(), x_data,
        x_broadcast_tensor.mutable_data<T>(ctx.GetPlace(), z->numel()),
        bcast_v.first, bcast_v.second);
    PADDLE_ENFORCE_EQ(
        ret, xpu::SUCCESS,
        platform::errors::External(
            "XPU kernel broadcast occur error in XPUElementwise error code %d",
            ret));
    need_wait = true;
    x_data = x_broadcast_tensor.data<T>();
  }
149

T
taixiurong 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
  if (y->numel() != z->numel()) {
    // broadcast y
    std::vector<int> bcast_x_v;
    std::vector<int> bcast_y_v;
    std::pair<std::vector<int>, std::vector<int>> bcast_v =
        XPUDimsToBroadcastVector(framework::make_ddim(y_dims_array), out_dim);
    ret = xpu::broadcast<T>(
        dev_ctx.x_context(), y_data,
        y_broadcast_tensor.mutable_data<T>(ctx.GetPlace(), z->numel()),
        bcast_v.first, bcast_v.second);
    PADDLE_ENFORCE_EQ(
        ret, xpu::SUCCESS,
        platform::errors::External(
            "XPU kernel broadcast occur error in XPUElementwise error code %d",
            ret));
    need_wait = true;
    y_data = y_broadcast_tensor.data<T>();
  }
  int len = z->numel();
  ret = func(dev_ctx.x_context(), x_data, y_data, z_data, len);
  PADDLE_ENFORCE_EQ(
      ret, xpu::SUCCESS,
      platform::errors::External(
          "XPU kernel Elementwise occur error in XPUElementwise error code ",
          ret));

  if (need_wait && dev_ctx.x_context()->xpu_stream) {
    dev_ctx.Wait();
  }
}

template <typename T>
void XPUElementwiseGrad(const framework::ExecutionContext& ctx,
                        std::function<int(xpu::Context*, const T*, const T*,
                                          const T*, const T*, T*, T*, int len)>
                            func,
                        bool use_x_y_data) {
  auto* x = ctx.Input<framework::Tensor>("X");
  auto* y = ctx.Input<framework::Tensor>("Y");
  auto* dz = ctx.Input<framework::Tensor>(framework::GradVarName("Out"));
  auto* z = dz;
  auto* dx = ctx.Output<framework::Tensor>(framework::GradVarName("X"));
  auto* dy = ctx.Output<framework::Tensor>(framework::GradVarName("Y"));
  int axis = ctx.Attr<int>("axis");
  const framework::DDim& x_dims = x->dims();
  const framework::DDim& y_dims = y->dims();
  int max_dim = std::max(x_dims.size(), y_dims.size());
  axis = (axis == -1 ? std::abs(x_dims.size() - y_dims.size()) : axis);
  PADDLE_ENFORCE_GE(
      axis, 0,
      platform::errors::InvalidArgument(
          "Axis should be great than or equal to 0, but received axis is %d.",
          axis));
  PADDLE_ENFORCE_LT(axis, max_dim,
                    platform::errors::InvalidArgument(
                        "Axis should be less than %d, but received axis is %d.",
                        max_dim, axis));

  std::vector<int> x_dims_array(max_dim);
  std::vector<int> y_dims_array(max_dim);
  std::vector<int> out_dims_array(max_dim);
  GetBroadcastDimsArrays(x_dims, y_dims, x_dims_array.data(),
                         y_dims_array.data(), out_dims_array.data(), max_dim,
                         axis);
  framework::DDim out_dim = framework::make_ddim(out_dims_array);

  int len = framework::product(out_dim);

  framework::Tensor x_broadcast_tensor;
  framework::Tensor y_broadcast_tensor;

  framework::Tensor dx_local_tensor;
  framework::Tensor dy_local_tensor;

  bool need_wait = false;
  const T* x_data = use_x_y_data ? x->data<T>() : z->data<T>();
  const T* y_data = use_x_y_data ? y->data<T>() : z->data<T>();

  const T* z_data = z->data<T>();
  const T* dz_data = (const T*)dz->data<T>();

  bool dx_need_reduce = (dx != nullptr) && (dx->numel() != len);
  bool dy_need_reduce = (dy != nullptr) && (dy->numel() != len);

  T* dx_data = ((dx == nullptr) || dx_need_reduce)
                   ? (dx_local_tensor.mutable_data<T>(ctx.GetPlace(), len))
                   : (dx->mutable_data<T>(ctx.GetPlace()));

  T* dy_data = ((dy == nullptr) || dy_need_reduce)
                   ? (dy_local_tensor.mutable_data<T>(ctx.GetPlace(), len))
                   : (dy->mutable_data<T>(ctx.GetPlace()));

  int ret = xpu::SUCCESS;
243 244 245
  auto& dev_ctx =
      ctx.template device_context<paddle::platform::XPUDeviceContext>();

T
taixiurong 已提交
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
  if (use_x_y_data && x->numel() != len) {
    std::vector<int> bcast_x_v;
    std::vector<int> bcast_y_v;
    std::pair<std::vector<int>, std::vector<int>> bcast_v =
        XPUDimsToBroadcastVector(framework::make_ddim(x_dims_array), out_dim);
    ret = xpu::broadcast<T>(
        dev_ctx.x_context(), x_data,
        x_broadcast_tensor.mutable_data<T>(ctx.GetPlace(), len), bcast_v.first,
        bcast_v.second);
    PADDLE_ENFORCE_EQ(ret, xpu::SUCCESS,
                      platform::errors::External(
                          "XPU kernel broadcast error occur! %d", ret));
    need_wait = true;
    x_data = x_broadcast_tensor.data<T>();
  }

  if (use_x_y_data && y->numel() != len) {
    // broadcast y
    std::vector<int> bcast_x_v;
    std::vector<int> bcast_y_v;
    std::pair<std::vector<int>, std::vector<int>> bcast_v =
        XPUDimsToBroadcastVector(framework::make_ddim(y_dims_array), out_dim);
    ret = xpu::broadcast<T>(
        dev_ctx.x_context(), y_data,
        y_broadcast_tensor.mutable_data<T>(ctx.GetPlace(), len), bcast_v.first,
        bcast_v.second);
    PADDLE_ENFORCE_EQ(ret, xpu::SUCCESS,
                      platform::errors::External(
                          "XPU kernel broadcast error occur! %d", ret));
    need_wait = true;
    y_data = y_broadcast_tensor.data<T>();
277 278
  }

T
taixiurong 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
  ret = func(dev_ctx.x_context(), x_data, y_data, z_data, dz_data, dx_data,
             dy_data, len);
  PADDLE_ENFORCE_EQ(ret, xpu::SUCCESS, platform::errors::External(
                                           "XPU kernel binary occur error in "
                                           "XPUElementwiseGrad, error code %d",
                                           ret));

  if (dx_need_reduce) {
    const framework::DDim& dx_dims = dx->dims();
    std::pair<std::vector<int>, std::vector<int>> reduce_v =
        XPUReducesAxisVector(out_dim, dx_dims);
    ret = xpu::reduce_sum(dev_ctx.x_context(), dx_data,
                          dx->mutable_data<T>(ctx.GetPlace()), reduce_v.first,
                          reduce_v.second);
    PADDLE_ENFORCE_EQ(
        ret, xpu::SUCCESS,
        platform::errors::External("XPU kernel reduce_sum occur error in "
                                   "XPUElementwiseGrad, error code %d",
                                   ret));
    need_wait = true;
299 300
  }

T
taixiurong 已提交
301 302 303 304 305 306 307 308 309 310 311 312 313 314
  if (dy_need_reduce) {
    const framework::DDim& dy_dims = dy->dims();
    std::pair<std::vector<int>, std::vector<int>> reduce_v =
        XPUReducesAxisVector(out_dim, dy_dims);
    ret = xpu::reduce_sum(dev_ctx.x_context(), dy_data,
                          dy->mutable_data<T>(ctx.GetPlace()), reduce_v.first,
                          reduce_v.second);
    PADDLE_ENFORCE_EQ(
        ret, xpu::SUCCESS,
        platform::errors::External("XPU kernel reduce_sum occur error in "
                                   "XPUElementwiseGrad, error code %d",
                                   ret));
    need_wait = true;
  }
315

T
taixiurong 已提交
316
  if (need_wait && dev_ctx.x_context()->xpu_stream) {
317 318 319 320 321 322 323
    dev_ctx.Wait();
  }
}

}  // namespace operators
}  // namespace paddle
#endif