xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/nchw/channel_shuffle.cl (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1/*
2* Copyright (c) 2018-2021 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "helpers.h"
25#include "tile_helpers.h"
26
27#if defined(DATA_TYPE) && defined(VEC_SIZE) && defined(NUM_GROUPS) && defined(K) && defined(SRC_DIM_Z)
28
29// Check valid VEC_SIZES
30#if VEC_SIZE != 1 && VEC_SIZE != 2 && VEC_SIZE != 3 && VEC_SIZE != 4 && VEC_SIZE != 8 && VEC_SIZE != 16
31#error "Only vector sizes 1, 2, 3, 4, 8 and 16 are supported"
32#endif // VEC_SIZE != 1 && VEC_SIZE != 2 && VEC_SIZE != 3 && VEC_SIZE != 4 && VEC_SIZE != 8 && VEC_SIZE != 16
33
34#define DIV_MOD_UINT(x, y, div_res, mod_res)                \
35    ({                                                      \
36        div_res = (uint)((x)/(y)); \
37        uint r  = div_res * (y);                            \
38        mod_res = (x)-r;                                    \
39    })
40
41/** Performs channel shuffle when the data layout is NCHW. See https://arxiv.org/pdf/1707.01083.pdf for details.
42 *
43 * @note The vector size must be given as a preprocessor argument using -DVEC_SIZE=num. e.g. -DVEC_SIZE=4
44 * @note The depth of the tensor must be given as a preprocessor argument using -DSRC_DIM_Z=num. e.g. -DSRC_DIM_Z=64
45 * @note The number of groups must be given as a preprocessor argument using -DNUM_GROUPS=num_groups. e.g. -DNUM_GROUPS=2
46 * @note The number of channels in each group must be given as a preprocessor argument using -DK=num. e.g. -DK=1
47 *       K is equal to num_channels / num_groups.
48 *
49 * @param[in]  src_ptr                           Pointer to the source matrix. Supported data types: All
50 * @param[in]  src_stride_x                      Stride of the first source tensor in X dimension (in bytes)
51 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
52 * @param[in]  src_stride_y                      Stride of the first source tensor in Y dimension (in bytes)
53 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
54 * @param[in]  src_stride_z                      Stride of the first source tensor in Z dimension (in bytes)
55 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
56 * @param[in]  src_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
57 * @param[in]  src_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
58 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the first source tensor
59 * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data types: same as @p src_ptr
60 * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
61 * @param[in]  dst_step_x                        output_stride_x * number of elements along X processed per workitem(in bytes)
62 * @param[in]  dst_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
63 * @param[in]  dst_step_y                        output_stride_y * number of elements along Y processed per workitem(in bytes)
64 * @param[in]  dst_stride_z                      Stride of the destination tensor in Z dimension (in bytes)
65 * @param[in]  dst_step_z                        output_stride_z * number of elements along Z processed per workitem(in bytes)
66 * @param[in]  dst_stride_w                      Stride of the destination tensor in Z dimension (in bytes)
67 * @param[in]  dst_step_w                        output_stride_z * number of elements along Z processed per workitem(in bytes)
68 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
69 */
70__kernel void channel_shuffle_nchw(TENSOR4D_DECLARATION(src),
71                                   TENSOR4D_DECLARATION(dst))
72{
73    uint curr_channel = 0; // channel id of input
74    uint batch_id     = 0; // batch id
75    uint group_id     = 0; // group id
76    uint channel_id   = 0; // channel id within the group
77
78    // Compute curr_channel and batch_id
79    DIV_MOD_UINT(get_global_id(2), SRC_DIM_Z, batch_id, curr_channel);
80
81    // Compute group_id and channel_id
82    DIV_MOD_UINT(curr_channel, K, group_id, channel_id);
83
84    const uint x = get_global_id(0) * VEC_SIZE;
85    const uint y = get_global_id(1) * 2;
86    const uint z = channel_id * NUM_GROUPS + group_id;
87
88    // Load the Nx2 block
89    const __global uchar *input_ptr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) + y * src_stride_y + curr_channel * src_stride_z + batch_id * src_stride_w;
90    VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
91    u0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_ptr + 0 * src_stride_y));
92    VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
93    u1 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_ptr + 1 * src_stride_y));
94
95    // Store blocks
96    __global uchar *output_ptr = dst_ptr + dst_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) + y * dst_stride_y + z * dst_stride_z + batch_id * dst_stride_w;
97    VSTORE(VEC_SIZE)
98    (u0, 0, (__global DATA_TYPE *)(output_ptr + 0 * dst_stride_y));
99    VSTORE(VEC_SIZE)
100    (u1, 0, (__global DATA_TYPE *)(output_ptr + 1 * dst_stride_y));
101}
102
103#endif // defined(DATA_TYPE) && defined(VEC_SIZE) && defined(NUM_GROUPS) && defined(K) && defined(SRC_DIM_Z)
104