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/03/05 11:43:39 UTC

[GitHub] [incubator-mxnet] bartekkuncer commented on a change in pull request #19896: [FEATURE] OneDNN adaptive pooling support was added to mxnet.

bartekkuncer commented on a change in pull request #19896:
URL: https://github.com/apache/incubator-mxnet/pull/19896#discussion_r588225900



##########
File path: src/operator/contrib/adaptive_avg_pooling.cc
##########
@@ -197,6 +198,91 @@ num_threads(engine::OpenMP::Get()->GetRecommendedOMPThreadCount())
   }
 }
 
+#if MXNET_USE_MKLDNN == 1
+bool CanMKLDNNSupportAveragePooling(const NDArray &in_data,

Review comment:
       Please change the name to SupportMKLDNNAveragePooling or SupportMKLDNNAP to stay consistent with other implementations.
   

##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling-inl.h
##########
@@ -0,0 +1,147 @@
+/*
+ * 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 mkldnn_adaptive_pooling-inl.h
+ * \brief
+ * \author Mateusz Ozga
+*/
+#ifndef MXNET_OPERATOR_NN_MKLDNN_MKLDNN_ADAPTIVE_POOLING_INL_H_
+#define MXNET_OPERATOR_NN_MKLDNN_MKLDNN_ADAPTIVE_POOLING_INL_H_
+
+#if MXNET_USE_MKLDNN == 1
+
+#include <mkldnn.hpp>
+#include <utility>
+#include "../../operator_common.h"
+#include "./mkldnn_base-inl.h"
+
+namespace mxnet {
+namespace op {
+
+class MKLDNNAdaptivePoolingFwd {
+ public:
+  MKLDNNAdaptivePoolingFwd(const mxnet::NDArray &input,
+                           const mxnet::NDArray &output,
+                           const mkldnn::memory::dims &kernel,
+                           const mkldnn::memory::dims &strides,
+                           const mkldnn::memory::dims &pad_l,
+                           const mkldnn::memory::dims &pad_r,
+                           const mkldnn::algorithm alg_kind,
+                           const bool with_workspace, const bool is_train)
+      : with_workspace_(with_workspace), fwd_(nullptr) {
+    Init(input, output, kernel, strides, pad_l, pad_r, is_train, alg_kind);
+  }
+  ~MKLDNNAdaptivePoolingFwd() = default;
+
+ public:
+  void Execute(const NDArray &input, const OpReqType req, const NDArray &output,
+               const NDArray *workspace);
+
+ private:
+  bool with_workspace_;
+  std::shared_ptr<mkldnn::pooling_forward::primitive_desc> fwd_pd_;
+  std::shared_ptr<mkldnn::pooling_forward> fwd_;
+
+ private:
+  void Init(const mxnet::NDArray &input, const mxnet::NDArray &output,
+            const mkldnn::memory::dims &kernel,
+            const mkldnn::memory::dims &strides,
+            const mkldnn::memory::dims &pad_l,
+            const mkldnn::memory::dims &pad_r, const bool is_train,
+            const mkldnn::algorithm alg_kind);
+};
+
+
+template <typename T = mkldnn::memory::dims>
+void updateAdaptivePaddingKernel(T *kernel, T *strides, T *pad_l, T *pad_r,
+                                 const NDArray &in_data,
+                                 const NDArray &out_data) {
+  const int IH = in_data.shape()[2];
+  const int IW = in_data.shape()[3];
+  const int OH = out_data.shape()[2];
+  const int OW = out_data.shape()[3];
+
+  strides->at(0) = floor((IH << 1) / OH) - floor(IH / OH);
+  strides->at(1) = floor((IW << 1) / OW) - floor(IW / OW);
+  kernel->at(0) = ceil((IH << 1) / OH) - floor(IH / OH);
+  kernel->at(1) = ceil((IW << 1) / OW) - floor(IW / OW);
+  pad_l->at(0) = (strides->at(0) * (OH - 1) + kernel->at(0) - IH) >> 1;
+  pad_l->at(1) = (strides->at(1) * (OW - 1) + kernel->at(1) - IW) >> 1;
+}
+
+template <typename T>
+MKLDNNAdaptivePoolingFwd &GetPoolingFwd(const T &param, const bool is_train,
+                                        const NDArray &input,
+                                        const NDArray &output) {
+  if (input.shape().ndim() != 4) {
+    LOG(FATAL) << "MKLDNN Adaptive Avg Pool 2d: Expect only 2D input";
+  }
+  typedef ParamOpSign<T> MKLDNNPoolingSignature;
+#if DMLC_CXX11_THREAD_LOCAL
+  static thread_local std::unordered_map<MKLDNNPoolingSignature,
+                                         MKLDNNAdaptivePoolingFwd, OpHash>
+      pooling_fwds;
+#else
+  static MX_THREAD_LOCAL std::unordered_map<MKLDNNPoolingSignature,
+                                            MKLDNNAdaptivePoolingFwd, OpHash>
+      pooling_fwds;
+#endif
+  bool with_workspace = is_train && true;

Review comment:
       What does this do?




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

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