gru_compute.h 2.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
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. */

#pragma once

Y
Yi Wang 已提交
14 15 16
#include "paddle/fluid/operators/math/detail/activation_functions.h"
#include "paddle/fluid/platform/device_context.h"
#include "paddle/fluid/platform/enforce.h"
G
guosheng 已提交
17 18 19 20 21 22

namespace paddle {
namespace operators {
namespace math {

template <typename T>
23
struct GRUMetaValue {
24 25 26
  const T *gate_weight;
  const T *state_weight;
  const T *reset_bias;
G
guosheng 已提交
27 28 29
  T *gate_value;
  T *reset_output_value;
  T *output_value;
30
  const T *prev_out_value;
G
guosheng 已提交
31 32 33
};

template <typename T>
34
struct GRUMetaGrad {
G
guosheng 已提交
35 36 37 38 39 40
  T *gate_weight_grad;
  T *state_weight_grad;
  T *gate_grad;
  T *reset_output_grad;
  T *output_grad;
  T *prev_out_grad;
41
  T *state_bias_grad;
G
guosheng 已提交
42 43
};

Q
QI JUN 已提交
44
template <typename DeviceContext, typename T>
G
guosheng 已提交
45
struct GRUUnitFunctor {
46
  static void compute(const DeviceContext &context, GRUMetaValue<T> value,
Q
QI JUN 已提交
47
                      int frame_size, int batch_size,
48
                      const detail::ActivationType active_node,
Q
Qiao Longfei 已提交
49 50
                      const detail::ActivationType active_gate,
                      bool origin_mode);
G
guosheng 已提交
51 52
};

Q
QI JUN 已提交
53
template <typename DeviceContext, typename T>
G
guosheng 已提交
54
struct GRUUnitGradFunctor {
55 56 57
  static void compute(const DeviceContext &context, GRUMetaValue<T> value,
                      GRUMetaGrad<T> grad, int frame_size, int batch_size,
                      const detail::ActivationType active_node,
Q
Qiao Longfei 已提交
58 59
                      const detail::ActivationType active_gate,
                      bool origin_mode);
G
guosheng 已提交
60 61
};

62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
template <typename DeviceContext, typename T>
struct GRUUnitFunctorV2 {
  static void compute(const DeviceContext &context, GRUMetaValue<T> value,
                      int frame_size, int batch_size,
                      const detail::ActivationType active_node,
                      const detail::ActivationType active_gate);
};

template <typename DeviceContext, typename T>
struct GRUUnitGradFunctorV2 {
  static void compute(const DeviceContext &context, GRUMetaValue<T> value,
                      GRUMetaGrad<T> grad, int frame_size, int batch_size,
                      const detail::ActivationType active_node,
                      const detail::ActivationType active_gate);
};

G
guosheng 已提交
78 79 80
}  // namespace math
}  // namespace operators
}  // namespace paddle