zheng-da commented on a change in pull request #10315: [MXNET-249] Add inplace 
support to mkldnn sum
URL: https://github.com/apache/incubator-mxnet/pull/10315#discussion_r178436558
 
 

 ##########
 File path: src/operator/nn/mkldnn/mkldnn_sum.cc
 ##########
 @@ -49,23 +49,42 @@ void Sum(const mkldnn::memory &arr1, const mkldnn::memory 
&arr2,
 void MKLDNNSumForward(const nnvm::NodeAttrs& attrs, const OpContext &ctx,
                       const std::vector<NDArray> &inputs, const OpReqType &req,
                       const NDArray &out_data) {
+  if (req == kNullOp) {
+    return;
+  }
+
   TmpMemMgr::Get()->Init(ctx.requested[0]);
   std::vector<mkldnn::primitive::at> in_prims;
   std::vector<mkldnn::memory::primitive_desc> in_pds(inputs.size());
   std::vector<float> scales(inputs.size(), 1);
   in_prims.reserve(inputs.size());
+  bool pd_same = true;
   for (size_t i = 0; i < inputs.size(); i++) {
     auto in_mem = inputs[i].GetMKLDNNData();
     in_prims.push_back(*in_mem);
     in_pds[i] = in_mem->get_primitive_desc();
+    pd_same = pd_same && (in_pds[i] == in_pds[0]);
   }
-  mkldnn::sum::primitive_desc pdesc(scales, in_pds);
 
-  auto out_mem = CreateMKLDNNMem(out_data, pdesc.dst_primitive_desc(), req);
-  MKLDNNStream *stream = MKLDNNStream::Get();
-  stream->RegisterPrim(mkldnn::sum(pdesc, in_prims, *out_mem.second));
-  CommitOutput(out_data, out_mem);
-  stream->Submit();
+  mkldnn::sum::primitive_desc pdesc(scales, in_pds);
+  pd_same = pd_same && (pdesc.dst_primitive_desc() == in_pds[0]);
+  auto out_mem = 
const_cast<NDArray&>(out_data).CreateMKLDNNData(pdesc.dst_primitive_desc());
+  bool addr_same = out_mem->get_data_handle() == 
inputs[0].GetMKLDNNData()->get_data_handle();
 
 Review comment:
   my PR tries to make GetMKLDNNData() lightweight. OK, I guess it's not a big 
problem.

----------------------------------------------------------------
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

Reply via email to