expand_op.cc 8.6 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 <string>
18
#include <vector>
Y
yangyaming 已提交
19 20 21 22 23 24 25 26 27 28 29

namespace paddle {
namespace operators {

using framework::Tensor;

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

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

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

L
liym27 已提交
38 39
    if (expand_times.size() == 0) {
      expand_times = std::vector<int>(x_dims.size(), -1);
40
    }
Y
yangyaming 已提交
41 42

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

    std::vector<int64_t> out_shape(x_dims.size());
    for (size_t i = 0; i < expand_times.size(); ++i) {
50 51 52
      if (x_dims[i] == -1 || expand_times[i] == -1) {
        out_shape[i] = -1;
      } else {
L
liym27 已提交
53 54 55
        PADDLE_ENFORCE_GT(
            expand_times[i], 0,
            "The element of Attr(expand_times) must greater than 0.");
56 57
        out_shape[i] = x_dims[i] * expand_times[i];
      }
M
minqiyang 已提交
58 59
    }

Y
yangyaming 已提交
60
    ctx->SetOutputDim("Out", framework::make_ddim(out_shape));
61 62 63
    if (out_shape[0] == x_dims[0]) {
      ctx->ShareLoD("X", "Out");
    }
Y
yangyaming 已提交
64
  }
65 66 67 68 69 70 71 72 73 74 75

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(ctx.Input<Tensor>("X")->type(),
                                   ctx.device_context());
  }

  framework::OpKernelType GetKernelTypeForVar(
      const std::string& var_name, const Tensor& tensor,
      const framework::OpKernelType& expected_kernel_type) const override {
L
liym27 已提交
76
    if (var_name == "expand_times_tensor" || var_name == "ExpandTimes") {
77 78 79 80 81
      return expected_kernel_type;
    }
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(), tensor.layout());
  }
Y
yangyaming 已提交
82 83 84 85
};

class ExpandOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
86
  void Make() override {
Y
yangyaming 已提交
87
    AddInput("X",
C
caoying03 已提交
88 89
             "(Tensor, default Tensor<float>). A tensor with rank in [1, 6]."
             "X is the input to be expanded.");
L
liym27 已提交
90 91 92 93 94 95 96 97 98
    AddInput("ExpandTimes",
             "(Tensor<int>), optional). If provided, expand according to "
             "this given expand times. It has a higher priority than "
             "expand_times_tensor and expand_times.")
        .AsDispensable();
    AddInput("expand_times_tensor",
             "(Tensor Tensor<int>), epxand times for X."
             "It has a higher priority than expand_times, but a lower priority "
             "than ExpandTimes")
99 100
        .AsDuplicable()
        .AsDispensable();
Y
yangyaming 已提交
101
    AddOutput("Out",
C
caoying03 已提交
102 103 104 105 106
              "(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).");
107
    AddAttr<std::vector<int>>("expand_times",
108 109
                              "Expand times number for each dimension.")
        .SetDefault({});
Y
yangyaming 已提交
110
    AddComment(R"DOC(
Y
yangyaming 已提交
111
Expand operator tiles the input by given times number. You should set times
112
number for each dimension by providing attribute 'expand_times'. The rank of X
C
caoying03 已提交
113 114
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 已提交
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131

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 已提交
132 133 134 135 136 137 138 139 140
)DOC");
  }
};

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

 protected:
Y
yangyaming 已提交
141
  void InferShape(framework::InferShapeContext* ctx) const override {
L
liym27 已提交
142 143 144
    PADDLE_ENFORCE_EQ(ctx->HasInput("X"), true, "Input(X) should not be null.");
    PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Out")), true,
                      "Input(Out@GRAD) should not be null.");
145

Y
yangyaming 已提交
146 147
    auto x_dims = ctx->GetInputDim("X");
    std::vector<int> expand_times =
148
        ctx->Attrs().Get<std::vector<int>>("expand_times");
149

Y
yangyaming 已提交
150
    auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
Y
yangyaming 已提交
151

M
minqiyang 已提交
152
    size_t start_pos = 0u;
M
minqiyang 已提交
153
    if (!ctx->IsRuntime() && x_dims[0] < 0) {
M
minqiyang 已提交
154
      PADDLE_ENFORCE_EQ(
M
minqiyang 已提交
155
          x_dims[0], out_dims[0],
M
minqiyang 已提交
156 157
          "The first dimension size of Input(Out@GRAD) should be "
          "equal to the crroresponding dimension size of Input(X)");
M
minqiyang 已提交
158 159 160 161
      start_pos = 1u;
    }

    for (size_t i = start_pos; i < expand_times.size(); ++i) {
L
liym27 已提交
162 163 164 165 166 167 168 169
      if (expand_times[i] == -1) {
        continue;
      } else {
        PADDLE_ENFORCE_EQ(x_dims[i] * expand_times[i], out_dims[i],
                          "Each dimension size of Input(Out@GRAD) should be "
                          "equal to multiplication of crroresponding dimension "
                          "size of Input(X) and Attr(expand_times) value.");
      }
Y
yangyaming 已提交
170
    }
Y
yangyaming 已提交
171 172 173 174 175
    auto x_grad_name = framework::GradVarName("X");

    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
Y
yangyaming 已提交
176
  }
177 178 179 180

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
181 182 183
    return framework::OpKernelType(
        ctx.Input<Tensor>(framework::GradVarName("Out"))->type(),
        ctx.device_context());
184 185 186 187 188 189 190 191 192 193 194
  }

  framework::OpKernelType GetKernelTypeForVar(
      const std::string& var_name, const Tensor& tensor,
      const framework::OpKernelType& expected_kernel_type) const override {
    if (var_name == "expand_times_tensor") {
      return expected_kernel_type;
    }
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(), tensor.layout());
  }
Y
yangyaming 已提交
195 196
};

S
sneaxiy 已提交
197 198 199 200 201 202 203 204 205 206 207
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"));
208
    op->SetInput("expand_times_tensor", Input("expand_times_tensor"));
L
liym27 已提交
209
    op->SetInput("ExpandTimes", Input("ExpandTimes"));
S
sneaxiy 已提交
210 211 212 213 214
    op->SetAttrMap(Attrs());
    return op;
  }
};

215 216
DECLARE_NO_NEED_BUFFER_VARS_INFERENCE(ExpandGradNoNeedBufVarsInferer, "X");

Y
yangyaming 已提交
217 218 219 220
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
221
REGISTER_OPERATOR(expand, ops::ExpandOp, ops::ExpandOpMaker,
S
sneaxiy 已提交
222
                  ops::ExpandGradOpDescMaker);
223 224
REGISTER_OPERATOR(expand_grad, ops::ExpandGradOp,
                  ops::ExpandGradNoNeedBufVarsInferer);
Y
yangyaming 已提交
225
REGISTER_OP_CPU_KERNEL(
226 227 228
    expand, ops::ExpandKernel<paddle::platform::CPUDeviceContext, float>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, double>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, int>,
W
wangchaochaohu 已提交
229
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, int64_t>,
230
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, bool>);
Q
QI JUN 已提交
231 232
REGISTER_OP_CPU_KERNEL(
    expand_grad,
233
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, float>,
W
wangchaochaohu 已提交
234 235 236
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, double>,
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, int>,
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, int64_t>);