expand_op.cc 6.1 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
yangyaming 已提交
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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/operators/expand_op.h"
S
sneaxiy 已提交
16
#include <memory>
17
#include <vector>
Y
yangyaming 已提交
18 19 20 21 22 23 24 25 26 27 28

namespace paddle {
namespace operators {

using framework::Tensor;

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

 protected:
Y
yangyaming 已提交
29
  void InferShape(framework::InferShapeContext* ctx) const override {
30 31 32
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
    PADDLE_ENFORCE(ctx->HasOutput("Out"), "Output(Out) should not be null.");

Y
yangyaming 已提交
33
    std::vector<int> expand_times =
34
        ctx->Attrs().Get<std::vector<int>>("expand_times");
Y
yangyaming 已提交
35 36 37
    auto x_dims = ctx->GetInputDim("X");

    PADDLE_ENFORCE_EQ(static_cast<size_t>(x_dims.size()), expand_times.size(),
38
                      "The number of Attr(expand_times)'s value must be equal "
Y
yangyaming 已提交
39
                      "to the rank of Input(X).");
Y
yangyaming 已提交
40
    PADDLE_ENFORCE_LE(x_dims.size(), 6,
Y
yangyaming 已提交
41
                      "The rank of Input(X) must not be greater than 6.");
Y
yangyaming 已提交
42 43 44 45

    std::vector<int64_t> out_shape(x_dims.size());
    for (size_t i = 0; i < expand_times.size(); ++i) {
      PADDLE_ENFORCE_GE(expand_times[i], 1,
46
                        "Each value of Attr(expand_times) should not be "
Y
yangyaming 已提交
47 48 49
                        "less than 1.");
      out_shape[i] = x_dims[i] * expand_times[i];
    }
Y
yangyaming 已提交
50

M
minqiyang 已提交
51
    // set the first dim to -1 in compile time
M
minqiyang 已提交
52
    if (!ctx->IsRuntime() && x_dims[0] < 0) {
M
minqiyang 已提交
53 54 55
      out_shape[0] = x_dims[0];
    }

Y
yangyaming 已提交
56
    ctx->SetOutputDim("Out", framework::make_ddim(out_shape));
57 58 59
    if (out_shape[0] == x_dims[0]) {
      ctx->ShareLoD("X", "Out");
    }
Y
yangyaming 已提交
60 61 62 63 64
  }
};

class ExpandOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
65
  void Make() override {
Y
yangyaming 已提交
66
    AddInput("X",
C
caoying03 已提交
67 68
             "(Tensor, default Tensor<float>). A tensor with rank in [1, 6]."
             "X is the input to be expanded.");
Y
yangyaming 已提交
69
    AddOutput("Out",
C
caoying03 已提交
70 71 72 73 74
              "(Tensor, default Tensor<float>). A tensor with rank in [1, 6]."
              "The rank of Output(Out) have the same with Input(X). "
              "After expanding, size of each dimension of Output(Out) is equal "
              "to size of the corresponding dimension of Input(X) multiplying "
              "the corresponding value given by Attr(expand_times).");
75
    AddAttr<std::vector<int>>("expand_times",
Y
yangyaming 已提交
76
                              "Expand times number for each dimension.");
Y
yangyaming 已提交
77
    AddComment(R"DOC(
Y
yangyaming 已提交
78
Expand operator tiles the input by given times number. You should set times
79
number for each dimension by providing attribute 'expand_times'. The rank of X
C
caoying03 已提交
80 81
should be in [1, 6]. Please note that size of 'expand_times' must be the same
with X's rank. Following is a using case:
Y
yangyaming 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98

Input(X) is a 3-D tensor with shape [2, 3, 1]:

        [
           [[1], [2], [3]],
           [[4], [5], [6]]
        ]

Attr(expand_times):  [1, 2, 2]

Output(Out) is a 3-D tensor with shape [2, 6, 2]:

        [
            [[1, 1], [2, 2], [3, 3], [1, 1], [2, 2], [3, 3]],
            [[4, 4], [5, 5], [6, 6], [4, 4], [5, 5], [6, 6]]
        ]

Y
yangyaming 已提交
99 100 101 102 103 104 105 106 107
)DOC");
  }
};

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

 protected:
Y
yangyaming 已提交
108 109 110 111
  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE(ctx->HasInput("X"), "Input(X) should not be null.");
    PADDLE_ENFORCE(ctx->HasInput(framework::GradVarName("Out")),
                   "Input(Out@GRAD) should not be null.");
112

Y
yangyaming 已提交
113 114
    auto x_dims = ctx->GetInputDim("X");
    std::vector<int> expand_times =
115
        ctx->Attrs().Get<std::vector<int>>("expand_times");
Y
yangyaming 已提交
116
    auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
Y
yangyaming 已提交
117

M
minqiyang 已提交
118
    size_t start_pos = 0u;
M
minqiyang 已提交
119
    if (!ctx->IsRuntime() && x_dims[0] < 0) {
M
minqiyang 已提交
120
      PADDLE_ENFORCE_EQ(
M
minqiyang 已提交
121
          x_dims[0], out_dims[0],
M
minqiyang 已提交
122 123
          "The first dimension size of Input(Out@GRAD) should be "
          "equal to the crroresponding dimension size of Input(X)");
M
minqiyang 已提交
124 125 126 127
      start_pos = 1u;
    }

    for (size_t i = start_pos; i < expand_times.size(); ++i) {
Y
yangyaming 已提交
128
      PADDLE_ENFORCE_EQ(x_dims[i] * expand_times[i], out_dims[i],
Y
yangyaming 已提交
129 130
                        "Each dimension size of Input(Out@GRAD) should be "
                        "equal to multiplication of crroresponding dimension "
131
                        "size of Input(X) and Attr(expand_times) value.");
Y
yangyaming 已提交
132 133
    }

Y
yangyaming 已提交
134 135 136 137 138
    auto x_grad_name = framework::GradVarName("X");

    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
Y
yangyaming 已提交
139 140 141
  }
};

S
sneaxiy 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
class ExpandGradOpDescMaker : 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("expand_grad");
    op->SetInput("X", Input("X"));
    op->SetInput(framework::GradVarName("Out"), OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), InputGrad("X"));
    op->SetAttrMap(Attrs());
    return op;
  }
};

Y
yangyaming 已提交
158 159 160 161
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
162
REGISTER_OPERATOR(expand, ops::ExpandOp, ops::ExpandOpMaker,
S
sneaxiy 已提交
163
                  ops::ExpandGradOpDescMaker);
164
REGISTER_OPERATOR(expand_grad, ops::ExpandGradOp);
Y
yangyaming 已提交
165
REGISTER_OP_CPU_KERNEL(
S
sneaxiy 已提交
166 167 168 169
    expand, ops::ExpandKernel<paddle::platform::CPUDeviceContext, float>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, double>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, int>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, bool>);
Q
QI JUN 已提交
170 171
REGISTER_OP_CPU_KERNEL(
    expand_grad,
S
sneaxiy 已提交
172 173
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, float>,
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, double>);