lstm_cpu_kernel.h 11.2 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
D
dangqingqing 已提交
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 <type_traits>
Y
Yi Wang 已提交
17 18
#include "paddle/fluid/operators/math/detail/activation_functions.h"
#include "paddle/fluid/operators/math/lstm_compute.h"
D
dangqingqing 已提交
19 20 21 22 23 24 25 26 27

namespace paddle {
namespace operators {
namespace math {
namespace detail {

#ifndef __NVCC__

template <class T, class Op>
28
void naive_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value,
D
dangqingqing 已提交
29
                                     int frame_size, ActivationType active_node,
30 31
                                     ActivationType active_gate,
                                     ActivationType active_state) {
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
  T r_value_in;
  T r_value_ig;
  T r_value_fg;
  T r_value_og;
  T r_checkI;
  T r_checkF;
  T r_checkO;
  T r_state;
  T r_prev_state = 0;
  T r_state_atv;
  T r_out;

  T *value_in = value.gate_value;
  T *value_ig = value.gate_value + frame_size;
  T *value_fg = value.gate_value + frame_size * 2;
  T *value_og = value.gate_value + frame_size * 3;

  for (int i = 0; i < frame_size; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    r_checkI = value.check_ig ? value.check_ig[i] : 0;
    r_checkF = value.check_fg ? value.check_fg[i] : 0;
    r_checkO = value.check_og ? value.check_og[i] : 0;

    if (value.prev_state_value) {
      r_prev_state = value.prev_state_value[i];
D
dangqingqing 已提交
60 61
    }

62 63 64 65 66 67 68 69 70 71 72
    op(r_value_in, r_value_ig, r_value_fg, r_value_og, r_prev_state, r_state,
       r_state_atv, r_out, r_checkI, r_checkF, r_checkO, active_node,
       active_gate, active_state);

    value_in[i] = r_value_in;
    value_ig[i] = r_value_ig;
    value_fg[i] = r_value_fg;
    value_og[i] = r_value_og;
    value.state_value[i] = r_state;
    value.state_active_value[i] = r_state_atv;
    value.output_value[i] = r_out;
D
dangqingqing 已提交
73 74 75 76
  }
}

template <class T, class Op>
77
void naive_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
78
                                      LstmMetaGrad<T> grad, int frame_size,
79 80 81
                                      ActivationType active_node,
                                      ActivationType active_gate,
                                      ActivationType active_state) {
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  T r_value_in;
  T r_value_ig;
  T r_value_fg;
  T r_value_og;
  T r_grad_in;
  T r_grad_ig;
  T r_grad_fg;
  T r_grad_og;
  T r_prev_state = 0;
  T r_prev_state_grad;
  T r_state;
  T r_state_grad;
  T r_state_atv;
  T r_output_grad;
  T r_checkI;
  T r_checkF;
  T r_checkO;
  T r_checkIGrad;
  T r_checkFGrad;
  T r_checkOGrad;

  T *value_in = value.gate_value;
  T *value_ig = value.gate_value + frame_size;
  T *value_fg = value.gate_value + frame_size * 2;
  T *value_og = value.gate_value + frame_size * 3;
  T *grad_in = grad.gate_grad;
  T *grad_ig = grad.gate_grad + frame_size;
  T *grad_fg = grad.gate_grad + frame_size * 2;
  T *grad_og = grad.gate_grad + frame_size * 3;

  for (int i = 0; i < frame_size; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    r_checkI = value.check_ig ? value.check_ig[i] : 0;
    r_checkF = value.check_fg ? value.check_fg[i] : 0;
    r_checkO = value.check_og ? value.check_og[i] : 0;
    r_state = value.state_value[i];
    r_state_atv = value.state_active_value[i];
    r_output_grad = grad.output_grad[i];
    r_state_grad = grad.state_grad[i];
    if (value.prev_state_value) {
      r_prev_state = value.prev_state_value[i];
D
dangqingqing 已提交
126 127
    }

128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    op(r_value_in, r_value_ig, r_value_fg, r_value_og, r_grad_in, r_grad_ig,
       r_grad_fg, r_grad_og, r_prev_state, r_prev_state_grad, r_state,
       r_state_grad, r_state_atv, r_output_grad, r_checkI, r_checkF, r_checkO,
       r_checkIGrad, r_checkFGrad, r_checkOGrad, active_node, active_gate,
       active_state);

    grad_in[i] = r_grad_in;
    grad_ig[i] = r_grad_ig;
    grad_fg[i] = r_grad_fg;
    grad_og[i] = r_grad_og;
    grad.state_grad[i] = r_state_grad;

    if (grad.prev_state_grad) grad.prev_state_grad[i] = r_prev_state_grad;
    if (value.prev_state_value) {
      if (grad.check_ig_grad) grad.check_ig_grad[i] += r_checkIGrad;
      if (grad.check_fg_grad) grad.check_fg_grad[i] += r_checkFGrad;
D
dangqingqing 已提交
144
    }
145
    if (grad.check_og_grad) grad.check_og_grad[i] += r_checkOGrad;
D
dangqingqing 已提交
146 147 148
  }
}

149
template <class T, class Op>
150
void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value,
D
dangqingqing 已提交
151
                                   int frame_size, ActivationType active_node,
152 153
                                   ActivationType active_gate,
                                   ActivationType active_state) {
D
dangqingqing 已提交
154
#ifdef __AVX__
155 156 157 158 159 160 161 162 163 164 165 166
  __m256 r_value_in;
  __m256 r_value_ig;
  __m256 r_value_fg;
  __m256 r_value_og;
  __m256 r_checkI = _mm256_set1_ps(0.0f);
  __m256 r_checkF = _mm256_set1_ps(0.0f);
  __m256 r_checkO = _mm256_set1_ps(0.0f);
  __m256 r_state;
  __m256 r_prev_state = _mm256_set1_ps(0.0f);
  __m256 r_state_atv;
  __m256 r_out;

167 168 169 170 171 172
  __m256 *value_in = reinterpret_cast<__m256 *>(value.gate_value);
  __m256 *value_ig = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
  __m256 *value_fg =
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
  __m256 *value_og =
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 3);
173 174 175 176 177 178 179

