helper.h 7.4 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* 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. */

#pragma once

17 18 19
extern "C" {
#include <xxhash.h>
}
T
tensor-tang 已提交
20
#include <iostream>
T
tensor-tang 已提交
21
#include <string>
22 23
#include <unordered_map>
#include <utility>  // for std::move
T
tensor-tang 已提交
24 25 26 27 28 29 30 31 32 33 34
#include <vector>
#include "paddle/fluid/operators/jit/gen_base.h"
#include "paddle/fluid/operators/jit/kernel_base.h"
#include "paddle/fluid/operators/jit/kernel_key.h"
#include "paddle/fluid/operators/jit/kernel_pool.h"
#include "paddle/fluid/platform/place.h"

namespace paddle {
namespace operators {
namespace jit {

35
template <typename KernelTuple, typename PlaceType>
T
tensor-tang 已提交
36
inline typename std::enable_if<
37
    std::is_same<typename KernelTuple::data_type, float>::value &&
T
tensor-tang 已提交
38
        std::is_same<PlaceType, platform::CPUPlace>::value,
39 40 41 42
    typename KernelTuple::func_type>::type
GetJitCode(const typename KernelTuple::attr_type& attr) {
  using Func = typename KernelTuple::func_type;
  using Attr = typename KernelTuple::attr_type;
T
tensor-tang 已提交
43
  size_t key = JitCodeKey<Attr>(attr);
44
  auto& codes = JitCodePool<KernelTuple::kernel_type>().Instance();
T
tensor-tang 已提交
45 46 47 48
  if (codes.Has(key)) {
    return codes.AllKernels().at(key)->template getCode<Func>();
  }

T
tensor-tang 已提交
49
  // creator is not related with attr, so can use KernelKey as key
50
  KernelKey kkey(KernelTuple::kernel_type, PlaceType());
T
tensor-tang 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63
  // pool: (KernelKey(type, place), vector<GenCreatorPtr>)
  auto& creator_map = JitCodeCreatorPool().Instance().AllCreators();
  auto iter = creator_map.find(kkey);
  if (iter != creator_map.end()) {
    auto& creators = iter->second;
    for (auto& cur : creators) {
      auto i = dynamic_cast<const JitCodeCreator<Attr>*>(cur.get());
      if (i && i->UseMe(attr)) {
        auto p = i->CreateJitCode(attr);
        if (p) {
          auto f = p->template getCode<Func>();
          codes.Insert(key, std::move(p));
          return f;
T
tensor-tang 已提交
64 65 66 67
        }
      }
    }
  }
T
tensor-tang 已提交
68 69 70
  return nullptr;
}

71
template <typename KernelTuple, typename PlaceType>
T
tensor-tang 已提交
72
inline typename std::enable_if<
73
    !std::is_same<typename KernelTuple::data_type, float>::value ||
T
tensor-tang 已提交
74
        !std::is_same<PlaceType, platform::CPUPlace>::value,
75 76
    typename KernelTuple::func_type>::type
GetJitCode(const typename KernelTuple::attr_type& attr) {
T
tensor-tang 已提交
77 78 79
  return nullptr;
}

T
tensor-tang 已提交
80 81
// Refer code do not related with attr, which is just for cast
// Refer is always on CPUPlace
82 83
template <typename KernelTuple>
inline typename KernelTuple::func_type GetRefer() {
T
tensor-tang 已提交
84
  auto& ref_pool = ReferKernelPool().Instance().AllKernels();
85
  KernelKey kkey(KernelTuple::kernel_type, platform::CPUPlace());
T
tensor-tang 已提交
86 87 88 89 90
  auto ref_iter = ref_pool.find(kkey);
  PADDLE_ENFORCE(ref_iter != ref_pool.end(),
                 "Every Kernel should have reference function.");
  auto& ref_impls = ref_iter->second;
  for (auto& impl : ref_impls) {
91
    auto i = dynamic_cast<const ReferKernel<KernelTuple>*>(impl.get());
T
tensor-tang 已提交
92 93 94 95 96 97 98
    if (i) {
      return i->GetFunc();
    }
  }
  return nullptr;
}

99 100 101 102
template <typename KernelTuple, typename PlaceType = platform::CPUPlace>
typename KernelTuple::func_type Get(
    const typename KernelTuple::attr_type& attr) {
  auto jitfunc = GetJitCode<KernelTuple, PlaceType>(attr);
T
tensor-tang 已提交
103 104 105
  if (jitfunc) {
    return jitfunc;
  }
T
tensor-tang 已提交
106 107

  // pool: (KernelKey(type, place), vector<KernelPtr>)
108
  KernelKey kkey(KernelTuple::kernel_type, PlaceType());
T
tensor-tang 已提交
109 110 111 112 113
  auto& pool = KernelPool().Instance().AllKernels();
  auto iter = pool.find(kkey);
  if (iter != pool.end()) {
    auto& impls = iter->second;
    for (auto& impl : impls) {
114
      auto i = dynamic_cast<const KernelMore<KernelTuple>*>(impl.get());
T
tensor-tang 已提交
115 116 117 118 119 120 121
      if (i && i->UseMe(attr)) {
        return i->GetFunc();
      }
    }
  }

  // The last implementation should be reference function on CPUPlace.
122
  return GetRefer<KernelTuple>();
T
tensor-tang 已提交
123 124
}

125
template <typename KernelTuple, typename PlaceType>
T
tensor-tang 已提交
126
class KernelFuncs {
T
tensor-tang 已提交
127
 public:
T
tensor-tang 已提交
128 129
  KernelFuncs() = default;
  static KernelFuncs& Cache() {
130
    static thread_local KernelFuncs<KernelTuple, PlaceType> g_func_cache;
T
tensor-tang 已提交
131 132 133
    return g_func_cache;
  }

134
  // the exposed interface to use
135 136
  typename KernelTuple::func_type At(
      const typename KernelTuple::attr_type& attr) {
137
    // XXH64: 13.8 GB/s
138 139 140
    // TODO(TJ): change me, maybe not all attr change need one key, should be
    // attrkey
    int64_t key = XXH64(&attr, sizeof(typename KernelTuple::attr_type), 0);
T
tensor-tang 已提交
141 142 143
    if (Has(key)) {
      return funcs_.at(key);
    }
144 145 146
    // If do not have this attr in cache,
    // then could run some runtime benchmark of this attr and save the best one.
    // Here just get the offline benchmarked best one.
147
    auto func = Get<KernelTuple, PlaceType>(attr);
T
tensor-tang 已提交
148 149 150 151
    Insert(key, func);
    return func;
  }

152 153
  typename KernelTuple::func_type operator[](
      const typename KernelTuple::attr_type& attr) {
154 155 156 157 158 159
    return At(attr);
  }

