lstm_unit_op.cc 4.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Z
zchen0211 已提交
2

L
Luo Tao 已提交
3 4 5
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
Z
zchen0211 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
Z
zchen0211 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
Z
zchen0211 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/lstm_unit_op.h"
16
#include <memory>
Z
zchen0211 已提交
17 18 19 20 21 22 23 24

namespace paddle {
namespace operators {

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

25
  void InferShape(framework::InferShapeContext* ctx) const override {
26 27
    OP_INOUT_CHECK(ctx->HasInput("X"), "Input", "X", "lstm_unit");
    OP_INOUT_CHECK(ctx->HasInput("C_prev"), "Input", "C_prev", "lstm_unit");
S
smallv0221 已提交
28 29
    OP_INOUT_CHECK(ctx->HasOutput("C"), "Output", "C", "lstm_unit");
    OP_INOUT_CHECK(ctx->HasOutput("H"), "Output", "H", "lstm_unit");
Q
Qiao Longfei 已提交
30 31 32 33

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

34 35
    PADDLE_ENFORCE_EQ(x_dims.size(), 2, platform::errors::InvalidArgument(
                                            "Input(X)'s rank must be 2."));
36 37
    if (ctx->IsRuntime()) {
      PADDLE_ENFORCE_EQ(x_dims[0], c_prev_dims[0],
38 39
                        platform::errors::InvalidArgument(
                            "Batch size of inputs and states must be equal"));
40
      PADDLE_ENFORCE_EQ(x_dims[1], c_prev_dims[1] * 4,
41 42
                        platform::errors::InvalidArgument(
                            "Dimension of FC should equal to prev state * 4"));
43
    }
Z
zchen0211 已提交
44

Q
Qiao Longfei 已提交
45 46 47 48
    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 已提交
49 50 51 52 53
  }
};

class LstmUnitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
54
  void Make() override {
Y
yangyaming 已提交
55 56 57 58
    AddInput("X",
             "Lstm unit only applies non-linear activations, please make sure"
             "that linear tranformation has already been applied to `X`. "
             "Linear tranformation can be applied by adding a `fc` layer");
Z
zchen0211 已提交
59 60 61 62 63
    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.");
K
kexinzhao 已提交
64 65 66 67 68 69
    AddAttr<float>("forget_bias",
                   "(float, default 0.0) "
                   "The forget bias of Lstm Unit.")
        .SetDefault(0.0);
    AddComment(R"DOC(
Lstm Unit Operator
Z
zchen0211 已提交
70

Q
Qiao Longfei 已提交
71
Equation:
K
kexinzhao 已提交
72 73 74 75 76 77

$$
i, f, o, j = split(X) \\
C = C_{prev} * sigm(f + forget\_bias) + sigm(i) * tanh(j) \\
H = C * sigm(o)
$$
Q
Qiao Longfei 已提交
78

Z
zchen0211 已提交
79 80 81 82 83 84 85 86
)DOC");
  }
};

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

87
  void InferShape(framework::InferShapeContext* ctx) const override {
88 89 90 91
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("C")), "Input",
                   framework::GradVarName("C"), "lstm_unit");
    OP_INOUT_CHECK(ctx->HasInput(framework::GradVarName("H")), "Input",
                   framework::GradVarName("H"), "lstm_unit");
Q
Qiao Longfei 已提交
92 93 94
    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
    ctx->SetOutputDim(framework::GradVarName("C_prev"),
                      ctx->GetInputDim("C_prev"));
Z
zchen0211 已提交
95 96 97
  }
};

98 99 100 101 102 103
template <typename T>
class LstmUnitGradOpMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
104
  void Apply(GradOpPtr<T> op) const override {
105 106 107 108 109 110 111 112 113 114 115 116
    op->SetType("lstm_unit_grad");
    op->SetInput("X", this->Input("X"));
    op->SetInput("C_prev", this->Input("C_prev"));
    op->SetInput("C", this->Output("C"));
    op->SetInput(framework::GradVarName("H"), this->OutputGrad("H"));
    op->SetInput(framework::GradVarName("C"), this->OutputGrad("C"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetOutput(framework::GradVarName("C_prev"), this->InputGrad("C_prev"));
    op->SetAttrMap(this->Attrs());
  }
};

Z
zchen0211 已提交
117 118 119 120
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
121 122 123
REGISTER_OPERATOR(lstm_unit, ops::LstmUnitOp, ops::LstmUnitOpMaker,
                  ops::LstmUnitGradOpMaker<paddle::framework::OpDesc>,
                  ops::LstmUnitGradOpMaker<paddle::imperative::OpBase>);
124
REGISTER_OPERATOR(lstm_unit_grad, ops::LstmUnitGradOp);
Z
zchen0211 已提交
125
REGISTER_OP_CPU_KERNEL(lstm_unit,
126 127
                       ops::LstmUnitKernel<paddle::platform::CPUPlace, float>,
                       ops::LstmUnitKernel<paddle::platform::CPUPlace, double>);
Z
lstm  
zchen0211 已提交
128
REGISTER_OP_CPU_KERNEL(
129 130
    lstm_unit_grad, ops::LstmUnitGradKernel<paddle::platform::CPUPlace, float>,
    ops::LstmUnitGradKernel<paddle::platform::CPUPlace, double>);