You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
4.6 KiB
85 lines
4.6 KiB
#include "cuda.h"
|
|
#include "../layers/localLayer.h"
|
|
|
|
void gpuCUDA::forward_layer(localLayer *layer, cortexSTATE state){
|
|
uint32_t out_h = (layer->m_nPad == 0) ? ((layer->m_nIn_Height - layer->m_nSize) / (layer->m_nStride + 1)) : ((layer->m_nIn_Height - 1) / (layer->m_nStride + 1));
|
|
uint32_t out_w = (layer->m_nPad == 0) ? ((layer->m_nIn_Width - layer->m_nSize) / (layer->m_nStride + 1)) : ((layer->m_nIn_Width - 1) / (layer->m_nStride + 1));
|
|
uint32_t locations = out_h * out_w;
|
|
for (uint32_t i = 0; i < layer->m_nBatch; ++i){
|
|
makeCopy(layer->m_nOutput, layer->m_pGpuBias, 1, layer->m_pGpuOutput + i*layer->m_nOutput, 1);
|
|
};
|
|
for (uint32_t i = 0; i < layer->m_nBatch; ++i){
|
|
float *input = state.input + i*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nIn_Channels;
|
|
im2col(input, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, layer->m_pGpuColImage);
|
|
float *output = layer->m_pGpuOutput + i*layer->m_nOutput;
|
|
for (uint32_t j = 0; j < locations; ++j){
|
|
float *a = layer->m_pGpuWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nOut_Channels;
|
|
float *b = layer->m_pGpuColImage + j;
|
|
float *c = output + j;
|
|
int m = layer->m_nOut_Channels;
|
|
int n = 1;
|
|
int k = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
|
|
gemm(0, 0, m, n, k, 1, a, k, b, locations, 1, c, locations);
|
|
};
|
|
};
|
|
makeActivatedArray(layer->m_pGpuOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation);
|
|
};
|
|
|
|
void gpuCUDA::backward_layer(localLayer *layer, cortexSTATE state){
|
|
uint32_t locations = layer->m_nOut_Width*layer->m_nOut_Height;
|
|
makeGradientArray(layer->m_pGpuOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation, layer->m_pGpuDelta);
|
|
for (uint32_t i = 0; i < layer->m_nBatch; ++i){
|
|
calcAxpy(layer->m_nOutput, 1, layer->m_pGpuDelta + i*layer->m_nOutput, 1, layer->m_pGpuBiasUpdate, 1);
|
|
};
|
|
for (uint32_t i = 0; i < layer->m_nBatch; ++i){
|
|
float *input = state.input + i*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nIn_Channels;
|
|
im2col(input, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, layer->m_pGpuColImage);
|
|
for (uint32_t j = 0; j < locations; ++j){
|
|
float *a = layer->m_pGpuDelta + i*layer->m_nOutput + j;
|
|
float *b = layer->m_pGpuColImage + j;
|
|
float *c = layer->m_pGpuWeightUpdate + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nOut_Channels;
|
|
int m = layer->m_nOut_Channels;
|
|
int n = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
|
|
int k = 1;
|
|
gemm(0, 1, m, n, k, 1, a, locations, b, locations, 1, c, n);
|
|
};
|
|
if (state.delta){
|
|
for (uint32_t j = 0; j < locations; ++j){
|
|
float *a = layer->m_pGpuWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nOut_Channels;
|
|
float *b = layer->m_pGpuDelta + i*layer->m_nOutput + j;
|
|
float *c = layer->m_pGpuColImage + j;
|
|
int m = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
|
|
int n = 1;
|
|
int k = layer->m_nOut_Channels;
|
|
gemm(1, 0, m, n, k, 1, a, m, b, locations, 0, c, locations);
|
|
};
|
|
col2im(layer->m_pGpuColImage, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.delta + i*layer->m_nIn_Channels*layer->m_nIn_Height*layer->m_nIn_Width);
|
|
};
|
|
};
|
|
};
|
|
|
|
void gpuCUDA::update_layer(localLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
|
|
int locations = layer->m_nOut_Width*layer->m_nOut_Height;
|
|
int size = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN*locations;
|
|
calcAxpy(layer->m_nOutput, learning_rate / batch, layer->m_pGpuBiasUpdate, 1, layer->m_pGpuBias, 1);
|
|
calcScal(layer->m_nOutput, momentum, layer->m_pGpuBiasUpdate, 1);
|
|
calcAxpy(size, -decay*batch, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1);
|
|
calcAxpy(size, learning_rate / batch, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeight, 1);
|
|
calcScal(size, momentum, layer->m_pGpuWeightUpdate, 1);
|
|
};
|
|
|
|
void gpuCUDA::pull_layer(localLayer *l){
|
|
//int locations = l->m_nOut_Width .out_w*l.out_h;
|
|
//int size = l->m_nSize*l.size*l.c*l.n*locations;
|
|
pullFloatArray(l->m_pGpuWeight, l->m_pWeight, l->m_nWeight);// size);
|
|
pullFloatArray(l->m_pGpuBias, l->m_pBias, l->m_nBias);
|
|
};
|
|
|
|
void gpuCUDA::push_layer(localLayer *l){
|
|
//int locations = l.out_w*l.out_h;
|
|
//int size = l.size*l.size*l.c*l.n*locations;
|
|
pushFloatArray(l->m_pGpuWeight, l->m_pWeight, l->m_nWeight);// size);
|
|
pushFloatArray(l->m_pGpuBias, l->m_pBias, l->m_nBias);
|
|
//cuda_push_array(l.weights_gpu, l.weights, size);
|
|
//cuda_push_array(l.biases_gpu, l.biases, l.outputs);
|
|
};
|