akarbown commented on a change in pull request #19896:
URL: https://github.com/apache/incubator-mxnet/pull/19896#discussion_r643227909
##########
File path: src/operator/contrib/adaptive_avg_pooling.cc
##########
@@ -219,6 +295,11 @@ The pooling kernel and stride sizes are automatically
chosen for desired output
.set_attr<FCompute>("FCompute<cpu>", AdaptiveAvgPoolOpForward<cpu>)
.set_attr<nnvm::FGradient>("FGradient",
ElemwiseGradUseNone{"_backward_contrib_AdaptiveAvgPooling2D"})
+.set_attr<FInferStorageType>("FInferStorageType", AdaptivePoolingStorageType)
+#if MXNET_USE_MKLDNN == 1
Review comment:
Please change it to MXNET_USE_ONEDNN.
##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling-inl.h
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2021 by Contributors
+ * \file mkldnn_adaptive_pooling-inl.h
+*/
+#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
Review comment:
Please change it to MXNET_USE_ONEDNN.
##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling-inl.h
##########
@@ -0,0 +1,146 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2021 by Contributors
+ * \file mkldnn_adaptive_pooling-inl.h
+*/
+#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 ¶m, 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;
+ MKLDNNPoolingSignature key(param);
+ key.AddSign(is_train);
+ key.AddSign(with_workspace);
+ key.AddSign(input);
+ key.AddSign(output);
+
+ auto it = pooling_fwds.find(key);
+ if (it == pooling_fwds.end()) {
+ const int kernel_ndims = input.shape().ndim();
+
+ mkldnn::memory::dims kernel(kernel_ndims);
+ mkldnn::memory::dims strides(kernel_ndims);
+ mkldnn::memory::dims pad_l(kernel_ndims);
+ mkldnn::memory::dims pad_r(kernel_ndims);
+
+ updateAdaptivePaddingKernel(&kernel, &strides, &pad_l, &pad_r, input,
+ output);
+ mkldnn::memory::validate_dims(kernel);
+ mkldnn::memory::validate_dims(strides);
+ mkldnn::memory::validate_dims(pad_l);
+ mkldnn::memory::validate_dims(pad_r);
+
+ mkldnn::algorithm kind = mkldnn::algorithm::pooling_avg;
+ MKLDNNAdaptivePoolingFwd fwd(input, output, kernel, kernel, pad_l, pad_r,
+ kind, false, false);
+ it = AddToCache(&pooling_fwds, key, fwd);
+ }
+ return it->second;
+}
+
+template <typename T>
+void MKLDNNAdaptivePoolingCompute(const OpContext &ctx, const T ¶m,
+ const NDArray &in_data, const OpReqType req,
+ const NDArray &out_data,
+ const NDArray *workspace) {
+ auto &fwd = GetPoolingFwd(param, ctx.is_train, in_data, out_data);
+ fwd.Execute(in_data, req, out_data, workspace);
+}
+} // namespace op
+} // namespace mxnet
+#endif // MXNET_USE_MKLDNN == 1
Review comment:
Please change it to MXNET_USE_ONEDNN.
##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling.cc
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2021 by Contributors
+ * \file mkldnn_adaptive_pooling.cc
+*/
+
+#if MXNET_USE_MKLDNN == 1
Review comment:
Please change it to MXNET_USE_ONEDNN.
##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling.cc
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2021 by Contributors
+ * \file mkldnn_adaptive_pooling.cc
+*/
+
+#if MXNET_USE_MKLDNN == 1
+
+#include "./mkldnn_adaptive_pooling-inl.h"
+
+namespace mxnet {
+namespace op {
+void MKLDNNAdaptivePoolingFwd::Init(
Review comment:
Functions: MKLDNNAdaptivePoolingFwd::Init() and
MKLDNNAdaptivePoolingFwd::Execute() are more or less the same as the one that
are in the mkldnn_poolling.cc file (MKLDNNPoolingFwd::Init() &&
MKLDNNPoolingFwd::Execute()). What do you think about combining them together?
So that the code won't be duplicated.
##########
File path: src/operator/nn/mkldnn/mkldnn_adaptive_pooling.cc
##########
@@ -0,0 +1,97 @@
+/*
+ * 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.
+ */
+
+/*!
+ * Copyright (c) 2021 by Contributors
+ * \file mkldnn_adaptive_pooling.cc
+*/
+
+#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,
+ workspace->GetMKLDNNData()->get_data_handle());
+ args[MKLDNN_ARG_WORKSPACE] = *ws;
+ }
+ if (this->fwd_) {
+ MKLDNNStream::Get()->RegisterPrimArgs(*(this->fwd_), args);
+ CommitOutput(output, output_mem_t);
+ MKLDNNStream::Get()->Submit();
+ } else {
+ LOG(FATAL) << "MKLDNN Pooling: forward primitive is nullptr";
+ }
+}
+
+} // namespace op
+} // namespace mxnet
+#endif // MXNET_USE_MKLDNN == 1
Review comment:
Please change it to MXNET_USE_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]