-
Notifications
You must be signed in to change notification settings - Fork 16
/
convolve_kernel.cl
410 lines (356 loc) · 15.2 KB
/
convolve_kernel.cl
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
__kernel
__attribute__((reqd_work_group_size(8, 32, 1)))
void convolve5(
__global const float * in,
__global float * merge,
__global const float * weights,
__local float * channel_buff,
__local float * row_buff) {
// cl::NDRange global(channels, outputs, row);
const int c = get_global_id(0); // channel
const int o = get_global_id(1); // output
const int row = get_global_id(2); // row
const int channels = get_global_size(0);
const int outputs = get_global_size(1);
// cl::NDRange local(2, (1->32), 1);
const int lx = get_local_id(0);
const int ly = get_local_id(1);
const int chan_buff_size = 8;
const int out_buff_size = get_local_size(1);
const int row_buff_size = 7;
const int chan_shift = 3;
const int filter_size = 5;
const int filter_len = filter_size * filter_size;
const int mid = (filter_size / 2) + 1;
const int extent = mid - 1;
// input = channels * height * width
// output = outputs * height * width
// weights = output * channels * filter
// merge = channels * outputs * height * width
const int width = 19;
const int height = 19;
const int strip_size = filter_size * width;
// Copy the input channels (strips) locally
if (out_buff_size < 19 && ly == 0) {
// strip-row
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
if ((unsigned)in_row >= height) {
for (int w = 0; w < width; w++) {
channel_buff[(lx * filter_size + srow) * width + w] = 0.0f;
}
} else {
for (int w = 0; w < width; w++) {
channel_buff[(lx * filter_size + srow) * width + w] =
in[(c * height + in_row) * width + w];
}
}
}
} else if (out_buff_size >= 19 && ly < 19) {
// Every thread copies a column
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
float val = 0.0f;
if ((unsigned)in_row < height) {
val = in[(c * height + in_row) * width + ly];
}
channel_buff[(lx * filter_size + srow) * width + ly] = val;
}
}
__private float filter_buff[25];
// Copy the filter we are applying locally
// output * channel * filter_len
for (int f = 0; f < filter_len; f++) {
filter_buff[f] = weights[(o * channels + c) * filter_len + f];
}
barrier(CLK_LOCAL_MEM_FENCE);
int out_lane = 0;
int out_cw = 0;
#pragma unroll
for (int cw = 0; cw < width; cw++) {
int fwstart = cw - extent;
int fwend = cw + extent;
float out;
// Start filter
if (fwstart >= 0 && fwend < width) {
int fid = lx * strip_size + fwstart;
out = channel_buff[fid ] * filter_buff[0];
out += channel_buff[fid + 1] * filter_buff[1];
out += channel_buff[fid + 2] * filter_buff[2];
out += channel_buff[fid + 3] * filter_buff[3];
out += channel_buff[fid + 4] * filter_buff[4];
out += channel_buff[fid + width ] * filter_buff[5];
out += channel_buff[fid + width + 1] * filter_buff[6];
out += channel_buff[fid + width + 2] * filter_buff[7];
out += channel_buff[fid + width + 3] * filter_buff[8];
out += channel_buff[fid + width + 4] * filter_buff[9];
out += channel_buff[fid + width*2 ] * filter_buff[10];
out += channel_buff[fid + width*2 + 1] * filter_buff[11];
out += channel_buff[fid + width*2 + 2] * filter_buff[12];
out += channel_buff[fid + width*2 + 3] * filter_buff[13];
out += channel_buff[fid + width*2 + 4] * filter_buff[14];
out += channel_buff[fid + width*3 ] * filter_buff[15];
out += channel_buff[fid + width*3 + 1] * filter_buff[16];
out += channel_buff[fid + width*3 + 2] * filter_buff[17];
out += channel_buff[fid + width*3 + 3] * filter_buff[18];
out += channel_buff[fid + width*3 + 4] * filter_buff[19];
out += channel_buff[fid + width*4 ] * filter_buff[20];
out += channel_buff[fid + width*4 + 1] * filter_buff[21];
out += channel_buff[fid + width*4 + 2] * filter_buff[22];
out += channel_buff[fid + width*4 + 3] * filter_buff[23];
out += channel_buff[fid + width*4 + 4] * filter_buff[24];
} else {
const float * filter_idx = filter_buff;
out = 0.0f;
#pragma unroll
for (int fh = 0; fh < filter_size; fh++) {
for (int fw = fwstart; fw <= fwend; fw++) {
// "zero padding"
if ((unsigned)fw >= width) {
filter_idx++;
continue;
}
float input = channel_buff[(lx * filter_size + fh) * width + fw];
out += input * *filter_idx++;
}
}
}
// End filter
row_buff[(ly * chan_buff_size + lx) * row_buff_size + out_lane] = out;
out_lane++;
// Row buffer full or last lane?
if (out_lane == row_buff_size || (cw == width - 1)) {
barrier(CLK_LOCAL_MEM_FENCE);
if (lx < out_lane) {
float val;
val = row_buff[(ly * chan_buff_size + 0) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 1) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 2) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 3) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 4) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 5) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 6) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 7) * row_buff_size + lx];
merge[(((c >> chan_shift) * height + row) * width + out_cw + lx) * outputs + o] = val;
}
out_cw += row_buff_size;
out_lane = 0;
}
}
}
__kernel
__attribute__((work_group_size_hint(8, 32, 1)))
void convolve3(
__global const float * in,
__global float * merge,
__global const float * weights,
__local float * channel_buff,
__local float * row_buff,
const int row_tile_size) {
// cl::NDRange global(channels, outputs, row);
const int c = get_global_id(0); // channel
const int o = get_global_id(1); // output
const int r = get_global_id(2); // row
const int channels = get_global_size(0);
const int outputs = get_global_size(1);
// cl::NDRange local(2, (1->32), 1);
const int lx = get_local_id(0);
const int ly = get_local_id(1);
const int chan_buff_size = 8;
const int out_buff_size = get_local_size(1);
const int row_buff_size = 7;
const int chan_shift = 3;
const int width = 19;
const int height = 19;
const int filter_size = 3;
const int filter_len = filter_size * filter_size;
const int mid = (filter_size / 2) + 1;
const int extent = mid - 1;
const int pad_width = width + filter_size - 1;
// input = channels * height * width
// output = outputs * height * width
// weights = output * channels * filter
// merge = channels * outputs * height * width
__private float filter_buff[9];
__private float chan_cache[2];
__private float stripe_cache[9];
// Copy the filter we are applying locally
// output * channel * filter_len
for (int f = 0; f < filter_len; f++) {
filter_buff[f] = weights[(o * channels + c) * filter_len + f];
}
for (int tile = 0; tile < row_tile_size; tile++) {
int row = r * row_tile_size + tile;
if (row > 18) break;
// Copy the input channels (strips) locally
if (out_buff_size < 21 && ly == 0) {
// strip-row
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
channel_buff[(lx * pad_width + 0) * filter_size + srow] = 0.0f;
if ((unsigned)in_row < height) {
for (int w = 0; w < width; w++) {
float val = in[(c * height + in_row) * width + w];
channel_buff[(lx * pad_width + w + extent) * filter_size + srow] = val;
}
} else {
for (int w = 0; w < width; w++) {
channel_buff[(lx * pad_width + w + extent) * filter_size + srow] = 0.0f;
}
}
channel_buff[(lx * pad_width + pad_width - 1) * filter_size + srow] = 0.0f;
}
} else if (out_buff_size >= 21 && ly < 21) {
// Every thread copies a column
int copy_idx = (lx * pad_width + ly) * filter_size;
if (tile == 0 || row == 18) {
// Every thread copies a column
for (int srow = 0; srow < filter_size; srow++) {
int in_row = row - extent + srow;
float val = 0.0f;
if ((unsigned)in_row < height && ly >= 1 && ly <= 19) {
val = in[(c * height + in_row) * width + ly - 1];
}
channel_buff[copy_idx + srow] = val;
if (srow > 0) {
chan_cache[srow - 1] = val;
}
}
} else {
int in_row = row - extent + 2;
float val = 0.0f;
if (ly >= 1 && ly <= 19) {
val = in[(c * height + in_row) * width + ly - 1];
}
channel_buff[copy_idx + 0] = chan_cache[0];
channel_buff[copy_idx + 1] = chan_cache[1];
channel_buff[copy_idx + 2] = val;
chan_cache[0] = chan_cache[1];
chan_cache[1] = val;
}
}
int out_lane = 0;
int out_cw = 0;
__local float * out_row_buff = &row_buff[(ly * chan_buff_size + lx) * row_buff_size];
int fid = (lx * pad_width) * filter_size;
barrier(CLK_LOCAL_MEM_FENCE);
for (int rc = 0; rc < 9; rc++) {
stripe_cache[rc] = channel_buff[fid + rc];
}
#pragma unroll
for (int cw = 0; cw < width; cw++) {
// Start filter
float out = stripe_cache[ 0] * filter_buff[0]
+ stripe_cache[ 1] * filter_buff[3]
+ stripe_cache[ 2] * filter_buff[6]
+ stripe_cache[ 3] * filter_buff[1]
+ stripe_cache[ 4] * filter_buff[4]
+ stripe_cache[ 5] * filter_buff[7]
+ stripe_cache[ 6] * filter_buff[2]
+ stripe_cache[ 7] * filter_buff[5]
+ stripe_cache[ 8] * filter_buff[8];
// End filter
out_row_buff[out_lane++] = out;
fid += filter_size;
for (int rc = 0; rc < 6; rc++) {
stripe_cache[rc] = stripe_cache[rc + 3];
}
stripe_cache[6] = channel_buff[fid + 6];
stripe_cache[7] = channel_buff[fid + 7];
stripe_cache[8] = channel_buff[fid + 8];
// Row buffer full or last lane?
if (out_lane == row_buff_size || (cw == width - 1)) {
barrier(CLK_LOCAL_MEM_FENCE);
if (lx < out_lane) {
// lx = channels 2 or 8, ly = outputs 32
// repurpose the lx threads over columns now
float val;
val = row_buff[(ly * chan_buff_size + 0) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 1) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 2) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 3) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 4) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 5) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 6) * row_buff_size + lx];
val += row_buff[(ly * chan_buff_size + 7) * row_buff_size + lx];
merge[(((c >> chan_shift) * height + row) * width + out_cw + lx) * outputs + o] = val;
}
out_cw += row_buff_size;
out_lane = 0;
}
}
}
}
__kernel void merge(
__global const float * in,
__global float * out,
__constant const float * biases,
__private const int channels) {
// cl::NDRange global(outputs, 19*19);
const int gx = get_global_id(0);
const int gy = get_global_id(1);
const int output = gx;
const int b = gy;
const int outputs = get_global_size(0);
const int width = 19;
const int height = 19;
const int boardsize = width * height;
const int o = output;
const float bias = biases[o];
float sum = bias;
for (int c = 0; c < channels; c++) {
sum += in[(c * boardsize + b) * outputs + o];
}
// ELU
sum = sum > 0 ? sum : 1.0f * (half_exp(sum) - 1.0f);
out[o * boardsize + b] = sum;
}
__kernel void batchnorm(
__global const float * in,
__global float * out,
__constant const float * means,
__constant const float * variances,
__constant const float * scale) {
// cl::NDRange global(outputs, 19*19);
const int gx = get_global_id(0);
const int gy = get_global_id(1);
const int output = gx;
const int outputs = get_global_size(0);
const int channel_size = get_global_size(1);
const unsigned int o = output;
const unsigned int b = gy;
const float epsilon = 1e-5;
const float mean = means[o] / scale[0];
const float variance = epsilon + variances[o] / scale[0];
const float scale_stddiv = 1.0f / sqrt(variance);
out[o * channel_size + b] = scale_stddiv
* (in[o * channel_size + b] - mean);
}
__kernel void innerproduct(
__private const int inputs,
__global const float * in,
__global float * out,
__global const float * weights,
__constant const float * biases) {
const int gx = get_global_id(0);
const int output = gx;
const int outputs = get_global_size(0);
const unsigned int o = output;
unsigned int i;
float16 val16 = (float16)(0.0f);
for (i = 0; i + 16 < inputs; i += 16) {
val16 += vload16(0, &in[i]) * vload16(0, &weights[o * inputs + i]);
}
float val = val16.s0 + val16.s1 + val16.s2 + val16.s3
+ val16.s4 + val16.s5 + val16.s6 + val16.s7
+ val16.s8 + val16.s9 + val16.sa + val16.sb
+ val16.sc + val16.sd + val16.se + val16.sf;
for (; i < inputs; i++) {
val += in[i] * weights[o * inputs + i];
}
val += biases[o];
if (outputs > 1) {
val = val > 0 ? val : 1.0f * (half_exp(val) - 1.0f);
}
out[o] = val;
}