|
| 1 | +/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#include "paddle/fluid/operators/utils.h" |
| 16 | +#include "paddle/fluid/platform/mkldnn_reuse.h" |
| 17 | +namespace paddle { |
| 18 | +namespace operators { |
| 19 | + |
| 20 | +using framework::DataLayout; |
| 21 | +using framework::Tensor; |
| 22 | +using framework::LoDTensor; |
| 23 | +using mkldnn::memory; |
| 24 | +using mkldnn::primitive; |
| 25 | +using mkldnn::concat; |
| 26 | +using mkldnn::stream; |
| 27 | +using platform::to_void_cast; |
| 28 | + |
| 29 | +template <typename T> |
| 30 | +class StackMKLDNNHandler |
| 31 | + : public platform::MKLDNNHandlerNoCachingT<T, dnnl::concat> { |
| 32 | + public: |
| 33 | + StackMKLDNNHandler(const framework::ExecutionContext& ctx, |
| 34 | + const mkldnn::engine mkldnn_engine, |
| 35 | + const std::vector<const Tensor*>& inputs, Tensor* output) |
| 36 | + : platform::MKLDNNHandlerNoCachingT<T, dnnl::concat>(mkldnn_engine, |
| 37 | + ctx.GetPlace()) { |
| 38 | + int stack_axis = ctx.Attr<int>("axis"); |
| 39 | + |
| 40 | + int ndims = inputs[0]->dims().size(); |
| 41 | + |
| 42 | + if (stack_axis < 0) { |
| 43 | + stack_axis = ndims + 1 + stack_axis; // +1 to match output's ndims |
| 44 | + } |
| 45 | + |
| 46 | + // in stack op all inputs must have same dims |
| 47 | + auto input_dims = framework::vectorize<int64_t>(inputs[0]->dims()); |
| 48 | + |
| 49 | + memory::data_type dt = framework::ToMKLDNNDataType(inputs[0]->type()); |
| 50 | + std::vector<memory::desc> srcs_md; |
| 51 | + memory::desc dst_md; |
| 52 | + MKLDNNMemoryFormat dst_fmt; |
| 53 | + |
| 54 | + srcs_md.reserve(inputs.size()); |
| 55 | + |
| 56 | + // if stack is not done on last(non existing) axis, then we can optimize |
| 57 | + // concat primitive by not adding additional dimension, since it causes |
| 58 | + // wrong output format deduction and suboptimal performance as a result |
| 59 | + if (stack_axis != ndims) { |
| 60 | + for (size_t i = 0; i < inputs.size(); ++i) { |
| 61 | + srcs_md.emplace_back(memory::desc(input_dims, dt, inputs[i]->format())); |
| 62 | + } |
| 63 | + |
| 64 | + input_dims[stack_axis] *= inputs.size(); |
| 65 | + dst_md = memory::desc(input_dims, dt, MKLDNNMemoryFormat::any); |
| 66 | + } else { |
| 67 | + auto extended_input_dims = framework::vectorize<int64_t>(output->dims()); |
| 68 | + extended_input_dims[stack_axis] = 1; |
| 69 | + |
| 70 | + for (size_t i = 0; i < inputs.size(); ++i) { |
| 71 | + srcs_md.emplace_back(memory::desc(input_dims, dt, inputs[i]->format()) |
| 72 | + .reshape(extended_input_dims)); |
| 73 | + } |
| 74 | + |
| 75 | + // concat primitive choses suboptimal format tag because it cannot |
| 76 | + // distinguish between f.e. abcd and abdc if last dim is equal to 1 so |
| 77 | + // enforcing is needed for better performance |
| 78 | + dst_fmt = platform::GetPlainMKLDNNFormat(extended_input_dims.size()); |
| 79 | + dst_md = memory::desc(framework::vectorize(output->dims()), dt, dst_fmt); |
| 80 | + } |
| 81 | + |
| 82 | + this->AcquireForwardPrimitiveDescriptor(dst_md, stack_axis, srcs_md); |
| 83 | + } |
| 84 | + |
| 85 | + // concat oneDNN prim is not having .desc attribute so we cannot use default |
| 86 | + // AcquireForwardPrimitiveDescriptor |
| 87 | + void AcquireForwardPrimitiveDescriptor( |
| 88 | + const memory::desc& dst_md, const int stack_axis, |
| 89 | + const std::vector<memory::desc>& srcs_md) { |
| 90 | + this->fwd_pd_.reset(new dnnl::concat::primitive_desc( |
| 91 | + dst_md, stack_axis, srcs_md, this->engine_)); |
| 92 | + } |
| 93 | + |
| 94 | + std::shared_ptr<mkldnn::memory> AcquireSrcMemory(const Tensor& input, int i) { |
| 95 | + const T* input_data = input.data<T>(); |
| 96 | + return this->AcquireMemoryFromPrimitive(this->fwd_pd_->src_desc(i), |
| 97 | + to_void_cast<T>(input_data)); |
| 98 | + } |
| 99 | +}; |
| 100 | + |
| 101 | +template <typename T> |
| 102 | +class StackMKLDNNOpKernel : public paddle::framework::OpKernel<T> { |
| 103 | + public: |
| 104 | + void Compute(const paddle::framework::ExecutionContext& ctx) const override { |
| 105 | + auto& dev_ctx = |
| 106 | + ctx.template device_context<platform::MKLDNNDeviceContext>(); |
| 107 | + const auto& mkldnn_engine = dev_ctx.GetEngine(); |
| 108 | + |
| 109 | + auto multi_input = ctx.MultiInput<Tensor>("X"); |
| 110 | + |
| 111 | + Tensor* output = ctx.Output<Tensor>("Y"); |
| 112 | + |
| 113 | + StackMKLDNNHandler<T> handler(ctx, mkldnn_engine, multi_input, output); |
| 114 | + |
| 115 | + std::vector<std::shared_ptr<memory>> srcs; |
| 116 | + srcs.reserve(multi_input.size()); |
| 117 | + |
| 118 | + auto dst_mem = handler.AcquireDstMemory(output); |
| 119 | + auto concat_p = handler.AcquireForwardPrimitive(); |
| 120 | + |
| 121 | + auto& astream = platform::MKLDNNDeviceContext::tls().get_stream(); |
| 122 | + std::unordered_map<int, memory> args; |
| 123 | + for (size_t i = 0; i < multi_input.size(); ++i) { |
| 124 | + srcs.push_back(handler.AcquireSrcMemory(*(multi_input[i]), i)); |
| 125 | + args.insert({MKLDNN_ARG_MULTIPLE_SRC + i, *(srcs.at(i))}); |
| 126 | + } |
| 127 | + args.insert({MKLDNN_ARG_DST, *dst_mem}); |
| 128 | + |
| 129 | + concat_p->execute(astream, args); |
| 130 | + astream.wait(); |
| 131 | + |
| 132 | + output->set_layout(DataLayout::kMKLDNN); |
| 133 | + output->set_format(platform::GetMKLDNNFormat( |
| 134 | + dst_mem->get_desc().reshape(framework::vectorize(output->dims())))); |
| 135 | + } |
| 136 | +}; |
| 137 | +} // namespace operators |
| 138 | +} // namespace paddle |
| 139 | + |
| 140 | +namespace ops = paddle::operators; |
| 141 | + |
| 142 | +REGISTER_OP_KERNEL(stack, MKLDNN, ::paddle::platform::CPUPlace, |
| 143 | + ops::StackMKLDNNOpKernel<float>); |
0 commit comments