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.

180 lines
7.5 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cuda.h"
  2. #include "../layers/convolutionalLayer.h"
  3. void gpuCUDA::forward_layer(convolutionalLayer *layer, cortexSTATE state){
  4. uint32_t CC = layer->m_nIn_Channels*layer->m_nSize*layer->m_nSize;
  5. uint32_t VV = layer->m_nIn_Channels*layer->m_nIn_Width*layer->m_nIn_Height*layer->m_nBatch;
  6. makeFill(layer->m_nOutput*layer->m_nBatch, 0, layer->m_pGpuOutput, 1);
  7. if (layer->m_bBinary){
  8. binarizeWeights(layer->m_pGpuWeight, layer->m_nN, CC, layer->m_pGpuBinaryWeight);
  9. layer->swapBinary();
  10. };
  11. if (layer->m_bXNOR){
  12. binarizeWeights(layer->m_pGpuWeight, layer->m_nN, CC, layer->m_pGpuBinaryWeight);
  13. layer->swapBinary();
  14. binarize(state.input, VV, layer->m_pGpuBinaryInput);
  15. state.input = layer->m_pGpuBinaryInput;
  16. };
  17. #ifdef CUDNN
  18. float one = 1;
  19. cudnnConvolutionForward(cudnn_handle(),
  20. &one,
  21. l.srcTensorDesc,
  22. state.input,
  23. l.weightDesc,
  24. l.weights_gpu,
  25. l.convDesc,
  26. l.fw_algo,
  27. state.workspace,
  28. l.workspace_size,
  29. &one,
  30. l.dstTensorDesc,
  31. l.output_gpu);
  32. #else // CUDNN
  33. int m = layer->m_nN;
  34. int n = layer->m_nOut_Width*layer->m_nOut_Height;
  35. for (uint32_t i = 0; i < layer->m_nBatch; ++i){
  36. im2col(state.input + i*CC, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.workspace);
  37. float * a = layer->m_pGpuWeight;
  38. float * b = state.workspace;
  39. float * c = layer->m_pGpuOutput;
  40. gemm_ongpu(0, 0, m, n, CC, 1., a, CC, b, n, 1., c + i*m*n, n);
  41. };
  42. #endif // CUDNN
  43. if (layer->m_bBatchNormalize) forward_layer(((batchnormLayer *)layer), state);
  44. addBias(layer->m_pGpuOutput, layer->m_pGpuBias, layer->m_nBatch, layer->m_nN, layer->m_nOut_Width*layer->m_nOut_Height);
  45. makeActivateArray(layer->m_pGpuOutput, layer->m_nOutput*layer->m_nBatch, layer->m_eActivation);
  46. if( layer->m_fDOT > 0.f ) makeDotError(layer);
  47. if( layer->m_bBinary || layer->m_bXNOR ) layer->swapBinary();
  48. //cudaDeviceSynchronize(); // for correct profiling of performance
  49. };
  50. void gpuCUDA::backward_layer(convolutionalLayer *layer, cortexSTATE state){
  51. makeGradientArray(layer->m_pGpuOutput, layer->m_nOutputVol, layer->m_eActivation, layer->m_pGpuDelta);
  52. makeBackwardBias(layer->m_pGpuBiasUpdate, layer->m_pGpuDelta, layer->m_nBatch, layer->m_nN, layer->m_nOut_Width*layer->m_nOut_Height);
  53. if( layer->m_bBatchNormalize ){
  54. backward_layer(((batchnormLayer*)layer), state);
  55. //axpy_ongpu(l.outputs*l.batch, -state.net.decay, l.x_gpu, 1, l.delta_gpu, 1);
  56. } else {
  57. //axpy_ongpu(l.outputs*l.batch, -state.net.decay, l.output_gpu, 1, l.delta_gpu, 1);
  58. };
  59. float *original_input = state.input;
  60. if (layer->m_bXNOR) state.input = layer->m_pGpuBinaryInput;
  61. #ifdef CUDNN
  62. float one = 1;
  63. cudnnConvolutionBackwardFilter(cudnn_handle(),
  64. &one,
  65. l.srcTensorDesc,
  66. state.input,
  67. l.ddstTensorDesc,
  68. l.delta_gpu,
  69. l.convDesc,
  70. l.bf_algo,
  71. state.workspace,
  72. l.workspace_size,
  73. &one,
  74. l.dweightDesc,
  75. l.weight_updates_gpu);
  76. if (state.delta){
  77. if (l.binary || l.xnor) swap_binary(&l);
  78. cudnnConvolutionBackwardData(cudnn_handle(),
  79. &one,
  80. l.weightDesc,
  81. l.weights_gpu,
  82. l.ddstTensorDesc,
  83. l.delta_gpu,
  84. l.convDesc,
  85. l.bd_algo,
  86. state.workspace,
  87. l.workspace_size,
  88. &one,
  89. l.dsrcTensorDesc,
  90. state.delta);
  91. if (l.binary || l.xnor) swap_binary(&l);
  92. if (l.xnor) gradient_array_ongpu(original_input, l.batch*l.c*l.h*l.w, HARDTAN, state.delta);
  93. }
  94. #else
  95. uint32_t m = layer->m_nN;
  96. uint32_t n = layer->m_nSize*layer->m_nSize*layer->m_nIn_Channels;
  97. uint32_t k = layer->m_nOut_Width*layer->m_nOut_Height;
  98. uint32_t CC = layer->m_nIn_Channels * layer->m_nIn_Height*layer->m_nIn_Width;
  99. for(uint32_t i = 0; i < layer->m_nBatch; ++i){
  100. float * a = layer->m_pGpuDelta;
  101. float * b = state.workspace;
  102. float * c = layer->m_pGpuWeightUpdate;
  103. im2col(state.input + i*CC, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.workspace);
  104. gemm_ongpu(0, 1, m, n, k, 1, a + i*m*k, k, b, k, 1, c, n);
  105. if( state.delta ){
  106. if(layer->m_bBinary || layer->m_bXNOR) layer->swapBinary();
  107. float * a = layer->m_pGpuWeight;
  108. float * b = layer->m_pGpuDelta;
  109. float * c = state.workspace;
  110. gemm_ongpu(1, 0, n, k, m, 1, a, n, b + i*k*m, k, 0, c, k);
  111. col2im(state.workspace, layer->m_nIn_Channels, layer->m_nIn_Height, layer->m_nIn_Width, layer->m_nSize, layer->m_nStride, layer->m_nPad, state.delta + i*CC);
  112. if (layer->m_bBinary || layer->m_bXNOR) layer->swapBinary();
  113. if (layer->m_bXNOR) makeGradientArray(original_input + i*CC, CC, HARDTAN, state.delta + i*CC);
  114. };
  115. };
  116. #endif
  117. };
  118. void gpuCUDA::pull_layer(convolutionalLayer *layer){
  119. uint32_t CC = layer->m_nIn_Channels * layer->m_nN * layer->m_nSize * layer->m_nSize;
  120. pullFloatArray(layer->m_pGpuWeight, layer->m_pWeight, CC);
  121. pullFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nN);
  122. pullFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, CC);
  123. pullFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nN);
  124. if (layer->m_bBatchNormalize){
  125. pullFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nN);
  126. pullFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nN);
  127. pullFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nN);
  128. };
  129. if (layer->m_bAdam){
  130. pullFloatArray(layer->m_pGpuM, layer->m_pHostM, CC);
  131. pullFloatArray(layer->m_pGpuV, layer->m_pHostV, CC);
  132. };
  133. };
  134. void gpuCUDA::push_layer(convolutionalLayer *layer){
  135. uint32_t CC = layer->m_nIn_Channels * layer->m_nN * layer->m_nSize * layer->m_nSize;
  136. pushFloatArray(layer->m_pGpuWeight, layer->m_pWeight, CC);
  137. pushFloatArray(layer->m_pGpuBias, layer->m_pBias, layer->m_nN);
  138. pushFloatArray(layer->m_pGpuWeightUpdate, layer->m_pWeightUpdate, CC);
  139. pushFloatArray(layer->m_pGpuBiasUpdate, layer->m_pBiasUpdate, layer->m_nN);
  140. if (layer->m_bBatchNormalize){
  141. pushFloatArray(layer->m_pGpuScale, layer->m_pScale, layer->m_nN);
  142. pushFloatArray(layer->m_pGpuRollingMean, layer->m_pRollingMean, layer->m_nN);
  143. pushFloatArray(layer->m_pGpuRollingVariance, layer->m_pRollingVariance, layer->m_nN);
  144. }
  145. if (layer->m_bAdam){
  146. pushFloatArray(layer->m_pGpuM, layer->m_pHostM, CC);
  147. pushFloatArray(layer->m_pGpuV, layer->m_pHostM, CC);
  148. };
  149. };
  150. void gpuCUDA::update_layer(convolutionalLayer *layer, uint32_t batch, float learning_rate, float momentum, float decay){
  151. uint32_t CC = layer->m_nIn_Channels * layer->m_nN * layer->m_nSize * layer->m_nSize;
  152. calcAxpy(layer->m_nN, learning_rate / batch, layer->m_pBiasUpdate, 1, layer->m_pGpuBias, 1);
  153. calcScal(layer->m_nN, momentum, layer->m_pGpuBiasUpdate, 1);
  154. if (layer->m_pGpuScale){
  155. calcAxpy(layer->m_nN, learning_rate / batch, layer->m_pGpuScaleUpdate, 1, layer->m_pGpuScale, 1);
  156. calcScal(layer->m_nN, momentum, layer->m_pGpuScaleUpdate, 1);
  157. };
  158. if (layer->m_bAdam){
  159. calcScal(CC, layer->m_fB1, layer->m_pGpuM, 1);
  160. calcScal(CC, layer->m_fB2, layer->m_pGpuV, 1);
  161. calcAxpy(CC, -decay*batch, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1);
  162. calcAxpy(CC, -(1 - layer->m_fB1), layer->m_pGpuWeightUpdate, 1, layer->m_pGpuM, 1);
  163. calcMul(CC, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeightUpdate, 1);
  164. calcAxpy(CC, (1 - layer->m_fB2), layer->m_pGpuWeightUpdate, 1, layer->m_pGpuV, 1);
  165. makeAdam(CC, layer->m_pGpuWeight, layer->m_pGpuM, layer->m_pGpuV, layer->m_fB1, layer->m_fB2, learning_rate / batch, layer->m_fEpsilon, layer->m_nT + 1);
  166. makeFill(CC, 0, layer->m_pGpuWeightUpdate, 1);
  167. } else {
  168. calcAxpy(CC, -decay*batch, layer->m_pGpuWeight, 1, layer->m_pGpuWeightUpdate, 1);
  169. calcAxpy(CC, learning_rate / batch, layer->m_pGpuWeightUpdate, 1, layer->m_pGpuWeight, 1);
  170. calcScal(CC, momentum, layer->m_pGpuWeightUpdate, 1);
  171. };
  172. };