lstm_unit_op.cc 3.7 KB
Newer Older
Z
zchen0211 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/* 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/lstm_unit_op.h"

namespace paddle {
namespace operators {

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

 protected:
Q
Qiao Longfei 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38
  void InferShape(framework::InferShapeContextBase* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) of LSTM should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("C_prev"),
                   "Input(C_prev) of LSTM should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("C"),
                   "Output(C) of LSTM should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("H"),
                   "Output(H) of LSTM should not be null.");

    auto x_dims = ctx->GetInputDim("X");
    auto c_prev_dims = ctx->GetInputDim("C_prev");

    PADDLE_ENFORCE_EQ(x_dims.size(), 2, "Input(X)'s rank must be 2.");
    PADDLE_ENFORCE(x_dims[0] == c_prev_dims[0],
Z
zchen0211 已提交
39
                   "Batch size of inputs and states must be equal");
Q
Qiao Longfei 已提交
40
    PADDLE_ENFORCE(x_dims[1] == c_prev_dims[1] * 4,
Z
zchen0211 已提交
41 42
                   "Dimension of FC should equal to prev state * 4");

Q
Qiao Longfei 已提交
43 44 45 46
    int b_size = c_prev_dims[0];  // batch size
    int s_dim = c_prev_dims[1];   // state dim
    ctx->SetOutputDim("C", {b_size, s_dim});
    ctx->SetOutputDim("H", {b_size, s_dim});
Z
zchen0211 已提交
47 48 49 50 51 52
  }
};

template <typename AttrType>
class LstmUnitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Q
Qiao Longfei 已提交
53 54
  LstmUnitOpMaker(framework::OpProto* proto,
                  framework::OpAttrChecker* op_checker)
Z
zchen0211 已提交
55 56 57 58 59 60 61 62 63 64
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X", "FC input before the non-linear activation.");
    AddInput(
        "C_prev",
        "The cell state tensor of last time-step in the Lstm Unit operator.");
    AddOutput("C", "The cell tensor of Lstm Unit operator.");
    AddOutput("H", "The hidden state tensor of Lstm Unit operator.");

    AddComment(R"DOC(Lstm-Unit Operator

Q
Qiao Longfei 已提交
65
Equation:
Z
lstm  
zchen0211 已提交
66
  i, f, o, j = split(X)
Z
zchen0211 已提交
67 68
  C = C_prev * sigm(f + forget_bias) + sigm(i) * tanh(j)
  H = C * sigm(o)
Q
Qiao Longfei 已提交
69

Z
zchen0211 已提交
70 71 72 73 74 75 76 77 78 79 80
)DOC");
    AddAttr<AttrType>("forget_bias", "The forget bias of Lstm Unit.")
        .SetDefault(0.0);
  }
};

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

 protected:
Q
Qiao Longfei 已提交
81 82 83 84 85 86 87 88
  void InferShape(framework::InferShapeContextBase* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("C")),
                   "Input(C@GRAD) should not be null");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("H")),
                   "Input(H@GRAD) should not be null");
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
    ctx->SetOutputDim(framework::GradVarName("C_prev"),
                      ctx->GetInputDim("C_prev"));
Z
zchen0211 已提交
89 90 91 92 93 94 95 96 97 98 99
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(lstm_unit, ops::LstmUnitOp, ops::LstmUnitOpMaker<float>,
            lstm_unit_grad, ops::LstmUnitGradOp);
REGISTER_OP_CPU_KERNEL(lstm_unit,
                       ops::LstmUnitKernel<paddle::platform::CPUPlace, float>);
Z
lstm  
zchen0211 已提交
100 101
REGISTER_OP_CPU_KERNEL(
    lstm_unit_grad, ops::LstmUnitGradKernel<paddle::platform::CPUPlace, float>);