lstm_cpu_kernel.h 10.1 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 17
#include <type_traits>
#include "paddle/operators/math/detail/hl_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 29
void naive_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value,
                                     int frameSize,
D
dangqingqing 已提交
30 31 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 60 61 62
                                     activation_mode_t active_node,
                                     activation_mode_t active_gate,
                                     activation_mode_t active_state) {
  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];
    rCheckI = value.checkIg[i];
    rCheckF = value.checkFg[i];
    rCheckO = value.checkOg[i];

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

63
    hppl::cpu::ForwardAct<T> act;
D
dangqingqing 已提交
64
    op(rValueIn, rValueIg, rValueFg, rValueOg, rPrevState, rState, rStateAtv,
65 66
       rOut, rCheckI, rCheckF, rCheckO, act(active_node), act(active_gate),
       act(active_state));
D
dangqingqing 已提交
67 68 69 70 71 72 73 74 75 76 77 78

    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>
79 80
void naive_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
                                      LstmMetaGrad<T> grad, int frameSize,
D
dangqingqing 已提交
81 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 126 127 128 129
                                      activation_mode_t active_node,
                                      activation_mode_t active_gate,
                                      activation_mode_t active_state) {
  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];
    rCheckI = value.checkIg[i];
    rCheckF = value.checkFg[i];
    rCheckO = value.checkOg[i];
    rState = value.stateValue[i];
    rStateAtv = value.stateActiveValue[i];
    rOutputGrad = grad.outputGrad[i];
    rStateGrad = grad.stateGrad[i];
    if (value.prevStateValue) {
      rPrevState = value.prevStateValue[i];
    }

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

    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;
  }
}

151 152
template <class T, class Op>
void avx_lstm_forward_one_sequence(Op op, LstmMetaValue<T> value, int frameSize,
D
dangqingqing 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
                                   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 rCheckI;
  __m256 rCheckF;
  __m256 rCheckO;
  __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];
    rCheckI = ((__m256 *)value.checkIg)[i];
    rCheckF = ((__m256 *)value.checkFg)[i];
    rCheckO = ((__m256 *)value.checkOg)[i];

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

    op(rValueIn, rValueIg, rValueFg, rValueOg, rPrevState, rState, rStateAtv,
       rOut, rCheckI, rCheckF, rCheckO, hppl::avx::forward[active_node],
       hppl::avx::forward[active_gate], hppl::avx::forward[active_state]);

    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
}

202 203 204
template <class T, class Op>
void avx_lstm_backward_one_sequence(Op op, LstmMetaValue<T> value,
                                    LstmMetaGrad<T> grad, int frameSize,
D
dangqingqing 已提交
205 206 207 208 209 210 211 212 213 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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                                    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;
  __m256 rCheckI;
  __m256 rCheckF;
  __m256 rCheckO;
  __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];
    rCheckI = ((__m256 *)value.checkIg)[i];
    rCheckF = ((__m256 *)value.checkFg)[i];
    rCheckO = ((__m256 *)value.checkOg)[i];
    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,
       rCheckOGrad, hppl::avx::backward[active_node],
       hppl::avx::backward[active_gate], hppl::avx::backward[active_state]);

    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>
278
void cpu_lstm_forward(Op op, LstmMetaValue<T> value, int frameSize,
D
dangqingqing 已提交
279 280 281
                      activation_mode_t active_node,
                      activation_mode_t active_gate,
                      activation_mode_t active_state) {
282 283 284
  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 已提交
285 286 287 288 289 290 291
  } else {
    naive_lstm_forward_one_sequence<T>(op, value, frameSize, active_node,
                                       active_gate, active_state);
  }
}

template <class T, class Op>
292 293
void cpu_lstm_backward(Op op, LstmMetaValue<T> value, LstmMetaGrad<T> grad,
                       int frameSize, activation_mode_t active_node,
D
dangqingqing 已提交
294 295
                       activation_mode_t active_gate,
                       activation_mode_t active_state) {
296 297 298
  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 已提交
299 300 301 302 303 304 305 306 307 308 309 310
  } else {
    naive_lstm_backward_one_sequence<T>(op, value, grad, frameSize, active_node,
                                        active_gate, active_state);
  }
}

#endif

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