  for (int i = 0; i < frame_size / 8; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    if (value.check_ig) {
180 181 182
      r_checkI = (reinterpret_cast<__m256 *>(value.check_ig))[i];
      r_checkF = (reinterpret_cast<__m256 *>(value.check_fg))[i];
      r_checkO = (reinterpret_cast<__m256 *>(value.check_og))[i];
D
dangqingqing 已提交
183
    }
D
dangqingqing 已提交
184

185
    if (value.prev_state_value) {
186
      r_prev_state = (reinterpret_cast<__m256 *>(value.prev_state_value))[i];
D
dangqingqing 已提交
187 188
    }

189 190 191 192 193 194 195 196
    op(r_value_in, r_value_ig, r_value_fg, r_value_og, r_prev_state, r_state,
       r_state_atv, r_out, r_checkI, r_checkF, r_checkO, active_node,
       active_gate, active_state);

    value_in[i] = r_value_in;
    value_ig[i] = r_value_ig;
    value_fg[i] = r_value_fg;
    value_og[i] = r_value_og;
197 198 199
    (reinterpret_cast<__m256 *>(value.state_value))[i] = r_state;
    (reinterpret_cast<__m256 *>(value.state_active_value))[i] = r_state_atv;
    (reinterpret_cast<__m256 *>(value.output_value))[i] = r_out;
D
dangqingqing 已提交
200 201 202 203
  }
#endif
}

204 205
template <class T, class Op>
void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
206
                                    LstmMetaGrad<T> grad, int frame_size,
207 208 209
                                    ActivationType active_node,
                                    ActivationType active_gate,
                                    ActivationType active_state) {
D
dangqingqing 已提交
210
#ifdef __AVX__
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
  __m256 r_value_in;
  __m256 r_value_ig;
  __m256 r_value_fg;
  __m256 r_value_og;
  __m256 r_grad_in;
  __m256 r_grad_ig;
  __m256 r_grad_fg;
  __m256 r_grad_og;
  __m256 r_prev_state = _mm256_set1_ps(0.0f);
  __m256 r_prev_state_grad;
  __m256 r_state_grad;
  __m256 r_state;
  __m256 r_state_atv;
  __m256 r_output_grad;
  __m256 r_checkI = _mm256_set1_ps(0.0f);
  __m256 r_checkF = _mm256_set1_ps(0.0f);
  __m256 r_checkO = _mm256_set1_ps(0.0f);
  __m256 r_checkIGrad;
  __m256 r_checkFGrad;
  __m256 r_checkOGrad;

232 233 234 235 236 237 238 239 240 241
  __m256 *value_in = reinterpret_cast<__m256 *>(value.gate_value);
  __m256 *value_ig = reinterpret_cast<__m256 *>(value.gate_value + frame_size);
  __m256 *value_fg =
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 2);
  __m256 *value_og =
      reinterpret_cast<__m256 *>(value.gate_value + frame_size * 3);
  __m256 *grad_in = reinterpret_cast<__m256 *>(grad.gate_grad);
  __m256 *grad_ig = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size);
  __m256 *grad_fg = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 2);
  __m256 *grad_og = reinterpret_cast<__m256 *>(grad.gate_grad + frame_size * 3);
