gru_kernel.h 6.7 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
G
guosheng 已提交
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"
G
guosheng 已提交
19

G
guosheng 已提交
20
// TODO(guosheng): refine code style in gru_kernel
G
guosheng 已提交
21 22 23 24 25 26 27 28 29 30
namespace paddle {
namespace operators {
namespace math {
namespace detail {

namespace forward {

template <typename T>
class gru_resetOutput {
 public:
31 32
  HOSTDEVICE void operator()(T *value_update_gate, T *value_reset_gate,
                             T *prev_out, T *value_reset_output,
33
                             ActivationType act_gate) {
34 35 36
    *value_update_gate = activation(*value_update_gate, act_gate);
    *value_reset_gate = activation(*value_reset_gate, act_gate);
    *value_reset_output = (*prev_out) * (*value_reset_gate);
G
guosheng 已提交
37 38 39 40 41 42
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
43 44 45
  HOSTDEVICE void operator()(__m256 *value_update_gate,
                             __m256 *value_reset_gate, __m256 *prev_out,
                             __m256 *value_reset_output,
46
                             ActivationType act_gate) {
47 48 49
    *value_update_gate = activation(*value_update_gate, act_gate);
    *value_reset_gate = activation(*value_reset_gate, act_gate);
    *value_reset_output = _mm256_mul_ps(*prev_out, *value_reset_gate);
G
guosheng 已提交
50 51 52 53 54 55 56 57
  }
#endif
#endif
};

template <typename T>
class gru_finalOutput {
 public:
58 59
  HOSTDEVICE void operator()(T *value_update_gate, T *value_frame_state,
                             T *prev_out, T *value_output,
Q
Qiao Longfei 已提交
60
                             ActivationType act_input, bool origin_mode) {
61
    *value_frame_state = activation(*value_frame_state, act_input);
Q
Qiao Longfei 已提交
62 63 64 65 66 67 68 69
    if (origin_mode) {
      *value_output = ((*value_update_gate) * (*prev_out)) +
                      *value_frame_state -
                      ((*value_update_gate) * (*value_frame_state));
    } else {
      *value_output = *prev_out - ((*value_update_gate) * (*prev_out)) +
                      ((*value_update_gate) * (*value_frame_state));
    }
G
guosheng 已提交
70 71 72 73 74 75
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
76 77
  HOSTDEVICE void operator()(__m256 *value_update_gate,
                             __m256 *value_frame_state, __m256 *prev_out,
Q
Qiao Longfei 已提交
78 79
                             __m256 *value_output, ActivationType act_input,
                             bool origin_mode) {
80
    *value_frame_state = activation(*value_frame_state, act_input);
Q
Qiao Longfei 已提交
81 82 83 84 85 86 87 88 89 90 91
    if (origin_mode) {
      *value_output = _mm256_sub_ps(
          _mm256_add_ps(_mm256_mul_ps(*value_update_gate, *prev_out),
                        *value_frame_state),
          _mm256_mul_ps(*value_update_gate, *value_frame_state));
    } else {
      *value_output = _mm256_add_ps(
          _mm256_sub_ps(*prev_out,
                        _mm256_mul_ps(*value_update_gate, *prev_out)),
          _mm256_mul_ps(*value_update_gate, *value_frame_state));
    }
G
guosheng 已提交
92 93 94 95 96 97 98 99 100 101 102
  }
#endif
#endif
};
}  // namespace forward

namespace backward {

template <typename T>
class gru_stateGrad {
 public:
103 104 105 106 107 108 109 110 111 112
  HOSTDEVICE void operator()(T *value_update_gate, T *grad_update_gate,
                             T *value_frame_state, T *grad_frame_state,
                             T *value_prev_out, T *grad_prev_out,
                             T *grad_output, ActivationType act_input) {
    *grad_update_gate = (*grad_output * (*value_frame_state));
    *grad_update_gate -= (*grad_output * (*value_prev_out));
    *grad_prev_out -= (*grad_output * (*value_update_gate));
    *grad_prev_out += *grad_output;
    *grad_frame_state = activation(*grad_output * (*value_update_gate),
                                   *value_frame_state, act_input);
G
guosheng 已提交
113 114 115 116 117 118
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
119 120 121 122 123
  HOSTDEVICE void operator()(__m256 *value_update_gate,
                             __m256 *grad_update_gate,
                             __m256 *value_frame_state,
                             __m256 *grad_frame_state, __m256 *value_prev_out,
                             __m256 *grad_prev_out, __m256 *grad_output,
124
                             ActivationType act_input) {
125 126 127 128 129 130 131 132 133 134
    *grad_update_gate = _mm256_mul_ps(*grad_output, *value_frame_state);
    *grad_update_gate = _mm256_sub_ps(
        *grad_update_gate, _mm256_mul_ps(*grad_output, *value_prev_out));
    *grad_prev_out = _mm256_add_ps(
        _mm256_sub_ps(*grad_prev_out,
                      _mm256_mul_ps(*grad_output, *value_update_gate)),
        *grad_output);
    *grad_frame_state =
        activation(_mm256_mul_ps(*grad_output, *value_update_gate),
                   *value_frame_state, act_input);
G
guosheng 已提交
135 136 137 138 139 140 141 142
  }
#endif
#endif
};

template <typename T>
class gru_resetGrad {
 public:
143 144 145 146 147 148 149 150 151 152
  HOSTDEVICE void operator()(T *value_update_gate, T *grad_update_gate,
                             T *value_reset_gate, T *grad_reset_gate,
                             T *value_prev_out, T *grad_prev_out,
                             T *grad_reset_output, ActivationType act_gate) {
    *grad_reset_gate = (*grad_reset_output * (*value_prev_out));
    *grad_prev_out += (*grad_reset_output * (*value_reset_gate));
    *grad_update_gate =
        activation(*grad_update_gate, *value_update_gate, act_gate);
    *grad_reset_gate =
        activation(*grad_reset_gate, *value_reset_gate, act_gate);
G
guosheng 已提交
153 154 155 156 157 158
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
159 160 161 162
  HOSTDEVICE void operator()(__m256 *value_update_gate,
                             __m256 *grad_update_gate, __m256 *value_reset_gate,
                             __m256 *grad_reset_gate, __m256 *value_prev_out,
                             __m256 *grad_prev_out, __m256 *grad_reset_output,
163
                             ActivationType act_gate) {
164 165 166 167 168 169 170
    *grad_reset_gate = _mm256_mul_ps(*grad_reset_output, *value_prev_out);
    *grad_prev_out = _mm256_add_ps(
        *grad_prev_out, _mm256_mul_ps(*grad_reset_output, *value_reset_gate));
    *grad_update_gate =
        activation(*grad_update_gate, *value_update_gate, act_gate);
    *grad_reset_gate =
        activation(*grad_reset_gate, *value_reset_gate, act_gate);
G
guosheng 已提交
171 172 173 174 175 176 177 178 179 180 181
  }
#endif
#endif
};

}  // namespace backward

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