activation_mkldnn_op.cc 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.

   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/activation_op.h"
K
Krzysztof Binias 已提交
16
#include "paddle/fluid/platform/mkldnn_helper.h"
17 18 19 20

namespace paddle {
namespace operators {

21 22 23 24 25 26 27 28
using framework::DataLayout;
using framework::Tensor;
using mkldnn::memory;
using mkldnn::primitive;
using mkldnn::stream;
using platform::GetMKLDNNFormat;
using platform::MKLDNNDeviceContext;
using platform::to_void_cast;
29 30

namespace {
K
Krzysztof Binias 已提交
31 32
std::string gethash(const mkldnn::memory::dims &operand_dims,
                    const mkldnn::algorithm algorithm) {
K
Krzysztof Binias 已提交
33 34 35 36 37 38 39 40
  auto dim2str = [](const mkldnn::memory::dims &operand_dims) {
    std::string dstr = "";
    for (size_t i = 0; i < operand_dims.size(); ++i) {
      dstr += std::to_string(operand_dims[i]) + "-";
    }
    return dstr;
  };
  return dim2str(operand_dims) + std::to_string(algorithm);
K
Krzysztof Binias 已提交
41
}
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
}  // namespace

template <typename Functor>
class MKLDNNActivationKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    const auto *x = ctx.Input<Tensor>("X");
    PADDLE_ENFORCE(x->layout() == DataLayout::kMKLDNN &&
                       x->format() != memory::format::format_undef,
                   "Wrong layout/format set for Input x tensor");

    Functor functor;

    auto attrs = functor.GetAttrs();
    for (auto &attr : attrs) {
      *attr.second = ctx.Attr<float>(attr.first);
    }
    functor(ctx);
  }
};
K
Krzysztof Binias 已提交
63

64 65 66 67 68 69 70 71 72 73
template <typename Functor>
class MKLDNNActivationGradKernel
    : public framework::OpKernel<typename Functor::ELEMENT_TYPE> {
 public:
  void Compute(const framework::ExecutionContext &ctx) const override {
    const auto *diff_y = ctx.Input<Tensor>(framework::GradVarName("Out"));
    PADDLE_ENFORCE(diff_y->layout() == DataLayout::kMKLDNN &&
                       diff_y->format() != memory::format::format_undef,
                   "Wrong layout/format set for Input OutGrad tensor");

74 75 76 77
    PADDLE_ENFORCE(
        !ctx.Attr<bool>("is_test"),
        "is_test attribute should be set to False in training phase.");

78 79 80 81 82 83 84 85 86 87 88 89 90 91
    Functor functor;

    auto attrs = functor.GetAttrs();
    for (auto &attr : attrs) {
      *attr.second = ctx.Attr<float>(attr.first);
    }
    functor(ctx);
  }
};

