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

 ##########
 File path: src/operator/nn/mkldnn/mkldnn_rnn_impl.h
 ##########
 @@ -0,0 +1,739 @@
+/*
+ * 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();
+}
+
+//  cached mkldnn memory
+//  first layer wx, wh with next L - 1 layers wx and wh
+//  with L layers hx and cx, src and dst data/iter etc.
+//  it will prepare memory on before and after reorder and concat.
+//  for unidirectional, it will fused as dim like 1  + (L - 1) when I != H.
+//  for bidirectional, it will fused as data + back_data (weight, bias, iter 
etc),
+//  also need to identify first layer and next layers
+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 AdjustGruWeightGateOrder(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;
+  }
+}
+
+template <typename DType>
+void AdjustGruBiasGateOrder(DType* bias,
+                            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* bias_reset = bias;
+  DType* bias_update = bias + H;
+  #pragma omp parallel for num_threads(omp_threads)
+  for (int i = 0; i < H; i++) {
+    DType tmp = bias_update[i];
+    bias_update[i] = bias_reset[i];
+    bias_reset[i] = tmp;
+  }
+}
+// since there is different sematics of MKLDNN's Fused RNN and Mxnet FusedRNN,
 
 Review comment:
   Mxnet to MXNet

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