aboutsummaryrefslogtreecommitdiff
path: root/src/core/GLES_COMPUTE/cs_shaders/convolution_layer.cs
blob: d40cbbbaf00e81390fc11d6258307bfbb6497005 (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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
 * Copyright (c) 2017-2018 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.
 */

layout(local_size_x = LOCAL_SIZE_X, local_size_y = LOCAL_SIZE_Y, local_size_z = LOCAL_SIZE_Z) in;

#include "helpers_cs.h"

#if defined(DATA_TYPE_FP16)
precision mediump float;
#endif // DATA_TYPE_FP16

#ifdef RESHAPE_TO_COLUMNS

/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM.
 *
 * @note The data type must be passed at compile time using "#define DATA_TYPE_NAME". e.g. "#define DATA_TYPE_FP32"
 * @note In case biases will be added to the convolution "#define HAS_BIAS" has to be passed to append the final matrix with 1 in each row.
 *
 * @param[in]  src_ptr       Pointer to the source tensor. Supported data types: F16/F32
 * @param[in]  src_attrs     The attributes of the source tensor
 * @param[out] dst_ptr       Pointer to the destination tensor. Supported data types: same as @p src_ptr
 * @param[in]  dst_attrs     The attributes of the destination tensor
 * @param[in]  biases_ptr    Pointer to the biases tensor. Same as @p src_ptr
 * @param[in]  biases_attrs  The attributes of the biases tensor
 * @param[in]  width         The width of the input tensor
 * @param[in]  height        The height of the input tensor
 * @param[in]  depth         The depth of the input tensor
 * @param[in]  total_filters Total number of filters. 4th dimension of the weights matrix
 */

SHADER_PARAMS_DECLARATION
{
    Tensor3DAttributes src_attrs;
    ImageAttributes    dst_attrs;
#ifdef HAS_BIAS
    VectorAttributes biases_attrs;
#endif /* HAS_BIAS */
    uint width;
    uint height;
    uint depth;
    uint total_filters;
};

#if defined(DATA_TYPE_FP32)

TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, writeonly);
#ifdef HAS_BIAS
TENSOR_DECLARATION(3, biasesBuffer, float, biases_ptr, biases_shift, 2, readonly);
#endif /* BIAS */

void main()
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    ImageIterator    dst_iter = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(dst_attrs, dst_shift);
#ifdef HAS_BIAS
    VectorIterator biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift);
#endif /* BIAS */

    bool is_last_thread = (((int(gl_GlobalInvocationID.x)) == (int(gl_NumWorkGroups.x * gl_WorkGroupSize.x) - 1)) && ((int(gl_GlobalInvocationID.y)) == (int(gl_NumWorkGroups.y * gl_WorkGroupSize.y) - 1))
                           && ((int(gl_GlobalInvocationID.z)) == (int(gl_NumWorkGroups.z * gl_WorkGroupSize.z) - 1)));
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, ((uint(gl_GlobalInvocationID.x) * uint(dst_attrs.stride_y)) + (uint(gl_GlobalInvocationID.y) * uint(width) * uint(dst_attrs.stride_y)) + (uint(
                                                    gl_GlobalInvocationID.z)
                                                * uint(width) * uint(height) * uint(dst_attrs.stride_y))));
    // Linearize convolution elements
    if(is_last_thread)
    {
        for(uint i = 0u; i < uint(total_filters); ++i)
        {
            float s0 = LOAD_CURRENT_ITEM(src_ptr, src_iter);
            STORE_CURRENT_ITEM(dst_ptr, dst_iter, s0);
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (depth * src_attrs.stride_z));
#ifdef HAS_BIAS
            float b = LOAD_CURRENT_ITEM(biases_ptr, biases_iter);
            STORE(dst_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(dst_iter, dst_attrs.stride_y), b);
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(biases_iter, biases_attrs.stride_x);
#endif /* HAS_BIAS */
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.stride_x);
        }
    }
    else
    {
        for(uint i = 0u; i < uint(total_filters); ++i)
        {
            float s0 = LOAD_CURRENT_ITEM(src_ptr, src_iter);
            STORE_CURRENT_ITEM(dst_ptr, dst_iter, s0);
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (depth * src_attrs.stride_z));
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.stride_x);
        }
    }
}

#elif defined(DATA_TYPE_FP16)

TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, writeonly);
#ifdef HAS_BIAS
TENSOR_DECLARATION(3, biasesBuffer, uint, biases_ptr, biases_shift, 2, readonly);
#endif /* BIAS */

void main()
{
    Tensor3DIterator src_iter    = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    ImageIterator    dst_iter    = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(dst_attrs, dst_shift);
#ifdef HAS_BIAS
    VectorIterator   biases_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(biases_attrs, biases_shift);
#endif /* BIAS */

    bool is_last_thread = (((int(gl_GlobalInvocationID.x)) == (int(gl_NumWorkGroups.x * gl_WorkGroupSize.x) - 1)) && ((int(gl_GlobalInvocationID.y)) == (int(gl_NumWorkGroups.y * gl_WorkGroupSize.y) - 1))
                           && ((int(gl_GlobalInvocationID.z)) == (int(gl_NumWorkGroups.z * gl_WorkGroupSize.z) - 1)));
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, ((uint(gl_GlobalInvocationID.x) * uint(dst_attrs.stride_y)) + (uint(gl_GlobalInvocationID.y) * uint(width) * uint(dst_attrs.stride_y)) + (uint(
                                                    gl_GlobalInvocationID.z)
                                                * uint(width) * uint(height) * uint(dst_attrs.stride_y))));
    // Linearize convolution elements
    if(is_last_thread)
    {
        for(uint i = 0u; i < uint(total_filters); i = i + 2u)
        {
            vec2 s0 = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter);
            vec2 s;
            if(int(CURRENT_ITEM_OFFSET_IN_BYTES(src_iter) >> 1u) % 2 == 0)
            {
                s.x = s0.x;
            }
            else
            {
                s.x = s0.y;
            }
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (depth * src_attrs.stride_z));

            vec2 s1 = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter);
            if(int(CURRENT_ITEM_OFFSET_IN_BYTES(src_iter) >> 1u) % 2 == 0)
            {
                s.y = s1.x;
            }
            else
            {
                s.y = s1.y;
            }
            STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, s);
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (depth * src_attrs.stride_z));
#ifdef HAS_BIAS
            vec2 b = LOAD_UNPACK2_CURRENT_ITEM_HALF(biases_ptr, biases_iter);
            STORE_PACK2_HALF(dst_ptr, TENSOR_OFFSET_ADVANCE_IN_BYTES(dst_iter, dst_attrs.stride_y), b);
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(biases_iter, (2u * biases_attrs.stride_x));
#endif /* HAS_BIAS */
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, (2u * dst_attrs.stride_x));
        }
    }
    else
    {
        for(uint i = 0u; i < uint(total_filters); i = i + 2u)
        {
            vec2 s0 = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter);
            vec2 s;
            if(int(CURRENT_ITEM_OFFSET_IN_BYTES(src_iter) >> 1u) % 2 == 0)
            {
                s.x = s0.x;
            }
            else
            {
                s.x = s0.y;
            }
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (depth * src_attrs.stride_z));

            vec2 s1 = LOAD_UNPACK2_CURRENT_ITEM_HALF(src_ptr, src_iter);
            if(int(CURRENT_ITEM_OFFSET_IN_BYTES(src_iter) >> 1u) % 2 == 0)
            {
                s.y = s1.x;
            }
            else
            {
                s.y = s1.y;
            }
            STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, s);
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (depth * src_attrs.stride_z));
            TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, (2u * dst_attrs.stride_x));
        }
    }
}

#endif /* DATA_TYPE_FP32 */
#endif // RESHAPE_TO_COLUMNS

#ifdef IM2COL_GENERIC

