From 4746121d438c72d0287a6ff7edb193a2da68029d Mon Sep 17 00:00:00 2001 From: Francesco Gatti Date: Sat, 15 Feb 2020 20:37:08 +0100 Subject: [PATCH] LSTM params --- include/tkDNN/Layer.h | 5 ++- src/LSTM.cpp | 94 +++++++++++++++++++++++++++++++++------ tests/imuodom/imuodom.cpp | 20 ++++++--- tests/imuodom/infer.py | 1 + tests/weights_exporter.py | 16 +++++-- 5 files changed, 111 insertions(+), 25 deletions(-) diff --git a/include/tkDNN/Layer.h b/include/tkDNN/Layer.h index 240d9a2..319df6a 100644 --- a/include/tkDNN/Layer.h +++ b/include/tkDNN/Layer.h @@ -212,6 +212,7 @@ protected: https://github.com/jiangnanhugo/seq2seq_cuda/blob/e4dbdcfa0517c972bfd4beea9f11a5233954093c/src/rnn.cpp https://github.com/Jeffery-Song/mxnet-test/blob/aab666faad44011f7a67b527b5f6c960367d0422/src/operator/cudnn_rnn-inl.h https://stackoverflow.com/a/38737941 + https://colah.github.io/posts/2015-08-Understanding-LSTMs/ PARAMS (numlayers*2): layer0: @@ -236,7 +237,7 @@ public: virtual dnnType* infer(dataDim_t &dim, dnnType* srcData); - const bool bidirectional = 1; /**> is the net bidir */ + const bool bidirectional = false; /**> is the net bidir */ bool returnSeq = false; /**> if false return only the result of last timestep */ int stateSize = 0; /**> number of hidden states */ int seqLen = 0; /**> number of timesteps */ @@ -254,9 +255,11 @@ protected: cudnnTensorDescriptor_t hx_desc_, cx_desc_; cudnnTensorDescriptor_t hy_desc_, cy_desc_; dnnType *hx_ptr, *cx_ptr, *hy_ptr, *cy_ptr; + int stateDataDim; cudnnFilterDescriptor_t w_desc_; dnnType *w_ptr; + dnnType *w_h; }; diff --git a/src/LSTM.cpp b/src/LSTM.cpp index c31bccf..ce2a9f5 100644 --- a/src/LSTM.cpp +++ b/src/LSTM.cpp @@ -66,10 +66,12 @@ LSTM::LSTM( Network *net, int hiddensize, bool returnSeq, std::string fname_weig checkCUDNN(cudnnSetTensorNdDescriptor(hy_desc_, net->dataType, 3, dimA, strideA)); checkCUDNN(cudnnSetTensorNdDescriptor(cy_desc_, net->dataType, 3, dimA, strideA)); // allocate dnnType *hx_ptr, *cx_ptr, *hy_ptr, *cy_ptr; - checkCuda( cudaMalloc(&hx_ptr, dimA[0]*dimA[1]*dimA[2]*sizeof(dnnType)) ); - checkCuda( cudaMalloc(&cx_ptr, dimA[0]*dimA[1]*dimA[2]*sizeof(dnnType)) ); - checkCuda( cudaMalloc(&hy_ptr, dimA[0]*dimA[1]*dimA[2]*sizeof(dnnType)) ); - checkCuda( cudaMalloc(&cy_ptr, dimA[0]*dimA[1]*dimA[2]*sizeof(dnnType)) ); + stateDataDim = dimA[0]*dimA[1]*dimA[2]; + checkCuda( cudaMalloc(&hx_ptr, stateDataDim*sizeof(dnnType)) ); + checkCuda( cudaMalloc(&cx_ptr, stateDataDim*sizeof(dnnType)) ); + checkCuda( cudaMalloc(&hy_ptr, stateDataDim*sizeof(dnnType)) ); + checkCuda( cudaMalloc(&cy_ptr, stateDataDim*sizeof(dnnType)) ); + // Create Dropout descriptors // TODO: ??? IS IT NECESSARY ??? @@ -89,7 +91,7 @@ LSTM::LSTM( Network *net, int hiddensize, bool returnSeq, std::string fname_weig checkCUDNN(cudnnSetRNNDescriptor(net->cudnnHandle, rnnDesc, stateSize, numLayers, dropoutDesc, cudnnRNNInputMode_t::CUDNN_LINEAR_INPUT, - cudnnDirectionMode_t::CUDNN_BIDIRECTIONAL, + (bidirectional ? cudnnDirectionMode_t::CUDNN_BIDIRECTIONAL : cudnnDirectionMode_t::CUDNN_UNIDIRECTIONAL), cudnnRNNMode_t::CUDNN_LSTM, cudnnRNNAlgo_t::CUDNN_RNN_ALGO_STANDARD, net->dataType)); @@ -115,22 +117,81 @@ LSTM::LSTM( Network *net, int hiddensize, bool returnSeq, std::string fname_weig dim_w[0] = cudnn_params; checkCUDNN(cudnnSetFilterNdDescriptor(w_desc_, net->dataType, net->tensorFormat, 3, dim_w)); - // allocate params dnnType *w_ptr; - checkCuda( cudaMalloc(&w_ptr, cudnn_params*sizeof(dnnType)) ); - + // load params + readBinaryFile(fname_weights, cudnn_params, &w_h, &w_ptr); //allocate data for infer result - int dstDim = input_dim.n * stateSize*2 * input_dim.h * input_dim.w; + int dstDim = input_dim.n * stateSize*(bidirectional ? 2 : 1) * input_dim.h * input_dim.w; checkCuda( cudaMalloc(&dstData, dstDim*sizeof(dnnType)) ); // set output dim output_dim = input_dim; - output_dim.c = stateSize*2; + output_dim.c = stateSize*(bidirectional ? 2 : 1); if(!returnSeq) { output_dim.h = 1; output_dim.w = 1; } + + + + + // Query weight layout + cudnnFilterDescriptor_t m_desc; + checkCUDNN(cudnnCreateFilterDescriptor(&m_desc)); + dnnType *p; + int n = 8; // lstm layers + + printCenteredTitle("WEIGHTS", '=', 20); + for (int i = 0; i < numLayers*(bidirectional?2:1); ++i) { + for (int j = 0; j < n; ++j) { + + checkCUDNN(cudnnGetRNNLinLayerMatrixParams(net->cudnnHandle, rnnDesc, + i, x_desc_vec_[0], w_desc_, 0, j, m_desc, (void**)&p)); + + std::cout << "ptr: " << ((int64_t)(p - NULL))/sizeof(dnnType)<<"\n"; + + cudnnDataType_t t; + cudnnTensorFormat_t f; + int ndim = 5; + int dims[5] = {0, 0, 0, 0, 0}; + checkCUDNN(cudnnGetFilterNdDescriptor(m_desc, ndim, &t, &f, &ndim, &dims[0])); + std::cout << "(layer, linlayer): " << i << " " << j << "\n"; + + int tot = 1; + for (int i = 0; i < ndim; ++i) { + std::cout << dims[i] << " "; + tot *= dims[i]; + } + std::cout<<"\t-> "<