This repository has been 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.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Integrate oneDNN support for add, subtract, multiply, divid…
…e. (#20713) * Integrate oneDNN support for binary elementwise operators. * Delete template xpu for BinaryOperatorComputeExCPU function * Fix binary operators StorageType functio. * Fix SupportDNNLBinary function. * Fix test_operator, DNNLAlgorithm structure, DNNLData and DNNLBinaryOpForward condition. * Fix test cases, add oneDNN runtime flag to dispatch, remove node attrs, rename pointers * Fix sanity
- Loading branch information
Showing
11 changed files
with
370 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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,86 @@ | ||
/* | ||
* 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_binary-inl.h | ||
* \author: Adam Grabowski, [email protected] | ||
*/ | ||
|
||
#ifndef MXNET_OPERATOR_NN_DNNL_DNNL_BINARY_INL_H_ | ||
#define MXNET_OPERATOR_NN_DNNL_DNNL_BINARY_INL_H_ | ||
|
||
#if MXNET_USE_ONEDNN == 1 | ||
#include "./dnnl_base-inl.h" | ||
#include "./dnnl_ops-inl.h" | ||
#include <vector> | ||
|
||
#include "../../tensor/elemwise_binary_broadcast_op.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
using binary_fwd_t = dnnl::binary; | ||
using binary_fwd_pd_t = dnnl::binary::primitive_desc; | ||
|
||
class DNNLBinaryOpFwd { | ||
public: | ||
template <dnnl::algorithm alg> | ||
static DNNLBinaryOpFwd& GetBinaryOpForward(const std::vector<NDArray>& inputs, | ||
const std::vector<NDArray>& outputs); | ||
DNNLBinaryOpFwd(const dnnl::algorithm alg, | ||
const std::vector<NDArray>& inputs, | ||
const std::vector<NDArray>& outputs); | ||
|
||
void Execute(const std::vector<NDArray>& inputs, | ||
const std::vector<OpReqType>& req, | ||
const std::vector<NDArray>& outputs); | ||
|
||
private: | ||
std::shared_ptr<binary_fwd_t> fwd; | ||
std::shared_ptr<binary_fwd_pd_t> fwd_pd; | ||
}; | ||
|
||
template <dnnl::algorithm alg> | ||
DNNLBinaryOpFwd& DNNLBinaryOpFwd::GetBinaryOpForward(const std::vector<NDArray>& inputs, | ||
const std::vector<NDArray>& outputs) { | ||
using binary_op_fwd_map = std::unordered_map<OpSignature, DNNLBinaryOpFwd, OpHash>; | ||
#if DMLC_CXX11_THREAD_LOCAL | ||
static thread_local binary_op_fwd_map fwds; | ||
#else | ||
static MX_THREAD_LOCAL binary_op_fwd_map fwds; | ||
#endif | ||
OpSignature key; | ||
key.AddSign(static_cast<int>(alg)); | ||
key.AddSign(inputs[0]); | ||
key.AddSign(inputs[1]); | ||
key.AddSign(outputs[0]); | ||
|
||
auto it = fwds.find(key); | ||
if (it == fwds.end()) { | ||
const DNNLBinaryOpFwd fwd(alg, inputs, outputs); | ||
it = AddToCache(&fwds, key, fwd); | ||
} | ||
return it->second; | ||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_USE_ONEDNN == 1 | ||
#endif // MXNET_OPERATOR_NN_DNNL_DNNL_BINARY_INL_H_ |
This file contains 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,78 @@ | ||
/* | ||
* 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_binary.cc | ||
* \author: Adam Grabowski, [email protected] | ||
*/ | ||
|
||
#if MXNET_USE_ONEDNN == 1 | ||
#include "./dnnl_binary-inl.h" | ||
|
||
namespace mxnet { | ||
namespace op { | ||
|
||
DNNLBinaryOpFwd::DNNLBinaryOpFwd(const dnnl::algorithm alg, | ||
const std::vector<NDArray>& inputs, | ||
const std::vector<NDArray>& outputs) { | ||
auto src0_desc = inputs[0].GetDNNLData()->get_desc(); | ||
auto src1_desc = inputs[1].GetDNNLData()->get_desc(); | ||
auto dst_desc = outputs[0].GetDNNLData()->get_desc(); | ||
|
||
dnnl::binary::desc fwd_desc(alg, src0_desc, src1_desc, dst_desc); | ||
fwd_pd = std::make_shared<binary_fwd_pd_t>(fwd_desc, mxnet::CpuEngine::Get()->get_engine()); | ||
fwd = std::make_shared<binary_fwd_t>(*fwd_pd); | ||
} | ||
|
||
void DNNLBinaryOpFwd::Execute(const std::vector<NDArray>& inputs, | ||
const std::vector<OpReqType>& req, | ||
const std::vector<NDArray>& outputs) { | ||
auto engine = mxnet::CpuEngine::Get()->get_engine(); | ||
auto src0 = inputs[0].GetDNNLData(); | ||
auto src1 = inputs[1].GetDNNLData(); | ||
dnnl_output_t out_mem; | ||
if (outputs[0].GetDNNLData()->get_data_handle() == inputs[1].GetDNNLData()->get_data_handle()) | ||
out_mem = CreateDNNLMem(outputs[0], fwd_pd->dst_desc(), req[0], &inputs[1]); | ||
else | ||
out_mem = CreateDNNLMem(outputs[0], fwd_pd->dst_desc(), req[0], &inputs[0]); | ||
|
||
dnnl_args_map_t args = { | ||
{DNNL_ARG_SRC_0, *src0}, | ||
{DNNL_ARG_SRC_1, *src1}, | ||
{DNNL_ARG_DST, *out_mem.second}, | ||
}; | ||
|
||
DNNLStream::Get()->RegisterPrimArgs(*fwd, args); | ||
CommitOutput(outputs[0], out_mem); | ||
DNNLStream::Get()->Submit(); | ||
} | ||
|
||
bool SupportDNNLBinary(const std::vector<NDArray>& inputs) { | ||
auto dtype = inputs[0].dtype(); | ||
auto ndim_0 = inputs[0].shape().ndim(); | ||
auto ndim_1 = inputs[1].shape().ndim(); | ||
return ndim_0 >= 1 && ndim_0 <= 6 && ndim_1 >= 1 && ndim_1 <= 6 && | ||
inputs[0].shape().Size() != 0 && inputs[1].shape().Size() != 0 && | ||
dtype == mshadow::kFloat32 && dtype == inputs[1].dtype(); | ||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_USE_ONEDNN == 1 |
This file contains 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 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 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 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 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 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
Oops, something went wrong.