seq_expand_op.cc 4.7 KB
Newer Older
W
wanghaoshuang 已提交
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
/* 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. */

#include "paddle/operators/seq_expand_op.h"

namespace paddle {
namespace operators {

using framework::Tensor;

class SeqExpandOp : public framework::OperatorWithKernel {
 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 32 33
    framework::DDim out_dim;
    out_dim = ctx->GetInputDim("Y");
    ctx->ShareLoD("Y", "Out");
W
wanghaoshuang 已提交
34 35 36 37 38 39 40 41 42
    ctx->SetOutputDim("Out", out_dim);
  }
};

class SeqExpandOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  SeqExpandOpMaker(framework::OpProto* proto,
                   framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
W
wanghaoshuang 已提交
43
    AddInput("X",
W
wanghaoshuang 已提交
44
             "(Tensor or LoDTensor) The input(X) of this operator can be a "
W
wanghaoshuang 已提交
45 46
             "LoDTensor or a base Tensor.");
    AddInput("Y",
W
wanghaoshuang 已提交
47
             "(LoDTensor)The reference input(Y) of seq_expand op."
W
wanghaoshuang 已提交
48
             "It must be a LoDTensor with k-level(k>0)."
W
wanghaoshuang 已提交
49 50 51
             "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 已提交
52
    AddOutput("Out",
53
              "(LodTensor)The output of seq_expand op."
W
wanghaoshuang 已提交
54
              "The lod of output will be as same as input(Y)'s lod.");
W
wanghaoshuang 已提交
55
    AddComment(R"DOC(
W
wanghaoshuang 已提交
56
Expand input(X) according to LOD of input(Y).
W
wanghaoshuang 已提交
57

W
wanghaoshuang 已提交
58
Case 1:
W
wanghaoshuang 已提交
59

W
wanghaoshuang 已提交
60 61 62 63 64 65 66 67
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, 7, 8]]
68
with condition len(Y.lod[-1]) -1 == X.dims[0]
W
wanghaoshuang 已提交
69 70 71 72 73
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 已提交
74 75 76

Case 2:

W
wanghaoshuang 已提交
77 78 79 80 81 82
Given a 0-level LoDTensor input(X)
    X.data = [a, b, c]
    X.lod = NULL
    X.dims = [3, 1]
and input(Y)
    Y.lod = [[0, 2, 3, 6]]
83
with condition len(Y.lod[-1]) -1 == X.dims[0]
W
wanghaoshuang 已提交
84 85 86 87
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 已提交
88 89 90

Case 3:

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

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
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 已提交
120 121 122 123 124 125 126 127 128 129 130

)DOC");
  }
};

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
131 132
    PADDLE_ENFORCE(ctx->HasInput("X"));
    PADDLE_ENFORCE(ctx->HasInput("Out"));
W
wanghaoshuang 已提交
133
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
W
wanghaoshuang 已提交
134
                   "The input(Out@GRAD) should not be null");
W
wanghaoshuang 已提交
135 136 137 138 139 140 141 142 143 144 145 146
    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 已提交
147 148
REGISTER_OP(seq_expand, ops::SeqExpandOp, ops::SeqExpandOpMaker,
            seq_expand_grad, ops::SeqExpandOpGrad);
W
wanghaoshuang 已提交
149 150 151 152 153
REGISTER_OP_CPU_KERNEL(seq_expand,
                       ops::SeqExpandKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
    seq_expand_grad,
    ops::SeqExpandGradKernel<paddle::platform::CPUPlace, float>);