concat_mkldnn_op.cc 5.4 KB
Newer Older
M
Michal Gallus 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* Copyright (c) 2018 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. */

M
Michal Gallus 已提交
15
#include <memory>
M
Michal Gallus 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#include "paddle/fluid/operators/concat_op.h"
#include "paddle/fluid/platform/mkldnn_helper.h"

namespace paddle {
namespace operators {

using framework::DataLayout;
using framework::Tensor;
using mkldnn::memory;
using mkldnn::primitive;
using mkldnn::concat;
using mkldnn::stream;
using platform::to_void_cast;

static void EnforceLayouts(const std::vector<const Tensor*> inputs) {
  for (auto* input : inputs) {
    const bool is_layout_correct = input->layout() == DataLayout::kMKLDNN;
33 34
    const bool is_format_defined =
        input->format() != memory::format::format_undef;
M
Michal Gallus 已提交
35 36 37 38 39
    PADDLE_ENFORCE(is_layout_correct && is_format_defined,
                   "Wrong layout/format set for Input tensor");
  }
}

40 41
static memory::primitive_desc CreateMemPrimDesc(const Tensor& input,
                                                const mkldnn::engine& engine) {
M
Michal Gallus 已提交
42 43 44 45 46 47 48 49
  constexpr auto data_type = mkldnn::memory::f32;
  const auto dims = paddle::framework::vectorize2int(input.dims());
  const auto format = input.format();
  auto description = memory::desc(dims, data_type, format);
  auto mem_prim_desc = memory::primitive_desc(description, engine);
  return mem_prim_desc;
}

M
Michal Gallus 已提交
50
static mkldnn::memory::format GetDstMemFormat(
51 52
    const concat::primitive_desc& concat_pd) {
  return (memory::format)concat_pd.dst_primitive_desc().desc().data.format;
M
Michal Gallus 已提交
53 54
}

M
Michal Gallus 已提交
55 56 57 58 59 60 61 62
static platform::CPUPlace GetCpuPlace(
    const paddle::framework::ExecutionContext& ctx) {
  auto place = ctx.GetPlace();
  PADDLE_ENFORCE(paddle::platform::is_cpu_place(place),
                 "It must use CPUPlace.");
  return boost::get<platform::CPUPlace>(place);
}

M
Michal Gallus 已提交
63
static const mkldnn::engine& GetMKLDNNEngine(
64 65 66
    const paddle::framework::ExecutionContext& ctx) {
  auto& dev_ctx = ctx.template device_context<platform::MKLDNNDeviceContext>();
  return dev_ctx.GetEngine();
M
Michal Gallus 已提交
67
}
M
Michal Gallus 已提交
68

M
Michal Gallus 已提交
69 70 71 72 73 74 75 76 77 78
template <typename T>
class ConcatPrimitiveFactory {
 public:
  concat::primitive_desc CreateConcatPrimDescriptor(
      const std::vector<const Tensor*> multi_input, Tensor* output,
      int concat_axis, const mkldnn::engine& mkldnn_engine) {
    CreateSourcesDescriptors(multi_input, mkldnn_engine);
    auto dst_desc = CreateDstMemDescriptor(output);
    return concat::primitive_desc(dst_desc, concat_axis, srcs_pd);
  }
M
Michal Gallus 已提交
79

M
Michal Gallus 已提交
80 81 82 83 84 85 86 87 88 89 90
  concat CreateConcatPrimitive(const concat::primitive_desc& concat_pd,
                               Tensor* output, platform::CPUPlace place) {
    CreateSourcePrimitiveAts();
    auto dst_mem = CreateDstMemory(concat_pd, output, place);
    return concat(concat_pd, inputs, dst_mem);
  }

 private:
  memory::desc CreateDstMemDescriptor(Tensor* output) {
    auto dst_dims = paddle::framework::vectorize2int(output->dims());
    return memory::desc(dst_dims, platform::MKLDNNGetDataType<T>(),
91
                        memory::format::any);
M
Michal Gallus 已提交
92 93 94 95 96 97 98
  }

  mkldnn::memory CreateDstMemory(const concat::primitive_desc& concat_pd,
                                 Tensor* output, platform::CPUPlace place) {
    return memory(concat_pd.dst_primitive_desc(),
                  output->mutable_data<T>(place));
  }
M
Michal Gallus 已提交
99

M
Michal Gallus 已提交
100 101
  void CreateSourcesDescriptors(const std::vector<const Tensor*> multi_input,
                                const mkldnn::engine& mkldnn_engine) {
M
Michal Gallus 已提交
102
    for (size_t i = 0; i < multi_input.size(); i++) {
103 104 105 106
      auto mem_prim_desc = CreateMemPrimDesc(*multi_input[i], mkldnn_engine);
      srcs_pd.push_back(mem_prim_desc);
      srcs.push_back(
          memory(mem_prim_desc, to_void_cast(multi_input[i]->data<T>())));
M
Michal Gallus 已提交
107
    }
M
Michal Gallus 已提交
108
  }
M
Michal Gallus 已提交
109

M
Michal Gallus 已提交
110
  void CreateSourcePrimitiveAts() {
M
Michal Gallus 已提交
111 112 113 114
    inputs.reserve(srcs.size());
    for (size_t i = 0; i < srcs.size(); i++) {
      inputs.push_back(srcs[i]);
    }
M
Michal Gallus 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
  }

 private:
  std::vector<memory::primitive_desc> srcs_pd;
  std::vector<memory> srcs;
  std::vector<primitive::at> inputs;
};

template <typename T>
class ConcatMKLDNNOpKernel : public paddle::framework::OpKernel<T> {
 public:
  void Compute(const paddle::framework::ExecutionContext& ctx) const override {
    auto place = GetCpuPlace(ctx);
    const auto& mkldnn_engine = GetMKLDNNEngine(ctx);

    auto multi_input = ctx.MultiInput<Tensor>("X");
    EnforceLayouts(multi_input);
    Tensor* output = ctx.Output<Tensor>("Out");
    int64_t concat_axis = static_cast<int64_t>(ctx.Attr<int>("axis"));

    ConcatPrimitiveFactory<T> prim_creator;
136 137
    auto concat_pd = prim_creator.CreateConcatPrimDescriptor(
        multi_input, output, static_cast<int>(concat_axis), mkldnn_engine);
M
Michal Gallus 已提交
138 139
    auto concat = prim_creator.CreateConcatPrimitive(concat_pd, output, place);
    stream(stream::kind::eager).submit({concat}).wait();
M
Michal Gallus 已提交
140 141

    output->set_layout(DataLayout::kMKLDNN);
M
Michal Gallus 已提交
142
    output->set_format(GetDstMemFormat(concat_pd));
M
Michal Gallus 已提交
143 144 145 146 147 148 149 150 151
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;

REGISTER_OP_KERNEL(concat, MKLDNN, ::paddle::platform::CPUPlace,
                   ops::ConcatMKLDNNOpKernel<float>)