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_r277167970
########## 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, Review comment: Please add comments for each parameter. ---------------------------------------------------------------- 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
