-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcifar10_vgg.cu
368 lines (309 loc) · 12.3 KB
/
cifar10_vgg.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
/** @file cifar10_vgg.cu
* @brief A VGG-like network for CIFAR10.
* @author Ang Li (PNNL)
*
*/
#include <stdio.h>
#include <assert.h>
#include <sys/time.h>
#include <iostream>
#include <string>
#include <cooperative_groups.h>
#include <iostream>
#include <fstream>
#include <vector>
#include "utility.h"
#include "sbnn32_param.h"
#include "sbnn64_param.h"
#include "sbnn32.cuh"
#include "sbnn64.cuh"
#include "data.h"
using namespace cooperative_groups;
using namespace std;
//int main32();
int main64();
int main()
{
//main32();
main64();
}
__global__ void vggnet32(
In32Conv32LayerParam* bconv1,
Conv32LayerParam* bconv2,
Conv32LayerParam* bconv3,
Conv32LayerParam* bconv4,
Conv32LayerParam* bconv5,
Conv32LayerParam* bconv6,
Fc32LayerParam* bfc1,
Fc32LayerParam* bfc2,
Out32LayerParam* bout)
{
grid_group grid = this_grid();
//========= Conv1 ============
In32Conv32Layer(bconv1);
grid.sync();
//========= Conv2 ============
ConvPool32Layer(bconv2);
grid.sync();
//========= Conv3 ============
Conv32Layer(bconv3);
grid.sync();
//========= Conv4 ============
ConvPool32Layer(bconv4);
grid.sync();
//========= Conv5 ============
Conv32Layer(bconv5);
grid.sync();
//========= Conv6 ============
ConvPool32Layer(bconv6);
grid.sync();
//========= Fc1 ============
Fc32Layer(bfc1);
//Fc32LayerBatched(bfc1);
grid.sync();
//========= Fc2 ============
Fc32Layer(bfc2);
//Fc32LayerBatched(bfc2);
grid.sync();
////========== Output ===========
Out32Layer(bout);
//Out32LayerBatched(bout);
}
__global__ void vggnet64(
In32Conv64LayerParam* bconv1,
Conv64LayerParam* bconv2,
Conv64LayerParam* bconv3,
Conv64LayerParam* bconv4,
Conv64LayerParam* bconv5,
Conv64LayerParam* bconv6,
Fc64LayerParam* bfc1,
Fc64LayerParam* bfc2,
Out64LayerParam* bout)
{
grid_group grid = this_grid();
SET_KERNEL_TIMER;
//========= Conv1 ============
In32Conv64Layer(bconv1);
grid.sync();
TICK_KERNEL_TIMER(bconv1);
//========= Conv2 ============
ConvPool64Layer(bconv2);
grid.sync();
TICK_KERNEL_TIMER(bconv2);
//========= Conv3 ============
Conv64Layer(bconv3);
grid.sync();
TICK_KERNEL_TIMER(bconv3);
//========= Conv4 ============
ConvPool64Layer(bconv4);
grid.sync();
TICK_KERNEL_TIMER(bconv4);
//========= Conv5 ============
Conv64Layer(bconv5);
grid.sync();
TICK_KERNEL_TIMER(bconv5);
//========= Conv6 ============
ConvPool64Layer(bconv6);
grid.sync();
TICK_KERNEL_TIMER(bconv6);
//========= Fc1 ============
//Fc64Layer(bfc1);
Fc64LayerBatched(bfc1);
grid.sync();
TICK_KERNEL_TIMER(bfc1);
//========= Fc2 ============
//Fc64Layer(bfc2);
Fc64LayerBatched(bfc2);
grid.sync();
TICK_KERNEL_TIMER(bfc2);
////========== Output ===========
//Out64Layer(bout);
Out64LayerBatched(bout);
TICK_KERNEL_TIMER(bout);
}
int main32()
{
int dev = 4;
cudaSetDevice(dev);
const unsigned batch = 64;
const unsigned output_size = 10;
const unsigned image_height = 32;
const unsigned image_width = 32;
const unsigned image_channel = 3;
const unsigned filter_height = 3;
const unsigned filter_width = 3;
const unsigned n_hidden = 1024;
//=============== Get Input and Label =================
float* images = (float*)malloc(batch*image_height*image_width*image_channel*sizeof(float));
unsigned* image_labels = (unsigned*)malloc(batch*sizeof(unsigned));
string cifar10_dir = "/home/lian599/raid/data/cifar10c/test_batch.bin";
read_CIFAR10_normalized(cifar10_dir, images, image_labels, batch);
//================ Get Weight =================
//FILE* config_file = fopen("./cifar10.config","r");
FILE* config_file = fopen("./pytorch_training/vgg_cifar10.csv","r");
//================ Set Network =================
//Bconv1 Layer
In32Conv32LayerParam* bconv1 = new In32Conv32LayerParam("Conv1", image_height, image_width,
filter_height, filter_width, 3, 128, batch);
In32Conv32LayerParam* bconv1_gpu = bconv1->initialize(images, config_file);
//Bconv2 Layer
Conv32LayerParam* bconv2 = new Conv32LayerParam("Conv2", bconv1->output_height,
bconv1->output_width, filter_height, filter_width, 128, 128, batch, 1, 1,
true, 2, 2, false);
Conv32LayerParam* bconv2_gpu = bconv2->initialize(config_file, bconv1->get_output_gpu());
//Bconv3 Layer
Conv32LayerParam* bconv3 = new Conv32LayerParam("Conv3", bconv2->output_height,
bconv2->output_width, filter_height, filter_width, 128, 256, batch);
Conv32LayerParam* bconv3_gpu = bconv3->initialize(config_file, bconv2->get_output_gpu());
//Bconv4 Layer
Conv32LayerParam* bconv4 = new Conv32LayerParam("Conv4", bconv3->output_height,
bconv3->output_width, filter_height, filter_width, 256, 256, batch, 1, 1,
true, 2, 2, false);
Conv32LayerParam* bconv4_gpu = bconv4->initialize(config_file, bconv3->get_output_gpu());
//Bconv5 Layer
Conv32LayerParam* bconv5 = new Conv32LayerParam("Conv5", bconv4->output_height,
bconv4->output_width, filter_height, filter_width, 256, 512, batch);
Conv32LayerParam* bconv5_gpu = bconv5->initialize(config_file, bconv4->get_output_gpu());
//Bconv6 Layer
Conv32LayerParam* bconv6 = new Conv32LayerParam("Conv6", bconv5->output_height,
bconv5->output_width, filter_height, filter_width, 512, 512, batch, 1, 1,
true, 2, 2, true);
Conv32LayerParam* bconv6_gpu = bconv6->initialize(config_file, bconv5->get_output_gpu());
//Fc1 Layer
Fc32LayerParam* bfc1 = new Fc32LayerParam("Fc1", batch, (bconv6->output_height)
*(bconv6->output_width)*512, n_hidden);
Fc32LayerParam* bfc1_gpu = bfc1->initialize(config_file, bconv6->get_output_gpu());
//Fc2 Layer
Fc32LayerParam* bfc2 = new Fc32LayerParam("Fc2", batch, n_hidden, n_hidden);
Fc32LayerParam* bfc2_gpu = bfc2->initialize(config_file, bfc1->get_output_gpu());
//Out Layer
Out32LayerParam* bout = new Out32LayerParam("Fout", batch, n_hidden, output_size, true);
Out32LayerParam* bout_gpu = bout->initialize(config_file, bfc2->get_output_gpu());
//================ Setup Kernel =================
int numThreads = 1024;
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
int numBlocksPerSm;
int shared_memory = 512*sizeof(int)*32;
cudaFuncSetAttribute(vggnet32, cudaFuncAttributeMaxDynamicSharedMemorySize,98304);
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&numBlocksPerSm, vggnet32, numThreads, shared_memory);
//cudaFuncSetAttribute(alexnet32, cudaFuncAttributePreferredSharedMemoryCarveout,0);
void* args[] = {&bconv1_gpu, &bconv2_gpu, &bconv3_gpu, &bconv4_gpu, &bconv5_gpu, &bconv6_gpu,
&bfc1_gpu, &bfc2_gpu, &bout_gpu};
START_TIMER;
cudaLaunchCooperativeKernel((void*)vggnet32, numBlocksPerSm*deviceProp.multiProcessorCount,
numThreads, args, shared_memory);
STOP_TIMER;
//float* ss = bfc1->download_full_output();
//int a = 20980;
//int b = 21080;
//int max_width = 4;
//for (int i=a; i<b; i++)
//{
//printf("%*.0f ",max_width, ss[i]);
//if ( (i-a+1)%18 == 0)
//printf("\n");
//}
//printf("\n");
//================ Output =================
float* output = bout->download_output();
validate_prediction(output, image_labels, output_size, batch);
delete bconv1;
delete bconv2;
delete bconv3;
delete bconv4;
delete bconv5;
delete bconv6;
delete bfc1;
delete bfc2;
delete bout;
return 0;
}
int main64()
{
int dev = 4;
cudaSetDevice(dev);
const unsigned batch = 128;
const unsigned output_size = 10;
const unsigned image_height = 32;
const unsigned image_width = 32;
const unsigned image_channel = 3;
const unsigned filter_height = 3;
const unsigned filter_width = 3;
const unsigned n_hidden = 1024;
//=============== Get Input and Label =================
float* images = (float*)malloc(batch*image_height*image_width*image_channel*sizeof(float));
unsigned* image_labels = (unsigned*)malloc(batch*sizeof(unsigned));
string cifar10_dir = "/home/lian599/raid/data/cifar10c/test_batch.bin";
read_CIFAR10_normalized(cifar10_dir, images, image_labels, batch);
//================ Get Weight =================
//FILE* config_file = fopen("./cifar10.config","r");
FILE* config_file = fopen("./pytorch_training/vgg_cifar10.csv","r");
//================ Set Network =================
//Bconv1 Layer
In32Conv64LayerParam* bconv1 = new In32Conv64LayerParam("Conv1", image_height, image_width,
filter_height, filter_width, 3, 128, batch);
In32Conv64LayerParam* bconv1_gpu = bconv1->initialize(images, config_file);
//Bconv2 Layer
Conv64LayerParam* bconv2 = new Conv64LayerParam("Conv2", bconv1->output_height,
bconv1->output_width, filter_height, filter_width, 128, 128, batch, 1, 1,
true, 2, 2, false);
Conv64LayerParam* bconv2_gpu = bconv2->initialize(config_file, bconv1->get_output_gpu());
//Bconv3 Layer
Conv64LayerParam* bconv3 = new Conv64LayerParam("Conv3", bconv2->output_height,
bconv2->output_width, filter_height, filter_width, 128, 256, batch);
Conv64LayerParam* bconv3_gpu = bconv3->initialize(config_file, bconv2->get_output_gpu());
//Bconv4 Layer
Conv64LayerParam* bconv4 = new Conv64LayerParam("Conv4", bconv3->output_height,
bconv3->output_width, filter_height, filter_width, 256, 256, batch, 1, 1,
true, 2, 2, false);
Conv64LayerParam* bconv4_gpu = bconv4->initialize(config_file, bconv3->get_output_gpu());
//Bconv5 Layer
Conv64LayerParam* bconv5 = new Conv64LayerParam("Conv5", bconv4->output_height,
bconv4->output_width, filter_height, filter_width, 256, 512, batch);
Conv64LayerParam* bconv5_gpu = bconv5->initialize(config_file, bconv4->get_output_gpu());
//Bconv6 Layer
Conv64LayerParam* bconv6 = new Conv64LayerParam("Conv6", bconv5->output_height,
bconv5->output_width, filter_height, filter_width, 512, 512, batch, 1, 1,
true, 2, 2, true);
Conv64LayerParam* bconv6_gpu = bconv6->initialize(config_file, bconv5->get_output_gpu());
//Fc1 Layer
Fc64LayerParam* bfc1 = new Fc64LayerParam("Fc1", batch, (bconv6->output_height)
*(bconv6->output_width)*512, n_hidden);
Fc64LayerParam* bfc1_gpu = bfc1->initialize(config_file, bconv6->get_output_gpu());
//Fc2 Layer
Fc64LayerParam* bfc2 = new Fc64LayerParam("Fc2", batch, n_hidden, n_hidden);
Fc64LayerParam* bfc2_gpu = bfc2->initialize(config_file, bfc1->get_output_gpu());
//Out Layer
Out64LayerParam* bout = new Out64LayerParam("Fout", batch, n_hidden, output_size, true);
Out64LayerParam* bout_gpu = bout->initialize(config_file, bfc2->get_output_gpu());
//================ Setup Kernel =================
int numThreads = 1024;
cudaDeviceProp deviceProp;
cudaGetDeviceProperties(&deviceProp, dev);
int numBlocksPerSm;
int shared_memory = 512*sizeof(int)*32;
cudaFuncSetAttribute(vggnet64, cudaFuncAttributeMaxDynamicSharedMemorySize,98304);
cudaOccupancyMaxActiveBlocksPerMultiprocessor(&numBlocksPerSm, vggnet64, numThreads, shared_memory);
//cudaFuncSetAttribute(alexnet64, cudaFuncAttributePreferredSharedMemoryCarveout,0);
void* args[] = {&bconv1_gpu, &bconv2_gpu, &bconv3_gpu, &bconv4_gpu, &bconv5_gpu, &bconv6_gpu,
&bfc1_gpu, &bfc2_gpu, &bout_gpu};
START_TIMER;
cudaLaunchCooperativeKernel((void*)vggnet64, numBlocksPerSm*deviceProp.multiProcessorCount,
numThreads, args, shared_memory);
//vggnet64<<<numBlocksPerSm*deviceProp.multiProcessorCount, numThreads, shared_memory>>> (
//bconv1_gpu, bconv2_gpu, bconv3_gpu, bconv4_gpu, bconv5_gpu, bfc1_gpu, bfc2_gpu, bout_gpu);
STOP_TIMER;
float* output = bout->download_output();
validate_prediction(output, image_labels, output_size, batch);
delete bconv1;
delete bconv2;
delete bconv3;
delete bconv4;
delete bconv5;
delete bconv6;
delete bfc1;
delete bfc2;
delete bout;
return 0;
}