This repository was archived by the owner on Nov 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Improve split operator by oneDNN reorder primitive #20757
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ba72ffa
Add oneDNN support for array_split operator
0a682f4
benchmark.py
878cd5e
refactor
f47aa18
update
e543126
review fixes
1dfbb3f
fix sanity
c5d816f
fix
7dbe9ad
review
eb1f605
Apply review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file dnnl_split-inl.h | ||
| */ | ||
|
|
||
| #ifndef MXNET_OPERATOR_NN_DNNL_DNNL_SPLIT_INL_H_ | ||
| #define MXNET_OPERATOR_NN_DNNL_DNNL_SPLIT_INL_H_ | ||
|
|
||
| #if MXNET_USE_ONEDNN == 1 | ||
| #include <vector> | ||
|
|
||
| #include "./dnnl_base-inl.h" | ||
| #include "./dnnl_ops-inl.h" | ||
|
|
||
| namespace mxnet { | ||
| namespace op { | ||
|
|
||
| using split_fwd_t = dnnl::reorder; | ||
| using split_fwd_pd_t = dnnl::reorder::primitive_desc; | ||
|
|
||
| class DNNLSplitFwd { | ||
| public: | ||
| struct Tensors { | ||
| Tensors(const NDArray& input, const std::vector<NDArray>& outputs); | ||
|
|
||
| const NDArray& input; | ||
| const std::vector<NDArray>& outputs; | ||
| }; | ||
|
|
||
| static DNNLSplitFwd& GetCached(const SplitParam& param, | ||
| const Tensors& tensors, | ||
| const TShape& split_pts, | ||
| const int split_axis); | ||
|
|
||
| DNNLSplitFwd(const Tensors& tensors, const TShape& split_pts, const int split_axis); | ||
|
|
||
| void Execute(const Tensors& tensors, | ||
| const TShape& split_pts, | ||
| const int split_axis, | ||
| const std::vector<OpReqType>& req) const; | ||
|
|
||
| private: | ||
| std::vector<split_fwd_t> split_fwds; | ||
| std::vector<split_fwd_pd_t> split_pds; | ||
| dnnl::memory::dims strides; | ||
| }; | ||
|
|
||
| } // namespace op | ||
| } // namespace mxnet | ||
| #endif | ||
| #endif // MXNET_OPERATOR_NN_DNNL_DNNL_SPLIT_INL_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,148 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| */ | ||
|
|
||
| /*! | ||
| * \file dnnl_split.cc | ||
| */ | ||
|
|
||
| #if MXNET_USE_ONEDNN == 1 | ||
|
|
||
| #include "../../tensor/matrix_op-inl.h" | ||
| #include "./dnnl_split-inl.h" | ||
|
|
||
| namespace mxnet { | ||
| namespace op { | ||
|
|
||
| bool SupportDNNLSplit(const NDArray& input) { | ||
| static const std::set<int> supported_dtypes = { | ||
| mshadow::kFloat32, mshadow::kBfloat16, mshadow::kInt32, mshadow::kInt8, mshadow::kUint8}; | ||
| return supported_dtypes.count(input.dtype()); | ||
| } | ||
|
|
||
| void DNNLSplitForward(const nnvm::NodeAttrs& attrs, | ||
| const OpContext& ctx, | ||
| const std::vector<NDArray>& inputs, | ||
| const std::vector<OpReqType>& req, | ||
| const std::vector<NDArray>& outputs) { | ||
| const SplitParam& param = dmlc::get<SplitParam>(attrs.parsed); | ||
| const auto tensors = DNNLSplitFwd::Tensors(inputs[0], outputs); | ||
|
|
||
| const auto& ishape = tensors.input.shape(); | ||
| const int split_axis = param.axis >= 0 ? param.axis : param.axis + ishape.ndim(); | ||
| const mxnet::TShape split_pts = | ||
| (param.sections > 0) ? GetSplitIndices(tensors.input.shape(), split_axis, param.sections) : | ||
| param.indices; | ||
|
|
||
| const auto& fwd = DNNLSplitFwd::GetCached(param, tensors, split_pts, split_axis); | ||
| fwd.Execute(tensors, split_pts, split_axis, req); | ||
| } | ||
|
|
||
| DNNLSplitFwd::Tensors::Tensors(const NDArray& input, const std::vector<NDArray>& outputs) | ||
| : input(input), outputs(outputs) {} | ||
|
|
||
| typedef ParamOpSign<SplitParam> DNNLSplitSignature; | ||
|
|
||
| DNNLSplitFwd& DNNLSplitFwd::GetCached(const SplitParam& param, | ||
| const Tensors& tensors, | ||
| const TShape& split_pts, | ||
| const int split_axis) { | ||
| #if DMLC_CXX11_THREAD_LOCAL | ||
| static thread_local std::unordered_map<DNNLSplitSignature, DNNLSplitFwd, OpHash> fwds; | ||
| #else | ||
| static MX_THREAD_LOCAL std::unordered_map<DNNLSplitSignature, DNNLSplitFwd, OpHash> fwds; | ||
| #endif | ||
|
|
||
| DNNLSplitSignature key(param); | ||
| key.AddSign(tensors.input); | ||
| key.AddSign(tensors.outputs); | ||
| key.AddSign(split_pts); | ||
| key.AddSign(split_axis); | ||
| auto it = fwds.find(key); | ||
| if (it == fwds.end()) { | ||
| DNNLSplitFwd fwd(tensors, split_pts, split_axis); | ||
| it = AddToCache(&fwds, key, fwd); | ||
| } | ||
| return it->second; | ||
| } | ||
|
|
||
| DNNLSplitFwd::DNNLSplitFwd(const Tensors& tensors, const TShape& split_pts, const int split_axis) { | ||
| const auto cpu_engine = CpuEngine::Get()->get_engine(); | ||
| const auto input = tensors.input.Reorder2Default(); | ||
| const auto& ishape = input.shape(); | ||
| const auto& dtype = get_dnnl_type(input.dtype()); | ||
| const auto format_tag = static_cast<dnnl::memory::format_tag>(GetDefaultFormat(ishape.ndim())); | ||
|
|
||
| strides = dnnl::memory::dims(ishape.ndim(), 1); | ||
| // last dim stride = 1, start loop from the penultimate | ||
| for (int i = ishape.ndim() - 2; i >= 0; --i) { | ||
| strides[i] = strides[i + 1] * ishape[i + 1]; | ||
bgawrych marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| for (int i = 0; i < tensors.outputs.size(); ++i) { | ||
| const auto& out = tensors.outputs[i]; | ||
| if (out.shape().Size() == 0) { | ||
| continue; | ||
| } | ||
| dnnl::memory::dims dnnl_dims(ishape.begin(), ishape.end()); | ||
| // ending split point is always last dimension | ||
| int end_split_pt = (i + 1 >= split_pts.ndim()) ? ishape[split_axis] : split_pts[i + 1]; | ||
| dnnl_dims[split_axis] = end_split_pt - split_pts[i]; | ||
|
|
||
| auto in_mem_desc = dnnl::memory::desc(dnnl_dims, dtype, strides); | ||
| auto out_mem_desc = dnnl::memory::desc(dnnl_dims, dtype, format_tag); | ||
|
|
||
| const auto split_pd = split_fwd_pd_t(cpu_engine, in_mem_desc, cpu_engine, out_mem_desc); | ||
| split_pds.emplace_back(split_pd); | ||
| split_fwds.emplace_back(split_fwd_t(split_pd)); | ||
| } | ||
| } | ||
|
|
||
| void DNNLSplitFwd::Execute(const Tensors& tensors, | ||
| const TShape& split_pts, | ||
| const int split_axis, | ||
| const std::vector<OpReqType>& req) const { | ||
| const auto& cpu_engine = CpuEngine::Get()->get_engine(); | ||
|
|
||
| const auto& input_tensor = tensors.input.Reorder2Default(); | ||
| int out_idx = 0, primitive_idx = 0; | ||
| int axis_offset = strides[split_axis] * GetTypeSize(input_tensor.dtype()); | ||
| std::byte* input_ptr = reinterpret_cast<std::byte*>(input_tensor.data().dptr_); | ||
|
|
||
| for (const auto& out : tensors.outputs) { | ||
bgawrych marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (out.shape().Size() == 0) { | ||
| out_idx++; | ||
| continue; | ||
| } | ||
| int offset = split_pts[out_idx] * axis_offset; | ||
| auto in_mem = dnnl::memory(split_pds[primitive_idx].src_desc(), cpu_engine, input_ptr + offset); | ||
|
|
||
| auto out_mem = CreateDNNLMem(out, split_pds[primitive_idx].dst_desc(), req[out_idx]); | ||
| DNNLStream::Get()->RegisterPrimArgs(split_fwds[primitive_idx], | ||
| {{DNNL_ARG_SRC, in_mem}, {DNNL_ARG_DST, *out_mem.second}}); | ||
|
|
||
| CommitOutput(out, out_mem); | ||
| ++out_idx; | ||
| ++primitive_idx; | ||
| } | ||
| DNNLStream::Get()->Submit(); | ||
| } | ||
|
|
||
| } // namespace op | ||
| } // namespace mxnet | ||
| #endif | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.