blob: d5cb10086ed2476aa8b6b98cde03a736760acae6 [file] [log] [blame]
Michele Di Giorgio72175632018-05-01 16:52:00 +01001/*
2* Copyright (c) 2018 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
26#if defined(DATA_TYPE) && defined(BLOCK_SIZE) && defined(NUM_GROUPS) && defined(K)
27
28// Check valid BLOCK_SIZES
29#if BLOCK_SIZE != 4 && BLOCK_SIZE != 8 && BLOCK_SIZE != 16
30#error "Only block sizes 4, 8 and 16 are supported"
31#endif /* BLOCK_SIZE != 4 && BLOCK_SIZE != 8 && BLOCK_SIZE != 16 */
32
33#define TYPE VEC_DATA_TYPE(DATA_TYPE, BLOCK_SIZE)
34
35/** Perfoms channel shuffle see https://arxiv.org/pdf/1707.01083.pdf for details.
36 *
37 * @note The number of groups should be given as a preprocessor argument using -DNUM_GROUPS=num_groups. e.g. -DNUM_GROUPS=2
38 * @note The number of channels in each group should be given as a preprocessor argument using -DK=num. e.g. -DK=1
39 * K is equal to num_channels / num_groups.
40 *
41 * @param[in] src_ptr Pointer to the source matrix. Supported data types: U8/S8/QS8/QASYMM8/U16/S16/QS16/F16/U32/S32/F32
42 * @param[in] src_stride_x Stride of the first source tensor in X dimension (in bytes)
43 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
44 * @param[in] src_stride_y Stride of the first source tensor in Y dimension (in bytes)
45 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
46 * @param[in] src_stride_z Stride of the first source tensor in Z dimension (in bytes)
47 * @param[in] src_step_z src_stride_z * number of elements along Z processed per workitem(in bytes)
48 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the first source tensor
49 * @param[out] dst_ptr Pointer to the destination tensor. Supported data types: same as @p src_ptr
50 * @param[in] dst_stride_x Stride of the destination tensor in X dimension (in bytes)
51 * @param[in] dst_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
52 * @param[in] dst_stride_y Stride of the destination tensor in Y dimension (in bytes)
53 * @param[in] dst_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
54 * @param[in] dst_stride_z Stride of the destination tensor in Z dimension (in bytes)
55 * @param[in] dst_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
56 * @param[in] dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
57 */
58__kernel void channel_shuffle_nchw(TENSOR3D_DECLARATION(src),
59 TENSOR3D_DECLARATION(dst))
60{
61 Tensor3D src = CONVERT_TO_TENSOR3D_STRUCT(src);
62 Tensor3D dst = CONVERT_TO_TENSOR3D_STRUCT_NO_STEP(dst);
63
64 const uint curr_channel = get_global_id(2); // channel id of input
65 const uint group_id = curr_channel / NUM_GROUPS; // group id
66 const uint channel_id = curr_channel % NUM_GROUPS; // channel id within the group
67
68 const uint x = get_global_id(0) * BLOCK_SIZE;
69 const uint y = get_global_id(1) * BLOCK_SIZE;
70 const uint z = channel_id * K + group_id;
71
72 // Load the NxN block
73 TYPE u0 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 0, 0));
74 TYPE u1 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 1, 0));
75 TYPE u2 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 2, 0));
76 TYPE u3 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 3, 0));
77#if BLOCK_SIZE > 4
78 TYPE u4 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 4, 0));
79 TYPE u5 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 5, 0));
80 TYPE u6 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 6, 0));
81 TYPE u7 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 7, 0));
82#if BLOCK_SIZE == 16
83 TYPE u8 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 8, 0));
84 TYPE u9 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 9, 0));
85 TYPE u10 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 10, 0));
86 TYPE u11 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 11, 0));
87 TYPE u12 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 12, 0));
88 TYPE u13 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 13, 0));
89 TYPE u14 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 14, 0));
90 TYPE u15 = VLOAD(BLOCK_SIZE)(0, (__global DATA_TYPE *)tensor3D_offset(&src, 0, 15, 0));
91#endif /* BLOCK_SIZE == 16 */
92#endif /* BLOCK_SIZE > 4 */
93
94 // Store blocks
95 VSTORE(BLOCK_SIZE)
96 (u0, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 0, z));
97 VSTORE(BLOCK_SIZE)
98 (u1, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 1, z));
99 VSTORE(BLOCK_SIZE)
100 (u2, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 2, z));
101 VSTORE(BLOCK_SIZE)
102 (u3, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 3, z));
103#if BLOCK_SIZE > 4
104 VSTORE(BLOCK_SIZE)
105 (u4, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 4, z));
106 VSTORE(BLOCK_SIZE)
107 (u5, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 5, z));
108 VSTORE(BLOCK_SIZE)
109 (u6, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 6, z));
110 VSTORE(BLOCK_SIZE)
111 (u7, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 7, z));
112#if BLOCK_SIZE == 16
113 VSTORE(BLOCK_SIZE)
114 (u8, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 8, z));
115 VSTORE(BLOCK_SIZE)
116 (u9, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 9, z));
117 VSTORE(BLOCK_SIZE)
118 (u10, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 10, z));
119 VSTORE(BLOCK_SIZE)
120 (u11, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 11, z));
121 VSTORE(BLOCK_SIZE)
122 (u12, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 12, z));
123 VSTORE(BLOCK_SIZE)
124 (u13, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 13, z));
125 VSTORE(BLOCK_SIZE)
126 (u14, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 14, z));
127 VSTORE(BLOCK_SIZE)
128 (u15, 0, (__global DATA_TYPE *)tensor3D_offset(&dst, x, y + 15, z));
129#endif /* BLOCK_SIZE == 16 */
130#endif /* BLOCK_SIZE > 4 */
131}
132#endif /* defined(DATA_TYPE) && defined(BLOCK_SIZE) && defined(NUM_GROUPS) && defined(K) */