gru_kernel.h 5.6 KB
Newer Older
G
guosheng 已提交
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"
G
guosheng 已提交
16 17 18 19
#include "paddle/platform/hostdevice.h"

#include <type_traits>

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

namespace forward {

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

template <typename T>
class gru_finalOutput {
 public:
  HOSTDEVICE void operator()(T &valueUpdateGate, T &valueFrameState, T &prevOut,
57 58
                             T &valueOutput, activation_mode_t actInput) {
    valueFrameState = activation(valueFrameState, actInput);
G
guosheng 已提交
59 60 61 62 63 64 65 66 67 68
    valueOutput = prevOut - (valueUpdateGate * prevOut) +
                  (valueUpdateGate * valueFrameState);
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
  HOSTDEVICE void operator()(__m256 &valueUpdateGate, __m256 &valueFrameState,
                             __m256 &prevOut, __m256 &valueOutput,
69 70
                             activation_mode_t actInput) {
    valueFrameState = activation(valueFrameState, actInput);
G
guosheng 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    valueOutput = _mm256_add_ps(
        _mm256_sub_ps(prevOut, _mm256_mul_ps(valueUpdateGate, prevOut)),
        _mm256_mul_ps(valueUpdateGate, valueFrameState));
  }
#endif
#endif
};
}  // namespace forward

namespace backward {

template <typename T>
class gru_stateGrad {
 public:
  HOSTDEVICE void operator()(T &valueUpdateGate, T &gradUpdateGate,
                             T &valueFrameState, T &gradFrameState,
                             T &valuePrevOut, T &gradPrevOut, T &gradOutput,
88
                             activation_mode_t actInput) {
G
guosheng 已提交
89 90 91 92
    gradUpdateGate = (gradOutput * valueFrameState);
    gradUpdateGate -= (gradOutput * valuePrevOut);
    gradPrevOut -= (gradOutput * valueUpdateGate);
    gradPrevOut += gradOutput;
93 94
    gradFrameState =
        activation(gradOutput * valueUpdateGate, valueFrameState, actInput);
G
guosheng 已提交
95 96 97 98 99 100 101 102 103
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
  HOSTDEVICE void operator()(__m256 &valueUpdateGate, __m256 &gradUpdateGate,
                             __m256 &valueFrameState, __m256 &gradFrameState,
                             __m256 &valuePrevOut, __m256 &gradPrevOut,
104
                             __m256 &gradOutput, activation_mode_t actInput) {
G
guosheng 已提交
105 106 107 108 109 110
    gradUpdateGate = _mm256_mul_ps(gradOutput, valueFrameState);
    gradUpdateGate =
        _mm256_sub_ps(gradUpdateGate, _mm256_mul_ps(gradOutput, valuePrevOut));
    gradPrevOut = _mm256_add_ps(
        _mm256_sub_ps(gradPrevOut, _mm256_mul_ps(gradOutput, valueUpdateGate)),
        gradOutput);
111 112
    gradFrameState = activation(_mm256_mul_ps(gradOutput, valueUpdateGate),
                                valueFrameState, actInput);
G
guosheng 已提交
113 114 115 116 117 118 119 120 121 122 123
  }
#endif
#endif
};

template <typename T>
class gru_resetGrad {
 public:
  HOSTDEVICE void operator()(T &valueUpdateGate, T &gradUpdateGate,
                             T &valueResetGate, T &gradResetGate,
                             T &valuePrevOut, T &gradPrevOut,
124
                             T &gradResetOutput, activation_mode_t actGate) {
G
guosheng 已提交
125 126
    gradResetGate = (gradResetOutput * valuePrevOut);
    gradPrevOut += (gradResetOutput * valueResetGate);
127 128
    gradUpdateGate = activation(gradUpdateGate, valueUpdateGate, actGate);
    gradResetGate = activation(gradResetGate, valueResetGate, actGate);
G
guosheng 已提交
129 130 131 132 133 134 135 136 137 138
  }
#ifndef __NVCC__
#ifndef __AVX__
  static const bool avx = false;
#else
  static const bool avx = true;
  HOSTDEVICE void operator()(__m256 &valueUpdateGate, __m256 &gradUpdateGate,
                             __m256 &valueResetGate, __m256 &gradResetGate,
                             __m256 &valuePrevOut, __m256 &gradPrevOut,
                             __m256 &gradResetOutput,
139
                             activation_mode_t actGate) {
G
guosheng 已提交
140 141 142
    gradResetGate = _mm256_mul_ps(gradResetOutput, valuePrevOut);
    gradPrevOut = _mm256_add_ps(gradPrevOut,
                                _mm256_mul_ps(gradResetOutput, valueResetGate));
143 144
    gradUpdateGate = activation(gradUpdateGate, valueUpdateGate, actGate);
    gradResetGate = activation(gradResetGate, valueResetGate, actGate);
G
guosheng 已提交
145 146 147 148 149 150 151 152 153 154 155
  }
#endif
#endif
};

}  // namespace backward

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