mkldnn_helper.h 6.8 KB
Newer Older
1
/* Copyright (c) 2017 PaddlePaddle Authors. All Rights Reserved.
T
tensor-tang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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 <mkldnn.h>
17
#include <algorithm>
P
Physher 已提交
18
#include <memory>
G
gongweibao 已提交
19
#include <string>
20
#include <utility>
21
#include <vector>
22
#include "paddle/fluid/framework/operator.h"
M
mozga-intel 已提交
23
#include "paddle/fluid/platform/place.h"
T
tensor-tang 已提交
24
namespace paddle {
25 26 27
#ifdef PADDLE_WITH_MKLDNN
using MKLDNNMemoryFormat = mkldnn::memory::format;
#endif
T
tensor-tang 已提交
28 29 30 31 32
namespace platform {

using MKLDNNStream = mkldnn::stream;
using MKLDNNEngine = mkldnn::engine;
using MKLDNNMemory = mkldnn::memory;
33
using MKLDNNMemoryDescriptor = mkldnn::memory::desc;
T
tensor-tang 已提交
34 35 36
using MKLDNNPrimitive = mkldnn::primitive;
using MKLDNNPrimitiveDesc = mkldnn::handle<mkldnn_primitive_desc_t>;

37 38 39 40 41
typedef std::unique_ptr<MKLDNNStream> MKLDNNStreamPtr;
typedef std::unique_ptr<MKLDNNEngine> MKLDNNEnginePtr;
typedef std::unique_ptr<MKLDNNMemory> MKLDNNMemoryPtr;
typedef std::unique_ptr<MKLDNNPrimitive> MKLDNNPrimitivePtr;
typedef std::unique_ptr<MKLDNNPrimitiveDesc> MKLDNNPrimitiveDescPtr;
T
tensor-tang 已提交
42

43 44 45 46 47
template <typename Type>
void* to_void_cast(const Type* t) {
  return static_cast<void*>(const_cast<Type*>(t));
}

K
Krzysztof Binias 已提交
48 49 50 51 52
template <typename Type>
void* to_void_reinterpret_cast(const Type* t) {
  return reinterpret_cast<void*>(const_cast<Type*>(t));
}

53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
template <class Type>
using tf_desc = typename Type::desc;

template <class Type>
using tf_pd = typename Type::primitive_desc;

template <typename Type, typename Engine, typename... Args>
std::shared_ptr<tf_pd<Type>> MKLDNNFwdPrimitiveDesc(const Engine& e,
                                                    Args&&... args) {
  auto desc = tf_desc<Type>(mkldnn::prop_kind::forward, (args)...);
  auto pd = new tf_pd<Type>(desc, e);
  return std::shared_ptr<tf_pd<Type>>(pd);
}

template <typename Type, typename Engine, typename Primitive, typename... Args>
tf_pd<Type> MKLDNNBwdPrimitiveDesc(const Engine& e, const Primitive& p,
                                   Args&&... args) {
  auto desc = tf_desc<Type>(args...);
  return tf_pd<Type>(desc, e, p);
}

74 75
inline mkldnn::memory::desc MKLDNNMemDesc(const std::vector<int>& dims,
                                          mkldnn::memory::data_type data_type,
76
                                          MKLDNNMemoryFormat format) {
77 78 79 80 81 82 83 84 85
  mkldnn::memory::dims tz = dims;
  return mkldnn::memory::desc({tz}, data_type, format);
}

inline bool CanMKLDNNBeUsed(const framework::ExecutionContext& ctx) {
  bool use_mkldnn = ctx.Attr<bool>("use_mkldnn");
  return use_mkldnn && platform::is_cpu_place(ctx.GetPlace());
}

86 87
template <typename Type>
mkldnn::memory::data_type MKLDNNGetDataType() {
88
  return mkldnn::memory::data_type::data_undef;
89 90 91 92
}

template <>
inline mkldnn::memory::data_type MKLDNNGetDataType<float>() {
93 94 95 96 97
  return mkldnn::memory::data_type::f32;
}
template <>
inline mkldnn::memory::data_type MKLDNNGetDataType<int32_t>() {
  return mkldnn::memory::data_type::s32;
98
}
P
Physher 已提交
99 100
template <>
inline mkldnn::memory::data_type MKLDNNGetDataType<int8_t>() {
101
  return mkldnn::memory::data_type::s8;
P
Physher 已提交
102 103 104
}
template <>
inline mkldnn::memory::data_type MKLDNNGetDataType<uint8_t>() {
105
  return mkldnn::memory::data_type::u8;
P
Physher 已提交
106 107
}

M
mozga-intel 已提交
108 109 110 111 112 113 114
inline void Reorder(const mkldnn::memory& src, const mkldnn::memory& dst) {
  auto reorder_prim = mkldnn::reorder(src, dst);
  std::vector<mkldnn::primitive> pipeline;
  pipeline.push_back(reorder_prim);
  mkldnn::stream(mkldnn::stream::kind::eager).submit(pipeline).wait();
}

115 116
inline MKLDNNMemoryFormat GetMKLDNNFormat(const mkldnn::memory memory) {
  return static_cast<MKLDNNMemoryFormat>(
M
mozga-intel 已提交
117 118 119
      memory.get_primitive_desc().desc().data.format);
}

120
inline MKLDNNMemoryFormat GetMKLDNNFormat(
121
    const mkldnn::sum::primitive_desc& memory) {
122
  return static_cast<MKLDNNMemoryFormat>(
123 124 125
      memory.dst_primitive_desc().desc().data.format);
}

126 127
inline MKLDNNMemoryFormat MKLDNNFormatForSize(size_t dims_size,
                                              MKLDNNMemoryFormat data_format) {
128
  if (dims_size == 1) {
129
    return MKLDNNMemoryFormat::x;
130
  } else if (dims_size == 2) {
131
    return MKLDNNMemoryFormat::nc;
132
  } else if (dims_size == 3) {
133 134 135 136
    if (data_format == MKLDNNMemoryFormat::nchw) {
      return MKLDNNMemoryFormat::ncw;
    } else if (data_format == MKLDNNMemoryFormat::nhwc) {
      return MKLDNNMemoryFormat::nwc;
137
    }
138
  } else if (dims_size == 4) {
139 140
    if (data_format == MKLDNNMemoryFormat::goihw) {
      return MKLDNNMemoryFormat::oihw;
141
    }
142
  } else if (dims_size == 5) {
143 144
    if (data_format == MKLDNNMemoryFormat::goidhw) {
      return MKLDNNMemoryFormat::oidhw;
145
    }
146 147 148 149
    if (data_format == MKLDNNMemoryFormat::nchw) {
      return MKLDNNMemoryFormat::ncdhw;
    } else if (data_format == MKLDNNMemoryFormat::nhwc) {
      return MKLDNNMemoryFormat::ndhwc;
150
    }
151 152 153 154
  }
  return data_format;
}

155
inline MKLDNNMemoryFormat data_format_to_memory_format(
156 157 158
    const std::string& data_format) {
  switch (framework::StringToDataLayout(data_format)) {
    case framework::DataLayout::kNHWC:
159
      return MKLDNNMemoryFormat::nhwc;
160
    case framework::DataLayout::kNCHW:
161
      return MKLDNNMemoryFormat::nchw;
162
    default:
163
      return MKLDNNMemoryFormat::any;
164 165 166
  }
}

167
inline MKLDNNMemoryFormat StringToMKLDNNFormat(std::string* format) {
168 169 170
  std::transform(format->begin(), format->end(), format->begin(), ::tolower);

  if (!format->compare("nchw")) {
171
    return MKLDNNMemoryFormat::nchw;
172
  } else if (!format->compare("nchw16c")) {
173
    return MKLDNNMemoryFormat::nChw16c;
174
  } else if (!format->compare("nchw8c")) {
175
    return MKLDNNMemoryFormat::nChw8c;
176
  } else if (!format->compare("nhwc")) {
177
    return MKLDNNMemoryFormat::nhwc;
178
  } else {
179
    return MKLDNNMemoryFormat::any;
180 181 182
  }
}

A
Adam 已提交
183 184 185 186 187
inline std::string ThreadIDasStr(void) {
  return std::to_string(
      std::hash<std::thread::id>()(std::this_thread::get_id()));
}

188 189 190
template <typename T>
inline void AppendKey(std::string* key, const T& num) {
  key->append(std::to_string(num));
A
Adam 已提交
191 192
}

193 194
inline void AppendKey(std::string* key, const std::string& str) {
  key->append(str);
A
Adam 已提交
195 196
}

197
inline void AppendKey(std::string* key, const char* str) { key->append(str); }
A
Adam 已提交
198

199 200
inline void AppendKey(std::string* key, const std::vector<int>& dims) {
  for (size_t i = 0; i < dims.size(); i++) {
A
Adam 已提交
201 202 203 204
    AppendKey(key, std::to_string(dims[i]));
  }
}

205 206 207
template <typename... ArgTypes>
inline std::string CreateKey(ArgTypes&&... args) {
  std::string key;
208
  key.reserve(64);
209
  using expand_type = int[];
210
  expand_type{0, (AppendKey(&key, std::forward<ArgTypes>(args)), 0)...};
211 212 213
  return key;
}

T
tensor-tang 已提交
214 215
}  // namespace platform
}  // namespace paddle