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.
76 lines
3.1 KiB
76 lines
3.1 KiB
#include "cuda.h"
|
|
#include "../layers/connectedLayer.h"
|
|
|
|
void gpuCUDA::forward_layer(connectedLayer *layer, cortexSTATE state){
|
|
makeFill(layer->m_nOutputVol, 0, layer->m_pGpuOutput, 1);
|
|
int m = layer->m_nBatch;
|
|
int k = layer->m_nInput;
|
|
int n = layer->m_nOutput;
|
|
float * a = state.input;
|
|
float * b = layer->m_pGpuWeight;
|
|
float * c = layer->m_pGpuOutput;
|
|
gemm(0, 1, m, n, k, 1, a, k, b, k, 1, c, n);
|
|
if (layer->m_bBatchNormalize){
|
|
forward_layer((batchnormLayer*)layer, state);
|
|
};
|
|
for (uint32_t i = 0; i < layer->m_nBatch; ++i){
|
|
calcAxpy(layer->m_nOutput, 1, layer->m_pGpuBias, 1, layer->m_pOutput + i*layer->m_nOutput, 1);
|
|
};
|
|
makeActivateArray(layer->m_pGpuOutput, layer->m_nOutputVol, layer->m_eActivation);
|
|
};
|
|
|
|
void gpuCUDA::backward_layer(connectedLayer *layer, cortexSTATE state){
|
|
constrain(layer->m_nOutputVol, 1, layer->m_pGpuDelta, 1);
|
|
makeGradientArray(layer->m_pGpuOutput, layer->m_nOutputVol, 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);
|
|
};
|
|
if (layer->m_bBatchNormalize){
|
|
backward_layer((batchnormLayer*)layer, state);
|
|
};
|
|
int m = layer->m_nOutput;
|
|
int k = layer->m_nBatch;
|
|
int n = layer->m_nInput;
|
|
float * a = layer->m_pGpuDelta;
|
|
float * b = state.input;
|
|
float * c = layer->m_pGpuWeightUpdate;
|
|
gemm(1, 0, m, n, k, 1, a, m, b, n, 1, c, n);
|
|
|
|
m = layer->m_nBatch;
|
|
k = layer->m_nOutput;
|
|
n = layer->m_nInput;
|
|
|
|
a = layer->m_pGpuDelta;
|
|
b = layer->m_pGpuWeight;
|
|
c = state.delta;
|
|
if (c) gemm(0, 0, m, n, k, 1, a, k, b, n, 1, c, n);
|
|
};
|
|
|
|
void gpuCUDA::pull_layer(connectedLayer *layer){
|
|
if (layer == NULL)return;
|
|
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);
|
|
if (layer->m_bBatchNormalize){
|
|
pullFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nScale);
|
|
pullFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nRollingMeanSize);
|
|
pullFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nRollingVarianceSize);
|
|
};
|
|
};
|
|
|
|
void gpuCUDA::push_layer(connectedLayer *layer){
|
|
if (layer == NULL)return;
|
|
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);
|
|
if (layer->m_bBatchNormalize){
|
|
pushFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nScale);
|
|
pushFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nRollingMeanSize);
|
|
pushFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nRollingVarianceSize);
|
|
};
|
|
};
|
|
|
|
void gpuCUDA::update_layer(connectedLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
|
|
};
|