sequence_pad_op.cc 5.3 KB
Newer Older
Y
yangyaming 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/* 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. */

#include "paddle/fluid/operators/sequence_pad_op.h"

namespace paddle {
namespace operators {

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

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of SequencePadOp should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of SequencePadOp should not be null.");

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

    PADDLE_ENFORCE_EQ(x_dims.size(), 2,
                      "Only support 2-D tensor, rank of Input(X) should be 2.");

35 36 37 38 39
    int lod_level = ctx->Attrs().Get<int>("lod_level");

    int64_t max_len = -1;
    int64_t seq_num = -1;
    int x_lod_size = -1;
Y
yangyaming 已提交
40 41 42 43 44 45 46

    if (ctx->IsRuntime()) {
      framework::Variable* x_var =
          boost::get<framework::Variable*>(ctx->GetInputVarPtrs("X")[0]);

      auto& x_lod = x_var->Get<LoDTensor>().lod();

47 48 49 50 51 52 53
      x_lod_size = x_lod.size();

      auto x_abs_offset = framework::ToAbsOffset(x_lod)[lod_level];

      PADDLE_ENFORCE_EQ(x_dims[0], static_cast<int64_t>(x_abs_offset.back()),
                        "The first dimension of `X` should be equal to sum "
                        "of all sequences' length.");
Y
yangyaming 已提交
54

55
      seq_num = x_abs_offset.size() - 1;
Y
yangyaming 已提交
56

Y
yangyaming 已提交
57
      for (int64_t i = 1; i <= seq_num; ++i) {
58
        int64_t seq_len = x_abs_offset[i] - x_abs_offset[i - 1];
Y
yangyaming 已提交
59 60 61 62 63
        max_len = max_len < seq_len ? seq_len : max_len;
      }
    } else {
      framework::VarDesc* x_desc =
          boost::get<framework::VarDesc*>(ctx->GetInputVarPtrs("X")[0]);
64
      x_lod_size = x_desc->GetLoDLevel();
Y
yangyaming 已提交
65 66
    }

67 68 69 70 71
    PADDLE_ENFORCE(lod_level >= 0 && lod_level < x_lod_size,
                   "Invalid `lod_level` which should be at least 0 and less "
                   "than maximum lod level of `X`");

    ctx->SetOutputDim("Out", {seq_num, max_len, x_dims[1]});
Y
yangyaming 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
        ctx.device_context());
  }
};

class SequencePadOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  SequencePadOpMaker(OpProto* proto, OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
    AddInput("X",
             "(LoDTensor, default LoDTensor<float>) Input variable which "
             "should contain lod information. Length of each sequence would "
             "be computed from the most bottom level lod.");
    AddOutput("Out",
              "(Tensor) Output variable which would be a common tensor "
              "without lod. Each sequence would be padded to the maximum "
              "length.");
95 96
    AddAttr<float>("lod_level",
                   "(int, default 0) Specify which level lod to referred to.");
Y
yangyaming 已提交
97
    AddAttr<float>("pad_value",
98 99
                   "(float, default 0.0) Specify which value to be padded to "
                   "the end of each sequence.");
Y
yangyaming 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    AddComment(R"DOC(

    )DOC");
  }
};

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

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of SequencePadGradOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
                   "Input(Out@GRAD) of SequencePadGradOp should not be null.");

    if (ctx->HasOutput(framework::GradVarName("X"))) {
      ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
      ctx->ShareLoD("X", /*->*/ framework::GradVarName("X"));
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OPERATOR(sequence_pad, ops::SequencePadOp, ops::SequencePadOpMaker,
                  paddle::framework::DefaultGradOpDescMaker<true>);
REGISTER_OPERATOR(sequence_pad_grad, ops::SequencePadGradOp);
REGISTER_OP_CPU_KERNEL(
    sequence_pad,
    ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, double>,
    ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, int>,
    ops::SequencePadOpKernel<paddle::platform::CPUDeviceContext, int64_t>);
REGISTER_OP_CPU_KERNEL(
    sequence_pad_grad,
    ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, float>,
    ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, double>,
    ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, int>,
    ops::SequencePadGradOpKernel<paddle::platform::CPUDeviceContext, int64_t>);