expand_op.cc 8.8 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.");
Y
yangyaming 已提交
34
    auto x_dims = ctx->GetInputDim("X");
L
liym27 已提交
35
    auto expand_times = ctx->Attrs().Get<std::vector<int>>("expand_times");
36

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

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

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

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

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
68 69 70
    return framework::OpKernelType(
        OperatorWithKernel::IndicateVarDataType(ctx, "X"),
        ctx.device_context());
71 72 73 74 75
  }

  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
      if (expand_times[i] == -1) {
        continue;
      } else {
L
liym27 已提交
165 166 167 168 169 170 171
        if (ctx->IsRuntime()) {
          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.");
        }
L
liym27 已提交
172
      }
Y
yangyaming 已提交
173
    }
Y
yangyaming 已提交
174 175 176 177 178
    auto x_grad_name = framework::GradVarName("X");

    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
Y
yangyaming 已提交
179
  }
180 181 182 183

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
184 185 186
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
187 188 189 190 191 192 193 194 195 196 197
  }

  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 已提交
198 199
};

H
hong 已提交
200 201
template <typename T>
class ExpandGradOpMaker : public framework::SingleGradOpMaker<T> {
S
sneaxiy 已提交
202
 public:
H
hong 已提交
203
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;
S
sneaxiy 已提交
204 205

 protected:
206
  void Apply(GradOpPtr<T> op) const override {
S
sneaxiy 已提交
207
    op->SetType("expand_grad");
H
hong 已提交
208 209 210 211 212 213
    op->SetInput("X", this->Input("X"));
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetInput("expand_times_tensor", this->Input("expand_times_tensor"));
    op->SetInput("ExpandTimes", this->Input("ExpandTimes"));
    op->SetAttrMap(this->Attrs());
S
sneaxiy 已提交
214 215 216
  }
};

217 218
DECLARE_NO_NEED_BUFFER_VARS_INFERENCE(ExpandGradNoNeedBufVarsInferer, "X");

Y
yangyaming 已提交
219 220 221 222
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
Y
Yang Yang 已提交
223
REGISTER_OPERATOR(expand, ops::ExpandOp, ops::ExpandOpMaker,
H
hong 已提交
224 225
                  ops::ExpandGradOpMaker<paddle::framework::OpDesc>,
                  ops::ExpandGradOpMaker<paddle::imperative::OpBase>);
226 227
REGISTER_OPERATOR(expand_grad, ops::ExpandGradOp,
                  ops::ExpandGradNoNeedBufVarsInferer);
Y
yangyaming 已提交
228
REGISTER_OP_CPU_KERNEL(
229 230 231
    expand, ops::ExpandKernel<paddle::platform::CPUDeviceContext, float>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, double>,
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, int>,
W
wangchaochaohu 已提交
232
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, int64_t>,
233
    ops::ExpandKernel<paddle::platform::CPUDeviceContext, bool>);
Q
QI JUN 已提交
234 235
REGISTER_OP_CPU_KERNEL(
    expand_grad,
236
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, float>,
W
wangchaochaohu 已提交
237 238 239
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, double>,
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, int>,
    ops::ExpandGradKernel<paddle::platform::CPUDeviceContext, int64_t>);