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.

251 lines
7.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <cuda_runtime.h>
  2. #include <curand.h>
  3. #include <cublas_v2.h>
  4. #ifdef CUDNN
  5. #pragma comment(lib, "cudnn.lib")
  6. #endif
  7. #include "cuda.h"
  8. #include "../base/basicCortex.h"
  9. #include "../layers/convolutionalLayer.h"
  10. #include "../cpu/gemm.h"
  11. #define LOG( X ) m_pLog->Log( "CUDA", this, X )
  12. #define LOG_ERROR( X ) m_pLog->Log( "CUDA", this, _LOG_ERROR, X )
  13. __device__ float lhtan_activate_kernel(float x){
  14. if (x < 0) return .001*x;
  15. if (x > 1) return .001*(x - 1) + 1;
  16. return x;
  17. };
  18. __device__ float lhtan_gradient_kernel(float x)
  19. {
  20. if (x > 0 && x < 1) return 1;
  21. return .001;
  22. }
  23. __device__ float hardtan_activate_kernel(float x)
  24. {
  25. if (x < -1) return -1;
  26. if (x > 1) return 1;
  27. return x;
  28. }
  29. __device__ float linear_activate_kernel(float x){ return x; }
  30. __device__ float logistic_activate_kernel(float x){ return 1. / (1. + exp(-x)); }
  31. __device__ float loggy_activate_kernel(float x){ return 2. / (1. + exp(-x)) - 1; }
  32. __device__ float relu_activate_kernel(float x){ return x*(x>0); }
  33. __device__ float elu_activate_kernel(float x){ return (x >= 0)*x + (x < 0)*(exp(x) - 1); }
  34. __device__ float relie_activate_kernel(float x){ return (x>0) ? x : .01*x; }
  35. __device__ float ramp_activate_kernel(float x){ return x*(x>0) + .1*x; }
  36. __device__ float leaky_activate_kernel(float x){ return (x>0) ? x : .1*x; }
  37. __device__ float tanh_activate_kernel(float x){ return (2 / (1 + exp(-2 * x)) - 1); }
  38. __device__ float plse_activate_kernel(float x){
  39. if (x < -4) return .01 * (x + 4);
  40. if (x > 4) return .01 * (x - 4) + 1;
  41. return .125*x + .5;
  42. }
  43. __device__ float stair_activate_kernel(float x){
  44. int n = floor(x);
  45. if (n % 2 == 0) return floor(x / 2.);
  46. else return (x - n) + floor(x / 2.);
  47. }
  48. __device__ float hardtan_gradient_kernel(float x){
  49. if (x > -1 && x < 1) return 1;
  50. return 0;
  51. }
  52. __device__ float linear_gradient_kernel(float x){ return 1; }
  53. __device__ float logistic_gradient_kernel(float x){ return (1 - x)*x; }
  54. __device__ float loggy_gradient_kernel(float x){
  55. float y = (x + 1.) / 2.;
  56. return 2 * (1 - y)*y;
  57. }
  58. __device__ float relu_gradient_kernel(float x){ return (x>0); }
  59. __device__ float elu_gradient_kernel(float x){ return (x >= 0) + (x < 0)*(x + 1); }
  60. __device__ float relie_gradient_kernel(float x){ return (x>0) ? 1 : .01; }
  61. __device__ float ramp_gradient_kernel(float x){ return (x>0) + .1; }
  62. __device__ float leaky_gradient_kernel(float x){ return (x>0) ? 1 : .1; }
  63. __device__ float tanh_gradient_kernel(float x){ return 1 - x*x; }
  64. __device__ float plse_gradient_kernel(float x){ return (x < 0 || x > 1) ? .01 : .125; }
  65. __device__ float stair_gradient_kernel(float x){
  66. if (floor(x) == x) return 0;
  67. return 1;
  68. }
  69. __device__ float activate_kernel(float x, ACTIVATION a){
  70. switch (a){
  71. case LINEAR:
  72. return linear_activate_kernel(x);
  73. case LOGISTIC:
  74. return logistic_activate_kernel(x);
  75. case LOGGY:
  76. return loggy_activate_kernel(x);
  77. case RELU:
  78. return relu_activate_kernel(x);
  79. case ELU:
  80. return elu_activate_kernel(x);
  81. case RELIE:
  82. return relie_activate_kernel(x);
  83. case RAMP:
  84. return ramp_activate_kernel(x);
  85. case LEAKY:
  86. return leaky_activate_kernel(x);
  87. case TANH:
  88. return tanh_activate_kernel(x);
  89. case PLSE:
  90. return plse_activate_kernel(x);
  91. case STAIR:
  92. return stair_activate_kernel(x);
  93. case HARDTAN:
  94. return hardtan_activate_kernel(x);
  95. case LHTAN:
  96. return lhtan_activate_kernel(x);
  97. }
  98. return 0;
  99. }
  100. __device__ float gradient_kernel(float x, ACTIVATION a){
  101. switch (a){
  102. case LINEAR:
  103. return linear_gradient_kernel(x);
  104. case LOGISTIC:
  105. return logistic_gradient_kernel(x);
  106. case LOGGY:
  107. return loggy_gradient_kernel(x);
  108. case RELU:
  109. return relu_gradient_kernel(x);
  110. case ELU:
  111. return elu_gradient_kernel(x);
  112. case RELIE:
  113. return relie_gradient_kernel(x);
  114. case RAMP:
  115. return ramp_gradient_kernel(x);
  116. case LEAKY:
  117. return leaky_gradient_kernel(x);
  118. case TANH:
  119. return tanh_gradient_kernel(x);
  120. case PLSE:
  121. return plse_gradient_kernel(x);
  122. case STAIR:
  123. return stair_gradient_kernel(x);
  124. case HARDTAN:
  125. return hardtan_gradient_kernel(x);
  126. case LHTAN:
  127. return lhtan_gradient_kernel(x);
  128. }
  129. return 0;
  130. }
  131. ///*__global__*/void gpuCUDA::activate_array_kernel(float *x, int n, ACTIVATION a){
  132. __global__ void activate_array_kernel(float *x, int n, ACTIVATION a){
  133. int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  134. if (i < n) x[i] = activate_kernel(x[i], a);
  135. }
  136. ///*__global__*/ void gpuCUDA::gradient_array_kernel(float *x, int n, ACTIVATION a, float *delta){
  137. __global__ void gradient_array_kernel(float *x, int n, ACTIVATION a, float *delta){
  138. int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  139. if (i < n) delta[i] *= gradient_kernel(x[i], a);
  140. }
  141. bool gpuCUDA::makeActivateArray(float *x, uint32_t n, ACTIVATION a){
  142. activate_array_kernel << < gridSize(n), BLOCK >> >(x, n, a);
  143. return checkError();
  144. }
  145. bool gpuCUDA::makeGradientArray(float *x, uint32_t n, ACTIVATION a, float *delta){
  146. gradient_array_kernel << < gridSize(n), BLOCK >> >(x, n, a, delta);
  147. return checkError();
  148. }
  149. __global__ void binarize_kernel(float *x, int n, float *binary){
  150. int i = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  151. if (i >= n) return;
  152. binary[i] = (x[i] >= 0) ? 1 : -1;
  153. }
  154. bool gpuCUDA::binarize(float *x, uint32_t n, float *binary){
  155. binarize_kernel <<< gridSize(n), BLOCK >>>(x, n, binary);
  156. return checkError();
  157. };
  158. __global__ void binarize_input_kernel(float *input, int n, int size, float *binary){
  159. int s = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  160. if (s >= size) return;
  161. int i = 0;
  162. float mean = 0;
  163. for (i = 0; i < n; ++i){
  164. mean += abs(input[i*size + s]);
  165. }
  166. mean = mean / n;
  167. for (i = 0; i < n; ++i){
  168. binary[i*size + s] = (input[i*size + s] > 0) ? mean : -mean;
  169. }
  170. }
  171. bool gpuCUDA::binarizeInput(float *input, uint32_t n, uint32_t size, float *binary){
  172. binarize_input_kernel <<< gridSize(size), BLOCK >>>(input, n, size, binary);
  173. return checkError();
  174. }
  175. __global__ void binarize_weights_kernel(float *weights, int n, int size, float *binary){
  176. int f = (blockIdx.x + blockIdx.y*gridDim.x) * blockDim.x + threadIdx.x;
  177. if (f >= n) return;
  178. int i = 0;
  179. float mean = 0;
  180. for (i = 0; i < size; ++i){
  181. mean += abs(weights[f*size + i]);
  182. }
  183. mean = mean / size;
  184. for (i = 0; i < size; ++i){
  185. binary[f*size + i] = (weights[f*size + i] > 0) ? mean : -mean;
  186. //binary[f*size + i] = weights[f*size + i];
  187. }
  188. }
  189. bool gpuCUDA::binarizeWeights(float *weights, uint32_t n, uint32_t size, float *binary){
  190. binarize_weights_kernel <<< gridSize(n), BLOCK >>>(weights, n, size, binary);
  191. return checkError();
  192. };
  193. __global__ void scale_bias_kernel(float *output, float *biases, int n, int size)
  194. {
  195. int offset = blockIdx.x * blockDim.x + threadIdx.x;
  196. int filter = blockIdx.y;
  197. int batch = blockIdx.z;
  198. if (offset < size) output[(batch*n + filter)*size + offset] *= biases[filter];
  199. }
  200. bool gpuCUDA::scaleBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size){
  201. dim3 dimGrid((size - 1) / BLOCK + 1, n, batch);
  202. dim3 dimBlock(BLOCK, 1, 1);
  203. scale_bias_kernel << < dimGrid, dimBlock >> >(output, biases, n, size);
  204. return checkError();
  205. };
  206. __global__ void add_bias_kernel(float *output, float *biases, int n, int size)
  207. {
  208. int offset = blockIdx.x * blockDim.x + threadIdx.x;
  209. int filter = blockIdx.y;
  210. int batch = blockIdx.z;
  211. if (offset < size) output[(batch*n + filter)*size + offset] += biases[filter];
  212. }
  213. bool gpuCUDA::addBias(float *output, float *biases, uint32_t batch, uint32_t n, uint32_t size){
  214. dim3 dimGrid((size - 1) / BLOCK + 1, n, batch);
  215. dim3 dimBlock(BLOCK, 1, 1);
  216. add_bias_kernel << <dimGrid, dimBlock >> >(output, biases, n, size);
  217. return checkError();
  218. };
  219. #undef LOG
  220. #undef LOG_ERROR