sequence_enumerate_op.cc 3.8 KB
Newer Older
C
chenweihang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//   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.

W
Wu Yi 已提交
15
#include "paddle/fluid/operators/sequence_ops/sequence_enumerate_op.h"
C
chenweihang 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32

namespace paddle {
namespace operators {

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

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

    const auto x_dims = ctx->GetInputDim("X");
C
chenweihang 已提交
33
    PADDLE_ENFORCE_EQ(
T
tensor-tang 已提交
34
        x_dims.size(), 2,
C
chenweihang 已提交
35
        "Input(X) of SequenceEnumerate operator's rank should be 2.");
36 37 38
    PADDLE_ENFORCE_EQ(x_dims[1], 1,
                      "Input(X) of SequenceEnumerate operator's 2nd "
                      "dimension should be 1.");
C
chenweihang 已提交
39 40

    const auto win_size = ctx->Attrs().Get<int>("win_size");
C
chenweihang 已提交
41
    ctx->SetOutputDim("Out", {x_dims[0], win_size});
C
chenweihang 已提交
42 43 44 45 46 47 48 49 50 51 52
    ctx->ShareLoD("X", "Out");
  }
};

class SequenceEnumerateOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X",
             "(2-D LoDTensor with the 2nd dimension equal to 1) "
             "Input LoDTensor of SequenceEnumerate operator.");
    AddOutput("Out",
C
chenweihang 已提交
53
              "(2-D LoDTensor with the 2nd dimension equal to win_size) "
C
chenweihang 已提交
54 55 56 57
              "Output LoDTensor of SequenceEnumerate operator.");
    AddAttr<int>("win_size", "(int) The enumerate sequence window size.")
        .AddCustomChecker([](const int& win_size) {
          PADDLE_ENFORCE(win_size >= 2,
C
chenweihang 已提交
58
                         "The window size should be not less than 2.");
C
chenweihang 已提交
59 60 61
        });
    AddAttr<int>("pad_value", "(int) The enumerate sequence padding value.")
        .SetDefault(0);
62 63 64 65 66 67 68 69
    AddAttr<bool>(
        "all_kernels_must_compute_runtime_shape",
        "(boolean, default true) "
        "An attribute to speed up OperatorWithKernel::RunImpl."
        "If true, all the kernels of this Op would compute runtime "
        "shape, but skip infershape in runtime. Note that it is a temporal "
        "attribute, please do DOT set it in python layer.")
        .SetDefault(true);
C
chenweihang 已提交
70 71 72
    AddComment(R"DOC(
Sequence Enumerate Operator.

C
chenweihang 已提交
73
Generate a new sequence for the input index sequence, which enumerates all the
C
chenweihang 已提交
74 75 76
sub-sequences with length `win_size` of the input. 
The enumerated sequence has the same 1st dimension with variable `input`, and
the 2nd dimension is `win_size`, padded by `pad_value` if necessary in generation.
C
chenweihang 已提交
77
    
C
chenweihang 已提交
78 79 80 81
Examples:
Case 1:
  Input:
    X.lod = [[0, 3, 5]]
82
    X.data = [[1], [2], [3], [4], [5]]
C
chenweihang 已提交
83 84 85 86 87 88
    X.dims = [5, 1]
  Attrs:
    win_size = 2
    pad_value = 0
  Output:
    Out.lod = [[0, 3, 5]]
89
    Out.data = [[1, 2], [2, 3], [3, 0], [4, 5], [5, 0]]
C
chenweihang 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
    Out.dims = [5, 2]

)DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP_WITHOUT_GRADIENT(sequence_enumerate, ops::SequenceEnumerateOp,
                             ops::SequenceEnumerateOpMaker);
REGISTER_OP_CPU_KERNEL(
    sequence_enumerate,
    ops::SequenceEnumerateKernel<paddle::platform::CPUDeviceContext, int32_t>,
    ops::SequenceEnumerateKernel<paddle::platform::CPUDeviceContext, int64_t>);