xref: /aosp_15_r20/external/ComputeLibrary/src/core/CL/cl_kernels/common/concatenate.cl (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1/*
2 * Copyright (c) 2017-2022 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(VEC_SIZE)
27#define VEC_INT VEC_DATA_TYPE(int, VEC_SIZE)
28
29#if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT)
30#define VEC_FLOAT VEC_DATA_TYPE(float, VEC_SIZE)
31#define VEC_QUANT VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
32#define CONVERT_RTE(x, type) (convert_##type##_rte((x)))
33#define CONVERT_DOWN(x, type) CONVERT_RTE(x, type)
34inline VEC_QUANT requantize(VEC_QUANT input, float in_offset, float out_offset, float in_scale, float out_scale)
35{
36    const VEC_FLOAT in_f32  = (CONVERT(input, VEC_FLOAT) - (VEC_FLOAT)((float)in_offset)) * (VEC_FLOAT)((float)in_scale);
37    const VEC_FLOAT out_f32 = in_f32 / ((VEC_FLOAT)(float)out_scale) + ((VEC_FLOAT)((float)out_offset));
38    const VEC_QUANT res_q8  = CONVERT_SAT(CONVERT_DOWN(out_f32, VEC_INT), VEC_QUANT);
39    return res_q8;
40}
41#endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */
42
43#if defined(DATA_TYPE)
44#define VEC_TYPE VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
45
46#if defined(ELEMENT_SIZE)
47
48#define SELECT_TYPE SELECT_VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
49#define SEQ VEC_OFFS(int, VEC_SIZE)
50
51#if defined(CONCATENATE_WIDTH_X2)
52/** This kernel concatenates two input tensors into the output tensor along the first dimension
53 *
54 * @note The data type has to be passed at compile time using -DDATA_TYPE. i.e. -DDATA_TYPE=float
55 * @note Vector size has to be passed at compile time using -DVEC_SIZE. i.e. -DVEC_SIZE=16
56 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
57 *
58 * @param[in]  src1_ptr                           Pointer to the source tensor. Supported data types: All.
59 * @param[in]  src1_stride_x                      Stride of the source tensor in X dimension (in bytes)
60 * @param[in]  src1_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
61 * @param[in]  src1_stride_y                      Stride of the source tensor in Y dimension (in bytes)
62 * @param[in]  src1_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
63 * @param[in]  src1_stride_z                      Stride of the source tensor in Z dimension (in bytes)
64 * @param[in]  src1_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
65 * @param[in]  src1_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
66 * @param[in]  src1_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
67 * @param[in]  src1_offset_first_element_in_bytes The offset of the first element in the source tensor
68 * @param[in]  src2_ptr                           Pointer to the source tensor. Supported data types: same as @p src1_ptr
69 * @param[in]  src2_stride_x                      Stride of the source tensor in X dimension (in bytes)
70 * @param[in]  src2_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
71 * @param[in]  src2_stride_y                      Stride of the source tensor in Y dimension (in bytes)
72 * @param[in]  src2_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
73 * @param[in]  src2_stride_z                      Stride of the source tensor in Z dimension (in bytes)
74 * @param[in]  src2_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
75 * @param[in]  src2_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
76 * @param[in]  src2_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
77 * @param[in]  src2_offset_first_element_in_bytes The offset of the first element in the source tensor
78 * @param[out] dst_ptr                            Pointer to the destination tensor. Supported data types: same as @p src1_ptr
79 * @param[in]  dst_stride_x                       Stride of the destination tensor in X dimension (in bytes)
80 * @param[in]  dst_step_x                         dst_stride_x * number of elements along X processed per workitem(in bytes)
81 * @param[in]  dst_stride_y                       Stride of the destination tensor in Y dimension (in bytes)
82 * @param[in]  dst_step_y                         dst_stride_y * number of elements along Y processed per workitem(in bytes)
83 * @param[in]  dst_stride_z                       Stride of the source tensor in Z dimension (in bytes)
84 * @param[in]  dst_step_z                         dst_stride_z * number of elements along Z processed per workitem(in bytes)
85 * @param[in]  dst_stride_w                       Stride of the destination tensor in Z dimension (in bytes)
86 * @param[in]  dst_step_w                         output_stride_z * number of elements along Z processed per workitem(in bytes)
87 * @param[in]  dst_offset_first_element_in_bytes  The offset of the first element in the destination tensor
88 * @param[in]  DEPTH                              Tensor depth
89 * @param[in]  INPUT1_WIDTH                       First input tensor width
90 */
91__kernel void concatenate_width_x2(
92    TENSOR4D_DECLARATION(src1),
93    TENSOR4D_DECLARATION(src2),
94    TENSOR4D_DECLARATION(dst),
95    const int DEPTH,
96    const int INPUT1_WIDTH)
97{
98    // Calculate input indices
99    const int x  = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
100    const int y  = get_global_id(1);
101    const int z  = get_global_id(2) % (int)DEPTH;
102    const int w  = get_global_id(2) / (int)DEPTH;
103    const int x1 = min(x, (int)INPUT1_WIDTH - (int)VEC_SIZE);
104    const int x2 = max(x - (int)INPUT1_WIDTH, 0);
105
106    // Calculate inputs and output addresses
107    const __global uchar *dst_addr  = dst_ptr + (int)dst_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) + y * (int)dst_stride_y + z * (int)dst_stride_z + w * (int)dst_stride_w;
108    const __global uchar *src1_addr = src1_ptr + (int)src1_offset_first_element_in_bytes + x1 * sizeof(DATA_TYPE) + y * (int)src1_stride_y + z * (int)src1_stride_z + w * (int)src1_stride_w;
109    const __global uchar *src2_addr = src2_ptr + (int)src2_offset_first_element_in_bytes + x2 * sizeof(DATA_TYPE) + y * (int)src2_stride_y + z * (int)src2_stride_z + w * (int)src2_stride_w;
110
111    VEC_TYPE src1_values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src1_addr);
112    VEC_TYPE src2_values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src2_addr);
113
114#if defined(OFFSET_IN1) && defined(OFFSET_IN2) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_IN2) && defined(SCALE_OUT)
115    src1_values = requantize(src1_values, OFFSET_IN1, OFFSET_OUT, SCALE_IN1, SCALE_OUT);
116    src2_values = requantize(src2_values, OFFSET_IN2, OFFSET_OUT, SCALE_IN2, SCALE_OUT);
117#endif /* defined(OFFSET_IN1) && defined(OFFSET_IN2) && defined(OFFSET_OUT) && defined(SCALE_IN1)  && defined(SCALE_IN2) && defined(SCALE_OUT) */
118    const VEC_INT x_coords = SEQ + (VEC_INT)(x);
119
120    // Rotate src1/2_values, if values0 is a combination of src1_values and src2_values.
121    SELECT_TYPE cond = CONVERT(((VEC_INT)x < (VEC_INT)INPUT1_WIDTH) && ((VEC_INT)x > (VEC_INT)(INPUT1_WIDTH - VEC_SIZE)), SELECT_TYPE);
122    src1_values      = select(src1_values, ROTATE(src1_values, VEC_SIZE, INPUT1_ROTATE_N), cond);
123    src2_values      = select(src2_values, ROTATE(src2_values, VEC_SIZE, INPUT1_ROTATE_N), cond);
124
125    cond                   = CONVERT(x_coords < (VEC_INT)(INPUT1_WIDTH), SELECT_TYPE);
126    const VEC_TYPE values0 = select(src2_values, src1_values, cond);
127
128    STORE_VECTOR_SELECT(values, DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
129}
130#endif // defined(CONCATENATE_WIDTH_X2)
131
132#if defined(CONCATENATE_WIDTH_X4)
133/** This kernel concatenates four input tensors into the output tensor along the first dimension
134 *
135 * @note The data type has to be passed at compile time using -DDATA_TYPE. i.e. -DDATA_TYPE=float
136 * @note Vector size has to be passed at compile time using -DVEC_SIZE. i.e. -DVEC_SIZE=16
137 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
138 * @note Tensor depth should be given as a preprocessor argument using -DDEPTH=size. e.g. -DDEPTH=16
139 *
140 * @param[in]  src1_ptr                           Pointer to the source tensor. Supported data types: All
141 * @param[in]  src1_stride_x                      Stride of the source tensor in X dimension (in bytes)
142 * @param[in]  src1_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
143 * @param[in]  src1_stride_y                      Stride of the source tensor in Y dimension (in bytes)
144 * @param[in]  src1_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
145 * @param[in]  src1_stride_z                      Stride of the source tensor in Z dimension (in bytes)
146 * @param[in]  src1_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
147 * @param[in]  src1_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
148 * @param[in]  src1_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
149 * @param[in]  src1_offset_first_element_in_bytes The offset of the first element in the source tensor
150 * @param[in]  src2_ptr                           Pointer to the source tensor. Supported data types: same as @p src1_ptr
151 * @param[in]  src2_stride_x                      Stride of the source tensor in X dimension (in bytes)
152 * @param[in]  src2_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
153 * @param[in]  src2_stride_y                      Stride of the source tensor in Y dimension (in bytes)
154 * @param[in]  src2_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
155 * @param[in]  src2_stride_z                      Stride of the source tensor in Z dimension (in bytes)
156 * @param[in]  src2_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
157 * @param[in]  src2_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
158 * @param[in]  src2_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
159 * @param[in]  src2_offset_first_element_in_bytes The offset of the first element in the source tensor
160 * @param[in]  src3_ptr                           Pointer to the source tensor. Supported data types: same as @p src1_ptr
161 * @param[in]  src3_stride_x                      Stride of the source tensor in X dimension (in bytes)
162 * @param[in]  src3_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
163 * @param[in]  src3_stride_y                      Stride of the source tensor in Y dimension (in bytes)
164 * @param[in]  src3_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
165 * @param[in]  src3_stride_z                      Stride of the source tensor in Z dimension (in bytes)
166 * @param[in]  src3_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
167 * @param[in]  src3_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
168 * @param[in]  src3_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
169 * @param[in]  src3_offset_first_element_in_bytes The offset of the first element in the source tensor
170 * @param[in]  src4_ptr                           Pointer to the source tensor. Supported data types: same as @p src1_ptr
171 * @param[in]  src4_stride_x                      Stride of the source tensor in X dimension (in bytes)
172 * @param[in]  src4_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
173 * @param[in]  src4_stride_y                      Stride of the source tensor in Y dimension (in bytes)
174 * @param[in]  src4_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
175 * @param[in]  src4_stride_z                      Stride of the source tensor in Z dimension (in bytes)
176 * @param[in]  src4_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
177 * @param[in]  src4_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
178 * @param[in]  src4_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
179 * @param[in]  src4_offset_first_element_in_bytes The offset of the first element in the source tensor
180 * @param[out] dst_ptr                            Pointer to the destination tensor. Supported data types: same as @p src1_ptr
181 * @param[in]  dst_stride_x                       Stride of the destination tensor in X dimension (in bytes)
182 * @param[in]  dst_step_x                         dst_stride_x * number of elements along X processed per workitem(in bytes)
183 * @param[in]  dst_stride_y                       Stride of the destination tensor in Y dimension (in bytes)
184 * @param[in]  dst_step_y                         dst_stride_y * number of elements along Y processed per workitem(in bytes)
185 * @param[in]  dst_stride_z                       Stride of the source tensor in Z dimension (in bytes)
186 * @param[in]  dst_step_z                         dst_stride_z * number of elements along Z processed per workitem(in bytes)
187 * @param[in]  dst_stride_w                       Stride of the destination tensor in Z dimension (in bytes)
188 * @param[in]  dst_step_w                         output_stride_z * number of elements along Z processed per workitem(in bytes)
189 * @param[in]  dst_offset_first_element_in_bytes  The offset of the first element in the destination tensor
190 * @param[in]  DEPTH                              Tensor depth
191 * @param[in]  INPUT1_WIDTH                       First input tensor width
192 * @param[in]  INPUT2_WIDTH                       Second input tensor width
193 * @param[in]  INPUT3_WIDTH                       Third input tensor width
194 */
195__kernel void concatenate_width_x4(
196    TENSOR4D_DECLARATION(src1),
197    TENSOR4D_DECLARATION(src2),
198    TENSOR4D_DECLARATION(src3),
199    TENSOR4D_DECLARATION(src4),
200    TENSOR4D_DECLARATION(dst),
201    const int DEPTH,
202    const int INPUT1_WIDTH,
203    const int INPUT2_WIDTH,
204    const int INPUT3_WIDTH)
205{
206    // Calculate input indices
207    const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
208    const int y = get_global_id(1);
209    const int z = get_global_id(2) % (int)DEPTH;
210    const int w = get_global_id(2) / (int)DEPTH;
211
212    const int x1 = min(x, (int)INPUT1_WIDTH - (int)VEC_SIZE);
213    const int x2 = min(max(x - (int)INPUT1_WIDTH, 0), (int)INPUT2_WIDTH - (int)VEC_SIZE);
214    const int x3 = min(max(x - (int)INPUT1_WIDTH - (int)INPUT2_WIDTH, 0), (int)INPUT3_WIDTH - (int)VEC_SIZE);
215    const int x4 = max(x - (int)INPUT1_WIDTH - (int)INPUT2_WIDTH - (int)INPUT3_WIDTH, 0);
216
217    // Calculate inputs and output addresses
218    const __global uchar *dst_addr  = dst_ptr + (int)dst_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) + y * (int)dst_stride_y + z * (int)dst_stride_z + w * (int)dst_stride_w;
219    const __global uchar *src1_addr = src1_ptr + (int)src1_offset_first_element_in_bytes + x1 * sizeof(DATA_TYPE) + y * (int)src1_stride_y + z * (int)src1_stride_z + w * (int)src1_stride_w;
220    const __global uchar *src2_addr = src2_ptr + (int)src2_offset_first_element_in_bytes + x2 * sizeof(DATA_TYPE) + y * (int)src2_stride_y + z * (int)src2_stride_z + w * (int)src2_stride_w;
221    const __global uchar *src3_addr = src3_ptr + (int)src3_offset_first_element_in_bytes + x3 * sizeof(DATA_TYPE) + y * (int)src3_stride_y + z * (int)src3_stride_z + w * (int)src3_stride_w;
222    const __global uchar *src4_addr = src4_ptr + (int)src4_offset_first_element_in_bytes + x4 * sizeof(DATA_TYPE) + y * (int)src4_stride_y + z * (int)src4_stride_z + w * (int)src4_stride_w;
223
224    VEC_TYPE src1_values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src1_addr);
225    VEC_TYPE src2_values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src2_addr);
226    VEC_TYPE src3_values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src3_addr);
227    VEC_TYPE src4_values = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src4_addr);
228
229#if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) && defined(OFFSET_IN2) && defined(SCALE_IN2) && defined(OFFSET_IN3) && defined(SCALE_IN3) && defined(OFFSET_IN4) && defined(SCALE_IN4)
230    src1_values = requantize(src1_values, OFFSET_IN1, OFFSET_OUT, SCALE_IN1, SCALE_OUT);
231    src2_values = requantize(src2_values, OFFSET_IN2, OFFSET_OUT, SCALE_IN2, SCALE_OUT);
232    src3_values = requantize(src3_values, OFFSET_IN3, OFFSET_OUT, SCALE_IN3, SCALE_OUT);
233    src4_values = requantize(src4_values, OFFSET_IN4, OFFSET_OUT, SCALE_IN4, SCALE_OUT);
234#endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) && defined(OFFSET_IN2) && defined(SCALE_IN2) && defined(OFFSET_IN3) && defined(SCALE_IN3) && defined(OFFSET_IN4) && defined(SCALE_IN4) */
235
236    const VEC_INT x_coords = SEQ + (VEC_INT)(x);
237
238    SELECT_TYPE cond_in2 = CONVERT(((VEC_INT)x < (VEC_INT)INPUT1_WIDTH && (VEC_INT)x > (VEC_INT)(INPUT1_WIDTH - VEC_SIZE)), SELECT_TYPE);
239    SELECT_TYPE cond_in3 = CONVERT(((VEC_INT)x < (VEC_INT)(INPUT1_WIDTH + INPUT2_WIDTH) && (VEC_INT)x > (VEC_INT)(INPUT1_WIDTH + INPUT2_WIDTH - VEC_SIZE)), SELECT_TYPE);
240    SELECT_TYPE cond_in4 = CONVERT(((VEC_INT)x < (VEC_INT)(INPUT1_WIDTH + INPUT2_WIDTH + INPUT3_WIDTH) && (VEC_INT)x > (VEC_INT)(INPUT1_WIDTH + INPUT2_WIDTH + INPUT3_WIDTH - VEC_SIZE)), SELECT_TYPE);
241
242    // Rotate src1/2_values, if values0 is a combination of src1_values and src2_values.
243    src1_values = select(src1_values, ROTATE(src1_values, VEC_SIZE, INPUT1_ROTATE_N), cond_in2);
244    src2_values = select(src2_values, ROTATE(src2_values, VEC_SIZE, INPUT1_ROTATE_N), cond_in2);
245    // Rotate src2/3_values, if values0 is a combination of src2_values and src3_values.
246    src2_values = select(src2_values, ROTATE(src2_values, VEC_SIZE, INPUT2_ROTATE_N), cond_in3);
247    src3_values = select(src3_values, ROTATE(src3_values, VEC_SIZE, INPUT2_ROTATE_N), cond_in3);
248    // Rotate src3/4_values, if values0 is a combination of src3_values and src4_values.
249    src3_values = select(src3_values, ROTATE(src3_values, VEC_SIZE, INPUT3_ROTATE_N), cond_in4);
250    src4_values = select(src4_values, ROTATE(src4_values, VEC_SIZE, INPUT3_ROTATE_N), cond_in4);
251
252    cond_in2 = CONVERT(x_coords < (VEC_INT)(INPUT1_WIDTH), SELECT_TYPE);
253    cond_in3 = CONVERT(x_coords < (VEC_INT)(INPUT1_WIDTH + INPUT2_WIDTH), SELECT_TYPE);
254    cond_in4 = CONVERT(x_coords < (VEC_INT)(INPUT1_WIDTH + INPUT2_WIDTH + INPUT3_WIDTH), SELECT_TYPE);
255
256    VEC_TYPE values0 = select(src2_values, src1_values, cond_in2);
257    values0          = select(src3_values, values0, cond_in3);
258    values0          = select(src4_values, values0, cond_in4);
259
260    STORE_VECTOR_SELECT(values, DATA_TYPE, dst_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
261}
262#endif /* defined(CONCATENATE_WIDTH_X4) */
263#endif /* defined(ELEMENT_SIZE) */
264
265#if defined(WIDTH_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)
266#if defined(CONCATENATE_WIDTH)
267/** This kernel concatenates the input tensor into the output tensor along the first dimension
268 *
269 * @note The data type has to be passed at compile time using -DDATA_TYPE. i.e. -DDATA_TYPE=float
270 * @note Vector size has to be passed at compile time using -DVEC_SIZE. i.e. -DVEC_SIZE=16
271 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE_LEFTOVER=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
272 * @note The offset for the first spatial dimension has to be passed at compile time using -DWIDTH_OFFSET. i.e. -DWIDTH_OFFSET=128
273 *
274 * @param[in]  src_ptr                           Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/F32
275 * @param[in]  src_stride_x                      Stride of the source tensor in X dimension (in bytes)
276 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
277 * @param[in]  src_stride_y                      Stride of the source tensor in Y dimension (in bytes)
278 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
279 * @param[in]  src_stride_z                      Stride of the source tensor in Z dimension (in bytes)
280 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
281 * @param[in]  src_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
282 * @param[in]  src_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
283 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source tensor
284 * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data types: same as @p src_ptr
285 * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
286 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
287 * @param[in]  dst_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
288 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
289 * @param[in]  dst_stride_z                      Stride of the source tensor in Z dimension (in bytes)
290 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
291 * @param[in]  dst_stride_w                      Stride of the destination tensor in Z dimension (in bytes)
292 * @param[in]  dst_step_w                        output_stride_z * number of elements along Z processed per workitem(in bytes)
293 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
294 * @param[in]  DEPTH                             Tensor depth
295 */
296__kernel void concatenate_width(
297    TENSOR4D_DECLARATION(src),
298    TENSOR4D_DECLARATION(dst),
299    const int DEPTH)
300{
301    // Calculate input indices
302    const int x = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
303    const int y = get_global_id(1);
304    const int z = get_global_id(2) % (int)DEPTH;
305    const int w = get_global_id(2) / (int)DEPTH;
306
307    __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) + y * src_stride_y + z * src_stride_z + w * src_stride_w;
308    __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x * sizeof(DATA_TYPE) + y * dst_stride_y + z * dst_stride_z + w * dst_stride_w;
309
310    VEC_TYPE source_values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src_addr);
311
312#if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT)
313    const VEC_QUANT out0 = requantize(source_values0, OFFSET_IN1, OFFSET_OUT, SCALE_IN1, SCALE_OUT);
314    STORE_VECTOR_SELECT(out, DATA_TYPE, dst_addr + WIDTH_OFFSET * sizeof(DATA_TYPE), VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
315#else  /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */
316    STORE_VECTOR_SELECT(source_values, DATA_TYPE, dst_addr + WIDTH_OFFSET * sizeof(DATA_TYPE), VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
317#endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */
318}
319#endif /* defined(CONCATENATE_WIDTH) */
320#endif /* defined(WIDTH_OFFSET) && defined(VEC_SIZE) && defined(VEC_SIZE_LEFTOVER)*/
321
322#if defined(VEC_SIZE_LEFTOVER)
323#if defined(CONCATENATE_HEIGHT)
324#if defined(HEIGHT_OFFSET) && defined(VEC_SIZE)
325/** This kernel concatenates the input tensor into the output tensor along the second dimension
326 *
327 * @note The data type has to be passed at compile time using -DDATA_TYPE. i.e. -DDATA_TYPE=float
328 * @note Vector size has to be passed at compile time using -DVEC_SIZE. i.e. -DVEC_SIZE=16
329 * @note Vector sizes supported are 2,4,8 and 16.
330 * @note The offset for the second spatial dimension has to be passed at compile time using -DHEIGHT_OFFSET. i.e. -DHEIGHT_OFFSET=128
331 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
332 *
333 * @param[in]  src_ptr                           Pointer to the source tensor. Supported data types: U8/S8/QASYMM8/U16/S16/F16/U32/F32
334 * @param[in]  src_stride_x                      Stride of the source tensor in X dimension (in bytes)
335 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
336 * @param[in]  src_stride_y                      Stride of the source tensor in Y dimension (in bytes)
337 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
338 * @param[in]  src_stride_z                      Stride of the source tensor in Z dimension (in bytes)
339 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
340 * @param[in]  src_stride_w                      Stride of the first source tensor in Z dimension (in bytes)
341 * @param[in]  src_step_w                        src_stride_z * number of elements along Z processed per workitem(in bytes)
342 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source tensor
343 * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data types: same as @p src_ptr
344 * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
345 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
346 * @param[in]  dst_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
347 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
348 * @param[in]  dst_stride_z                      Stride of the source tensor in Z dimension (in bytes)
349 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
350 * @param[in]  dst_stride_w                      Stride of the destination tensor in Z dimension (in bytes)
351 * @param[in]  dst_step_w                        output_stride_z * number of elements along Z processed per workitem(in bytes)
352 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
353 * @param[in]  DEPTH                             Tensor depth
354 */
355__kernel void concatenate_height(
356    TENSOR4D_DECLARATION(src),
357    TENSOR4D_DECLARATION(dst),
358    const int DEPTH)
359{
360    const int x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0) * sizeof(DATA_TYPE);
361
362    __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x_offs + get_global_id(1) * src_stride_y + (get_global_id(2) % DEPTH) * src_stride_z + (get_global_id(
363                                   2) / DEPTH) * src_stride_w;
364    __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs + get_global_id(1) * dst_stride_y + (get_global_id(2) % DEPTH) * dst_stride_z + (get_global_id(
365                                   2) / DEPTH) * dst_stride_w;
366
367    VEC_TYPE source_values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src_addr);
368
369#if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT)
370    const VEC_QUANT out0 = requantize(source_values0, OFFSET_IN1, OFFSET_OUT, SCALE_IN1, SCALE_OUT);
371    STORE_VECTOR_SELECT(out, DATA_TYPE, dst_addr + HEIGHT_OFFSET * dst_stride_y, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
372#else  /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */
373    STORE_VECTOR_SELECT(source_values, DATA_TYPE, dst_addr + HEIGHT_OFFSET * dst_stride_y, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
374#endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */
375}
376#endif /* defined(CONCATENATE_HEIGHT) */
377#endif /* defined(HEIGHT_OFFSET) */
378
379#if defined(CONCATENATE)
380/** This kernel concatenates the input tensor into the output tensor along the third dimension
381 *
382 * @note The data type has to be passed at compile time using -DDATA_TYPE. i.e. -DDATA_TYPE=float
383 * @note Vector size has to be passed at compile time using -DVEC_SIZE. i.e. -DVEC_SIZE=16
384 * @note Leftover vector size has to be passed at compile time using -DVEC_SIZE_LEFTOVER. e.g. -DVEC_SIZE=3. It is defined as the remainder between the input's first dimension and VEC_SIZE
385 *
386 * @param[in]  src_ptr                           Pointer to the source tensor. Supported data types: All
387 * @param[in]  src_stride_x                      Stride of the source tensor in X dimension (in bytes)
388 * @param[in]  src_step_x                        src_stride_x * number of elements along X processed per workitem(in bytes)
389 * @param[in]  src_stride_y                      Stride of the source tensor in Y dimension (in bytes)
390 * @param[in]  src_step_y                        src_stride_y * number of elements along Y processed per workitem(in bytes)
391 * @param[in]  src_stride_z                      Stride of the source tensor in Z dimension (in bytes)
392 * @param[in]  src_step_z                        src_stride_z * number of elements along Z processed per workitem(in bytes)
393 * @param[in]  src_offset_first_element_in_bytes The offset of the first element in the source tensor
394 * @param[out] dst_ptr                           Pointer to the destination tensor. Supported data types: same as @p src_ptr
395 * @param[in]  dst_stride_x                      Stride of the destination tensor in X dimension (in bytes)
396 * @param[in]  dst_step_x                        dst_stride_x * number of elements along X processed per workitem(in bytes)
397 * @param[in]  dst_stride_y                      Stride of the destination tensor in Y dimension (in bytes)
398 * @param[in]  dst_step_y                        dst_stride_y * number of elements along Y processed per workitem(in bytes)
399 * @param[in]  dst_stride_z                      Stride of the source tensor in Z dimension (in bytes)
400 * @param[in]  dst_step_z                        dst_stride_z * number of elements along Z processed per workitem(in bytes)
401 * @param[in]  dst_offset_first_element_in_bytes The offset of the first element in the destination tensor
402 * @param[in]  offsets                           The offsets to the first valid element of the output tensor in bytes
403 */
404__kernel void concatenate(
405    TENSOR3D_DECLARATION(src),
406    TENSOR3D_DECLARATION(dst),
407    int offset)
408{
409    uint x_offs = max((int)(get_global_id(0) * VEC_SIZE * sizeof(DATA_TYPE) - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE * sizeof(DATA_TYPE)), 0);
410
411    __global uchar *src_addr = src_ptr + src_offset_first_element_in_bytes + x_offs + get_global_id(1) * src_stride_y + get_global_id(2) * src_stride_z;
412    __global uchar *dst_addr = dst_ptr + dst_offset_first_element_in_bytes + x_offs + get_global_id(1) * dst_stride_y + get_global_id(2) * dst_stride_z;
413
414    VEC_TYPE source_values0 = VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)src_addr);
415
416#if defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT)
417    source_values0 = requantize(source_values0, OFFSET_IN1, OFFSET_OUT, SCALE_IN1, SCALE_OUT);
418#endif /* defined(OFFSET_IN1) && defined(OFFSET_OUT) && defined(SCALE_IN1) && defined(SCALE_OUT) */
419
420    STORE_VECTOR_SELECT(source_values, DATA_TYPE, dst_addr + offset, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0)
421}
422#endif // defined(CONCATENATE)
423#endif /* defined(VEC_SIZE_LEFTOVER) */
424#endif /* defined(DATA_TYPE) */
425#endif /* defined(VEC_SIZE) */
426