lstm_kernel.h 7.5 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

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 16
#pragma once
#include <type_traits>
Y
Yi Wang 已提交
17 18
#include "paddle/fluid/operators/math/detail/activation_functions.h"
#include "paddle/fluid/platform/hostdevice.h"
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:
30 31
  HOSTDEVICE void operator()(T *value_in, T *value_ig, T *value_fg, T *value_og,
                             T *prev_state, T *state, T *state_atv, T *output,
32
                             T *checkI, T *checkF, T *checkO, T *cell_clip,
33 34 35
                             ActivationType active_node,
                             ActivationType active_gate,
                             ActivationType active_state) {
36 37 38 39
    *value_in = activation(*value_in, active_node);
    *value_ig = activation(*value_ig + (*prev_state) * (*checkI), active_gate);
    *value_fg = activation(*value_fg + (*prev_state) * (*checkF), active_gate);
    *state = (*value_in) * (*value_ig) + (*prev_state) * (*value_fg);
X
xuezhong 已提交
40

41 42 43 44 45 46 47 48
    if (*cell_clip > 0.0) {
      if (*state < -1.0 * (*cell_clip)) {
        *state = -1.0 * (*cell_clip);
      }
      if (*state > *cell_clip) {
        *state = *cell_clip;
      }
    }
49 50 51
    *value_og = activation(*value_og + (*state) * (*checkO), active_gate);
    *state_atv = activation(*state, active_state);
    *output = (*value_og) * (*state_atv);
D
dangqingqing 已提交
52 53
  }
#ifndef __NVCC__
Y
Yu Yang 已提交
54
#ifndef __AVX__  // If not compiled with AVX instructs. Disable AVX by default
D
dangqingqing 已提交
55 56
  static const bool avx = false;
#else
Y
Yu Yang 已提交
57 58 59
  // Only float support AVX optimization
  static const bool avx = std::is_same<T, float>::value;

60 61 62 63
  HOSTDEVICE void operator()(__m256 *value_in, __m256 *value_ig,
                             __m256 *value_fg, __m256 *value_og,
                             __m256 *prev_state, __m256 *state,
                             __m256 *state_atv, __m256 *output, __m256 *checkI,
64
                             __m256 *checkF, __m256 *checkO, T *cell_clip,
65 66 67
                             ActivationType active_node,
                             ActivationType active_gate,
                             ActivationType active_state) {
68 69 70 71 72 73 74 75 76
    *value_in = activation(*value_in, active_node);
    *value_ig = activation(
        _mm256_add_ps(*value_ig, _mm256_mul_ps(*prev_state, *checkI)),
        active_gate);
    *value_fg = activation(
        _mm256_add_ps(*value_fg, _mm256_mul_ps(*prev_state, *checkF)),
        active_gate);
    *state = _mm256_add_ps(_mm256_mul_ps(*value_in, *value_ig),
                           _mm256_mul_ps(*prev_state, *value_fg));
X
xuezhong 已提交
77

78 79 80 81 82 83
    if (*cell_clip > 0.0f) {
      __m256 min = _mm256_set1_ps(0.0f - *cell_clip);
      __m256 max = _mm256_set1_ps(*cell_clip);
      *state = _mm256_min_ps(max, *state);
      *state = _mm256_max_ps(min, *state);
    }
84 85 86 87
    *value_og = activation(
        _mm256_add_ps(*value_og, _mm256_mul_ps(*state, *checkO)), active_gate);
    *state_atv = activation(*state, active_state);
    *output = _mm256_mul_ps(*value_og, *state_atv);
D
dangqingqing 已提交
88 89 90 91 92 93 94 95 96 97 98 99
  }
#endif
#endif
};

}  // namespace forward