/** This kernel performs a reshaping of the input tensor to a tensor used to perform convolution using GEMM.
 *
 * @note The data type must be passed at compile time using "#define DATA_TYPE_FP32"
 * @note PAD_LEFT/PAD_RIGHT/PAD_TOP/PAD_BOTTOM must be passed for padding info, e.g. "#define PAD_LEFT xxx"
 * @note KERNEL_WIDTH/KERNEL_HEIGHT/KERNEL_DEPTH must be passed for kernel dimension, e.g. "#define KERNEL_WIDTH xxx"
 * @note STRIDE_X/STRIDE_Y must be passed for stride info, e.g. "#define STRIDE_X xxx"
 * @note CONVOLVED_WIDTH/CONVOLVED_HEIGHT must be passed for convolved dimension, e.g. "#define CONVOLVED_WIDTH xxx"
 * @note SRC_WIDTH/SRC_HEIGHT must be passed for input dimension, e.g. "#define SRC_WIDTH xxx"
 * @note DILATION_X/DILATION_Y must be passed for dilation sizes, e.g. "#define DILATION_X xxx"
 * @note In case biases will be added to the convolution "#define HAS_BIAS" has to be passed to append the final matrix with 1 in each row.
 *
 * @param[in]  src_ptr      Pointer to the source tensor. Supported data types: F16/F32
 * @param[in]  src_attrs    The attributes of the source tensor
 * @param[out] dst_ptr      Pointer to the destination tensor. Supported data types: same as @p src_ptr
 * @param[in]  dst_attrs    The attributes of the destination tensor
 * @param[in]  src_stride_w Stride of the source tensor in W dimension (in bytes).
 * @param[in]  dst_stride_w Stride of the destination tensor in W dimension (in bytes).
 */

SHADER_PARAMS_DECLARATION
{
    Tensor3DAttributes src_attrs;
    ImageAttributes    dst_attrs;
    uint               src_stride_w;
    uint               dst_stride_w;
};

#ifdef DATA_TYPE_FP32

TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, restrict);

void main(void)
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    ImageIterator    dst_iter = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(dst_attrs, dst_shift);

    int xc    = int(gl_GlobalInvocationID.x);                // x coordinate in the convolved tensor
    int yc    = int(gl_GlobalInvocationID.y);                // y coordinate in the convolved tensor
    int ch    = int(gl_GlobalInvocationID.z) % KERNEL_DEPTH; // input feature map
    int batch = int(gl_GlobalInvocationID.z) / KERNEL_DEPTH; // the batch

    // Calculate input indeces
    int xi = xc * STRIDE_X - PAD_LEFT;
    int yi = yc * STRIDE_Y - PAD_TOP;
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, (ch * int(src_attrs.stride_z)) + (batch * int(src_stride_w)));

    // Calculate output indeces
    int xo = ch * KERNEL_WIDTH * KERNEL_HEIGHT;
    int yo = xc + yc * CONVOLVED_WIDTH; // Index of the convolution
    // sizeof is not available in GLES, so we'll use stride_x
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, (yo * int(dst_attrs.stride_y)) + (batch * int(dst_stride_w)) + xo * int(dst_attrs.stride_x));

    uint src_pos = 0u;

    // Linearize convolution elements
    for(int y = yi, y_e = yi + KERNEL_HEIGHT * DILATION_Y; y < y_e; y += DILATION_Y)
    {
        for(int x = xi, x_e = xi + KERNEL_WIDTH * DILATION_X; x < x_e; x += DILATION_X, TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, int(dst_attrs.stride_x)))
        {
#if PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0
            src_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * int(src_attrs.stride_x) + y * int(src_attrs.stride_y));
            STORE_CURRENT_ITEM(dst_ptr, dst_iter, LOAD(src_ptr, src_pos));
#else  /* PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0 */
            if(x < 0 || x >= SRC_WIDTH || y < 0 || y >= SRC_HEIGHT)
            {
                STORE_CURRENT_ITEM(dst_ptr, dst_iter, 0.0f);
            }
            else
            {
                src_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * int(src_attrs.stride_x) + y * int(src_attrs.stride_y));
                STORE_CURRENT_ITEM(dst_ptr, dst_iter, LOAD(src_ptr, src_pos));
            }
#endif /* PAD_LEFT == 0 && PAD_TOP == 0 && PAD_RIGHT == 0 && PAD_BOTTOM == 0 */
        }
    }

#ifdef HAS_BIAS
    if(ch == (KERNEL_DEPTH - 1))
    {
        STORE_CURRENT_ITEM(dst_ptr, dst_iter, 1.0f);
    }
#endif /* HAS_BIAS */
}

#elif defined(DATA_TYPE_FP16)

TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, writeonly);

#ifdef KERNEL_1x1

void main(void)
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    ImageIterator    dst_iter = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(dst_attrs, dst_shift);

    uint xc    = gl_GlobalInvocationID.x;
    uint yc    = gl_GlobalInvocationID.y;
    uint zc    = gl_GlobalInvocationID.z;
    uint ch    = zc % uint(KERNEL_DEPTH); // input feature map
    uint batch = zc / uint(KERNEL_DEPTH); // the batch

    // Calculate input indeces
    uint xi = xc;
    uint yi = yc;
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, batch * src_stride_w + ch * src_attrs.step_z);

    // Calculate output indeces
    uint dst_element_count = dst_attrs.step_x / dst_attrs.stride_x;
    uint xo                = ch * dst_element_count;
    uint yo                = xc + yc * uint(CONVOLVED_WIDTH);
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, batch * dst_stride_w + yo * dst_attrs.stride_y + xo);

    bool x_start_even = ((xc % 2u) == 0u);
    bool z_depth_even = ((uint(KERNEL_DEPTH) % 2u) == 0u);
    uint input_pos    = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, xi * src_attrs.stride_x + yi * src_attrs.stride_y);
    uint tmp_left     = 0u;
    uint tmp_right    = 0u;

    if(ch % 2u != 0u)
    {
        return;
    }

    if(z_depth_even || (!z_depth_even && (int(ch) < (KERNEL_DEPTH - 1))))
    {
        tmp_left  = LOAD(src_ptr, input_pos);
        input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, xi * src_attrs.stride_x + yi * src_attrs.stride_y + src_attrs.stride_z);
        tmp_right = LOAD(src_ptr, input_pos);
        if(x_start_even)
        {
            tmp_right = (tmp_left & 0xffffu) + (tmp_right << 16u);
        }
        else
        {
            tmp_right = (tmp_left >> 16u) + (tmp_right & 0xffff0000u);
        }
        STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
        TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x);

#ifdef HAS_BIAS
        if(ch == (uint(KERNEL_DEPTH) - 2u))
        {
            mediump vec2 bias_vec = vec2(1.f, 0.f);
            uint         bias_u   = packHalf2x16(bias_vec);
            STORE_CURRENT_ITEM(dst_ptr, dst_iter, bias_u);
        }
#endif /* HAS_BIAS */
    }
    else
    {
        tmp_left = LOAD(src_ptr, input_pos);
        if(x_start_even)
        {
            tmp_right = (tmp_left & 0xffffu);
        }
        else
        {
            tmp_right = (tmp_left >> 16u);
        }

#ifdef HAS_BIAS
        mediump vec2 bias_vec = vec2(0.f, 1.f);
        uint         bias_u   = packHalf2x16(bias_vec);
        tmp_right += (bias_u & 0xffff0000u);
#endif /* HAS_BIAS */

        STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
    }
}

#else /* KERNEL_1x1 */

