sequence_concat_op.cc 4.9 KB
Newer Older
C
chengduoZH 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
// Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
//
// 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
Yancey1989 已提交
14

W
Wu Yi 已提交
15
#include "paddle/fluid/operators/sequence_ops/sequence_concat_op.h"
16
#include <memory>
C
chengduoZH 已提交
17
#include <vector>
Y
Yancey1989 已提交
18 19 20 21

namespace paddle {
namespace operators {

C
chengduoZH 已提交
22
class SeqConcatOpMaker : public framework::OpProtoAndCheckerMaker {
Y
Yancey1989 已提交
23
 public:
C
chengduoZH 已提交
24 25 26 27 28 29 30 31 32 33 34
  void Make() override {
    AddInput("X", "The inputs of sequence concat op").AsDuplicable();
    AddOutput("Out", "The output of sequence concat op");
    AddComment(
        "Sequence Concat Op\n"
        "It will concat LoD tensors by its sequence information.\n"
        "For example:\n"
        "  LoD of X1 = [0, 3, 7]\n"
        "  LoD of X2 = [0, 7, 9]\n"
        "  Result LoD is [0, (3+7), (7+9)]\n"
        "            i.e.[0, 10, 16]\n");
Y
Yancey1989 已提交
35 36 37
  }
};

Z
Zeng Jinle 已提交
38
class SequenceConcatOp : public framework::OperatorWithKernel {
Y
Yancey1989 已提交
39
 public:
Z
Zeng Jinle 已提交
40 41 42 43
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext *context) const override {
C
chengduoZH 已提交
44 45 46 47
    PADDLE_ENFORCE(context->HasInputs("X"),
                   "Input(X) of Sequence Concat Op should not be null.");
    PADDLE_ENFORCE(context->HasOutput("Out"),
                   "Output(Out) of Sequence Concat Op should not be null.");
C
chengduoZH 已提交
48

C
chengduoZH 已提交
49 50 51 52 53 54 55 56 57
    PADDLE_ENFORCE_GT(context->Inputs("X").size(), 1,
                      "The number of input sequences is at least two.");
    auto x_dims = context->GetInputsDim("X");
    int64_t batch_size = 0;
    int64_t feature_size = 0;
    std::vector<int64_t> out_dims;
    for (auto &x_dim : x_dims) {
      if (out_dims.empty()) {
        out_dims = framework::vectorize(x_dim);
C
chengduoZH 已提交
58
      }
C
chengduoZH 已提交
59 60 61 62 63 64 65
      batch_size += x_dim[0];
      if (feature_size == 0) {
        feature_size = framework::product(x_dim) / x_dim[0];
      } else {
        PADDLE_ENFORCE_EQ(
            feature_size, framework::product(x_dim) / x_dim[0],
            "Inputs of sequence concat must have same feature size");
C
chengduoZH 已提交
66
      }
C
chengduoZH 已提交
67 68 69 70 71 72 73 74 75
    }
    if (batch_size < 0) {
      batch_size = -1;  // Normalize batch size for compile time.
    }
    out_dims[0] = batch_size;
    context->SetOutputDim("Out", framework::make_ddim(out_dims));
    if (!context->IsRuntime()) {  // Runtime LoD infershape will be computed
      // in Kernel.
      context->ShareLoD("X", "Out");
C
chengduoZH 已提交
76
    }
Y
Yancey1989 已提交
77 78 79
  }
};

H
hong 已提交
80 81
template <typename T>
class SeqConcatGradOpMaker : public framework::SingleGradOpMaker<T> {
Y
Yancey1989 已提交
82
 public:
H
hong 已提交
83
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
84 85

 protected:
H
hong 已提交
86 87
  std::unique_ptr<T> Apply() const override {
    std::unique_ptr<T> op(new T());
88
    op->SetType("sequence_concat_grad");
H
hong 已提交
89 90 91 92
    op->SetInput("X", this->Input("X"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X", false));
    op->SetAttrMap(this->Attrs());
93 94 95 96 97 98 99 100 101
    return op;
  }
};

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

  void InferShape(framework::InferShapeContext *context) const override {
C
chengduoZH 已提交
102 103
    context->SetOutputsDim(framework::GradVarName("X"),
                           context->GetInputsDim("X"));
Y
Yancey1989 已提交
104
  }
105 106 107 108

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
109 110 111
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.GetPlace());
112
  }
Y
Yancey1989 已提交
113
};
114 115 116 117

DECLARE_NO_NEED_BUFFER_VARS_INFERENCE(SeqConcatGradNoNeedBufferVarsInference,
                                      "X");

Y
Yancey1989 已提交
118 119 120
}  // namespace operators
}  // namespace paddle

C
chengduoZH 已提交
121 122
namespace op = paddle::operators;

Z
Zeng Jinle 已提交
123
REGISTER_OPERATOR(sequence_concat, op::SequenceConcatOp, op::SeqConcatOpMaker,
H
hong 已提交
124 125
                  op::SeqConcatGradOpMaker<paddle::framework::OpDesc>,
                  op::SeqConcatGradOpMaker<paddle::imperative::OpBase>);
C
chengduoZH 已提交
126 127
template <typename T>
using Kernel = op::SeqConcatKernel<paddle::platform::CPUDeviceContext, T>;
M
minqiyang 已提交
128 129
REGISTER_OP_CPU_KERNEL(sequence_concat, Kernel<float>, Kernel<double>,
                       Kernel<int64_t>);
M
minqiyang 已提交
130

131 132
REGISTER_OPERATOR(sequence_concat_grad, op::SeqConcatGradOp,
                  op::SeqConcatGradNoNeedBufferVarsInference);
C
chengduoZH 已提交
133 134 135 136
template <typename T>
using GradKernel =
    op::SeqConcatGradKernel<paddle::platform::CPUDeviceContext, T>;
REGISTER_OP_CPU_KERNEL(sequence_concat_grad, GradKernel<float>,
M
minqiyang 已提交
137
                       GradKernel<double>, GradKernel<int64_t>);