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.
88 lines
3.7 KiB
88 lines
3.7 KiB
#include "cuda.h"
|
|
#include "../layers/crnnLayer.h"
|
|
|
|
void gpuCUDA::pull_layer(crnnLayer *layer){
|
|
pull_layer(layer->m_pInputLayer);
|
|
pull_layer(layer->m_pSelfLayer);
|
|
pull_layer(layer->m_pOutputLayer);
|
|
};
|
|
|
|
void gpuCUDA::push_layer(crnnLayer *layer){
|
|
push_layer(layer->m_pInputLayer);
|
|
push_layer(layer->m_pSelfLayer);
|
|
push_layer(layer->m_pOutputLayer);
|
|
};
|
|
|
|
void gpuCUDA::update_layer(crnnLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
|
|
update_layer(layer->m_pInputLayer, batch, learning_rate, momentum, decay);
|
|
update_layer(layer->m_pSelfLayer, batch, learning_rate, momentum, decay);
|
|
update_layer(layer->m_pOutputLayer, batch, learning_rate, momentum, decay);
|
|
};
|
|
|
|
void gpuCUDA::forward_layer(crnnLayer *layer, cortexSTATE state){
|
|
cortexSTATE s = { 0 };
|
|
s.train = state.train;
|
|
cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
|
|
cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
|
|
cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
|
|
uint32_t CC = layer->m_nHidden * layer->m_nBatch;
|
|
makeFill(layer->m_nOutputVol * layer->m_nSteps, 0, output_layer->m_pGpuDelta, 1);
|
|
makeFill(CC * layer->m_nSteps, 0, self_layer->m_pGpuDelta, 1);
|
|
makeFill(CC * layer->m_nSteps, 0, input_layer->m_pGpuDelta, 1);
|
|
if (state.train) makeFill(CC, 0, layer->m_pGpuState, 1);
|
|
for (uint32_t i = 0; i < layer->m_nSteps; ++i) {
|
|
s.input = state.input;
|
|
forward_layer((convolutionalLayer*)input_layer, s);
|
|
s.input = layer->m_pGpuState;
|
|
forward_layer((convolutionalLayer*)self_layer, s);
|
|
float *old_state = layer->m_pGpuState;
|
|
if (state.train) layer->m_pGpuState += layer->m_nHidden*layer->m_nBatch;
|
|
if (layer->m_bShortcut){
|
|
makeCopy(CC, old_state, 1, layer->m_pGpuState, 1);
|
|
} else{
|
|
makeFill(CC, 0, layer->m_pGpuState, 1);
|
|
};
|
|
calcAxpy(CC, 1, input_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
|
|
calcAxpy(CC, 1, self_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
|
|
s.input = layer->m_pGpuState;
|
|
forward_layer((convolutionalLayer*)output_layer, s);
|
|
state.input += layer->m_nInput*layer->m_nBatch;
|
|
incrementLayer(input_layer, 1);
|
|
incrementLayer(self_layer, 1);
|
|
incrementLayer(output_layer, 1);
|
|
};
|
|
};
|
|
|
|
void gpuCUDA::backward_layer(crnnLayer *layer, cortexSTATE state){
|
|
cortexSTATE s = { 0 };
|
|
s.train = state.train;
|
|
cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
|
|
cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
|
|
cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
|
|
incrementLayer(input_layer, layer->m_nSteps - 1);
|
|
incrementLayer(self_layer, layer->m_nSteps - 1);
|
|
incrementLayer(output_layer, layer->m_nSteps - 1);
|
|
uint32_t CC = layer->m_nHidden * layer->m_nBatch;
|
|
layer->m_pGpuState += CC*layer->m_nSteps;
|
|
for (uint32_t i = layer->m_nSteps - 1; i >= 0; --i) {
|
|
makeCopy(CC, input_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
|
|
calcAxpy(CC, 1, self_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
|
|
s.input = layer->m_pGpuState;
|
|
s.delta = self_layer->m_pGpuDelta;
|
|
backward_layer((convolutionalLayer*)output_layer, s);
|
|
layer->m_pGpuState -= CC;
|
|
s.input = layer->m_pGpuState;
|
|
s.delta = self_layer->m_pGpuDelta - CC;
|
|
if (i == 0) s.delta = 0;
|
|
backward_layer((convolutionalLayer*)self_layer, s);
|
|
makeCopy(CC, self_layer->m_pGpuDelta, 1, input_layer->m_pGpuDelta, 1);
|
|
if (i > 0 && layer->m_bShortcut) calcAxpy(CC, 1, self_layer->m_pGpuDelta, 1, self_layer->m_pGpuDelta - CC, 1);
|
|
s.input = state.input + i*layer->m_nInput*layer->m_nBatch;
|
|
if (state.delta) s.delta = state.delta + i*layer->m_nInput*layer->m_nBatch;
|
|
else s.delta = 0;
|
|
backward_layer((convolutionalLayer*)input_layer, s);
|
|
incrementLayer(input_layer, -1);
|
|
incrementLayer(self_layer, -1);
|
|
incrementLayer(output_layer, -1);
|
|
};
|
|
};
|