-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cu
225 lines (175 loc) · 6.46 KB
/
main.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
#define USE_MNIST_LOADER
#define MNIST_DOUBLE
#include "mnist.h"
#include "layer.h"
#include <cuda.h>
#include <cstdio>
#include <time.h>
static mnist_data *train_set, *test_set;
static unsigned int train_cnt, test_cnt; // count: the number of samples in train/test set
// Define layers of CNN
static Layer l_input = Layer(0, 0, 28*28);
static Layer l_c1 = Layer(5*5, 6, 24*24*6); // kernal_size: 5*5, kernal_channel: 6, output_size: 24*24*6
static Layer l_s1 = Layer(4*4, 1, 6*6*6);
static Layer l_f = Layer(6*6*6, 10, 10);
static void learn();
static unsigned int classify(double data[28][28]);
static void test();
static double forward_pass(double data[28][28]);
static double back_pass();
static inline void loaddata()
{
mnist_load("data/train-images.idx3-ubyte", "data/train-labels.idx1-ubyte",
&train_set, &train_cnt);
mnist_load("data/t10k-images.idx3-ubyte", "data/t10k-labels.idx1-ubyte",
&test_set, &test_cnt);
}
int main(int argc, const char **argv)
{
srand(time(NULL));
CUresult err = cuInit(0);
if (err != CUDA_SUCCESS) {
fprintf(stderr, "CUDA initialisation failed with error code - %d\n", err);
return 1;
}
printf("train_cnt: %d, test_cnt: %d\n", train_cnt, test_cnt);
loaddata();
printf("train_cnt: %d, test_cnt: %d\n", train_cnt, test_cnt);
learn();
test();
return 0;
}
// Forward propagation of a single row in dataset
static double forward_pass(double data[28][28])
{
float input[28][28];
for (int i = 0; i < 28; ++i) {
for (int j = 0; j < 28; ++j) {
input[i][j] = data[i][j];
}
}
l_input.clear();
l_c1.clear();
l_s1.clear();
l_f.clear();
clock_t start, end;
start = clock();
l_input.setOutput((float *)input);
// convolution layer: 1×28×28 --> 6×24×24 (kernal: 6×5×5, stride: 1)
fp_preact_c1<<<64, 64>>>((float (*)[28])l_input.output, (float (*)[24][24])l_c1.preact, (float (*)[5][5])l_c1.weight);
fp_bias_c1<<<64, 64>>>((float (*)[24][24])l_c1.preact, l_c1.bias);
// apply sigmoid activation function: 1 / (1 + exp(-x))
apply_step_function<<<64, 64>>>(l_c1.preact, l_c1.output, l_c1.O); // l_c1.O = 24*24*6
// convolution layer: 6×24×24 --> 6×6×6 (kernal: 1×4×4, stride: 4)
fp_preact_s1<<<64, 64>>>((float (*)[24][24])l_c1.output, (float (*)[6][6])l_s1.preact, (float (*)[4][4])l_s1.weight);
fp_bias_s1<<<64, 64>>>((float (*)[6][6])l_s1.preact, l_s1.bias);
// apply sigmoid activation function: 1 / (1 + exp(-x))
apply_step_function<<<64, 64>>>(l_s1.preact, l_s1.output, l_s1.O);
// fully connected layer: 6×6×6 --> 10
fp_preact_f<<<64, 64>>>((float (*)[6][6])l_s1.output, l_f.preact, (float (*)[6][6][6])l_f.weight);
fp_bias_f<<<64, 64>>>(l_f.preact, l_f.bias);
apply_step_function<<<64, 64>>>(l_f.preact, l_f.output, l_f.O);
end = clock();
return ((double) (end - start)) / CLOCKS_PER_SEC;
}
// Back propagation to update weights
static double back_pass()
{
clock_t start, end;
start = clock();
bp_weight_f<<<64, 64>>>((float (*)[6][6][6])l_f.d_weight, l_f.d_preact, (float (*)[6][6])l_s1.output);
bp_bias_f<<<64, 64>>>(l_f.bias, l_f.d_preact);
bp_output_s1<<<64, 64>>>((float (*)[6][6])l_s1.d_output, (float (*)[6][6][6])l_f.weight, l_f.d_preact);
bp_preact_s1<<<64, 64>>>((float (*)[6][6])l_s1.d_preact, (float (*)[6][6])l_s1.d_output, (float (*)[6][6])l_s1.preact);
bp_weight_s1<<<64, 64>>>((float (*)[4][4])l_s1.d_weight, (float (*)[6][6])l_s1.d_preact, (float (*)[24][24])l_c1.output);
bp_bias_s1<<<64, 64>>>(l_s1.bias, (float (*)[6][6])l_s1.d_preact);
bp_output_c1<<<64, 64>>>((float (*)[24][24])l_c1.d_output, (float (*)[4][4])l_s1.weight, (float (*)[6][6])l_s1.d_preact);
bp_preact_c1<<<64, 64>>>((float (*)[24][24])l_c1.d_preact, (float (*)[24][24])l_c1.d_output, (float (*)[24][24])l_c1.preact);
bp_weight_c1<<<64, 64>>>((float (*)[5][5])l_c1.d_weight, (float (*)[24][24])l_c1.d_preact, (float (*)[28])l_input.output);
bp_bias_c1<<<64, 64>>>(l_c1.bias, (float (*)[24][24])l_c1.d_preact);
apply_grad<<<64, 64>>>(l_f.weight, l_f.d_weight, l_f.M * l_f.N);
apply_grad<<<64, 64>>>(l_s1.weight, l_s1.d_weight, l_s1.M * l_s1.N);
apply_grad<<<64, 64>>>(l_c1.weight, l_c1.d_weight, l_c1.M * l_c1.N);
end = clock();
// return time taken
return ((double) (end - start)) / CLOCKS_PER_SEC;
}
// Unfold the input layer
static void unfold_input(double input[28][28], double unfolded[24*24][5*5])
{
int a = 0;
(void)unfold_input;
for (int i = 0; i < 2; ++i)
for (int j = 0; j < 2; ++j) {
int b = 0;
for (int x = i; x < i + 2; ++x)
for (int y = j; y < j+2; ++y)
unfolded[a][b++] = input[x][y];
a++;
}
}
static void learn()
{
// cuBLAS is an abbreviation of cuda basic linear algebra subprograms.
// NVIDIA cuBLAS is a GPU-accelerated library for accelerating AI and HPC applications.
// It is used here for matrix multiplication.
static cublasHandle_t blas;
cublasCreate(&blas);
float err;
int iter = 10;
double time_taken = 0.0;
fprintf(stdout ,"Learning\n");
// 如果设置iter=-1, 相当于while(true), 直到err < threshold执行break退出循环
while (iter < 0 || iter-- > 0) {
printf("iter: %d\n", iter);
err = 0.0f;
for (int i = 0; i < train_cnt; ++i) {
float tmp_err;
time_taken += forward_pass(train_set[i].data); // <-- forward propagation
// set d_output, d_preact, d_weight to 0x00 respectively
l_f.bp_clear();
l_s1.bp_clear();
l_c1.bp_clear();
// Euclid distance of train_set[i]
makeError<<<10, 1>>>(l_f.d_preact, l_f.output, train_set[i].label, 10); // <-- d_preact
// https://docs.nvidia.com/cuda/cublas/#cublas-t-nrm2
// cublas + S + nrm2, where "S" means single-precision (float)
cublasSnrm2(blas, 10, l_f.d_preact, 1, &tmp_err);
err += tmp_err;
time_taken += back_pass(); // <-- back propagation
}
err /= train_cnt;
fprintf(stdout, "error: %e, time_on_gpu: %lf\n", err, time_taken);
if (err < threshold) {
fprintf(stdout, "Training complete, error less than threshold\n\n");
break;
}
}
fprintf(stdout, "\n Time - %lf\n", time_taken);
}
// Returns label of given data (0-9)
static unsigned int classify(double data[28][28])
{
float res[10];
forward_pass(data);
unsigned int max = 0;
cudaMemcpy(res, l_f.output, sizeof(float) * 10, cudaMemcpyDeviceToHost);
for (int i = 1; i < 10; ++i) {
if (res[max] < res[i]) {
max = i;
}
}
return max;
}
// Perform forward propagation of test data
static void test()
{
int error = 0;
for (int i = 0; i < test_cnt; ++i) {
if (classify(test_set[i].data) != test_set[i].label) {
++error;
}
}
fprintf(stdout, "Error Rate: %.2lf%%\n",
double(error) / double(test_cnt) * 100.0);
}