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/11/19 08:54:29 UTC

[GitHub] [incubator-mxnet] bgawrych commented on a change in pull request #20669: Add oneDNN support for reduce operators

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



##########
File path: src/operator/nn/dnnl/dnnl_reduce.cc
##########
@@ -0,0 +1,236 @@
+/*
+ * 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_reduce.cc
+ * \brief
+ */
+
+#if MXNET_USE_ONEDNN == 1
+
+#include "./dnnl_reduce-inl.h"
+#include "../../numpy/np_broadcast_reduce_op.h"
+
+namespace mxnet {
+namespace op {
+
+template <>
+NumpyReduceAxesParam ConvertParamsToNumpy<ReduceAxesParam>(const ReduceAxesParam& original_param,
+                                                           const NDArray& input,
+                                                           const NDArray& output) {
+  NumpyReduceAxesParam numpy_param;
+  numpy_param.axis = dmlc::optional<mxnet::Tuple<int>>();
+  if (original_param.axis.has_value()) {
+    mxnet::Tuple<int> axes(original_param.axis.value().begin(), original_param.axis.value().end());
+    std::sort(axes.begin(), axes.end());
+
+    if (original_param.exclude) {
+      const size_t in_ndim = input.shape().ndim();
+      mxnet::Tuple<int> inverted_axes(in_ndim - axes.ndim(), -1);
+      for (int i = 0, j = 0; i < input.shape().ndim(); i++) {
+        if (j >= axes.ndim() || i != axes[j]) {
+          inverted_axes[i - j] = i;
+        } else {
+          j++;
+        }
+      }
+      numpy_param.axis = inverted_axes;
+    } else {
+      numpy_param.axis = axes;
+    }
+  }
+  numpy_param.keepdims = original_param.keepdims;
+  numpy_param.dtype    = dmlc::optional<int>(output.dtype());
+  return numpy_param;
+}
+
+template <>
+NumpyReduceAxesParam ConvertParamsToNumpy<NumpyReduceAxesParam>(
+    const NumpyReduceAxesParam& original_param,
+    const NDArray& input,
+    const NDArray& output) {
+  return original_param;
+}
+
+mxnet::Tuple<int> CanonicalizeAndSortAxes(const NDArray& input,
+                                          const NumpyReduceAxesParam& param,
+                                          mxnet::Tuple<int> original_axes) {
+  int in_ndim = input.shape().ndim();
+  mxnet::Tuple<int> axes(param.axis.value());
+  // canonicalize
+  for (index_t i = 0; i < axes.ndim(); i++) {
+    if (axes[i] < 0) {
+      axes[i] += in_ndim;
+    }
+  }
+  std::sort(axes.begin(), axes.end());
+  return axes;
+}
+
+bool SupportDNNLReduceImpl(const NumpyReduceAxesParam& param,
+                           const NDArray& input,
+                           const NDArray& output) {
+  int in_ndim          = input.shape().ndim();
+  int out_size         = output.shape().Size();
+  int in_size          = input.shape().Size();
+  bool param_supported = true;
+  if (param.axis.has_value()) {
+    auto axes    = CanonicalizeAndSortAxes(input, param, param.axis.value());
+    int last_dim = *(axes.end() - 1);
+    if (last_dim != input.shape().ndim() - 1) {
+      // oneDNN not optimized case

Review comment:
       added version




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