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.

432 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include <cuda_runtime.h>
  2. #include <curand.h>
  3. #include <cublas_v2.h>
  4. extern "C" {
  5. #include <stdio.h>
  6. #include <time.h>
  7. #include <assert.h>
  8. #include "network.h"
  9. #include "data.h"
  10. #include "utils.h"
  11. #include "parser.h"
  12. #include "crop_layer.h"
  13. #include "connected_layer.h"
  14. #include "rnn_layer.h"
  15. #include "gru_layer.h"
  16. #include "crnn_layer.h"
  17. #include "detection_layer.h"
  18. #include "region_layer.h"
  19. #include "convolutional_layer.h"
  20. #include "activation_layer.h"
  21. #include "maxpool_layer.h"
  22. #include "reorg_layer.h"
  23. #include "avgpool_layer.h"
  24. #include "normalization_layer.h"
  25. #include "batchnorm_layer.h"
  26. #include "cost_layer.h"
  27. #include "local_layer.h"
  28. #include "softmax_layer.h"
  29. #include "dropout_layer.h"
  30. #include "route_layer.h"
  31. #include "shortcut_layer.h"
  32. #include "blas.h"
  33. #include "dn_cuda.h"
  34. }
  35. namespace DN{
  36. void forward_network_gpu(network net)
  37. {
  38. int i;
  39. for (i = 0; i < net.n; ++i){
  40. net.index = i;
  41. layer l = net.layers[i];
  42. if (l.delta_gpu){
  43. fill_gpu(l.outputs * l.batch, 0, l.delta_gpu, 1);
  44. }
  45. l.forward_gpu(l, net);
  46. net.input_gpu = l.output_gpu;
  47. net.input = l.output;
  48. if (l.truth) {
  49. net.truth_gpu = l.output_gpu;
  50. net.truth = l.output;
  51. }
  52. }
  53. pull_network_output(net);
  54. calc_network_cost(net);
  55. }
  56. void backward_network_gpu(network net)
  57. {
  58. int i;
  59. network orig = net;
  60. for (i = net.n - 1; i >= 0; --i){
  61. layer l = net.layers[i];
  62. if (l.stopbackward) break;
  63. if (i == 0){
  64. net = orig;
  65. }
  66. else{
  67. layer prev = net.layers[i - 1];
  68. net.input = prev.output;
  69. net.delta = prev.delta;
  70. net.input_gpu = prev.output_gpu;
  71. net.delta_gpu = prev.delta_gpu;
  72. }
  73. net.index = i;
  74. l.backward_gpu(l, net);
  75. }
  76. }
  77. void update_network_gpu(network net)
  78. {
  79. cuda_set_device(net.gpu_index);
  80. int i;
  81. update_args a = { 0 };
  82. a.batch = net.batch*net.subdivisions;
  83. a.learning_rate = get_current_rate(net);
  84. a.momentum = net.momentum;
  85. a.decay = net.decay;
  86. a.adam = net.adam;
  87. a.B1 = net.B1;
  88. a.B2 = net.B2;
  89. a.eps = net.eps;
  90. ++*net.t;
  91. a.t = (*net.t);
  92. for (i = 0; i < net.n; ++i){
  93. layer l = net.layers[i];
  94. if (l.update_gpu){
  95. l.update_gpu(l, a);
  96. }
  97. }
  98. }
  99. void harmless_update_network_gpu(network net)
  100. {
  101. cuda_set_device(net.gpu_index);
  102. int i;
  103. for (i = 0; i < net.n; ++i){
  104. layer l = net.layers[i];
  105. if (l.weight_updates_gpu) fill_gpu(l.nweights, 0, l.weight_updates_gpu, 1);
  106. if (l.bias_updates_gpu) fill_gpu(l.nbiases, 0, l.bias_updates_gpu, 1);
  107. if (l.scale_updates_gpu) fill_gpu(l.nbiases, 0, l.scale_updates_gpu, 1);
  108. }
  109. }
  110. float train_network_datum_gpu(network net)
  111. {
  112. *net.seen += net.batch;
  113. int x_size = net.inputs*net.batch;
  114. int y_size = net.truths*net.batch;
  115. cuda_push_array(net.input_gpu, net.input, x_size);
  116. cuda_push_array(net.truth_gpu, net.truth, y_size);
  117. net.train = 1;
  118. forward_network_gpu(net);
  119. backward_network_gpu(net);
  120. float error = *net.cost;
  121. if (((*net.seen) / net.batch) % net.subdivisions == 0) update_network_gpu(net);
  122. return error;
  123. }
  124. typedef struct {
  125. network net;
  126. data d;
  127. float *err;
  128. } train_args;
  129. void *train_thread(void *ptr)
  130. {
  131. train_args args = *(train_args*)ptr;
  132. free(ptr);
  133. cuda_set_device(args.net.gpu_index);
  134. *args.err = train_network(args.net, args.d);
  135. return 0;
  136. }
  137. pthread_t train_network_in_thread(network net, data d, float *err)
  138. {
  139. pthread_t thread;
  140. train_args *ptr = (train_args *)calloc(1, sizeof(train_args));
  141. ptr->net = net;
  142. ptr->d = d;
  143. ptr->err = err;
  144. if (pthread_create(&thread, 0, train_thread, ptr)) error("Thread creation failed");
  145. return thread;
  146. }
  147. void merge_weights(layer l, layer base)
  148. {
  149. if (l.type == CONVOLUTIONAL) {
  150. axpy_cpu(l.n, 1, l.bias_updates, 1, base.biases, 1);
  151. axpy_cpu(l.n*l.size*l.size*l.c, 1, l.weight_updates, 1, base.weights, 1);
  152. if (l.scales) {
  153. axpy_cpu(l.n, 1, l.scale_updates, 1, base.scales, 1);
  154. }
  155. }
  156. else if (l.type == CONNECTED) {
  157. axpy_cpu(l.outputs, 1, l.bias_updates, 1, base.biases, 1);
  158. axpy_cpu(l.outputs*l.inputs, 1, l.weight_updates, 1, base.weights, 1);
  159. }
  160. }
  161. void scale_weights(layer l, float s)
  162. {
  163. if (l.type == CONVOLUTIONAL) {
  164. scal_cpu(l.n, s, l.biases, 1);
  165. scal_cpu(l.n*l.size*l.size*l.c, s, l.weights, 1);
  166. if (l.scales) {
  167. scal_cpu(l.n, s, l.scales, 1);
  168. }
  169. }
  170. else if (l.type == CONNECTED) {
  171. scal_cpu(l.outputs, s, l.biases, 1);
  172. scal_cpu(l.outputs*l.inputs, s, l.weights, 1);
  173. }
  174. }
  175. void pull_weights(layer l)
  176. {
  177. if (l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
  178. cuda_pull_array(l.biases_gpu, l.bias_updates, l.n);
  179. cuda_pull_array(l.weights_gpu, l.weight_updates, l.n*l.size*l.size*l.c);
  180. if (l.scales) cuda_pull_array(l.scales_gpu, l.scale_updates, l.n);
  181. }
  182. else if (l.type == CONNECTED){
  183. cuda_pull_array(l.biases_gpu, l.bias_updates, l.outputs);
  184. cuda_pull_array(l.weights_gpu, l.weight_updates, l.outputs*l.inputs);
  185. }
  186. }
  187. void push_weights(layer l)
  188. {
  189. if (l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
  190. cuda_push_array(l.biases_gpu, l.biases, l.n);
  191. cuda_push_array(l.weights_gpu, l.weights, l.n*l.size*l.size*l.c);
  192. if (l.scales) cuda_push_array(l.scales_gpu, l.scales, l.n);
  193. }
  194. else if (l.type == CONNECTED){
  195. cuda_push_array(l.biases_gpu, l.biases, l.outputs);
  196. cuda_push_array(l.weights_gpu, l.weights, l.outputs*l.inputs);
  197. }
  198. }
  199. void distribute_weights(layer l, layer base)
  200. {
  201. if (l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL) {
  202. cuda_push_array(l.biases_gpu, base.biases, l.n);
  203. cuda_push_array(l.weights_gpu, base.weights, l.n*l.size*l.size*l.c);
  204. if (base.scales) cuda_push_array(l.scales_gpu, base.scales, l.n);
  205. }
  206. else if (l.type == CONNECTED) {
  207. cuda_push_array(l.biases_gpu, base.biases, l.outputs);
  208. cuda_push_array(l.weights_gpu, base.weights, l.outputs*l.inputs);
  209. }
  210. }
  211. /*
  212. void pull_updates(layer l)
  213. {
  214. if(l.type == CONVOLUTIONAL){
  215. cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.n);
  216. cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.n*l.size*l.size*l.c);
  217. if(l.scale_updates) cuda_pull_array(l.scale_updates_gpu, l.scale_updates, l.n);
  218. } else if(l.type == CONNECTED){
  219. cuda_pull_array(l.bias_updates_gpu, l.bias_updates, l.outputs);
  220. cuda_pull_array(l.weight_updates_gpu, l.weight_updates, l.outputs*l.inputs);
  221. }
  222. }
  223. void push_updates(layer l)
  224. {
  225. if(l.type == CONVOLUTIONAL){
  226. cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.n);
  227. cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.n*l.size*l.size*l.c);
  228. if(l.scale_updates) cuda_push_array(l.scale_updates_gpu, l.scale_updates, l.n);
  229. } else if(l.type == CONNECTED){
  230. cuda_push_array(l.bias_updates_gpu, l.bias_updates, l.outputs);
  231. cuda_push_array(l.weight_updates_gpu, l.weight_updates, l.outputs*l.inputs);
  232. }
  233. }
  234. void update_layer(layer l, network net)
  235. {
  236. int update_batch = net.batch*net.subdivisions;
  237. float rate = get_current_rate(net);
  238. l.t = get_current_batch(net);
  239. if(l.update_gpu){
  240. l.update_gpu(l, update_batch, rate*l.learning_rate_scale, net.momentum, net.decay);
  241. }
  242. }
  243. void merge_updates(layer l, layer base)
  244. {
  245. if (l.type == CONVOLUTIONAL) {
  246. axpy_cpu(l.n, 1, l.bias_updates, 1, base.bias_updates, 1);
  247. axpy_cpu(l.n*l.size*l.size*l.c, 1, l.weight_updates, 1, base.weight_updates, 1);
  248. if (l.scale_updates) {
  249. axpy_cpu(l.n, 1, l.scale_updates, 1, base.scale_updates, 1);
  250. }
  251. } else if(l.type == CONNECTED) {
  252. axpy_cpu(l.outputs, 1, l.bias_updates, 1, base.bias_updates, 1);
  253. axpy_cpu(l.outputs*l.inputs, 1, l.weight_updates, 1, base.weight_updates, 1);
  254. }
  255. }
  256. void distribute_updates(layer l, layer base)
  257. {
  258. if(l.type == CONVOLUTIONAL || l.type == DECONVOLUTIONAL){
  259. cuda_push_array(l.bias_updates_gpu, base.bias_updates, l.n);
  260. cuda_push_array(l.weight_updates_gpu, base.weight_updates, l.n*l.size*l.size*l.c);
  261. if(base.scale_updates) cuda_push_array(l.scale_updates_gpu, base.scale_updates, l.n);
  262. } else if(l.type == CONNECTED){
  263. cuda_push_array(l.bias_updates_gpu, base.bias_updates, l.outputs);
  264. cuda_push_array(l.weight_updates_gpu, base.weight_updates, l.outputs*l.inputs);
  265. }
  266. }
  267. */
  268. /*
  269. void sync_layer(network *nets, int n, int j)
  270. {
  271. int i;
  272. network net = nets[0];
  273. layer base = net.layers[j];
  274. scale_weights(base, 0);
  275. for (i = 0; i < n; ++i) {
  276. cuda_set_device(nets[i].gpu_index);
  277. layer l = nets[i].layers[j];
  278. pull_weights(l);
  279. merge_weights(l, base);
  280. }
  281. scale_weights(base, 1./n);
  282. for (i = 0; i < n; ++i) {
  283. cuda_set_device(nets[i].gpu_index);
  284. layer l = nets[i].layers[j];
  285. distribute_weights(l, base);
  286. }
  287. }
  288. */
  289. void sync_layer(network *nets, int n, int j)
  290. {
  291. int i;
  292. network net = nets[0];
  293. layer base = net.layers[j];
  294. scale_weights(base, 0);
  295. for (i = 0; i < n; ++i) {
  296. cuda_set_device(nets[i].gpu_index);
  297. layer l = nets[i].layers[j];
  298. pull_weights(l);
  299. merge_weights(l, base);
  300. }
  301. scale_weights(base, 1. / n);
  302. for (i = 0; i < n; ++i) {
  303. cuda_set_device(nets[i].gpu_index);
  304. layer l = nets[i].layers[j];
  305. distribute_weights(l, base);
  306. }
  307. }
  308. typedef struct{
  309. network *nets;
  310. int n;
  311. int j;
  312. } sync_args;
  313. void *sync_layer_thread(void *ptr)
  314. {
  315. sync_args args = *(sync_args*)ptr;
  316. sync_layer(args.nets, args.n, args.j);
  317. free(ptr);
  318. return 0;
  319. }
  320. pthread_t sync_layer_in_thread(network *nets, int n, int j)
  321. {
  322. pthread_t thread;
  323. sync_args *ptr = (sync_args *)calloc(1, sizeof(sync_args));
  324. ptr->nets = nets;
  325. ptr->n = n;
  326. ptr->j = j;
  327. if (pthread_create(&thread, 0, sync_layer_thread, ptr)) error("Thread creation failed");
  328. return thread;
  329. }
  330. void sync_nets(network *nets, int n, int interval)
  331. {
  332. int j;
  333. int layers = nets[0].n;
  334. pthread_t *threads = (pthread_t *)calloc(layers, sizeof(pthread_t));
  335. *nets[0].seen += interval * (n - 1) * nets[0].batch * nets[0].subdivisions;
  336. for (j = 0; j < n; ++j){
  337. *nets[j].seen = *nets[0].seen;
  338. }
  339. for (j = 0; j < layers; ++j) {
  340. threads[j] = sync_layer_in_thread(nets, n, j);
  341. }
  342. for (j = 0; j < layers; ++j) {
  343. pthread_join(threads[j], 0);
  344. }
  345. free(threads);
  346. }
  347. float train_networks(network *nets, int n, data d, int interval)
  348. {
  349. int i;
  350. int batch = nets[0].batch;
  351. int subdivisions = nets[0].subdivisions;
  352. assert(batch * subdivisions * n == d.X.rows);
  353. pthread_t *threads = (pthread_t *)calloc(n, sizeof(pthread_t));
  354. float *errors = (float *)calloc(n, sizeof(float));
  355. float sum = 0;
  356. for (i = 0; i < n; ++i){
  357. data p = get_data_part(d, i, n);
  358. threads[i] = train_network_in_thread(nets[i], p, errors + i);
  359. }
  360. for (i = 0; i < n; ++i){
  361. pthread_join(threads[i], 0);
  362. //printf("%f\n", errors[i]);
  363. sum += errors[i];
  364. }
  365. //cudaDeviceSynchronize();
  366. if (get_current_batch(nets[0]) % interval == 0) {
  367. printf("Syncing... ");
  368. fflush(stdout);
  369. sync_nets(nets, n, interval);
  370. printf("Done!\n");
  371. }
  372. //cudaDeviceSynchronize();
  373. free(threads);
  374. free(errors);
  375. return (float)sum / (n);
  376. }
  377. void pull_network_output(network net)
  378. {
  379. layer l = get_network_output_layer(net);
  380. cuda_pull_array(l.output_gpu, l.output, l.outputs*l.batch);
  381. }
  382. float *network_predict_gpu(network net, float *input)
  383. {
  384. cuda_set_device(net.gpu_index);
  385. cuda_push_array(net.input_gpu, input, net.inputs*net.batch);
  386. net.truth = 0;
  387. net.train = 0;
  388. forward_network_gpu(net);
  389. return net.output;
  390. }
  391. };