242 243 244 245 246 247 248

  for (int i = 0; i < frame_size / 8; i++) {
    r_value_in = value_in[i];
    r_value_ig = value_ig[i];
    r_value_fg = value_fg[i];
    r_value_og = value_og[i];
    if (value.check_ig) {
249 250 251
      r_checkI = (reinterpret_cast<__m256 *>(value.check_ig))[i];
      r_checkF = (reinterpret_cast<__m256 *>(value.check_fg))[i];
      r_checkO = (reinterpret_cast<__m256 *>(value.check_og))[i];
D
dangqingqing 已提交
252
    }
253 254 255 256
    r_state = (reinterpret_cast<__m256 *>(value.state_value))[i];
    r_state_atv = (reinterpret_cast<__m256 *>(value.state_active_value))[i];
    r_output_grad = (reinterpret_cast<__m256 *>(grad.output_grad))[i];
    r_state_grad = (reinterpret_cast<__m256 *>(grad.state_grad))[i];
257
    if (value.prev_state_value) {
258
      r_prev_state = (reinterpret_cast<__m256 *>(value.prev_state_value))[i];
D
dangqingqing 已提交
259 260
    }

261 262 263 264 265 266 267 268 269 270
    op(r_value_in, r_value_ig, r_value_fg, r_value_og, r_grad_in, r_grad_ig,
       r_grad_fg, r_grad_og, r_prev_state, r_prev_state_grad, r_state,
       r_state_grad, r_state_atv, r_output_grad, r_checkI, r_checkF, r_checkO,
       r_checkIGrad, r_checkFGrad, r_checkOGrad, active_node, active_gate,
       active_state);

    grad_in[i] = r_grad_in;
    grad_ig[i] = r_grad_ig;
    grad_fg[i] = r_grad_fg;
    grad_og[i] = r_grad_og;
271
    (reinterpret_cast<__m256 *>(grad.state_grad))[i] = r_state_grad;
272 273

    if (grad.prev_state_grad)
274
      (reinterpret_cast<__m256 *>(grad.prev_state_grad))[i] = r_prev_state_grad;
275
    if (value.prev_state_value) {
276 277 278 279
      if (grad.check_ig_grad)
        (reinterpret_cast<__m256 *>(grad.check_ig_grad))[i] += r_checkIGrad;
      if (grad.check_fg_grad)
        (reinterpret_cast<__m256 *>(grad.check_fg_grad))[i] += r_checkFGrad;
D
dangqingqing 已提交
280
    }
281 282
    if (grad.check_og_grad)
      (reinterpret_cast<__m256 *>(grad.check_og_grad))[i] += r_checkOGrad;
D
dangqingqing 已提交
283 284 285 286 287
  }
#endif
}

template <class T, class Op>
288
void cpu_lstm_forward(Op op, LstmMetaValue<T> value, int frame_size,
D
dangqingqing 已提交
289
                      ActivationType active_node, ActivationType active_gate,
290
                      ActivationType active_state) {
291 292
  if (Op::avx && !(frame_size & (8 - 1)) && (std::is_same<T, float>::value)) {
    avx_lstm_forward_one_sequence<T>(op, value, frame_size, active_node,
293
                                     active_gate, active_state);
D
dangqingqing 已提交
294
  } else {
295
    naive_lstm_forward_one_sequence<T>(op, value, frame_size, active_node,
296
                                       active_gate, active_state);
D
dangqingqing 已提交
297 298 299 300
  }
}

template <class T, class Op>
301
void cpu_lstm_backward(Op op, LstmMetaValue<T> value, LstmMetaGrad<T> grad,
302 303 304
                       int frame_size, ActivationType active_node,
                       ActivationType active_gate,
                       ActivationType active_state) {
305 306
  if (Op::avx && !(frame_size & (8 - 1)) && (std::is_same<T, float>::value)) {
    avx_lstm_backward_one_sequence<T>(op, value, grad, frame_size, active_node,
307
                                      active_gate, active_state);
D
dangqingqing 已提交
308
  } else {
309 310
    naive_lstm_backward_one_sequence<T>(op, value, grad, frame_size,
                                        active_node, active_gate, active_state);
D
dangqingqing 已提交
311 312 313 314 315 316 317 318 319
  }
}

#endif

}  // namespace detail
}  // namespace math
}  // namespace operators
}  // namespace paddle