|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file dnnl_split.cc |
| 22 | + */ |
| 23 | + |
| 24 | +#if MXNET_USE_ONEDNN == 1 |
| 25 | + |
| 26 | +#include "../../tensor/matrix_op-inl.h" |
| 27 | +#include "./dnnl_split-inl.h" |
| 28 | + |
| 29 | +namespace mxnet { |
| 30 | +namespace op { |
| 31 | + |
| 32 | +bool SupportDNNLSplit(const NDArray& input) { |
| 33 | + static const std::set<int> supported_dtypes = { |
| 34 | + mshadow::kFloat32, mshadow::kBfloat16, mshadow::kInt32, mshadow::kInt8, mshadow::kUint8}; |
| 35 | + return supported_dtypes.count(input.dtype()); |
| 36 | +} |
| 37 | + |
| 38 | +void DNNLSplitForward(const nnvm::NodeAttrs& attrs, |
| 39 | + const OpContext& ctx, |
| 40 | + const std::vector<NDArray>& inputs, |
| 41 | + const std::vector<OpReqType>& req, |
| 42 | + const std::vector<NDArray>& outputs) { |
| 43 | + const SplitParam& param = dmlc::get<SplitParam>(attrs.parsed); |
| 44 | + const auto tensors = DNNLSplitFwd::Tensors(inputs[0], outputs); |
| 45 | + |
| 46 | + const auto& ishape = tensors.input.shape(); |
| 47 | + const int split_axis = param.axis >= 0 ? param.axis : param.axis + ishape.ndim(); |
| 48 | + const mxnet::TShape split_pts = |
| 49 | + (param.sections > 0) ? GetSplitIndices(tensors.input.shape(), split_axis, param.sections) : |
| 50 | + param.indices; |
| 51 | + |
| 52 | + const auto& fwd = DNNLSplitFwd::GetCached(param, tensors, split_pts, split_axis); |
| 53 | + fwd.Execute(tensors, split_pts, split_axis, req); |
| 54 | +} |
| 55 | + |
| 56 | +DNNLSplitFwd::Tensors::Tensors(const NDArray& input, const std::vector<NDArray>& outputs) |
| 57 | + : input(input), outputs(outputs) {} |
| 58 | + |
| 59 | +typedef ParamOpSign<SplitParam> DNNLSplitSignature; |
| 60 | + |
| 61 | +DNNLSplitFwd& DNNLSplitFwd::GetCached(const SplitParam& param, |
| 62 | + const Tensors& tensors, |
| 63 | + const TShape& split_pts, |
| 64 | + const int split_axis) { |
| 65 | +#if DMLC_CXX11_THREAD_LOCAL |
| 66 | + static thread_local std::unordered_map<DNNLSplitSignature, DNNLSplitFwd, OpHash> fwds; |
| 67 | +#else |
| 68 | + static MX_THREAD_LOCAL std::unordered_map<DNNLSplitSignature, DNNLSplitFwd, OpHash> fwds; |
| 69 | +#endif |
| 70 | + |
| 71 | + DNNLSplitSignature key(param); |
| 72 | + key.AddSign(tensors.input); |
| 73 | + key.AddSign(tensors.outputs); |
| 74 | + key.AddSign(split_pts); |
| 75 | + key.AddSign(split_axis); |
| 76 | + auto it = fwds.find(key); |
| 77 | + if (it == fwds.end()) { |
| 78 | + DNNLSplitFwd fwd(tensors, split_pts, split_axis); |
| 79 | + it = AddToCache(&fwds, key, fwd); |
| 80 | + } |
| 81 | + return it->second; |
| 82 | +} |
| 83 | + |
| 84 | +DNNLSplitFwd::DNNLSplitFwd(const Tensors& tensors, const TShape& split_pts, const int split_axis) { |
| 85 | + const auto cpu_engine = CpuEngine::Get()->get_engine(); |
| 86 | + const auto input = tensors.input.Reorder2Default(); |
| 87 | + const auto& ishape = input.shape(); |
| 88 | + const auto& dtype = get_dnnl_type(input.dtype()); |
| 89 | + const auto format_tag = static_cast<dnnl::memory::format_tag>(GetDefaultFormat(ishape.ndim())); |
| 90 | + |
| 91 | + strides = dnnl::memory::dims(ishape.ndim(), 1); |
| 92 | + // last dim stride = 1, start loop from the penultimate |
| 93 | + for (int i = ishape.ndim() - 2; i >= 0; --i) { |
| 94 | + strides[i] = strides[i + 1] * ishape[i + 1]; |
| 95 | + } |
| 96 | + |
| 97 | + for (int i = 0; i < tensors.outputs.size(); ++i) { |
| 98 | + const auto& out = tensors.outputs[i]; |
| 99 | + if (out.shape().Size() == 0) { |
| 100 | + continue; |
| 101 | + } |
| 102 | + dnnl::memory::dims dnnl_dims(ishape.begin(), ishape.end()); |
| 103 | + // ending split point is always last dimension |
| 104 | + int end_split_pt = (i + 1 >= split_pts.ndim()) ? ishape[split_axis] : split_pts[i + 1]; |
| 105 | + dnnl_dims[split_axis] = end_split_pt - split_pts[i]; |
| 106 | + |
| 107 | + auto in_mem_desc = dnnl::memory::desc(dnnl_dims, dtype, strides); |
| 108 | + auto out_mem_desc = dnnl::memory::desc(dnnl_dims, dtype, format_tag); |
| 109 | + |
| 110 | + const auto split_pd = split_fwd_pd_t(cpu_engine, in_mem_desc, cpu_engine, out_mem_desc); |
| 111 | + split_pds.emplace_back(split_pd); |
| 112 | + split_fwds.emplace_back(split_fwd_t(split_pd)); |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +void DNNLSplitFwd::Execute(const Tensors& tensors, |
| 117 | + const TShape& split_pts, |
| 118 | + const int split_axis, |
| 119 | + const std::vector<OpReqType>& req) const { |
| 120 | + const auto& cpu_engine = CpuEngine::Get()->get_engine(); |
| 121 | + |
| 122 | + const auto& input_tensor = tensors.input.Reorder2Default(); |
| 123 | + int out_idx = 0, primitive_idx = 0; |
| 124 | + int axis_offset = strides[split_axis] * GetTypeSize(input_tensor.dtype()); |
| 125 | + std::byte* input_ptr = reinterpret_cast<std::byte*>(input_tensor.data().dptr_); |
| 126 | + |
| 127 | + for (const auto& out : tensors.outputs) { |
| 128 | + if (out.shape().Size() == 0) { |
| 129 | + out_idx++; |
| 130 | + continue; |
| 131 | + } |
| 132 | + int offset = split_pts[out_idx] * axis_offset; |
| 133 | + auto in_mem = dnnl::memory(split_pds[primitive_idx].src_desc(), cpu_engine, input_ptr + offset); |
| 134 | + |
| 135 | + auto out_mem = CreateDNNLMem(out, split_pds[primitive_idx].dst_desc(), req[out_idx]); |
| 136 | + DNNLStream::Get()->RegisterPrimArgs(split_fwds[primitive_idx], |
| 137 | + {{DNNL_ARG_SRC, in_mem}, {DNNL_ARG_DST, *out_mem.second}}); |
| 138 | + |
| 139 | + CommitOutput(out, out_mem); |
| 140 | + ++out_idx; |
| 141 | + ++primitive_idx; |
| 142 | + } |
| 143 | + DNNLStream::Get()->Submit(); |
| 144 | +} |
| 145 | + |
| 146 | +} // namespace op |
| 147 | +} // namespace mxnet |
| 148 | +#endif |
0 commit comments