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

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cuda.h"
  2. #include "../layers/crnnLayer.h"
  3. void gpuCUDA::pull_layer(crnnLayer *layer){
  4. pull_layer(layer->m_pInputLayer);
  5. pull_layer(layer->m_pSelfLayer);
  6. pull_layer(layer->m_pOutputLayer);
  7. };
  8. void gpuCUDA::push_layer(crnnLayer *layer){
  9. push_layer(layer->m_pInputLayer);
  10. push_layer(layer->m_pSelfLayer);
  11. push_layer(layer->m_pOutputLayer);
  12. };
  13. void gpuCUDA::update_layer(crnnLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
  14. update_layer(layer->m_pInputLayer, batch, learning_rate, momentum, decay);
  15. update_layer(layer->m_pSelfLayer, batch, learning_rate, momentum, decay);
  16. update_layer(layer->m_pOutputLayer, batch, learning_rate, momentum, decay);
  17. };
  18. void gpuCUDA::forward_layer(crnnLayer *layer, cortexSTATE state){
  19. cortexSTATE s = { 0 };
  20. s.train = state.train;
  21. cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
  22. cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
  23. cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
  24. uint32_t CC = layer->m_nHidden * layer->m_nBatch;
  25. makeFill(layer->m_nOutputVol * layer->m_nSteps, 0, output_layer->m_pGpuDelta, 1);
  26. makeFill(CC * layer->m_nSteps, 0, self_layer->m_pGpuDelta, 1);
  27. makeFill(CC * layer->m_nSteps, 0, input_layer->m_pGpuDelta, 1);
  28. if (state.train) makeFill(CC, 0, layer->m_pGpuState, 1);
  29. for (uint32_t i = 0; i < layer->m_nSteps; ++i) {
  30. s.input = state.input;
  31. forward_layer((convolutionalLayer*)input_layer, s);
  32. s.input = layer->m_pGpuState;
  33. forward_layer((convolutionalLayer*)self_layer, s);
  34. float *old_state = layer->m_pGpuState;
  35. if (state.train) layer->m_pGpuState += layer->m_nHidden*layer->m_nBatch;
  36. if (layer->m_bShortcut){
  37. makeCopy(CC, old_state, 1, layer->m_pGpuState, 1);
  38. } else{
  39. makeFill(CC, 0, layer->m_pGpuState, 1);
  40. };
  41. calcAxpy(CC, 1, input_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
  42. calcAxpy(CC, 1, self_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
  43. s.input = layer->m_pGpuState;
  44. forward_layer((convolutionalLayer*)output_layer, s);
  45. state.input += layer->m_nInput*layer->m_nBatch;
  46. incrementLayer(input_layer, 1);
  47. incrementLayer(self_layer, 1);
  48. incrementLayer(output_layer, 1);
  49. };
  50. };
  51. void gpuCUDA::backward_layer(crnnLayer *layer, cortexSTATE state){
  52. cortexSTATE s = { 0 };
  53. s.train = state.train;
  54. cortexLayer *input_layer = (cortexLayer*)layer->m_pInputLayer;
  55. cortexLayer *self_layer = (cortexLayer*)layer->m_pSelfLayer;
  56. cortexLayer *output_layer = (cortexLayer*)layer->m_pOutputLayer;
  57. incrementLayer(input_layer, layer->m_nSteps - 1);
  58. incrementLayer(self_layer, layer->m_nSteps - 1);
  59. incrementLayer(output_layer, layer->m_nSteps - 1);
  60. uint32_t CC = layer->m_nHidden * layer->m_nBatch;
  61. layer->m_pGpuState += CC*layer->m_nSteps;
  62. for (uint32_t i = layer->m_nSteps - 1; i >= 0; --i) {
  63. makeCopy(CC, input_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
  64. calcAxpy(CC, 1, self_layer->m_pGpuOutput, 1, layer->m_pGpuState, 1);
  65. s.input = layer->m_pGpuState;
  66. s.delta = self_layer->m_pGpuDelta;
  67. backward_layer((convolutionalLayer*)output_layer, s);
  68. layer->m_pGpuState -= CC;
  69. s.input = layer->m_pGpuState;
  70. s.delta = self_layer->m_pGpuDelta - CC;
  71. if (i == 0) s.delta = 0;
  72. backward_layer((convolutionalLayer*)self_layer, s);
  73. makeCopy(CC, self_layer->m_pGpuDelta, 1, input_layer->m_pGpuDelta, 1);
  74. if (i > 0 && layer->m_bShortcut) calcAxpy(CC, 1, self_layer->m_pGpuDelta, 1, self_layer->m_pGpuDelta - CC, 1);
  75. s.input = state.input + i*layer->m_nInput*layer->m_nBatch;
  76. if (state.delta) s.delta = state.delta + i*layer->m_nInput*layer->m_nBatch;
  77. else s.delta = 0;
  78. backward_layer((convolutionalLayer*)input_layer, s);
  79. incrementLayer(input_layer, -1);
  80. incrementLayer(self_layer, -1);
  81. incrementLayer(output_layer, -1);
  82. };
  83. };