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.

53 lines
1.6 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "cuda_runtime.h"
  2. #include "curand.h"
  3. #include "cublas_v2.h"
  4. #include "cuda.h"
  5. #include "../layers/avgpoolLayer.h"
  6. __global__ void forward_avgpool_layer_kernel(int n, int w, int h, int c, float *input, float *output)
  7. {
  8. int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  9. if (id >= n) return;
  10. int k = id % c;
  11. id /= c;
  12. int b = id;
  13. int i;
  14. int out_index = (k + c*b);
  15. output[out_index] = 0;
  16. for (i = 0; i < w*h; ++i){
  17. int in_index = i + h*w*(k + b*c);
  18. output[out_index] += input[in_index];
  19. }
  20. output[out_index] /= w*h;
  21. }
  22. __global__ void backward_avgpool_layer_kernel(int n, int w, int h, int c, float *in_delta, float *out_delta)
  23. {
  24. int id = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  25. if (id >= n) return;
  26. int k = id % c;
  27. id /= c;
  28. int b = id;
  29. int i;
  30. int out_index = (k + c*b);
  31. for (i = 0; i < w*h; ++i){
  32. int in_index = i + h*w*(k + b*c);
  33. in_delta[in_index] += out_delta[out_index] / (w*h);
  34. }
  35. }
  36. void gpuCUDA::forward_layer(avgpoolLayer *layer, cortexSTATE state){
  37. uint32_t Area = layer->m_nIn_Channels * layer->m_nBatch;
  38. forward_avgpool_layer_kernel << <gridSize(Area), BLOCK >> >(Area, layer->m_nIn_Width, layer->m_nIn_Height, layer->m_nIn_Channels, state.input, layer->m_pGpuOutput);
  39. checkError();
  40. };
  41. void gpuCUDA::backward_layer(avgpoolLayer *layer, cortexSTATE state){
  42. uint32_t Area = layer->m_nIn_Channels * layer->m_nBatch;
  43. backward_avgpool_layer_kernel << <gridSize(Area), BLOCK >> >(Area, layer->m_nIn_Width, layer->m_nIn_Height, layer->m_nIn_Channels, state.delta, layer->m_pGpuDelta);
  44. checkError();
  45. };