jit_kernel_rnn.cc 10.2 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. */

#include "paddle/fluid/operators/math/jit_kernel.h"
#include <string>
T
tensor-tang 已提交
17
#include "paddle/fluid/operators/math/jit_kernel_macro.h"
18
#include "paddle/fluid/operators/math/jit_kernel_refer.h"
T
tensor-tang 已提交
19
#include "paddle/fluid/platform/enforce.h"
T
tensor-tang 已提交
20
#include "paddle/fluid/platform/macros.h"
T
tensor-tang 已提交
21

22 23 24 25
#ifdef PADDLE_WITH_XBYAK
#include "paddle/fluid/operators/math/jit_code.h"
#endif

T
tensor-tang 已提交
26 27 28
namespace paddle {
namespace operators {
namespace math {
T
tensor-tang 已提交
29
namespace jitkernel {
T
tensor-tang 已提交
30

T
tensor-tang 已提交
31
/* LSTM JitKernel */
32
template <typename T>
T
tensor-tang 已提交
33 34
class LSTMKernelImpl : public LSTMKernel<T> {
 public:
35 36
  static inline std::string name(const lstm_attr_t& attr) {
    PADDLE_THROW("DType should be either float or double");
T
tensor-tang 已提交
37
  }
38 39 40 41 42
  static inline bool useJIT(int d) { return false; }
  static inline bool useMKL(int d) { return false; }
  explicit LSTMKernelImpl(const lstm_attr_t& attr) : LSTMKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(attr.d)) {
43
      size_t sz = 96 + attr.d / YMM_FLOAT_BLOCK * 90 * 4 * 8;
44 45 46 47 48 49 50 51 52 53
      jitcode0_.reset(new gen::LSTMJitCode(false, attr, sz > 4096 ? sz : 4096));
      this->ComputeCtHt =
          jitcode0_->getCode<void (*)(lstm_t*, const lstm_attr_t*)>();

      jitcode1_.reset(new gen::LSTMJitCode(true, attr, sz > 4096 ? sz : 4096));
      this->ComputeC1H1 =
          jitcode1_->getCode<void (*)(lstm_t*, const lstm_attr_t*)>();
      return;
    }
#endif
T
tensor-tang 已提交
54

55 56
    this->ComputeCtHt = refer::LSTMCtHt<T>;
    this->ComputeC1H1 = refer::LSTMC1H1<T>;
57
  }
T
tensor-tang 已提交
58

59 60
#ifdef PADDLE_WITH_XBYAK

T
tensor-tang 已提交
61
 private:
62
  std::unique_ptr<gen::LSTMJitCode> jitcode0_{nullptr}, jitcode1_{nullptr};
T
tensor-tang 已提交
63
#endif
T
tensor-tang 已提交
64 65
};

66 67 68
#ifdef PADDLE_WITH_XBYAK
template <>
bool LSTMKernelImpl<float>::useJIT(int d) {
69
  return gen::LSTMJitCode::init(d);
70
}
T
tensor-tang 已提交
71 72
#endif

T
tensor-tang 已提交
73
/* Peephole JitKernel */
74
template <typename T>
T
tensor-tang 已提交
75 76
class PeepholeKernelImpl : public LSTMKernel<T> {
 public:
77 78
  static inline std::string name(const lstm_attr_t& attr) {
    PADDLE_THROW("DType should be either float or double");
T
tensor-tang 已提交
79
  }
80 81 82 83 84
  static inline bool useJIT(int d) { return false; }
  static inline bool useMKL(int d) { return false; }
  explicit PeepholeKernelImpl(const lstm_attr_t& attr) : LSTMKernel<T>() {
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(attr.d)) {
85
      size_t sz = 96 + attr.d / YMM_FLOAT_BLOCK * 96 * 4 * 8;
86 87 88 89 90 91 92 93 94 95
      jitcode0_.reset(new gen::LSTMJitCode(false, attr, sz > 4096 ? sz : 4096));
      this->ComputeCtHt =
          jitcode0_->getCode<void (*)(lstm_t*, const lstm_attr_t*)>();

      jitcode1_.reset(new gen::LSTMJitCode(true, attr, sz > 4096 ? sz : 4096));
      this->ComputeC1H1 =
          jitcode1_->getCode<void (*)(lstm_t*, const lstm_attr_t*)>();
      return;
    }
#endif
T
tensor-tang 已提交
96

97 98
    this->ComputeCtHt = refer::LSTMCtHt<T>;
    this->ComputeC1H1 = refer::LSTMC1H1<T>;
99
  }
T
tensor-tang 已提交
100

101
#ifdef PADDLE_WITH_XBYAK
T
tensor-tang 已提交
102

T
tensor-tang 已提交
103
 private:
104 105
  std::unique_ptr<gen::LSTMJitCode> jitcode0_{nullptr}, jitcode1_{nullptr};
#endif
T
tensor-tang 已提交
106 107
};

108 109 110
#ifdef PADDLE_WITH_XBYAK
template <>
bool PeepholeKernelImpl<float>::useJIT(int d) {
T
tensor-tang 已提交
111
  return gen::LSTMJitCode::init(d);
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
}
#endif

#define JITKERNEL_DEFINE_NAME_LSTM(ker_key, ker_class)                 \
  template <>                                                          \
  std::string ker_class##Impl<float>::name(const lstm_attr_t& attr) {  \
    std::string key(#ker_key "f");                                     \
    key += (attr.act_gate + attr.act_cand + attr.act_cell +            \
            (attr.use_peephole ? "p" : "n"));                          \
    if (useJIT(attr.d)) {                                              \
      /* only jit code need record d*/                                 \
      return key + "jit" + std::to_string(attr.d);                     \
    } else if (useMKL(attr.d)) {                                       \
      return key + "mkl";                                              \
    } else {                                                           \
      return key + "any";                                              \
    }                                                                  \
  }                                                                    \
  template <>                                                          \
  std::string ker_class##Impl<double>::name(const lstm_attr_t& attr) { \
    std::string key(#ker_key "d");                                     \
    /* jit code do not support double yet*/                            \
    if (useMKL(attr.d)) {                                              \
      return key + "mkl";                                              \
    } else {                                                           \
      return key + "any";                                              \
    }                                                                  \
T
tensor-tang 已提交
139
  }
T
tensor-tang 已提交
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
#define JITKERNEL_DECLARE_LSTM(ker_class, ker_dtype)          \
  template <>                                                 \
  std::shared_ptr<const LSTMKernel<ker_dtype>>                \
  KernelPool::Get<LSTMKernel<ker_dtype>, const lstm_attr_t&>( \
      const lstm_attr_t& attr)

#define JITKERNEL_FIND_KEY_LSTM(ker_class, ker_dtype) \
  std::string key = ker_class##Impl<ker_dtype>::name(attr)

#define JITKERNEL_LSTM_IMPL(ker, dtype)                     \
  if (attr.use_peephole) {                                  \
    p = std::dynamic_pointer_cast<ker<dtype>>(              \
        std::make_shared<PeepholeKernelImpl<dtype>>(attr)); \
  } else {                                                  \
    p = std::dynamic_pointer_cast<ker<dtype>>(              \
        std::make_shared<ker##Impl<dtype>>(attr));          \
  }
T
tensor-tang 已提交
158

159 160 161
REGISTER_JITKERNEL_ARGS(lstm, LSTMKernel, JITKERNEL_DEFINE_NAME_LSTM,
                        JITKERNEL_DECLARE_LSTM, JITKERNEL_FIND_KEY_LSTM,
                        JITKERNEL_LSTM_IMPL);
T
tensor-tang 已提交
162

163 164 165 166 167
#undef JITKERNEL_LSTM_IMPL
#undef JITKERNEL_FIND_KEY_LSTM
#undef JITKERNEL_DECLARE_LSTM
#undef JITKERNEL_DEFINE_NAME_LSTM

T
tensor-tang 已提交
168
/* GRU JitKernel */
169
template <typename T>
T
tensor-tang 已提交
170 171
class GRUKernelImpl : public GRUKernel<T> {
 public:
172 173
  static inline std::string name(const gru_attr_t& attr) {
    PADDLE_THROW("DType should be either float or double");
T
tensor-tang 已提交
174
  }
175 176 177
  static inline bool useJIT(int d) { return false; }
  static inline bool useMKL(int d) { return false; }
  explicit GRUKernelImpl(const gru_attr_t& attr) : GRUKernel<T>() {
178 179
#ifdef PADDLE_WITH_XBYAK
    if (useJIT(attr.d)) {
180
      size_t sz = 96 + attr.d / YMM_FLOAT_BLOCK * 96 * 2 * 8;
181 182 183 184 185 186 187 188 189 190
      jitcode0_.reset(new gen::GRUJitCode(0, attr, sz > 4096 ? sz : 4096));
      this->ComputeH1 =
          jitcode0_->getCode<void (*)(gru_t*, const gru_attr_t*)>();

      jitcode1_.reset(new gen::GRUJitCode(1, attr, sz > 4096 ? sz : 4096));
      this->ComputeHtPart1 =
          jitcode1_->getCode<void (*)(gru_t*, const gru_attr_t*)>();

      jitcode2_.reset(new gen::GRUJitCode(2, attr, sz > 4096 ? sz : 4096));
      this->ComputeHtPart2 =
191
          jitcode2_->getCode<void (*)(gru_t*, const gru_attr_t*)>();
192 193 194
      return;
    }
#endif
195 196 197
    this->ComputeH1 = refer::GRUH1<T>;
    this->ComputeHtPart1 = refer::GRUHtPart1<T>;
    this->ComputeHtPart2 = refer::GRUHtPart2<T>;
T
tensor-tang 已提交
198
  }
199 200 201 202 203 204
#ifdef PADDLE_WITH_XBYAK

 private:
  std::unique_ptr<gen::GRUJitCode> jitcode0_{nullptr}, jitcode1_{nullptr},
      jitcode2_{nullptr};
#endif
T
tensor-tang 已提交
205 206
};

207 208 209
#ifdef PADDLE_WITH_XBYAK
template <>
bool GRUKernelImpl<float>::useJIT(int d) {
210
  return gen::GRUJitCode::init(d);
211 212 213
}
#endif

214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
#define JITKERNEL_DEFINE_NAME_GRU(ker_key, ker_class)                 \
  template <>                                                         \
  std::string ker_class##Impl<float>::name(const gru_attr_t& attr) {  \
    std::string key(#ker_key "f");                                    \
    key += (attr.act_gate + attr.act_cand);                           \
    if (useJIT(attr.d)) {                                             \
      /* only jit code need record d*/                                \
      return key + "jit" + std::to_string(attr.d);                    \
    } else if (useMKL(attr.d)) {                                      \
      return key + "mkl";                                             \
    } else {                                                          \
      return key + "any";                                             \
    }                                                                 \
  }                                                                   \
  template <>                                                         \
  std::string ker_class##Impl<double>::name(const gru_attr_t& attr) { \
    std::string key(#ker_key "d");                                    \
    /* jit code do not support double yet*/                           \
    if (useMKL(attr.d)) {                                             \
      return key + "mkl";                                             \
    } else {                                                          \
      return key + "any";                                             \
    }                                                                 \
  }

#define JITKERNEL_DECLARE_GRU(ker_class, ker_dtype)         \
  template <>                                               \
  std::shared_ptr<const ker_class<ker_dtype>>               \
  KernelPool::Get<ker_class<ker_dtype>, const gru_attr_t&>( \
      const gru_attr_t& attr)

#define JITKERNEL_FIND_KEY_GRU(ker_class, ker_dtype) \
  std::string key = ker_class##Impl<ker_dtype>::name(attr)
T
tensor-tang 已提交
247

248 249 250
#define JITKERNEL_GRU_IMPL(ker, dtype)       \
  p = std::dynamic_pointer_cast<ker<dtype>>( \
      std::make_shared<ker##Impl<dtype>>(attr));
T
tensor-tang 已提交
251

252 253 254
REGISTER_JITKERNEL_ARGS(gru, GRUKernel, JITKERNEL_DEFINE_NAME_GRU,
                        JITKERNEL_DECLARE_GRU, JITKERNEL_FIND_KEY_GRU,
                        JITKERNEL_GRU_IMPL);
T
tensor-tang 已提交
255

256 257
#undef JITKERNEL_GRU_IMPL
#undef JITKERNEL_FIND_KEY_GRU
T
tensor-tang 已提交
258
#undef JITKERNEL_DECLARE_GRU
259
#undef JITKERNEL_DEFINE_NAME_GRU
T
tensor-tang 已提交
260 261 262 263
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle