jit_kernel_lstm.cc 11.7 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 18
#include "paddle/fluid/operators/math/jit_kernel_macro.h"
#include "paddle/fluid/platform/enforce.h"
T
tensor-tang 已提交
19
#include "paddle/fluid/platform/macros.h"
T
tensor-tang 已提交
20 21 22 23

#ifdef __AVX__
#include <immintrin.h>
#endif
T
tensor-tang 已提交
24 25 26 27

namespace paddle {
namespace operators {
namespace math {
T
tensor-tang 已提交
28 29 30 31 32
#ifdef __AVX__
namespace detail {
__m256 Exp(__m256 a);
}  // namespace detail
#endif
T
tensor-tang 已提交
33

T
tensor-tang 已提交
34
namespace jitkernel {
T
tensor-tang 已提交
35 36
namespace jit = platform::jit;

T
tensor-tang 已提交
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 74 75 76 77 78 79 80 81 82 83 84
#ifdef __AVX__
typedef enum { kSigmoid, kRelu, kTanh, kIdentity } act_type;

class AVXAct {
 public:
  virtual ~AVXAct() = default;
  virtual __m256 Compute(__m256 x) const = 0;
};

template <act_type type>
class AVXActImpl : public AVXAct {
 public:
  __m256 Compute(__m256 x) const override { PADDLE_THROW("Unkown type!"); }
};

template <>
__m256 AVXActImpl<kSigmoid>::Compute(__m256 x) const {
  __m256 ones = _mm256_set1_ps(1.0f);
  x = _mm256_max_ps(x, _mm256_set1_ps(SIGMOID_THRESHOLD_MIN));
  x = _mm256_min_ps(x, _mm256_set1_ps(SIGMOID_THRESHOLD_MAX));
  x = _mm256_sub_ps(_mm256_set1_ps(0.0f), x);
  x = detail::Exp(x);
  x = _mm256_add_ps(ones, x);
  return _mm256_div_ps(ones, x);
}

template <>
__m256 AVXActImpl<kTanh>::Compute(__m256 x) const {
  __m256 ones = _mm256_set1_ps(1.0f);
  x = _mm256_mul_ps(_mm256_set1_ps(-2.0f), x);
  x = _mm256_min_ps(x, _mm256_set1_ps(EXP_MAX_INPUT));
  x = detail::Exp(x);
  x = _mm256_add_ps(ones, x);
  x = _mm256_div_ps(_mm256_set1_ps(2.0f), x);
  return _mm256_sub_ps(x, ones);
}

template <>
__m256 AVXActImpl<kRelu>::Compute(__m256 x) const {
  return _mm256_max_ps(x, _mm256_setzero_ps());
}

template <>
__m256 AVXActImpl<kIdentity>::Compute(__m256 x) const {
  return x;
}
#endif

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
template <typename T>
static std::shared_ptr<const VActKernel<T>> GetActKernel(
    const std::string& type, int n) {
  if (type == "sigmoid") {
    return std::dynamic_pointer_cast<const VActKernel<T>>(
        KernelPool::Instance().template Get<VSigmoidKernel<T>>(n));
  } else if (type == "relu") {
    return std::dynamic_pointer_cast<const VActKernel<T>>(
        KernelPool::Instance().template Get<VReluKernel<T>>(n));
  } else if (type == "tanh") {
    return std::dynamic_pointer_cast<const VActKernel<T>>(
        KernelPool::Instance().template Get<VTanhKernel<T>>(n));
  } else if (type == "identity" || type == "") {
    return std::dynamic_pointer_cast<const VActKernel<T>>(
        KernelPool::Instance().template Get<VIdentityKernel<T>>(n));
  }
  PADDLE_THROW("Not support type: %s", type);
  return nullptr;
}

T
tensor-tang 已提交
105 106 107 108
/* LSTM JitKernel */
template <typename T, jit::cpu_isa_t isa, jit_block>
class LSTMKernelImpl : public LSTMKernel<T> {
 public:
T
tensor-tang 已提交
109
  explicit LSTMKernelImpl(const std::string& act_gate,
T
tensor-tang 已提交
110
                          const std::string& act_cand,
T
tensor-tang 已提交
111
                          const std::string& act_cell, int d)
T
tensor-tang 已提交
112 113 114 115
      : LSTMKernel<T>() {
    d_ = d;
    d2_ = d * 2;
    d3_ = d * 3;
116 117 118 119
    act_gate_d3_ = GetActKernel<T>(act_gate, d3_);
    act_gate_d_ = GetActKernel<T>(act_gate, d);
    act_cand_d_ = GetActKernel<T>(act_cand, d);
    act_cell_d_ = GetActKernel<T>(act_cell, d);
T
tensor-tang 已提交
120 121
    vmul_d_ = KernelPool::Instance().template Get<VMulKernel<T>>(d);
    vadd_d_ = KernelPool::Instance().template Get<VAddKernel<T>>(d);
T
tensor-tang 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
#ifdef __AVX__
    auto GetAVXAct = [&](const std::string& type) -> std::unique_ptr<AVXAct> {
      if (type == "sigmoid") {
        return std::unique_ptr<AVXAct>(new AVXActImpl<kSigmoid>());
      } else if (type == "relu") {
        return std::unique_ptr<AVXAct>(new AVXActImpl<kRelu>());
      } else if (type == "tanh") {
        return std::unique_ptr<AVXAct>(new AVXActImpl<kTanh>());
      } else if (type == "identity" || type == "") {
        return std::unique_ptr<AVXAct>(new AVXActImpl<kIdentity>());
      }
      PADDLE_THROW("Not support type: %s", type);
    };
    avx_act_gate_ = GetAVXAct(act_gate);
    avx_act_cand_ = GetAVXAct(act_cand);
    avx_act_cell_ = GetAVXAct(act_cell);
#endif
T
tensor-tang 已提交
139 140
  }

141
  void ComputeCtHt(T* gates, const T* ct_1, T* ct, T* ht, const T* wp_data,
T
tensor-tang 已提交
142
                   T* checked) const override {
T
tensor-tang 已提交
143
    // gates: W_ch, W_ih, W_fh, W_oh
144
    act_gate_d3_->Compute(gates + d_, gates + d_);
T
tensor-tang 已提交
145 146 147 148 149 150 151 152 153 154

    /* C_t = C_t-1 * fgated + cand_gated * igated */
    act_cand_d_->Compute(gates, gates);
    vmul_d_->Compute(gates, gates + d_, gates + d_);
    vmul_d_->Compute(ct_1, gates + d2_, gates + d2_);
    vadd_d_->Compute(gates + d_, gates + d2_, ct);

    /* H_t = act_cell(C_t) * ogated */
    act_cell_d_->Compute(ct, gates + d2_);
    vmul_d_->Compute(gates + d2_, gates + d3_, ht);
T
tensor-tang 已提交
155
  }
156 157 158 159 160 161 162 163 164 165
  void ComputeC1H1(T* gates, T* ct, T* ht, const T* wp_data) const override {
    /* C_t = igated * cgated*/
    act_gate_d_->Compute(gates + d_, gates + d_);
    act_cand_d_->Compute(gates, gates);
    vmul_d_->Compute(gates, gates + d_, ct);
    /* H_t = act_cell(C_t) * ogated */
    act_gate_d_->Compute(gates + d3_, gates + d3_);
    act_cell_d_->Compute(ct, gates + d2_);
    vmul_d_->Compute(gates + d2_, gates + d3_, ht);
  }
T
tensor-tang 已提交
166 167 168

 private:
  int d_, d2_, d3_;
169 170
  std::shared_ptr<const VActKernel<T>> act_gate_d3_, act_gate_d_, act_cand_d_,
      act_cell_d_;
T
tensor-tang 已提交
171 172
  std::shared_ptr<const VMulKernel<T>> vmul_d_;
  std::shared_ptr<const VAddKernel<T>> vadd_d_;
T
tensor-tang 已提交
173 174 175
#ifdef __AVX__
  std::unique_ptr<const AVXAct> avx_act_gate_, avx_act_cand_, avx_act_cell_;
#endif
T
tensor-tang 已提交
176 177
};

T
tensor-tang 已提交
178 179 180
#define INTRI8_FLOAT(isa)                                                    \
  template <>                                                                \
  void LSTMKernelImpl<float, isa, kEQ8>::ComputeCtHt(                        \
181 182
      float* gates, const float* ct_1, float* ct, float* ht,                 \
      const float* wp_data, float* checked) const {                          \
T
tensor-tang 已提交
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
    /* gates: W_ch, W_ih, W_fh, W_oh */                                      \
    __m256 c, i, f, o;                                                       \
    c = _mm256_loadu_ps(gates);                                              \
    i = _mm256_loadu_ps(gates + 8);                                          \
    f = _mm256_loadu_ps(gates + 16);                                         \
    o = _mm256_loadu_ps(gates + 24);                                         \
    /* C_t = C_t-1 * fgated + cand_gated * igated*/                          \
    c = _mm256_mul_ps(avx_act_cand_->Compute(c), avx_act_gate_->Compute(i)); \
    i = _mm256_loadu_ps(ct_1);                                               \
    f = _mm256_mul_ps(i, avx_act_gate_->Compute(f));                         \
    f = _mm256_add_ps(c, f);                                                 \
    _mm256_storeu_ps(ct, f);                                                 \
    /* H_t = act_cell(C_t) * ogated */                                       \
    o = _mm256_mul_ps(avx_act_cell_->Compute(f), avx_act_gate_->Compute(o)); \
    _mm256_storeu_ps(ht, o);                                                 \
  }

// TODO(TJ): optimize keq16

#ifdef __AVX__
INTRI8_FLOAT(jit::avx);
#endif
#ifdef __AVX2__
INTRI8_FLOAT(jit::avx2);
#endif
#ifdef __AVX512F__
INTRI8_FLOAT(jit::avx512f);
#endif

T
tensor-tang 已提交
212 213 214 215 216 217 218 219 220 221 222
/* Peephole JitKernel */
template <typename T, jit::cpu_isa_t isa, jit_block>
class PeepholeKernelImpl : public LSTMKernel<T> {
 public:
  explicit PeepholeKernelImpl(const std::string& act_gate,
                              const std::string& act_cand,
                              const std::string& act_cell, int d)
      : LSTMKernel<T>() {
    d_ = d;
    d2_ = d * 2;
    d3_ = d * 3;
223 224 225
    act_gate_d_ = GetActKernel<T>(act_gate, d);
    act_cand_d_ = GetActKernel<T>(act_cand, d);
    act_cell_d_ = GetActKernel<T>(act_cell, d);
T
tensor-tang 已提交
226 227
    vmul_d_ = KernelPool::Instance().template Get<VMulKernel<T>>(d);
    vadd_d_ = KernelPool::Instance().template Get<VAddKernel<T>>(d);
228 229
    vadd_d2_ = KernelPool::Instance().template Get<VAddKernel<T>>(d2_);
    act_gate_d2_ = GetActKernel<T>(act_gate, d2_);
T
tensor-tang 已提交
230 231
  }

232
  void ComputeCtHt(T* gates, const T* ct_1, T* ct, T* ht, const T* wp_data,
T
tensor-tang 已提交
233
                   T* checked) const override {
234 235 236 237 238 239
    /* get fgated and igated*/
    vmul_d_->Compute(wp_data, ct_1, checked);
    vmul_d_->Compute(wp_data + d_, ct_1, checked + d_);
    vadd_d2_->Compute(checked, gates + d_, gates + d_);
    act_gate_d2_->Compute(gates + d_, gates + d_);
    /* C_t = C_t-1 * fgated + cand_gated * igated*/
T
tensor-tang 已提交
240 241 242 243
    act_cand_d_->Compute(gates, gates);
    vmul_d_->Compute(gates, gates + d_, gates + d_);
    vmul_d_->Compute(ct_1, gates + d2_, gates + d2_);
    vadd_d_->Compute(gates + d_, gates + d2_, ct);
244 245 246 247 248 249 250 251
    /* get ogated*/
    vmul_d_->Compute(wp_data + d2_, ct, gates + d_);
    vadd_d_->Compute(gates + d_, gates + d3_, gates + d3_);
    act_gate_d_->Compute(gates + d3_, gates + d3_);
    /* H_t = act_cell(C_t) * ogated */
    act_cell_d_->Compute(ct, gates + d2_);
    vmul_d_->Compute(gates + d2_, gates + d3_, ht);
  }
T
tensor-tang 已提交
252

253 254 255 256 257 258 259 260
  void ComputeC1H1(T* gates, T* ct, T* ht, const T* wp_data) const override {
    /* C_t = igated * cgated*/
    act_gate_d_->Compute(gates + d_, gates + d_);
    act_cand_d_->Compute(gates, gates);
    vmul_d_->Compute(gates, gates + d_, ct);
    /* get outgated, put W_oc * C_t on igated */
    vmul_d_->Compute(wp_data + d2_, ct, gates + d_);
    vadd_d_->Compute(gates + d_, gates + d3_, gates + d3_);
T
tensor-tang 已提交
261
    /* H_t = act_cell(C_t) * ogated */
262
    act_gate_d_->Compute(gates + d3_, gates + d3_);
T
tensor-tang 已提交
263 264 265
    act_cell_d_->Compute(ct, gates + d2_);
    vmul_d_->Compute(gates + d2_, gates + d3_, ht);
  }
T
tensor-tang 已提交
266

T
tensor-tang 已提交
267 268
 private:
  int d_, d2_, d3_;
269 270
  std::shared_ptr<const VActKernel<T>> act_gate_d2_, act_gate_d_, act_cand_d_,
      act_cell_d_;
T
tensor-tang 已提交
271
  std::shared_ptr<const VMulKernel<T>> vmul_d_;
272
  std::shared_ptr<const VAddKernel<T>> vadd_d_, vadd_d2_;
T
tensor-tang 已提交
273 274 275 276 277 278 279 280 281
};

#define JITKERNEL_DECLARE_LSTM(ker_class, ker_dtype)                  \
  template <>                                                         \
  std::shared_ptr<const LSTMKernel<ker_dtype>>                        \
  KernelPool::Get<LSTMKernel<ker_dtype>, const std::string&,          \
                  const std::string&, const std::string&, int, bool>( \
      const std::string& act_gate, const std::string& act_cand,       \
      const std::string& act_cell, int d, bool use_peephole)
T
tensor-tang 已提交
282

T
tensor-tang 已提交
283 284 285 286 287 288 289 290 291 292 293 294 295 296
#define JITKERNEL_KEY_LSTM(ker_key, dtype_key)                               \
  #ker_key #dtype_key + std::to_string(d) + act_gate + act_cand + act_cell + \
                                       (use_peephole ? "p" : "n")

#define JITKERNEL_NEW_LSTM_IMPL(ker, dtype, isa, k)                    \
  if (use_peephole) {                                                  \
    p = std::dynamic_pointer_cast<ker<dtype>>(                         \
        std::make_shared<PeepholeKernelImpl<dtype, isa, k>>(           \
            act_gate, act_cand, act_cell, d));                         \
  } else {                                                             \
    p = std::dynamic_pointer_cast<ker<dtype>>(                         \
        std::make_shared<ker##Impl<dtype, isa, k>>(act_gate, act_cand, \
                                                   act_cell, d));      \
  }
T
tensor-tang 已提交
297 298 299 300

REGISTER_JITKERNEL_ARGS(lstm, LSTMKernel, JITKERNEL_DECLARE_LSTM,
                        JITKERNEL_KEY_LSTM, JITKERNEL_NEW_LSTM_IMPL);

T
tensor-tang 已提交
301
#undef INTRI8_FLOAT
T
tensor-tang 已提交
302 303 304
#undef JITKERNEL_DECLARE_LSTM
#undef JITKERNEL_KEY_LSTM
#undef JITKERNEL_NEW_LSTM_IMPL
T
tensor-tang 已提交
305 306 307 308
}  // namespace jitkernel
}  // namespace math
}  // namespace operators
}  // namespace paddle