sequence_concat_op.cc 5.1 KB
Newer Older
Y
Yancey1989 已提交
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/sequence_concat_op.h"

namespace paddle {
namespace operators {

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

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

class SequenceConcatOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  SequenceConcatOpMaker(framework::OpProto* proto,
                        framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X",
Y
Yancey1989 已提交
51 52
             "(A vector of LoDTensor), the input is a vector of LoDTensor, "
             "each of which is a variable-length sequence or nested sequence.")
Y
Yancey1989 已提交
53 54
        .AsDuplicable();
    AddOutput("Out",
Y
Yancey1989 已提交
55
              "(A LoDTensor), the variable-length output of "
Y
Yancey1989 已提交
56
              "sequence_concat Op.");
Y
Yancey1989 已提交
57
    AddAttr<int>("axis",
Y
Yancey1989 已提交
58
                 "(int, default 0)"
Y
Yancey1989 已提交
59
                 "The axis which the inputs will be joined with."
Y
Yancey1989 已提交
60
                 "If axis is 0, the inputs will be joined with LoD index.")
Y
Yancey1989 已提交
61 62
        .SetDefault(0);
    AddAttr<int>("level",
Y
Yancey1989 已提交
63
                 "(int, default 0)"
Y
Yancey1989 已提交
64 65 66 67 68
                 "The level at which the inputs will be joined."
                 "If the level is 0, the inputs will be joined at the nested "
                 "sequence level."
                 "If the level is 1, the inputs will be joined at the "
                 "sequence level.")
Y
Yancey1989 已提交
69 70
        .SetDefault(0);
    AddComment(R"DOC(
Y
Yancey1989 已提交
71
    The sequence_concat operator concatenates multiple LoDTensors. 
Y
Yancey1989 已提交
72 73
    It only supports sequence (LoD Tensor with level number is 1) 
    or a nested sequence (LoD tensor with level number is 2) as its input.
Y
Yancey1989 已提交
74
    - Case1:
Y
Yancey1989 已提交
75 76 77 78 79 80
      If the axis is other than 0(here, axis is 1 and level is 1),
      each input should have the same LoD information and the LoD 
      information of the output keeps the same as the input.
      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
Yancey1989 已提交
81
    - Case2:
Y
Yancey1989 已提交
82 83 84 85 86 87 88 89 90 91 92 93
      If the axis is 0(here, leve is 0), the inputs are concatenated along 
      time steps, the LoD information of the output need to re-compute.
      LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
      LoD(x1) = {{0,3,5}, {0,1,2,3,5}}; Dims(x1) = (5,3,4)
      LoD(Out) = {{0,5,9}, {0,1,2,3,4,5,6,7,9}}; Dims(Out) = (9,3,4)
    - Case3:
      If the axis is 0(here, level is 1).
      LoD(x0) = {{0,2,4}, {0,1,2,3,4}}; Dims(x0) = (4,3,4)
      LoD(x1) = {{0,3,5}, {0,1,3,4,5}}; Dims(x1) = (5,3,4)
      LoD(Out) = {{0,5,9}, {0,2,5,7,9}}; Dims(Out) = (9,3,4)
      
    NOTE: The levels of all the inputs should be the same.
Y
Yancey1989 已提交
94 95 96 97 98 99 100 101 102
    )DOC");
  }
};

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

 protected:
Y
update  
Yancey1989 已提交
103
  void InferShape(framework::InferShapeContext* ctx) const override {
Y
Yancey1989 已提交
104
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
Y
Yancey1989 已提交
105 106
                   "The gradient of Out should not be null.");
    PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName("X")),
Y
Yancey1989 已提交
107
                   "The gradient of X should not be null.");
Y
Yancey1989 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    ctx->SetOutputsDim(framework::GradVarName("X"), ctx->GetInputsDim("X"));
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(sequence_concat, ops::SequenceConcatOp, ops::SequenceConcatOpMaker,
            sequence_concat_grad, ops::SequenceConcatGradOp);
REGISTER_OP_CPU_KERNEL(
    sequence_concat,
    ops::SequenceConcatOpKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
    sequence_concat_grad,
    ops::SequenceConcatGradOpKernel<paddle::platform::CPUPlace, float>);