gru_unit_op.cc 9.3 KB
Newer Older
G
guosheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/* 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. */

#include "paddle/operators/gru_unit_op.h"

namespace paddle {
namespace operators {

using framework::Tensor;

class GRUUnitOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
27 28 29 30 31 32 33 34 35 36 37 38
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Input"),
                   "Input(%s) of GRUUnitOp should not be null.", "Input");
    PADDLE_ENFORCE(ctx->HasInput("HiddenPrev"),
                   "Input(%s) of GRUUnitOp should not be null.", "HiddenPrev");
    PADDLE_ENFORCE(ctx->HasInput("Weight"),
                   "Input(%s) of GRUUnitOp should not be null.", "Weight");
    PADDLE_ENFORCE(ctx->HasInput("Bias"),
                   "Input(%s) of GRUUnitOp should not be null.", "Bias");
    PADDLE_ENFORCE(ctx->HasOutput("Gate"),
                   "Output(%s) of GRUUnitOp should not be null.", "Gate");
    PADDLE_ENFORCE(ctx->HasOutput("ResetHiddenPrev"),
G
guosheng 已提交
39
                   "Output(%s) of GRUUnitOp should not be null.",
40 41 42 43 44 45 46
                   "ResetHiddenPrev");
    PADDLE_ENFORCE(ctx->HasOutput("Hidden"),
                   "Output(%s) of GRUUnitOp should not be null.", "Hidden");
    auto input_dims = ctx->GetInputDim("Input");
    auto hidden_prev_dims = ctx->GetInputDim("HiddenPrev");
    auto weight_dims = ctx->GetInputDim("Weight");
    auto bias_dims = ctx->GetInputDim("Bias");
G
guosheng 已提交
47 48 49 50 51 52 53 54 55
    int batch_size = input_dims[0];
    int input_size = input_dims[1];
    int frame_size = hidden_prev_dims[1];
    int weight_height = weight_dims[0];
    int weight_width = weight_dims[1];
    int bias_height = bias_dims[0];
    int bias_width = bias_dims[1];
    PADDLE_ENFORCE_EQ(
        input_size, frame_size * 3,
56
        "The input_size must be 3 times of frame_size in GRUUnitOp.");
G
guosheng 已提交
57 58
    PADDLE_ENFORCE_EQ(
        weight_height, frame_size,
59
        "The shape of Weight matrix must be [frame_size, frame_size * 3].");
G
guosheng 已提交
60 61
    PADDLE_ENFORCE_EQ(
        weight_width, frame_size * 3,
62
        "The shape of Weight matrix must be [frame_size, frame_size * 3].");
G
guosheng 已提交
63
    PADDLE_ENFORCE_EQ(bias_height, 1,
64
                      "The shape of Bias must be [1, frame_size * 3].");
G
guosheng 已提交
65
    PADDLE_ENFORCE_EQ(bias_width, frame_size * 3,
66 67 68 69
                      "The shape of Bias must be [1, frame_size * 3].");
    ctx->SetOutputDim("Gate", {batch_size, frame_size * 3});
    ctx->SetOutputDim("ResetHiddenPrev", {batch_size, frame_size});
    ctx->SetOutputDim("Hidden", {batch_size, frame_size});
G
guosheng 已提交
70 71 72 73 74
  }
};

class GRUUnitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
75 76
  GRUUnitOpMaker(framework::OpProto* proto,
                 framework::OpAttrChecker* op_checker)
G
guosheng 已提交
77
      : OpProtoAndCheckerMaker(proto, op_checker) {
78
    AddInput("Input",
G
guosheng 已提交
79 80
             "(Tensor) Matrix with shape [batch_size, frame_size * 3] for the "
             "input.");
81
    AddInput("HiddenPrev",
G
guosheng 已提交
82 83
             "(Tensor) Matrix with shape [batch_size, frame_size] for the "
             "states of previous time step.");
84
    AddInput("Weight",
G
guosheng 已提交
85 86 87 88 89
             "(Tensor) Weight matrix with shape [frame_size, frame_size * 3]. "
             "The elements continuous in memory can be divided into two parts. "
             "The first part are weights of the update gate and reset gate "
             "with shape [frame_size, frame_size * 2], and the second part are "
             "weights of output candidate with shape [frame_size, frame_size]");
90
    AddInput("Bias",
G
guosheng 已提交
91 92
             "(Tensor) Bias vector with shape [1, frame_size * 3] concating "
             "bias of the update gate, reset gate and output candidate.");
93
    AddOutput("Gate",
G
guosheng 已提交
94 95 96
              "(Tensor) Matrix with shape [batch_size, frame_size * 3] for the "
              "output of update gate, reset gate and output candidate")
        .AsIntermediate();
97
    AddOutput("ResetHiddenPrev",
G
guosheng 已提交
98 99 100
              "(Tensor) Matrix with shape [batch_size, frame_size] for the "
              "reseted hidden state of previous time step.")
        .AsIntermediate();
101
    AddOutput("Hidden",
G
guosheng 已提交
102 103
              "(Tensor) The GRU hidden state of the current time step "
              "with shape [batch_size, frame_size].");
104 105 106 107 108 109 110 111 112 113
    AddAttr<int>("activation",
                 "(enum int, default tanh) "
                 "The activation type used for output candidate {h}_t.")
        .SetDefault(tanh)
        .InEnum({identity, sigmoid, tanh, relu});
    AddAttr<int>("gate_activation",
                 "(enum int, default sigmoid) "
                 "The activation type used in update gate and reset gate.")
        .SetDefault(sigmoid)
        .InEnum({identity, sigmoid, tanh, relu});
G
guosheng 已提交
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
    AddComment(R"DOC(
GRUUnitOp implements part calculations of the GRU unit as following:

\f[
update \ gate: u_t = actGate(xu_t + W_u * hidden_prev + bias_u) \\
reset \ gate: r_t = actGate(xr_t + W_r * hidden_prev + bias_r)  \\
output \ candidate: {h}_t = actNode(xc_t + W_c * dot(r_t, hidden_prev) + bias_c) \\
output: h_t = dot((1-u_t), {h}_t) + dot(u_t, hidden_prev)
\f]

The rest of GRU unit can be completed by using FCOp's output as the input of GRUUnitOp.
)DOC");
  }
};

class GRUUnitGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
134 135 136 137
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("Input"),
                   "Input(%s) of GRUUnitGradOp should not be null.", "Input");
    PADDLE_ENFORCE(ctx->HasInput("HiddenPrev"),
G
guosheng 已提交
138
                   "Input(%s) of GRUUnitGradOp should not be null.",
139 140 141 142 143 144 145 146
                   "HiddenPrev");
    PADDLE_ENFORCE(ctx->HasInput("Weight"),
                   "Input(%s) of GRUUnitGradOp should not be null.", "Weight");
    PADDLE_ENFORCE(ctx->HasInput("Bias"),
                   "Input(%s) of GRUUnitGradOp should not be null.", "Bias");
    PADDLE_ENFORCE(ctx->HasInput("Gate"),
                   "Input(%s) of GRUUnitGradOp should not be null.", "Gate");
    PADDLE_ENFORCE(ctx->HasInput("ResetHiddenPrev"),
G
guosheng 已提交
147
                   "Input(%s) of GRUUnitGradOp should not be null.",
148 149 150 151
                   "ResetHiddenPrev");
    PADDLE_ENFORCE(ctx->HasInput("Hidden"),
                   "Input(%s) of GRUUnitGradOp should not be null.", "Hidden");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Gate")),
G
guosheng 已提交
152
                   "Input(%s@GRAD) of GRUUnitGradOp should not be null.",
153 154
                   "Gate");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("ResetHiddenPrev")),
G
guosheng 已提交
155
                   "Input(%s@GRAD) of GRUUnitGradOp should not be null.",
156 157
                   "ResetHiddenPrev");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Hidden")),
G
guosheng 已提交
158
                   "Input(%s@GRAD) of GRUUnitGradOp should not be null.",
159 160 161 162 163
                   "Hidden");
    auto input_dims = ctx->GetInputDim("Input");
    auto hidden_prev_dims = ctx->GetInputDim("HiddenPrev");
    auto weight_dims = ctx->GetInputDim("Weight");
    auto bias_dims = ctx->GetInputDim("Bias");
G
guosheng 已提交
164 165 166 167 168 169 170 171 172
    // int batch_size = input_dims[0];
    int input_size = input_dims[1];
    int frame_size = hidden_prev_dims[1];
    int weight_height = weight_dims[0];
    int weight_width = weight_dims[1];
    int bias_height = bias_dims[0];
    int bias_width = bias_dims[1];
    PADDLE_ENFORCE_EQ(
        input_size, frame_size * 3,
173
        "The input_size must be 3 times of frame_size in GRUUnitOp.");
G
guosheng 已提交
174 175
    PADDLE_ENFORCE_EQ(
        weight_height, frame_size,
176
        "The shape of Weight matrix must be [frame_size, frame_size * 3].");
G
guosheng 已提交
177 178
    PADDLE_ENFORCE_EQ(
        weight_width, frame_size * 3,
179
        "The shape of Weight matrix must be [frame_size, frame_size * 3].");
G
guosheng 已提交
180
    PADDLE_ENFORCE_EQ(bias_height, 1,
181
                      "The shape of Bias must be [1, frame_size * 3].");
G
guosheng 已提交
182
    PADDLE_ENFORCE_EQ(bias_width, frame_size * 3,
183 184
                      "The shape of Bias must be [1, frame_size * 3].");
    auto input_grad_name = framework::GradVarName("Input");
G
guosheng 已提交
185 186
    if (ctx->HasOutput(input_grad_name))
      ctx->SetOutputDim(input_grad_name, input_dims);
187
    auto hidden_prev_grad_name = framework::GradVarName("HiddenPrev");
G
guosheng 已提交
188 189
    if (ctx->HasOutput(hidden_prev_grad_name))
      ctx->SetOutputDim(hidden_prev_grad_name, hidden_prev_dims);
190
    auto weight_grad_name = framework::GradVarName("Weight");
G
guosheng 已提交
191 192
    if (ctx->HasOutput(weight_grad_name))
      ctx->SetOutputDim(weight_grad_name, weight_dims);
193
    auto bias_grad_name = framework::GradVarName("Bias");
G
guosheng 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
    if (ctx->HasOutput(bias_grad_name))
      ctx->SetOutputDim(bias_grad_name, bias_dims);
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(gru_unit, ops::GRUUnitOp, ops::GRUUnitOpMaker, gru_unit_grad,
            ops::GRUUnitGradOp);
REGISTER_OP_CPU_KERNEL(gru_unit,
                       ops::GRUUnitKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
    gru_unit_grad, ops::GRUUnitGradKernel<paddle::platform::CPUPlace, float>);