aboutsummaryrefslogtreecommitdiff
path: root/src/core/CL/cl_kernels/direct_convolution.cl
blob: 5d2a24e7407ba018105a0be7bcc24c6cd89dddf9 (plain)
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
/*
 * Copyright (c) 2021 Arm Limited.
 *
 * SPDX-License-Identifier: MIT
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
#include "gemm_helpers.h"
#include "helpers_asymm.h"
#include "repeat.h"

#if defined(IS_QUANTIZED)

#if defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
#define ARM_DOT(x, y, val) val = arm_dot_acc((x), (y), (val));
#elif defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8) // defined(ARM_COMPUTE_OPENCL_DOT8_ENABLED) && defined(cl_arm_integer_dot_product_int8)
#define ARM_DOT(x, y, val) val += arm_dot((x), (y));
#else // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)
#define ARM_DOT(x, y, val)                                \
    ({                                                    \
        val += (ACC_DATA_TYPE)x.s0 * (ACC_DATA_TYPE)y.s0; \
        val += (ACC_DATA_TYPE)x.s1 * (ACC_DATA_TYPE)y.s1; \
        val += (ACC_DATA_TYPE)x.s2 * (ACC_DATA_TYPE)y.s2; \
        val += (ACC_DATA_TYPE)x.s3 * (ACC_DATA_TYPE)y.s3; \
    })
#endif // defined(ARM_COMPUTE_OPENCL_DOT8_ACC_ENABLED) && defined(cl_arm_integer_dot_product_accumulate_int8)

#define ARM_DOT1(a, b, c)                                                                                                                                                   \
    ({                                                                                                                                                                      \
        ARM_DOT(((VEC_DATA_TYPE(SRC_DATA_TYPE, 4))(a, (VEC_DATA_TYPE(SRC_DATA_TYPE, 3))0)), ((VEC_DATA_TYPE(WEI_DATA_TYPE, 4))(b, (VEC_DATA_TYPE(WEI_DATA_TYPE, 3))0)), c); \
    })
#define ARM_DOT2(a, b, c)                                                                                                                                                   \
    ({                                                                                                                                                                      \
        ARM_DOT(((VEC_DATA_TYPE(SRC_DATA_TYPE, 4))(a, (VEC_DATA_TYPE(SRC_DATA_TYPE, 2))0)), ((VEC_DATA_TYPE(WEI_DATA_TYPE, 4))(b, (VEC_DATA_TYPE(WEI_DATA_TYPE, 2))0)), c); \
    })
#define ARM_DOT3(a, b, c)                                                                                                               \
    ({                                                                                                                                  \
        ARM_DOT(((VEC_DATA_TYPE(SRC_DATA_TYPE, 4))(a, (SRC_DATA_TYPE)0)), ((VEC_DATA_TYPE(WEI_DATA_TYPE, 4))(b, (WEI_DATA_TYPE)0)), c); \
    })
#define ARM_DOT4(a, b, c) \
    ({                    \
        ARM_DOT(a, b, c); \
    })
#define ARM_DOT8(a, b, c)            \
    ({                               \
        ARM_DOT4((a.lo), (b.lo), c); \
        ARM_DOT4((a.hi), (b.hi), c); \
    })
#define ARM_DOT16(a, b, c)           \
    ({                               \
        ARM_DOT8((a.lo), (b.lo), c); \
        ARM_DOT8((a.hi), (b.hi), c); \
    })

#define ARM_OFFSET1(a, b, c)                      \
    ({                                            \
        c += (ACC_DATA_TYPE)a * (ACC_DATA_TYPE)b; \
    })
#define ARM_OFFSET2(a, b, c)                         \
    ({                                               \
        c += (ACC_DATA_TYPE)a.s0 * (ACC_DATA_TYPE)b; \
        c += (ACC_DATA_TYPE)a.s1 * (ACC_DATA_TYPE)b; \
    })
#define ARM_OFFSET3(a, b, c)                         \
    ({                                               \
        ARM_OFFSET2(a, b, c);                        \
        c += (ACC_DATA_TYPE)a.s2 * (ACC_DATA_TYPE)b; \
    })
#define ARM_OFFSET4(a, b, c)                         \
    ({                                               \
        ARM_OFFSET3(a, b, c);                        \
        c += (ACC_DATA_TYPE)a.s3 * (ACC_DATA_TYPE)b; \
    })
#define ARM_OFFSET8(a, b, c)         \
    ({                               \
        ARM_OFFSET4((a.lo), (b), c); \
        ARM_OFFSET4((a.hi), (b), c); \
    })
#define ARM_OFFSET16(a, b, c)        \
    ({                               \
        ARM_OFFSET8((a.lo), (b), c); \
        ARM_OFFSET8((a.hi), (b), c); \
    })

#if N0 == 1
#define ARM_OFFSET_K0XN0(k0, a, b, a_offset, b_offset, c) \
    ({                                                    \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c));                           \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##0), (a_offset), (c));                        \
    })
#elif N0 == 2 // N) == 3
#define ARM_OFFSET_K0XN0(k0, a, b, a_offset, b_offset, c) \
    ({                                                    \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s0));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##0), (a_offset), (c.s0));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s1));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##1), (a_offset), (c.s1));                     \
    })
#elif N0 == 3 // N0 == 3
#define ARM_OFFSET_K0XN0(k0, a, b, a_offset, b_offset, c) \
    ({                                                    \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s0));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##0), (a_offset), (c.s0));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s1));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##1), (a_offset), (c.s1));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s2));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##2), (a_offset), (c.s2));                     \
    })
#elif N0 == 4 // N0 == 4
#define ARM_OFFSET_K0XN0(k0, a, b, a_offset, b_offset, c) \
    ({                                                    \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s0));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##0), (a_offset), (c.s0));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s1));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##1), (a_offset), (c.s1));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s2));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##2), (a_offset), (c.s2));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s3));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##3), (a_offset), (c.s3));                     \
    })
#elif N0 == 8 // N0 == 8
#define ARM_OFFSET_K0XN0(k0, a, b, a_offset, b_offset, c) \
    ({                                                    \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s0));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##0), (a_offset), (c.s0));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s1));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##1), (a_offset), (c.s1));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s2));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##2), (a_offset), (c.s2));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s3));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##3), (a_offset), (c.s3));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s4));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##4), (a_offset), (c.s4));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s5));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##5), (a_offset), (c.s5));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s6));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##6), (a_offset), (c.s6));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s7));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##7), (a_offset), (c.s7));                     \
    })
#elif N0 == 16 // N0 == 16
#define ARM_OFFSET_K0XN0(k0, a, b, a_offset, b_offset, c) \
    ({                                                    \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s0));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##0), (a_offset), (c.s0));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s1));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##1), (a_offset), (c.s1));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s2));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##2), (a_offset), (c.s2));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s3));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##3), (a_offset), (c.s3));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s4));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##4), (a_offset), (c.s4));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s5));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##5), (a_offset), (c.s5));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s6));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##6), (a_offset), (c.s6));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s7));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##7), (a_offset), (c.s7));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s8));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##8), (a_offset), (c.s8));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.s9));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##9), (a_offset), (c.s9));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.sA));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##A), (a_offset), (c.sA));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.sB));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##B), (a_offset), (c.sB));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.sC));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##C), (a_offset), (c.sC));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.sD));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##D), (a_offset), (c.sD));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.sE));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##E), (a_offset), (c.sE));                     \
        CONCAT(ARM_OFFSET, k0)                            \
        ((a), (b_offset), (c.sF));                        \
        CONCAT(ARM_OFFSET, k0)                            \
        ((b##F), (a_offset), (c.sF));                     \
    })
#else // N0 not supported
#error "N0 value not supported"
#endif // N0 conditions
#else  // defined(IS_QUANTIZED)

#define ARM_DOT1(a, b, c)                         \
    ({                                            \
        c += (ACC_DATA_TYPE)a * (ACC_DATA_TYPE)b; \
    })
#define ARM_DOT2(a, b, c)                               \
    ({                                                  \
        c += (ACC_DATA_TYPE)a.s0 * (ACC_DATA_TYPE)b.s0; \
        c += (ACC_DATA_TYPE)a.s1 * (ACC_DATA_TYPE)b.s1; \
    })
#define ARM_DOT3(a, b, c)                               \
    ({                                                  \
        ARM_DOT2(a, b, c);                              \
        c += (ACC_DATA_TYPE)a.s2 * (ACC_DATA_TYPE)b.s2; \
    })
#define ARM_DOT4(a, b, c)                               \
    ({                                                  \
        ARM_DOT3(a, b, c);                              \
        c += (ACC_DATA_TYPE)a.s3 * (ACC_DATA_TYPE)b.s3; \
    })
#define ARM_DOT8(a, b, c)            \
    ({                               \
        ARM_DOT4((a.lo), (b.lo), c); \
        ARM_DOT4((a.hi), (b.hi), c); \
    })
#define ARM_DOT16(a, b, c)           \
    ({                               \
        ARM_DOT8((a.lo), (b.lo), c); \
        ARM_DOT8((a.hi), (b.hi), c); \
    })
#endif // defined(IS_QUANTIZED)

#if N0 == 1
#define ARM_DOT_K0XN0(k0, a, b, c) \
    ({                             \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##0), (c));        \
    })
#elif N0 == 2 // N) == 3
#define ARM_DOT_K0XN0(k0, a, b, c) \
    ({                             \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##0), (c.s0));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##1), (c.s1));     \
    })
#elif N0 == 3 // N0 == 3
#define ARM_DOT_K0XN0(k0, a, b, c) \
    ({                             \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##0), (c.s0));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##1), (c.s1));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##2), (c.s2));     \
    })
#elif N0 == 4 // N0 == 4
#define ARM_DOT_K0XN0(k0, a, b, c) \
    ({                             \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##0), (c.s0));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##1), (c.s1));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##2), (c.s2));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##3), (c.s3));     \
    })
#elif N0 == 8 // N0 == 8
#define ARM_DOT_K0XN0(k0, a, b, c) \
    ({                             \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##0), (c.s0));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##1), (c.s1));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##2), (c.s2));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##3), (c.s3));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##4), (c.s4));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##5), (c.s5));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##6), (c.s6));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##7), (c.s7));     \
    })
#elif N0 == 16 // N0 == 16
#define ARM_DOT_K0XN0(k0, a, b, c) \
    ({                             \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##0), (c.s0));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##1), (c.s1));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##2), (c.s2));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##3), (c.s3));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##4), (c.s4));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##5), (c.s5));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##6), (c.s6));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##7), (c.s7));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##8), (c.s8));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##9), (c.s9));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##A), (c.sA));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##B), (c.sB));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##C), (c.sC));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##D), (c.sD));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##E), (c.sE));     \
        CONCAT(ARM_DOT, k0)        \
        ((a), (b##F), (c.sF));     \
    })
#else // N0 not supported
#error "N0 value not supported"
#endif // N0 conditions

/** OpenCL kernel to compute the direct convolution.
 *
 * @note Data layout supported: NHWC
 * @note Data type supported: F32/F16/QASYMM8/QASYMM8_SIGNED
 * @note The data type must be passed at compile time using -DDATA_TYPE (e.g. -DDATA_TYPE=half)
 * @note The accumulation data type must be passed at compile time using -DACC_DATA_TYPE (e.g. -DDATA_TYPE_PROMOTED=half)
 * @note The convolution padding (left and top) must be passed at compile time using -DPAD_LEFT and -DPAD_TOP (e.g. -DPAD_LEFT=2, -DPAD_TOP=2)
 * @note The convolution strides must be passed at compile time using -DSTRIDE and -DPAD_TOP (e.g. -DPAD_LEFT=2, -DPAD_TOP=2)
 * @note The spatial dimensions of the weights must be passed at compile time using -DWEI_WIDTH and -DWEI_HEIGHT (e.g. -DWEI_WIDTH=9, -DWEI_HEIGHT=9)
 * @note The spatial dimensions of the source tensor must be passed at compile time using -DSRC_WIDTH and -DSRC_HEIGHT (e.g. -DSRC_WIDTH=96, -DSRC_HEIGHT=64)
 * @note The spatial dimensions of the destination tensor must be passed at compile time using -DDST_WIDTH and -DDST_HEIGHT (e.g. -DDST_WIDTH=96, -DDST_HEIGHT=64)
 * @note The channels of the source tensor must be passed at compile time using -DSRC_CHANNELS (e.g. -DSRC_CHANNELS=64)
 * @note The channels of the destination tensor must be passed at compile time using -DDST_CHANNELS (e.g. -DDDST_CHANNELS=64)
 * @note The data type of the source tensor must be passed at compile time using -DSRC_DATA_TYPE (e.g. -DSRC_DATA_TYPE=float)
 * @note The data type of the weights tensor must be passed at compile time using -DWEI_DATA_TYPE (e.g. -DWEI_DATA_TYPE=float)
 * @note The data type of the destination tensor must be passed at compile time using -DDST_DATA_TYPE (e.g. -DDST_DATA_TYPE=float)
 * @note The data type of the accumulators must be passed at compile time using -DACC_DATA_TYPE (e.g. -DACC_DATA_TYPE=float)
 * @note The number of M0 rows (width*height) to process must be passed at compile time using -DM0 (e.g. -DM0=2)
 * @note The number of N0 output channels to process must be passed at compile time using -DN0 (e.g. -DN0=2)
 * @note The number of K0 inner accumulations must be passed at compile time using -DK0 (e.g. -DK0=2)
 * @note The size of the partial store block in y must be passed at compile time using -DPARTIAL_STORE_M0 (e.g. -DPARTIAL_STORE_M0=1)
 * @note The size of the partial store block in x must be passed at compile time using -DPARTIAL_STORE_N0 (e.g. -DPARTIAL_STORE_N0=1)
 * @note Only the following configurations of M0, N0 and K0 are currently supported:
 *  - M0 = 1
 *  - N0 = 2, 3, 4, 8, 16
 *  - K0 = 2, 3, 4, 8, 16
 *
 *@note In case of QASYMM8/QASYMM8_SIGNED, the following extra information must be passed at compile time:
 * - -DIS_QUANTIZED
 * - The destination quantization multiplier e.g. -DDST_MULTIPLIER=1234
 * - The destination quantization shift e.g. -DDST_SHIFT=4
 * - The destination offset e.g. -DDST_OFFSET=4
 * - The source offset e.g. -DSRC_OFFSET=4
 * - The weights offset e.g. -DWEI_OFFSET=4
 * - The quantized zero value e.g. -DZERO_VALUE=4
 *
 * @param[in]  src_ptr                           Pointer to the source tensor. Supported data type: F16/F32
 * @param[in]  src_stride_x                      Stride of the source tensor in X dimension (in bytes)
 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  src_stride_y                      Stride of the source tensor in Y dimension (in bytes)
 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  src_stride_z                      Stride of the source tensor in Z dimension (in bytes)
 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source tensor
 * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data type: same as @p src_ptr
 * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  dst_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  dst_stride_z                      Stride of the destination tensor in Z dimension (in bytes)
 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
 * @param[in]  wei_ptr                           Pointer to the weights tensor. Supported data type: same as @p src_ptr
 * @param[in]  wei_stride_x                      Stride of the weights tensor in X dimension (in bytes)
 * @param[in]  wei_step_x                        wei_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  wei_stride_y                      Stride of the weights tensor in Y dimension (in bytes)
 * @param[in]  wei_step_y                        wei_stride_y * number of elements along Y processed per workitem(in bytes)
 * @param[in]  wei_stride_z                      Stride of the weights tensor in Z dimension (in bytes)
 * @param[in]  wei_step_z                        wei_stride_z * number of elements along Z processed per workitem(in bytes)
 * @param[in]  wei_offset_first_element_in_bytes The offset of the first element in the bias matrix
 * @param[in]  bia_ptr                           (Optional) Pointer to the bias tensor Supported data type: same as @p src_ptr (if F32/F16) or S32 (if QASYMM8/QASYMM8_SIGNED)
 * @param[in]  bia_stride_x                      (Optional) Stride of the bias tensor in X dimension (in bytes)
 * @param[in]  bia_step_x                        (Optional) bia_stride_x * number of elements along X processed per workitem(in bytes)
 * @param[in]  bia_offset_first_element_in_bytes (Optional) The offset of the first element in the bias matrix
 * @param[in]  wei_stride_w                      Stride of the weights tensor in W dimension (in bytes)
 */
