sequence_expand_op.cc 4.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
W
wanghaoshuang 已提交
2

L
Luo Tao 已提交
3 4 5
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
W
wanghaoshuang 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
W
wanghaoshuang 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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
wanghaoshuang 已提交
14

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/sequence_expand_op.h"
W
wanghaoshuang 已提交
16 17 18 19 20 21

namespace paddle {
namespace operators {

using framework::Tensor;

W
wanghaoshuang 已提交
22
class SequenceExpandOp : public framework::OperatorWithKernel {
W
wanghaoshuang 已提交
23 24 25 26 27
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
28 29
    PADDLE_ENFORCE(ctx->HasInput("X"));
    PADDLE_ENFORCE(ctx->HasOutput("Out"));
W
wanghaoshuang 已提交
30
    PADDLE_ENFORCE(ctx->HasInput("Y"));
W
wanghaoshuang 已提交
31
    framework::DDim out_dim;
32 33 34
    auto y_dim = ctx->GetInputDim("Y");
    out_dim = ctx->GetInputDim("X");
    out_dim[0] = y_dim[0];
W
wanghaoshuang 已提交
35
    ctx->ShareLoD("Y", "Out");
W
wanghaoshuang 已提交
36 37 38 39
    ctx->SetOutputDim("Out", out_dim);
  }
};

W
wanghaoshuang 已提交
40
class SequenceExpandOpMaker : public framework::OpProtoAndCheckerMaker {
W
wanghaoshuang 已提交
41
 public:
42
  SequenceExpandOpMaker(OpProto* proto, OpAttrChecker* op_checker)
W
wanghaoshuang 已提交
43
      : OpProtoAndCheckerMaker(proto, op_checker) {
W
wanghaoshuang 已提交
44
    AddInput("X",
W
wanghaoshuang 已提交
45
             "(Tensor or LoDTensor) The input(X) of this operator can be a "
W
wanghaoshuang 已提交
46 47
             "LoDTensor or a base Tensor.");
    AddInput("Y",
W
wanghaoshuang 已提交
48
             "(LoDTensor)The reference input(Y) of sequence_expand op."
W
wanghaoshuang 已提交
49
             "It must be a LoDTensor with k-level(k>0)."
W
wanghaoshuang 已提交
50 51 52
             "The input(X) will be expanded according to LOD of input(Y)."
             "The element numbers of last level in input(Y) "
             "must be equal to dims[0] of input(X).");
W
wanghaoshuang 已提交
53
    AddOutput("Out",
W
wanghaoshuang 已提交
54
              "(LodTensor)The output of sequence_expand op."
W
wanghaoshuang 已提交
55
              "The lod of output will be as same as input(Y)'s lod.");
W
wanghaoshuang 已提交
56
    AddComment(R"DOC(
W
wanghaoshuang 已提交
57
Sequence Expand Operator.
W
wanghaoshuang 已提交
58

59 60
This operator expands input(X) according to LOD of input(Y).
Following are cases to better explain how this works:
W
wanghaoshuang 已提交
61
Case 1:
W
wanghaoshuang 已提交
62

W
wanghaoshuang 已提交
63
Given a 2-level LoDTensor input(X)
W
wanghaoshuang 已提交
64 65 66 67 68 69 70
    X.lod = [[0,       2, 3],
             [0, 1,    3, 4]]
    X.data = [a, b, c, d]
    X.dims = [4, 1]
and input(Y)
    Y.lod = [[0,    2,    4],
             [0, 3, 6, 7, 8]]
71
with condition len(Y.lod[-1]) -1 == X.dims[0]
W
wanghaoshuang 已提交
72 73 74 75 76
then we get 2-level LoDTensor
    Out.lod = [[0,                2,    4],
               [0,       3,       6, 7, 8]]
    Out.data = [a, a, a, b, b, b, c, d]
    Out.dims = [8, 1]
W
wanghaoshuang 已提交
77 78 79

Case 2:

W
wanghaoshuang 已提交
80
Given a common Tensor input(X)
W
wanghaoshuang 已提交
81 82 83 84
    X.data = [a, b, c]
    X.dims = [3, 1]
and input(Y)
    Y.lod = [[0, 2, 3, 6]]
85
with condition len(Y.lod[-1]) -1 == X.dims[0]
W
wanghaoshuang 已提交
86 87 88 89
then we get 1-level LoDTensor
    Out.lod = [[0,    2, 3,      6]]
    Out.data = [a, a, b, c, c, c]
    Out.dims = [6, 1]
W
wanghaoshuang 已提交
90 91 92

Case 3:

W
wanghaoshuang 已提交
93
Given a common Tensor input(X)
W
wanghaoshuang 已提交
94 95 96 97
    X.data = [[a, b], [c, d], [e, f]]
    X.dims = [3, 2]
and input(Y)
    Y.lod = [[0, 2, 3, 6]]
98
with condition len(Y.lod[-1]) -1 == X.dims[0]
W
wanghaoshuang 已提交
99
then we get 1-level LoDTensor
W
wanghaoshuang 已提交
100 101 102 103
    Out.lod = [[0,           2,     3,                     6]]
    Out.data = [[a,b], [a,b] [c,d], [e, f], [e, f], [e, f]]
    Out.dims = [6, 2]

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
Case 4:

Given 2-level a LoDTensor input(X)
    X.lod = [[0,       2, 3],
             [0, 1,    3, 4]]
    X.data = [a, b, c, d]
    X.dims = [4, 1]
and input(Y)
    Y.lod = [[0,    2,    4],
             [0, 3, 6, 6, 8]]
with condition len(Y.lod[-1]) -1 == X.dims[0]
then we get 2-level LoDTensor
    Out.lod = [[0,                2,    4],
               [0,       3,       6, 6, 8]]
    Out.data = [a, a, a, b, b, b, d, d]
    Out.dims = [8, 1]

W
wanghaoshuang 已提交
121 122 123 124 125

)DOC");
  }
};

W
wanghaoshuang 已提交
126
class SequenceExpandOpGrad : public framework::OperatorWithKernel {
W
wanghaoshuang 已提交
127 128 129 130 131
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
132 133
    PADDLE_ENFORCE(ctx->HasInput("X"));
    PADDLE_ENFORCE(ctx->HasInput("Out"));
W
wanghaoshuang 已提交
134
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
W
wanghaoshuang 已提交
135
                   "The input(Out@GRAD) should not be null");
W
wanghaoshuang 已提交
136 137 138 139 140 141 142 143 144 145 146 147
    auto x_dims = ctx->GetInputDim("X");
    auto x_grad_name = framework::GradVarName("X");
    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
W
wanghaoshuang 已提交
148 149
REGISTER_OP(sequence_expand, ops::SequenceExpandOp, ops::SequenceExpandOpMaker,
            sequence_expand_grad, ops::SequenceExpandOpGrad);
Q
QI JUN 已提交
150
REGISTER_OP_CPU_KERNEL(
W
wanghaoshuang 已提交
151 152
    sequence_expand,
    ops::SequenceExpandKernel<paddle::platform::CPUDeviceContext, float>);
W
wanghaoshuang 已提交
153
REGISTER_OP_CPU_KERNEL(
W
wanghaoshuang 已提交
154 155
    sequence_expand_grad,
    ops::SequenceExpandGradKernel<paddle::platform::CPUDeviceContext, float>);