TaoLv commented on a change in pull request #14713: MKLDNN RNN Inference 
Integration(fp32 LSTM and vRNN with tanh and relu)
URL: https://github.com/apache/incubator-mxnet/pull/14713#discussion_r277168188
 
 

 ##########
 File path: src/operator/nn/mkldnn/mkldnn_rnn_impl.h
 ##########
 @@ -0,0 +1,710 @@
+/*
+ * 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.
+ */
+
+#ifndef MXNET_OPERATOR_NN_MKLDNN_MKLDNN_RNN_IMPL_H_
+#define MXNET_OPERATOR_NN_MKLDNN_MKLDNN_RNN_IMPL_H_
+#if MXNET_USE_MKLDNN == 1
+#include <dmlc/logging.h>
+#include <dmlc/parameter.h>
+#include <mxnet/operator.h>
+#include <mxnet/storage.h>
+#include <algorithm>
+#include <map>
+#include <vector>
+#include <utility>
+#include <string>
+#include "../../math.h"
+#include "../../math_functions-inl.h"
+#include "../../operator_common.h"
+#include "../../rnn_impl.h"
+#include "../../rnn-inl.h"
+#include "mkldnn.hpp"
+#include "./mkldnn_base-inl.h"
+
+namespace mxnet {
+namespace op {
+
+algorithm GetMKLDNNRNNAlgo(int mode,
+                           int* ngates,
+                           int* nstates) {
+  algorithm algo = algorithm::vanilla_rnn;
+  switch (mode) {
+    case rnn_enum::kLstm:
+      *ngates = 4;
+      *nstates = 2;
+      algo = algorithm::vanilla_lstm;
+      break;
+    case rnn_enum::kGru:
+      *ngates = 3;
+      *nstates = 1;
+      algo = algorithm::vanilla_gru;
+      break;
+    case rnn_enum::kRnnRelu:
+    case rnn_enum::kRnnTanh:
+      *ngates = 1;
+      *nstates = 1;
+      algo = algorithm::vanilla_rnn;
+      break;
+    default:
+      LOG(FATAL) << "unsupported RNN mode:" << mode;
+      break;
+  }
+  return algo;
+}
+
+void ConcatData(mkldnn::memory::format src_format,
+                mkldnn::memory::format dst_format,
+                std::vector<mkldnn::memory::dims> srcs_cds,
+                mkldnn::memory::dims dst_cds,
+                mkldnn::memory::data_type mkldnn_dtype,
+                int concat_dimension,
+                std::vector<void*> srcs_data,
+                const mkldnn::memory &dst) {
+  auto cpu_engine = CpuEngine::Get()->get_engine();
+  std::vector<mkldnn::memory::primitive_desc> srcs_pd;
+  std::vector<mkldnn::memory> srcs;
+  for (size_t i = 0; i < srcs_cds.size(); i++) {
+    auto desc = mkldnn::memory::desc(srcs_cds[i], mkldnn_dtype, src_format);
+    auto mpd = mkldnn::memory::primitive_desc(desc, cpu_engine);
+    auto src_memory = mkldnn::memory(mpd, srcs_data[i]);
+    srcs_pd.push_back(mpd);
+    srcs.push_back(src_memory);
+  }
+  std::vector<primitive::at> inputs;
+  for (size_t i = 0; i < srcs_cds.size(); i++) {
+    inputs.push_back(srcs[i]);
+  }
+  auto dst_desc = mkldnn::memory::desc(dst_cds, mkldnn_dtype, dst_format);
+  auto concat_pd = concat::primitive_desc(dst_desc, concat_dimension, srcs_pd);
+  MKLDNNStream::Get()->RegisterPrim(concat(concat_pd, inputs, dst));
+  MKLDNNStream::Get()->Submit();
+}
+
+inline size_t GetMKLDNNRNNCacheMemorySize(int L,
+                                          int D,
+                                          int T,
+                                          int N,
+                                          int I,
+                                          int H,
+                                          int mode) {
+  size_t size = 0;
+  switch (mode) {
+    case rnn_enum::kLstm:
+      size = 2 * (D * (I + H) * 4 * H + (L - 1) * D * (D * H + H) * 4 * H +
+             L * D * 2 * N * H) + T * N * D * H + L * 2 * D * 4 * H + (L + 2) 
* D * 2 * N * H +
+             6 * D * (I + H + 2) * 4 * H + T * N * I * 2;
+      break;
+    case rnn_enum::kGru:
+      size = 2 * (D * (I + H) * 3 * H + (L - 1) * D * (D * H + H) * 3 * H +
+             L * D * 2 * N * H) + T * N * D * H + L * 2 * D * 3 * H + (L + 2) 
* D * 2 * N * H +
+             6 * D * (I + H + 2) * 3 * H + T * N * I * 2;
+      break;
+    case rnn_enum::kRnnRelu:
+    case rnn_enum::kRnnTanh:
+      size = 2 * (D * (I + H) * 1 * H + (L - 1) * D * (D * H + H) * 1 * H +
+             L * D * 2 * N * H) + T * N * D * H + L * 2 * D * 1 * H + (L + 2) 
* D * 2 * N * H +
+             6 * D * (I + H + 2) * 1 * H + T * N * I * 2;
+      break;
+    default:
+      LOG(FATAL) << "unknown RNN mode " << mode;
+      break;
+  }
+  return size;
+}
+
+template <typename DType>
+void AdjustGruGateOrder(DType* weight,
+                        const int I,
+                        const int H) {
+  // mxnet gru gate order is reset, update and new gates
+  // mkldnn gru gate order is update, reset and new gates
+  const int omp_threads = 
mxnet::engine::OpenMP::Get()->GetRecommendedOMPThreadCount();
+  DType* weight_reset = weight;
+  DType* weight_update = weight + I * H;
+  #pragma omp parallel for num_threads(omp_threads)
+  for (int i = 0; i < I * H; i++) {
+    DType tmp = weight_update[i];
+    weight_update[i] = weight_reset[i];
+    weight_reset[i] = tmp;
+  }
+}
+// since there is different sematics of MKLDNN's Fused RNN and Mxnet FusedRNN,
+// bidirectional will be fused layer by layer,
+// unidirectional will be done by fused 1 + fused (L - 1) layers or fused L 
layers(when I = H)
+
+template <typename DType>
+void MKLDNNRNNForwardSingleLayerBi(bool state_outputs,
+                                   const int T,
+                                   const int N,
+                                   const int I,
+                                   const int H,
+                                   DType* x_ptr,
+                                   mkldnn::memory user_src_layer_memory,
+                                   DType* hx_ptr,
+                                   DType* cx_ptr,
+                                   DType* w_ptr,
+                                   DType* b_ptr,
+                                   DType* y_ptr,
+                                   DType* hy_ptr,
+                                   DType* cy_ptr,
+                                   std::vector<mkldnn::memory> 
*concat_weight_memory,
+                                   std::vector<mkldnn::memory> 
*concat_iter_memory,
+                                   std::vector<mkldnn::memory> *x_memory,
+                                   std::vector<mkldnn::memory> *hcx_memory,
+                                   std::vector<mkldnn::memory> *wx_memory,
+                                   std::vector<mkldnn::memory> *wh_memory,
+                                   std::vector<mkldnn::memory> *bias_memory,
+                                   std::vector<mkldnn::memory> *y_memory,
+                                   std::vector<mkldnn::memory> *hcy_memory,
+                                   std::vector<primitive> *rnn_forward_prim,
+                                   int layer_index,
+                                   bool *has_cache,
+                                   int lvalue,
+                                   int dtype,
+                                   bool is_train,
+                                   int mode) {
+  int ngates = 0, nstates = 0;
+  algorithm nalgorithm = GetMKLDNNRNNAlgo(mode, &ngates, &nstates);
+  mkldnn::memory::data_type mkldnn_dtype = get_mkldnn_type(dtype);
+  const int single_cell_size = N * H;
+  const int single_b_size = ngates * H;
+  DType* wx = w_ptr;  //  ngates * H, I
+  DType* wh = w_ptr + I * H * ngates;  //  ngates * H, H
+  DType* back_wx = w_ptr + ngates * H * (I + H);
+  DType* back_wh = back_wx + I * H * ngates;
+  DType* bx = b_ptr;
+  DType* bh = b_ptr + H * ngates;
+  DType* back_bx = b_ptr + single_b_size * 2;
+  DType* back_bh = back_bx + H * ngates;
+  const int omp_threads = 
mxnet::engine::OpenMP::Get()->GetRecommendedOMPThreadCount();
+  auto cpu_engine = CpuEngine::Get()->get_engine();
+  auto null_memory_ = null_memory(cpu_engine);
+  int offset1 = 0, offset2 = 0;
+  bool cached = *has_cache;
+  mkldnn::memory::dims src_layer_tz = {T, N, I};
+  mkldnn::memory::dims dst_layer_tz = {T, N, 2 * H};
+  mkldnn::memory::dims weights_layer_tz = {1, 2, I, ngates, H};  //  ldigo
+  mkldnn::memory::dims weights_layer_r_tz = {1, 1, I, ngates, H};  //  ldigo 
for reorder
+  mkldnn::memory::dims weights_iter_tz = {1, 2, H, ngates, H};  //  ldigo
+  mkldnn::memory::dims weights_iter_r_tz = {1, 1, H, ngates, H};  //  ldigo 
for reorder
+  mkldnn::memory::dims bias_tz = {1, 2, ngates, H};
+  mkldnn::memory::dims src_iter_tz = {1, 2, nstates, N, H};  //  ldsnc
+  mkldnn::memory::dims dst_iter_tz = {1, 2, nstates, N, H};  //  ldsnc
+
+  std::vector<float> weights_scales(ngates * H);
 
 Review comment:
   Seems it's not used?

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


With regards,
Apache Git Services

Reply via email to