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 2021/12/07 14:21:10 UTC

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

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



##########
File path: src/operator/nn/dnnl/dnnl_split-inl.h
##########
@@ -0,0 +1,70 @@
+/*
+ * 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-inl.h
+ */
+
+#ifndef MXNET_OPERATOR_NN_DNNL_DNNL_SPLIT_INL_H_
+#define MXNET_OPERATOR_NN_DNNL_DNNL_SPLIT_INL_H_
+
+#if MXNET_USE_ONEDNN == 1
+#include <vector>
+
+#include "./dnnl_base-inl.h"
+#include "./dnnl_ops-inl.h"
+
+namespace mxnet {
+namespace op {
+
+using split_fwd_t    = dnnl::reorder;
+using split_fwd_pd_t = dnnl::reorder::primitive_desc;
+
+class DNNLSplitFwd {
+ public:
+  struct Tensors {
+    Tensors(const NDArray& input, const std::vector<NDArray>& outputs);
+
+    const NDArray& input;
+    const std::vector<NDArray>& outputs;
+  };
+
+  static DNNLSplitFwd GetCached(const SplitParam& param,
+                                const Tensors& tensors,
+                                const TShape& split_pts,
+                                const int split_axis);
+
+  DNNLSplitFwd(const Tensors& tensors, const TShape& split_pts, const int split_axis);
+
+  void Execute(const Tensors& tensors,
+               const TShape& split_pts,
+               const int split_axis,
+               const std::vector<OpReqType>& req) const;
+
+ private:
+  std::vector<split_fwd_t> split_fwds;
+  std::vector<split_fwd_pd_t> split_pds;
+};
+
+bool SupportDNNLSplit(const NDArray& input);

Review comment:
       SupportDNNL functions' definitions are in dnnl_base-inl.h file.

##########
File path: src/operator/nn/dnnl/dnnl_split.cc
##########
@@ -0,0 +1,155 @@
+/*
+ * 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()));
+
+  dnnl::memory::dims strides(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];

Review comment:
       This vector could be an attribute of DNNLSplitFwd class to avoid duplicate loop in Execute() function.

##########
File path: src/operator/nn/dnnl/dnnl_split.cc
##########
@@ -0,0 +1,155 @@
+/*
+ * 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()));
+
+  dnnl::memory::dims strides(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();
+  const auto& ishape       = input_tensor.shape();
+
+  std::vector<int> strides(ishape.ndim(), 1);
+  for (int i = ishape.ndim() - 2; i >= 0; --i) {
+    strides[i] = strides[i + 1] * ishape[i + 1];

Review comment:
       This vector could be an attribute initialized in DNNLSplitFwd() class constructor.




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