namespace backward {

template <class T>
class lstm {
 public:
100 101 102 103 104
  HOSTDEVICE void operator()(T *value_in, T *value_ig, T *value_fg, T *value_og,
                             T *grad_in, T *grad_ig, T *grad_fg, T *grad_og,
                             T *prev_state, T *prev_state_grad, T *state,
                             T *state_grad, T *state_atv, T *output_grad,
                             T *checkI, T *checkF, T *checkO, T *checkIGrad,
105
                             T *checkFGrad, T *checkOGrad, T *cell_clip,
106 107 108
                             ActivationType active_node,
                             ActivationType active_gate,
                             ActivationType active_state) {
109 110
    *grad_og =
        activation((*output_grad) * (*state_atv), *value_og, active_gate);
111 112 113 114 115 116 117 118
    if (*cell_clip > 0.0f) {
      if (*state >= (*cell_clip) || *state <= (0.0f - (*cell_clip))) {
        *state_grad = 0.0f;
      } else {
        *state_grad +=
            activation((*output_grad) * (*value_og), *state_atv, active_state) +
            (*grad_og) * (*checkO);
      }
X
xuezhong 已提交
119 120 121 122
    } else {
      *state_grad +=
          activation((*output_grad) * (*value_og), *state_atv, active_state) +
          (*grad_og) * (*checkO);
123
    }
X
xuezhong 已提交
124

125 126 127 128 129 130 131 132 133
    *grad_in = activation((*state_grad) * (*value_ig), *value_in, active_node);
    *grad_ig = activation((*state_grad) * (*value_in), *value_ig, active_gate);
    *grad_fg =
        activation((*state_grad) * (*prev_state), *value_fg, active_gate);
    *prev_state_grad = (*grad_ig) * (*checkI) + (*grad_fg) * (*checkF) +
                       (*state_grad) * (*value_fg);
    *checkIGrad = (*grad_ig) * (*prev_state);
    *checkFGrad = (*grad_fg) * (*prev_state);
    *checkOGrad = (*grad_og) * (*state);
D
dangqingqing 已提交
134 135
  }
#ifndef __NVCC__
Y
Yu Yang 已提交
136
#ifndef __AVX__  // If not compiled with AVX instructs. Disable AVX by default
D
dangqingqing 已提交
137 138
  static const bool avx = false;
#else
Y
Yu Yang 已提交
139 140
  // Only float support AVX optimization
  static const bool avx = std::is_same<T, float>::value;
141
  HOSTDEVICE void operator()(
142 143 144 145 146
      __m256 *value_in, __m256 *value_ig, __m256 *value_fg, __m256 *value_og,
      __m256 *grad_in, __m256 *grad_ig, __m256 *grad_fg, __m256 *grad_og,
      __m256 *prev_state, __m256 *prev_state_grad, __m256 *state,
      __m256 *state_grad, __m256 *state_atv, __m256 *output_grad,
      __m256 *checkI, __m256 *checkF, __m256 *checkO, __m256 *checkIGrad,
147 148 149
      __m256 *checkFGrad, __m256 *checkOGrad, T *cell_clip,
      ActivationType active_node, ActivationType active_gate,
      ActivationType active_state) {
150 151
    *grad_og = activation(_mm256_mul_ps(*output_grad, *state_atv), *value_og,
                          active_gate);
152 153 154 155 156 157 158 159 160 161 162 163 164
    if (*cell_clip > 0.0f) {
      T *state_ = reinterpret_cast<T *>(state);
      if (*state_ >= (*cell_clip) || *state_ <= (0.0f - (*cell_clip))) {
        *state_grad = _mm256_set1_ps(0.0f);
      } else {
        *state_grad =
            _mm256_add_ps(activation(_mm256_mul_ps(*output_grad, *value_og),
                                     *state_atv, active_state),
                          *state_grad);
        *state_grad =
            _mm256_add_ps(_mm256_mul_ps(*grad_og, *checkO), *state_grad);
      }
    }
165 166 167 168 169 170 171 172 173 174 175 176 177
    *grad_in = activation(_mm256_mul_ps(*state_grad, *value_ig), *value_in,
                          active_node);
    *grad_ig = activation(_mm256_mul_ps(*state_grad, *value_in), *value_ig,
                          active_gate);
    *grad_fg = activation(_mm256_mul_ps(*state_grad, *prev_state), *value_fg,
                          active_gate);
    *prev_state_grad = _mm256_add_ps(_mm256_mul_ps(*grad_ig, *checkI),
                                     _mm256_mul_ps(*grad_fg, *checkF));
    *prev_state_grad =
        _mm256_add_ps(_mm256_mul_ps(*state_grad, *value_fg), *prev_state_grad);
    *checkIGrad = _mm256_mul_ps(*grad_ig, *prev_state);
    *checkFGrad = _mm256_mul_ps(*grad_fg, *prev_state);
    *checkOGrad = _mm256_mul_ps(*grad_og, *state);
D
dangqingqing 已提交
178 179 180 181 182 183 184 185 186 187 188
  }
#endif
#endif
};

}  // namespace backward

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