__kernel void direct_convolution_nhwc(
    TENSOR3D_DECLARATION(src),
    TENSOR3D_DECLARATION(dst),
    TENSOR3D_DECLARATION(wei),
#if defined(HAS_BIAS)
    VECTOR_DECLARATION(bia),
#endif // defined(HAS_BIAS)
    unsigned int wei_stride_w)
{
#if M0 != 1
#error "M0: Only supported 1"
#endif // M0 != 1

    const int cout = max((int)(get_global_id(0) * N0 - (N0 - PARTIAL_STORE_N0) % N0), 0); // input channels
    const int mout = get_global_id(1);                                                    // width x height
    const int zout = get_global_id(2);                                                    // batch size index

    REPEAT_VAR_INIT_TO_CONST(16, int, zero, 0);
    REPEAT_VAR_INIT_TO_CONST(M0, int, xi, 0);
    REPEAT_VAR_INIT_TO_CONST(M0, int, yi, 0);

#define LINEAR_2_COORDS(i)                            \
    xi##i = ((mout * M0 + i) % DST_WIDTH) * STRIDE_X; \
    yi##i = ((mout * M0 + i) / DST_WIDTH) * STRIDE_Y; \
    xi##i -= PAD_LEFT;                                \
    yi##i -= PAD_TOP;

    // Convert the linear index to coordinate
    LINEAR_2_COORDS(0);

#undef LINEAR_2_COORDS

    uint src_offset = src_offset_first_element_in_bytes + zout * src_stride_y * (SRC_WIDTH * SRC_HEIGHT);
    uint wei_offset = wei_offset_first_element_in_bytes + cout * wei_stride_w;

    // Initialize the accumulators
    REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(ACC_DATA_TYPE, N0), c, 0);

    for(int i = 0; i < (WEI_WIDTH * WEI_HEIGHT); ++i)
    {
        int xk = i % WEI_WIDTH;
        int yk = i / WEI_WIDTH;

        REPEAT_VAR_INIT_TO_CONST(M0, int, mi_valid_row, 0);
        REPEAT_VAR_INIT_TO_CONST(M0, int, mi_mask, 0);

        // Calculate the input row to read from source tensor
#define MI_INIT(i)                                                                                                  \
    mi_valid_row##i = max(min(xi##i + xk, SRC_WIDTH - 1), 0) + max(min(yi##i + yk, SRC_HEIGHT - 1), 0) * SRC_WIDTH; \
    mi_mask##i      = (xi##i + xk) >= 0 && (xi##i + xk) < SRC_WIDTH && (yi##i + yk) >= 0 && (yi##i + yk) < SRC_HEIGHT;

        MI_INIT(0);

#undef MI_INIT

        int k = 0;
        for(; k <= (SRC_CHANNELS - K0); k += K0)
        {
            // Load values from src tensor
            LOAD_BLOCK_INDIRECT(M0, K0, SRC_DATA_TYPE, a, src_ptr, src_offset + k * sizeof(SRC_DATA_TYPE), src_stride_y, mi_valid_row, mi_mask);

            // Load values from weights tensor
            LOAD_BLOCK(N0, K0, WEI_DATA_TYPE, b, wei_ptr, wei_offset, wei_stride_w, zero);

#if defined(IS_QUANTIZED)
#define TENSOR_DOT(K0, i)                                                                                      \
    if(mi_mask##i != 0)                                                                                        \
    {                                                                                                          \
        ARM_DOT_K0XN0(K0, a##i, b, c##i);                                                                      \
        ARM_OFFSET_K0XN0(K0, a##i, b, SRC_OFFSET, WEI_OFFSET, c##i);                                           \
    }                                                                                                          \
    else                                                                                                       \
    {                                                                                                          \
        ARM_DOT_K0XN0(K0, ((VEC_DATA_TYPE(SRC_DATA_TYPE, K0))ZERO_VALUE), b, c##i);                            \
        ARM_OFFSET_K0XN0(K0, ((VEC_DATA_TYPE(SRC_DATA_TYPE, K0))ZERO_VALUE), b, SRC_OFFSET, WEI_OFFSET, c##i); \
    }
#else // defined(IS_QUANTIZED)
#define TENSOR_DOT(K0, i) \
    ARM_DOT_K0XN0(K0, a##i, b, c##i);
#endif // defined(IS_QUANTIZED)

            TENSOR_DOT(K0, 0);

            wei_offset += K0 * sizeof(WEI_DATA_TYPE);
        }

#if(SRC_CHANNELS % K0) != 0
        // Left-over accumulations
        for(; k < SRC_CHANNELS; ++k)
        {
            // Load values from src tensor
            LOAD_BLOCK_INDIRECT(M0, 1, SRC_DATA_TYPE, a, src_ptr, src_offset + k * sizeof(SRC_DATA_TYPE), src_stride_y, mi_valid_row, mi_mask);

            // Load values from weights tensor
            LOAD_BLOCK(N0, 1, WEI_DATA_TYPE, b, wei_ptr, wei_offset, wei_stride_w, zero);

            TENSOR_DOT(1, 0);

#undef TENSOR_DOT

            wei_offset += sizeof(WEI_DATA_TYPE);
        }
#endif // (SRC_CHANNELS % K0) != 0

        c0 += (SRC_CHANNELS * SRC_OFFSET * WEI_OFFSET);
    }

    __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + (cout * sizeof(DST_DATA_TYPE)) + (mout * M0 * dst_stride_y);

    // Batched direct convolution
    dst_addr += zout * dst_stride_y * (DST_WIDTH * DST_HEIGHT);

#if defined(HAS_BIAS)
    __global uchar *bias_addr = bia_ptr + bia_offset_first_element_in_bytes + (cout * sizeof(BIA_DATA_TYPE));

    LOAD_BLOCK(1, N0, BIA_DATA_TYPE, bias, bias_addr, 0, zero0, zero);

    // c = c + bias[broadcasted]
    ADD_BLOCK_BROADCAST(M0, c, bias0);
#endif // HAS_BIAS

#if defined(IS_QUANTIZED)

    REPEAT_VAR_INIT_TO_CONST(M0, VEC_DATA_TYPE(DST_DATA_TYPE, N0), cq, 0);

#if DST_SHIFT < 0
#define QUANTIZE(i)                                                                               \
    c##i  = ASYMM_MULT_BY_QUANT_MULTIPLIER_GREATER_THAN_ONE(c##i, DST_MULTIPLIER, DST_SHIFT, N0); \
    c##i  = c##i + DST_OFFSET;                                                                    \
    cq##i = CONVERT_SAT(c##i, VEC_DATA_TYPE(DST_DATA_TYPE, N0));
#else // OUTPUT_SHIFT < 0
#define QUANTIZE(i)                                                                            \
    c##i  = ASYMM_MULT_BY_QUANT_MULTIPLIER_LESS_THAN_ONE(c##i, DST_MULTIPLIER, DST_SHIFT, N0); \
    c##i  = c##i + DST_OFFSET;                                                                 \
    cq##i = CONVERT_SAT(c##i, VEC_DATA_TYPE(DST_DATA_TYPE, N0));
#endif // OUTPUT_SHIFT < 0

    QUANTIZE(0);

#undef QUANTIZE

    STORE_VECTOR_SELECT(cq, DST_DATA_TYPE, dst_addr, N0, PARTIAL_STORE_N0, PARTIAL_STORE_N0 != 0 && get_global_id(0) == 0);
#else  // defined(IS_QUANTIZED)
    STORE_VECTOR_SELECT(c, DST_DATA_TYPE, dst_addr, N0, PARTIAL_STORE_N0, PARTIAL_STORE_N0 != 0 && get_global_id(0) == 0);
#endif // defined(IS_QUANTIZED)
}