void main(void)
{
    uint xc    = gl_GlobalInvocationID.x;
    uint yc    = gl_GlobalInvocationID.y;
    uint zc    = gl_GlobalInvocationID.z;
    uint ch    = zc % uint(KERNEL_DEPTH); // input feature map
    uint batch = zc / uint(KERNEL_DEPTH); // the batch

    Tensor3DIterator src_iter   = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    Tensor3DIterator src_iter_b = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    ImageIterator    dst_iter   = CONVERT_TO_IMAGE_ITERATOR_NO_STEP(dst_attrs, dst_shift);

    // Calculate input indeces
    uint src_element_count = src_attrs.step_x / src_attrs.stride_x;
    uint xi                = (xc * uint(STRIDE_X)) / src_element_count;
    uint yi                = yc * uint(STRIDE_Y);
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, batch * src_stride_w + ch * src_attrs.stride_z);

    // Calculate output indeces
    uint dst_element_count = dst_attrs.step_x / dst_attrs.stride_x;
    uint xo                = (ch * uint(KERNEL_WIDTH) * uint(KERNEL_HEIGHT)) * dst_element_count;
    uint yo                = xc + yc * uint(CONVOLVED_WIDTH);
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, batch * dst_stride_w + yo * dst_attrs.stride_y + xo);

    bool x_start_even = ((xc * uint(STRIDE_X)) % 2u == 0u);
    bool z_start_even = ((ch % 2u) == 0u);
    uint input_pos    = 0u;
    uint tmp          = 0u;
    uint tmp_left     = 0u;
    uint tmp_right    = 0u;

    // Linearize convolution elements
    for(uint y = yi, y_e = yi + uint(KERNEL_HEIGHT); y < y_e; ++y)
    {
        uint xstart = 0u;
        uint xend   = 0u;

        // even col, even row
        if(x_start_even)
        {
            if(((y - yi + ch) % 2u) == 0u)
            {
                for(uint x = xi, x_e = xi + (uint(KERNEL_WIDTH) / 2u); x < x_e; ++x, TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x))
                {
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * src_attrs.step_x + y * src_attrs.stride_y);
                    STORE_CURRENT_ITEM(dst_ptr, dst_iter, LOAD(src_ptr, input_pos));
                }
            }
            else
            {
                // 1st pair
                if(!z_start_even && (y == yi))
                {
                    // cross 2d feature map
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter_b, (xi + (uint(KERNEL_WIDTH) / 2u)) * src_attrs.step_x + (yi + uint(KERNEL_HEIGHT) - 1u) * src_attrs.stride_y + batch * src_stride_w +
                                                               (ch - 1u) * src_attrs.stride_z);
                }
                else
                {
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter,
                                                               (xi + (uint(KERNEL_WIDTH) / 2u)) * src_attrs.step_x + (y - 1u) * src_attrs.stride_y);
                }
                tmp_right = LOAD(src_ptr, input_pos);
                input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, xi * src_attrs.step_x + y * src_attrs.stride_y);
                tmp_left  = LOAD(src_ptr, input_pos);
                tmp_right = (tmp_right & 0xffffu) + (tmp_left << 16u);
                STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
                TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x);

                // remaining
                for(uint x = xi + 1u, x_e = xi + (uint(KERNEL_WIDTH) / 2u) + 1u; x < x_e; ++x, TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x))
                {
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, (x - 1u) * src_attrs.step_x + y * src_attrs.stride_y);
                    tmp_left  = LOAD(src_ptr, input_pos);
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * src_attrs.step_x + y * src_attrs.stride_y);
                    tmp_right = LOAD(src_ptr, input_pos);
                    tmp_right = (tmp_left >> 16u) + (tmp_right << 16u);
                    STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
                }
            }
        }
        else
        {
            if((((y - yi) % 2u) == 0u && !z_start_even) || (((y - yi) % 2u) != 0u && z_start_even))
            {
                // 1st pair
                if(y == yi)
                {
                    // cross 2d feature map
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter_b, (xi + (uint(KERNEL_WIDTH) / 2u)) * src_attrs.step_x + (yi + uint(KERNEL_HEIGHT) - 1u) * src_attrs.stride_y + batch * src_stride_w +
                                                               (ch - 1u) * src_attrs.stride_z);
                }
                else
                {
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter,
                                                               (xi + (uint(KERNEL_WIDTH) / 2u)) * src_attrs.step_x + (y - 1u) * src_attrs.stride_y);
                }

                tmp_right = LOAD(src_ptr, input_pos);
                input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, xi * src_attrs.step_x + y * src_attrs.stride_y);
                tmp_left  = LOAD(src_ptr, input_pos);
                tmp_right = (tmp_right >> 16u) + (tmp_left & 0xffff0000u);
                STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
                TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x);

                // remaining
                for(uint x = xi + 1u, x_e = xi + (uint(KERNEL_WIDTH) / 2u) + 1u; x < x_e; ++x, TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x))
                {
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * src_attrs.step_x + y * src_attrs.stride_y);
                    STORE_CURRENT_ITEM(dst_ptr, dst_iter, LOAD(src_ptr, input_pos));
                }
            }
            else if((((y - yi) % 2u) == 0u && z_start_even) || (((y - yi) % 2u) != 0u && !z_start_even))
            {
                // 1st pair
                input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, xi * src_attrs.step_x + y * src_attrs.stride_y);
                tmp_right = LOAD(src_ptr, input_pos);
                input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, (xi + 1u) * src_attrs.step_x + y * src_attrs.stride_y);
                tmp_left  = LOAD(src_ptr, input_pos);
                tmp_right = (tmp_right >> 16u) + (tmp_left << 16u);
                STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
                TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x);

                // remaining
                for(uint x = xi + 1u, x_e = xi + (uint(KERNEL_WIDTH) / 2u); x < x_e; ++x, TENSOR_ITERATOR_ADVANCE_IN_BYTES(dst_iter, dst_attrs.step_x))
                {
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * src_attrs.step_x + y * src_attrs.stride_y);
                    tmp_right = LOAD(src_ptr, input_pos);
                    input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, (x + 1u) * src_attrs.step_x + y * src_attrs.stride_y);
                    tmp_left  = LOAD(src_ptr, input_pos);
                    tmp_right = (tmp_right >> 16u) + (tmp_left << 16u);
                    STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp_right);
                }
            }
        }
    }

    // NOTE: must handle last element manually instead of in loops
    // to avoid write conflict across 2d boundary
    if(ch == uint(KERNEL_DEPTH) - 1u)
    {
        uint x    = xi + (uint(KERNEL_WIDTH) / 2u);
        uint y    = yi + uint(KERNEL_HEIGHT) - 1u;
        input_pos = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, x * src_attrs.step_x + y * src_attrs.stride_y);
        tmp       = LOAD(src_ptr, input_pos);
        if(!x_start_even)
        {
            tmp = (tmp >> 16u) + (tmp << 16u);
        }

