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.
 
 
 
 
 
 

180 lines
7.5 KiB

#include "cuda.h"
#include "../layers/convolutionalLayer.h"
void gpuCUDA::forward_layer(convolutionalLayer *layer, cortexSTATE state){
uint32_t CC = layer->m_nIn_Channels*layer->m_nSize*layer->m_nSize;
uint32_t VV = layer->m_nIn_Channels*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nBatch;
makeFill(layer->m_nOutput*layer->m_nBatch, 0, layer->m_pGpuOutput, 1);
if (layer->m_bBinary){
binarizeWeights(layer->m_pGpuWeight, layer->m_nN, CC, layer->m_pGpuBinaryWeight);
layer->swapBinary();
};
if (layer->m_bXNOR){
binarizeWeights(layer->m_pGpuWeight, layer->m_nN, CC, layer->m_pGpuBinaryWeight);
layer->swapBinary();
binarize(state.input, VV, layer->m_pGpuBinaryInput);
state.input = layer->m_pGpuBinaryInput;
};
#ifdef CUDNN
float one = 1;
cudnnConvolutionForward(cudnn_handle(),
&one,
l.srcTensorDesc,
state.input,
l.weightDesc,
l.weights_gpu,
l.convDesc,
l.fw_algo,
state.workspace,
l.workspace_size,
&one,
l.dstTensorDesc,
l.output_gpu);
#else // CUDNN
int m = layer->m_nN;
int n = layer->m_nOut_Width*layer->m_nOut_Height;
for (uint32_t i = 0; i < layer->m_nBatch; ++i){
im2col(state.input + i*CC, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.workspace);
float * a = layer->m_pGpuWeight;
float * b = state.workspace;
float * c = layer->m_pGpuOutput;
gemm_ongpu(0, 0, m, n, CC, 1., a, CC, b, n, 1., c + i*m*n, n);
};
#endif // CUDNN
if (layer->m_bBatchNormalize) forward_layer(((batchnormLayer *)layer), state);
addBias(layer->m_pGpuOutput, layer->m_pGpuBias, layer->m_nBatch, layer->m_nN, layer->m_nOut_Width*layer->m_nOut_Height);
makeActivateArray(layer->m_pGpuOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation);
if( layer->m_fDOT > 0.f ) makeDotError(layer);
if( layer->m_bBinary || layer->m_bXNOR ) layer->swapBinary();
//cudaDeviceSynchronize(); // for correct profiling of performance
};
void gpuCUDA::backward_layer(convolutionalLayer *layer, cortexSTATE state){
makeGradientArray(layer->m_pGpuOutput, layer->m_nOutputVol, layer->m_eActivation, layer->m_pGpuDelta);
makeBackwardBias(layer->m_pGpuBiasUpdate, layer->m_pGpuDelta, layer->m_nBatch, layer->m_nN, layer->m_nOut_Width*layer->m_nOut_Height);
if( layer->m_bBatchNormalize ){
backward_layer(((batchnormLayer*)layer), state);
//axpy_ongpu(l.outputs*l.batch, -state.net.decay, l.x_gpu, 1, l.delta_gpu, 1);
} else {
//axpy_ongpu(l.outputs*l.batch, -state.net.decay, l.output_gpu, 1, l.delta_gpu, 1);
};
float *original_input = state.input;
if (layer->m_bXNOR) state.input = layer->m_pGpuBinaryInput;
#ifdef CUDNN
float one = 1;
cudnnConvolutionBackwardFilter(cudnn_handle(),
&one,
l.srcTensorDesc,
state.input,
l.ddstTensorDesc,
l.delta_gpu,
l.convDesc,
l.bf_algo,
state.workspace,
l.workspace_size,
&one,
l.dweightDesc,
l.weight_updates_gpu);
if (state.delta){
if (l.binary || l.xnor) swap_binary(&l);
cudnnConvolutionBackwardData(cudnn_handle(),
&one,
l.weightDesc,
l.weights_gpu,
l.ddstTensorDesc,
l.delta_gpu,
l.convDesc,
l.bd_algo,
state.workspace,
l.workspace_size,
&one,
l.dsrcTensorDesc,
state.delta);
if (l.binary || l.xnor) swap_binary(&l);
if (l.xnor) gradient_array_ongpu(original_input, l.batch*l.c*l.h*l.w, HARDTAN, state.delta);
}
#else
uint32_t m = layer->m_nN;
uint32_t n = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
uint32_t k = layer->m_nOut_Width*layer->m_nOut_Height;
uint32_t CC = layer->m_nIn_Channels * layer->m_nIn_Height*layer->m_nIn_Width;
for(uint32_t i = 0; i < layer->m_nBatch; ++i){
float * a = layer->m_pGpuDelta;
float * b = state.workspace;
float * c = layer->m_pGpuWeightUpdate;
im2col(state.input + i*CC, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.workspace);
gemm_ongpu(0, 1, m, n, k, 1, a + i*m*k, k, b, k, 1, c, n);
if( state.delta ){
if(layer->m_bBinary || layer->m_bXNOR) layer->swapBinary();
float * a = layer->m_pGpuWeight;
float * b = layer->m_pGpuDelta;
float * c = state.workspace;
gemm_ongpu(1, 0, n, k, m, 1, a, n, b + i*k*m, k, 0, c, k);
col2im(state.workspace, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.delta + i*CC);
if (layer->m_bBinary || layer->m_bXNOR) layer->swapBinary();
if (layer->m_bXNOR) makeGradientArray(original_input + i*CC, CC, HARDTAN, state.delta + i*CC);
};
};
#endif
};
void gpuCUDA::pull_layer(convolutionalLayer *layer){
uint32_t CC = layer->m_nIn_Channels * layer->m_nN * layer->m_nSize * layer->m_nSize;
pullFloatArray(layer->m_pGpuWeight, layer->m_pWeight, CC);
pullFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nN);
pullFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, CC);
pullFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nN);
if (layer->m_bBatchNormalize){
pullFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nN);
pullFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nN);
pullFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nN);
};
if (layer->m_bAdam){
pullFloatArray(layer->m_pGpuM, layer->m_pHostM, CC);
pullFloatArray(layer->m_pGpuV, layer->m_pHostV, CC);
};
};
void gpuCUDA::push_layer(convolutionalLayer *layer){
uint32_t CC = layer->m_nIn_Channels * layer->m_nN * layer->m_nSize * layer->m_nSize;
pushFloatArray(layer->m_pGpuWeight, layer->m_pWeight, CC);
pushFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nN);
pushFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, CC);
pushFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nN);
if (layer->m_bBatchNormalize){
pushFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nN);
pushFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nN);
pushFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nN);
}
if (layer->m_bAdam){
pushFloatArray(layer->m_pGpuM, layer->m_pHostM, CC);
pushFloatArray(layer->m_pGpuV, layer->m_pHostM, CC);
};
};
void gpuCUDA::update_layer(convolutionalLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
uint32_t CC = layer->m_nIn_Channels * layer->m_nN * layer->m_nSize * layer->m_nSize;
calcAxpy(layer->m_nN, learning_rate / batch, layer->m_pBiasUpdate, 1, layer->m_pGpuBias, 1);
calcScal(layer->m_nN, momentum, layer->m_pGpuBiasUpdate, 1);
if (layer->m_pGpuScale){
calcAxpy(layer->m_nN, learning_rate / batch, layer->m_pGpuScaleUpdate, 1, layer->m_pGpuScale, 1);
calcScal(layer->m_nN, momentum, layer->m_pGpuScaleUpdate, 1);
};
if (layer->m_bAdam){
calcScal(CC, layer->m_fB1, layer->m_pGpuM, 1);
calcScal(CC, layer->m_fB2, layer->m_pGpuV, 1);
calcAxpy(CC, -decay*batch, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1);
calcAxpy(CC, -(1 - layer->m_fB1), layer->m_pGpuWeightUpdate, 1, layer->m_pGpuM, 1);
calcMul(CC, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeightUpdate, 1);
calcAxpy(CC, (1 - layer->m_fB2), layer->m_pGpuWeightUpdate, 1, layer->m_pGpuV, 1);
makeAdam(CC, layer->m_pGpuWeight, layer->m_pGpuM, layer->m_pGpuV, layer->m_fB1, layer->m_fB2, learning_rate / batch, layer->m_fEpsilon, layer->m_nT + 1);
makeFill(CC, 0, layer->m_pGpuWeightUpdate, 1);
} else {
calcAxpy(CC, -decay*batch, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1);
calcAxpy(CC, learning_rate / batch, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeight, 1);
calcScal(CC, momentum, layer->m_pGpuWeightUpdate, 1);
};
};