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 20 21 22 23 24 25 26 27 28 29 30
#include "paddle/platform/hostdevice.h"

#include <type_traits>

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,
31 32 33
                             T &valueResetOutput, activation_mode_t actGate) {
    valueUpdateGate = activation(valueUpdateGate, actGate);
    valueResetGate = activation(valueResetGate, actGate);
G
guosheng 已提交
34 35 36 37 38 39 40 41 42
    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,
43 44 45
                             activation_mode_t actGate) {
    valueUpdateGate = activation(valueUpdateGate, actGate);
    valueResetGate = activation(valueResetGate, actGate);
G
guosheng 已提交
46 47 48 49 50 51 52 53 54 55
    valueResetOutput = _mm256_mul_ps(prevOut, valueResetGate);
  }
#endif
#endif
};

template <typename T>
class gru_finalOutput {
 public:
  HOSTDEVICE void operator()(T &valueUpdateGate, T &valueFrameState, T &prevOut,
56 57
                             T &valueOutput, activation_mode_t actInput) {
    valueFrameState = activation(valueFrameState, actInput);
G
guosheng 已提交
58 59 60 61 62 63 64 65 66 67
    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,
68 69
                             activation_mode_t actInput) {
    valueFrameState = activation(valueFrameState, actInput);
G
guosheng 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
    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,
87
                             activation_mode_t actInput) {
G
guosheng 已提交
88 89 90 91
    gradUpdateGate = (gradOutput * valueFrameState);
    gradUpdateGate -= (gradOutput * valuePrevOut);
    gradPrevOut -= (gradOutput * valueUpdateGate);
    gradPrevOut += gradOutput;
92 93
    gradFrameState =
        activation(gradOutput * valueUpdateGate, valueFrameState, actInput);
G
guosheng 已提交
94 95 96 97 98 99 100 101 102
  }
#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,
103
                             __m256 &gradOutput, activation_mode_t actInput) {
G
guosheng 已提交
104 105 106 107 108 109
    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);
110 111
    gradFrameState = activation(_mm256_mul_ps(gradOutput, valueUpdateGate),
                                valueFrameState, actInput);
G
guosheng 已提交
112 113 114 115 116 117 118 119 120 121 122
  }
#endif
#endif
};

template <typename T>
class gru_resetGrad {
 public:
  HOSTDEVICE void operator()(T &valueUpdateGate, T &gradUpdateGate,
                             T &valueResetGate, T &gradResetGate,
                             T &valuePrevOut, T &gradPrevOut,
123
                             T &gradResetOutput, activation_mode_t actGate) {
G
guosheng 已提交
124 125
    gradResetGate = (gradResetOutput * valuePrevOut);
    gradPrevOut += (gradResetOutput * valueResetGate);
126 127
    gradUpdateGate = activation(gradUpdateGate, valueUpdateGate, actGate);
    gradResetGate = activation(gradResetGate, valueResetGate, actGate);
G
guosheng 已提交
128 129 130 131 132 133 134 135 136 137
  }
#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,
138
                             activation_mode_t actGate) {
G
guosheng 已提交
139 140 141
    gradResetGate = _mm256_mul_ps(gradResetOutput, valuePrevOut);
    gradPrevOut = _mm256_add_ps(gradPrevOut,
                                _mm256_mul_ps(gradResetOutput, valueResetGate));
142 143
    gradUpdateGate = activation(gradUpdateGate, valueUpdateGate, actGate);
    gradResetGate = activation(gradResetGate, valueResetGate, actGate);
G
guosheng 已提交
144 145 146 147 148 149 150 151 152 153 154
  }
#endif
#endif
};

}  // namespace backward

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