piiswrong commented on a change in pull request #8302: Refactor operators & 
MKLDNN
URL: https://github.com/apache/incubator-mxnet/pull/8302#discussion_r156269494
 
 

 ##########
 File path: src/operator/nn/mkldnn/mkldnn_act-inl.h
 ##########
 @@ -0,0 +1,133 @@
+/*
+ * 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_act-inl.h
+ * \brief
+ * \author Da Zheng
+*/
+
+#ifndef MXNET_OPERATOR_NN_MKLDNN_MKLDNN_ACT_INL_H_
+#define MXNET_OPERATOR_NN_MKLDNN_MKLDNN_ACT_INL_H_
+
+
+#include <dmlc/logging.h>
+#include <dmlc/parameter.h>
+#include <mxnet/operator.h>
+#include <algorithm>
+#include <map>
+#include <vector>
+#include <string>
+#include <utility>
+#include "../../operator_common.h"
+#include "./mkldnn_base-inl.h"
+
+#if MXNET_USE_MKLDNN == 1
+
+#include <mkldnn.hpp>
+
+namespace mxnet {
+namespace op {
+
+static inline bool SupportMKLDNNAct(const ActivationParam& param) {
+  // We don't include tanh for now. It seems MKLDNN tanh has some precision
+  // problems.
+  return param.act_type == activation::kReLU
+      || param.act_type == activation::kSigmoid
+      || param.act_type == activation::kSoftReLU;
+}
+
+static inline mkldnn::algorithm GetMKLDNNActAlgo(const ActivationParam& param) 
{
+  switch (param.act_type) {
+    case activation::kReLU:
+      return mkldnn::algorithm::eltwise_relu;
+    case activation::kSigmoid:
+      return mkldnn::algorithm::eltwise_logistic;
+    case activation::kTanh:
+      return mkldnn::algorithm::eltwise_tanh;
+    case activation::kSoftReLU:
+      return mkldnn::algorithm::eltwise_soft_relu;
+    default:
+      LOG(FATAL) << "unknown activation type";
+      return mkldnn::algorithm::eltwise_relu;
+  }
+}
+
+template<typename Dtype>
+void MKLDNNAct_Forward(const OpContext &ctx, const ActivationParam& param,
+                       const NDArray &in_data, const OpReqType &req,
+                       const NDArray &out_data) {
+  std::shared_ptr<const mkldnn::memory> input_mem = in_data.GetMKLDNNData();
+  mkldnn::memory::primitive_desc data_mpd = input_mem->get_primitive_desc();
+  mkldnn::memory::desc data_md = data_mpd.desc();
+  auto cpu_engine = data_mpd.get_engine();
+  Dtype alpha = 0;
+
+  auto alg = GetMKLDNNActAlgo(param);
+  mkldnn::eltwise_forward::desc desc = ctx.is_train
+      ? mkldnn::eltwise_forward::desc(mkldnn::prop_kind::forward_training,
+                                      alg, data_md, alpha)
+      : mkldnn::eltwise_forward::desc(mkldnn::prop_kind::forward_scoring,
+                                      alg, data_md, alpha);
+  mkldnn::eltwise_forward::primitive_desc pdesc(desc, cpu_engine);
+
+  std::shared_ptr<const mkldnn::memory> output_memory
+    = const_cast<NDArray 
&>(out_data).CreateMKLDNNData(pdesc.dst_primitive_desc());
 
 Review comment:
   note that NDArray is a pointer type, so you can modify Chunk even if 
CreateMKLDNNData is const

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to