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.

75 lines
3.1 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cuda.h"
  2. #include "../layers/connectedLayer.h"
  3. void gpuCUDA::forward_layer(connectedLayer *layer, cortexSTATE state){
  4. makeFill(layer->m_nOutputVol, 0, layer->m_pGpuOutput, 1);
  5. int m = layer->m_nBatch;
  6. int k = layer->m_nInput;
  7. int n = layer->m_nOutput;
  8. float * a = state.input;
  9. float * b = layer->m_pGpuWeight;
  10. float * c = layer->m_pGpuOutput;
  11. gemm(0, 1, m, n, k, 1, a, k, b, k, 1, c, n);
  12. if (layer->m_bBatchNormalize){
  13. forward_layer((batchnormLayer*)layer, state);
  14. };
  15. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  16. calcAxpy(layer->m_nOutput, 1, layer->m_pGpuBias, 1, layer->m_pOutput + i*layer->m_nOutput, 1);
  17. };
  18. makeActivateArray(layer->m_pGpuOutput, layer->m_nOutputVol, layer->m_eActivation);
  19. };
  20. void gpuCUDA::backward_layer(connectedLayer *layer, cortexSTATE state){
  21. constrain(layer->m_nOutputVol, 1, layer->m_pGpuDelta, 1);
  22. makeGradientArray(layer->m_pGpuOutput, layer->m_nOutputVol, layer->m_eActivation, layer->m_pGpuDelta);
  23. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  24. calcAxpy(layer->m_nOutput, 1, layer->m_pGpuDelta + i*layer->m_nOutput, 1, layer->m_pGpuBiasUpdate, 1);
  25. };
  26. if (layer->m_bBatchNormalize){
  27. backward_layer((batchnormLayer*)layer, state);
  28. };
  29. int m = layer->m_nOutput;
  30. int k = layer->m_nBatch;
  31. int n = layer->m_nInput;
  32. float * a = layer->m_pGpuDelta;
  33. float * b = state.input;
  34. float * c = layer->m_pGpuWeightUpdate;
  35. gemm(1, 0, m, n, k, 1, a, m, b, n, 1, c, n);
  36. m = layer->m_nBatch;
  37. k = layer->m_nOutput;
  38. n = layer->m_nInput;
  39. a = layer->m_pGpuDelta;
  40. b = layer->m_pGpuWeight;
  41. c = state.delta;
  42. if (c) gemm(0, 0, m, n, k, 1, a, k, b, n, 1, c, n);
  43. };
  44. void gpuCUDA::pull_layer(connectedLayer *layer){
  45. if (layer == NULL)return;
  46. pullFloatArray(layer->m_pGpuWeight, layer->m_pWeight, layer->m_nWeight);
  47. pullFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nBias);
  48. pullFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, layer->m_nWeight);
  49. pullFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nBias);
  50. if (layer->m_bBatchNormalize){
  51. pullFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nScale);
  52. pullFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nRollingMeanSize);
  53. pullFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nRollingVarianceSize);
  54. };
  55. };
  56. void gpuCUDA::push_layer(connectedLayer *layer){
  57. if (layer == NULL)return;
  58. pushFloatArray(layer->m_pGpuWeight, layer->m_pWeight, layer->m_nWeight);
  59. pushFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nBias);
  60. pushFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, layer->m_nWeight);
  61. pushFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nBias);
  62. if (layer->m_bBatchNormalize){
  63. pushFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nScale);
  64. pushFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nRollingMeanSize);
  65. pushFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nRollingVarianceSize);
  66. };
  67. };
  68. void gpuCUDA::update_layer(connectedLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
  69. };