lstm_kernel.h 5.9 KB
Newer Older
D
dangqingqing 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/operators/math/detail/activation_functions.h"
Y
Yu Yang 已提交
16
#include "paddle/platform/hostdevice.h"
D
dangqingqing 已提交
17

Y
Yu Yang 已提交
18
#include <type_traits>
D
dangqingqing 已提交
19 20 21 22 23 24 25 26 27 28 29

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

namespace forward {

template <class T>
class lstm {
 public:
Y
Yu Yang 已提交
30 31
  HOSTDEVICE void operator()(T &valueIn, T &valueIg, T &valueFg, T &valueOg,
                             T &prevState, T &state, T &stateAtv, T &output,
32 33 34 35 36 37 38
                             T &checkI, T &checkF, T &checkO,
                             activation_mode_t active_node,
                             activation_mode_t active_gate,
                             activation_mode_t active_state) {
    valueIn = activation(valueIn, active_node);
    valueIg = activation(valueIg + prevState * checkI, active_gate);
    valueFg = activation(valueFg + prevState * checkF, active_gate);
39
    state = valueIn * valueIg + prevState * valueFg;
40 41
    valueOg = activation(valueOg + state * checkO, active_gate);
    stateAtv = activation(state, active_state);
42
    output = valueOg * stateAtv;
D
dangqingqing 已提交
43 44
  }
#ifndef __NVCC__
Y
Yu Yang 已提交
45
#ifndef __AVX__  // If not compiled with AVX instructs. Disable AVX by default
D
dangqingqing 已提交
46 47
  static const bool avx = false;
#else
Y
Yu Yang 已提交
48 49 50 51 52 53 54
  // Only float support AVX optimization
  static const bool avx = std::is_same<T, float>::value;

  HOSTDEVICE void operator()(__m256 &valueIn, __m256 &valueIg, __m256 &valueFg,
                             __m256 &valueOg, __m256 &prevState, __m256 &state,
                             __m256 &stateAtv, __m256 &output, __m256 &checkI,
                             __m256 &checkF, __m256 &checkO,
55 56 57 58 59 60 61 62
                             activation_mode_t active_node,
                             activation_mode_t active_gate,
                             activation_mode_t active_state) {
    valueIn = activation(valueIn, active_node);
    valueIg = activation(
        _mm256_add_ps(valueIg, _mm256_mul_ps(prevState, checkI)), active_gate);
    valueFg = activation(
        _mm256_add_ps(valueFg, _mm256_mul_ps(prevState, checkF)), active_gate);
D
dangqingqing 已提交
63 64
    state = _mm256_add_ps(_mm256_mul_ps(valueIn, valueIg),
                          _mm256_mul_ps(prevState, valueFg));
65 66 67
    valueOg = activation(_mm256_add_ps(valueOg, _mm256_mul_ps(state, checkO)),
                         active_gate);
    stateAtv = activation(state, active_state);
D
dangqingqing 已提交
68 69 70 71 72 73 74 75 76 77 78 79 80
    output = _mm256_mul_ps(valueOg, stateAtv);
  }
#endif
#endif
};

}  // namespace forward

namespace backward {

template <class T>
class lstm {
 public:
Y
Yu Yang 已提交
81 82 83 84 85
  HOSTDEVICE void operator()(T &valueIn, T &valueIg, T &valueFg, T &valueOg,
                             T &gradIn, T &gradIg, T &gradFg, T &gradOg,
                             T &prevState, T &prevStateGrad, T &state,
                             T &stateGrad, T &stateAtv, T &outputGrad,
                             T &checkI, T &checkF, T &checkO, T &checkIGrad,
86 87 88 89 90 91 92 93 94 95
                             T &checkFGrad, T &checkOGrad,
                             activation_mode_t active_node,
                             activation_mode_t active_gate,
                             activation_mode_t active_state) {
    gradOg = activation(outputGrad * stateAtv, valueOg, active_gate);
    stateGrad += activation(outputGrad * valueOg, stateAtv, active_state) +
                 gradOg * checkO;
    gradIn = activation(stateGrad * valueIg, valueIn, active_node);
    gradIg = activation(stateGrad * valueIn, valueIg, active_gate);
    gradFg = activation(stateGrad * prevState, valueFg, active_gate);
D
dangqingqing 已提交
96 97 98 99 100 101
    prevStateGrad = gradIg * checkI + gradFg * checkF + stateGrad * valueFg;
    checkIGrad = gradIg * prevState;
    checkFGrad = gradFg * prevState;
    checkOGrad = gradOg * state;
  }
#ifndef __NVCC__
Y
Yu Yang 已提交
102
#ifndef __AVX__  // If not compiled with AVX instructs. Disable AVX by default
D
dangqingqing 已提交
103 104
  static const bool avx = false;
#else
Y
Yu Yang 已提交
105 106
  // Only float support AVX optimization
  static const bool avx = std::is_same<T, float>::value;
107 108 109 110 111 112 113 114 115 116
  HOSTDEVICE void operator()(
      __m256 &valueIn, __m256 &valueIg, __m256 &valueFg, __m256 &valueOg,
      __m256 &gradIn, __m256 &gradIg, __m256 &gradFg, __m256 &gradOg,
      __m256 &prevState, __m256 &prevStateGrad, __m256 &state,
      __m256 &stateGrad, __m256 &stateAtv, __m256 &outputGrad, __m256 &checkI,
      __m256 &checkF, __m256 &checkO, __m256 &checkIGrad, __m256 &checkFGrad,
      __m256 &checkOGrad, activation_mode_t active_node,
      activation_mode_t active_gate, activation_mode_t active_state) {
    gradOg =
        activation(_mm256_mul_ps(outputGrad, stateAtv), valueOg, active_gate);
D
dangqingqing 已提交
117
    stateGrad = _mm256_add_ps(
118 119
        activation(_mm256_mul_ps(outputGrad, valueOg), stateAtv, active_state),
        stateGrad);
D
dangqingqing 已提交
120
    stateGrad = _mm256_add_ps(_mm256_mul_ps(gradOg, checkO), stateGrad);
121 122 123 124 125 126
    gradIn =
        activation(_mm256_mul_ps(stateGrad, valueIg), valueIn, active_node);
    gradIg =
        activation(_mm256_mul_ps(stateGrad, valueIn), valueIg, active_gate);
    gradFg =
        activation(_mm256_mul_ps(stateGrad, prevState), valueFg, active_gate);
D
dangqingqing 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    prevStateGrad = _mm256_add_ps(_mm256_mul_ps(gradIg, checkI),
                                  _mm256_mul_ps(gradFg, checkF));
    prevStateGrad =
        _mm256_add_ps(_mm256_mul_ps(stateGrad, valueFg), prevStateGrad);
    checkIGrad = _mm256_mul_ps(gradIg, prevState);
    checkFGrad = _mm256_mul_ps(gradFg, prevState);
    checkOGrad = _mm256_mul_ps(gradOg, state);
  }
#endif
#endif
};

}  // namespace backward

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