|
|
|
@ -12,7 +12,7 @@ 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. */
|
|
|
|
|
|
|
|
|
|
#include "paddle/operators/math/detail/hl_activation_functions.h"
|
|
|
|
|
#include "paddle/operators/math/detail/activation_functions.h"
|
|
|
|
|
#include "paddle/platform/hostdevice.h"
|
|
|
|
|
|
|
|
|
|
#include <type_traits>
|
|
|
|
@ -27,18 +27,10 @@ namespace forward {
|
|
|
|
|
template <typename T>
|
|
|
|
|
class gru_resetOutput {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @param[in,out] valueUpdateGate update gate
|
|
|
|
|
* @param[in,out] valueResetGate reset gate
|
|
|
|
|
* @param[in] prevOut previous output
|
|
|
|
|
* @param[out] valueResetOutput intermediate value for frame state
|
|
|
|
|
* @param[in] actGate forward function of gate
|
|
|
|
|
*/
|
|
|
|
|
HOSTDEVICE void operator()(T &valueUpdateGate, T &valueResetGate, T &prevOut,
|
|
|
|
|
T &valueResetOutput,
|
|
|
|
|
typename hppl::Active<T>::forward actGate) {
|
|
|
|
|
valueUpdateGate = actGate(valueUpdateGate);
|
|
|
|
|
valueResetGate = actGate(valueResetGate);
|
|
|
|
|
T &valueResetOutput, activation_mode_t actGate) {
|
|
|
|
|
valueUpdateGate = activation(valueUpdateGate, actGate);
|
|
|
|
|
valueResetGate = activation(valueResetGate, actGate);
|
|
|
|
|
valueResetOutput = prevOut * valueResetGate;
|
|
|
|
|
}
|
|
|
|
|
#ifndef __NVCC__
|
|
|
|
@ -48,9 +40,9 @@ class gru_resetOutput {
|
|
|
|
|
static const bool avx = true;
|
|
|
|
|
HOSTDEVICE void operator()(__m256 &valueUpdateGate, __m256 &valueResetGate,
|
|
|
|
|
__m256 &prevOut, __m256 &valueResetOutput,
|
|
|
|
|
typename hppl::Active<__m256>::forward actGate) {
|
|
|
|
|
valueUpdateGate = actGate(valueUpdateGate);
|
|
|
|
|
valueResetGate = actGate(valueResetGate);
|
|
|
|
|
activation_mode_t actGate) {
|
|
|
|
|
valueUpdateGate = activation(valueUpdateGate, actGate);
|
|
|
|
|
valueResetGate = activation(valueResetGate, actGate);
|
|
|
|
|
valueResetOutput = _mm256_mul_ps(prevOut, valueResetGate);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
@ -60,17 +52,9 @@ class gru_resetOutput {
|
|
|
|
|
template <typename T>
|
|
|
|
|
class gru_finalOutput {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @param[in] valueUpdateGate update gate
|
|
|
|
|
* @param[in,out] valueFrameState frame state ({\tilde{h}_t})
|
|
|
|
|
* @param[in] prevOut previous output
|
|
|
|
|
* @param[out] valueOutput output
|
|
|
|
|
* @param[in] actInput forward function of node
|
|
|
|
|
*/
|
|
|
|
|
HOSTDEVICE void operator()(T &valueUpdateGate, T &valueFrameState, T &prevOut,
|
|
|
|
|
T &valueOutput,
|
|
|
|
|
typename hppl::Active<T>::forward actInput) {
|
|
|
|
|
valueFrameState = actInput(valueFrameState);
|
|
|
|
|
T &valueOutput, activation_mode_t actInput) {
|
|
|
|
|
valueFrameState = activation(valueFrameState, actInput);
|
|
|
|
|
valueOutput = prevOut - (valueUpdateGate * prevOut) +
|
|
|
|
|
(valueUpdateGate * valueFrameState);
|
|
|
|
|
}
|
|
|
|
@ -81,8 +65,8 @@ class gru_finalOutput {
|
|
|
|
|
static const bool avx = true;
|
|
|
|
|
HOSTDEVICE void operator()(__m256 &valueUpdateGate, __m256 &valueFrameState,
|
|
|
|
|
__m256 &prevOut, __m256 &valueOutput,
|
|
|
|
|
typename hppl::Active<__m256>::forward actInput) {
|
|
|
|
|
valueFrameState = actInput(valueFrameState);
|
|
|
|
|
activation_mode_t actInput) {
|
|
|
|
|
valueFrameState = activation(valueFrameState, actInput);
|
|
|
|
|
valueOutput = _mm256_add_ps(
|
|
|
|
|
_mm256_sub_ps(prevOut, _mm256_mul_ps(valueUpdateGate, prevOut)),
|
|
|
|
|
_mm256_mul_ps(valueUpdateGate, valueFrameState));
|
|
|
|
@ -97,25 +81,16 @@ namespace backward {
|
|
|
|
|
template <typename T>
|
|
|
|
|
class gru_stateGrad {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @param[in] valueUpdateGate update gate value
|
|
|
|
|
* @param[out] gradUpdateGate update gate grad
|
|
|
|
|
* @param[in] valueFrameState frame state value
|
|
|
|
|
* @param[out] gradFrameState frame state grad
|
|
|
|
|
* @param[in] valuePrevOut previous output value
|
|
|
|
|
* @param[in,out] gradPrevOut previous output grad
|
|
|
|
|
* @param[in] gradOutput output grad
|
|
|
|
|
* @param[in] actInput backward function of frame state
|
|
|
|
|
*/
|
|
|
|
|
HOSTDEVICE void operator()(T &valueUpdateGate, T &gradUpdateGate,
|
|
|
|
|
T &valueFrameState, T &gradFrameState,
|
|
|
|
|
T &valuePrevOut, T &gradPrevOut, T &gradOutput,
|
|
|
|
|
typename hppl::Active<T>::backward actInput) {
|
|
|
|
|
activation_mode_t actInput) {
|
|
|
|
|
gradUpdateGate = (gradOutput * valueFrameState);
|
|
|
|
|
gradUpdateGate -= (gradOutput * valuePrevOut);
|
|
|
|
|
gradPrevOut -= (gradOutput * valueUpdateGate);
|
|
|
|
|
gradPrevOut += gradOutput;
|
|
|
|
|
gradFrameState = actInput(gradOutput * valueUpdateGate, valueFrameState);
|
|
|
|
|
gradFrameState =
|
|
|
|
|
activation(gradOutput * valueUpdateGate, valueFrameState, actInput);
|
|
|
|
|
}
|
|
|
|
|
#ifndef __NVCC__
|
|
|
|
|
#ifndef __AVX__
|
|
|
|
@ -125,16 +100,15 @@ class gru_stateGrad {
|
|
|
|
|
HOSTDEVICE void operator()(__m256 &valueUpdateGate, __m256 &gradUpdateGate,
|
|
|
|
|
__m256 &valueFrameState, __m256 &gradFrameState,
|
|
|
|
|
__m256 &valuePrevOut, __m256 &gradPrevOut,
|
|
|
|
|
__m256 &gradOutput,
|
|
|
|
|
typename hppl::Active<__m256>::backward actInput) {
|
|
|
|
|
__m256 &gradOutput, activation_mode_t actInput) {
|
|
|
|
|
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);
|
|
|
|
|
gradFrameState =
|
|
|
|
|
actInput(_mm256_mul_ps(gradOutput, valueUpdateGate), valueFrameState);
|
|
|
|
|
gradFrameState = activation(_mm256_mul_ps(gradOutput, valueUpdateGate),
|
|
|
|
|
valueFrameState, actInput);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
@ -143,25 +117,14 @@ class gru_stateGrad {
|
|
|
|
|
template <typename T>
|
|
|
|
|
class gru_resetGrad {
|
|
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* @param[in] valueUpdateGate update gate value
|
|
|
|
|
* @param[in,out] gradUpdateGate update gate grad
|
|
|
|
|
* @param[in] valueResetGate reset gate value
|
|
|
|
|
* @param[out] gradResetGate reset gate grad
|
|
|
|
|
* @param[in] valuePrevOut previous output value
|
|
|
|
|
* @param[in,out] gradPrevOut previous output grad
|
|
|
|
|
* @param[in] gradResetOutput reset output grad (temp val)
|
|
|
|
|
* @param[in] actGate backward function of gate
|
|
|
|
|
*/
|
|
|
|
|
HOSTDEVICE void operator()(T &valueUpdateGate, T &gradUpdateGate,
|
|
|
|
|
T &valueResetGate, T &gradResetGate,
|
|
|
|
|
T &valuePrevOut, T &gradPrevOut,
|
|
|
|
|
T &gradResetOutput,
|
|
|
|
|
typename hppl::Active<T>::backward actGate) {
|
|
|
|
|
T &gradResetOutput, activation_mode_t actGate) {
|
|
|
|
|
gradResetGate = (gradResetOutput * valuePrevOut);
|
|
|
|
|
gradPrevOut += (gradResetOutput * valueResetGate);
|
|
|
|
|
gradUpdateGate = actGate(gradUpdateGate, valueUpdateGate);
|
|
|
|
|
gradResetGate = actGate(gradResetGate, valueResetGate);
|
|
|
|
|
gradUpdateGate = activation(gradUpdateGate, valueUpdateGate, actGate);
|
|
|
|
|
gradResetGate = activation(gradResetGate, valueResetGate, actGate);
|
|
|
|
|
}
|
|
|
|
|
#ifndef __NVCC__
|
|
|
|
|
#ifndef __AVX__
|
|
|
|
@ -172,12 +135,12 @@ class gru_resetGrad {
|
|
|
|
|
__m256 &valueResetGate, __m256 &gradResetGate,
|
|
|
|
|
__m256 &valuePrevOut, __m256 &gradPrevOut,
|
|
|
|
|
__m256 &gradResetOutput,
|
|
|
|
|
typename hppl::Active<__m256>::backward actGate) {
|
|
|
|
|
activation_mode_t actGate) {
|
|
|
|
|
gradResetGate = _mm256_mul_ps(gradResetOutput, valuePrevOut);
|
|
|
|
|
gradPrevOut = _mm256_add_ps(gradPrevOut,
|
|
|
|
|
_mm256_mul_ps(gradResetOutput, valueResetGate));
|
|
|
|
|
gradUpdateGate = actGate(gradUpdateGate, valueUpdateGate);
|
|
|
|
|
gradResetGate = actGate(gradResetGate, valueResetGate);
|
|
|
|
|
gradUpdateGate = activation(gradUpdateGate, valueUpdateGate, actGate);
|
|
|
|
|
gradResetGate = activation(gradResetGate, valueResetGate, actGate);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|