elementwise_mkldnn_op.h 6.0 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
16
#include <string>
17 18 19 20 21 22 23 24 25 26 27 28
#include <unordered_map>
#include "paddle/fluid/operators/elementwise/elementwise_add_op.h"
#include "paddle/fluid/operators/elementwise/elementwise_op_function.h"

#include "paddle/fluid/framework/data_layout_transform.h"
#include "paddle/fluid/platform/mkldnn_reuse.h"

namespace paddle {
namespace operators {

using framework::DataLayout;
using framework::Tensor;
29 30 31
using dnnl::memory;
using dnnl::primitive;
using dnnl::stream;
32 33 34

template <typename T, dnnl::algorithm BINARY_OP>
class EltwiseMKLDNNKernel : public framework::OpKernel<T> {
35 36 37 38 39 40 41 42 43 44 45 46 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
 private:
  dnnl::post_ops get_post_ops(const framework::ExecutionContext& ctx) const {
    dnnl::post_ops post_operations;
    if (ctx.HasAttr("activation_type")) {
      const float scale = ctx.HasAttr("activation_scale")
                              ? ctx.Attr<float>("activation_scale")
                              : 1.0f;
      const float alpha = ctx.HasAttr("activation_alpha")
                              ? ctx.Attr<float>("activation_alpha")
                              : 0.0f;
      const float beta = ctx.HasAttr("activation_beta")
                             ? ctx.Attr<float>("activation_beta")
                             : 0.0f;

      static std::unordered_map<std::string, dnnl::algorithm> algo_map = {
          {"relu", dnnl::algorithm::eltwise_relu},
          {"tanh", dnnl::algorithm::eltwise_tanh},
          {"leaky_relu", dnnl::algorithm::eltwise_relu},
          {"swish", dnnl::algorithm::eltwise_swish},
          {"hardswish", dnnl::algorithm::eltwise_hardswish},
          {"sqrt", dnnl::algorithm::eltwise_sqrt},
          {"abs", dnnl::algorithm::eltwise_abs},
          {"clip", dnnl::algorithm::eltwise_clip},
          {"gelu", dnnl::algorithm::eltwise_gelu_erf},
          {"gelu_tanh", dnnl::algorithm::eltwise_gelu_tanh},
          {"relu6", dnnl::algorithm::eltwise_bounded_relu},
          {"sigmoid", dnnl::algorithm::eltwise_logistic}};

      const auto& activation_type =
          algo_map.find(ctx.Attr<std::string>("activation_type"));

      if (activation_type != algo_map.end()) {
        post_operations.append_eltwise(scale, activation_type->second, alpha,
                                       beta);
      }
    }
    return post_operations;
  }

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    const auto& dev_ctx =
        ctx.template device_context<paddle::platform::MKLDNNDeviceContext>();
    const auto& mkldnn_engine = dev_ctx.GetEngine();

    const auto* x = ctx.Input<Tensor>("X");
    const auto* y = ctx.Input<Tensor>("Y");
    auto* z = ctx.Output<Tensor>("Out");

    float scale_x = ctx.Attr<float>("Scale_x");
    float scale_y = ctx.Attr<float>("Scale_y");
    float scale_o = ctx.Attr<float>("Scale_out");
    int axis = ctx.Attr<int>("axis");

89 90 91
    platform::BinaryMKLDNNHandler<T> handler(
        BINARY_OP, axis, mkldnn_engine, ctx.GetPlace(), x, y, z, scale_x,
        scale_y, scale_o, get_post_ops(ctx));
92 93 94

    const auto src_x_memory = handler.AcquireSrcMemory(x);
    const auto src_y_memory = handler.AcquireSecondSrcMemory(y);
95 96 97 98 99 100 101 102 103
    // (jczaja) For Inplace src and dst should be the same memory object.
    // So x should share buffer with z. But UT mechanics is testing inplace
    // execution for this op not checking that x can be bradcasted to match in
    // shape y tensor.
    // This is wrong as when x is to be broadcasted then z(out) will match the
    // shape of y which is bigger than x. Hence if x is smaller in shape than z
    // and they share a buffer (of
    // shape x) then this buffer is not big enough to hold result of elementwise
    // operation.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
    const bool reuse_x_memopry =
        x->numel() == z->numel() && x->IsSharedBufferWith(*z);
    std::shared_ptr<dnnl::memory> dst_memory = nullptr;
    if (reuse_x_memopry) {
      dst_memory = src_x_memory;
      // NOTE(chenfeiyu): when the output reuses memory from other tensor rather
      // than allocate its own, it's still need to take care of its data type.
      // Unfortunately, paddle's operator only infers the output' shape, but not
      // the data type. mutable_data<T> takes care of allocation and data type
      // normally, but if the memory is already allocated and there is no need
      // to re-allocate, it just set the data type. So this it added there to
      // get the right data type.
      z->mutable_data<T>(ctx.GetPlace());
    } else {
      dst_memory = handler.AcquireDstMemory(z);
    }
120 121 122

    const auto binary_prim = handler.AcquireForwardPrimitive();

123
    auto& astream = platform::MKLDNNDeviceContext::tls().get_stream();
124 125 126 127 128 129 130 131 132 133 134 135 136

    const std::unordered_map<int, dnnl::memory> args = {
        {DNNL_ARG_SRC_0, *src_x_memory},
        {DNNL_ARG_SRC_1, *src_y_memory},
        {DNNL_ARG_DST, *dst_memory}};

    binary_prim->execute(astream, args);
    astream.wait();

    z->set_layout(DataLayout::kMKLDNN);
    z->set_format(platform::GetMKLDNNFormat(*dst_memory));
  }
};
137 138 139

inline std::vector<int64_t> CalculateBroadcastedDims(const Tensor* x,
                                                     const Tensor* y) {
140 141
  const auto src_tz = phi::vectorize(x->dims());
  const auto dst_tz = phi::vectorize(y->dims());
142 143 144 145 146 147 148 149 150 151

  size_t j = 0;
  std::vector<int64_t> dst_tz_ex(src_tz.size(), 1);
  for (size_t i = 0; i < src_tz.size(); ++i) {
    dst_tz_ex[i] = (src_tz[i] != dst_tz[j]) ? 1 : dst_tz[j++];
    if (j == dst_tz.size()) break;
  }

  return dst_tz_ex;
}
152 153
}  // namespace operators
}  // namespace paddle