#ifdef HAS_BIAS
        mediump vec2 bias_vec = vec2(1.f, 1.f);
        uint         bias_u   = packHalf2x16(bias_vec);
        if(z_start_even)
        {
            tmp = (tmp & 0xffffu) + (bias_u & 0xffff0000u);
        }
        else
        {
            tmp = (bias_u & 0xffffu);
        }
#endif /* HAS_BIAS */

        STORE_CURRENT_ITEM(dst_ptr, dst_iter, tmp);
    }
}

#endif /* KERNEL_1x1 */
#else  /* DATA_TYPE_FP32 */
#error Data type not supported
#endif /* DATA_TYPE_FP32 */
#endif /* IM2COL_GENERIC */

#ifdef IM2COL_REDUCED

/** This kernel reshapes the tensor's low three dimensions to single row for GEMM operation
 *
 * @note The data type must be passed at compile time using "#define DATA_TYPE_FP16"
 * @note In case biases will be added in late stage, "#define HAS_BIAS" has to be passed to append the final matrix with 1 in each row.
 *
 * @param[in]  src_ptr   Pointer to the source tensor. Supported data types: F16/F32
 * @param[in]  src_attrs The attributes of the source tensor
 * @param[out] dst_ptr   Pointer to the destination tensor. Same as @p src_ptr
 * @param[in]  dst_attrs The attributes of the destination tensor
 * @param[in]  width     The width of the input tensor
 * @param[in]  height    The height of the input tensor
 */

SHADER_PARAMS_DECLARATION
{
    Tensor3DAttributes src_attrs;
    VectorAttributes   dst_attrs;
    uint               width;
    uint               height;
};

#ifdef DATA_TYPE_FP32

TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, restrict);

void main(void)
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    VectorIterator   dst_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(dst_attrs, dst_shift);

    uvec3 pos            = uvec3(gl_GlobalInvocationID.xyz);
    uvec3 size           = uvec3(gl_WorkGroupSize.xyz);
    uint  image_size     = width * height;
    uint  tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x + pos.y * width + pos.z * image_size);

    STORE(dst_ptr, tmp_out_offset, LOAD_CURRENT_ITEM(src_ptr, src_iter));

#ifdef HAS_BIAS
    // If it is the last thread in the 3 dimensional workgroup
    if(pos.x == (size.x - 1) && pos.y == (size.y - 1) && pos.z == (size.z - 1))
    {
        tmp_out_offset += (dst_attrs.stride_x >> uint(2));
        STORE(dst_ptr, tmp_out_offset, 1.f);
    }
#endif // HAS_BIAS
}

#elif defined(DATA_TYPE_FP16)

#if defined(IM2COL_REDUCED_8X)
TENSOR_DECLARATION(1, srcBuffer, uvec4, src_ptr, src_shift, 4, readonly);
TENSOR_DECLARATION(2, dstBuffer, uvec4, dst_ptr, dst_shift, 4, restrict);
#elif defined(IM2COL_REDUCED_4X) /* IM2COL_REDUCED_8X */
TENSOR_DECLARATION(1, srcBuffer, uvec2, src_ptr, src_shift, 3, readonly);
TENSOR_DECLARATION(2, dstBuffer, uvec2, dst_ptr, dst_shift, 3, restrict);
#else                            /* IM2COL_REDUCED_8X */
TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, restrict);
#endif                           /* IM2COL_REDUCED_8X */

#if defined(IM2COL_REDUCED_GENERIC)

