|
|
#include "cpu.h"
#include "../layers/localLayer.h"
void cpuHOST::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_pBias, 1, layer->m_pOutput + 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(layer->m_pInput, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, layer->m_pColImage); float *output = layer->m_pOutput + i*layer->m_nOutput; for (uint32_t j = 0; j < locations; ++j){ float *a = layer->m_pWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN; float *b = layer->m_pColImage + j; float *c = output + j; int m = layer->m_nN; 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_pOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation);};
void cpuHOST::backward_layer(localLayer *layer, cortexSTATE state){ uint32_t locations = layer->m_nOut_Width*layer->m_nOut_Height; makeGradientArray(layer->m_pOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation, layer->m_pDelta); for (uint32_t i = 0; i < layer->m_nBatch; ++i){ calcAxpy(layer->m_nOutput, 1, layer->m_pDelta + i*layer->m_nOutput, 1, layer->m_pBiasUpdate, 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(layer->m_pInput, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, layer->m_pColImage); for (uint32_t j = 0; j < locations; ++j){ float *a = layer->m_pDelta + i*layer->m_nOutput + j; float *b = layer->m_pColImage + j; float *c = layer->m_pWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN; int m = layer->m_nN; 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 != NULL){ for (uint32_t j = 0; j < locations; ++j){ float *a = layer->m_pWeight + j*layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nN; float *b = layer->m_pDelta + i*layer->m_nOutput + j; float *c = layer->m_pColImage + j; int m = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels; int n = 1; int k = layer->m_nN; gemm(1, 0, m, n, k, 1, a, m, b, locations, 0, c, locations); }; col2im(layer->m_pColImage, 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_Width*layer->m_nIn_Height*layer->m_nIn_Channels); }; };};
void cpuHOST::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_pBiasUpdate, 1, layer->m_pBias, 1); calcScal(layer->m_nOutput, momentum, layer->m_pBiasUpdate, 1); calcAxpy(size, -decay*batch, layer->m_pWeight, 1, layer->m_pWeightUpdate, 1); calcAxpy(size, learning_rate / batch, layer->m_pWeightUpdate, 1, layer->m_pWeight, 1); calcScal(size, momentum, layer->m_pWeightUpdate, 1);};
|