template <typename T>
void eltwise_forward(const framework::ExecutionContext &ctx,
                     mkldnn::algorithm algorithm, const T alpha = 0,
                     const T beta = 0) {
92 93 94 95 96
  PADDLE_ENFORCE(paddle::platform::is_cpu_place(ctx.GetPlace()),
                 "It must use CPUPlace.");
  auto &dev_ctx = ctx.template device_context<MKLDNNDeviceContext>();
  const auto &mkldnn_engine = dev_ctx.GetEngine();

97 98
  const auto *x = ctx.Input<Tensor>("X");
  auto *y = ctx.Output<Tensor>("Out");
99

100 101
  const T *x_data = x->data<T>();
  T *y_data = y->mutable_data<T>(ctx.GetPlace());
102

Y
Yihua Xu 已提交
103 104 105 106
  PADDLE_ENFORCE(
      x->dims().size() == 2 || x->dims().size() == 3 || x->dims().size() == 4,
      "Input dim must be with 2, 3 or 4");

107 108 109 110
  std::vector<int> src_tz = framework::vectorize2int(x->dims());

  auto src_format =
      src_tz.size() == 2 ? mkldnn::memory::format::nc : x->format();
111

K
Krzysztof Binias 已提交
112
  const std::string key = gethash(src_tz, algorithm);
K
Krzysztof Binias 已提交
113 114
  const std::string key_src_data =
      key + ctx.op().Output("Out") + "@eltwise_fwd_src_data";
115 116 117 118 119 120 121 122
  const std::string key_src_layout =
      key + ctx.op().Output("Out") + "@eltwise_fwd_src_layout";
  const std::string key_with_layout = key + std::to_string(src_format);
  const std::string key_src_mem = key_with_layout + "@eltwise_fwd_src_mem";
  const std::string key_dst_mem = key_with_layout + "@eltwise_fwd_dst_mem";
  const std::string key_fwd = key_with_layout + "@eltwise_fwd";
  const std::string key_fwd_pd = key_with_layout + "@eltwise_fwd_pd";

123 124
  bool is_test = ctx.Attr<bool>("is_test");

125 126 127
  // save input data and layout to be referred in backward path
  auto p_src_data = std::make_shared<const T *>(x_data);
  auto p_src_layout = std::make_shared<memory::format>(src_format);
128 129 130 131
  if (!is_test) {
    dev_ctx.SetBlob(key_src_data, p_src_data);
    dev_ctx.SetBlob(key_src_layout, p_src_layout);
  }
K
Krzysztof Binias 已提交
132

K
Krzysztof Binias 已提交
133 134
  auto p_fwd = std::static_pointer_cast<mkldnn::eltwise_forward>(
      dev_ctx.GetBlob(key_fwd));
K
Krzysztof Binias 已提交
135

136
  std::shared_ptr<memory> dst_memory;
K
Krzysztof Binias 已提交
137

K
Krzysztof Binias 已提交
138
  if (p_fwd == nullptr) {
139 140 141 142 143 144 145 146 147
    // create mkldnn memory for input X
    auto src_md = platform::MKLDNNMemDesc(
        src_tz, platform::MKLDNNGetDataType<T>(), src_format);
    auto src_memory = std::shared_ptr<memory>(
        new memory({src_md, mkldnn_engine}, to_void_cast(x_data)));
    // save src_memory to be referred in backward path
    dev_ctx.SetBlob(key_src_mem, src_memory);

    // create primitive descriptor for activation forward and save it
148 149 150
    auto mkldnn_forward_prop_kind = is_test
                                        ? mkldnn::prop_kind::forward_inference
                                        : mkldnn::prop_kind::forward_training;
151
    auto forward_desc = mkldnn::eltwise_forward::desc(
152
        mkldnn_forward_prop_kind, algorithm,
153 154 155 156 157
        src_memory->get_primitive_desc().desc(), alpha, beta);
    auto forward_pd = std::make_shared<mkldnn::eltwise_forward::primitive_desc>(
        forward_desc, mkldnn_engine);

    // save prim desc into global device context to be referred in backward path
158
    if (!is_test) dev_ctx.SetBlob(key_fwd_pd, forward_pd);
159 160 161 162 163 164 165 166 167 168

    // create mkldnn memory for output y
    dst_memory =
        std::make_shared<memory>(forward_pd->dst_primitive_desc(), y_data);

    dev_ctx.SetBlob(key_dst_mem, dst_memory);

    // create activation primitive
    p_fwd = std::make_shared<mkldnn::eltwise_forward>(*forward_pd, *src_memory,
                                                      *dst_memory);
K
Krzysztof Binias 已提交
169 170
    dev_ctx.SetBlob(key_fwd, p_fwd);
  } else {
K
Krzysztof Binias 已提交
171
    // primitives already exist
172
    auto src_memory =
K
Krzysztof Binias 已提交
173
        std::static_pointer_cast<mkldnn::memory>(dev_ctx.GetBlob(key_src_mem));
174 175 176
    PADDLE_ENFORCE(src_memory != nullptr,
                   "Fail to find eltwise src_memory in device context.");
    dst_memory =
K
Krzysztof Binias 已提交
177
        std::static_pointer_cast<mkldnn::memory>(dev_ctx.GetBlob(key_dst_mem));
178 179
    PADDLE_ENFORCE(dst_memory != nullptr,
                   "Fail to find eltwise dst_memory in device context.");
K
Krzysztof Binias 已提交
180

181 182
    src_memory->set_data_handle(platform::to_void_cast(x_data));
    dst_memory->set_data_handle(y_data);
K
Krzysztof Binias 已提交
183
  }
184 185

  // push primitive to stream and wait until it's executed
186 187 188 189 190 191
  std::vector<primitive> pipeline;
  pipeline.push_back(*p_fwd);
  stream(stream::kind::eager).submit(pipeline).wait();

  y->set_layout(DataLayout::kMKLDNN);
  y->set_format(GetMKLDNNFormat(*dst_memory));
192 193
}

