sequence_slice_op.cc 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

15
#include "paddle/operators/sequence_slice_op.h"
16 17 18 19

namespace paddle {
namespace operators {

20
class SequenceSliceOp : public framework::OperatorWithKernel {
21 22 23 24 25
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
26 27 28 29 30
                   "Input(X) of SequenceSliceOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Offset"),
                   "Input(Offset) of SequenceSliceOp should not be null.");
    PADDLE_ENFORCE(ctx->HasInput("Length"),
                   "Input(Length) of SequenceSliceOp should not be null.");
31
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
32
                   "Output(Out) of SequenceSliceOp should not be null.");
33 34
    auto input_dims = ctx->GetInputDim("X");

35
    ctx->SetOutputDim("Out", input_dims);
36 37
    }

38 39 40 41 42 43
 protected:
  framework::OpKernelType GetKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(
        framework::ToDataType(ctx.Input<framework::LoDTensor>("X")->type()),
        ctx.device_context());
44 45 46
  }
};

47
class SequenceSliceGradOp : public framework::OperatorWithKernel {
48 49 50 51 52 53 54 55 56 57
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
                   "The gradient of Out should not be null.");
    PADDLE_ENFORCE(ctx->HasOutputs(framework::GradVarName("X")),
                   "The gradient of X should not be null.");
    ctx->SetOutputsDim(framework::GradVarName("X"), ctx->GetInputsDim("X"));
  }
58 59 60 61 62 63 64 65

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

68
class SequenceSliceOpMaker : public framework::OpProtoAndCheckerMaker {
69
 public:
70 71
  SequenceSliceOpMaker(framework::OpProto* proto,
                       framework::OpAttrChecker* op_checker)
72
      : OpProtoAndCheckerMaker(proto, op_checker) {
73 74 75 76 77
    AddInput("X",
             "(LoDTensor), "
             "the input of SequenceSliceOp.");
    AddInput("Offset",
             "(Tensor), "
78 79
             "a vector<int> to describe the offset of every input sequence for "
             "sub sequence item.");
80 81
    AddInput("Length",
             "(Tensor), "
82 83
             "a vector<int> to describe the length of every input sequence for "
             "sub sequence item.");
84
    AddOutput("Out",
85
              "(LoDTensor), The output of SequenceSliceOp.");
86
    AddComment(R"DOC(
87
Sequence slice operator
88

89
The operator crop a subsequence from given sequence with given start offset and subsequence length.
90 91
It only supports sequence (LoD Tensor with level number is 1).
- Case:
92 93 94 95 96
    X = [[a1, a2;
        b1, b2;
        c1, c2]
       [d1, d2;
        e1, e2]]
97 98
    LoD(X) = {{0, 3, 5}}; Dims(X) = (5, 2)
    Offset = [0, 1]; Length = [2, 1]
99 100 101 102

    Out = [[a1, a2;
            b1, b2]
            [e1, e2]]
103
    LoD(Out) = {{0, 2, 3}}; Dims(Out) = (3, 2)
104
NOTE: The length of the input, offset and length should be the same. The offset start from 0.
105 106 107 108 109 110 111 112
    )DOC");
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
113 114
REGISTER_OP(sequence_slice, ops::SequenceSliceOp, ops::SequenceSliceOpMaker,
            sequence_slice_grad, ops::SequenceSliceGradOp);
115
REGISTER_OP_CPU_KERNEL(
116 117
    sequence_slice,
    ops::SequenceSliceOpKernel<paddle::platform::CPUPlace, float>);
118
REGISTER_OP_CPU_KERNEL(
119 120
    sequence_slice_grad,
    ops::SequenceSliceGradOpKernel<paddle::platform::CPUPlace, float>);