|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/operators/gru_unit_op.h" |
| 16 | + |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +using framework::Tensor; |
| 21 | + |
| 22 | +class GRUUnitOp : public framework::OperatorWithKernel { |
| 23 | + public: |
| 24 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 25 | + |
| 26 | + protected: |
| 27 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 28 | + PADDLE_ENFORCE(ctx->HasInput("Input"), |
| 29 | + "Input(%s) of GRUUnitOp should not be null.", "Input"); |
| 30 | + PADDLE_ENFORCE(ctx->HasInput("HiddenPrev"), |
| 31 | + "Input(%s) of GRUUnitOp should not be null.", "HiddenPrev"); |
| 32 | + PADDLE_ENFORCE(ctx->HasInput("Weight"), |
| 33 | + "Input(%s) of GRUUnitOp should not be null.", "Weight"); |
| 34 | + PADDLE_ENFORCE(ctx->HasOutput("Gate"), |
| 35 | + "Output(%s) of GRUUnitOp should not be null.", "Gate"); |
| 36 | + PADDLE_ENFORCE(ctx->HasOutput("ResetHiddenPrev"), |
| 37 | + "Output(%s) of GRUUnitOp should not be null.", |
| 38 | + "ResetHiddenPrev"); |
| 39 | + PADDLE_ENFORCE(ctx->HasOutput("Hidden"), |
| 40 | + "Output(%s) of GRUUnitOp should not be null.", "Hidden"); |
| 41 | + auto input_dims = ctx->GetInputDim("Input"); |
| 42 | + auto hidden_prev_dims = ctx->GetInputDim("HiddenPrev"); |
| 43 | + auto weight_dims = ctx->GetInputDim("Weight"); |
| 44 | + int batch_size = input_dims[0]; |
| 45 | + int input_size = input_dims[1]; |
| 46 | + int frame_size = hidden_prev_dims[1]; |
| 47 | + int weight_height = weight_dims[0]; |
| 48 | + int weight_width = weight_dims[1]; |
| 49 | + PADDLE_ENFORCE_EQ( |
| 50 | + input_size, frame_size * 3, |
| 51 | + "The input_size must be 3 times of frame_size in GRUUnitOp."); |
| 52 | + PADDLE_ENFORCE_EQ( |
| 53 | + weight_height, frame_size, |
| 54 | + "The shape of Weight matrix must be [frame_size, frame_size * 3]."); |
| 55 | + PADDLE_ENFORCE_EQ( |
| 56 | + weight_width, frame_size * 3, |
| 57 | + "The shape of Weight matrix must be [frame_size, frame_size * 3]."); |
| 58 | + auto bias = Input("Bias"); |
| 59 | + if (bias != framework::kEmptyVarName) { |
| 60 | + auto bias_dims = ctx->GetInputDim("Bias"); |
| 61 | + int bias_height = bias_dims[0]; |
| 62 | + int bias_width = bias_dims[1]; |
| 63 | + PADDLE_ENFORCE_EQ(bias_height, 1, |
| 64 | + "The shape of Bias must be [1, frame_size * 3]."); |
| 65 | + PADDLE_ENFORCE_EQ(bias_width, frame_size * 3, |
| 66 | + "The shape of Bias must be [1, frame_size * 3]."); |
| 67 | + } |
| 68 | + ctx->SetOutputDim("Gate", {batch_size, frame_size * 3}); |
| 69 | + ctx->SetOutputDim("ResetHiddenPrev", {batch_size, frame_size}); |
| 70 | + ctx->SetOutputDim("Hidden", {batch_size, frame_size}); |
| 71 | + } |
| 72 | +}; |
| 73 | + |
| 74 | +class GRUUnitOpMaker : public framework::OpProtoAndCheckerMaker { |
| 75 | + public: |
| 76 | + GRUUnitOpMaker(framework::OpProto* proto, |
| 77 | + framework::OpAttrChecker* op_checker) |
| 78 | + : OpProtoAndCheckerMaker(proto, op_checker) { |
| 79 | + AddInput("Input", |
| 80 | + "(Tensor) Matrix with shape [batch_size, frame_size * 3] for the " |
| 81 | + "input."); |
| 82 | + AddInput("HiddenPrev", |
| 83 | + "(Tensor) Matrix with shape [batch_size, frame_size] for the " |
| 84 | + "states of previous time step."); |
| 85 | + AddInput("Weight", |
| 86 | + "(Tensor) Weight matrix with shape [frame_size, frame_size * 3]. " |
| 87 | + "The elements continuous in memory can be divided into two parts. " |
| 88 | + "The first part are weights of the update gate and reset gate " |
| 89 | + "with shape [frame_size, frame_size * 2], and the second part are " |
| 90 | + "weights of output candidate with shape [frame_size, frame_size]"); |
| 91 | + AddInput("Bias", |
| 92 | + "(Tensor) Bias vector with shape [1, frame_size * 3] concating " |
| 93 | + "bias of the update gate, reset gate and output candidate."); |
| 94 | + AddOutput("Gate", |
| 95 | + "(Tensor) Matrix with shape [batch_size, frame_size * 3] for the " |
| 96 | + "output of update gate, reset gate and output candidate") |
| 97 | + .AsIntermediate(); |
| 98 | + AddOutput("ResetHiddenPrev", |
| 99 | + "(Tensor) Matrix with shape [batch_size, frame_size] for the " |
| 100 | + "reseted hidden state of previous time step.") |
| 101 | + .AsIntermediate(); |
| 102 | + AddOutput("Hidden", |
| 103 | + "(Tensor) The GRU hidden state of the current time step " |
| 104 | + "with shape [batch_size, frame_size]."); |
| 105 | + AddAttr<int>("activation", |
| 106 | + "(enum int, default tanh) " |
| 107 | + "The activation type used for output candidate {h}_t.") |
| 108 | + .SetDefault(tanh) |
| 109 | + .InEnum({identity, sigmoid, tanh, relu}); |
| 110 | + AddAttr<int>("gate_activation", |
| 111 | + "(enum int, default sigmoid) " |
| 112 | + "The activation type used in update gate and reset gate.") |
| 113 | + .SetDefault(sigmoid) |
| 114 | + .InEnum({identity, sigmoid, tanh, relu}); |
| 115 | + AddComment(R"DOC( |
| 116 | +GRUUnitOp implements part calculations of the GRU unit as following: |
| 117 | +
|
| 118 | +\f[ |
| 119 | +update \ gate: u_t = actGate(xu_t + W_u * hidden_prev + bias_u) \\ |
| 120 | +reset \ gate: r_t = actGate(xr_t + W_r * hidden_prev + bias_r) \\ |
| 121 | +output \ candidate: {h}_t = actNode(xc_t + W_c * dot(r_t, hidden_prev) + bias_c) \\ |
| 122 | +output: h_t = dot((1-u_t), {h}_t) + dot(u_t, hidden_prev) |
| 123 | +\f] |
| 124 | +
|
| 125 | +The rest of GRU unit can be completed by using FCOp's output as the input of GRUUnitOp. |
| 126 | +)DOC"); |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +class GRUUnitGradOp : public framework::OperatorWithKernel { |
| 131 | + public: |
| 132 | + using framework::OperatorWithKernel::OperatorWithKernel; |
| 133 | + |
| 134 | + protected: |
| 135 | + void InferShape(framework::InferShapeContext* ctx) const override { |
| 136 | + PADDLE_ENFORCE(ctx->HasInput("Input"), |
| 137 | + "Input(%s) of GRUUnitGradOp should not be null.", "Input"); |
| 138 | + PADDLE_ENFORCE(ctx->HasInput("HiddenPrev"), |
| 139 | + "Input(%s) of GRUUnitGradOp should not be null.", |
| 140 | + "HiddenPrev"); |
| 141 | + PADDLE_ENFORCE(ctx->HasInput("Weight"), |
| 142 | + "Input(%s) of GRUUnitGradOp should not be null.", "Weight"); |
| 143 | + PADDLE_ENFORCE(ctx->HasInput("Gate"), |
| 144 | + "Input(%s) of GRUUnitGradOp should not be null.", "Gate"); |
| 145 | + PADDLE_ENFORCE(ctx->HasInput("ResetHiddenPrev"), |
| 146 | + "Input(%s) of GRUUnitGradOp should not be null.", |
| 147 | + "ResetHiddenPrev"); |
| 148 | + PADDLE_ENFORCE(ctx->HasInput("Hidden"), |
| 149 | + "Input(%s) of GRUUnitGradOp should not be null.", "Hidden"); |
| 150 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Gate")), |
| 151 | + "Input(%s@GRAD) of GRUUnitGradOp should not be null.", |
| 152 | + "Gate"); |
| 153 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("ResetHiddenPrev")), |
| 154 | + "Input(%s@GRAD) of GRUUnitGradOp should not be null.", |
| 155 | + "ResetHiddenPrev"); |
| 156 | + PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Hidden")), |
| 157 | + "Input(%s@GRAD) of GRUUnitGradOp should not be null.", |
| 158 | + "Hidden"); |
| 159 | + auto input_dims = ctx->GetInputDim("Input"); |
| 160 | + auto hidden_prev_dims = ctx->GetInputDim("HiddenPrev"); |
| 161 | + auto weight_dims = ctx->GetInputDim("Weight"); |
| 162 | + // int batch_size = input_dims[0]; |
| 163 | + int input_size = input_dims[1]; |
| 164 | + int frame_size = hidden_prev_dims[1]; |
| 165 | + int weight_height = weight_dims[0]; |
| 166 | + int weight_width = weight_dims[1]; |
| 167 | + PADDLE_ENFORCE_EQ( |
| 168 | + input_size, frame_size * 3, |
| 169 | + "The input_size must be 3 times of frame_size in GRUUnitOp."); |
| 170 | + PADDLE_ENFORCE_EQ( |
| 171 | + weight_height, frame_size, |
| 172 | + "The shape of Weight matrix must be [frame_size, frame_size * 3]."); |
| 173 | + PADDLE_ENFORCE_EQ( |
| 174 | + weight_width, frame_size * 3, |
| 175 | + "The shape of Weight matrix must be [frame_size, frame_size * 3]."); |
| 176 | + auto bias = Input("Bias"); |
| 177 | + if (bias != framework::kEmptyVarName) { |
| 178 | + auto bias_dims = ctx->GetInputDim("Bias"); |
| 179 | + int bias_height = bias_dims[0]; |
| 180 | + int bias_width = bias_dims[1]; |
| 181 | + PADDLE_ENFORCE_EQ(bias_height, 1, |
| 182 | + "The shape of Bias must be [1, frame_size * 3]."); |
| 183 | + PADDLE_ENFORCE_EQ(bias_width, frame_size * 3, |
| 184 | + "The shape of Bias must be [1, frame_size * 3]."); |
| 185 | + auto bias_grad_name = framework::GradVarName("Bias"); |
| 186 | + if (ctx->HasOutput(bias_grad_name)) |
| 187 | + ctx->SetOutputDim(bias_grad_name, bias_dims); |
| 188 | + } |
| 189 | + auto input_grad_name = framework::GradVarName("Input"); |
| 190 | + if (ctx->HasOutput(input_grad_name)) |
| 191 | + ctx->SetOutputDim(input_grad_name, input_dims); |
| 192 | + auto hidden_prev_grad_name = framework::GradVarName("HiddenPrev"); |
| 193 | + if (ctx->HasOutput(hidden_prev_grad_name)) |
| 194 | + ctx->SetOutputDim(hidden_prev_grad_name, hidden_prev_dims); |
| 195 | + auto weight_grad_name = framework::GradVarName("Weight"); |
| 196 | + if (ctx->HasOutput(weight_grad_name)) |
| 197 | + ctx->SetOutputDim(weight_grad_name, weight_dims); |
| 198 | + } |
| 199 | +}; |
| 200 | + |
| 201 | +} // namespace operators |
| 202 | +} // namespace paddle |
| 203 | + |
| 204 | +namespace ops = paddle::operators; |
| 205 | +REGISTER_OP(gru_unit, ops::GRUUnitOp, ops::GRUUnitOpMaker, gru_unit_grad, |
| 206 | + ops::GRUUnitGradOp); |
| 207 | +REGISTER_OP_CPU_KERNEL(gru_unit, |
| 208 | + ops::GRUUnitKernel<paddle::platform::CPUPlace, float>); |
| 209 | +REGISTER_OP_CPU_KERNEL( |
| 210 | + gru_unit_grad, ops::GRUUnitGradKernel<paddle::platform::CPUPlace, float>); |
0 commit comments