void main(void)
{
    Tensor3DIterator src_iter        = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    Tensor3DIterator src_nostep_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    VectorIterator   dst_iter        = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(dst_attrs, dst_shift);

    uvec3 pos            = uvec3(gl_GlobalInvocationID.xyz);
    uvec3 size           = uvec3(gl_WorkGroupSize.xyz);
    uint  image_size     = width * height;
    uint  element_count  = src_attrs.step_x / src_attrs.stride_x;
    uint  tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * element_count + pos.y * width + pos.z * image_size);
    uint  width_fp16     = (width + uint(1)) >> uint(1);
    uint  tmp;

    // odd width
    if(width % uint(2) != uint(0))
    {
        // even row
        if((pos.y + pos.z * height) % uint(2) == uint(0))
        {
            // skip last element of each line to avoid write conflict except for last line
            if((pos.x < (width / element_count)) || ((pos.y == gl_NumWorkGroups.y - 1u) && (pos.z == gl_NumWorkGroups.z - 1u)))
            {
                tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
                STORE(dst_ptr, tmp_out_offset, tmp);
            }
        }
        else
        {
            // special op
            uint tmp_left  = uint(0);
            uint tmp_right = uint(0);
            tmp_right      = LOAD_CURRENT_ITEM(src_ptr, src_iter); //right half
            if(pos.x == uint(0))
            {
                tmp_left  = LOAD(src_ptr, TENSOR3D_OFFSET(src_nostep_iter, int(width), int(pos.y) - 1, int(pos.z))); //left half
                tmp_right = (tmp_left & uint(0xffff)) + (tmp_right << uint(16));
            }
            else
            {
                tmp_left  = LOAD(src_ptr, TENSOR3D_OFFSET(src_nostep_iter, (int(pos.x) - 1) * int(element_count), int(pos.y), int(pos.z)));
                tmp_right = ((tmp_left >> uint(16)) + (tmp_right << uint(16)));
            }
            STORE(dst_ptr, tmp_out_offset, tmp_right);
        }
    }
    else
    {
        tmp = LOAD_CURRENT_ITEM(src_ptr, src_iter);
        STORE(dst_ptr, tmp_out_offset, tmp);
    }

#ifdef HAS_BIAS
    // If it is the last thread in the 3 dimensional workgroup
    if(pos.x == (size.x - 1u) && pos.y == (size.y - 1u) && pos.z == (size.z - 1u))
    {
        tmp_out_offset += (dst_attrs.stride_x >> dst_shift);

        // FIXME: need odd/even detection for tmp_out_offset?
        mediump vec2 bias_vec = vec2(1.0f, 1.0f);
        STORE_PACK2_HALF(dst_ptr, tmp_out_offset, bias_vec);
    }
#endif // HAS_BIAS
}

#else /* IM2COL_REDUCED_GENERIC */

void main(void)
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR(src_attrs, src_shift);
    VectorIterator   dst_iter = CONVERT_TO_VECTOR_ITERATOR_NO_STEP(dst_attrs, dst_shift);

    uvec3 pos            = uvec3(gl_GlobalInvocationID.xyz);
#if defined(IM2COL_REDUCED_8X)
    uint  tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * uint(8) + pos.y * width + pos.z * uint(IMAGE_SIZE));
    uvec4 tmp            = LOAD_CURRENT_ITEM(src_ptr, src_iter);
    STORE(dst_ptr, tmp_out_offset, tmp);
#elif defined(IM2COL_REDUCED_4X) /* IM2COL_REDUCED_8X */
    uint  tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * uint(4) + pos.y * width + pos.z * uint(IMAGE_SIZE));
    uvec2 tmp            = LOAD_CURRENT_ITEM(src_ptr, src_iter);
    STORE(dst_ptr, tmp_out_offset, tmp);
#else                            /* IM2COL_REDUCED_8X */
    uint tmp_out_offset = VECTOR_OFFSET(dst_iter, pos.x * uint(2) + pos.y * width + pos.z * uint(IMAGE_SIZE));
    uint tmp            = LOAD_CURRENT_ITEM(src_ptr, src_iter);
    STORE(dst_ptr, tmp_out_offset, tmp);
#endif                           /* IM2COL_REDUCED_8X */
}

#endif /* IM2COL_REDUCED_GENERIC */
#else  /* DATA_TYPE_FP32 */
#error Data type not supported
#endif /* DATA_TYPE_FP32 */
#endif /* IM2COL_REDUCED */

