split_op.cc 4.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yancey 已提交
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/split_op.h"
16
#include <string>
Y
Yancey 已提交
17

18 19 20
#include "paddle/fluid/framework/infershape_utils.h"
#include "paddle/phi/infermeta/unary.h"

Y
Yancey 已提交
21 22 23 24 25 26 27 28
namespace paddle {
namespace operators {
using framework::Tensor;

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

29 30 31
 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext &ctx) const override {
32 33 34 35 36
    auto input_data_type =
        framework::OperatorWithKernel::IndicateVarDataType(ctx, "X");

#ifdef PADDLE_WITH_MKLDNN
    if (this->CanMKLDNNBeUsed(ctx, input_data_type)) {
37 38 39
      // OneDNN uses blocking format, which cannot be always supported with
      // reorders, because if blocked dimension is not divisible by 8 or
      // 16(depending on which blocking format is used) submemory cannot be
J
jakpiase 已提交
40 41
      // created, so in that scenario a fallback is needed
      auto tmp_md = dnnl::memory::desc(
42
          phi::vectorize(ctx.Input<Tensor>("X")->dims()),
J
jakpiase 已提交
43 44 45 46 47
          dnnl::memory::data_type::f32, ctx.Input<Tensor>("X")->format());
      if (tmp_md.data.format_desc.blocking.inner_nblks == 0)
        return framework::OpKernelType(input_data_type, ctx.GetPlace(),
                                       framework::DataLayout::kMKLDNN,
                                       framework::LibraryType::kMKLDNN);
48 49 50
    }
#endif
    return framework::OpKernelType(input_data_type, ctx.GetPlace());
51 52 53 54 55 56 57 58 59 60 61
  }

  framework::OpKernelType GetKernelTypeForVar(
      const std::string &var_name, const Tensor &tensor,
      const framework::OpKernelType &expected_kernel_type) const override {
    if (var_name == "AxisTensor" || var_name == "SectionsTensorList") {
      return expected_kernel_type;
    }
    return framework::OpKernelType(expected_kernel_type.data_type_,
                                   tensor.place(), tensor.layout());
  }
Y
Yancey 已提交
62 63 64 65
};

class SplitOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
Y
Yu Yang 已提交
66
  void Make() override {
67
    AddInput("X", "(Tensor) Input tensor of the split operator.");
68
    AddInput("AxisTensor",
T
tianshuo78520a 已提交
69
             "(Tensor) The axis which the input will be split on. "
70 71 72 73 74 75 76 77 78 79
             "It has higher priority than Attr(axis). "
             "The shape of AxisTensor must be [1]")
        .AsDispensable();
    AddInput("SectionsTensorList",
             "(vector<Tensor<int>>, optional). "
             "The length of each output along the specified axis. "
             "It has a higher priority than Attr(sections)."
             "The shape of the element in vector must be [1].")
        .AsDuplicable()
        .AsDispensable();
80 81
    AddOutput("Out", "(Tensor) Output tensors of the split operator.")
        .AsDuplicable();
Y
Yancey 已提交
82
    AddComment(R"DOC(
83 84 85 86 87 88 89 90 91 92 93 94 95
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 已提交
96 97 98

    )DOC");
    AddAttr<std::vector<int>>("sections",
99 100 101
                              "(vector<int>) "
                              "the length of each output along the "
                              "specified axis.")
Y
Yancey 已提交
102 103
        .SetDefault(std::vector<int>{});
    AddAttr<int>("num",
104 105
                 "(int, default 0)"
                 "Number of sub-tensors. This must evenly divide "
Y
Yancey 已提交
106 107
                 "Input.dims()[axis]")
        .SetDefault(0);
108 109
    AddAttr<int>("axis",
                 "(int, default 0) "
T
tianshuo78520a 已提交
110
                 "The axis which the input will be split on.")
Y
Yancey 已提交
111
        .SetDefault(0);
112 113 114 115 116 117 118 119
    AddAttr<bool>("use_mkldnn",
                  "(bool, default false) Only used in mkldnn kernel")
        .SetDefault(false);
    AddAttr<std::string>(
        "mkldnn_data_type",
        "(string, default \"float32\"). Data type of mkldnn kernel")
        .SetDefault("float32")
        .InEnum({"float32", "bfloat16"});
Y
Yancey 已提交
120 121 122 123 124 125 126
  }
};

}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
H
hong 已提交
127

128 129 130
DELCARE_INFER_SHAPE_FUNCTOR(split, SplitInferShapeFunctor,
                            PT_INFER_META(phi::SplitInferMeta));

H
hong 已提交
131 132
REGISTER_OPERATOR(split, ops::SplitOp, ops::SplitOpMaker,
                  ops::SplitGradMaker<paddle::framework::OpDesc>,
133 134
                  ops::SplitGradMaker<paddle::imperative::OpBase>,
                  SplitInferShapeFunctor);