Refactoring and modularization

Signed-off-by: Micaela Verucchi <micaela.verucchi@unimore.it>
This commit is contained in:
Micaela Verucchi
2019-10-04 11:12:01 +02:00
parent bb7d382d96
commit 35787cc771
25 changed files with 1708 additions and 1560 deletions
+27 -21
View File
@@ -2,10 +2,13 @@
#include "Layer.h"
namespace tk { namespace dnn {
namespace tk
{
namespace dnn
{
Dense::Dense(Network *net, int out_ch, const char* fname_weights) :
LayerWgs(net, net->getOutputDim().tot(), out_ch, 1, 1, 1, fname_weights) {
Dense::Dense(Network *net, int out_ch, const char *fname_weights) : LayerWgs(net, net->getOutputDim().tot(), out_ch, 1, 1, 1, fname_weights)
{
output_dim.n = 1;
output_dim.c = out_ch;
@@ -14,19 +17,21 @@ Dense::Dense(Network *net, int out_ch, const char* fname_weights) :
output_dim.l = 1;
//allocate data for infer result
checkCuda( cudaMalloc(&dstData, output_dim.tot()*sizeof(dnnType)) );
checkCuda(cudaMalloc(&dstData, output_dim.tot() * sizeof(dnnType)));
}
Dense::~Dense() {
Dense::~Dense()
{
checkCuda( cudaFree(dstData) );
checkCuda(cudaFree(dstData));
}
dnnType* Dense::infer(dataDim_t &dim, dnnType* srcData) {
dnnType *Dense::infer(dataDim_t &dim, dnnType *srcData)
{
if (dim.n != 1)
FatalError("Not Implemented");
FatalError("Not Implemented");
int dim_x = dim.tot();
int dim_y = output_dim.tot();
@@ -35,18 +40,18 @@ dnnType* Dense::infer(dataDim_t &dim, dnnType* srcData) {
dnnType alpha = dnnType(1), beta = dnnType(1);
// place bias into dstData
checkCuda( cudaMemcpy(dstData, bias_d, dim_y*sizeof(dnnType), 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) );
checkCuda(cudaMemcpy(dstData, bias_d, dim_y * sizeof(dnnType), cudaMemcpyDeviceToDevice));
//update data dimensions
//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;
@@ -55,4 +60,5 @@ dnnType* Dense::infer(dataDim_t &dim, dnnType* srcData) {
return dstData;
}
}}
} // namespace dnn
} // namespace tk