-
Notifications
You must be signed in to change notification settings - Fork 248
Expand file tree
/
Copy pathConvolution_kernels.hpp
More file actions
538 lines (504 loc) · 22.7 KB
/
Convolution_kernels.hpp
File metadata and controls
538 lines (504 loc) · 22.7 KB
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
#ifndef UTENSOR_CONVOLUTION_KERNELS_H
#define UTENSOR_CONVOLUTION_KERNELS_H
#include <algorithm>
#include <limits>
#include "uTensor/core/operatorBase.hpp"
namespace uTensor {
enum Padding : uint8_t { UNKNOWN = 0, VALID = 1, SAME = 2 };
template <typename T, typename Filter, typename Bias>
void generic_convolution_kernel(Tensor& out, const Tensor& in, Filter filter,
Bias bias,
const Padding padding,
const uint16_t (&strides)[4]) {
const TensorShape& in_shape = in->get_shape();
const int16_t input_depth = in_shape[3];
const int16_t input_rows = in_shape[1];
const int16_t input_cols = in_shape[2];
const int16_t input_batches = in_shape[0];
const int16_t out_depth = filter.out_channels();
const int16_t filter_rows = filter.height();
const int16_t filter_cols = filter.width();
const int16_t filter_count = filter.out_channels();
const int16_t stride_rows = strides[1];
const int16_t stride_cols = strides[2];
// Compute for now, but should assume codegen does this
int16_t out_rows = out->get_shape()[1];
int16_t out_cols = out->get_shape()[2];
if (padding == VALID) {
// out_rows = (input_rows - filter_rows) / stride_rows + 1;
// out_cols = (input_cols - filter_cols) / stride_cols + 1;
} else {
// SAME
// out_rows = input_rows;
// out_cols = input_cols;
}
// When we're converting the 32 bit accumulator to a lower bit depth, we
int filter_left_offset;
int filter_top_offset;
if (padding == VALID) {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols + 1) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows + 1) / 2;
} else {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows) / 2;
}
// If we've got multiple images in our input, work through each of them.
for (int batch = 0; batch < input_batches; ++batch) {
// Walk through all the output image values, sliding the filter to
// different positions in the input.
for (int out_y = 0; out_y < out_rows; ++out_y) {
for (int out_x = 0; out_x < out_cols; ++out_x) {
// Each filter kernel produces one output channel.
for (int out_channel = 0; out_channel < filter_count; ++out_channel) {
const int in_x_origin = (out_x * stride_cols) - filter_left_offset;
const int in_y_origin = (out_y * stride_rows) - filter_top_offset;
// T output_val = 0;
filter.reset();
for (int filter_y = 0; filter_y < filter_rows; ++filter_y) {
for (int filter_x = 0; filter_x < filter_cols; ++filter_x) {
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
const int in_x = in_x_origin + filter_x;
const int in_y = in_y_origin + filter_y;
T input_value;
if ((in_x >= 0) && (in_x < input_cols) && (in_y >= 0) &&
(in_y < input_rows)) {
// Commenting out since these indices might be useful later
/*
size_t input_index = batch * input_rows * input_cols *
input_depth + in_y * input_cols * input_depth + in_x *
input_depth + in_channel; input_value =
in((uint32_t)input_index);
*/
input_value = in(batch, in_y, in_x, in_channel);
} else {
input_value = 0;
}
// size_t filter_index = filter_y * filter_cols * input_depth *
// filter_count +
// filter_x * input_depth * filter_count +
// in_channel * filter_count + out_channel;
// const T filter_value = filter(filter_index);
filter.PartialCompute(input_value, out_channel, filter_y, filter_x,
in_channel);
}
}
}
/*
out((batch * out_rows * out_cols * filter_count) +
(out_y * out_cols * filter_count) +
(out_x * filter_count) + out_channel) = output_val;
*/
out(batch, out_y, out_x, out_channel) = filter.finalize() + bias(out_channel);
}
}
}
}
}
template <typename Filter, typename Bias>
void generic_sq_convolution_kernel(
Tensor& out, const Tensor& in, Filter filter,
Bias bias,
const Padding padding,
const uint16_t (&strides)[4]) {
const TensorShape& in_shape = in->get_shape();
const int16_t input_depth = in_shape[3];
const int16_t input_rows = in_shape[1];
const int16_t input_cols = in_shape[2];
const int16_t input_batches = in_shape[0];
const int16_t out_depth = filter.out_channels();
const int16_t filter_rows = filter.height();
const int16_t filter_cols = filter.width();
const int16_t filter_count = filter.out_channels();
const int16_t stride_rows = strides[1];
const int16_t stride_cols = strides[2];
// Compute for now, but should assume codegen does this
int16_t out_rows = out->get_shape()[1];
int16_t out_cols = out->get_shape()[2];
if (padding == VALID) {
// out_rows = (input_rows - filter_rows) / stride_rows + 1;
// out_cols = (input_cols - filter_cols) / stride_cols + 1;
} else {
// SAME
// out_rows = input_rows;
// out_cols = input_cols;
}
// When we're converting the 32 bit accumulator to a lower bit depth, we
int filter_left_offset;
int filter_top_offset;
if (padding == VALID) {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols + 1) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows + 1) / 2;
} else {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows) / 2;
}
// If we've got multiple images in our input, work through each of them.
for (int batch = 0; batch < input_batches; ++batch) {
// Walk through all the output image values, sliding the filter to
// different positions in the input.
for (int out_y = 0; out_y < out_rows; ++out_y) {
for (int out_x = 0; out_x < out_cols; ++out_x) {
// Each filter kernel produces one output channel.
for (int out_channel = 0; out_channel < filter_count; ++out_channel) {
const int in_x_origin = (out_x * stride_cols) - filter_left_offset;
const int in_y_origin = (out_y * stride_rows) - filter_top_offset;
// T output_val = 0;
filter.reset();
for (int filter_y = 0; filter_y < filter_rows; ++filter_y) {
for (int filter_x = 0; filter_x < filter_cols; ++filter_x) {
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
const int in_x = in_x_origin + filter_x;
const int in_y = in_y_origin + filter_y;
float input_value;
if ((in_x >= 0) && (in_x < input_cols) && (in_y >= 0) &&
(in_y < input_rows)) {
// Commenting out since these indices might be useful later
/*
size_t input_index = batch * input_rows * input_cols *
input_depth + in_y * input_cols * input_depth + in_x *
input_depth + in_channel; input_value =
in((uint32_t)input_index);
*/
const int32_t iv8 = static_cast<int8_t>(in(batch, in_y, in_x, in_channel));
const float scale = in->get_quantization_params().get_scale_for_channel(in_channel);
const int32_t zp = in->get_quantization_params().get_zeroP_for_channel(in_channel);
input_value = (iv8 - zp)*scale;
} else {
input_value = 0;
}
// size_t filter_index = filter_y * filter_cols * input_depth *
// filter_count +
// filter_x * input_depth * filter_count +
// in_channel * filter_count + out_channel;
// const T filter_value = filter(filter_index);
filter.PartialCompute(input_value, out_channel, filter_y, filter_x,
in_channel);
}
}
}
/*
out((batch * out_rows * out_cols * filter_count) +
(out_y * out_cols * filter_count) +
(out_x * filter_count) + out_channel) = output_val;
*/
const float out_val = filter.finalize() + bias(out_channel);
const float oscale = out->get_quantization_params().get_scale_for_channel(out_channel);
const int32_t ozp = out->get_quantization_params().get_zeroP_for_channel(out_channel);
const int32_t otmp = static_cast<int32_t>(out_val/oscale) + ozp;
const int8_t out8 = (otmp < -127 ) ? -128 : (otmp > 127) ? 127 : static_cast<int8_t>(otmp);
out(batch, out_y, out_x, out_channel) = out8;
}
}
}
}
}
// Hack until I get the generic version working
template <typename T, typename Filter>
void generic_pool_convolution_kernel(Tensor& out, const Tensor& in,
Filter filter, const Padding padding,
const uint16_t (&strides)[4]) {
const TensorShape& in_shape = in->get_shape();
const int16_t input_depth = in_shape[3];
const int16_t input_rows = in_shape[1];
const int16_t input_cols = in_shape[2];
const int16_t input_batches = in_shape[0];
const int16_t out_depth = input_depth; // filter.out_channels();
const int16_t filter_rows = filter.height();
const int16_t filter_cols = filter.width();
// const int16_t filter_count = filter.out_channels();
const int16_t stride_rows = strides[1];
const int16_t stride_cols = strides[2];
// Compute for now, but should assume codegen does this
int16_t out_rows = out->get_shape()[1];
int16_t out_cols = out->get_shape()[2];
if (padding == VALID) {
// out_rows = (input_rows - filter_rows) / stride_rows + 1;
// out_cols = (input_cols - filter_cols) / stride_cols + 1;
} else {
// SAME
// out_rows = input_rows;
// out_cols = input_cols;
}
// When we're converting the 32 bit accumulator to a lower bit depth, we
int filter_left_offset;
int filter_top_offset;
if (padding == VALID) {
// filter_left_offset =
// ((out_cols - 1) * stride_cols + filter_cols - input_cols + 1) / 2;
// filter_top_offset =
// ((out_rows - 1) * stride_rows + filter_rows - input_rows + 1) / 2;
filter_left_offset =
(((input_cols - filter_cols) / stride_cols) * stride_cols +
filter_cols - input_cols + 1) /
2;
filter_top_offset =
(((input_rows - filter_rows) / stride_rows) * stride_rows +
filter_rows - input_rows + 1) /
2;
} else {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows) / 2;
}
// If we've got multiple images in our input, work through each of them.
for (int batch = 0; batch < input_batches; ++batch) {
// Walk through all the output image values, sliding the filter to
// different positions in the input.
// Each filter kernel produces one output channel.
for (int out_channel = 0; out_channel < out_depth;
++out_channel) { // Thank god for no caches
for (int out_y = 0; out_y < out_rows; ++out_y) {
for (int out_x = 0; out_x < out_cols; ++out_x) {
const int in_x_origin = (out_x * stride_cols) - filter_left_offset;
const int in_y_origin = (out_y * stride_rows) - filter_top_offset;
// T output_val = 0;
filter.reset();
for (int filter_y = 0; filter_y < filter_rows; ++filter_y) {
for (int filter_x = 0; filter_x < filter_cols; ++filter_x) {
// for (int in_channel = 0; in_channel < input_depth;
// ++in_channel) {
const int in_x = in_x_origin + filter_x;
const int in_y = in_y_origin + filter_y;
T input_value;
if ((in_x >= 0) && (in_x < input_cols) && (in_y >= 0) &&
(in_y < input_rows)) {
// Commenting out since these indices might be useful later
/*
size_t input_index = batch * input_rows * input_cols *
input_depth + in_y * input_cols * input_depth + in_x *
input_depth + in_channel; input_value =
in((uint32_t)input_index);
*/
input_value = in(batch, in_y, in_x, out_channel);
} else {
input_value = 0;
}
// size_t filter_index = filter_y * filter_cols * input_depth *
// filter_count +
// filter_x * input_depth * filter_count +
// in_channel * filter_count + out_channel;
// const T filter_value = filter(filter_index);
filter.PartialCompute(input_value, filter_y, filter_x,
out_channel, out_channel);
//}
}
}
/*
out((batch * out_rows * out_cols * filter_count) +
(out_y * out_cols * filter_count) +
(out_x * filter_count) + out_channel) = output_val;
*/
out(batch, out_y, out_x, out_channel) = filter.finalize();
}
}
}
}
}
template <typename T>
void convolution_kernel(Tensor& out, const Tensor& in, const Tensor& filter,
const Padding padding, const uint16_t (&strides)[4]) {
const TensorShape& in_shape = in->get_shape();
const TensorShape& f_shape = filter->get_shape();
const int16_t input_depth = in_shape[3];
const int16_t input_rows = in_shape[1];
const int16_t input_cols = in_shape[2];
const int16_t input_batches = in_shape[0];
const int16_t out_depth = f_shape[3];
const int16_t filter_rows = f_shape[0];
const int16_t filter_cols = f_shape[1];
const int16_t filter_count = f_shape[3];
const int16_t stride_rows = strides[1];
const int16_t stride_cols = strides[2];
// Compute for now, but should assume codegen does this
int16_t out_rows = out->get_shape()[1];
int16_t out_cols = out->get_shape()[2];
if (padding == VALID) {
// out_rows = (input_rows - filter_rows) / stride_rows + 1;
// out_cols = (input_cols - filter_cols) / stride_cols + 1;
} else {
// SAME
// out_rows = input_rows;
// out_cols = input_cols;
}
// When we're converting the 32 bit accumulator to a lower bit depth, we
int filter_left_offset;
int filter_top_offset;
if (padding == VALID) {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols + 1) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows + 1) / 2;
} else {
filter_left_offset =
((out_cols - 1) * stride_cols + filter_cols - input_cols) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + filter_rows - input_rows) / 2;
}
// If we've got multiple images in our input, work through each of them.
for (int batch = 0; batch < input_batches; ++batch) {
// Walk through all the output image values, sliding the filter to
// different positions in the input.
for (int out_y = 0; out_y < out_rows; ++out_y) {
for (int out_x = 0; out_x < out_cols; ++out_x) {
// Each filter kernel produces one output channel.
for (int out_channel = 0; out_channel < filter_count; ++out_channel) {
const int in_x_origin = (out_x * stride_cols) - filter_left_offset;
const int in_y_origin = (out_y * stride_rows) - filter_top_offset;
T output_val = 0;
for (int filter_y = 0; filter_y < filter_rows; ++filter_y) {
for (int filter_x = 0; filter_x < filter_cols; ++filter_x) {
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
const int in_x = in_x_origin + filter_x;
const int in_y = in_y_origin + filter_y;
T input_value;
if ((in_x >= 0) && (in_x < input_cols) && (in_y >= 0) &&
(in_y < input_rows)) {
// Commenting out since these indices might be useful later
/*
size_t input_index = batch * input_rows * input_cols *
input_depth + in_y * input_cols * input_depth + in_x *
input_depth + in_channel; input_value =
in((uint32_t)input_index);
*/
input_value = in(batch, in_y, in_x, in_channel);
} else {
input_value = 0;
}
// size_t filter_index = filter_y * filter_cols * input_depth *
// filter_count +
// filter_x * input_depth * filter_count +
// in_channel * filter_count + out_channel;
// const T filter_value = filter(filter_index);
const T filter_value =
filter(filter_y, filter_x, in_channel, out_channel);
output_val += (input_value * filter_value);
}
}
}
/*
out((batch * out_rows * out_cols * filter_count) +
(out_y * out_cols * filter_count) +
(out_x * filter_count) + out_channel) = output_val;
*/
out(batch, out_y, out_x, out_channel) = output_val;
}
}
}
}
}
template <typename T>
void depthwise_separable_convolution_kernel(Tensor& out, const Tensor& in,
const Tensor& dw_filter,
const Tensor& pw_filter,
const Padding padding,
const uint16_t (&strides)[4]) {
const TensorShape& in_shape = in->get_shape();
const TensorShape& df_shape = dw_filter->get_shape();
const TensorShape& pf_shape = pw_filter->get_shape();
const int16_t input_depth = in_shape[3];
const int16_t input_rows = in_shape[1];
const int16_t input_cols = in_shape[2];
const int16_t input_batches = in_shape[0];
const int16_t out_depth = pf_shape[3];
const int16_t dw_filter_rows = df_shape[0];
const int16_t dw_filter_cols = df_shape[1];
const int16_t dw_filter_in_channels = df_shape[2];
const int16_t dw_filter_channel_mult = df_shape[3];
const int16_t pw_filter_in_channels = pf_shape[2];
const int16_t stride_rows = strides[1];
const int16_t stride_cols = strides[2];
// Compute for now, but should assume codegen does this
int16_t out_rows = out->get_shape()[1];
int16_t out_cols = out->get_shape()[2];
if (padding == VALID) {
// out_rows = (input_rows - filter_rows) / stride_rows + 1;
// out_cols = (input_cols - filter_cols) / stride_cols + 1;
} else {
// SAME
// out_rows = input_rows;
// out_cols = input_cols;
}
// When we're converting the 32 bit accumulator to a lower bit depth, we
int filter_left_offset;
int filter_top_offset;
if (padding == VALID) {
filter_left_offset =
((out_cols - 1) * stride_cols + dw_filter_cols - input_cols + 1) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + dw_filter_rows - input_rows + 1) / 2;
} else {
filter_left_offset =
((out_cols - 1) * stride_cols + dw_filter_cols - input_cols) / 2;
filter_top_offset =
((out_rows - 1) * stride_rows + dw_filter_rows - input_rows) / 2;
}
// If we've got multiple images in our input, work through each of them.
for (int batch = 0; batch < input_batches; ++batch) {
// Walk through all the output image values, sliding the filter to
// different positions in the input.
for (int out_y = 0; out_y < out_rows; ++out_y) {
for (int out_x = 0; out_x < out_cols; ++out_x) {
// Fuse the Depthwise filtering with the pointwise filtering by
// iterating over the channels first
for (int out_channel = 0; out_channel < out_depth; ++out_channel) {
const int in_x_origin = (out_x * stride_cols) - filter_left_offset;
const int in_y_origin = (out_y * stride_rows) - filter_top_offset;
T output_val = 0;
for (int filter_y = 0; filter_y < dw_filter_rows; ++filter_y) {
for (int filter_x = 0; filter_x < dw_filter_cols; ++filter_x) {
for (int in_channel = 0; in_channel < input_depth; ++in_channel) {
for (int r = 0; r < dw_filter_channel_mult; ++r) {
const int in_x = in_x_origin + filter_x;
const int in_y = in_y_origin + filter_y;
T input_value;
if ((in_x >= 0) && (in_x < input_cols) && (in_y >= 0) &&
(in_y < input_rows)) {
// Commenting out since these indices might be useful later
/*
size_t input_index = batch * input_rows * input_cols *
input_depth + in_y * input_cols * input_depth + in_x *
input_depth + in_channel; input_value =
in((uint32_t)input_index);
*/
input_value = in(batch, in_y, in_x, in_channel);
} else {
input_value = 0;
}
// size_t filter_index = filter_y * filter_cols * input_depth
// * filter_count +
// filter_x * input_depth * filter_count +
// in_channel * filter_count + out_channel;
// const T filter_value = filter(filter_index);
const T dw_filter_value =
dw_filter(filter_y, filter_x, in_channel, r);
const T pw_filter_value =
pw_filter(0, 0, in_channel * dw_filter_channel_mult + r,
out_channel);
output_val +=
(input_value * dw_filter_value * pw_filter_value);
}
}
}
}
/*
out((batch * out_rows * out_cols * filter_count) +
(out_y * out_cols * filter_count) +
(out_x * filter_count) + out_channel) = output_val;
*/
out(batch, out_y, out_x, out_channel) = output_val;
}
}
}
}
}
} // namespace uTensor
#endif