194 195 196 197
template <typename T>
void eltwise_grad(const framework::ExecutionContext &ctx,
                  mkldnn::algorithm algorithm, const T alpha = 0,
                  const T beta = 0) {
198 199 200
  auto &dev_ctx = ctx.template device_context<MKLDNNDeviceContext>();
  const auto &mkldnn_engine = dev_ctx.GetEngine();

201 202
  const auto *diff_y = ctx.Input<Tensor>(framework::GradVarName("Out"));
  auto *diff_x = ctx.Output<Tensor>(framework::GradVarName("X"));
203

204 205
  const T *diff_y_data = diff_y->data<T>();
  T *diff_x_data = diff_x->mutable_data<T>(ctx.GetPlace());
206

207
  std::vector<int> diff_dst_tz = framework::vectorize2int(diff_y->dims());
K
Krzysztof Binias 已提交
208

209 210
  auto diff_y_format =
      diff_dst_tz.size() == 2 ? mkldnn::memory::format::nc : diff_y->format();
K
Krzysztof Binias 已提交
211

212
  const std::string key = gethash(diff_dst_tz, algorithm);
K
Krzysztof Binias 已提交
213 214
  const std::string key_src_data =
      key + ctx.op().Input("Out") + "@eltwise_fwd_src_data";
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
  const std::string key_src_layout =
      key + ctx.op().Input("Out") + "@eltwise_fwd_src_layout";
  const auto p_src_layout =
      std::static_pointer_cast<memory::format>(dev_ctx.GetBlob(key_src_layout));
  const std::string key_src_mem =
      key + std::to_string(*p_src_layout) + "@eltwise_fwd_src_mem";
  const std::string key_fwd_pd =
      key + std::to_string(*p_src_layout) + "@eltwise_fwd_pd";
  const std::string key_with_layouts =
      key + std::to_string(*p_src_layout) + "-" + std::to_string(diff_y_format);
  const std::string key_diff_src_mem =
      key_with_layouts + "@eltwise_diff_src_mem";
  const std::string key_diff_dst_mem =
      key_with_layouts + "@eltwise_diff_dst_mem";
  const std::string key_grad = key_with_layouts + "@eltwise_grad";

K
Krzysztof Binias 已提交
231 232 233
  const auto p_src_data =
      std::static_pointer_cast<T *>(dev_ctx.GetBlob(key_src_data));

234
  auto src_memory =
K
Krzysztof Binias 已提交
235
      std::static_pointer_cast<mkldnn::memory>(dev_ctx.GetBlob(key_src_mem));
236 237 238 239 240
  PADDLE_ENFORCE(src_memory != nullptr,
                 "Fail to find src_memory in device context");
  src_memory->set_data_handle(*p_src_data.get());

  std::shared_ptr<memory> diff_src_memory;
K
Krzysztof Binias 已提交
241

242
  auto p_grad = std::static_pointer_cast<mkldnn::eltwise_backward>(
K
Krzysztof Binias 已提交
243 244 245
      dev_ctx.GetBlob(key_grad));

  if (p_grad == nullptr) {
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
    // create mkldnn memory for input diff_y
    auto diff_dst_md = platform::MKLDNNMemDesc(
        diff_dst_tz, platform::MKLDNNGetDataType<T>(), diff_y_format);
    auto diff_dst_memory = std::shared_ptr<memory>(
        new memory({diff_dst_md, mkldnn_engine}, to_void_cast(diff_y_data)));
    dev_ctx.SetBlob(key_diff_dst_mem, diff_dst_memory);

    // retrieve eltwise primitive desc from device context
    auto forward_pd =
        std::static_pointer_cast<mkldnn::eltwise_forward::primitive_desc>(
            dev_ctx.GetBlob(key_fwd_pd));
    PADDLE_ENFORCE(forward_pd != nullptr,
                   "Fail to find eltwise_fwd_pd in device context");

    // ceate primitive descriptor for activation backward
    auto backward_desc = mkldnn::eltwise_backward::desc(
        algorithm, diff_dst_memory->get_primitive_desc().desc(),
        src_memory->get_primitive_desc().desc(), alpha, beta);
    auto backward_pd = mkldnn::eltwise_backward::primitive_desc(
        backward_desc, mkldnn_engine, *forward_pd);

    // create mkldnn memory for output diff_src
    diff_src_memory = std::make_shared<memory>(
        backward_pd.diff_src_primitive_desc(), diff_x_data);
    dev_ctx.SetBlob(key_diff_src_mem, diff_src_memory);

    // create activation backward primitive
K
Krzysztof Binias 已提交
273
    p_grad = std::make_shared<mkldnn::eltwise_backward>(
274 275
        backward_pd, *src_memory, *diff_dst_memory, *diff_src_memory);
    dev_ctx.SetBlob(key_grad, p_grad);
K
Krzysztof Binias 已提交
276 277
  } else {
    // primitives already exist
278
    diff_src_memory = std::static_pointer_cast<mkldnn::memory>(
K
Krzysztof Binias 已提交
279
        dev_ctx.GetBlob(key_diff_src_mem));
280
    auto diff_dst_memory = std::static_pointer_cast<mkldnn::memory>(
K
Krzysztof Binias 已提交
281 282
        dev_ctx.GetBlob(key_diff_dst_mem));

283 284 285 286
    diff_src_memory->set_data_handle(
        platform::to_void_reinterpret_cast(diff_x_data));
    diff_dst_memory->set_data_handle(
        platform::to_void_reinterpret_cast(diff_y_data));
K
Krzysztof Binias 已提交
287
  }
288 289

  // push primitive to stream and wait until it's executed
290 291 292 293 294 295
  std::vector<primitive> pipeline;
  pipeline.push_back(*p_grad);
  stream(stream::kind::eager).submit(pipeline).wait();

  diff_x->set_layout(DataLayout::kMKLDNN);
  diff_x->set_format(GetMKLDNNFormat(*diff_src_memory));
296 297 298 299
}

