split_op.cc 4.5 KB
Newer Older
Y
Yancey 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/* 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. */

Y
Yi Wang 已提交
15 16
#include "paddle/fluid/operators/split_op.h"
#include "paddle/fluid/operators/net_op.h"
Y
Yancey 已提交
17 18 19 20 21 22 23 24 25

namespace paddle {
namespace operators {
using framework::Tensor;

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

26
  void InferShape(framework::InferShapeContext *ctx) const override {
27 28 29 30
    PADDLE_ENFORCE(ctx->HasInput("X"),
                   "Input(X) of SplitOp should not be null.");
    PADDLE_ENFORCE_GE(ctx->Outputs("Out").size(), 1UL,
                      "Outputs(Out) of SplitOp should not be empty.");
Q
Qiao Longfei 已提交
31 32 33 34 35 36 37 38 39
    auto in_dims = ctx->GetInputDim("X");
    auto outs_names = ctx->Outputs("Out");
    size_t axis = static_cast<size_t>(ctx->Attrs().Get<int>("axis"));
    size_t num = static_cast<size_t>(ctx->Attrs().Get<int>("num"));
    std::vector<int> sections = static_cast<std::vector<int>>(
        ctx->Attrs().Get<std::vector<int>>("sections"));
    const size_t outs_number = outs_names.size();
    std::vector<framework::DDim> outs_dims;
    outs_dims.reserve(outs_number);
Y
Yancey 已提交
40 41

    if (num > 0) {
Q
Qiao Longfei 已提交
42
      int64_t in_axis_dim = in_dims[axis];
Y
Yancey 已提交
43 44 45 46
      PADDLE_ENFORCE_EQ(in_axis_dim % num, 0,
                        "tensor split does not result"
                        " in an equal division");
      size_t out_axis_dim = in_axis_dim / num;
Q
Qiao Longfei 已提交
47 48
      for (size_t i = 0; i < outs_number; ++i) {
        auto dim = in_dims;
Y
Yancey 已提交
49
        dim[axis] = out_axis_dim;
Q
Qiao Longfei 已提交
50
        outs_dims.push_back(dim);
Y
Yancey 已提交
51 52
      }
    } else if (sections.size() > 0) {
Q
Qiao Longfei 已提交
53
      PADDLE_ENFORCE_EQ(sections.size(), outs_number,
Y
Yancey 已提交
54 55
                        "tensor split sections size"
                        "should be equal to output size.");
Q
Qiao Longfei 已提交
56 57
      for (size_t i = 0; i < outs_number; ++i) {
        auto dim = in_dims;
Y
Yancey 已提交
58
        dim[axis] = sections[i];
Q
Qiao Longfei 已提交
59
        outs_dims.push_back(dim);
Y
Yancey 已提交
60 61
      }
    }
Q
Qiao Longfei 已提交
62
    ctx->SetOutputsDim("Out", outs_dims);
G
guosheng 已提交
63 64 65 66 67 68
    if (axis != 0) {
      // Only pass LoD when not spliting along the first dim.
      for (size_t i = 0; i < outs_number; ++i) {
        ctx->ShareLoD("X", "Out", 0, i);
      }
    }
Y
Yancey 已提交
69 70 71 72 73
  }
};

class SplitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
74
  SplitOpMaker(OpProto *proto, OpAttrChecker *op_checker)
Y
Yancey 已提交
75
      : OpProtoAndCheckerMaker(proto, op_checker) {
76 77 78
    AddInput("X", "(Tensor) Input tensor of the split operator.");
    AddOutput("Out", "(Tensor) Output tensors of the split operator.")
        .AsDuplicable();
Y
Yancey 已提交
79
    AddComment(R"DOC(
80 81 82 83 84 85 86 87 88 89 90 91 92
Split operator

This operator splits the input tensor into multiple sub-tensors.

Example:
  Input = [[1,2],
           [3,4],
           [5,6]]
  sections = [2,1]
  axis = 0
  Output[0] = [[1,2],
               [3,4]]
  Output[1] = [[5,6]]
Y
Yancey 已提交
93 94 95

    )DOC");
    AddAttr<std::vector<int>>("sections",
96 97 98
                              "(vector<int>) "
                              "the length of each output along the "
                              "specified axis.")
Y
Yancey 已提交
99 100
        .SetDefault(std::vector<int>{});
    AddAttr<int>("num",
101 102
                 "(int, default 0)"
                 "Number of sub-tensors. This must evenly divide "
Y
Yancey 已提交
103 104
                 "Input.dims()[axis]")
        .SetDefault(0);
105 106 107
    AddAttr<int>("axis",
                 "(int, default 0) "
                 "The axis which the input will be splited on.")
Y
Yancey 已提交
108 109 110 111
        .SetDefault(0);
  }
};

Y
Yu Yang 已提交
112
class SplitGradMaker : public framework::SingleGradOpDescMaker {
Y
Yancey 已提交
113
 public:
Y
Yu Yang 已提交
114 115 116
  using framework::SingleGradOpDescMaker::SingleGradOpDescMaker;

 protected:
Y
Yu Yang 已提交
117 118
  std::unique_ptr<framework::OpDesc> Apply() const override {
    auto op = new framework::OpDesc();
Y
Yu Yang 已提交
119 120 121 122
    op->SetType("concat");
    op->SetInput("X", OutputGrad("Out"));
    op->SetOutput("Out", InputGrad("X"));
    op->SetAttrMap(Attrs());
Y
Yu Yang 已提交
123
    return std::unique_ptr<framework::OpDesc>(op);
Y
Yancey 已提交
124 125 126 127 128 129 130 131
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
USE_CPU_ONLY_OP(concat);
Y
Yu Yang 已提交
132 133

REGISTER_OPERATOR(split, ops::SplitOp, ops::SplitOpMaker, ops::SplitGradMaker);
Y
Yancey 已提交
134
REGISTER_OP_CPU_KERNEL(split,
135
                       ops::SplitOpKernel<paddle::platform::CPUPlace, float>);