-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenet.cu
1122 lines (947 loc) · 52.1 KB
/
lenet.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 <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cfloat>
#include <algorithm>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <random>
#include <sstream>
#include <string>
#include <vector>
#include <cuda_runtime.h>
#include <device_launch_parameters.h>
#include <cublas_v2.h>
#include <cudnn.h>
#include "readubyte.h"
///////////////////////////////////////////////////////////////////////////////////////////
// Definitions and helper utilities
// Block width for CUDA kernels
#define BW 128
#ifdef USE_GFLAGS
#include <gflags/gflags.h>
#ifndef _WIN32
#define gflags google
#endif
#else
// Constant versions of gflags
#define DEFINE_int32(flag, default_value, description) const int FLAGS_##flag = (default_value)
#define DEFINE_uint64(flag, default_value, description) const unsigned long long FLAGS_##flag = (default_value)
#define DEFINE_bool(flag, default_value, description) const bool FLAGS_##flag = (default_value)
#define DEFINE_double(flag, default_value, description) const double FLAGS_##flag = (default_value)
#define DEFINE_string(flag, default_value, description) const std::string FLAGS_##flag ((default_value))
#endif
/**
* Computes ceil(x / y) for integral nonnegative values.
*/
static inline unsigned int RoundUp(unsigned int nominator, unsigned int denominator)
{
return (nominator + denominator - 1) / denominator;
}
/**
* Saves a PGM grayscale image out of unsigned 8-bit data
*/
void SavePGMFile(const unsigned char *data, size_t width, size_t height, const char *filename)
{
FILE *fp = fopen(filename, "wb");
if (fp)
{
fprintf(fp, "P5\n%lu %lu\n255\n", width, height);
fwrite(data, sizeof(unsigned char), width * height, fp);
fclose(fp);
}
}
#define FatalError(s) do { \
std::stringstream _where, _message; \
_where << __FILE__ << ':' << __LINE__; \
_message << std::string(s) + "\n" << __FILE__ << ':' << __LINE__; \
std::cerr << _message.str() << "\nAborting...\n"; \
cudaDeviceReset(); \
exit(1); \
} while(0)
#define checkCUDNN(status) do { \
std::stringstream _error; \
if (status != CUDNN_STATUS_SUCCESS) { \
_error << "CUDNN failure: " << cudnnGetErrorString(status); \
FatalError(_error.str()); \
} \
} while(0)
#define checkCudaErrors(status) do { \
std::stringstream _error; \
if (status != 0) { \
_error << "Cuda failure: " << status; \
FatalError(_error.str()); \
} \
} while(0)
///////////////////////////////////////////////////////////////////////////////////////////
// Command-line flags
// Application parameters
DEFINE_int32(gpu, 0, "The GPU ID to use");
DEFINE_int32(iterations, 1000, "Number of iterations for training");
DEFINE_int32(random_seed, -1, "Override random seed (default uses std::random_device)");
DEFINE_int32(classify, -1, "Number of images to classify to compute error rate (default uses entire test set)");
// Batch parameters
DEFINE_uint64(batch_size, 64, "Batch size for training");
// Filenames
DEFINE_bool(pretrained, false, "Use the pretrained CUDNN model as input");
DEFINE_bool(save_data, false, "Save pretrained weights to file");
DEFINE_string(train_images, "train-images-idx3-ubyte", "Training images filename");
DEFINE_string(train_labels, "train-labels-idx1-ubyte", "Training labels filename");
DEFINE_string(test_images, "t10k-images-idx3-ubyte", "Test images filename");
DEFINE_string(test_labels, "t10k-labels-idx1-ubyte", "Test labels filename");
// Solver parameters
DEFINE_double(learning_rate, 0.01, "Base learning rate");
DEFINE_double(lr_gamma, 0.0001, "Learning rate policy gamma");
DEFINE_double(lr_power, 0.75, "Learning rate policy power");
///////////////////////////////////////////////////////////////////////////////////////////
// Layer representations
/**
* Represents a convolutional layer with bias.
*/
struct ConvBiasLayer
{
int in_channels, out_channels, kernel_size;
int in_width, in_height, out_width, out_height;
std::vector<float> pconv, pbias;
ConvBiasLayer(int in_channels_, int out_channels_, int kernel_size_,
int in_w_, int in_h_) : pconv(in_channels_ * kernel_size_ * kernel_size_ * out_channels_),
pbias(out_channels_)
{
in_channels = in_channels_;
out_channels = out_channels_;
kernel_size = kernel_size_;
in_width = in_w_;
in_height = in_h_;
out_width = in_w_ - kernel_size_ + 1;
out_height = in_h_ - kernel_size_ + 1;
}
bool FromFile(const char *fileprefix)
{
std::stringstream ssf, ssbf;
ssf << fileprefix << ".bin";
ssbf << fileprefix << ".bias.bin";
// Read weights file
FILE *fp = fopen(ssf.str().c_str(), "rb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssf.str().c_str());
return false;
}
fread(&pconv[0], sizeof(float), in_channels * out_channels * kernel_size * kernel_size, fp);
fclose(fp);
// Read bias file
fp = fopen(ssbf.str().c_str(), "rb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssbf.str().c_str());
return false;
}
fread(&pbias[0], sizeof(float), out_channels, fp);
fclose(fp);
return true;
}
void ToFile(const char *fileprefix)
{
std::stringstream ssf, ssbf;
ssf << fileprefix << ".bin";
ssbf << fileprefix << ".bias.bin";
// Write weights file
FILE *fp = fopen(ssf.str().c_str(), "wb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssf.str().c_str());
exit(2);
}
fwrite(&pconv[0], sizeof(float), in_channels * out_channels * kernel_size * kernel_size, fp);
fclose(fp);
// Write bias file
fp = fopen(ssbf.str().c_str(), "wb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssbf.str().c_str());
exit(2);
}
fwrite(&pbias[0], sizeof(float), out_channels, fp);
fclose(fp);
}
};
/**
* Represents a max-pooling layer.
*/
struct MaxPoolLayer
{
int size, stride;
MaxPoolLayer(int size_, int stride_) : size(size_), stride(stride_) {}
};
/**
* Represents a fully-connected neural network layer with bias.
*/
struct FullyConnectedLayer
{
int inputs, outputs;
std::vector<float> pneurons, pbias;
FullyConnectedLayer(int inputs_, int outputs_) : outputs(outputs_), inputs(inputs_),
pneurons(inputs_ * outputs_), pbias(outputs_) {}
bool FromFile(const char *fileprefix)
{
std::stringstream ssf, ssbf;
ssf << fileprefix << ".bin";
ssbf << fileprefix << ".bias.bin";
// Read weights file
FILE *fp = fopen(ssf.str().c_str(), "rb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssf.str().c_str());
return false;
}
fread(&pneurons[0], sizeof(float), inputs * outputs, fp);
fclose(fp);
// Read bias file
fp = fopen(ssbf.str().c_str(), "rb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssbf.str().c_str());
return false;
}
fread(&pbias[0], sizeof(float), outputs, fp);
fclose(fp);
return true;
}
void ToFile(const char *fileprefix)
{
std::stringstream ssf, ssbf;
ssf << fileprefix << ".bin";
ssbf << fileprefix << ".bias.bin";
// Write weights file
FILE *fp = fopen(ssf.str().c_str(), "wb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssf.str().c_str());
exit(2);
}
fwrite(&pneurons[0], sizeof(float), inputs * outputs, fp);
fclose(fp);
// Write bias file
fp = fopen(ssbf.str().c_str(), "wb");
if (!fp)
{
printf("ERROR: Cannot open file %s\n", ssbf.str().c_str());
exit(2);
}
fwrite(&pbias[0], sizeof(float), outputs, fp);
fclose(fp);
}
};
///////////////////////////////////////////////////////////////////////////////////////////
// GPU Kernels
/**
* Fills a floating-point array with ones.
*
* @param vec The array to fill.
* @param size The number of elements in the array.
*/
__global__ void FillOnes(float *vec, int size)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= size)
return;
vec[idx] = 1.0f;
}
/**
* Computes the backpropagation results of the Softmax loss for each result in a batch.
* Uses the softmax values obtained from forward propagation to compute the difference.
*
* @param label The training batch label values.
* @param num_labels The number of possible labels.
* @param batch_size The size of the trained batch.
* @param diff The resulting gradient.
*/
__global__ void SoftmaxLossBackprop(const float *label, int num_labels, int batch_size, float *diff)
{
int idx = blockIdx.x * blockDim.x + threadIdx.x;
if (idx >= batch_size)
return;
const int label_value = static_cast<int>(label[idx]);
// For each item in the batch, decrease the result of the label's value by 1
diff[idx * num_labels + label_value] -= 1.0f;
}
///////////////////////////////////////////////////////////////////////////////////////////
// CUDNN/CUBLAS training context
struct TrainingContext
{
cudnnHandle_t cudnnHandle;
cublasHandle_t cublasHandle;
cudnnTensorDescriptor_t dataTensor, conv1Tensor, conv1BiasTensor, pool1Tensor,
conv2Tensor, conv2BiasTensor, pool2Tensor, fc1Tensor, fc2Tensor;
cudnnFilterDescriptor_t conv1filterDesc, conv2filterDesc;
cudnnConvolutionDescriptor_t conv1Desc, conv2Desc;
cudnnConvolutionFwdAlgo_t conv1algo, conv2algo;
cudnnConvolutionBwdFilterAlgo_t conv1bwfalgo, conv2bwfalgo;
cudnnConvolutionBwdDataAlgo_t conv2bwdalgo;
cudnnPoolingDescriptor_t poolDesc;
cudnnActivationDescriptor_t fc1Activation;
int m_gpuid;
int m_batchSize;
size_t m_workspaceSize;
FullyConnectedLayer& ref_fc1, &ref_fc2;
// Disable copying
TrainingContext& operator=(const TrainingContext&) = delete;
TrainingContext(const TrainingContext&) = delete;
TrainingContext(int gpuid, int batch_size,
ConvBiasLayer& conv1, MaxPoolLayer& pool1, ConvBiasLayer& conv2, MaxPoolLayer& pool2,
FullyConnectedLayer& fc1, FullyConnectedLayer& fc2) : ref_fc1(fc1), ref_fc2(fc2), m_gpuid(gpuid)
{
m_batchSize = batch_size;
// Create CUBLAS and CUDNN handles
checkCudaErrors(cudaSetDevice(gpuid));
checkCudaErrors(cublasCreate(&cublasHandle));
checkCUDNN(cudnnCreate(&cudnnHandle));
// Create tensor descriptors
checkCUDNN(cudnnCreateTensorDescriptor(&dataTensor));
checkCUDNN(cudnnCreateTensorDescriptor(&conv1Tensor));
checkCUDNN(cudnnCreateTensorDescriptor(&conv1BiasTensor));
checkCUDNN(cudnnCreateTensorDescriptor(&pool1Tensor));
checkCUDNN(cudnnCreateTensorDescriptor(&conv2Tensor));
checkCUDNN(cudnnCreateTensorDescriptor(&conv2BiasTensor));
checkCUDNN(cudnnCreateTensorDescriptor(&pool2Tensor));
checkCUDNN(cudnnCreateTensorDescriptor(&fc1Tensor));
checkCUDNN(cudnnCreateTensorDescriptor(&fc2Tensor));
checkCUDNN(cudnnCreateActivationDescriptor(&fc1Activation));
checkCUDNN(cudnnCreateFilterDescriptor(&conv1filterDesc));
checkCUDNN(cudnnCreateFilterDescriptor(&conv2filterDesc));
checkCUDNN(cudnnCreateConvolutionDescriptor(&conv1Desc));
checkCUDNN(cudnnCreateConvolutionDescriptor(&conv2Desc));
checkCUDNN(cudnnCreatePoolingDescriptor(&poolDesc));
// Set tensor descriptor sizes
checkCUDNN(cudnnSetTensor4dDescriptor(conv1BiasTensor,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
1, conv1.out_channels,
1, 1));
checkCUDNN(cudnnSetTensor4dDescriptor(conv2BiasTensor,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
1, conv2.out_channels,
1, 1));
checkCUDNN(cudnnSetPooling2dDescriptor(poolDesc,
CUDNN_POOLING_MAX,
CUDNN_PROPAGATE_NAN,
pool1.size, pool1.size,
0, 0,
pool1.stride, pool1.stride));
checkCUDNN(cudnnSetTensor4dDescriptor(pool2Tensor,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
batch_size, conv2.out_channels,
conv2.out_height / pool2.stride,
conv2.out_width / pool2.stride));
checkCUDNN(cudnnSetTensor4dDescriptor(fc1Tensor,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
batch_size, fc1.outputs, 1, 1));
checkCUDNN(cudnnSetTensor4dDescriptor(fc2Tensor,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
batch_size, fc2.outputs, 1, 1));
checkCUDNN(cudnnSetActivationDescriptor(fc1Activation, CUDNN_ACTIVATION_RELU,
CUDNN_PROPAGATE_NAN, 0.0));
// Set convolution tensor sizes and compute workspace size
size_t workspace = 0;
workspace = std::max(workspace, SetFwdConvolutionTensors(conv1, dataTensor, conv1Tensor, conv1filterDesc, conv1Desc, conv1algo));
workspace = std::max(workspace, SetBwdConvolutionTensors(dataTensor, conv1Tensor, conv1filterDesc, conv1Desc, &conv1bwfalgo, nullptr));
workspace = std::max(workspace, SetFwdConvolutionTensors(conv2, pool1Tensor, conv2Tensor, conv2filterDesc, conv2Desc, conv2algo));
workspace = std::max(workspace, SetBwdConvolutionTensors(pool1Tensor, conv2Tensor, conv2filterDesc, conv2Desc, &conv2bwfalgo, &conv2bwdalgo));
// The workspace is allocated later (if necessary)
m_workspaceSize = workspace;
}
~TrainingContext()
{
checkCudaErrors(cudaSetDevice(m_gpuid));
checkCudaErrors(cublasDestroy(cublasHandle));
checkCUDNN(cudnnDestroy(cudnnHandle));
checkCUDNN(cudnnDestroyTensorDescriptor(dataTensor));
checkCUDNN(cudnnDestroyTensorDescriptor(conv1Tensor));
checkCUDNN(cudnnDestroyTensorDescriptor(conv1BiasTensor));
checkCUDNN(cudnnDestroyTensorDescriptor(pool1Tensor));
checkCUDNN(cudnnDestroyTensorDescriptor(conv2Tensor));
checkCUDNN(cudnnDestroyTensorDescriptor(conv2BiasTensor));
checkCUDNN(cudnnDestroyTensorDescriptor(pool2Tensor));
checkCUDNN(cudnnDestroyTensorDescriptor(fc1Tensor));
checkCUDNN(cudnnDestroyTensorDescriptor(fc2Tensor));
checkCUDNN(cudnnDestroyActivationDescriptor(fc1Activation));
checkCUDNN(cudnnDestroyFilterDescriptor(conv1filterDesc));
checkCUDNN(cudnnDestroyFilterDescriptor(conv2filterDesc));
checkCUDNN(cudnnDestroyConvolutionDescriptor(conv1Desc));
checkCUDNN(cudnnDestroyConvolutionDescriptor(conv2Desc));
checkCUDNN(cudnnDestroyPoolingDescriptor(poolDesc));
}
size_t SetFwdConvolutionTensors(ConvBiasLayer& conv, cudnnTensorDescriptor_t& srcTensorDesc, cudnnTensorDescriptor_t& dstTensorDesc,
cudnnFilterDescriptor_t& filterDesc, cudnnConvolutionDescriptor_t& convDesc,
cudnnConvolutionFwdAlgo_t& algo)
{
size_t sizeInBytes = 0;
int n = m_batchSize;
int c = conv.in_channels;
int h = conv.in_height;
int w = conv.in_width;
checkCUDNN(cudnnSetTensor4dDescriptor(srcTensorDesc,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
n, c,
h, w));
checkCUDNN(cudnnSetFilter4dDescriptor(filterDesc,
CUDNN_DATA_FLOAT,
CUDNN_TENSOR_NCHW,
conv.out_channels,
conv.in_channels,
conv.kernel_size,
conv.kernel_size));
#if CUDNN_MAJOR > 5
checkCUDNN(cudnnSetConvolution2dDescriptor(convDesc,
0, 0,
1, 1,
1, 1,
CUDNN_CROSS_CORRELATION,
CUDNN_DATA_FLOAT));
#else
checkCUDNN(cudnnSetConvolution2dDescriptor(convDesc,
0, 0,
1, 1,
1, 1,
CUDNN_CROSS_CORRELATION));
#endif
// Find dimension of convolution output
checkCUDNN(cudnnGetConvolution2dForwardOutputDim(convDesc,
srcTensorDesc,
filterDesc,
&n, &c, &h, &w));
checkCUDNN(cudnnSetTensor4dDescriptor(dstTensorDesc,
CUDNN_TENSOR_NCHW,
CUDNN_DATA_FLOAT,
n, c,
h, w));
checkCUDNN(cudnnGetConvolutionForwardAlgorithm(cudnnHandle,
srcTensorDesc,
filterDesc,
convDesc,
dstTensorDesc,
CUDNN_CONVOLUTION_FWD_PREFER_FASTEST,
0,
&algo));
checkCUDNN(cudnnGetConvolutionForwardWorkspaceSize(cudnnHandle,
srcTensorDesc,
filterDesc,
convDesc,
dstTensorDesc,
algo,
&sizeInBytes));
return sizeInBytes;
}
void ForwardPropagation(float *data, float *conv1, float *pool1, float *conv2, float *pool2, float *fc1, float *fc1relu,
float *fc2, float *result,
float *pconv1, float *pconv1bias,
float *pconv2, float *pconv2bias,
float *pfc1, float *pfc1bias,
float *pfc2, float *pfc2bias, void *workspace, float *onevec)
{
float alpha = 1.0f, beta = 0.0f;
checkCudaErrors(cudaSetDevice(m_gpuid));
// Conv1 layer
checkCUDNN(cudnnConvolutionForward(cudnnHandle, &alpha, dataTensor,
data, conv1filterDesc, pconv1, conv1Desc,
conv1algo, workspace, m_workspaceSize, &beta,
conv1Tensor, conv1));
checkCUDNN(cudnnAddTensor(cudnnHandle, &alpha, conv1BiasTensor,
pconv1bias, &alpha, conv1Tensor, conv1));
// Pool1 layer
checkCUDNN(cudnnPoolingForward(cudnnHandle, poolDesc, &alpha, conv1Tensor,
conv1, &beta, pool1Tensor, pool1));
// Conv2 layer
checkCUDNN(cudnnConvolutionForward(cudnnHandle, &alpha, pool1Tensor,
pool1, conv2filterDesc, pconv2, conv2Desc,
conv2algo, workspace, m_workspaceSize, &beta,
conv2Tensor, conv2));
checkCUDNN(cudnnAddTensor(cudnnHandle, &alpha, conv2BiasTensor,
pconv2bias, &alpha, conv2Tensor, conv2));
// Pool2 layer
checkCUDNN(cudnnPoolingForward(cudnnHandle, poolDesc, &alpha, conv2Tensor,
conv2, &beta, pool2Tensor, pool2));
// FC1 layer
// Forward propagate neurons using weights (fc1 = pfc1'*pool2)
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_T, CUBLAS_OP_N,
ref_fc1.outputs, m_batchSize, ref_fc1.inputs,
&alpha,
pfc1, ref_fc1.inputs,
pool2, ref_fc1.inputs,
&beta,
fc1, ref_fc1.outputs));
// Add bias using GEMM's "beta" (fc1 += pfc1bias*1_vec')
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_N,
ref_fc1.outputs, m_batchSize, 1,
&alpha,
pfc1bias, ref_fc1.outputs,
onevec, 1,
&alpha,
fc1, ref_fc1.outputs));
// ReLU activation
checkCUDNN(cudnnActivationForward(cudnnHandle, fc1Activation, &alpha,
fc1Tensor, fc1, &beta, fc1Tensor, fc1relu));
// FC2 layer
// Forward propagate neurons using weights (fc2 = pfc2'*fc1relu)
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_T, CUBLAS_OP_N,
ref_fc2.outputs, m_batchSize, ref_fc2.inputs,
&alpha,
pfc2, ref_fc2.inputs,
fc1relu, ref_fc2.inputs,
&beta,
fc2, ref_fc2.outputs));
// Add bias using GEMM's "beta" (fc2 += pfc2bias*1_vec')
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_N,
ref_fc2.outputs, m_batchSize, 1,
&alpha,
pfc2bias, ref_fc2.outputs,
onevec, 1,
&alpha,
fc2, ref_fc2.outputs));
// Softmax loss
checkCUDNN(cudnnSoftmaxForward(cudnnHandle, CUDNN_SOFTMAX_ACCURATE, CUDNN_SOFTMAX_MODE_CHANNEL,
&alpha, fc2Tensor, fc2, &beta, fc2Tensor, result));
}
size_t SetBwdConvolutionTensors(cudnnTensorDescriptor_t& srcTensorDesc, cudnnTensorDescriptor_t& dstTensorDesc,
cudnnFilterDescriptor_t& filterDesc, cudnnConvolutionDescriptor_t& convDesc,
cudnnConvolutionBwdFilterAlgo_t *falgo, cudnnConvolutionBwdDataAlgo_t *dalgo)
{
size_t sizeInBytes = 0, tmpsize = 0;
// If backprop filter algorithm was requested
if (falgo)
{
checkCUDNN(cudnnGetConvolutionBackwardFilterAlgorithm(
cudnnHandle, srcTensorDesc, dstTensorDesc, convDesc, filterDesc,
CUDNN_CONVOLUTION_BWD_FILTER_PREFER_FASTEST, 0, falgo));
checkCUDNN(cudnnGetConvolutionBackwardFilterWorkspaceSize(
cudnnHandle, srcTensorDesc, dstTensorDesc, convDesc, filterDesc,
*falgo, &tmpsize));
sizeInBytes = std::max(sizeInBytes, tmpsize);
}
// If backprop data algorithm was requested
if (dalgo)
{
checkCUDNN(cudnnGetConvolutionBackwardDataAlgorithm(
cudnnHandle, filterDesc, dstTensorDesc, convDesc, srcTensorDesc,
CUDNN_CONVOLUTION_BWD_DATA_PREFER_FASTEST, 0, dalgo));
checkCUDNN(cudnnGetConvolutionBackwardDataWorkspaceSize(
cudnnHandle, filterDesc, dstTensorDesc, convDesc, srcTensorDesc,
*dalgo, &tmpsize));
sizeInBytes = std::max(sizeInBytes, tmpsize);
}
return sizeInBytes;
}
void Backpropagation(ConvBiasLayer& layer_conv1, MaxPoolLayer& layer_pool1, ConvBiasLayer& layer_conv2, MaxPoolLayer& layer_pool2,
float *data, float *labels, float *conv1, float *pool1, float *conv2, float *pool2, float *fc1, float *fc1relu,
float *fc2, float *fc2smax, float *dloss_data,
float *pconv1, float *pconv1bias,
float *pconv2, float *pconv2bias,
float *pfc1, float *pfc1bias,
float *pfc2, float *pfc2bias,
float *gconv1, float *gconv1bias, float *dpool1,
float *gconv2, float *gconv2bias, float *dconv2, float *dpool2,
float *gfc1, float *gfc1bias, float *dfc1, float *dfc1relu,
float *gfc2, float *gfc2bias, float *dfc2,
void *workspace, float *onevec)
{
float alpha = 1.0f, beta = 0.0f;
float scalVal = 1.0f / static_cast<float>(m_batchSize);
checkCudaErrors(cudaSetDevice(m_gpuid));
// Initialization (using the training error function)
checkCudaErrors(cudaMemcpyAsync(dloss_data, fc2smax, sizeof(float) * m_batchSize * ref_fc2.outputs, cudaMemcpyDeviceToDevice));
// Softmax layer
SoftmaxLossBackprop<<<RoundUp(m_batchSize, BW), BW>>>(labels, ref_fc2.outputs, m_batchSize, dloss_data);
// Accounting for batch size in SGD
checkCudaErrors(cublasSscal(cublasHandle, ref_fc2.outputs * m_batchSize, &scalVal, dloss_data, 1));
// FC2 layer
// Compute derivative with respect to weights: gfc2 = (fc1relu * dfc2smax')
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_T, ref_fc2.inputs, ref_fc2.outputs, m_batchSize,
&alpha, fc1relu, ref_fc2.inputs, dloss_data, ref_fc2.outputs, &beta, gfc2, ref_fc2.inputs));
// Compute derivative with respect to bias: gfc2bias = dfc2smax * 1_vec
checkCudaErrors(cublasSgemv(cublasHandle, CUBLAS_OP_N, ref_fc2.outputs, m_batchSize,
&alpha, dloss_data, ref_fc2.outputs, onevec, 1, &beta, gfc2bias, 1));
// Compute derivative with respect to data (for previous layer): pfc2*dfc2smax (500x10*10xN)
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_N, ref_fc2.inputs, m_batchSize, ref_fc2.outputs,
&alpha, pfc2, ref_fc2.inputs, dloss_data, ref_fc2.outputs, &beta, dfc2, ref_fc2.inputs));
// ReLU activation
checkCUDNN(cudnnActivationBackward(cudnnHandle, fc1Activation, &alpha,
fc1Tensor, fc1relu, fc1Tensor, dfc2,
fc1Tensor, fc1, &beta, fc1Tensor, dfc1relu));
// FC1 layer
// Compute derivative with respect to weights: gfc1 = (pool2 * dfc1relu')
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_T, ref_fc1.inputs, ref_fc1.outputs, m_batchSize,
&alpha, pool2, ref_fc1.inputs, dfc1relu, ref_fc1.outputs, &beta, gfc1, ref_fc1.inputs));
// Compute derivative with respect to bias: gfc1bias = dfc1relu * 1_vec
checkCudaErrors(cublasSgemv(cublasHandle, CUBLAS_OP_N, ref_fc1.outputs, m_batchSize,
&alpha, dfc1relu, ref_fc1.outputs, onevec, 1, &beta, gfc1bias, 1));
// Compute derivative with respect to data (for previous layer): pfc1*dfc1relu (800x500*500xN)
checkCudaErrors(cublasSgemm(cublasHandle, CUBLAS_OP_N, CUBLAS_OP_N, ref_fc1.inputs, m_batchSize, ref_fc1.outputs,
&alpha, pfc1, ref_fc1.inputs, dfc1relu, ref_fc1.outputs, &beta, dfc1, ref_fc1.inputs));
// Pool2 layer
checkCUDNN(cudnnPoolingBackward(cudnnHandle, poolDesc, &alpha,
pool2Tensor, pool2, pool2Tensor, dfc1,
conv2Tensor, conv2, &beta, conv2Tensor, dpool2));
// Conv2 layer
checkCUDNN(cudnnConvolutionBackwardBias(cudnnHandle, &alpha, conv2Tensor,
dpool2, &beta, conv2BiasTensor, gconv2bias));
checkCUDNN(cudnnConvolutionBackwardFilter(cudnnHandle, &alpha, pool1Tensor,
pool1, conv2Tensor, dpool2, conv2Desc,
conv2bwfalgo, workspace, m_workspaceSize,
&beta, conv2filterDesc, gconv2));
checkCUDNN(cudnnConvolutionBackwardData(cudnnHandle, &alpha, conv2filterDesc,
pconv2, conv2Tensor, dpool2, conv2Desc,
conv2bwdalgo, workspace, m_workspaceSize,
&beta, pool1Tensor, dconv2));
// Pool1 layer
checkCUDNN(cudnnPoolingBackward(cudnnHandle, poolDesc, &alpha,
pool1Tensor, pool1, pool1Tensor, dconv2,
conv1Tensor, conv1, &beta, conv1Tensor, dpool1));
// Conv1 layer
checkCUDNN(cudnnConvolutionBackwardBias(cudnnHandle, &alpha, conv1Tensor,
dpool1, &beta, conv1BiasTensor, gconv1bias));
checkCUDNN(cudnnConvolutionBackwardFilter(cudnnHandle, &alpha, dataTensor,
data, conv1Tensor, dpool1, conv1Desc,
conv1bwfalgo, workspace, m_workspaceSize,
&beta, conv1filterDesc, gconv1));
// No need for convBackwardData because there are no more layers below
}
void UpdateWeights(float learning_rate,
ConvBiasLayer& conv1, ConvBiasLayer& conv2,
float *pconv1, float *pconv1bias,
float *pconv2, float *pconv2bias,
float *pfc1, float *pfc1bias,
float *pfc2, float *pfc2bias,
float *gconv1, float *gconv1bias,
float *gconv2, float *gconv2bias,
float *gfc1, float *gfc1bias,
float *gfc2, float *gfc2bias)
{
float alpha = -learning_rate;
checkCudaErrors(cudaSetDevice(m_gpuid));
// Conv1
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(conv1.pconv.size()),
&alpha, gconv1, 1, pconv1, 1));
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(conv1.pbias.size()),
&alpha, gconv1bias, 1, pconv1bias, 1));
// Conv2
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(conv2.pconv.size()),
&alpha, gconv2, 1, pconv2, 1));
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(conv2.pbias.size()),
&alpha, gconv2bias, 1, pconv2bias, 1));
// Fully connected 1
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(ref_fc1.pneurons.size()),
&alpha, gfc1, 1, pfc1, 1));
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(ref_fc1.pbias.size()),
&alpha, gfc1bias, 1, pfc1bias, 1));
// Fully connected 2
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(ref_fc2.pneurons.size()),
&alpha, gfc2, 1, pfc2, 1));
checkCudaErrors(cublasSaxpy(cublasHandle, static_cast<int>(ref_fc2.pbias.size()),
&alpha, gfc2bias, 1, pfc2bias, 1));
}
};
///////////////////////////////////////////////////////////////////////////////////////////
// Main function
int main(int argc, char **argv)
{
#ifdef USE_GFLAGS
gflags::ParseCommandLineFlags(&argc, &argv, true);
#endif
size_t width, height, channels = 1;
// Open input data
printf("Reading input data\n");
// Read dataset sizes
size_t train_size = ReadUByteDataset(FLAGS_train_images.c_str(), FLAGS_train_labels.c_str(), nullptr, nullptr, width, height);
size_t test_size = ReadUByteDataset(FLAGS_test_images.c_str(), FLAGS_test_labels.c_str(), nullptr, nullptr, width, height);
if (train_size == 0)
return 1;
std::vector<uint8_t> train_images(train_size * width * height * channels), train_labels(train_size);
std::vector<uint8_t> test_images(test_size * width * height * channels), test_labels(test_size);
// Read data from datasets
if (ReadUByteDataset(FLAGS_train_images.c_str(), FLAGS_train_labels.c_str(), &train_images[0], &train_labels[0], width, height) != train_size)
return 2;
if (ReadUByteDataset(FLAGS_test_images.c_str(), FLAGS_test_labels.c_str(), &test_images[0], &test_labels[0], width, height) != test_size)
return 3;
printf("Done. Training dataset size: %d, Test dataset size: %d\n", (int)train_size, (int)test_size);
printf("Batch size: %lld, iterations: %d\n", FLAGS_batch_size, FLAGS_iterations);
// This code snippet saves a random image and its label
/*
std::random_device rd_image;
int random_image = rd_image() % train_size;
std::stringstream ss; ss << "image-" << (int)train_labels[random_image] << ".pgm";
SavePGMFile(&train_images[0] + random_image * width*height*channels, width, height, ss.str().c_str());
*/
// Choose GPU
int num_gpus;
checkCudaErrors(cudaGetDeviceCount(&num_gpus));
if (FLAGS_gpu < 0 || FLAGS_gpu >= num_gpus)
{
printf("ERROR: Invalid GPU ID %d (There are %d GPUs on this machine)\n",
FLAGS_gpu, num_gpus);
return 4;
}
// Create the LeNet network architecture
ConvBiasLayer conv1((int)channels, 20, 5, (int)width, (int)height);
MaxPoolLayer pool1(2, 2);
ConvBiasLayer conv2(conv1.out_channels, 50, 5, conv1.out_width / pool1.stride, conv1.out_height / pool1.stride);
MaxPoolLayer pool2(2, 2);
FullyConnectedLayer fc1((conv2.out_channels*conv2.out_width*conv2.out_height) / (pool2.stride * pool2.stride),
500);
FullyConnectedLayer fc2(fc1.outputs, 10);
// Initialize CUDNN/CUBLAS training context
TrainingContext context(FLAGS_gpu, FLAGS_batch_size, conv1, pool1, conv2, pool2, fc1, fc2);
// Determine initial network structure
bool bRet = true;
if (FLAGS_pretrained)
{
bRet = conv1.FromFile("conv1");
bRet &= conv2.FromFile("conv2");
bRet &= fc1.FromFile("ip1");
bRet &= fc2.FromFile("ip2");
}
if (!bRet || !FLAGS_pretrained)
{
// Create random network
std::random_device rd;
std::mt19937 gen(FLAGS_random_seed < 0 ? rd() : static_cast<unsigned int>(FLAGS_random_seed));
// Xavier weight filling
float wconv1 = sqrt(3.0f / (conv1.kernel_size * conv1.kernel_size * conv1.in_channels));
std::uniform_real_distribution<> dconv1(-wconv1, wconv1);
float wconv2 = sqrt(3.0f / (conv2.kernel_size * conv2.kernel_size * conv2.in_channels));
std::uniform_real_distribution<> dconv2(-wconv2, wconv2);
float wfc1 = sqrt(3.0f / (fc1.inputs * fc1.outputs));
std::uniform_real_distribution<> dfc1(-wfc1, wfc1);
float wfc2 = sqrt(3.0f / (fc2.inputs * fc2.outputs));
std::uniform_real_distribution<> dfc2(-wfc2, wfc2);
// Randomize network
for (auto&& iter : conv1.pconv)
iter = static_cast<float>(dconv1(gen));
for (auto&& iter : conv1.pbias)
iter = static_cast<float>(dconv1(gen));
for (auto&& iter : conv2.pconv)
iter = static_cast<float>(dconv2(gen));
for (auto&& iter : conv2.pbias)
iter = static_cast<float>(dconv2(gen));
for (auto&& iter : fc1.pneurons)
iter = static_cast<float>(dfc1(gen));
for (auto&& iter : fc1.pbias)
iter = static_cast<float>(dfc1(gen));
for (auto&& iter : fc2.pneurons)
iter = static_cast<float>(dfc2(gen));
for (auto&& iter : fc2.pbias)
iter = static_cast<float>(dfc2(gen));
}
/////////////////////////////////////////////////////////////////////////////
// Create GPU data structures
// Forward propagation data
float *d_data, *d_labels, *d_conv1, *d_pool1, *d_conv2, *d_pool2, *d_fc1, *d_fc1relu, *d_fc2, *d_fc2smax;
// Buffer | Element | N | C | H | W
//-----------------------------------------------------------------------------------------------------------------------------------------
checkCudaErrors(cudaMalloc(&d_data, sizeof(float) * context.m_batchSize * channels * height * width));
checkCudaErrors(cudaMalloc(&d_labels, sizeof(float) * context.m_batchSize * 1 * 1 * 1));
checkCudaErrors(cudaMalloc(&d_conv1, sizeof(float) * context.m_batchSize * conv1.out_channels * conv1.out_height * conv1.out_width));
checkCudaErrors(cudaMalloc(&d_pool1, sizeof(float) * context.m_batchSize * conv1.out_channels * (conv1.out_height / pool1.stride) * (conv1.out_width / pool1.stride)));
checkCudaErrors(cudaMalloc(&d_conv2, sizeof(float) * context.m_batchSize * conv2.out_channels * conv2.out_height * conv2.out_width));
checkCudaErrors(cudaMalloc(&d_pool2, sizeof(float) * context.m_batchSize * conv2.out_channels * (conv2.out_height / pool2.stride) * (conv2.out_width / pool2.stride)));
checkCudaErrors(cudaMalloc(&d_fc1, sizeof(float) * context.m_batchSize * fc1.outputs));
checkCudaErrors(cudaMalloc(&d_fc1relu, sizeof(float) * context.m_batchSize * fc1.outputs));
checkCudaErrors(cudaMalloc(&d_fc2, sizeof(float) * context.m_batchSize * fc2.outputs));
checkCudaErrors(cudaMalloc(&d_fc2smax, sizeof(float) * context.m_batchSize * fc2.outputs));
// Network parameters
float *d_pconv1, *d_pconv1bias, *d_pconv2, *d_pconv2bias;
float *d_pfc1, *d_pfc1bias, *d_pfc2, *d_pfc2bias;
checkCudaErrors(cudaMalloc(&d_pconv1, sizeof(float) * conv1.pconv.size()));
checkCudaErrors(cudaMalloc(&d_pconv1bias, sizeof(float) * conv1.pbias.size()));
checkCudaErrors(cudaMalloc(&d_pconv2, sizeof(float) * conv2.pconv.size()));
checkCudaErrors(cudaMalloc(&d_pconv2bias, sizeof(float) * conv2.pbias.size()));
checkCudaErrors(cudaMalloc(&d_pfc1, sizeof(float) * fc1.pneurons.size()));
checkCudaErrors(cudaMalloc(&d_pfc1bias, sizeof(float) * fc1.pbias.size()));
checkCudaErrors(cudaMalloc(&d_pfc2, sizeof(float) * fc2.pneurons.size()));
checkCudaErrors(cudaMalloc(&d_pfc2bias, sizeof(float) * fc2.pbias.size()));
// Network parameter gradients
float *d_gconv1, *d_gconv1bias, *d_gconv2, *d_gconv2bias;
float *d_gfc1, *d_gfc1bias, *d_gfc2, *d_gfc2bias;
checkCudaErrors(cudaMalloc(&d_gconv1, sizeof(float) * conv1.pconv.size()));
checkCudaErrors(cudaMalloc(&d_gconv1bias, sizeof(float) * conv1.pbias.size()));
checkCudaErrors(cudaMalloc(&d_gconv2, sizeof(float) * conv2.pconv.size()));
checkCudaErrors(cudaMalloc(&d_gconv2bias, sizeof(float) * conv2.pbias.size()));
checkCudaErrors(cudaMalloc(&d_gfc1, sizeof(float) * fc1.pneurons.size()));
checkCudaErrors(cudaMalloc(&d_gfc1bias, sizeof(float) * fc1.pbias.size()));
checkCudaErrors(cudaMalloc(&d_gfc2, sizeof(float) * fc2.pneurons.size()));
checkCudaErrors(cudaMalloc(&d_gfc2bias, sizeof(float) * fc2.pbias.size()));
// Differentials w.r.t. data
float *d_dpool1, *d_dpool2, *d_dconv2, *d_dfc1, *d_dfc1relu, *d_dfc2, *d_dfc2smax, *d_dlossdata;
// Buffer | Element | N | C | H | W
//-----------------------------------------------------------------------------------------------------------------------------------------
checkCudaErrors(cudaMalloc(&d_dpool1, sizeof(float) * context.m_batchSize * conv1.out_channels * conv1.out_height * conv1.out_width));
checkCudaErrors(cudaMalloc(&d_dpool2, sizeof(float) * context.m_batchSize * conv2.out_channels * conv2.out_height * conv2.out_width));
checkCudaErrors(cudaMalloc(&d_dconv2, sizeof(float) * context.m_batchSize * conv1.out_channels * (conv1.out_height / pool1.stride) * (conv1.out_width / pool1.stride)));
checkCudaErrors(cudaMalloc(&d_dfc1, sizeof(float) * context.m_batchSize * fc1.inputs));
checkCudaErrors(cudaMalloc(&d_dfc1relu, sizeof(float) * context.m_batchSize * fc1.outputs));
checkCudaErrors(cudaMalloc(&d_dfc2, sizeof(float) * context.m_batchSize * fc2.inputs));
checkCudaErrors(cudaMalloc(&d_dfc2smax, sizeof(float) * context.m_batchSize * fc2.outputs));
checkCudaErrors(cudaMalloc(&d_dlossdata,sizeof(float) * context.m_batchSize * fc2.outputs));
// Temporary buffers and workspaces
float *d_onevec;
void *d_cudnn_workspace = nullptr;
checkCudaErrors(cudaMalloc(&d_onevec, sizeof(float)* context.m_batchSize));
if (context.m_workspaceSize > 0)
checkCudaErrors(cudaMalloc(&d_cudnn_workspace, context.m_workspaceSize));
/////////////////////////////////////////////////////////////////////////////
// Copy initial network to device
checkCudaErrors(cudaMemcpyAsync(d_pconv1, &conv1.pconv[0], sizeof(float) * conv1.pconv.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pconv1bias, &conv1.pbias[0], sizeof(float) * conv1.pbias.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pconv2, &conv2.pconv[0], sizeof(float) * conv2.pconv.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pconv2bias, &conv2.pbias[0], sizeof(float) * conv2.pbias.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pfc1, &fc1.pneurons[0], sizeof(float) * fc1.pneurons.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pfc1bias, &fc1.pbias[0], sizeof(float) * fc1.pbias.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pfc2, &fc2.pneurons[0], sizeof(float) * fc2.pneurons.size(), cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_pfc2bias, &fc2.pbias[0], sizeof(float) * fc2.pbias.size(), cudaMemcpyHostToDevice));
// Fill one-vector with ones
FillOnes<<<RoundUp(context.m_batchSize, BW), BW>>>(d_onevec, context.m_batchSize);
printf("Preparing dataset\n");
// Normalize training set to be in [0,1]
std::vector<float> train_images_float(train_images.size()), train_labels_float(train_size);
for (size_t i = 0; i < train_size * channels * width * height; ++i)
train_images_float[i] = (float)train_images[i] / 255.0f;
for (size_t i = 0; i < train_size; ++i)
train_labels_float[i] = (float)train_labels[i];
printf("Training...\n");
// Use SGD to train the network
checkCudaErrors(cudaDeviceSynchronize());
auto t1 = std::chrono::high_resolution_clock::now();
for (int iter = 0; iter < FLAGS_iterations; ++iter)
{
// Train
int imageid = iter % (train_size / context.m_batchSize);
// Prepare current batch on device
checkCudaErrors(cudaMemcpyAsync(d_data, &train_images_float[imageid * context.m_batchSize * width*height*channels],
sizeof(float) * context.m_batchSize * channels * width * height, cudaMemcpyHostToDevice));
checkCudaErrors(cudaMemcpyAsync(d_labels, &train_labels_float[imageid * context.m_batchSize],
sizeof(float) * context.m_batchSize, cudaMemcpyHostToDevice));
// Forward propagation
context.ForwardPropagation(d_data, d_conv1, d_pool1, d_conv2, d_pool2, d_fc1, d_fc1relu, d_fc2, d_fc2smax,
d_pconv1, d_pconv1bias, d_pconv2, d_pconv2bias, d_pfc1, d_pfc1bias, d_pfc2, d_pfc2bias,
d_cudnn_workspace, d_onevec);
// Backward propagation
context.Backpropagation(conv1, pool1, conv2, pool2,
d_data, d_labels, d_conv1, d_pool1, d_conv2, d_pool2, d_fc1, d_fc1relu, d_fc2, d_fc2smax, d_dlossdata,
d_pconv1, d_pconv1bias, d_pconv2, d_pconv2bias, d_pfc1, d_pfc1bias, d_pfc2, d_pfc2bias,
d_gconv1, d_gconv1bias, d_dpool1, d_gconv2, d_gconv2bias, d_dconv2, d_dpool2, d_gfc1, d_gfc1bias,
d_dfc1, d_dfc1relu, d_gfc2, d_gfc2bias, d_dfc2, d_cudnn_workspace, d_onevec);
// Compute learning rate
float learningRate = static_cast<float>(FLAGS_learning_rate * pow((1.0 + FLAGS_lr_gamma * iter), (-FLAGS_lr_power)));
// Update weights