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/09/24 09:29:37 UTC

[GitHub] [incubator-mxnet] anko-intel commented on a change in pull request #20567: [Performance] Add oneDNN support for temperature parameter in Softmax

anko-intel commented on a change in pull request #20567:
URL: https://github.com/apache/incubator-mxnet/pull/20567#discussion_r715461528



##########
File path: src/operator/nn/mkldnn/mkldnn_softmax-inl.h
##########
@@ -0,0 +1,154 @@
+/*
+ * 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_softmax-inl.h
+ * Naming convention:
+ *                  ________
+ *                 |Softmax|
+ *  data  -------->|  FWD  |---> out
+ *                 |_______|
+ *                 ________
+ *                |Softmax|<--- out
+ *  data_grad <---|  BWD  |
+ *                |_______|<--- out_grad
+ */
+
+#ifndef MXNET_OPERATOR_NN_MKLDNN_MKLDNN_SOFTMAX_INL_H_
+#define MXNET_OPERATOR_NN_MKLDNN_MKLDNN_SOFTMAX_INL_H_
+
+#if MXNET_USE_ONEDNN == 1
+#include <vector>
+
+#include "./mkldnn_base-inl.h"
+#include "./mkldnn_ops-inl.h"
+
+#include "../softmax-inl.h"
+
+namespace mxnet {
+namespace op {
+
+using softmax_fwd_t    = mkldnn::softmax_forward;
+using softmax_fwd_pd_t = mkldnn::softmax_forward::primitive_desc;
+
+using softmax_bwd_t    = mkldnn::softmax_backward;
+using softmax_bwd_pd_t = mkldnn::softmax_backward::primitive_desc;
+
+using linear_t    = mkldnn::eltwise_forward;
+using linear_pd_t = mkldnn::eltwise_forward::primitive_desc;
+
+class MKLDNNSoftmaxFwd {
+ public:
+  struct Tensors {
+    Tensors(const NDArray& data, const NDArray& out);
+
+    const NDArray& data;
+    const NDArray& out;
+  };
+
+  static MKLDNNSoftmaxFwd& GetCached(const SoftmaxParam& param,
+                                     const Tensors& tensors,
+                                     const bool is_train);
+
+  static softmax_fwd_pd_t GetSoftmaxFwdPd(const mkldnn::memory& input_mem,
+                                          const int axis,
+                                          const bool is_train);
+
+  static linear_pd_t GetTemperaturePd(const mkldnn::memory& input_mem, const float temperature);
+
+  MKLDNNSoftmaxFwd(const SoftmaxParam& param, const Tensors& tensors, const bool is_train);
+  void Execute(const Tensors& tensors) const;
+
+ private:
+  std::shared_ptr<softmax_fwd_pd_t> softmax_pd;
+  std::shared_ptr<softmax_fwd_t> softmax_fwd;
+  std::shared_ptr<linear_pd_t> temperature_pd;
+  std::shared_ptr<linear_t> temperature_fwd;
+};
+
+MKLDNNSoftmaxFwd::Tensors::Tensors(const NDArray& data, const NDArray& output)
+    : data(data), out(output) {}
+
+MKLDNNSoftmaxFwd::MKLDNNSoftmaxFwd(const SoftmaxParam& param,
+                                   const Tensors& tensors,
+                                   const bool is_train) {
+  float temperature = param.temperature.has_value() ? param.temperature.value() : 1.0f;
+  int axis          = CheckAxis(param.axis, tensors.data.shape().ndim());

Review comment:
       GetNormalizedAxis is a good proposition for the name. Also it could be GetValidatedAxis (?)
   But I agree with @bgawrych - for now it should stay as is. We can open separate PR for change the name(s).




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