lstm_cpu_kernel.h 10.2 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.

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>
17
#include "paddle/operators/math/detail/activation_functions.h"
D
dangqingqing 已提交
18 19 20 21 22 23 24 25 26 27
#include "paddle/operators/math/lstm_compute.h"

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,
29 30 31 32
                                     int frameSize,
                                     activation_mode_t active_node,
                                     activation_mode_t active_gate,
                                     activation_mode_t active_state) {
D
dangqingqing 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  T rValueIn;
  T rValueIg;
  T rValueFg;
  T rValueOg;
  T rCheckI;
  T rCheckF;
  T rCheckO;
  T rState;
  T rPrevState = 0;
  T rStateAtv;
  T rOut;

  T *valueIn = value.gateValue;
  T *valueIg = value.gateValue + frameSize;
  T *valueFg = value.gateValue + frameSize * 2;
  T *valueOg = value.gateValue + frameSize * 3;

  for (int i = 0; i < frameSize; i++) {
    rValueIn = valueIn[i];
    rValueIg = valueIg[i];
    rValueFg = valueFg[i];
    rValueOg = valueOg[i];
D
dangqingqing 已提交
55 56 57
    rCheckI = value.checkIg ? value.checkIg[i] : 0;
    rCheckF = value.checkFg ? value.checkFg[i] : 0;
    rCheckO = value.checkOg ? value.checkOg[i] : 0;
D
dangqingqing 已提交
58 59 60 61 62 63

    if (value.prevStateValue) {
      rPrevState = value.prevStateValue[i];
    }

    op(rValueIn, rValueIg, rValueFg, rValueOg, rPrevState, rState, rStateAtv,
64
       rOut, rCheckI, rCheckF, rCheckO, active_node, active_gate, active_state);
D
dangqingqing 已提交
65 66 67 68 69 70 71 72 73 74 75 76

    valueIn[i] = rValueIn;
    valueIg[i] = rValueIg;
    valueFg[i] = rValueFg;
    valueOg[i] = rValueOg;
    value.stateValue[i] = rState;
    value.stateActiveValue[i] = rStateAtv;
    value.outputValue[i] = rOut;
  }
}

template <class T, class Op>
77
void naive_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
78 79 80 81
                                      LstmMetaGrad<T> grad, int frameSize,
                                      activation_mode_t active_node,
                                      activation_mode_t active_gate,
                                      activation_mode_t active_state) {
D
dangqingqing 已提交
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
  T rValueIn;
  T rValueIg;
  T rValueFg;
  T rValueOg;
  T rGradIn;
  T rGradIg;
  T rGradFg;
  T rGradOg;
  T rPrevState = 0;
  T rPrevStateGrad;
  T rState;
  T rStateGrad;
  T rStateAtv;
  T rOutputGrad;
  T rCheckI;
  T rCheckF;
  T rCheckO;
  T rCheckIGrad;
  T rCheckFGrad;
  T rCheckOGrad;

  T *valueIn = value.gateValue;
  T *valueIg = value.gateValue + frameSize;
  T *valueFg = value.gateValue + frameSize * 2;
  T *valueOg = value.gateValue + frameSize * 3;
  T *gradIn = grad.gateGrad;
  T *gradIg = grad.gateGrad + frameSize;
  T *gradFg = grad.gateGrad + frameSize * 2;
  T *gradOg = grad.gateGrad + frameSize * 3;

  for (int i = 0; i < frameSize; i++) {
    rValueIn = valueIn[i];
    rValueIg = valueIg[i];
    rValueFg = valueFg[i];
    rValueOg = valueOg[i];
D
dangqingqing 已提交
117 118 119
    rCheckI = value.checkIg ? value.checkIg[i] : 0;
    rCheckF = value.checkFg ? value.checkFg[i] : 0;
    rCheckO = value.checkOg ? value.checkOg[i] : 0;
D
dangqingqing 已提交
120 121 122 123 124 125 126 127 128 129 130
    rState = value.stateValue[i];
    rStateAtv = value.stateActiveValue[i];
    rOutputGrad = grad.outputGrad[i];
    rStateGrad = grad.stateGrad[i];
    if (value.prevStateValue) {
      rPrevState = value.prevStateValue[i];
    }

    op(rValueIn, rValueIg, rValueFg, rValueOg, rGradIn, rGradIg, rGradFg,
       rGradOg, rPrevState, rPrevStateGrad, rState, rStateGrad, rStateAtv,
       rOutputGrad, rCheckI, rCheckF, rCheckO, rCheckIGrad, rCheckFGrad,
131
       rCheckOGrad, active_node, active_gate, active_state);
D
dangqingqing 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147

    gradIn[i] = rGradIn;
    gradIg[i] = rGradIg;
    gradFg[i] = rGradFg;
    gradOg[i] = rGradOg;
    grad.stateGrad[i] = rStateGrad;

    if (grad.prevStateGrad) grad.prevStateGrad[i] = rPrevStateGrad;
    if (value.prevStateValue) {
      if (grad.checkIgGrad) grad.checkIgGrad[i] += rCheckIGrad;
      if (grad.checkFgGrad) grad.checkFgGrad[i] += rCheckFGrad;
    }
    if (grad.checkOgGrad) grad.checkOgGrad[i] += rCheckOGrad;
  }
}

148 149
template <class T, class Op>
void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value, int frameSize,
D
dangqingqing 已提交
150 151 152 153 154 155 156 157
                                   activation_mode_t active_node,
                                   activation_mode_t active_gate,
                                   activation_mode_t active_state) {
#ifdef __AVX__
  __m256 rValueIn;
  __m256 rValueIg;
  __m256 rValueFg;
  __m256 rValueOg;
D
dangqingqing 已提交
158 159 160
  __m256 rCheckI = _mm256_set1_ps(0.0f);
  __m256 rCheckF = _mm256_set1_ps(0.0f);
  __m256 rCheckO = _mm256_set1_ps(0.0f);
D
dangqingqing 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  __m256 rState;
  __m256 rPrevState = _mm256_set1_ps(0.0f);
  __m256 rStateAtv;
  __m256 rOut;

  __m256 *valueIn = (__m256 *)value.gateValue;
  __m256 *valueIg = (__m256 *)(value.gateValue + frameSize);
  __m256 *valueFg = (__m256 *)(value.gateValue + frameSize * 2);
  __m256 *valueOg = (__m256 *)(value.gateValue + frameSize * 3);

  for (int i = 0; i < frameSize / 8; i++) {
    rValueIn = valueIn[i];
    rValueIg = valueIg[i];
    rValueFg = valueFg[i];
    rValueOg = valueOg[i];
D
dangqingqing 已提交
176 177 178 179 180
    if (value.checkIg) {
      rCheckI = ((__m256 *)value.checkIg)[i];
      rCheckF = ((__m256 *)value.checkFg)[i];
      rCheckO = ((__m256 *)value.checkOg)[i];
    }
D
dangqingqing 已提交
181 182 183 184 185 186

    if (value.prevStateValue) {
      rPrevState = ((__m256 *)value.prevStateValue)[i];
    }

    op(rValueIn, rValueIg, rValueFg, rValueOg, rPrevState, rState, rStateAtv,
187
       rOut, rCheckI, rCheckF, rCheckO, active_node, active_gate, active_state);
D
dangqingqing 已提交
188 189 190 191 192 193 194 195 196 197 198 199

    valueIn[i] = rValueIn;
    valueIg[i] = rValueIg;
    valueFg[i] = rValueFg;
    valueOg[i] = rValueOg;
    ((__m256 *)value.stateValue)[i] = rState;
    ((__m256 *)value.stateActiveValue)[i] = rStateAtv;
    ((__m256 *)value.outputValue)[i] = rOut;
  }
#endif
}

200 201 202
template <class T, class Op>
void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
                                    LstmMetaGrad<T> grad, int frameSize,
D
dangqingqing 已提交
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
                                    activation_mode_t active_node,
                                    activation_mode_t active_gate,
                                    activation_mode_t active_state) {
#ifdef __AVX__
  __m256 rValueIn;
  __m256 rValueIg;
  __m256 rValueFg;
  __m256 rValueOg;
  __m256 rGradIn;
  __m256 rGradIg;
  __m256 rGradFg;
  __m256 rGradOg;
  __m256 rPrevState = _mm256_set1_ps(0.0f);
  __m256 rPrevStateGrad;
  __m256 rStateGrad;
  __m256 rState;
  __m256 rStateAtv;
  __m256 rOutputGrad;
D
dangqingqing 已提交
221 222 223
  __m256 rCheckI = _mm256_set1_ps(0.0f);
  __m256 rCheckF = _mm256_set1_ps(0.0f);
  __m256 rCheckO = _mm256_set1_ps(0.0f);
D
dangqingqing 已提交
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  __m256 rCheckIGrad;
  __m256 rCheckFGrad;
  __m256 rCheckOGrad;

  __m256 *valueIn = (__m256 *)value.gateValue;
  __m256 *valueIg = (__m256 *)(value.gateValue + frameSize);
  __m256 *valueFg = (__m256 *)(value.gateValue + frameSize * 2);
  __m256 *valueOg = (__m256 *)(value.gateValue + frameSize * 3);
  __m256 *gradIn = (__m256 *)grad.gateGrad;
  __m256 *gradIg = (__m256 *)(grad.gateGrad + frameSize);
  __m256 *gradFg = (__m256 *)(grad.gateGrad + frameSize * 2);
  __m256 *gradOg = (__m256 *)(grad.gateGrad + frameSize * 3);

  for (int i = 0; i < frameSize / 8; i++) {
    rValueIn = valueIn[i];
    rValueIg = valueIg[i];
    rValueFg = valueFg[i];
    rValueOg = valueOg[i];
D
dangqingqing 已提交
242 243 244 245 246
    if (value.checkIg) {
      rCheckI = ((__m256 *)value.checkIg)[i];
      rCheckF = ((__m256 *)value.checkFg)[i];
      rCheckO = ((__m256 *)value.checkOg)[i];
    }
D
dangqingqing 已提交
247 248 249 250 251 252 253 254 255 256 257
    rState = ((__m256 *)value.stateValue)[i];
    rStateAtv = ((__m256 *)value.stateActiveValue)[i];
    rOutputGrad = ((__m256 *)grad.outputGrad)[i];
    rStateGrad = ((__m256 *)grad.stateGrad)[i];
    if (value.prevStateValue) {
      rPrevState = ((__m256 *)value.prevStateValue)[i];
    }

    op(rValueIn, rValueIg, rValueFg, rValueOg, rGradIn, rGradIg, rGradFg,
       rGradOg, rPrevState, rPrevStateGrad, rState, rStateGrad, rStateAtv,
       rOutputGrad, rCheckI, rCheckF, rCheckO, rCheckIGrad, rCheckFGrad,
258
       rCheckOGrad, active_node, active_gate, active_state);
D
dangqingqing 已提交
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

    gradIn[i] = rGradIn;
    gradIg[i] = rGradIg;
    gradFg[i] = rGradFg;
    gradOg[i] = rGradOg;
    ((__m256 *)grad.stateGrad)[i] = rStateGrad;

    if (grad.prevStateGrad) ((__m256 *)grad.prevStateGrad)[i] = rPrevStateGrad;
    if (value.prevStateValue) {
      if (grad.checkIgGrad) ((__m256 *)grad.checkIgGrad)[i] += rCheckIGrad;
      if (grad.checkFgGrad) ((__m256 *)grad.checkFgGrad)[i] += rCheckFGrad;
    }
    if (grad.checkOgGrad) ((__m256 *)grad.checkOgGrad)[i] += rCheckOGrad;
  }
#endif
}

template <class T, class Op>
277
void cpu_lstm_forward(Op op, LstmMetaValue<T> value, int frameSize,
D
dangqingqing 已提交
278 279 280
                      activation_mode_t active_node,
                      activation_mode_t active_gate,
                      activation_mode_t active_state) {
281 282 283
  if (Op::avx && !(frameSize & (8 - 1)) && (std::is_same<T, float>::value)) {
    avx_lstm_forward_one_sequence<T>(op, value, frameSize, active_node,
                                     active_gate, active_state);
D
dangqingqing 已提交
284
  } else {
285 286
    naive_lstm_forward_one_sequence<T>(op, value, frameSize, active_node,
                                       active_gate, active_state);
D
dangqingqing 已提交
287 288 289 290
  }
}

template <class T, class Op>
291 292
void cpu_lstm_backward(Op op, LstmMetaValue<T> value, LstmMetaGrad<T> grad,
                       int frameSize, activation_mode_t active_node,
D
dangqingqing 已提交
293 294
                       activation_mode_t active_gate,
                       activation_mode_t active_state) {
295 296 297
  if (Op::avx && !(frameSize & (8 - 1)) && (std::is_same<T, float>::value)) {
    avx_lstm_backward_one_sequence<T>(op, value, grad, frameSize, active_node,
                                      active_gate, active_state);
D
dangqingqing 已提交
298
  } else {
299 300
    naive_lstm_backward_one_sequence<T>(op, value, grad, frameSize, active_node,
                                        active_gate, active_state);
D
dangqingqing 已提交
301 302 303 304 305 306 307 308 309
  }
}

#endif

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