split_mkldnn_op.cc 4.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

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/fluid/operators/utils.h"
#include "paddle/fluid/platform/mkldnn_reuse.h"

namespace paddle {
namespace operators {

using paddle::framework::Tensor;

static inline std::vector<std::vector<int64_t>> CalculateOutsDims(
24 25 26 27
    const framework::DDim& in_dims,
    const size_t num,
    const std::vector<int>& sections,
    const size_t axis,
28 29
    const int outs_number) {
  std::vector<std::vector<int64_t>> outs_dims(outs_number,
30
                                              phi::vectorize(in_dims));
31 32

  if (num > 0) {
33 34
    PADDLE_ENFORCE_EQ(in_dims[axis] % num,
                      0,
35 36 37 38 39
                      platform::errors::InvalidArgument(
                          "The input's size along the split dimension "
                          "must be evenly divisible by Attr(num_or_sections). "
                          "But received Attr(num_or_sections) "
                          "= %d, input(X)'s shape = [%s], Attr(dim) = %d.",
40 41 42
                          num,
                          in_dims,
                          axis));
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91

    const size_t out_axis_dim = in_dims[axis] / num;

    for (auto& out_dim : outs_dims) out_dim[axis] = out_axis_dim;
  } else {
    for (size_t i = 0; i < outs_dims.size(); ++i)
      outs_dims[i][axis] = sections[i];
  }
  return outs_dims;
}

template <typename T>
class SplitMKLDNNKernel : public framework::OpKernel<T> {
 public:
  void Compute(const framework::ExecutionContext& ctx) const override {
    this->RunKernel(ctx);
  }

  void RunKernel(const framework::ExecutionContext& ctx) const {
    const auto& dev_ctx =
        ctx.template device_context<platform::MKLDNNDeviceContext>();
    const auto& onednn_engine = dev_ctx.GetEngine();

    const auto* x = ctx.Input<Tensor>("X");
    auto outs = ctx.MultiOutput<Tensor>("Out");

    int num = ctx.Attr<int>("num");
    auto sections = ctx.Attr<std::vector<int>>("sections");
    int axis = ctx.Attr<int>("axis");
    auto outs_number = outs.size();
    const auto x_dims = x->dims();

    bool need_resize = false;
    if (ctx.HasInput("AxisTensor")) {
      auto* axis_tensor = ctx.Input<Tensor>("AxisTensor");
      axis = GetDataFromTensor(axis_tensor)[0];
      need_resize = true;
    }

    auto sections_tensor_list = ctx.MultiInput<Tensor>("SectionsTensorList");
    if (sections_tensor_list.size() > 0) {
      sections = GetDataFromTensorList(sections_tensor_list);
      need_resize = true;
    }

    if (need_resize) {
      const auto outs_dims =
          CalculateOutsDims(x->dims(), num, sections, axis, outs_number);
      for (size_t i = 0; i < outs.size(); ++i) {
92
        outs[i]->Resize(phi::make_ddim(outs_dims[i]));
93 94 95
      }
    }

96
    auto x_vec_dims = phi::vectorize(x_dims);
97

98 99
    dnnl::memory::data_type x_type =
        framework::ToMKLDNNDataType(framework::TransToProtoVarType(x->dtype()));
100 101 102 103 104

    auto& astream = platform::MKLDNNDeviceContext::tls().get_stream();

    std::vector<int64_t> offset(x_vec_dims.size(), 0);

105
    platform::ReorderMKLDNNHandler reorder_handler(
106 107 108
        x_vec_dims,
        framework::TransToProtoVarType(x->dtype()),
        x_type,
109
        onednn_engine);
110
    auto reorder_src_memory_p = reorder_handler.AcquireSrcMemory(
111
        x->mem_desc(), platform::to_void_cast(x->data<T>()));
112 113

    for (size_t i = 0; i < outs_number; ++i) {
114
      auto out_vec_dims = phi::vectorize(outs[i]->dims());
115 116
      auto slice_mem_p = reorder_handler.AcquireSubmemory(
          out_vec_dims, offset, reorder_src_memory_p);
117 118

      auto reorder_dst_memory_p = reorder_handler.AcquireDstMemory(
119
          outs[i], out_vec_dims, x->format(), ctx.GetPlace());
120
      auto reorder_p =
121
          reorder_handler.AcquireReorder(reorder_dst_memory_p, slice_mem_p);
122 123 124 125 126

      reorder_p->execute(astream, *slice_mem_p, *reorder_dst_memory_p);

      offset[axis] += num > 0 ? x->dims()[axis] / num : sections[i];

127
      outs[i]->set_mem_desc(reorder_dst_memory_p->get_desc());
128 129 130 131 132 133 134 135
    }
    astream.wait();
  }
};
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
136 137 138
REGISTER_OP_KERNEL(split,
                   MKLDNN,
                   paddle::platform::CPUPlace,
139 140
                   ops::SplitMKLDNNKernel<float>,
                   ops::SplitMKLDNNKernel<paddle::platform::bfloat16>);