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



##########
File path: src/operator/contrib/adaptive_avg_pooling-inl.h
##########
@@ -156,5 +161,13 @@ MSHADOW_XINLINE int get_stride(Tensor<xpu, Dim, DType> 
tensor, int idx) {
 
 }  // namespace op
 }  // namespace mxnet
-
+namespace std {
+template <>
+struct hash<mxnet::op::AdaptiveAvgPoolParam> {

Review comment:
       Maybe it would be worth to comment why here we returning just 0? If I 
understand correctly only param of this OP determines output size and shape is 
already part of NDArray passed to AddSign function 

##########
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,
+                                    const NDArray &out_data) {
+  for (int64_t idx = 2; idx < in_data.shape().ndim(); ++idx) {
+    const int s1 = in_data.shape()[idx];
+    const int s2 = out_data.shape()[idx];
+    if (s2 == 0) {
+      return false;
+    }
+    if (s1 % s2 != 0) {
+      return false;
+    }
+  }
+  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];
+
+  const int strides_1 = floor((IH << 1) / OH) - floor(IH / OH);
+  const int strides_2 = floor((IW << 1) / OW) - floor(IW / OW);
+  const int kernel_1 = ceil((IH << 1) / OH) - floor(IH / OH);
+  const int kernel_2 = ceil((IW << 1) / OW) - floor(IW / OW);
+  const int pad_l_top = (strides_1 * (OH - 1) + kernel_1 - IH) / 2;
+  const int pad_l_left = (strides_2 * (OW - 1) + kernel_2 - IW) / 2;
+
+  return pad_l_top == 0 && pad_l_left == 0;
+}
+
+
+void AdaptiveAvgPoolComputeExCPU(const nnvm::NodeAttrs &attrs,
+                                 const OpContext &ctx,
+                                 const std::vector<NDArray> &inputs,
+                                 const std::vector<OpReqType> &req,
+                                 const std::vector<NDArray> &outputs) {
+  CHECK_EQ(inputs.size(), 1U);
+  CHECK_EQ(outputs.size(), 1U);
+  /*
+  DNNL doesn't support adaptive pooling. 
+  Fallback is needed when padding is not equal 0;
+  */
+  if (SupportMKLDNN(inputs[0]) &&
+      CanMKLDNNSupportAveragePooling(inputs[0], outputs[0])) {
+    const AdaptiveAvgPoolParam &param =
+        nnvm::get<AdaptiveAvgPoolParam>(attrs.parsed);
+    const NDArray *workspace = nullptr;
+    MKLDNN_OPCHECK_INIT(false, 1, inputs, outputs);
+    MKLDNNAdaptivePoolingCompute(ctx, param, inputs[0], req[0], outputs[0],
+                                 workspace);
+    return;
+  }
+  FallBackCompute(AdaptiveAvgPoolOpForward<cpu>, attrs, ctx, inputs, req,
+                  outputs);
+}
+#endif
+
+
+inline static bool AdaptivePoolingStorageType(const nnvm::NodeAttrs &attrs,
+                                              const int dev_mask,
+                                              DispatchMode *dispatch_mode,
+                                              std::vector<int> *in_attrs,
+                                              std::vector<int> *out_attrs) {
+  CHECK_EQ(in_attrs->size(), 1);
+  bool dispatched = false;
+#if MXNET_USE_MKLDNN == 1
+  if (!dispatched) {
+    dispatched = MKLDNNStorageType(attrs, dev_mask, true, dispatch_mode,
+                                   in_attrs, out_attrs);
+  }
+  if (!MKLDNNEnvSet()) {

Review comment:
       MKLDNNEnvSet is already called in MKLDNNStorageType, so probably there 
is no need to second check 

##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling.cc
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.cc
+ * \brief
+ * \author Mateusz Ozga
+*/
+
+#if MXNET_USE_MKLDNN == 1
+
+#include "./mkldnn_adaptive_pooling-inl.h"
+
+namespace mxnet {
+namespace op {
+void MKLDNNAdaptivePoolingFwd::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) {
+  const auto src_md = input.GetMKLDNNData()->get_desc();
+  const auto dst_md = GetMemDesc(output);
+  const mkldnn::engine engine = CpuEngine::Get()->get_engine();
+
+  if (alg_kind != mkldnn::algorithm::pooling_avg &&
+      alg_kind != mkldnn::algorithm::pooling_avg_include_padding &&
+      alg_kind != mkldnn::algorithm::pooling_avg_exclude_padding) {
+    LOG(FATAL) << "MKLDNN Adaptive Pooling: algorithm is not supported";
+  }
+
+  mkldnn::prop_kind prop = mkldnn::prop_kind::forward_scoring;
+  if (is_train && alg_kind != mkldnn::algorithm::pooling_avg) {
+    prop = mkldnn::prop_kind::forward_training;
+  }
+  if (is_train && prop == mkldnn::prop_kind::forward_scoring) {
+    LOG(INFO) << "MKLDNN Pooling: training with prop_kind is forward_scoring";
+  }
+
+  const auto fwd_desc = mkldnn::pooling_forward::desc(
+      prop, alg_kind, src_md, dst_md, strides, kernel, pad_l, pad_r);
+  this->fwd_pd_.reset(
+      new mkldnn::pooling_forward::primitive_desc(fwd_desc, engine));
+  this->fwd_.reset(new mkldnn::pooling_forward(*(this->fwd_pd_)));
+}
+
+void MKLDNNAdaptivePoolingFwd::Execute(const NDArray &input,
+                                       const OpReqType req,
+                                       const NDArray &output,
+                                       const NDArray *workspace) {
+  NDArray in_buffer = input;
+  if (input.IsView() && input.IsMKLDNNData()) {
+    in_buffer = input.Reorder2Default();
+  }
+
+  auto input_mem = in_buffer.GetMKLDNNData();
+  auto output_mem_t = CreateMKLDNNMem(output, this->fwd_pd_->dst_desc(), req);
+
+  mkldnn_args_map_t args = {{MKLDNN_ARG_SRC, *input_mem},
+                            {MKLDNN_ARG_DST, *(output_mem_t.second)}};
+
+  if (this->with_workspace_) {
+    auto engine = CpuEngine::Get()->get_engine();
+    if (workspace == nullptr) {
+      LOG(FATAL) << "MKLDNN Average Pooling: incorrect worskapce input";
+    }
+    auto ws = std::make_shared<mkldnn::memory>(
+        (*(this->fwd_pd_)).workspace_desc(), engine,

Review comment:
       Can't it be just this->fwd_pd_->workspace_desc() ?

##########
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,
+                                    const NDArray &out_data) {
+  for (int64_t idx = 2; idx < in_data.shape().ndim(); ++idx) {
+    const int s1 = in_data.shape()[idx];
+    const int s2 = out_data.shape()[idx];
+    if (s2 == 0) {
+      return false;
+    }
+    if (s1 % s2 != 0) {
+      return false;
+    }
+  }
+  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];
+
+  const int strides_1 = floor((IH << 1) / OH) - floor(IH / OH);
+  const int strides_2 = floor((IW << 1) / OW) - floor(IW / OW);
+  const int kernel_1 = ceil((IH << 1) / OH) - floor(IH / OH);
+  const int kernel_2 = ceil((IW << 1) / OW) - floor(IW / OW);
+  const int pad_l_top = (strides_1 * (OH - 1) + kernel_1 - IH) / 2;
+  const int pad_l_left = (strides_2 * (OW - 1) + kernel_2 - IW) / 2;
+
+  return pad_l_top == 0 && pad_l_left == 0;
+}
+
+
+void AdaptiveAvgPoolComputeExCPU(const nnvm::NodeAttrs &attrs,
+                                 const OpContext &ctx,
+                                 const std::vector<NDArray> &inputs,
+                                 const std::vector<OpReqType> &req,
+                                 const std::vector<NDArray> &outputs) {
+  CHECK_EQ(inputs.size(), 1U);
+  CHECK_EQ(outputs.size(), 1U);
+  /*
+  DNNL doesn't support adaptive pooling. 

Review comment:
       oneDNN ?




----------------------------------------------------------------
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:
[email protected]


Reply via email to