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/17 14:14:35 UTC
[GitHub] [incubator-mxnet] anko-intel commented on a change in pull request #20669: Add oneDNN support for reduce operators
anko-intel commented on a change in pull request #20669:
URL: https://github.com/apache/incubator-mxnet/pull/20669#discussion_r751255879
##########
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,
Review comment:
what about using CanonicalizeAxes?
##########
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:
not optimized in particular oneDNN version ?
##########
File path: src/operator/nn/dnnl/dnnl_reduce-inl.h
##########
@@ -0,0 +1,108 @@
+/*
+ * 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-inl.h
+ */
+
+#ifndef MXNET_OPERATOR_NN_DNNL_DNNL_REDUCE_INL_H_
+#define MXNET_OPERATOR_NN_DNNL_DNNL_REDUCE_INL_H_
+
+#if MXNET_USE_ONEDNN == 1
+#include <vector>
+
+#include "./dnnl_base-inl.h"
+#include "./dnnl_ops-inl.h"
+
+namespace mxnet {
+namespace op {
+
+using reduce_fwd_t = dnnl::reduction;
+using reduce_fwd_pd_t = dnnl::reduction::primitive_desc;
+struct NumpyReduceAxesParam;
+struct ReduceAxesParam;
+class DNNLReduceFwd {
+ public:
+ struct Tensors {
+ Tensors(const NDArray& data, const NDArray& out);
+
+ const NDArray& data;
+ const NDArray& out;
+ };
+
+ static DNNLReduceFwd GetCached(const NumpyReduceAxesParam& param,
+ const Tensors& tensors,
+ const bool is_train,
+ const dnnl::algorithm reduction_alg);
+
+ static reduce_fwd_pd_t GetReduceFwdPd(const dnnl::memory::desc& input_md,
+ const dnnl::memory::desc& output_md,
+ const dnnl::algorithm reduction_alg);
+
+ DNNLReduceFwd(const NumpyReduceAxesParam& param,
+ const Tensors& tensors,
+ const bool is_train,
+ const dnnl::algorithm reduction_alg);
+ void Execute(const Tensors& tensors) const;
+
+ private:
+ std::shared_ptr<reduce_fwd_pd_t> reduce_pd;
+ std::shared_ptr<reduce_fwd_t> reduce_fwd;
+};
+
+template <class T>
+NumpyReduceAxesParam ConvertParamsToNumpy(const T& original_param,
Review comment:
is it possible that different class has the same functionality? So maybe the function name should be different depending what is returned?
--
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