#include #include #include #include "cuda.h" #include "../layers/deconvolutionalLayer.h" void gpuCUDA::forward_layer(deconvolutionalLayer *layer, cortexSTATE state){ uint32_t size = layer->m_nOut_Height*layer->m_nOut_Width; uint32_t m = layer->m_nSize*layer->m_nSize*layer->m_nOut_Channels;// .n; uint32_t n = layer->m_nIn_Height*layer->m_nIn_Width; uint32_t k = layer->m_nIn_Channels; makeFill(layer->m_nOutput*layer->m_nBatch, 0, layer->m_pGpuOutput, 1); for (uint32_t i = 0; i < layer->m_nBatch; ++i){ float *a = layer->m_pGpuWeight; float *b = &state.input[i*layer->m_nIn_Channels*layer->m_nIn_Height*layer->m_nIn_Width]; float *c = layer->m_pGpuColImage; gemm(1,0,m,n,k,1,a,m,b,n,0,c,n); col2im(c, layer->m_nOut_Channels, layer->m_nOut_Height, layer->m_nOut_Width, layer->m_nSize, layer->m_nStride, 0, &layer->m_pGpuOutput[i*layer->m_nOut_Channels*size]); }; addBias(layer->m_pGpuOutput, layer->m_pGpuBias, layer->m_nBatch, layer->m_nOut_Channels, size); makeActivateArray(layer->m_pGpuOutput, layer->m_nBatch*layer->m_nOut_Channels*size, layer->m_eActivation); }; void gpuCUDA::backward_layer(deconvolutionalLayer *layer, cortexSTATE state){ float alpha = 1.f /layer->m_nBatch; uint32_t size = layer->m_nOut_Height*layer->m_nOut_Width; makeGradientArray(layer->m_pGpuOutput, size*layer->m_nOut_Channels*layer->m_nBatch, layer->m_eActivation, layer->m_pGpuDelta); makeBackwardBias(layer->m_pGpuBiasUpdate, layer->m_pDelta, layer->m_nBatch, layer->m_nOut_Channels, size); if(state.delta) memset(state.delta, 0, layer->m_nBatch*layer->m_nIn_Height*layer->m_nIn_Width*layer->m_nIn_Channels*sizeof(float)); for(uint32_t i = 0; i < layer->m_nBatch; ++i){ int m = layer->m_nIn_Channels; int n = layer->m_nSize*layer->m_nSize*layer->m_nOut_Channels; int k = layer->m_nIn_Height*layer->m_nIn_Width; float *a = state.input + i*m*n; float *b = layer->getGpuColImage(); float *c = layer->m_pGpuWeightUpdate; im2col( &layer->m_pGpuDelta[i*layer->m_nOut_Channels*size], layer->m_nOut_Channels, layer->m_nOut_Height, layer->m_nOut_Width, layer->m_nSize, layer->m_nStride, 0, b ); gemm(0,1,m,n,k,alpha,a,k,b,k,1,c,n); if(state.delta){ int m = layer->m_nIn_Channels; int n = layer->m_nIn_Height*layer->m_nIn_Width; int k = layer->m_nSize*layer->m_nSize*layer->m_nOut_Channels; float *a = layer->m_pGpuWeight; float *b = layer->m_pGpuColImage; float *c = &state.delta[i*n*m]; gemm(0,0,m,n,k,1,a,k,b,n,1,c,n); }; }; }; void gpuCUDA::pull_layer(deconvolutionalLayer *layer){ pullFloatArray(layer->m_pGpuWeight, layer->m_pWeight, layer->m_nWeight); pullFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nBias); pullFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, layer->m_nWeight); pullFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nBias); }; void gpuCUDA::push_layer(deconvolutionalLayer *layer){ pushFloatArray(layer->m_pGpuWeight, layer->m_pWeight, layer->m_nWeight); pushFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nBias); pushFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, layer->m_nWeight); pushFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nBias); }; void gpuCUDA::update_layer(deconvolutionalLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){ int size = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nOut_Channels; calcAxpy(layer->m_nOut_Channels, learning_rate, layer->m_pGpuBiasUpdate, 1, layer->m_pGpuBias, 1); calcScal(layer->m_nOut_Channels, momentum, layer->m_pGpuBiasUpdate, 1); calcAxpy(size, -decay, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1); calcAxpy(size, learning_rate, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeight, 1); calcScal(size, momentum, layer->m_pGpuWeightUpdate, 1); };