sequence_slice_op.cc 5.6 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

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_slice_op.h"
16
#include <memory>
17 18 19 20

namespace paddle {
namespace operators {

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

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"),
27 28 29 30 31
                   "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.");
32
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
33
                   "Output(Out) of SequenceSliceOp should not be null.");
34 35
    auto input_dims = ctx->GetInputDim("X");

36 37 38
    auto offset_dim = ctx->GetInputDim("Offset");
    auto length_dim = ctx->GetInputDim("Length");

W
wanghaox 已提交
39 40 41 42 43 44
    PADDLE_ENFORCE_EQ(
        offset_dim.size(), 2UL,
        "Only support one level sequence now, The rank of offset must be 2.");
    PADDLE_ENFORCE_EQ(
        length_dim.size(), 2UL,
        "Only support one level sequence now, The rank of Length must be 2.");
45

W
wanghaox 已提交
46 47
    // Initialize the output's dims to maximum,
    // and re-set to real dims by the value of Offset and Length at kernel
48
    ctx->SetOutputDim("Out", input_dims);
49
  }
50

51
 protected:
52
  framework::OpKernelType GetExpectedKernelType(
53
      const framework::ExecutionContext& ctx) const override {
54 55 56
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
57 58 59
  }
};

60
class SequenceSliceGradOp : public framework::OperatorWithKernel {
61 62 63 64 65 66 67 68 69 70
 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"));
  }
71 72

 protected:
73
  framework::OpKernelType GetExpectedKernelType(
74
      const framework::ExecutionContext& ctx) const override {
75 76 77
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
78
  }
79 80
};

81
class SequenceSliceOpMaker : public framework::OpProtoAndCheckerMaker {
82
 public:
Y
Yu Yang 已提交
83
  void Make() override {
84 85 86 87 88
    AddInput("X",
             "(LoDTensor), "
             "the input of SequenceSliceOp.");
    AddInput("Offset",
             "(Tensor), "
89 90
             "a vector<int> to describe the offset of every input sequence for "
             "sub sequence item.");
91 92
    AddInput("Length",
             "(Tensor), "
93 94
             "a vector<int> to describe the length of every input sequence for "
             "sub sequence item.");
95
    AddOutput("Out", "(LoDTensor), the output of SequenceSliceOp.");
96
    AddComment(R"DOC(
97
Sequence slice operator
98

W
wanghaox 已提交
99
The operator crops a subsequence from given sequence with given start offset and subsequence length.
100 101
It only supports sequence (LoD Tensor with level number is 1).
- Case:
102 103 104 105 106
    X = [[a1, a2;
        b1, b2;
        c1, c2]
       [d1, d2;
        e1, e2]]
107
    LoD(X) = {{0, 3, 5}}; Dims(X) = (5, 2)
108
    Offset = [[0], [1]]; Length = [[2], [1]]
109 110 111 112

    Out = [[a1, a2;
            b1, b2]
            [e1, e2]]
113
    LoD(Out) = {{0, 2, 3}}; Dims(Out) = (3, 2)
W
wanghaox 已提交
114
NOTE: The first dimension size of input, the size of offset and Length, should be equal. The offset start from 0.
115 116 117 118
    )DOC");
  }
};

119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
class SequenceSliceGradOpDescMaker : public framework::SingleGradOpDescMaker {
 public:
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

 protected:
  std::unique_ptr<framework::OpDesc> Apply() const override {
    std::unique_ptr<framework::OpDesc> op(new framework::OpDesc());
    op->SetType("sequence_slice_grad");
    op->SetInput("X", Input("X"));
    op->SetInput("Offset", Input("Offset"));
    op->SetInput("Length", Input("Length"));
    op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
    op->SetAttrMap(Attrs());
    return op;
  }
};

DECLARE_NO_NEED_BUFFER_VARS_INFERENCE(
    SequenceSliceGradNoNeedBufferVarsInference, "X");

140 141 142 143
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
144
REGISTER_OPERATOR(sequence_slice, ops::SequenceSliceOp,
145 146 147
                  ops::SequenceSliceOpMaker, ops::SequenceSliceGradOpDescMaker);
REGISTER_OPERATOR(sequence_slice_grad, ops::SequenceSliceGradOp,
                  ops::SequenceSliceGradNoNeedBufferVarsInference);
148
REGISTER_OP_CPU_KERNEL(
149
    sequence_slice,
Q
QI JUN 已提交
150
    ops::SequenceSliceOpKernel<paddle::platform::CPUDeviceContext, float>);
151
REGISTER_OP_CPU_KERNEL(
152
    sequence_slice_grad,
Q
QI JUN 已提交
153
    ops::SequenceSliceGradOpKernel<paddle::platform::CPUDeviceContext, float>);