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.

85 lines
3.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <cuda_runtime.h>
  2. #include <curand.h>
  3. #include <cublas_v2.h>
  4. #include "cuda.h"
  5. #include "../layers/deconvolutionalLayer.h"
  6. void gpuCUDA::forward_layer(deconvolutionalLayer *layer, cortexSTATE state){
  7. uint32_t size = layer->m_nOut_Height*layer->m_nOut_Width;
  8. uint32_t m = layer->m_nSize*layer->m_nSize*layer->m_nOut_Channels;// .n;
  9. uint32_t n = layer->m_nIn_Height*layer->m_nIn_Width;
  10. uint32_t k = layer->m_nIn_Channels;
  11. makeFill(layer->m_nOutput*layer->m_nBatch, 0, layer->m_pGpuOutput, 1);
  12. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  13. float *a = layer->m_pGpuWeight;
  14. float *b = &state.input[i*layer->m_nIn_Channels*layer->m_nIn_Height*layer->m_nIn_Width];
  15. float *c = layer->m_pGpuColImage;
  16. gemm(1,0,m,n,k,1,a,m,b,n,0,c,n);
  17. col2im(c, layer->m_nOut_Channels, layer->m_nOut_Height, layer->m_nOut_Width, layer->m_nSize, layer->m_nStride, 0, &layer->m_pGpuOutput[i*layer->m_nOut_Channels*size]);
  18. };
  19. addBias(layer->m_pGpuOutput, layer->m_pGpuBias, layer->m_nBatch, layer->m_nOut_Channels, size);
  20. makeActivateArray(layer->m_pGpuOutput, layer->m_nBatch*layer->m_nOut_Channels*size, layer->m_eActivation);
  21. };
  22. void gpuCUDA::backward_layer(deconvolutionalLayer *layer, cortexSTATE state){
  23. float alpha = 1.f /layer->m_nBatch;
  24. uint32_t size = layer->m_nOut_Height*layer->m_nOut_Width;
  25. makeGradientArray(layer->m_pGpuOutput, size*layer->m_nOut_Channels*layer->m_nBatch, layer->m_eActivation, layer->m_pGpuDelta);
  26. makeBackwardBias(layer->m_pGpuBiasUpdate, layer->m_pDelta, layer->m_nBatch, layer->m_nOut_Channels, size);
  27. if(state.delta) memset(state.delta, 0, layer->m_nBatch*layer->m_nIn_Height*layer->m_nIn_Width*layer->m_nIn_Channels*sizeof(float));
  28. for(uint32_t i = 0; i < layer->m_nBatch; ++i){
  29. int m = layer->m_nIn_Channels;
  30. int n = layer->m_nSize*layer->m_nSize*layer->m_nOut_Channels;
  31. int k = layer->m_nIn_Height*layer->m_nIn_Width;
  32. float *a = state.input + i*m*n;
  33. float *b = layer->getGpuColImage();
  34. float *c = layer->m_pGpuWeightUpdate;
  35. im2col(
  36. &layer->m_pGpuDelta[i*layer->m_nOut_Channels*size],
  37. layer->m_nOut_Channels,
  38. layer->m_nOut_Height,
  39. layer->m_nOut_Width,
  40. layer->m_nSize,
  41. layer->m_nStride,
  42. 0,
  43. b
  44. );
  45. gemm(0,1,m,n,k,alpha,a,k,b,k,1,c,n);
  46. if(state.delta){
  47. int m = layer->m_nIn_Channels;
  48. int n = layer->m_nIn_Height*layer->m_nIn_Width;
  49. int k = layer->m_nSize*layer->m_nSize*layer->m_nOut_Channels;
  50. float *a = layer->m_pGpuWeight;
  51. float *b = layer->m_pGpuColImage;
  52. float *c = &state.delta[i*n*m];
  53. gemm(0,0,m,n,k,1,a,k,b,n,1,c,n);
  54. };
  55. };
  56. };
  57. void gpuCUDA::pull_layer(deconvolutionalLayer *layer){
  58. pullFloatArray(layer->m_pGpuWeight, layer->m_pWeight, layer->m_nWeight);
  59. pullFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nBias);
  60. pullFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, layer->m_nWeight);
  61. pullFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nBias);
  62. };
  63. void gpuCUDA::push_layer(deconvolutionalLayer *layer){
  64. pushFloatArray(layer->m_pGpuWeight, layer->m_pWeight, layer->m_nWeight);
  65. pushFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nBias);
  66. pushFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, layer->m_nWeight);
  67. pushFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nBias);
  68. };
  69. void gpuCUDA::update_layer(deconvolutionalLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
  70. int size = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels*layer->m_nOut_Channels;
  71. calcAxpy(layer->m_nOut_Channels, learning_rate, layer->m_pGpuBiasUpdate, 1, layer->m_pGpuBias, 1);
  72. calcScal(layer->m_nOut_Channels, momentum, layer->m_pGpuBiasUpdate, 1);
  73. calcAxpy(size, -decay, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1);
  74. calcAxpy(size, learning_rate, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeight, 1);
  75. calcScal(size, momentum, layer->m_pGpuWeightUpdate, 1);
  76. };