From 0767df43a290649d9c64264b11d0b5b0df97c523 Mon Sep 17 00:00:00 2001 From: Francesco Gatti Date: Wed, 28 Jun 2017 01:14:39 +0200 Subject: [PATCH] initial commit --- .gitignore | 3 ++ CMakeLists.txt | 13 ++++++ include/Layer.h | 104 +++++++++++++++++++++++++++++++++++++++++++++ include/Network.h | 21 +++++++++ include/utils.h | 69 ++++++++++++++++++++++++++++++ src/Activation.cpp | 46 ++++++++++++++++++++ src/Dense.cpp | 54 +++++++++++++++++++++++ src/Layer.cpp | 22 ++++++++++ src/LayerWgs.cpp | 29 +++++++++++++ src/Network.cpp | 23 ++++++++++ src/utils.cpp | 45 ++++++++++++++++++++ tests/test.cpp | 12 ++++++ 12 files changed, 441 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 include/Layer.h create mode 100644 include/Network.h create mode 100644 include/utils.h create mode 100644 src/Activation.cpp create mode 100644 src/Dense.cpp create mode 100644 src/Layer.cpp create mode 100644 src/LayerWgs.cpp create mode 100644 src/Network.cpp create mode 100644 src/utils.cpp create mode 100644 tests/test.cpp diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3b47b1f --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +*~ +build/ +.vscode/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..d5fef62 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 2.8) + +project (tkDNN) + +find_package(CUDA QUIET REQUIRED) + + +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include ${CUDA_INCLUDE_DIRS}) +add_library(tkDNN SHARED src/Layer.cpp src/LayerWgs.cpp src/Dense.cpp src/Activation.cpp src/Network.cpp src/utils.cpp) + +add_executable(tkDNNtest tests/test.cpp) +message(${CUDA_LIBRARIES}) +target_link_libraries(tkDNNtest tkDNN ${CUDA_LIBRARIES} ${CUDA_CUBLAS_LIBRARIES} /usr/local/cuda-8.0/cudnn/libcudnn.so) \ No newline at end of file diff --git a/include/Layer.h b/include/Layer.h new file mode 100644 index 0000000..9eab1d9 --- /dev/null +++ b/include/Layer.h @@ -0,0 +1,104 @@ +#ifndef LAYER_H +#define LAYER_H + +#include +#include "utils.h" +#include "Network.h" + +namespace tkDNN { + +/** + Data rapresentation beetween layers +*/ +struct dataDim_t { + + int n, c, h, w, l; + + dataDim_t() : n(1), c(1), h(1), w(1), l(1) {}; + + dataDim_t(int _n, int _c, int _h, int _w, int _l = 1) : + n(_n), c(_c), h(_h), w(_w), l(_l) {}; + + void print() { + std::cout<<"Data dim: "< +#include +#include +#include +#include + +#include "cuda.h" +#include "cuda_runtime_api.h" +#include +#include + +#define value_type float + + +#define TIMER_START timespec start, end; \ + clock_gettime(CLOCK_MONOTONIC, &start); + +#define TIMER_STOP clock_gettime(CLOCK_MONOTONIC, &end); \ + double t_ns = ((double)(end.tv_sec - start.tv_sec) * 1.0e9 + \ + (double)(end.tv_nsec - start.tv_nsec))/1.0e6; \ + std::cout<<"Time:"< + +#include "Layer.h" + +namespace tkDNN { + +Activation::Activation(Network *net, dataDim_t input_dim, cudnnActivationMode_t act_mode) : + Layer(net, input_dim) { + + this->act_mode = act_mode; + checkCuda( cudaMalloc(&dstData, input_dim.tot()*sizeof(value_type)) ); + + checkCUDNN( cudnnSetTensor4dDescriptor(srcTensorDesc, + net->tensorFormat, + net->dataType, + input_dim.n, input_dim.c, + input_dim.h, input_dim.w) ); + checkCUDNN( cudnnSetTensor4dDescriptor(dstTensorDesc, + net->tensorFormat, + net->dataType, + input_dim.n, input_dim.c, + input_dim.h, input_dim.w) ); +} + +Activation::~Activation() { + + checkCuda( cudaFree(dstData) ); +} + +value_type* Activation::infer(dataDim_t &dim, value_type* srcData) { + + value_type alpha = value_type(1); + value_type beta = value_type(0); + checkCUDNN( cudnnActivationForward(net->cudnnHandle, + act_mode, + &alpha, + srcTensorDesc, + srcData, + &beta, + dstTensorDesc, + dstData) ); + + return dstData; +} + +} \ No newline at end of file diff --git a/src/Dense.cpp b/src/Dense.cpp new file mode 100644 index 0000000..7edf888 --- /dev/null +++ b/src/Dense.cpp @@ -0,0 +1,54 @@ +#include + +#include "Layer.h" + +namespace tkDNN { + +Dense::Dense(Network *net, dataDim_t in_dim, + int out_ch, const char* fname_weights, const char* fname_bias) : + LayerWgs(net, in_dim, in_dim.tot(), out_ch, 1, 1, 1, fname_weights, fname_bias) { + + this->out_ch = out_ch; + //allocate data for infer result + checkCuda( cudaMalloc(&dstData, outputs*sizeof(value_type)) ); +} + +Dense::~Dense() { + + checkCuda( cudaFree(dstData) ); +} + +value_type* Dense::infer(dataDim_t &dim, value_type* srcData) { + + if (dim.n != 1) + FatalError("Not Implemented"); + + int dim_x = dim.tot(); + int dim_y = outputs; + + if (dim_x != inputs) + FatalError("Input mismatch"); + + value_type alpha = value_type(1), beta = value_type(1); + // place bias into dstData + checkCuda( cudaMemcpy(dstData, bias_d, dim_y*sizeof(value_type), cudaMemcpyDeviceToDevice) ); + + //do matrix moltiplication + checkERROR( cublasSgemv(net->cublasHandle, CUBLAS_OP_T, + dim_x, dim_y, + &alpha, + data_d, dim_x, + srcData, 1, + &beta, + dstData, 1) ); + + //update data dimensions + dim.h = 1; + dim.w = 1; + dim.l = 1; + dim.c = dim_y; + + return dstData; +} + +} \ No newline at end of file diff --git a/src/Layer.cpp b/src/Layer.cpp new file mode 100644 index 0000000..37999e6 --- /dev/null +++ b/src/Layer.cpp @@ -0,0 +1,22 @@ +#include + +#include "Layer.h" + +namespace tkDNN { + +Layer::Layer(Network *net, dataDim_t in_dim) { + + this->net = net; + this->input_dim = in_dim; + + checkCUDNN( cudnnCreateTensorDescriptor(&srcTensorDesc) ); + checkCUDNN( cudnnCreateTensorDescriptor(&dstTensorDesc) ); +} + +Layer::~Layer() { + + checkCUDNN( cudnnDestroyTensorDescriptor(srcTensorDesc) ); + checkCUDNN( cudnnDestroyTensorDescriptor(dstTensorDesc) ); +} + +} \ No newline at end of file diff --git a/src/LayerWgs.cpp b/src/LayerWgs.cpp new file mode 100644 index 0000000..b4c6c8b --- /dev/null +++ b/src/LayerWgs.cpp @@ -0,0 +1,29 @@ +#include + +#include "Layer.h" + +namespace tkDNN { + +LayerWgs::LayerWgs(Network *net, dataDim_t in_dim, + int inputs, int outputs, int kh, int kw, int kl, + const char* fname_weights, const char* fname_bias) : Layer(net, in_dim) { + + this->inputs = inputs; + this->outputs = outputs; + this->weights_path = std::string(fname_weights); + this->bias_path = std::string(fname_bias); + + std::cout<<"Reading weights: I="< + +#include "Network.h" + +namespace tkDNN { + +Network::Network() { + + std::cout<<"New NETWORK with CUDNN v"< +#include "Layer.h" + +int main() { + + tkDNN::dataDim_t dim(1, 1, 10, 10); + dim.print(); + tkDNN::Network net; + tkDNN::Dense d(&net, dim, 2, "ci", "lol"); + + return 0; +} \ No newline at end of file