-
Notifications
You must be signed in to change notification settings - Fork 6.8k
[FEATURE] Integrate oneDNN binary primitive support for forward add, subtract, multiply, divide. #20713
[FEATURE] Integrate oneDNN binary primitive support for forward add, subtract, multiply, divide. #20713
Changes from all commits
44568a0
5acdf4f
ea64b1f
27f157d
fc6cc12
f2f52fe
3d7100a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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_ |
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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please check if oneDNN supports bfloat, if yes please create separate PR for it. |
||
} | ||
|
||
} // namespace op | ||
} // namespace mxnet | ||
|
||
#endif // MXNET_USE_ONEDNN == 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,6 +33,10 @@ MXNET_OPERATOR_REGISTER_NP_BINARY_MIXED_PRECISION(_npi_subtract) | |
op::mshadow_op::minus, | ||
op::mshadow_op::mixed_minus, | ||
op::mshadow_op::mixed_rminus>) | ||
#if MXNET_USE_ONEDNN == 1 | ||
.set_attr<FComputeEx>("FComputeEx<cpu>", NumpyBinaryOperatorComputeExCPU<op::mshadow_op::minus>) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about mixed version? is it work properly for GPU if oneDNN is enabled (default configuration). Could you check if there is any test for it ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. OneDNN dispatch is only taken under consideration after dev_mask == mshadow::cpu::kDevMask condition is met, thus not affecting GPU workflow. |
||
.set_attr<FInferStorageType>("FInferStorageType", NumpyBinaryBroadcastStorageType) | ||
#endif // MXNET_USE_ONEDNN | ||
.set_attr<nnvm::FGradient>("FGradient", ElemwiseGradUseIn{"_backward_npi_broadcast_sub"}); | ||
|
||
NNVM_REGISTER_OP(_backward_npi_broadcast_sub) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what about "attrs" in the key ?
I think we probably should add attrs to key or remove it form DNNLBinaryOpFwd constructor parameters
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to remove attrs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
attrs removed where it was possible