-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
cwc_convnet.cu
2373 lines (2306 loc) · 121 KB
/
cwc_convnet.cu
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <cuda.h>
#include <cublas_v2.h>
extern "C" {
#include "cwc.h"
#include "cwc_internal.h"
#include "../ccv_internal.h"
#ifdef HAVE_GSL
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#endif
#include "cwc_ext.h"
}
#include "../3rdparty/sqlite3/sqlite3.h"
#include "../inc/ccv_convnet_internal.h"
#define MAX_DEVICE_SUPPORT (4)
// this structure holds intermediate on-device memory representation of convnet
typedef struct {
// on host
struct {
float* input; // input per batch
int* c; // class
float* out; // confidence score
float** dor; // dropout regulator, in this version I generate dor on CPU because it is lightweight and gsl has shuffle method, which is better suited for this (and faster than per-node randomization)
} host[MAX_DEVICE_SUPPORT];
// on device
struct {
// this is modeled after Alex's "One Weird Trick", there are 3 join points for me: 1). forward pass from data parallelism to model parallelism; 2). compute logistic loss; 3). backward pass from model parallelism to data parallelism;
cudaStream_t data_stream; // based on above description, we need 3 streams, one stream for data parallelism
cudaStream_t model_stream[2]; // two streams for model parallelism (to overlap data transfer and computation)
// based on above description, we need 6 events (3 join points):
// data_joint: in forward pass, when data parallelism is done, and model parallelism will start;
// model_joint[0]: in forward pass, the first stream's model parallelism is done;
// model_joint[1]: in forward pass, the second stream's model parallelism is done;
cudaEvent_t data_joint;
cudaEvent_t model_joint[2];
cublasHandle_t data_cublas; // the same, just cublas handle to stream
cublasHandle_t model_cublas[2]; // the same, just cublas handle to stream
float* input;
int* c;
float* out;
float** dor;
// events to synchronize a round
cudaEvent_t start_timing;
cudaEvent_t stop_timing;
} device[MAX_DEVICE_SUPPORT];
} cwc_convnet_context_t;
typedef struct {
size_t memory_usage;
} cwc_convnet_stats_t;
typedef struct {
int batch;
int tops;
int device_count;
ccv_convnet_layer_train_param_t* layer_params;
struct {
ccv_convnet_layer_t* layers;
ccv_convnet_layer_t* configurations;
ccv_convnet_layer_t* momentums;
float** forwards; // the forward output layers
float** backwards; // the backwards output layer
float** denoms; // the denominator for rnorm layer, thus, backprop can reuse the value
float** scans; // the scan layer to reformat outputs
float* unit; // the unit vector for a batch, ease the GEMM on full-connect layer
float* scratch; // the scratch space for temporary reuse, it will be max(wnum, input rows * cols * channels + output rows * cols * channels)
int can_access_peer[MAX_DEVICE_SUPPORT];
} device[MAX_DEVICE_SUPPORT];
cwc_convnet_context_t contexts[2];
cwc_convnet_stats_t stats;
} cwc_convnet_t;
#define GPU(x) ((cwc_convnet_t*)((x)->reserved))
static void _cwc_convnet_reorder_convolutional_weights_onto_device(float* w, float* ow, int wnum, int filters, int channels, int channel_partition)
{
int channels_per_partition = channels / channel_partition;
assert(wnum % (filters * channels_per_partition) == 0);
float* iw = (float*)ccmalloc(sizeof(float) * wnum);
int count = wnum / (filters * channels_per_partition);
int i, j, k;
for (i = 0; i < channels_per_partition; i++)
for (j = 0; j < count; j++)
for (k = 0; k < filters; k++)
iw[i * count * filters + j * filters + k] = w[k * count * channels_per_partition + j * channels_per_partition + i];
cudaMemcpy(ow, iw, sizeof(float) * wnum, cudaMemcpyHostToDevice);
ccfree(iw);
}
static void _cwc_convnet_reorder_full_connect_weights_onto_device(float* w, float* ow, int wnum, int count, int channels)
{
assert(wnum % (count * channels) == 0);
float* iw = (float*)ccmalloc(sizeof(float) * wnum);
int rows = wnum / (count * channels);
int i, j, k;
for (i = 0; i < rows; i++)
for (j = 0; j < channels; j++)
for (k = 0; k < count; k++)
iw[i * channels * count + j * count + k] = w[i * channels * count + k * channels + j];
cudaMemcpy(ow, iw, sizeof(float) * wnum, cudaMemcpyHostToDevice);
ccfree(iw);
}
static void _cwc_convnet_alloc_layers(ccv_convnet_t* convnet, int device_id, int device_count)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
assert(device_id < device_count);
for (i = 0; i < convnet->count; i++)
switch (layers[i].type)
{
case CCV_CONVNET_CONVOLUTIONAL:
// allocating for layer
layers[i].w = 0;
cudaMalloc(&layers[i].w, sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count));
GPU(convnet)->stats.memory_usage += sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count);
assert(layers[i].w);
layers[i].bias = layers[i].w + layers[i].wnum;
_cwc_convnet_reorder_convolutional_weights_onto_device(convnet->layers[i].w, layers[i].w, layers[i].wnum, layers[i].net.convolutional.count, layers[i].net.convolutional.channels, layers[i].input.matrix.partition);
cudaMemcpy(layers[i].bias, convnet->layers[i].bias, sizeof(float) * layers[i].net.convolutional.count, cudaMemcpyHostToDevice);
break;
case CCV_CONVNET_FULL_CONNECT:
assert(i > 0);
// allocating for layer
layers[i].w = 0;
cudaMalloc(&layers[i].w, sizeof(float) * (layers[i].wnum + layers[i].net.full_connect.count));
GPU(convnet)->stats.memory_usage += sizeof(float) * (layers[i].wnum + layers[i].net.full_connect.count);
assert(layers[i].w);
layers[i].bias = layers[i].w + layers[i].wnum;
// if it is due device, rewind it from different parts of the model
_cwc_convnet_reorder_full_connect_weights_onto_device(convnet->layers[i].w + device_id * layers[i].wnum, layers[i].w, layers[i].wnum, layers[i].input.matrix.rows * layers[i].input.matrix.cols, layers[i].input.matrix.channels);
cudaMemcpy(layers[i].bias, convnet->layers[i].bias + device_id * layers[i].net.full_connect.count, sizeof(float) * layers[i].net.full_connect.count, cudaMemcpyHostToDevice);
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
assert(i > 0);
layers[i].w = layers[i].bias = 0;
break;
}
}
static void _cwc_convnet_alloc_configurations(ccv_convnet_t* convnet, int device_id, int device_count)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
ccv_convnet_layer_t* configurations = GPU(convnet)->device[device_id].configurations;
for (i = 0; i < convnet->count; i++)
switch (layers[i].type)
{
case CCV_CONVNET_CONVOLUTIONAL:
assert(configurations[i].type == CCV_CONVNET_CONVOLUTIONAL);
// allocating for configurations
configurations[i].w = 0;
cudaMalloc(&configurations[i].w, sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count));
cudaMemset(configurations[i].w, 0, sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count));
GPU(convnet)->stats.memory_usage += sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count);
assert(configurations[i].w);
configurations[i].bias = configurations[i].w + layers[i].wnum;
break;
case CCV_CONVNET_FULL_CONNECT:
assert(i > 0);
assert(configurations[i].type == CCV_CONVNET_FULL_CONNECT);
// allocating for configurations
configurations[i].w = 0;
// need to allocate two because we duplex backprop, therefore, need double-buffer for multiple devices
cudaMalloc(&configurations[i].w, sizeof(float) * (device_count > 1 ? 2 : 1) * (layers[i].wnum + layers[i].net.full_connect.count));
cudaMemset(configurations[i].w, 0, sizeof(float) * (device_count > 1 ? 2 : 1) * (layers[i].wnum + layers[i].net.full_connect.count));
GPU(convnet)->stats.memory_usage += sizeof(float) * (device_count > 1 ? 2 : 1) * (layers[i].wnum + layers[i].net.full_connect.count);
assert(configurations[i].w);
configurations[i].bias = configurations[i].w + layers[i].wnum * (device_count > 1 ? 2 : 1);
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
assert(i > 0);
assert(configurations[i].type == layers[i].type);
configurations[i].w = configurations[i].bias = 0;
break;
}
}
static void _cwc_convnet_alloc_momentums(ccv_convnet_t* convnet, int device_id)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
ccv_convnet_layer_t* momentums = GPU(convnet)->device[device_id].momentums;
for (i = 0; i < convnet->count; i++)
switch (layers[i].type)
{
case CCV_CONVNET_CONVOLUTIONAL:
assert(momentums[i].type == CCV_CONVNET_CONVOLUTIONAL);
// allocating for momentums
momentums[i].w = 0;
cudaMalloc(&momentums[i].w, sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count));
cudaMemset(momentums[i].w, 0, sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count));
GPU(convnet)->stats.memory_usage += sizeof(float) * (layers[i].wnum + layers[i].net.convolutional.count);
assert(momentums[i].w);
momentums[i].bias = momentums[i].w + layers[i].wnum;
break;
case CCV_CONVNET_FULL_CONNECT:
assert(i > 0);
assert(momentums[i].type == CCV_CONVNET_FULL_CONNECT);
// allocating for momentums
momentums[i].w = 0;
cudaMalloc(&momentums[i].w, sizeof(float) * (layers[i].wnum + layers[i].net.full_connect.count));
cudaMemset(momentums[i].w, 0, sizeof(float) * (layers[i].wnum + layers[i].net.full_connect.count));
GPU(convnet)->stats.memory_usage += sizeof(float) * (layers[i].wnum + layers[i].net.full_connect.count);
assert(momentums[i].w);
momentums[i].bias = momentums[i].w + layers[i].wnum;
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
assert(i > 0);
assert(momentums[i].type == layers[i].type);
momentums[i].w = momentums[i].bias = 0;
break;
}
}
static void _cwc_convnet_alloc_forwards(ccv_convnet_t* convnet, int device_id, int device_count, int start, int length, int rows, int cols, int batch)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
assert(start >= 0 && start + length <= convnet->count);
int out_rows, out_cols, out_partition;
for (i = start; i < start + length; i++)
{
ccv_convnet_make_output(layers + i, rows, cols, &out_rows, &out_cols, &out_partition);
// if the next layer is full connect (model parallelism), the forwards neuron needs to hold all batches rather than 1
int multi_batch = (i + 1 < start + length && layers[i + 1].type == CCV_CONVNET_FULL_CONNECT) ? batch * device_count : batch;
switch (layers[i].type)
{
case CCV_CONVNET_CONVOLUTIONAL:
GPU(convnet)->device[device_id].forwards[i] = 0;
cudaMalloc(&GPU(convnet)->device[device_id].forwards[i], sizeof(float) * out_rows * out_cols * layers[i].net.convolutional.count * multi_batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * out_rows * out_cols * layers[i].net.convolutional.count * multi_batch;
assert(GPU(convnet)->device[device_id].forwards[i]);
break;
case CCV_CONVNET_FULL_CONNECT:
assert(i > 0);
GPU(convnet)->device[device_id].forwards[i] = 0;
cudaMalloc(&GPU(convnet)->device[device_id].forwards[i], sizeof(float) * layers[i].net.full_connect.count * batch * device_count * device_count);
GPU(convnet)->stats.memory_usage += sizeof(float) * layers[i].net.full_connect.count * batch * device_count * device_count;
assert(GPU(convnet)->device[device_id].forwards[i]);
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
assert(i > 0);
GPU(convnet)->device[device_id].forwards[i] = 0;
cudaMalloc(&GPU(convnet)->device[device_id].forwards[i], sizeof(float) * out_rows * out_cols * layers[i].input.matrix.channels * multi_batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * out_rows * out_cols * layers[i].input.matrix.channels * multi_batch;
assert(GPU(convnet)->device[device_id].forwards[i]);
break;
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
assert(i > 0);
GPU(convnet)->device[device_id].forwards[i] = 0;
cudaMalloc(&GPU(convnet)->device[device_id].forwards[i], sizeof(float) * out_rows * out_cols * layers[i].input.matrix.channels * multi_batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * out_rows * out_cols * layers[i].input.matrix.channels * multi_batch;
assert(GPU(convnet)->device[device_id].forwards[i]);
break;
}
rows = out_rows, cols = out_cols;
}
}
static void _cwc_convnet_alloc_denoms(ccv_convnet_t* convnet, int device_id, int start, int length, int rows, int cols, int batch)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
assert(start >= 0 && start + length <= convnet->count);
int out_rows, out_cols, out_partition;
for (i = start; i < start + length; i++)
{
ccv_convnet_make_output(layers + i, rows, cols, &out_rows, &out_cols, &out_partition);
switch (layers[i].type)
{
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
GPU(convnet)->device[device_id].denoms[i] = 0;
cudaMalloc(&GPU(convnet)->device[device_id].denoms[i], sizeof(float) * out_rows * out_cols * layers[i].input.matrix.channels * batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * out_rows * out_cols * layers[i].input.matrix.channels * batch;
assert(GPU(convnet)->device[device_id].denoms[i]);
break;
case CCV_CONVNET_CONVOLUTIONAL:
case CCV_CONVNET_FULL_CONNECT:
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
GPU(convnet)->device[device_id].denoms[i] = 0;
break;
}
rows = out_rows, cols = out_cols;
}
}
static void _cwc_convnet_alloc_backwards(ccv_convnet_t* convnet, int device_id, int device_count, int batch)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
// find the layer with max memory usage, and then allocate two of them, because for backward propagate, no need to preserve the results
size_t max_memory_usage[2] = {0};
for (i = 1; i < convnet->count; i++)
switch (layers[i].type)
{
case CCV_CONVNET_CONVOLUTIONAL:
max_memory_usage[i % 2] = ccv_max(max_memory_usage[i % 2], sizeof(float) * layers[i].input.matrix.rows * layers[i].input.matrix.cols * layers[i].input.matrix.channels * batch);
break;
case CCV_CONVNET_FULL_CONNECT:
assert(i > 0);
// for full connect layer, because it uses model parallelism, each layer needs to hold multiple batches
max_memory_usage[i % 2] = ccv_max(max_memory_usage[i % 2], sizeof(float) * layers[i].input.matrix.rows * layers[i].input.matrix.cols * layers[i].input.matrix.channels * batch * device_count);
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
max_memory_usage[i % 2] = ccv_max(max_memory_usage[i % 2], sizeof(float) * layers[i].input.matrix.rows * layers[i].input.matrix.cols * layers[i].input.matrix.channels * batch);
break;
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
assert(i > 0);
max_memory_usage[i % 2] = ccv_max(max_memory_usage[i % 2], sizeof(float) * layers[i].input.matrix.rows * layers[i].input.matrix.cols * layers[i].input.matrix.channels * batch);
break;
}
assert(convnet->count > 2);
// allocate two layers
GPU(convnet)->device[device_id].backwards[0] = 0;
for (i = 1; i < 3; i++)
{
GPU(convnet)->device[device_id].backwards[i] = 0;
cudaMalloc(&GPU(convnet)->device[device_id].backwards[i], max_memory_usage[i % 2]);
GPU(convnet)->stats.memory_usage += max_memory_usage[i % 2];
assert(GPU(convnet)->device[device_id].backwards[i]);
}
for (i = 3; i < convnet->count; i += 2)
GPU(convnet)->device[device_id].backwards[i] = GPU(convnet)->device[device_id].backwards[1];
for (i = 4; i < convnet->count; i += 2)
GPU(convnet)->device[device_id].backwards[i] = GPU(convnet)->device[device_id].backwards[2];
}
static void _cwc_convnet_alloc_dor(ccv_convnet_t* convnet, int device_id, int batch, ccv_convnet_layer_train_param_t* layer_params)
{
int i, j;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
int rows = convnet->rows;
int cols = convnet->cols;
int out_rows, out_cols, out_partition;
for (i = 0; i < convnet->count; i++)
{
ccv_convnet_make_output(layers + i, rows, cols, &out_rows, &out_cols, &out_partition);
switch (layers[i].type)
{
case CCV_CONVNET_CONVOLUTIONAL:
for (j = 0; j < 2; j++)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + j;
context->host[device_id].dor[i] = 0;
context->device[device_id].dor[i] = 0;
if (layer_params && layer_params[i].dor > 0)
{
assert(i > 0);
cudaMallocHost(&context->host[device_id].dor[i], sizeof(float) * batch * out_rows * out_cols * layers[i].net.convolutional.count);
assert(context->host[device_id].dor[i]);
cudaMalloc(&context->device[device_id].dor[i], sizeof(float) * batch * out_rows * out_cols * layers[i].net.convolutional.count);
GPU(convnet)->stats.memory_usage += sizeof(float) * batch * out_rows * out_cols * layers[i].net.convolutional.count;
assert(context->device[device_id].dor[i]);
}
}
break;
case CCV_CONVNET_FULL_CONNECT:
for (j = 0; j < 2; j++)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + j;
context->host[device_id].dor[i] = 0;
context->device[device_id].dor[i] = 0;
if (layer_params && layer_params[i].dor > 0)
{
cudaMallocHost(&context->host[device_id].dor[i], sizeof(float) * batch * layers[i].net.full_connect.count);
assert(context->host[device_id].dor[i]);
cudaMalloc(&context->device[device_id].dor[i], sizeof(float) * batch * layers[i].net.full_connect.count);
GPU(convnet)->stats.memory_usage += sizeof(float) * batch * layers[i].net.full_connect.count;
assert(context->device[device_id].dor[i]);
}
}
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
case CCV_CONVNET_MAX_POOL:
case CCV_CONVNET_AVERAGE_POOL:
for (j = 0; j < 2; j++)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + j;
context->host[device_id].dor[i] = 0;
context->device[device_id].dor[i] = 0;
}
break;
}
rows = out_rows, cols = out_cols;
}
}
static void _cwc_convnet_alloc_input(ccv_convnet_t* convnet, int device_id, int context_id, int rows, int cols, int batch)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + context_id;
context->host[device_id].input = 0;
cudaMallocHost(&context->host[device_id].input, sizeof(float) * rows * cols * convnet->channels * batch);
assert(context->host[device_id].input);
context->device[device_id].input = 0;
cudaMalloc(&context->device[device_id].input, sizeof(float) * rows * cols * convnet->channels * batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * rows * cols * convnet->channels * batch;
assert(context->device[device_id].input);
}
static void _cwc_convnet_alloc_c(ccv_convnet_t* convnet, int device_id, int context_id, int batch)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + context_id;
context->host[device_id].c = 0;
cudaMallocHost(&context->host[device_id].c, sizeof(int) * batch);
assert(context->host[device_id].c);
context->device[device_id].c = 0;
cudaMalloc(&context->device[device_id].c, sizeof(int) * batch);
GPU(convnet)->stats.memory_usage += sizeof(int) * batch;
}
static void _cwc_convnet_alloc_out(ccv_convnet_t* convnet, int device_id, int context_id, int batch)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + context_id;
context->host[device_id].out = 0;
cudaMallocHost(&context->host[device_id].out, sizeof(float) * batch);
assert(context->host[device_id].out);
context->device[device_id].out = 0;
cudaMalloc(&context->device[device_id].out, sizeof(float) * batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * batch;
}
static void _cwc_convnet_alloc_context(ccv_convnet_t* convnet, int device_id, int context_id, int device_count)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + context_id;
cudaStreamCreate(&context->device[device_id].data_stream);
cublasCreate(&context->device[device_id].data_cublas);
cublasSetStream(context->device[device_id].data_cublas, context->device[device_id].data_stream);
cudaEventCreate(&context->device[device_id].start_timing);
cudaEventCreate(&context->device[device_id].stop_timing);
int i;
if (device_count > 1)
{
// only allocate model parallelism stream / cublas handle / joint events when dual device mode is on
for (i = 0; i < 2; i++)
{
cudaStreamCreate(&context->device[device_id].model_stream[i]);
cublasCreate(&context->device[device_id].model_cublas[i]);
cublasSetStream(context->device[device_id].model_cublas[i], context->device[device_id].model_stream[i]);
cudaEventCreateWithFlags(&context->device[device_id].model_joint[i], cudaEventDisableTiming);
}
cudaEventCreateWithFlags(&context->device[device_id].data_joint, cudaEventDisableTiming);
} else {
for (i = 0; i < 2; i++)
{
context->device[device_id].model_stream[i] = 0;
context->device[device_id].model_cublas[i] = 0;
context->device[device_id].model_joint[i] = 0;
}
context->device[device_id].data_joint = 0;
}
}
static void _cwc_convnet_alloc_scratch(ccv_convnet_t* convnet, int device_id, int device_count, int batch)
{
int i;
int out_rows, out_cols, out_partition;
size_t scratch_space = 6; // for debugging and computing the stats
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
for (i = 0; i < convnet->count; i++)
if (layers[i].type == CCV_CONVNET_CONVOLUTIONAL)
{
int use_rows = cwc_convnet_layer_use_rows(layers + i);
ccv_convnet_make_output(layers + i, layers[i].input.matrix.rows, layers[i].input.matrix.cols, &out_rows, &out_cols, &out_partition);
scratch_space = ccv_max(scratch_space, ccv_max(layers[i].wnum, layers[i].net.convolutional.count));
scratch_space = ccv_max(scratch_space,
(size_t)out_rows * out_cols * layers[i].net.convolutional.count * batch + // output layer reorder
layers[i].input.matrix.rows * layers[i].input.matrix.cols * layers[i].input.matrix.channels * batch + // input layer reorder
layers[i].wnum * (use_rows ? out_rows : 1) * (batch / BATCH_PER_BLOCK)); // unconsolidated weights output
} else if (layers[i].type == CCV_CONVNET_FULL_CONNECT && device_count > 1)
// for multiple device only, when we need it to copy over outputs for backprop
scratch_space = ccv_max(scratch_space, ccv_max(layers[i].input.node.count * batch, layers[i].net.full_connect.count * batch * 2));
GPU(convnet)->device[device_id].scratch = 0;
cudaMalloc(&GPU(convnet)->device[device_id].scratch, sizeof(float) * scratch_space);
assert(GPU(convnet)->device[device_id].scratch);
GPU(convnet)->stats.memory_usage += sizeof(float) * scratch_space;
}
static void _cwc_convnet_make_unit(ccv_convnet_t* convnet, int device_id, int batch)
{
int i;
int out_rows, out_cols, out_partition;
size_t unit_size = batch;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
for (i = 0; i < convnet->count; i++)
if (layers[i].type == CCV_CONVNET_CONVOLUTIONAL)
{
ccv_convnet_make_output(layers + i, layers[i].input.matrix.rows, layers[i].input.matrix.cols, &out_rows, &out_cols, &out_partition);
if (cwc_convnet_layer_use_rows(layers + i))
unit_size = ccv_max(unit_size, out_rows * (batch / BATCH_PER_BLOCK));
unit_size = ccv_max(unit_size, out_rows * out_cols * batch);
}
float* unit = 0;
cudaMallocHost(&unit, sizeof(float) * unit_size);
for (i = 0; i < unit_size; i++)
unit[i] = 1;
GPU(convnet)->device[device_id].unit = 0;
cudaMalloc(&GPU(convnet)->device[device_id].unit, sizeof(float) * unit_size);
GPU(convnet)->stats.memory_usage += sizeof(float) * unit_size;
cudaMemcpy(GPU(convnet)->device[device_id].unit, unit, sizeof(float) * unit_size, cudaMemcpyHostToDevice);
cudaFreeHost(unit);
}
static void _cwc_convnet_alloc_scans(ccv_convnet_t* convnet, int device_id, int offset, int batch)
{
int i;
for (i = 0; i < convnet->count; i++)
{
GPU(convnet)->device[device_id].scans[i] = 0;
if (i == offset)
{
ccv_convnet_layer_t* layer = GPU(convnet)->device[device_id].layers + offset + 1;
cudaMalloc(&GPU(convnet)->device[device_id].scans[i], sizeof(float) * layer->input.matrix.rows * layer->input.matrix.cols * layer->input.matrix.channels * batch);
GPU(convnet)->stats.memory_usage += sizeof(float) * layer->input.matrix.rows * layer->input.matrix.cols * layer->input.matrix.channels * batch;
assert(GPU(convnet)->device[device_id].scans[i]);
}
}
}
// find the layer for scanning (it is the last convolutional layer)
static int _cwc_convnet_find_scan(ccv_convnet_t* convnet, int device_id)
{
int i;
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
for (i = convnet->count - 1; i >= 0; i--)
if (layers[i].type == CCV_CONVNET_CONVOLUTIONAL)
return i;
return -1;
}
// allocate reserved for only forward path, this is only interesting to single-device mode
static void _cwc_convnet_alloc_reserved_for_classify(ccv_convnet_t* convnet, int tops, int batch)
{
if (GPU(convnet) && (GPU(convnet)->batch != batch || GPU(convnet)->tops != tops || GPU(convnet)->layer_params != 0))
ccv_convnet_compact(convnet);
else if (GPU(convnet))
return; // it is allocated properly, no-op
convnet->reserved = (cwc_convnet_t*)ccmalloc(sizeof(cwc_convnet_t) + sizeof(cwc_convnet_layer_t) * convnet->count + sizeof(ccv_convnet_layer_t) * convnet->count + sizeof(float*) * convnet->count * 3);
GPU(convnet)->batch = batch;
GPU(convnet)->tops = tops;
GPU(convnet)->device_count = 1;
GPU(convnet)->layer_params = 0;
GPU(convnet)->stats.memory_usage = 0;
cwc_convnet_layer_t* layer_extra = (cwc_convnet_layer_t*)(GPU(convnet) + 1);
memset(layer_extra, 0, sizeof(cwc_convnet_layer_t) * convnet->count);
GPU(convnet)->device[0].layers = (ccv_convnet_layer_t*)(layer_extra + convnet->count);
memcpy(GPU(convnet)->device[0].layers, convnet->layers, sizeof(ccv_convnet_layer_t) * convnet->count);
ccv_convnet_layer_t* layers = GPU(convnet)->device[0].layers;
// point reserved place to layer_vary
int i;
for (i = 0; i < convnet->count; i++)
layers[i].reserved = layer_extra + i;
// alloc and copy layers
_cwc_convnet_alloc_layers(convnet, 0, 1);
GPU(convnet)->device[0].configurations = 0;
GPU(convnet)->device[0].momentums = 0;
GPU(convnet)->device[0].scratch = 0;
_cwc_convnet_make_unit(convnet, 0, batch * 30);
int scan = _cwc_convnet_find_scan(convnet, 0);
GPU(convnet)->device[0].forwards = (float**)(GPU(convnet)->device[0].layers + convnet->count);
// alloc forwards until the scan layer (for initial 6 patches)
_cwc_convnet_alloc_forwards(convnet, 0, 1, 0, scan + 1, convnet->input.height, convnet->input.width, batch * 6);
// alloc forwards from scan layer (for scanned 30 patches)
_cwc_convnet_alloc_forwards(convnet, 0, 1, scan + 1, convnet->count - scan - 1, GPU(convnet)->device[0].layers[scan + 1].input.matrix.rows, GPU(convnet)->device[0].layers[scan + 1].input.matrix.cols, batch * 30);
GPU(convnet)->device[0].denoms = (float**)(GPU(convnet)->device[0].layers + convnet->count) + convnet->count;
// alloc until the scan layer
_cwc_convnet_alloc_denoms(convnet, 0, 0, scan + 1, convnet->input.height, convnet->input.width, batch * 6);
// alloc denoms from scan layer to the end
_cwc_convnet_alloc_denoms(convnet, 0, scan + 1, convnet->count - scan - 1, GPU(convnet)->device[0].layers[scan + 1].input.matrix.rows, GPU(convnet)->device[0].layers[scan + 1].input.matrix.cols, batch * 30);
// alloc scan layer
GPU(convnet)->device[0].scans = (float**)(GPU(convnet)->device[0].layers + convnet->count) + convnet->count * 2;
_cwc_convnet_alloc_scans(convnet, 0, scan, batch * 30);
GPU(convnet)->device[0].backwards = 0;
GPU(convnet)->contexts[0].host[0].dor = GPU(convnet)->contexts[0].device[0].dor = 0;
_cwc_convnet_alloc_input(convnet, 0, 0, convnet->input.height, convnet->input.width, batch * 6);
_cwc_convnet_alloc_c(convnet, 0, 0, batch * tops);
_cwc_convnet_alloc_out(convnet, 0, 0, batch * tops);
_cwc_convnet_alloc_context(convnet, 0, 0, 1);
GPU(convnet)->contexts[1].host[0].dor = GPU(convnet)->contexts[1].device[0].dor = 0;
GPU(convnet)->contexts[1].host[0].input = GPU(convnet)->contexts[1].device[0].input = 0;
GPU(convnet)->contexts[1].host[0].c = GPU(convnet)->contexts[1].device[0].c = 0;
GPU(convnet)->contexts[1].host[0].out = GPU(convnet)->contexts[1].device[0].out = 0;
GPU(convnet)->contexts[1].device[0].data_stream = 0;
GPU(convnet)->contexts[1].device[0].data_cublas = 0;
GPU(convnet)->contexts[1].device[0].data_joint = 0;
for (i = 0; i < 2; i++)
{
GPU(convnet)->contexts[1].device[0].model_stream[i] = 0;
GPU(convnet)->contexts[1].device[0].model_cublas[i] = 0;
GPU(convnet)->contexts[1].device[0].model_joint[i] = 0;
}
}
// allocate reserved for both forward and backward path
static void _cwc_convnet_alloc_reserved_both(ccv_convnet_t* convnet, int batch, int device_count, ccv_convnet_layer_train_param_t* layer_params)
{
if (GPU(convnet) && (GPU(convnet)->batch != batch || GPU(convnet)->tops != 0 || GPU(convnet)->device_count != device_count || GPU(convnet)->layer_params != layer_params))
ccv_convnet_compact(convnet);
else if (GPU(convnet))
return; // it is allocated properly, no-op
uint8_t* reserved = (uint8_t*)ccmalloc(sizeof(cwc_convnet_t) + (sizeof(cwc_convnet_layer_t) * convnet->count + sizeof(ccv_convnet_layer_t) * convnet->count * 3 + sizeof(float*) * convnet->count * 10) * device_count);
convnet->reserved = (cwc_convnet_t*)reserved;
GPU(convnet)->batch = batch;
GPU(convnet)->tops = 0;
GPU(convnet)->device_count = device_count;
GPU(convnet)->layer_params = layer_params;
GPU(convnet)->stats.memory_usage = 0;
int i, device_id;
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
GPU(convnet)->device[device_id].scans = 0;
for (i = 0; i < device_count; i++)
GPU(convnet)->device[device_id].can_access_peer[i] = (i == device_id); // init to it can access itself
cwc_convnet_layer_t* layer_extra = (cwc_convnet_layer_t*)(reserved + sizeof(cwc_convnet_t) + (sizeof(cwc_convnet_layer_t) * convnet->count + sizeof(ccv_convnet_layer_t) * convnet->count * 3 + sizeof(float*) * convnet->count * 10) * device_id);
memset(layer_extra, 0, sizeof(cwc_convnet_layer_t) * convnet->count);
GPU(convnet)->device[device_id].layers = (ccv_convnet_layer_t*)(layer_extra + convnet->count);
memcpy(GPU(convnet)->device[device_id].layers, convnet->layers, sizeof(ccv_convnet_layer_t) * convnet->count);
ccv_convnet_layer_t* layers = GPU(convnet)->device[device_id].layers;
for (i = 0; i < convnet->count; i++)
{
// point reserved place to layer_extra
layers[i].reserved = layer_extra + i;
// depends on if it is device_count or not, full_connect will use model parallelism, therefore, here we split the model into half
if (layers[i].type == CCV_CONVNET_FULL_CONNECT)
{
assert(convnet->layers[i].net.full_connect.count % device_count == 0);
layers[i].net.full_connect.count = convnet->layers[i].net.full_connect.count / device_count;
layers[i].wnum = layers[i].net.full_connect.count * layers[i].input.node.count;
}
}
// hook up configurations (the backprop coefficients)
GPU(convnet)->device[device_id].configurations = GPU(convnet)->device[device_id].layers + convnet->count;
memcpy(GPU(convnet)->device[device_id].configurations, layers, sizeof(ccv_convnet_layer_t) * convnet->count);
// hook up momentums
GPU(convnet)->device[device_id].momentums = GPU(convnet)->device[device_id].layers + convnet->count * 2;
memcpy(GPU(convnet)->device[device_id].momentums, layers, sizeof(ccv_convnet_layer_t) * convnet->count);
// alloc and copy layers
_cwc_convnet_alloc_layers(convnet, device_id, device_count);
// alloc scratch space (for backprop on convolutional layer)
_cwc_convnet_alloc_scratch(convnet, device_id, device_count, batch);
// alloc and make unit vector
_cwc_convnet_make_unit(convnet, device_id, batch);
// alloc & copy configurations (the backprop coefficients)
_cwc_convnet_alloc_configurations(convnet, device_id, device_count);
// alloc & copy momentums
_cwc_convnet_alloc_momentums(convnet, device_id);
// hook up forwards and alloc forwards
GPU(convnet)->device[device_id].forwards = (float**)(GPU(convnet)->device[device_id].layers + convnet->count * 3);
_cwc_convnet_alloc_forwards(convnet, device_id, device_count, 0, convnet->count, convnet->rows, convnet->cols, batch);
// hook up denoms and alloc denoms
GPU(convnet)->device[device_id].denoms = (float**)(GPU(convnet)->device[device_id].layers + convnet->count * 3) + convnet->count * 2;
_cwc_convnet_alloc_denoms(convnet, device_id, 0, convnet->count, convnet->rows, convnet->cols, batch);
// hook up backwards and alloc backwards
GPU(convnet)->device[device_id].backwards = (float**)(GPU(convnet)->device[device_id].layers + convnet->count * 3) + convnet->count;
// hook up dor and alloc dor
_cwc_convnet_alloc_backwards(convnet, device_id, device_count, batch);
for (i = 0; i < 2; i++)
{
cwc_convnet_context_t* context = GPU(convnet)->contexts + i;
context->host[device_id].dor = (float**)(GPU(convnet)->device[device_id].layers + convnet->count * 3) + convnet->count * 3 + convnet->count * i;
context->device[device_id].dor = (float**)(GPU(convnet)->device[device_id].layers + convnet->count * 3) + convnet->count * 5 + convnet->count * i;
}
_cwc_convnet_alloc_dor(convnet, device_id, batch, layer_params);
// alloc contexts
for (i = 0; i < 2; i++)
{
_cwc_convnet_alloc_input(convnet, device_id, i, convnet->rows, convnet->cols, batch);
_cwc_convnet_alloc_c(convnet, device_id, i, batch);
GPU(convnet)->contexts[i].host[device_id].out = 0;
GPU(convnet)->contexts[i].device[device_id].out = 0;
_cwc_convnet_alloc_context(convnet, device_id, i, device_count);
}
}
}
static void _cwc_convnet_enable_peer_access(ccv_convnet_t* convnet, int device_count)
{
int device_id, other_device_id;
for (device_id = 0; device_id < device_count; device_id++)
for (other_device_id = 0; other_device_id < device_count; other_device_id++)
if (device_id != other_device_id)
{
cudaSetDevice(device_id);
GPU(convnet)->device[device_id].can_access_peer[other_device_id] = 0;
assert(cudaSuccess == cudaDeviceCanAccessPeer(&GPU(convnet)->device[device_id].can_access_peer[other_device_id], device_id, other_device_id));
if (GPU(convnet)->device[device_id].can_access_peer[other_device_id]) // only enable peer access when can access peer
cudaDeviceEnablePeerAccess(other_device_id, 0);
} else
GPU(convnet)->device[device_id].can_access_peer[other_device_id] = 1; // of course
}
// =========================================== KERNEL CODE ===================================================
__global__ static void _cwc_kern_mute_neuron(float* a, float* d)
{
a += blockIdx.x * blockDim.x;
d += blockIdx.x * blockDim.x;
const int thidx = threadIdx.x;
a[thidx] = a[thidx] * d[thidx];
}
static void _cwc_convnet_layer_forward_propagate(ccv_convnet_layer_t* layer, int device_id, int k, int rows, int cols, int batch, int dor, float* a, float* b, float* denoms, float* batch_unit, cwc_convnet_context_t* context)
{
switch (layer->type)
{
case CCV_CONVNET_CONVOLUTIONAL:
cwc_convnet_convolutional_forward_propagate(layer, rows, cols, batch, a, b, context->device[device_id].data_stream);
if (dor && context->device[device_id].dor[k])
{
int out_rows, out_cols, out_partition;
ccv_convnet_make_output(layer, rows, cols, &out_rows, &out_cols, &out_partition);
_cwc_kern_mute_neuron
<<<out_rows * out_cols * layer->net.convolutional.count, batch, 0, context->device[device_id].data_stream>>>
(b, context->device[device_id].dor[k]);
}
break;
case CCV_CONVNET_FULL_CONNECT:
assert(k > 0);
cwc_convnet_full_connect_forward_propagate(layer, batch, a, b, batch_unit, context->device[device_id].data_stream, context->device[device_id].data_cublas);
if (dor && context->device[device_id].dor[k])
_cwc_kern_mute_neuron
<<<layer->net.full_connect.count, batch, 0, context->device[device_id].data_stream>>>
(b, context->device[device_id].dor[k]);
break;
case CCV_CONVNET_LOCAL_RESPONSE_NORM:
assert(k > 0);
cwc_convnet_rnorm_forward_propagate(layer, rows, cols, batch, a, b, denoms, context->device[device_id].data_stream);
break;
case CCV_CONVNET_MAX_POOL:
assert(k > 0);
cwc_convnet_max_pool_forward_propagate(layer, rows, cols, batch, a, b, context->device[device_id].data_stream);
break;
case CCV_CONVNET_AVERAGE_POOL:
assert(k > 0);
cwc_convnet_average_pool_forward_propagate(layer, rows, cols, batch, a, b, context->device[device_id].data_stream);
break;
}
}
static int _cwc_convnet_first_full_connect(ccv_convnet_t* convnet)
{
int i;
for (i = 0; i < convnet->count; i++)
if (convnet->layers[i].type == CCV_CONVNET_FULL_CONNECT)
return i;
return 0;
}
// assuming a is in device memory
static void _cwc_convnet_encode_impl(ccv_convnet_t* convnet, int device_count, int batch, int dor, cwc_convnet_context_t* context)
{
assert(batch % 16 == 0);
int i;
if (device_count > 1)
{
int j, device_id, other_device_id;
int count = _cwc_convnet_first_full_connect(convnet);
for (i = 0; i < count; i++)
{
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
ccv_convnet_layer_t* layer = GPU(convnet)->device[device_id].layers + i;
// adding offset so that follow up computation is easier
float* a = (i == count - 1) ? GPU(convnet)->device[device_id].forwards[i] + device_id * batch * GPU(convnet)->device[device_id].layers[count].input.node.count : GPU(convnet)->device[device_id].forwards[i];
_cwc_convnet_layer_forward_propagate(layer, device_id, i, layer->input.matrix.rows, layer->input.matrix.cols, batch, dor, i == 0 ? context->device[device_id].input : GPU(convnet)->device[device_id].forwards[i - 1], a, GPU(convnet)->device[device_id].denoms[i], GPU(convnet)->device[device_id].unit, context);
}
}
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
cudaEventRecord(context->device[device_id].data_joint, context->device[device_id].data_stream);
}
// the connecting layer from data parallelism to model parallelism
// this is different because first, I need to copy the full batch from first GPU to the second GPU (and vice versa).
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
ccv_convnet_layer_t* layer = GPU(convnet)->device[device_id].layers + count;
assert(layer->type == CCV_CONVNET_FULL_CONNECT);
for (i = 0; i < device_count; i++)
{
cudaStreamWaitEvent(context->device[device_id].model_stream[i % 2], context->device[i].data_joint, 0);
if (device_id != i)
// overlap the memory copy and the gemm computation
// although it is slightly faster to use source stream, but we sync on the destination stream because otherwise we need to signal an event, and in that case, total cost is higher
cudaMemcpyPeerAsync(GPU(convnet)->device[device_id].forwards[count - 1] + i * batch * layer->input.node.count, device_id, GPU(convnet)->device[i].forwards[count - 1] + i * batch * layer->input.node.count, i, sizeof(float) * batch * layer->input.node.count, context->device[device_id].model_stream[i % 2]);
// the last compute issued on this device
cwc_convnet_full_connect_forward_propagate(layer, batch,
GPU(convnet)->device[device_id].forwards[count - 1] + i * batch * layer->input.node.count,
GPU(convnet)->device[device_id].forwards[count] + (device_count * i + device_id) * batch * layer->net.full_connect.count,
GPU(convnet)->device[device_id].unit,
context->device[device_id].model_stream[i % 2], context->device[device_id].model_cublas[i % 2]);
if (dor && context->device[device_id].dor[count])
_cwc_kern_mute_neuron
<<<layer->net.full_connect.count, batch, 0, context->device[device_id].model_stream[i % 2]>>>
(GPU(convnet)->device[device_id].forwards[count] + (device_count * i + device_id) * batch * layer->net.full_connect.count, context->device[device_id].dor[count]);
// figure out the free memory to use
}
}
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
cudaEventRecord(context->device[device_id].model_joint[0], context->device[device_id].model_stream[0]);
cudaEventRecord(context->device[device_id].model_joint[1], context->device[device_id].model_stream[1]);
}
for (i = count + 1; i < convnet->count; i++)
{
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
ccv_convnet_layer_t* layer = GPU(convnet)->device[device_id].layers + i;
assert(layer->type == CCV_CONVNET_FULL_CONNECT);
// finishing copy the 1 / 4th so that everyone can proceed
assert(layer->input.node.count % device_count == 0);
int input_node_count = layer->input.node.count / device_count;
for (j = 0; j < device_count; j++)
{
// copy data in, and do the computation
for (other_device_id = 0; other_device_id < device_count; other_device_id++)
if (other_device_id != device_id)
{
cudaStreamWaitEvent(context->device[device_id].model_stream[j % 2], context->device[other_device_id].model_joint[j % 2], 0);
// wait the previous iteration on this to finish
cudaMemcpyPeerAsync(GPU(convnet)->device[device_id].forwards[i - 1] + (device_count * j + other_device_id) * batch * input_node_count, device_id, GPU(convnet)->device[other_device_id].forwards[i - 1] + (device_count * j + other_device_id) * batch * input_node_count, other_device_id, sizeof(float) * batch * input_node_count, context->device[device_id].model_stream[j % 2]);
}
// first do the computation on the device that we already have full data
cwc_convnet_full_connect_forward_propagate(layer, batch,
GPU(convnet)->device[device_id].forwards[i - 1] + j * batch * layer->input.node.count,
GPU(convnet)->device[device_id].forwards[i] + (device_count * j + device_id) * batch * layer->net.full_connect.count,
GPU(convnet)->device[device_id].unit, context->device[device_id].model_stream[j % 2], context->device[device_id].model_cublas[j % 2]);
if (dor && context->device[device_id].dor[i])
_cwc_kern_mute_neuron
<<<layer->net.full_connect.count, batch, 0, context->device[device_id].model_stream[j % 2]>>>
(GPU(convnet)->device[device_id].forwards[i] + (device_count * j + device_id) * batch * layer->net.full_connect.count,
context->device[device_id].dor[i]);
}
}
// record events after we completed issuing command to avoid race (otherwise we are waiting events we just recorded)
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
cudaEventRecord(context->device[device_id].model_joint[0], context->device[device_id].model_stream[0]);
cudaEventRecord(context->device[device_id].model_joint[1], context->device[device_id].model_stream[1]);
}
}
// copy each batch full result to each device
for (device_id = 0; device_id < device_count; device_id++)
{
cudaSetDevice(device_id);
ccv_convnet_layer_t* last_layer = GPU(convnet)->device[device_id].layers + (convnet->count - 1);
assert(last_layer->type == CCV_CONVNET_FULL_CONNECT);
int output_node_count = last_layer->net.full_connect.count; // this is already halved.
// copy data in, and do the computation
for (other_device_id = 0; other_device_id < device_count; other_device_id++)
if (other_device_id != device_id)
{
cudaStreamWaitEvent(context->device[device_id].data_stream, context->device[other_device_id].model_joint[device_id % 2], 0);
cudaMemcpyPeerAsync(GPU(convnet)->device[device_id].forwards[convnet->count - 1] + (device_count * device_id + other_device_id) * batch * output_node_count, device_id, GPU(convnet)->device[other_device_id].forwards[convnet->count - 1] + (device_count * device_id + other_device_id) * batch * output_node_count, other_device_id, sizeof(float) * batch * output_node_count, context->device[device_id].data_stream);
}
cudaStreamWaitEvent(context->device[device_id].data_stream, context->device[device_id].model_joint[device_id % 2], 0);
}
} else {
for (i = 0; i < convnet->count; i++)
{
ccv_convnet_layer_t* layer = GPU(convnet)->device[0].layers + i;
_cwc_convnet_layer_forward_propagate(layer, 0, i, layer->input.matrix.rows, layer->input.matrix.cols, batch, dor, i == 0 ? context->device[0].input : GPU(convnet)->device[0].forwards[i - 1], GPU(convnet)->device[0].forwards[i], GPU(convnet)->device[0].denoms[i], GPU(convnet)->device[0].unit, context);
}
}
}
#ifdef HAVE_GSL
__global__ static void _cwc_kern_softmax_with_logistic_loss(const int batch, const int count, float* a, int* c)
{
const int thidx = blockIdx.x * blockDim.x + threadIdx.x;
if (thidx < batch)
{
int i;
float max_val = a[thidx];
for (i = 1; i < count; i++)
{
float prod = a[i * batch + thidx];
if (prod > max_val)
max_val = prod;
}
float val = 0;
for (i = 0; i < count; i++)
{
float prod = a[i * batch + thidx];
val += (prod = expf(prod - max_val));
a[i * batch + thidx] = prod;
}
val = 1.0 / val;
for (i = 0; i < count; i++)
a[i * batch + thidx] = (i == c[thidx]) - (a[i * batch + thidx] * val);
}
}
static void _cwc_convnet_softmax_with_logistic_loss(int batch, int count, float* a, int* c, const cudaStream_t& stream)
{
dim3 num_blocks((batch + 63) / 64);
dim3 threads_per_block(ccv_min(batch, 64));
assert(threads_per_block.x <= 1024);
int shared_memory_size = sizeof(float) * batch;
_cwc_kern_softmax_with_logistic_loss
<<<num_blocks, threads_per_block, shared_memory_size, stream>>>
(batch, count, a, c);
}
__global__ static void _cwc_kern_tests_return(const int batch, const int count, float* a, int* c)
{
const int thidx = blockIdx.x * blockDim.x + threadIdx.x;
if (thidx < batch)
{
int i;
float max_val = a[thidx];
int max_idx = 0;
for (i = 1; i < count; i++)
{
float val = a[i * batch + thidx];
if (val > max_val)
max_val = val, max_idx = i;
}
c[thidx] = max_idx;
}
}
static void _cwc_convnet_tests_return(int batch, int count, float* a, int* c, const cudaStream_t& stream)
{
dim3 num_blocks((batch + 63) / 64);
dim3 threads_per_block(ccv_min(batch, 64));
assert(threads_per_block.x <= 1024);
_cwc_kern_tests_return
<<<num_blocks, threads_per_block, 0, stream>>>
(batch, count, a, c);
}
__global__ static void _cwc_kern_net_sgd(float* a, float* grad, float* momentum,
const int count,
const float learn_rate, const float momentum_rate, const float decay_and_learn)
{
if (blockIdx.x * blockDim.x + threadIdx.x < count)
{
a += blockIdx.x * blockDim.x;
grad += blockIdx.x * blockDim.x;
momentum += blockIdx.x * blockDim.x;
const int thidx = threadIdx.x;
float old_a = a[thidx];
float velocity = momentum_rate * momentum[thidx] - decay_and_learn * old_a + learn_rate * grad[thidx];
a[thidx] = velocity + old_a;
momentum[thidx] = velocity;
}
}
static void _cwc_convnet_collect_disk_stats(ccv_convnet_t* convnet)
{
int i, j;
for (i = 0; i < convnet->count; i++)
{
ccv_convnet_layer_t* layer = convnet->layers + i;
if (layer->type == CCV_CONVNET_CONVOLUTIONAL || layer->type == CCV_CONVNET_FULL_CONNECT)
{
int bias_count = layer->type == CCV_CONVNET_CONVOLUTIONAL ? layer->net.convolutional.count : layer->net.full_connect.count;
float w_asum = 0;
for (j = 0; j < layer->wnum; j++)
w_asum += layer->w[j];
float w_mean = w_asum / layer->wnum;
float w_variance = 0;
for (j = 0; j < layer->wnum; j++)
w_variance += (layer->w[j] - w_mean) * (layer->w[j] - w_mean);
w_variance = w_variance / layer->wnum;
float bias_asum = 0;
for (j = 0; j < bias_count; j++)
bias_asum += layer->bias[j];
float bias_mean = bias_asum / bias_count;
float bias_variance = 0;
for (j = 0; j < bias_count; j++)
bias_variance += (layer->bias[j] - bias_mean) * (layer->bias[j] - bias_mean);
bias_variance = bias_variance / bias_count;
PRINT(CCV_CLI_VERBOSE, " - %03d * %g %g | %g %g\n", i, w_mean, w_variance, bias_mean, bias_variance);
}
}
}
static void _cwc_convnet_collect_runtime_stats(ccv_convnet_t* convnet, cwc_convnet_context_t* context)