template <typename T, mkldnn::algorithm algorithm>
struct MKLDNNActivationFunc : public BaseActivationFunctor<T> {
300
  void operator()(const framework::ExecutionContext &ctx) const {
301 302 303 304 305 306
    eltwise_forward<T>(ctx, algorithm);
  }
};

template <typename T, mkldnn::algorithm algorithm>
struct MKLDNNActivationGradFunc : public BaseActivationFunctor<T> {
307
  void operator()(const framework::ExecutionContext &ctx) const {
308 309 310 311 312
    eltwise_grad<T>(ctx, algorithm);
  }
};

template <typename T>
T
tensor-tang 已提交
313
using ReluMKLDNNFunctor =
314 315 316
    MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_relu>;

template <typename T>
T
tensor-tang 已提交
317
using TanhMKLDNNFunctor =
318 319 320
    MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_tanh>;

template <typename T>
T
tensor-tang 已提交
321
using SqrtMKLDNNFunctor =
322 323 324
    MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_sqrt>;

template <typename T>
T
tensor-tang 已提交
325
using AbsMKLDNNFunctor =
326 327 328
    MKLDNNActivationFunc<T, mkldnn::algorithm::eltwise_abs>;

template <typename T>
T
tensor-tang 已提交
329
using ReluMKLDNNGradFunctor =
330 331 332
    MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_relu>;

template <typename T>
T
tensor-tang 已提交
333
using TanhMKLDNNGradFunctor =
334 335 336
    MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_tanh>;

template <typename T>
T
tensor-tang 已提交
337
using SqrtMKLDNNGradFunctor =
338 339 340
    MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_sqrt>;

template <typename T>
T
tensor-tang 已提交
341
using AbsMKLDNNGradFunctor =
342 343 344 345 346 347 348 349 350 351 352 353 354
    MKLDNNActivationGradFunc<T, mkldnn::algorithm::eltwise_abs>;
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

#define REGISTER_ACTIVATION_MKLDNN_KERNEL(act_type, functor, grad_functor) \
  REGISTER_OP_KERNEL(act_type, MKLDNN, ::paddle::platform::CPUPlace,       \
                     ops::MKLDNNActivationKernel<ops::functor<float>>);    \
  REGISTER_OP_KERNEL(                                                      \
      act_type##_grad, MKLDNN, ::paddle::platform::CPUPlace,               \
      ops::MKLDNNActivationGradKernel<ops::grad_functor<float>>);

K
Krzysztof Binias 已提交
355
#define FOR_EACH_MKLDNN_KERNEL_FUNCTOR(__macro)            \
T
tensor-tang 已提交
356 357 358 359
  __macro(relu, ReluMKLDNNFunctor, ReluMKLDNNGradFunctor); \
  __macro(tanh, TanhMKLDNNFunctor, TanhMKLDNNGradFunctor); \
  __macro(sqrt, SqrtMKLDNNFunctor, SqrtMKLDNNGradFunctor); \
  __macro(abs, AbsMKLDNNFunctor, AbsMKLDNNGradFunctor);
360 361

FOR_EACH_MKLDNN_KERNEL_FUNCTOR(REGISTER_ACTIVATION_MKLDNN_KERNEL);