-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsbnn32_param.h
1241 lines (1146 loc) · 47 KB
/
sbnn32_param.h
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
/** @file sbnn32_param.h
* @brief Layer parameter definition 64bit implementation.
*
* @author Ang Li (PNNL)
*
*/
#ifndef SBNN32_PARAM_H
#define SBNN32_PARAM_H
#include <string.h>
#include "utility.h"
/** @brief Binarize and pack weight matrix into 32-bit unsigned matrix.
*
* Binarization function to convert row-major 32-bit floating-point weight matrix into
* bit column-major bit-matrix. This is for the preparation of the weight matrices for
* FC layers.
*
* @return Void.
*/
__global__ void PackFcWeight32(const float* __restrict__ A, unsigned* B,
const int A_height, const int A_width)
{
unsigned laneid; asm("mov.u32 %0, %%laneid;":"=r"(laneid));
const unsigned bx = blockIdx.x;
const unsigned by = blockIdx.y;
unsigned Bval=0;
#pragma unroll
for (int i=0; i<32; i++)
{
float f0 = ((by*32+laneid<A_width)&&(bx*32+i<A_height))?A[(bx*32+i)*A_width+by*32+laneid]:-1.0f;
Bval = (Bval<<1)|(f0>=0?1:0);
}
if (laneid < A_height*A_width)
B[bx*gridDim.y*32+by*32+laneid] = Bval;
}
/** @brief Unpack 32-bit row-major unsigned activation matrix into floating-point.
*
* Unpack compact 32-bit unsigned layer output activation matrix into floating-point for
* validation purpose.
*
* @return Void.
*/
__global__ void UnpackFcOutput32(const unsigned* __restrict__ A, float* B,
const int A_height, const int A_width)
{
unsigned laneid; asm("mov.u32 %0, %%laneid;":"=r"(laneid));
const unsigned by = blockIdx.y;
const unsigned bx = blockIdx.x;
unsigned Aval = A[by*gridDim.x*32+bx*32+laneid];
#pragma unroll
for (int i=0; i<32; i++)
{
unsigned r0 = __shfl_sync(0xFFFFFFFF, Aval, i); //from lane-i
if ((32*bx+i)<A_height && by*32+laneid<A_width)
{
B[(32*bx+i)*A_width+by*32+laneid] = 2*(float)((r0>>(31-laneid)) & 0x1)-1;
}
}
}
/** @brief Binarize and pack filter tensor at output-channels into 32-bit unsigned tensor.
*
* Binarization function to convert 32-bit floating-point filter tensor (HWIO) into
* bit filter tensor (HWIO). This is for the preparation of the filter tensor for
* Conv layers.
*
* @return Void.
*/
__global__ void PackFiltersByOutChannels32(const float* __restrict__ filter,
unsigned* filter_binarized, const int in_channels, const int out_channels,
const int filter_width, const int filter_height)
{
unsigned laneid; asm("mov.u32 %0, %%laneid;":"=r"(laneid));
const int bx = blockIdx.x;//iter over (filter_width*filter_height)
const int by = blockIdx.y;//iter over input_channels
const int ots = CEIL(out_channels);//condense K:output_channel into 32bit-unsigned
for (int k=0; k<ots; k++) //iter over K:output_channels
{
// From shape[filter_height, filter_width, in_channels, out_channels]
float f0 = ((k*32+laneid)<out_channels)? filter[bx*in_channels*out_channels
+ by*out_channels + k*32 + laneid]:0;
unsigned r0 = __brev(__ballot_sync(0xFFFFFFFF, f0>=0));
// To shape[filter_height, filter_width, in_channels, out_channels/32]
filter_binarized[bx*ots*in_channels+ by*ots + k] = r0;
}
}
/** @brief Unpack condensed unsigned activation tensor into floating-point.
*
* Unpack compact 32-bit unsigned layer output activation tensor into floating-point for
* validation purpose.
*
* @return Void.
*/
__global__ void UnpackConvOutput32(const unsigned* __restrict__ input_binarized,
float* input, const int input_height, const int input_width,
const int input_channels, const int batch)
{
unsigned laneid; asm("mov.u32 %0, %%laneid;":"=r"(laneid));
const int bx = blockIdx.x;//input_width
const int by = blockIdx.y;//input_height
const int bz = blockIdx.z;//batch
const int ins = CEIL(input_channels);//condense C:in_channel into 32bit-unsigned
for (int c=0; c<ins; c++) //iter over C:in_channels
{
// From shape[batch, input_height, input_width, in_channels/32]
unsigned r0 = input_binarized[bz*input_height*input_width*ins + by*input_width*ins + bx*ins + c];
// To shape[batch, input_height, input_width, in_channels]
if (c*32+laneid<input_channels)
{
input[bz*input_height*input_width*input_channels + by*input_width*input_channels
+ bx*input_channels + c*32 + laneid] = 2*(float)((r0>>(31-laneid)) & 0x1)-1;
}
}
}
/** @brief Binarize and pack filter tensor at input-channels into 32-bit unsigned tensor.
*
* Binarization function to convert 32-bit floating-point filter tensor (HWIO) into
* bit filter tensor (HWIO). This is for the preparation of the filter tensor for
* Conv layers.
*
* @return Void.
*/
__global__ void PackFiltersByInChannels32(const float* __restrict__ filter,
unsigned* filter_binarized, const int in_channels, const int out_channels,
const int filter_width, const int filter_height)
{
unsigned laneid; asm("mov.u32 %0, %%laneid;":"=r"(laneid));
const int bx = blockIdx.x;//iter over (filter_width*filter_height)
const int by = blockIdx.y;//iter over out_channels
const int ins = CEIL(in_channels);//condense C:in_channel into 32bit-unsigned
for (int c=0; c<ins; c++) //iter over C:in_channels
{
// From shape[filter_height, filter_width, in_channels, out_channels]
float f0 = ((c*32+laneid)<in_channels)? filter[bx*in_channels*out_channels
+ (c*32+laneid)*out_channels + by]:0;
unsigned r0 = __brev(__ballot_sync(0xFFFFFFFF, f0>=0));
//if (laneid == 0) //avoid warp conflict
// To shape[filter_height, filter_width, in_channels/32, out_channels]
filter_binarized[bx*ins*out_channels+ c*out_channels + by] = r0;
}
}
//===============================================================
//1-Bit FC input layer (32-bit)
class In32LayerParam
{
public:
In32LayerParam(const char* name, unsigned input_height, unsigned input_width)
{
strncpy(this->name, name, 8);
this->input_height = input_height;
this->output_height = input_height;
this->input_width = input_width;
this->output_width = input_width;
this->input = NULL;
this->input_gpu = NULL;
this->output = NULL;
this->output_gpu = NULL;
this->gpu = NULL;
}
In32LayerParam* initialize(float* input)
{
if (input == NULL)
{
fprintf(stderr, "Error: NULL input.\n");
exit(1);
}
this->input = input;
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->input_gpu), input_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(input_gpu, input, input_bytes(),
cudaMemcpyHostToDevice) );
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->output_gpu), output_bit_bytes()) );
CUDA_SAFE_CALL( cudaMemset(this->output_gpu, 0, output_bit_bytes()) );
return this->ready();
}
int input_size() { return input_height * input_width; }
int input_bytes() { return input_size() * sizeof(float);}
int input_bit_size() { return input_height * input_width; }
int input_bit_bytes() { return input_bit_size() * sizeof(float);}
//column-major, ceil row to 32/64 bits
int output_size() { return output_height * output_width;}
int output_bytes() { return output_size()*sizeof(unsigned);}
int output_bit_size() { return CEIL(output_height)*FEIL(output_width);}
int output_bit_bytes() { return output_bit_size()*sizeof(unsigned);}
In32LayerParam* ready()
{
if (input_gpu == NULL)
{
fprintf(stderr, "Input data has not been uploaded to GPU.\n");
exit(1);
}
if (output_gpu == NULL)
{
fprintf(stderr, "Output on GPU has not been allocated.\n");
exit(1);
}
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->gpu), sizeof(In32LayerParam)) );
CUDA_SAFE_CALL( cudaMemcpy(this->gpu, this, sizeof(In32LayerParam),
cudaMemcpyHostToDevice) );
return this->gpu;
}
void set_output_gpu(unsigned* _output_gpu)
{
this->output_gpu = _output_gpu;
}
unsigned* get_output_gpu()
{
return this->output_gpu;
}
unsigned* download_output()
{
if (this->output == NULL) (this->output) = (unsigned*)malloc(output_bit_bytes());
CUDA_SAFE_CALL( cudaMemcpy(output, output_gpu, output_bit_bytes(),
cudaMemcpyDeviceToHost) );
return this->output;
}
float* download_full_output()
{
const int size = output_size()*sizeof(float);
float* full_output = (float*)malloc(size);
float* full_output_gpu = NULL;
CUDA_SAFE_CALL( cudaMalloc((void**)&(full_output_gpu), size) );
CUDA_SAFE_CALL( cudaMemset(full_output_gpu, 0, size) );
UnpackFcOutput32<<<dim3( CEIL(output_height), CEIL(output_width) ),32>>>(
output_gpu, full_output_gpu, output_height, output_width);
CUDA_SAFE_CALL( cudaMemcpy(full_output, full_output_gpu, size,
cudaMemcpyDeviceToHost) );
CUDA_SAFE_CALL( cudaFree(full_output_gpu) );
return full_output;
}
void release()
{
if (this->input_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->input_gpu) );
this->input_gpu = NULL;
}
if (this->output != NULL)
{
free(this->output);
this->output = NULL;
}
if (this->output_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->output_gpu) );
this->output_gpu = NULL;
}
if (this->gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->gpu) );
this->gpu = NULL;
}
}
~In32LayerParam() { release(); }
public:
//Input
float* input;
float* input_gpu;
unsigned input_width;
unsigned input_height;
//Output
unsigned* output;
unsigned* output_gpu;
unsigned output_width;
unsigned output_height;
//GPU Shadow
In32LayerParam* gpu;
char name[8];
};
class Fc32LayerParam
{
public:
Fc32LayerParam(const char* name, unsigned _input_height, unsigned _input_width,
unsigned _weight_width)
{
strncpy(this->name, name, 8);
weight_height = input_width = _input_width;
output_height = input_height = _input_height;
bn_width = output_width = weight_width = _weight_width;
this->weight = NULL;
this->weight_gpu = NULL;
this->bn = NULL;
this->bn_gpu = NULL;
this->output = NULL;
this->output_gpu = NULL;
this->input = NULL;
this->input_gpu = NULL;
this->gpu = NULL;
}
void set_input_gpu(unsigned* input_gpu)
{
this->input_gpu = input_gpu;
}
Fc32LayerParam* ready()
{
if (input_gpu == NULL)
{
fprintf(stderr, "Input data has not been uploaded to GPU.\n");
exit(1);
}
if (output_gpu == NULL)
{
fprintf(stderr, "Output on GPU has not been allocated.\n");
exit(1);
}
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->gpu), sizeof(Fc32LayerParam)) );
CUDA_SAFE_CALL( cudaMemcpy(this->gpu, this, sizeof(Fc32LayerParam),
cudaMemcpyHostToDevice) );
return this->gpu;
}
Fc32LayerParam* initialize(FILE* config_file, unsigned* prev_layer_gpu)
{
//Process weight
this->weight = (float*)malloc(weight_bytes());
launch_array(config_file, this->weight, weight_size());
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->weight_gpu), weight_bit_bytes()) );
float* weight_float = NULL;
CUDA_SAFE_CALL( cudaMalloc((void**)&(weight_float), weight_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(weight_float, weight, weight_bytes(),
cudaMemcpyHostToDevice) );
//Binarize and compact weight
PackFcWeight32<<<dim3( CEIL(weight_height), CEIL(weight_width) ), 32>>>(
weight_float, weight_gpu, weight_height, weight_width);
CUDA_SAFE_CALL( cudaFree(weight_float) );
//Process bn
this->bn = (float*)malloc(bn_bytes());
launch_array(config_file, this->bn, bn_size());
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->bn_gpu), bn_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(bn_gpu, bn, bn_bytes(), cudaMemcpyHostToDevice) );
//Allocate output
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->output_gpu), output_bit_bytes()) );
CUDA_SAFE_CALL( cudaMemset(this->output_gpu, 0, output_bit_bytes()) );
set_input_gpu(prev_layer_gpu);
return this->ready();
}
//column-major, ceil row
int input_size() { return input_height*input_width;}
int input_bytes() { return input_size()*sizeof(unsigned);}
int input_bit_size() { return FEIL(input_height)*CEIL(input_width);}
int input_bit_bytes() { return input_bit_size()*sizeof(unsigned);}
//row-major, ceil column to 32bit
int weight_size() { return weight_height*weight_width;}
int weight_bytes() { return weight_size()*sizeof(float);}
int weight_bit_size() { return CEIL(weight_height)*FEIL(weight_width);}
int weight_bit_bytes() { return weight_bit_size()*sizeof(unsigned);}
//column-major, ceil row to 32bit
int output_size() { return output_height*output_width;}
int output_bytes() { return output_size()*sizeof(unsigned);}
int output_bit_size() { return FEIL(output_height)*CEIL(output_width);}
int output_bit_bytes() { return output_bit_size()*sizeof(unsigned);}
//batch-norm
int bn_size() { return bn_width;}
int bn_bytes() { return bn_size()*sizeof(float);}
unsigned* get_output_gpu()
{
return this->output_gpu;
}
unsigned* download_output()
{
if (output == NULL) output = (unsigned*)malloc(output_bit_bytes());
CUDA_SAFE_CALL( cudaMemcpy(output, output_gpu, output_bit_bytes(),
cudaMemcpyDeviceToHost) );
return this->output;
}
float* download_full_output()
{
const int size = FEIL(output_height)*FEIL(output_width)*sizeof(float);
float* full_output = (float*)malloc(size);
float* full_output_gpu = NULL;
CUDA_SAFE_CALL( cudaMalloc((void**)&(full_output_gpu), size) );
CUDA_SAFE_CALL( cudaMemset(full_output_gpu, 0, size) );
UnpackFcOutput32<<<dim3( CEIL(output_height), CEIL(output_width) ), 32>>>(
output_gpu, full_output_gpu, output_height, output_width);
CUDA_SAFE_CALL( cudaMemcpy(full_output, full_output_gpu, size,
cudaMemcpyDeviceToHost) );
CUDA_SAFE_CALL( cudaFree(full_output_gpu) );
return full_output;
}
void release()
{
if (this->weight != NULL) { free(this->weight); this->weight = NULL;}
if (this->bn != NULL) { free(this->bn); this->bn = NULL;}
if (this->output != NULL) { free(this->output); this->output = NULL;}
if (this->weight_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(weight_gpu) );
weight_gpu = NULL;
}
if (this->bn_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(bn_gpu) );
bn_gpu = NULL;
}
if (this->output_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->output_gpu) );
this->output_gpu = NULL;
}
if (this->gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->gpu) );
this->gpu = NULL;
}
}
~Fc32LayerParam() { release(); }
public:
//Input
unsigned* input;
unsigned* input_gpu;
unsigned input_width;
unsigned input_height;
//Weight
float* weight;
unsigned* weight_gpu;
unsigned weight_width;
unsigned weight_height;
//Output
unsigned* output;
unsigned* output_gpu;
unsigned output_width;
unsigned output_height;
//Batch normalization
float* bn;
float* bn_gpu;
unsigned bn_width;
//GPU shodow
Fc32LayerParam* gpu;
char name[8];
};
class Out32LayerParam
{
public:
Out32LayerParam(const char* name, unsigned _input_height, unsigned _input_width,
unsigned _weight_width, bool has_bn=true)
{
strncpy(this->name, name, 8);
weight_height = input_width = _input_width;
output_height = input_height = _input_height;
output_width = weight_width = _weight_width;
this->input = NULL;
this->input_gpu = NULL;
this->output = NULL;
this->output_gpu = NULL;
this->weight = NULL;
this->weight_gpu = NULL;
this->gpu = NULL;
this->has_bn = has_bn;
this->bn_scale = NULL;
this->bn_scale_gpu = NULL;
this->bn_bias = NULL;
this->bn_bias_gpu = NULL;
}
Out32LayerParam* ready()
{
if (input_gpu == NULL)
{
fprintf(stderr, "Input data has not been uploaded to GPU.\n");
exit(1);
}
if (output_gpu == NULL)
{
fprintf(stderr, "Output on GPU has not been allocated.\n");
exit(1);
}
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->gpu), sizeof(Out32LayerParam)) );
CUDA_SAFE_CALL( cudaMemcpy(this->gpu, this, sizeof(Out32LayerParam),
cudaMemcpyHostToDevice) );
return this->gpu;
}
void set_input_gpu(unsigned* _input_gpu)
{
this->input_gpu = _input_gpu;
}
Out32LayerParam* initialize(FILE* config_file, unsigned* prev_layer_gpu)
{
this->weight = (float*)malloc(weight_bytes());
launch_array(config_file, this->weight, weight_size());
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->weight_gpu), weight_bit_bytes()) );
float* weight_float = NULL;
CUDA_SAFE_CALL( cudaMalloc((void**)&(weight_float), weight_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(weight_float, weight, weight_bytes(),
cudaMemcpyHostToDevice) );
//Binarize and compact weight
PackFcWeight32<<<dim3( CEIL(weight_height), CEIL(weight_width) ), 32>>>(
weight_float, weight_gpu, weight_height, weight_width);
CUDA_SAFE_CALL( cudaFree(weight_float) );
//BN
if (this->has_bn)
{
this->bn_scale = (float*)malloc(bn_bytes());
launch_array(config_file, this->bn_scale, bn_size());
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->bn_scale_gpu), bn_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(bn_scale_gpu, bn_scale,
bn_bytes(), cudaMemcpyHostToDevice) );
this->bn_bias = (float*)malloc(bn_bytes());
launch_array(config_file, this->bn_bias, bn_size());
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->bn_bias_gpu), bn_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(bn_bias_gpu, bn_bias, bn_bytes(),
cudaMemcpyHostToDevice) );
}
//Allocate output
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->output_gpu), output_bytes()) );
set_input_gpu(prev_layer_gpu);
return this->ready();
}
//column-major, ceil row to 32bit
int input_size() { return input_height*input_width;}
int input_bytes() { return input_size()*sizeof(unsigned);}
int input_bit_size() {return FEIL(input_height)*FEIL(input_width);}
int input_bit_bytes() {return CEIL(input_height)*FEIL(input_width)*sizeof(unsigned);}
//row-major, ceil column to 32bit
int weight_size() { return weight_height*weight_width;}
int weight_bytes() { return weight_size()*sizeof(unsigned);}
int weight_bit_size() {return FEIL(weight_height)*FEIL(weight_width);}
int weight_bit_bytes() {return FEIL(weight_height)*CEIL(weight_width)*sizeof(unsigned);}
//row-major, float
int output_size() { return output_height * output_width;}
int output_bytes() { return output_size()*sizeof(float);}
int output_bit_size() { return output_height * output_width;}
int output_bit_bytes() { return output_bit_size()*sizeof(float);}
int bn_size() { return output_width;}
int bn_bytes() { return output_width*sizeof(float); }
void allocate_input_gpu()
{
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->input_gpu), input_bit_bytes()) );
}
float* download_output()
{
if (output == NULL) output = (float*)malloc(output_bytes());
CUDA_SAFE_CALL( cudaMemcpy(output, output_gpu, output_bytes(),
cudaMemcpyDeviceToHost) );
return this->output;
}
void release()
{
if (this->weight != NULL) { free(this->weight); this->weight = NULL;}
if (this->output != NULL) { free(this->output); this->output = NULL;}
if (this->weight_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->weight_gpu) );
this->weight_gpu = NULL;
}
if (this->output_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->output_gpu) );
this->output_gpu = NULL;
}
if (this->gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->gpu) );
this->gpu = NULL;
}
if (this->has_bn == true)
{
if (this->bn_scale != NULL)
{
free(this->bn_scale);
this->bn_scale = NULL;
}
if (this->bn_scale_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->bn_scale_gpu) );
this->bn_scale = NULL;
}
if (this->bn_bias != NULL)
{
free(this->bn_bias);
this->bn_bias = NULL;
}
if (this->bn_bias_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->bn_bias_gpu) );
this->bn_bias_gpu = NULL;
}
}
}
~Out32LayerParam() { release(); }
public:
//Input
unsigned* input;
unsigned* input_gpu;
unsigned input_width;
unsigned input_height;
//Weight
float* weight;
unsigned* weight_gpu;
unsigned weight_width;
unsigned weight_height;
//Output
float* output;
float* output_gpu;
unsigned output_height;
unsigned output_width;
//Batch normalization
bool has_bn;
float* bn_scale;
float* bn_scale_gpu;
float* bn_bias;
float* bn_bias_gpu;
//GPU shadow
Out32LayerParam* gpu;
char name[8];
};
////======================== Convolution ==========================
class In32Conv32LayerParam
{
public:
In32Conv32LayerParam(const char* name,
unsigned input_height,
unsigned input_width,
unsigned filter_height,
unsigned filter_width,
unsigned input_channels,
unsigned output_channels,
unsigned batch,
unsigned stride_vertical=1,
unsigned stride_horizontal=1,
bool same_padding=true,
unsigned pool_height=0,
unsigned pool_width=0,
bool output_transpose=false,
bool save_residual=false)
{
strncpy(this->name, name, 8);
this->input_height = input_height;
this->input_width = input_width;
this->filter_height = filter_height;
this->filter_width = filter_width;
this->input_channels = input_channels;
this->output_channels = output_channels;
this->batch = batch;
this->stride_vertical = stride_vertical;
this->stride_horizontal = stride_horizontal;
this->pool_height = pool_height;
this->pool_width = pool_width;
this->output_transpose = output_transpose;
this->save_residual = save_residual;
this->pad_h = same_padding?((( (input_height+stride_vertical-(input_height%stride_vertical))
/stride_vertical-1)*stride_vertical+filter_height-input_height)>>1):0;
this->pad_w = same_padding?((( (input_width+stride_horizontal-(input_width%stride_horizontal))
/stride_horizontal-1)*stride_horizontal+filter_width-input_width)>>1):0;
if (pool_height == 0)
{
output_height = same_padding?(input_height+stride_vertical-1)/stride_vertical
:((input_height-filter_height)/stride_vertical+1);
this->buf_height = 0;
}
else
{
buf_height = same_padding?(input_height+stride_vertical-1)/stride_vertical
:((input_height-filter_height)/stride_vertical+1);
output_height = (buf_height+pool_height-1)/pool_height;//pooling height
}
if (pool_width == 0)
{
output_width = same_padding?(input_width+stride_horizontal-1)/stride_horizontal
:((input_width-filter_width)/stride_horizontal+1);
this->buf_width = 0;
}
else
{
buf_width = same_padding?(input_width+stride_horizontal-1)/stride_horizontal
:((input_width-filter_width)/stride_horizontal+1);
output_width = (buf_width+pool_width-1)/pool_width; //pooling width
}
this->bn = NULL;
this->filter = NULL;
this->output = NULL;
this->output_gpu = NULL;
this->input = NULL;
this->input_gpu = NULL;
this->gpu = NULL;
this->save_residual_gpu = NULL;
}
In32Conv32LayerParam* ready()
{
if (input_gpu == NULL)
{
fprintf(stderr, "Input data has not been uploaded to GPU.\n");
exit(1);
}
if (output_gpu == NULL)
{
fprintf(stderr, "Output on GPU has not been allocated.\n");
exit(1);
}
if (save_residual && save_residual_gpu == NULL)
{
fprintf(stderr, "Residual for saving on GPU has not been allocated.\n");
exit(1);
}
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->gpu), sizeof(In32Conv32LayerParam)) );
CUDA_SAFE_CALL( cudaMemcpy(this->gpu, this,
sizeof(In32Conv32LayerParam), cudaMemcpyHostToDevice) );
return this->gpu;
}
In32Conv32LayerParam* initialize(float* input, FILE* config_file)
{
//Process input
if (input == NULL)
{
fprintf(stderr, "Error: NULL image input.\n");
exit(1);
}
this->input = input;
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->input_gpu), input_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(input_gpu, input,
input_bytes(), cudaMemcpyHostToDevice) );
//Process weight
this->filter = (float*)malloc(filter_bytes());
launch_array(config_file, this->filter, filter_size());
CUDA_SAFE_CALL (cudaMalloc((void**)&(this->filter_gpu), filter_bit_bytes()) );
float* filter_float = NULL;
CUDA_SAFE_CALL( cudaMalloc((void**)&(filter_float), filter_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(filter_float, filter,
filter_bytes(), cudaMemcpyHostToDevice) );
//Binarize Filter
PackFiltersByOutChannels32<<<dim3(filter_height*filter_width, input_channels), 32>>>(
filter_float, filter_gpu, input_channels, output_channels, filter_width, filter_height);
CUDA_SAFE_CALL( cudaFree(filter_float) );
//Process bn
this->bn = (float*)malloc(bn_bytes());
launch_array(config_file, this->bn, bn_size());
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->bn_gpu), bn_bytes()) );
CUDA_SAFE_CALL( cudaMemcpy(this->bn_gpu, this->bn,
bn_bytes(), cudaMemcpyHostToDevice) );
//Allocate output
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->output_gpu), output_bit_bytes()) );
CUDA_SAFE_CALL( cudaMemset(this->output_gpu, 0, output_bit_bytes()) );
//Allocate residual for saving
if (save_residual)
{
CUDA_SAFE_CALL( cudaMalloc((void**)&(this->save_residual_gpu), output_bytes()) );
CUDA_SAFE_CALL( cudaMemset(this->save_residual_gpu, 0, output_bytes()) );
}
return this->ready();
}
int input_size() { return input_channels*input_height*input_width*batch;}
int input_bytes() { return input_size()*sizeof(float);}
int input_bit_size() { return input_channels*input_height*input_width*batch;}
int input_bit_bytes() {return input_bit_size()*sizeof(float);}
//Size has problem, should be a factor of 32, also applied to other convParam
int filter_size() { return output_channels*input_channels*filter_height*filter_width;}
int filter_bytes() { return filter_size()*sizeof(float);}
int filter_bit_size() {return FEIL(output_channels)*input_channels*filter_height*filter_width;}
int filter_bit_bytes() { return CEIL(output_channels)*input_channels
*filter_height*filter_width*sizeof(unsigned);}
//In In32Conv32 we binarize output_channels
int output_size() { return output_channels*output_height*output_width*batch;}
int output_bytes() { return output_size()*sizeof(unsigned);}
int output_bit_size()
{
return output_transpose?FEIL(output_channels)*output_height*output_width*FEIL(batch):
FEIL(output_channels)*output_height*output_width*batch;
}
int output_bit_bytes()
{
return output_transpose?CEIL(output_channels)*output_height*output_width*
FEIL(batch)*sizeof(unsigned): CEIL(output_channels)*output_height*
output_width*batch*sizeof(unsigned);
}
//batch-norm
int bn_size() { return output_channels;}
int bn_bytes() { return bn_size()*sizeof(float);}
unsigned* get_output_gpu()
{
return this->output_gpu;
}
int* get_residual_gpu()
{
return this->save_residual_gpu;
}
unsigned* download_output()
{
if (output == NULL) output = (unsigned*)malloc(output_bit_bytes());
CUDA_SAFE_CALL( cudaMemcpy(output, output_gpu,
output_bit_bytes(), cudaMemcpyDeviceToHost) );
return this->output;
}
float* download_full_output()
{
float* full_output = (float*)malloc(output_bytes());
float* full_output_gpu = NULL;
CUDA_SAFE_CALL( cudaMalloc((void**)&(full_output_gpu), output_bytes()) );
CUDA_SAFE_CALL( cudaMemset(full_output_gpu, 0, output_bytes()) );
UnpackConvOutput32<<<dim3(output_width,output_height,batch), 32>>>(output_gpu,
full_output_gpu, output_height, output_width, output_channels, batch);
CUDA_SAFE_CALL( cudaMemcpy(full_output, full_output_gpu,
output_bytes(), cudaMemcpyDeviceToHost) );
CUDA_SAFE_CALL( cudaFree(full_output_gpu) );
return full_output;
}
void release()
{
if (this->filter!=NULL) {free(this->filter); this->filter=NULL;}
if (this->bn!=NULL) {free(this->bn); this->bn=NULL;}
if (this->output!=NULL) {free(this->output); this->output=NULL;}
if (this->input_gpu!=NULL)
{
CUDA_SAFE_CALL( cudaFree(this->input_gpu) );
this->input_gpu=NULL;
}
if (this->output_gpu!=NULL)
{
CUDA_SAFE_CALL( cudaFree(this->output_gpu) );
this->output_gpu=NULL;
}
if (this->filter_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->filter_gpu) );
this->filter_gpu = NULL;
}
if (this->bn_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->bn_gpu) );
this->bn_gpu = NULL;
}
if (this->gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->gpu) );
this->gpu = NULL;
}
if (this->save_residual && this->save_residual_gpu != NULL)
{
CUDA_SAFE_CALL( cudaFree(this->save_residual_gpu) );
this->save_residual_gpu = NULL;
}
}
~In32Conv32LayerParam() { release(); }
public:
//Input
float* input;
float* input_gpu;
unsigned input_width;
unsigned input_height;
unsigned input_channels;
//Weight
float* filter;
unsigned* filter_gpu;
unsigned filter_width;
unsigned filter_height;
//Output
unsigned* output;
unsigned* output_gpu;
unsigned output_width;
unsigned output_height;
unsigned output_channels;
bool output_transpose;
//Batch normalization
float* bn;
float* bn_gpu;
//Others
unsigned batch;
unsigned stride_vertical;
unsigned stride_horizontal;
unsigned pad_h;
unsigned pad_w;
//Pooling
unsigned pool_width;
unsigned pool_height;
unsigned buf_width;
unsigned buf_height;
//GPU shadow
In32Conv32LayerParam* gpu;
char name[8];
//Residual
bool save_residual;
int* save_residual_gpu;
};
class Conv32LayerParam
{
public:
Conv32LayerParam(const char* name,
unsigned input_height,
unsigned input_width,
unsigned filter_height,
unsigned filter_width,
unsigned input_channels,
unsigned output_channels,
unsigned batch,
unsigned stride_vertical=1,
unsigned stride_horizontal=1,
bool same_padding=true,
unsigned pool_height=0,
unsigned pool_width=0,
bool output_transpose=false,
bool save_residual=false,
bool inject_residual=false,
unsigned residual_channels=0,
bool residual_pool=false)
{
strncpy(this->name, name, 8);
this->input_height = input_height;
this->input_width = input_width;
this->filter_height = filter_height;
this->filter_width = filter_width;
this->input_channels = input_channels;
this->output_channels = output_channels;
this->batch = batch;
this->stride_vertical = stride_vertical;
this->stride_horizontal = stride_horizontal;
this->pool_height = pool_height;
this->pool_width = pool_width;
this->output_transpose = output_transpose;
this->save_residual = save_residual;
this->inject_residual = inject_residual;
this->residual_channels = residual_channels;
this->residual_pool = residual_pool;
this->pad_h = same_padding?((( (input_height+stride_vertical-(input_height%stride_vertical))
/stride_vertical-1)*stride_vertical+filter_height-input_height)>>1):0;
this->pad_w = same_padding?((( (input_width+stride_horizontal-(input_width%stride_horizontal))
/stride_horizontal-1)*stride_horizontal+filter_width-input_width)>>1):0;
if (pool_height == 0)
{
output_height = same_padding?(input_height+stride_vertical-1)/stride_vertical
:((input_height-filter_height)/stride_vertical+1);
this->buf_height = 0;
}
else
{
buf_height = same_padding?(input_height+stride_vertical-1)/stride_vertical
:((input_height-filter_height)/stride_vertical+1);
output_height = (buf_height+pool_height-1)/pool_height;//pooling height
}
if (pool_width == 0)
{
output_width = same_padding?(input_width+stride_horizontal-1)/stride_horizontal
:((input_width-filter_width)/stride_horizontal+1);
this->buf_width = 0;
}
else