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 28 29 30
/* 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 {
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of SeqExpandOp should not be null.");
    int repeat = ctx->Attrs().Get<int>("repeat");
W
wanghaoshuang 已提交
31
    framework::DDim out_dim;
W
wanghaoshuang 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    if (repeat == 0) {
      PADDLE_ENFORCE(
          ctx->HasInput("Y"),
          "Input(Y) of SeqExpandOp should not be null while repeat == 0.");
      out_dim = ctx->GetInputDim("Y");
      ctx->ShareLoD("Y", "Out");
    } else {
      out_dim = ctx->GetInputDim("X");
      out_dim[0] = out_dim[0] * repeat;
    }
    PADDLE_ENFORCE(ctx->HasOutput("Out"),
                   "Output(Out) of PadOp should not be null.");
    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 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
    AddInput(
        "X",
        "The input('X') of seq_expand op. It can be LoDTensor or base Tensor.");
    AddInput(
        "Y",
        "The reference input('Y') of seq_expand op."
        "It must be a LoDTensor with k-level(k>0)."
        "This reference input is essential if 'repeat' attribute is not "
        "configured."
        "Input(X) will be expanded by LoD of input(Y) while repeat ==  0.");
    AddOutput("Out",
              "The output of seq_expand op."
              "The output is a (k+1)-level LoDTensor"
              "while input(X) being k-level LoDTensor."
              "(Given base tensor is 0-level LoDTensor.)");
    AddAttr<int>("repeat",
                 "(type:int; default value: 0)"
                 "Repeatting times of each element while expanding input(X)."
                 "It works while input(Y) is not configured.")
        .SetDefault(0);
W
wanghaoshuang 已提交
73
    AddComment(R"DOC(
W
wanghaoshuang 已提交
74 75
Expand k-level LoDTensor to (k+1)-level LoDTensor
by lod of input(Y) or 'repeat' attribute.
W
wanghaoshuang 已提交
76

W
wanghaoshuang 已提交
77
Case 1:
W
wanghaoshuang 已提交
78

W
wanghaoshuang 已提交
79 80 81
Given a 2-level LoDTensor X:
    X.data = [1, 2 , 3, 4]
    X.lod = [[0, 3, 4], [0, 1, 3, 4]]
W
wanghaoshuang 已提交
82
and
W
wanghaoshuang 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    repeat = 2
then we get 3-level LoDTensor
    Out.data = [1, 2, 3, 1, 2, 3, 4, 4]
    Out.lod = [[0, 6, 8],
               [0, 3, 6, 7, 8],
               [0, 1, 3, 4, 6, 7, 8]]

Case 2:

Given 2-level a LoDTensor X
    X.data = [1, 2, 3, 4]
    X.lod = [[0, 3, 4], [0, 1, 3, 4]]
and
    Y.lod = [[0, 6, 8],
             [0, 3, 6, 7, 8],
             [0,1,3,4,6,7,8]]
then we get 3-level LoDTensor
    Out.data = [1, 2, 3, 1, 2, 3, 4, 4]
    Out.lod = [[0, 6, 8],
               [0, 3, 6, 7, 8],
               [0, 1, 3, 4, 6, 7, 8]]

Case 3:

Given a 0-level LoDTensor X
    X.data = [1, 2, 3, 4]
    X.lod = NULL
and
    repeat = 2
then we get 1-level LoDTensor
    Out.data = [1, 1, 2, 2, 3, 3, 4, 4]
    Out.lod = [[0, 2, 4, 6, 8]]
W
wanghaoshuang 已提交
115 116 117 118 119 120 121 122 123 124 125 126

)DOC");
  }
};

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

 protected:
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null");
W
wanghaoshuang 已提交
127
    PADDLE_ENFORCE(ctx->HasInput("Out"), "Input(Out) should not be null");
W
wanghaoshuang 已提交
128 129 130 131 132 133 134 135 136 137 138 139 140 141
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
                   "Input(Out@GRAD) should not be null");
    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 已提交
142 143
REGISTER_OP(seq_expand, ops::SeqExpandOp, ops::SeqExpandOpMaker,
            seq_expand_grad, ops::SeqExpandOpGrad);
W
wanghaoshuang 已提交
144 145 146 147 148
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>);