 protected:
  bool Has(int64_t key) const { return funcs_.find(key) != funcs_.end(); }

160
  void Insert(int64_t key, typename KernelTuple::func_type func) {
161 162 163
    funcs_.emplace(key, func);
  }

T
tensor-tang 已提交
164
 private:
165
  std::unordered_map<int64_t, typename KernelTuple::func_type> funcs_;
T
tensor-tang 已提交
166
  DISABLE_COPY_AND_ASSIGN(KernelFuncs);
T
tensor-tang 已提交
167 168
};

169
const char* to_string(KernelType kt);
170
const char* to_string(SeqPoolType kt);
171

T
tensor-tang 已提交
172 173
KernelType to_kerneltype(const std::string& act);

T
tensor-tang 已提交
174 175 176 177 178 179 180
inline std::ostream& operator<<(std::ostream& os, const lstm_attr_t& attr) {
  os << "dim_size[" << attr.d << "],act_gate[" << to_string(attr.act_gate)
     << "],act_cand[" << to_string(attr.act_cand) << "],act_cell["
     << to_string(attr.act_cell) << "],use_peephole["
     << (attr.use_peephole ? "True" : "False") << "]";
  return os;
}
181

T
tensor-tang 已提交
182 183 184 185 186
inline std::ostream& operator<<(std::ostream& os, const gru_attr_t& attr) {
  os << "dim_size[" << attr.d << "],act_gate[" << to_string(attr.act_gate)
     << "],act_cand[" << to_string(attr.act_cand) << "]";
  return os;
}
187

188 189 190 191 192
inline std::ostream& operator<<(std::ostream& os, const seq_pool_attr_t& attr) {
  os << "height_size[" << attr.h << "],width_size[" << attr.w << "],pool_type["
     << to_string(attr.type) << "]";
  return os;
}
T
tensor-tang 已提交
193

194 195 196 197 198 199 200 201 202
inline std::ostream& operator<<(std::ostream& os,
                                const emb_seq_pool_attr_t& attr) {
  os << "table_height[" << attr.table_height << "],table_width["
     << attr.table_width << "],index_height[" << attr.index_height
     << "],index_width[" << attr.index_width << "],output_width["
     << attr.out_width << "],pool_type[" << to_string(attr.pool_type) << "]";
  return os;
}

203 204 205 206 207 208 209 210
inline std::ostream& operator<<(std::ostream& os, const sgd_attr_t& attr) {
  os << "param_height[" << attr.param_height << "],param_width["
     << attr.param_width << "],grad_height[" << attr.grad_height
     << "],grad_width[" << attr.grad_width << "],selected_rows_size["
     << attr.selected_rows_size << "]";
  return os;
}

211 212 213 214 215 216 217 218 219
inline std::ostream& operator<<(std::ostream& os, const matmul_attr_t& attr) {
  os << "M[" << attr.m << "],N[" << attr.n << "],K[" << attr.k << "]";
  return os;
}

// expose the method to pack matmul weight
template <typename T>
void pack_weights(const T* src, T* dst, int n, int k);

T
tensor-tang 已提交
220 221 222
}  // namespace jit
}  // namespace operators
}  // namespace paddle