#ifdef COL2IM
#ifdef WIDTH_OUTPUT

/** This kernel performs a reshaping of the output of the convolution layer.
 *
 * @note The data type must be passed at compile time using "#define DATA_TYPE_FP32"
 *
 * @param[in]  src_ptr     Pointer to the source tensor. Supported data types: F16/F32
 * @param[in]  src_attrs   The attributes of the source tensor
 * @param[out] dst_ptr     Pointer to the destination tensor. Supported data types: same as @p src_ptr
 * @param[in]  dst_attrs   The attributes of the destination tensor
 * @param[in]  dst_depth   The length of the destination tensor in Z dimension
 * @param[in]  dst_strideZ The actual stride of the destination tensor in Z dimension
 */

SHADER_PARAMS_DECLARATION
{
    Tensor3DAttributes src_attrs;
    Tensor3DAttributes dst_attrs;
    uint               dst_depth;
    uint               dst_strideZ;
};

#ifdef DATA_TYPE_FP32

TENSOR_DECLARATION(1, srcBuffer, float, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, float, dst_ptr, dst_shift, 2, restrict);

void main(void)
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);

    uvec3 pos = uvec3(gl_GlobalInvocationID.xyz);
    TENSOR_ITERATOR_ADVANCE_IN_BYTES(src_iter, pos.x * src_attrs.step_y + pos.y * uint(WIDTH_OUTPUT) * src_attrs.step_y + (pos.z % dst_depth) * src_attrs.stride_x + (pos.z / dst_depth) * dst_strideZ);

    STORE_CURRENT_ITEM(dst_ptr, dst_iter, LOAD_CURRENT_ITEM(src_ptr, src_iter));
}

#elif defined(DATA_TYPE_FP16)

TENSOR_DECLARATION(1, srcBuffer, uint, src_ptr, src_shift, 2, readonly);
TENSOR_DECLARATION(2, dstBuffer, uint, dst_ptr, dst_shift, 2, restrict);

void main(void)
{
    Tensor3DIterator src_iter = CONVERT_TO_TENSOR3D_ITERATOR_NO_STEP(src_attrs, src_shift);
    Tensor3DIterator dst_iter = CONVERT_TO_TENSOR3D_ITERATOR(dst_attrs, dst_shift);

    uvec3 pos = uvec3(gl_GlobalInvocationID.xyz);

    if((pos.z % dst_depth) % 2u == 0u)
    {
        uint common_offset_in_bytes = pos.x * src_attrs.step_y * 2u + pos.y * uint(WIDTH_OUTPUT) * src_attrs.step_y + (pos.z % dst_depth) * src_attrs.stride_x + (pos.z / dst_depth) * dst_strideZ;
        uint tmp1_in_offset         = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, common_offset_in_bytes);
        uint tmp2_in_offset         = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, common_offset_in_bytes + src_attrs.step_y);
        vec2 tmp1                   = LOAD_UNPACK2_HALF(src_ptr, tmp1_in_offset);
        vec2 tmp2                   = LOAD_UNPACK2_HALF(src_ptr, tmp2_in_offset);
        vec2 result                 = vec2(tmp1.x, tmp2.x);
        STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, result);
    }
    else
    {
        uint common_offset_in_bytes = pos.x * src_attrs.step_y * 2u + pos.y * uint(WIDTH_OUTPUT) * src_attrs.step_y + (pos.z % dst_depth) * src_attrs.stride_x + (pos.z / dst_depth) * dst_strideZ - 2u;
        uint tmp1_in_offset         = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, common_offset_in_bytes);
        uint tmp2_in_offset         = TENSOR_OFFSET_ADVANCE_IN_BYTES(src_iter, common_offset_in_bytes + src_attrs.step_y);
        vec2 tmp1                   = LOAD_UNPACK2_HALF(src_ptr, tmp1_in_offset);
        vec2 tmp2                   = LOAD_UNPACK2_HALF(src_ptr, tmp2_in_offset);
        vec2 result                 = vec2(tmp1.y, tmp2.y);
        STORE_PACK2_CURRENT_ITEM_HALF(dst_ptr, dst_iter, result);
    }
}

#else /* DATA_TYPE_FP32 */
#error Data type not supported
#endif /* DATA_TYPE_FP32 */
#endif /* WIDTH_OUTPUT */
#endif /* COL2IM */