expand_op.cc 4.5 KB
Newer Older
Y
yangyaming 已提交
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
/* 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/expand_op.h"

namespace paddle {
namespace operators {

using framework::Tensor;

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

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

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

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

    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,
44
                        "Each value of Attr(expand_times) should not be "
Y
yangyaming 已提交
45 46 47
                        "less than 1.");
      out_shape[i] = x_dims[i] * expand_times[i];
    }
Y
yangyaming 已提交
48 49

    ctx->SetOutputDim("Out", framework::make_ddim(out_shape));
50 51 52
    if (out_shape[0] == x_dims[0]) {
      ctx->ShareLoD("X", "Out");
    }
Y
yangyaming 已提交
53 54 55 56 57 58 59
  }
};

class ExpandOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  ExpandOpMaker(framework::OpProto* proto, framework::OpAttrChecker* op_checker)
      : OpProtoAndCheckerMaker(proto, op_checker) {
Y
yangyaming 已提交
60
    AddInput("X",
Y
yangyaming 已提交
61 62
             "(Tensor, default Tensor<float>) A tensor with rank in [1, 6]."
             "X is the input tensor to be expanded.");
Y
yangyaming 已提交
63
    AddOutput("Out",
Y
yangyaming 已提交
64 65 66 67
              "(Tensor, default Tensor<float>) A tensor with rank in [1, 6]."
              "The rank of Output(Out) is same as Input(X) except that each "
              "dimension size of Output(Out) is equal to corresponding "
              "dimension size of Input(X) multiplying corresponding value of "
68 69
              "Attr(expand_times).");
    AddAttr<std::vector<int>>("expand_times",
Y
yangyaming 已提交
70
                              "Expand times number for each dimension.");
Y
yangyaming 已提交
71
    AddComment(R"DOC(
Y
yangyaming 已提交
72
Expand operator tiles the input by given times number. You should set times
73 74
number for each dimension by providing attribute 'expand_times'. The rank of X
should be in [1, 6]. Please notice that size of 'expand_times' must be same with
Y
yangyaming 已提交
75
X's rank.
Y
yangyaming 已提交
76 77 78 79 80 81 82 83 84
)DOC");
  }
};

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

 protected:
Y
yangyaming 已提交
85 86 87 88
  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.");
89

Y
yangyaming 已提交
90 91
    auto x_dims = ctx->GetInputDim("X");
    std::vector<int> expand_times =
92
        ctx->Attrs().Get<std::vector<int>>("expand_times");
Y
yangyaming 已提交
93
    auto out_dims = ctx->GetInputDim(framework::GradVarName("Out"));
Y
yangyaming 已提交
94 95 96

    for (size_t i = 0; i < expand_times.size(); ++i) {
      PADDLE_ENFORCE_EQ(x_dims[i] * expand_times[i], out_dims[i],
Y
yangyaming 已提交
97 98
                        "Each dimension size of Input(Out@GRAD) should be "
                        "equal to multiplication of crroresponding dimension "
99
                        "size of Input(X) and Attr(expand_times) value.");
Y
yangyaming 已提交
100 101
    }

Y
yangyaming 已提交
102 103 104 105 106
    auto x_grad_name = framework::GradVarName("X");

    if (ctx->HasOutput(x_grad_name)) {
      ctx->SetOutputDim(x_grad_name, x_dims);
    }
Y
yangyaming 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
REGISTER_OP(expand, ops::ExpandOp, ops::ExpandOpMaker, expand_grad,
            ops::ExpandGradOp);
REGISTER_OP_CPU_KERNEL(expand,
                       ops::ExpandKernel<paddle::platform::CPUPlace, float>);
REGISTER_OP_CPU_KERNEL(
    expand_grad, ops::ExpandGradKernel<paddle::platform::CPUPlace, float>);