You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2022/01/10 13:42:05 UTC

[GitHub] [incubator-mxnet] bgawrych commented on a change in pull request #20757: Improve split operator by oneDNN reorder primitive

bgawrych commented on a change in pull request #20757:
URL: https://github.com/apache/incubator-mxnet/pull/20757#discussion_r781200498



##########
File path: src/operator/nn/dnnl/dnnl_split.cc
##########
@@ -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];
+  }
+
+  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) {

Review comment:
       Split op can split data into multiple different tensors basing on passed axis and split points. Split points can be irregular e.g. we can split tensor of shape [4, 6, 6] into tensors [4, 1, 6], [4, 2, 6] and [4,3,6]. We need different reorder primitives to have different dimensions of output tensors and as there is reorder2default I'm passing address with offset to dnnl memory to copy memory from input to output




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@mxnet.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org