sequence_concat_op.cc 5.4 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yancey1989 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/sequence_concat_op.h"
Y
Yancey1989 已提交
16 17 18 19 20 21 22 23

namespace paddle {
namespace operators {

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

Y
update  
Yancey1989 已提交
24
  void InferShape(framework::InferShapeContext* ctx) const override {
Y
Yancey1989 已提交
25 26
    PADDLE_ENFORCE(ctx->HasInputs("X"),
                   "Inputs(X) of SequenceConcatOp should not be null.");
Y
Yancey1989 已提交
27 28 29 30 31
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of SequenceConcatOp should not be null.");
    const size_t level = static_cast<size_t>(ctx->Attrs().Get<int>("level"));
    const size_t axis = static_cast<size_t>(ctx->Attrs().Get<int>("axis"));
    PADDLE_ENFORCE(level == 0UL || level == 1UL,
Y
Yancey1989 已提交
32 33
                   "The sequence_concat operator only accepts sequence "
                   "or a nested sequence as its input.");
Y
Yancey1989 已提交
34 35 36
    auto ins_dims = ctx->GetInputsDim("X");
    framework::DDim out_dims = ins_dims[0];
    const size_t n = ins_dims.size();
Y
Yancey1989 已提交
37
    for (size_t i = 1; i < n; ++i) {
Y
Yancey1989 已提交
38 39 40 41 42 43 44 45
      out_dims[axis] += ins_dims[i][axis];
    }
    ctx->SetOutputDim("Out", out_dims);
  }
};

class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
46
  SequenceConcatOpMaker(OpProto* proto, OpAttrChecker* op_checker)
Y
Yancey1989 已提交
47 48
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X",
Q
Qiao Longfei 已提交
49
             "(LodTensorArray) Input is a vector of LoDTensor, "
Y
Yancey1989 已提交
50
             "each of which is a variable-length sequence or nested sequence.")
Y
Yancey1989 已提交
51 52
        .AsDuplicable();
    AddOutput("Out",
53
              "(LoDTensor), Variable-length output of "
Y
Yancey1989 已提交
54
              "sequence_concat Op.");
Y
Yancey1989 已提交
55
    AddAttr<int>("axis",
56 57
                 "(int, default 0) "
                 "The axis along which the inputs will be joined. "
Y
Yancey1989 已提交
58
                 "If axis is 0, the inputs will be joined with LoD index.")
Y
Yancey1989 已提交
59 60
        .SetDefault(0);
    AddAttr<int>("level",
61
                 "(int, default 0) "
Y
update  
Yancey1989 已提交
62
                 "The level at which the inputs will be joined. "
Y
Yancey1989 已提交
63
                 "If the level is 0, the inputs will be joined at the nested "
Y
update  
Yancey1989 已提交
64
                 "sequence level. "
Y
Yancey1989 已提交
65
                 "If the level is 1, the inputs will be joined at the "
Y
update  
Yancey1989 已提交
66 67
                 "sequence level. "
                 "The level should be less than the level number of inputs.")
Y
Yancey1989 已提交
68 69
        .SetDefault(0);
    AddComment(R"DOC(
70 71
The sequence_concat operator concatenates multiple LoDTensors.
It only supports sequence (LoD Tensor with level number is 1)
72 73 74
or a nested sequence (LoD tensor with level number is 2) as its input.
- Case1:
  If the axis is other than 0(here, axis is 1 and level is 1),
75
  each input should have the same LoD information and the LoD
76
  information of the output keeps the same as the input.
Y
update  
Yancey1989 已提交
77

78 79 80
  LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
  LoD(x1) = {{0,2,4}, {0,1,2,3,4}}; Dims(x1) = (4,4,4)
  LoD(Out) = {{0,2,4}, {0,1,2,3,4}}; Dims(Out) = (4,7,4)
Y
update  
Yancey1989 已提交
81

82
- Case2:
83
  If the axis is 0(here, leve is 0), the inputs are concatenated along
84
  time steps, the LoD information of the output need to re-compute.
85
  The LoD information of level-1 should be same.
Y
update  
Yancey1989 已提交
86

87 88 89
  LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
  LoD(x1) = {{0,2,4}, {0,1,3,5,7}}; Dims(x1) = (7,3,4)
  LoD(Out) = {{0,2,4}, {0,2,5,8,11}}; Dims(Out) = (11,3,4)
Y
update  
Yancey1989 已提交
90

91 92
- Case3:
  If the axis is 0(here, level is 1).
W
wanghaoshuang 已提交
93

94 95 96
  LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
  LoD(x1) = {{0,3,4}, {0,1,3,5,7}}; Dims(x1) = (7,3,4)
  LoD(Out) = {{0,5,8}, {0,1,2,3,5,7,8,9,11}}; Dims(Out) = (11,3,4)
97

98 99
- Case4:
  If the LoD number is 1, axis is 0, level is 0
100

101 102 103 104 105
  LoD(x0) = {{0,1,2,3,4}}; Dims(x0) = (4,3,4)
  LoD(x1) = {{0,1,3,5,7}}; Dims(x1) = (7,3,4)
  LoD(Out) = {{0,2,5,8,11}}; Dims(Out) = (11,3,4)

NOTE: The levels of all the inputs should be the same.
Y
Yancey1989 已提交
106 107 108 109 110 111 112 113
    )DOC");
  }
};

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

Y
update  
Yancey1989 已提交
114
  void InferShape(framework::InferShapeContext* ctx) const override {
Y
Yancey1989 已提交
115
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
Y
Yancey1989 已提交
116 117
                   "The gradient of Out should not be null.");
    PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName("X")),
Y
Yancey1989 已提交
118
                   "The gradient of X should not be null.");
Y
Yancey1989 已提交
119 120 121 122 123 124 125 126
    ctx->SetOutputsDim(framework::GradVarName("X"), ctx->GetInputsDim("X"));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
127 128 129 130 131
REGISTER_OPERATOR(sequence_concat, ops::SequenceConcatOp,
                  ops::SequenceConcatOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<
                      false> /* set false to disable empty grad */)
REGISTER_OPERATOR(sequence_concat_grad, ops::SequenceConcatGradOp);
Y
Yancey1989 已提交
132 133
REGISTER_OP_CPU_KERNEL(
    sequence_concat,
Q
QI JUN 已提交
134
    ops::SequenceConcatOpKernel<paddle::platform::CPUDeviceContext, float>);
Y
Yancey1989 已提交
135 136
REGISTER_OP_CPU_KERNEL(
    sequence_concat_grad,
Q
QI JUN 已提交
137
    ops::SequenceConcatGradOpKernel<paddle::platform::CPUDeviceContext, float>);