azai91 closed pull request #11328: [MXNET-549] MKLDNNSum can handle variable
number of inputs
URL: https://github.com/apache/incubator-mxnet/pull/11328
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git a/src/operator/nn/mkldnn/mkldnn_base.cc
b/src/operator/nn/mkldnn/mkldnn_base.cc
index b182aa0b68d..a05a3218911 100644
--- a/src/operator/nn/mkldnn/mkldnn_base.cc
+++ b/src/operator/nn/mkldnn/mkldnn_base.cc
@@ -146,7 +146,8 @@ void CommitOutput(const NDArray &arr, const mkldnn_output_t
&res) {
// We have to allocate new memory for the sum result.
auto sum_res = TmpMemMgr::Get()->Alloc(
res.second->get_primitive_desc());
- op::MKLDNNSum(*res.second, *mem, *sum_res);
+ std::vector<mkldnn::memory> in_mems = {*res.second, *mem};
+ op::MKLDNNSum(in_mems, *sum_res);
const_cast<NDArray &>(arr).CopyFrom(*sum_res);
}
}
diff --git a/src/operator/nn/mkldnn/mkldnn_copy.cc
b/src/operator/nn/mkldnn/mkldnn_copy.cc
index 75e51aff006..d6a12f01610 100644
--- a/src/operator/nn/mkldnn/mkldnn_copy.cc
+++ b/src/operator/nn/mkldnn/mkldnn_copy.cc
@@ -50,7 +50,8 @@ void MKLDNNCopy(const nnvm::NodeAttrs& attrs, const OpContext
&ctx,
if (out_mem == nullptr)
out_mem = out_data.GetMKLDNNData();
auto sum_res = TmpMemMgr::Get()->Alloc(out_mem->get_primitive_desc());
- MKLDNNSum(*in_mem, *out_mem, *sum_res);
+ std::vector<mkldnn::memory> in_mems = {in_mem, out_mem};
+ MKLDNNSum(in_mems, *sum_res);
const_cast<NDArray &>(out_data).CopyFrom(*sum_res);
} else {
const_cast<NDArray &>(out_data).CopyFrom(*in_mem);
diff --git a/src/operator/nn/mkldnn/mkldnn_ops-inl.h
b/src/operator/nn/mkldnn/mkldnn_ops-inl.h
index 50937706d93..850fc509d23 100644
--- a/src/operator/nn/mkldnn/mkldnn_ops-inl.h
+++ b/src/operator/nn/mkldnn/mkldnn_ops-inl.h
@@ -104,8 +104,8 @@ void MKLDNNActivationBackward(const nnvm::NodeAttrs& attrs,
const OpContext &ctx
const NDArray &out_grad, const NDArray &in_data,
const OpReqType &req, const NDArray &in_grad);
-void MKLDNNSum(const mkldnn::memory &arr1, const mkldnn::memory &arr2,
- const mkldnn::memory &out);
+void MKLDNNSum(std::vector<mkldnn::memory> &in_mems,
+ const mkldnn::memory &out);
} // namespace op
} // namespace mxnet
diff --git a/src/operator/nn/mkldnn/mkldnn_sum.cc
b/src/operator/nn/mkldnn/mkldnn_sum.cc
index c51e1081d69..00f2a323510 100644
--- a/src/operator/nn/mkldnn/mkldnn_sum.cc
+++ b/src/operator/nn/mkldnn/mkldnn_sum.cc
@@ -23,6 +23,7 @@
* \author Da Zheng
*/
#include <iostream>
+#include <vector>
#include "./mkldnn_ops-inl.h"
#include "./mkldnn_base-inl.h"
@@ -31,16 +32,16 @@
namespace mxnet {
namespace op {
-void MKLDNNSum(const mkldnn::memory &arr1, const mkldnn::memory &arr2,
+void MKLDNNSum(std::vector<mkldnn::memory> &in_mems,
const mkldnn::memory &out) {
- std::vector<mkldnn::memory::primitive_desc> input_pds(2);
- std::vector<float> scales(2, 1);
+ std::vector<mkldnn::memory::primitive_desc> input_pds(in_mems.size());
+ std::vector<float> scales(in_mems.size(), 1);
std::vector<mkldnn::primitive::at> inputs;
- input_pds[0] = arr1.get_primitive_desc();
- input_pds[1] = arr2.get_primitive_desc();
- CHECK(input_pds[0] == input_pds[1]);
- inputs.push_back(arr1);
- inputs.push_back(arr2);
+ for (int i = 0; i < in_mems.size(); i++) {
+ input_pds[i] = in_mems[i].get_primitive_desc();
+ inputs.push_back(in_mems[i]);
+ if (i > 0) CHECK(input_pds[i] == input_pds[i-1]);
+ }
// TODO(zhengda) I need to reorder memory here.
mkldnn::sum::primitive_desc sum_pd(scales, input_pds);
MKLDNNStream::Get()->RegisterPrim(mkldnn::sum(sum_pd, inputs, out));
@@ -54,7 +55,7 @@ void MKLDNNSumForward(const nnvm::NodeAttrs& attrs, const
OpContext &ctx,
}
TmpMemMgr::Get()->Init(ctx.requested[0]);
- std::vector<mkldnn::primitive::at> in_prims;
+ std::vector<mkldnn::memory> in_prims;
std::vector<mkldnn::memory::primitive_desc> in_pds(inputs.size());
std::vector<float> scales(inputs.size(), 1);
in_prims.reserve(inputs.size());
@@ -70,11 +71,10 @@ void MKLDNNSumForward(const nnvm::NodeAttrs& attrs, const
OpContext &ctx,
in_prims.push_back(*in_mem);
in_pds[i] = in_mem->get_primitive_desc();
}
-
mkldnn::sum::primitive_desc pdesc(scales, in_pds);
auto mem = CreateMKLDNNMem(out_data, pdesc.dst_primitive_desc(), req,
&inputs[0]);
MKLDNNStream *stream = MKLDNNStream::Get();
- stream->RegisterPrim(mkldnn::sum(pdesc, in_prims, *mem.second));
+ MKLDNNSum(in_prims, *mem.second);
CommitOutput(out_data, mem);
stream->Submit();
}
diff --git a/tests/cpp/operator/mkldnn.cc b/tests/cpp/operator/mkldnn.cc
index 82fee67b114..45c03d61e62 100644
--- a/tests/cpp/operator/mkldnn.cc
+++ b/tests/cpp/operator/mkldnn.cc
@@ -799,7 +799,8 @@ TEST(MKLDNN_BASE, MKLDNNSum) {
if (out_mem == nullptr)
continue;
PrintVerifyMsg(in_arr, in_arr);
- op::MKLDNNSum(*in_mem1, *in_mem2, *out_mem);
+ std::vector<mkldnn::memory> in_mems = {*in_mem1, *in_mem2};
+ op::MKLDNNSum(in_mems, *out_mem);
MKLDNNStream::Get()->Submit();
VerifySumMemory(*in_mem1, *in_mem2, *out_mem);
}
@@ -812,7 +813,8 @@ TEST(MKLDNN_BASE, MKLDNNSum) {
InitMKLDNNArray(&orig_arr.arr, input_mem->get_primitive_desc(),
InitDefaultArray);
orig_arr.arr.CopyFrom(*input_mem);
auto old_mem = orig_arr.arr.GetMKLDNNData();
- op::MKLDNNSum(*input_mem, *input_mem2, *input_mem);
+ std::vector<mkldnn::memory> in_mems = {*input_mem, *input_mem2};
+ op::MKLDNNSum(in_mems, *input_mem);
MKLDNNStream::Get()->Submit();
VerifySumMemory(*old_mem, *input_mem2, *input_mem);
}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services