blob: 6e57ed0af1f4dbf5153cb9325a12e18c970041c1 [file] [log] [blame]
Manuel Bottini7b9998d2019-10-21 17:59:07 +01001/*
Giorgio Arena3d3a01c2021-01-11 15:31:15 +00002 * Copyright (c) 2019-2021 Arm Limited.
Manuel Bottini7b9998d2019-10-21 17:59:07 +01003 *
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
Giorgio Arenae36208c2021-01-21 14:53:56 +000026#if defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE_OUTPUT)
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000027
28#define VEC_TYPE_IN VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
29#define VEC_TYPE_OUT VEC_DATA_TYPE(DATA_TYPE_OUTPUT, VEC_SIZE)
Giorgio Arenae36208c2021-01-21 14:53:56 +000030#define VEC_SELECT_IN SELECT_VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
31#define VEC_SIGNED_INT_IN SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, VEC_SIZE)
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000032
Michalis Spyrou7317e392020-01-17 11:27:49 +000033#if defined(FLOAT_DATA_TYPE)
Giorgio Arenae36208c2021-01-21 14:53:56 +000034#define ISGREATER(x, y) (VEC_SELECT_IN) isgreater(x, y)
35#define ISLESS(x, y) (VEC_SELECT_IN) isless(x, y)
Michalis Spyrou7317e392020-01-17 11:27:49 +000036#else // !FLOAT_DATA_TYPE
37#if defined(WIDTH)
38#define ISGREATER(x, y) (x > y) ? 1 : 0
39#define ISLESS(x, y) (x < y) ? 1 : 0
40#else // !defined(WIDTH)
Giorgio Arena76cb7512021-02-03 12:29:57 +000041#define ISGREATER(x, y) select((VEC_SIGNED_INT_IN)0, (VEC_SIGNED_INT_IN)-1, (VEC_SIGNED_INT_IN)(x > y))
42#define ISLESS(x, y) select((VEC_SIGNED_INT_IN)0, (VEC_SIGNED_INT_IN)-1, (VEC_SIGNED_INT_IN)(x < y))
Michalis Spyrou7317e392020-01-17 11:27:49 +000043#endif // defined(WIDTH)
44#endif // defined(FLOAT_DATA_TYPE)
45
Manuel Bottini7b9998d2019-10-21 17:59:07 +010046#if defined(ARG_MAX)
47#define CONDITION_TO_USE(x, y) ISGREATER(x, y)
48#elif defined(ARG_MIN)
49#define CONDITION_TO_USE(x, y) ISLESS(x, y)
50#else // !(defined(ARG_MAX) || defined(ARG_MIN))
51#error "Unsupported reduction operation!"
52#endif // defined(ARG_MAX)
53
Manuel Bottini7b9998d2019-10-21 17:59:07 +010054#if defined(WIDTH)
55#if defined(ARG_MIN)
56#if defined(PREV_OUTPUT)
57/** Find index minimum value of a vector
58 *
59 * @param[in] input Pointer to the first value.
60 *
61 * @return index of the vector.
62 */
63inline DATA_TYPE_OUTPUT arg_idx_min_prev_out(__global const DATA_TYPE *input, __global const DATA_TYPE_OUTPUT *prev_res, const int x_idx)
64{
65 int end_elem = (x_idx + 1) * 16;
66 if(end_elem > WIDTH)
67 {
68 end_elem = WIDTH - x_idx * 16;
69 }
70 DATA_TYPE_OUTPUT res = prev_res[0];
71 for(int x_v = 1; x_v < end_elem; ++x_v)
72 {
73 res = select(res, prev_res[x_v], *(input + prev_res[x_v]) < * (input + res));
74 }
75 return res;
76}
77#else // !defined(PREV_OUTPUT)
78/** Find index minimum value of a vector
79 *
80 * @param[in] input Pointer to the first value.
81 *
82 * @return index of the vector.
83 */
84inline DATA_TYPE_OUTPUT arg_idx_min(__global const DATA_TYPE *input, const int x_idx)
85{
86#if WIDTH < 16
87 DATA_TYPE_OUTPUT res = 0;
88 for(DATA_TYPE_OUTPUT x_v = res + 1; x_v < WIDTH; ++x_v)
89 {
90 res = select(res, x_v, *(input + x_v) < * (input + res));
91 }
92 return res;
93#else // WIDTH >= 16
94 int x_elem = x_idx * 16;
95 const int x_goback = select(0, 16 - WIDTH % 16, x_elem + 16 > WIDTH);
96 x_elem -= x_goback;
97
98 VEC_DATA_TYPE(DATA_TYPE, 16)
99 in = vload16(0, input - x_goback);
100 VEC_DATA_TYPE(DATA_TYPE_OUTPUT, 16)
101 res = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
102
Giorgio Arenae36208c2021-01-21 14:53:56 +0000103 SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, 8)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100104 idx_sel = (in.s01234567 <= in.s89abcdef);
105 in.s01234567 = select(in.s89abcdef, in.s01234567, idx_sel);
106 res.s01234567 = select(res.s89abcdef, res.s01234567, CONVERT(idx_sel, int8));
107
Giorgio Arenae36208c2021-01-21 14:53:56 +0000108 idx_sel.s0123 = (in.s0123 < in.s4567) || (in.s0123 == in.s4567 && CONVERT((res.s0123 < res.s4567), SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, 4)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100109 in.s0123 = select(in.s4567, in.s0123, idx_sel.s0123);
110 res.s0123 = select(res.s4567, res.s0123, CONVERT(idx_sel.s0123, int4));
111
Giorgio Arenae36208c2021-01-21 14:53:56 +0000112 idx_sel.s01 = (in.s01 < in.s23) || (in.s01 == in.s23 && CONVERT((res.s01 < res.s23), SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, 2)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100113 in.s01 = select(in.s23, in.s01, idx_sel.s01);
114 res.s01 = select(res.s23, res.s01, CONVERT(idx_sel.s01, int2));
115
Giorgio Arenae36208c2021-01-21 14:53:56 +0000116 idx_sel.s0 = (in.s0 < in.s1) || (in.s0 == in.s1 && CONVERT((res.s0 < res.s1), SIGNED_INT_DATA_TYPE(DATA_TYPE)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100117 res.s0 = select(res.s1, res.s0, CONVERT(idx_sel.s0, int));
118
119 return res.s0 + x_elem;
120#endif // WIDTH < 16
121}
122#endif // defined(PREV_OUTPUT)
123#endif // defined(ARG_MIN)
124#if defined(ARG_MAX)
125#if defined(PREV_OUTPUT)
126/** Find index maximum value of a vector
127 *
128 * @param[in] input Pointer to the first value.
129 *
130 * @return index of the vector.
131 */
132inline DATA_TYPE_OUTPUT arg_idx_max_prev_out(__global const DATA_TYPE *input, __global const DATA_TYPE_OUTPUT *prev_res, const int x_idx)
133{
134 int end_elem = (x_idx + 1) * 16;
135 if(end_elem > WIDTH)
136 {
137 end_elem = WIDTH - x_idx * 16;
138 }
139 DATA_TYPE_OUTPUT res = prev_res[0];
140 for(int x_v = 1; x_v < end_elem; ++x_v)
141 {
142 res = select(res, prev_res[x_v], *(input + prev_res[x_v]) > *(input + res));
143 }
144 return res;
145}
146#else // !defined(PREV_OUTPUT)
147/** Find index maximum value of a vector
148 *
149 * @param[in] input Pointer to the first value.
150 *
151 * @return index of the vector.
152 */
153inline DATA_TYPE_OUTPUT arg_idx_max(__global const DATA_TYPE *input, const int x_idx)
154{
155#if WIDTH < 16
156 DATA_TYPE_OUTPUT res = 0;
157 for(DATA_TYPE_OUTPUT x_v = res + 1; x_v < WIDTH; ++x_v)
158 {
159 res = select(res, x_v, *(input + x_v) > *(input + res));
160 }
161 return res;
162#else // WIDTH >= 16
163 int x_elem = x_idx * 16;
164 const int x_goback = select(0, 16 - WIDTH % 16, x_elem + 16 > WIDTH);
165 x_elem -= x_goback;
166
167 VEC_DATA_TYPE(DATA_TYPE, 16)
168 in = vload16(0, input - x_goback);
169 VEC_DATA_TYPE(DATA_TYPE_OUTPUT, 16)
170 res = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
171
Giorgio Arenae36208c2021-01-21 14:53:56 +0000172 SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, 8)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100173 idx_sel = (in.s01234567 >= in.s89abcdef);
174 in.s01234567 = select(in.s89abcdef, in.s01234567, idx_sel);
175 res.s01234567 = select(res.s89abcdef, res.s01234567, CONVERT(idx_sel, int8));
176
Giorgio Arenae36208c2021-01-21 14:53:56 +0000177 idx_sel.s0123 = (in.s0123 > in.s4567) || (in.s0123 == in.s4567 && CONVERT((res.s0123 < res.s4567), SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, 4)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100178 in.s0123 = select(in.s4567, in.s0123, idx_sel.s0123);
179 res.s0123 = select(res.s4567, res.s0123, CONVERT(idx_sel.s0123, int4));
180
Giorgio Arenae36208c2021-01-21 14:53:56 +0000181 idx_sel.s01 = (in.s01 > in.s23) || (in.s01 == in.s23 && CONVERT((res.s01 < res.s23), SIGNED_INT_VEC_DATA_TYPE(DATA_TYPE, 2)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100182 in.s01 = select(in.s23, in.s01, idx_sel.s01);
183 res.s01 = select(res.s23, res.s01, CONVERT(idx_sel.s01, int2));
184
Giorgio Arenae36208c2021-01-21 14:53:56 +0000185 idx_sel.s0 = (in.s0 > in.s1) || (in.s0 == in.s1 && CONVERT((res.s0 < res.s1), SIGNED_INT_DATA_TYPE(DATA_TYPE)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100186 res.s0 = select(res.s1, res.s0, CONVERT(idx_sel.s0, int));
187
188 return res.s0 + x_elem;
189#endif // WIDTH < 16
190}
191#endif // defined(PREV_OUTPUT)
192#endif // defined(ARG_MAX)
193
194/** This kernel performs parallel reduction given an operation on x-axis.
195 *
196 * @note In case the results of previous stages are passed the flag PREV_OUTPUT has to be passed using -DPREV_OUTPUT
197 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
198 * @note The data type of the output must be passed at compile time using -DDATA_TYPE_OUTPUT: e.g. -DDATA_TYPE_OUTPUT=uint
199 * @note The arg_max flag must be passed at compile time using -DARG_MAX if we want to compute the ArgMax
200 * @note The arg_min flag must be passed at compile time using -DARG_MIN if we want to compute the ArgMin
201 *
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100202 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100203 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
204 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
205 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
206 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
207 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
208 * @param[in] prev_res_ptr (Optional) Pointer to previous results tensor. Supported data types: U32/S32
209 * @param[in] prev_res_stride_x (Optional) Stride of the output tensor in X dimension (in bytes)
210 * @param[in] prev_res_step_x (Optional) prev_res_stride_x * number of elements along X processed per workitem(in bytes)
211 * @param[in] prev_res_stride_y (Optional) Stride of the output tensor in Y dimension (in bytes)
212 * @param[in] prev_res_step_y (Optional) prev_res_stride_y * number of elements along Y processed per workitem(in bytes)
213 * @param[in] prev_res_offset_first_element_in_bytes (Optional) The offset of the first element in the previous results tensor
214 * @param[in] partial_res_ptr The local buffer to hold partial result values. Supported data types: U32/S32
215 * @param[in] partial_res_stride_x Stride of the output tensor in X dimension (in bytes)
216 * @param[in] partial_res_step_x partial_res_stride_x * number of elements along X processed per workitem(in bytes)
217 * @param[in] partial_res_stride_y Stride of the output tensor in Y dimension (in bytes)
218 * @param[in] partial_res_step_y partial_res_stride_y * number of elements along Y processed per workitem(in bytes)
219 * @param[in] partial_res_offset_first_element_in_bytes The offset of the first element in the source tensor
220 * @param[in] local_results Local buffer for storing the partial result
221 */
222__kernel void arg_min_max_x(
223 IMAGE_DECLARATION(src),
224#if defined(PREV_OUTPUT)
225 IMAGE_DECLARATION(prev_res),
226#endif // defined(PREV_OUTPUT)
227 IMAGE_DECLARATION(partial_res),
228 __local DATA_TYPE_OUTPUT *local_results)
229{
230#if defined(PREV_OUTPUT)
231 Image src = CONVERT_TO_IMAGE_STRUCT_NO_STEP(src);
232 Image prev_res = CONVERT_TO_IMAGE_STRUCT(prev_res);
233#else // !defined(PREV_OUTPUT)
234 Image src = CONVERT_TO_IMAGE_STRUCT(src);
235#endif // defined(PREV_OUTPUT)
236 Image partial_res = CONVERT_TO_IMAGE_STRUCT(partial_res);
237
238 unsigned int lsize = get_local_size(0);
239 unsigned int lid = get_local_id(0);
240
241 const uint x_idx = get_global_id(0);
242 const uint y_idx = get_global_id(1);
243 const __global DATA_TYPE *src_in_row = (const __global DATA_TYPE *)(src_ptr + src_offset_first_element_in_bytes + y_idx * src_step_y);
244
245 for(unsigned int y = 0; y < get_local_size(1); ++y)
246 {
247#if defined(ARG_MAX)
248#if defined(PREV_OUTPUT)
249 local_results[lid] = arg_idx_max_prev_out(src_in_row, (__global DATA_TYPE_OUTPUT *)offset(&prev_res, 0, y), x_idx);
250#else // !defined(PREV_OUTPUT)
251 local_results[lid] = arg_idx_max((__global DATA_TYPE *)offset(&src, 0, y), x_idx);
252#endif // defined(PREV_OUTPUT)
253#else // defined(ARG_MIN)
254#if defined(PREV_OUTPUT)
255 local_results[lid] = arg_idx_min_prev_out(src_in_row, (__global DATA_TYPE_OUTPUT *)offset(&prev_res, 0, y), x_idx);
256#else // !defined(PREV_OUTPUT)
257 local_results[lid] = arg_idx_min((__global DATA_TYPE *)offset(&src, 0, y), x_idx);
258#endif // defined(PREV_OUTPUT)
259#endif // defined(ARG_MAX) || defined(ARG_MIN)
260
261 barrier(CLK_LOCAL_MEM_FENCE);
262
Manuel Bottini5c829ca2020-01-28 17:25:48 +0000263 // Looking for the next highest power of 2 (maximum value of lsize is 8)
264 unsigned int middle = lsize - 1;
265 middle |= middle >> 1;
266 middle |= middle >> 2;
267 middle += 1;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100268 // Perform parallel reduction
Manuel Bottini5c829ca2020-01-28 17:25:48 +0000269 for(unsigned int i = middle; i > 0; i >>= 1)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100270 {
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100271 if(lid < i && lid + i < lsize)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100272 {
273 DATA_TYPE tmp0 = *(src_in_row + local_results[lid]);
274 DATA_TYPE tmp1 = *(src_in_row + local_results[lid + i]);
275#if defined(ARG_MAX)
276 local_results[lid] = select(
277 local_results[lid],
278 local_results[lid + i],
279 ((tmp0 == tmp1) && (local_results[lid + i] < local_results[lid])) || (tmp0 < tmp1));
280#else // defined(ARG_MIN)
281 local_results[lid] = select(
282 local_results[lid],
283 local_results[lid + i],
284 ((tmp0 == tmp1) && (local_results[lid + i] < local_results[lid])) || (tmp0 > tmp1));
285#endif // defined(ARG_MAX) || defined(ARG_MIN)
286 }
287 barrier(CLK_LOCAL_MEM_FENCE);
288 }
289
290 if(lid == 0)
291 {
292 ((__global DATA_TYPE_OUTPUT *)offset(&partial_res, get_group_id(0), y))[0] = local_results[0];
293 }
294 }
295}
296#endif // defined(WIDTH)
297
298#if defined(HEIGHT)
299/** This kernel performs reduction on y-axis.
300 *
301 * @note The input data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000302 * @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
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100303 * @note The data type of the output must be passed at compile time using -DDATA_TYPE_OUTPUT: e.g. -DDATA_TYPE_OUTPUT=uint
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100304 * @note The height size must be passed at compile time using -DHEIGHT e.g. -DHEIGHT=128
305 *
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000306 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
307 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
308 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
309 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
310 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
311 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100312 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: U32/S32
313 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
314 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
315 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
316 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
317 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
318 */
319__kernel void arg_min_max_y(
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000320 IMAGE_DECLARATION(input),
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100321 IMAGE_DECLARATION(output))
322{
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000323 const int x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100324
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000325 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y;
326 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE_OUTPUT) + get_global_id(1) * output_stride_y;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100327
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000328 VEC_TYPE_IN res = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr), VEC_TYPE_IN);
329
330 VEC_TYPE_OUT indx0 = 0;
Giorgio Arenae36208c2021-01-21 14:53:56 +0000331 for(DATA_TYPE_OUTPUT y = 1; y < HEIGHT; ++y)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100332 {
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000333 VEC_TYPE_IN in = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + y * input_stride_y)), VEC_TYPE_IN);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100334
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000335 VEC_TYPE_OUT cond_conv = CONVERT(CONDITION_TO_USE(in, res), VEC_TYPE_OUT);
Giorgio Arenae36208c2021-01-21 14:53:56 +0000336 indx0 = select(indx0, (VEC_TYPE_OUT)y, cond_conv);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000337 res = select(res, in, CONDITION_TO_USE(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100338 }
339
340 // Store result
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000341 STORE_VECTOR_SELECT(indx, DATA_TYPE_OUTPUT, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100342}
343#endif // defined(HEIGHT)
344
Giorgio Arena76cb7512021-02-03 12:29:57 +0000345#if defined(DEPTH) && !defined(BATCH)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100346/** This kernel performs reduction on z-axis.
347 *
348 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000349 * @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
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100350 * @note The depth size must be passed at compile time using -DDEPTH e.g. -DDEPTH=128
351 *
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100352 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100353 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
354 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
355 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
356 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
357 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
358 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
359 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
360 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: U32/S32
361 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
362 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
363 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
364 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
365 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
366 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
367 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
368 */
369__kernel void arg_min_max_z(
370 TENSOR3D_DECLARATION(input),
371 TENSOR3D_DECLARATION(output))
372{
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000373 const int x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100374
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000375 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y + get_global_id(2) * input_stride_z;
376 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE_OUTPUT) + get_global_id(1) * output_stride_y + get_global_id(2) * output_stride_z;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100377
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000378 VEC_TYPE_IN res = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr), VEC_TYPE_IN);
379
380 VEC_TYPE_OUT indx0 = 0;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100381 for(DATA_TYPE_OUTPUT z = 1; z < DEPTH; ++z)
382 {
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000383 VEC_TYPE_IN in = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + z * input_stride_z)), VEC_TYPE_IN);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100384
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000385 VEC_TYPE_OUT cond_conv = CONVERT(CONDITION_TO_USE(in, res), VEC_TYPE_OUT);
Giorgio Arenae36208c2021-01-21 14:53:56 +0000386 indx0 = select(indx0, (VEC_TYPE_OUT)z, cond_conv);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000387 res = select(res, in, CONDITION_TO_USE(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100388 }
389
390 // Store result
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000391 STORE_VECTOR_SELECT(indx, DATA_TYPE_OUTPUT, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100392}
Giorgio Arena76cb7512021-02-03 12:29:57 +0000393#endif /* defined(DEPTH) && !defined(BATCH) */
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100394
395#if defined(BATCH) && defined(DEPTH)
396/** This kernel performs reduction on w-axis.
397 *
398 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000399 * @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
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100400 * @note The batch size must be passed at compile time using -DBATCH e.g. -DBATCH=128
401 * @note The depth size must be passed at compile time using -DBATCH e.g. -DDEPTH=128
402 *
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100403 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100404 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
405 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
406 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
407 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
408 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
409 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
410 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
411 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
412 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
413 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: U32/S32
414 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
415 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
416 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
417 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
418 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
419 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
420 * @param[in] output_stride_w Stride of the output tensor in W dimension (in bytes)
421 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
422 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
423 */
424__kernel void arg_min_max_w(
425 TENSOR4D_DECLARATION(input),
426 TENSOR4D_DECLARATION(output))
427{
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000428 const int x_offs = max((int)(get_global_id(0) * VEC_SIZE - (VEC_SIZE - VEC_SIZE_LEFTOVER) % VEC_SIZE), 0);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100429
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000430 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y + (get_global_id(2) % DEPTH) * input_stride_z +
431 (get_global_id(2) / DEPTH) * input_stride_w;
432 __global uchar *output_addr = output_ptr + output_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE_OUTPUT) + get_global_id(1) * output_stride_y + (get_global_id(
433 2) % DEPTH) * output_stride_z + (get_global_id(2) / DEPTH) * output_stride_w;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100434
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000435 VEC_TYPE_IN res = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr), VEC_TYPE_IN);
436
437 VEC_TYPE_OUT indx0 = 0;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100438 for(DATA_TYPE_OUTPUT w = 1; w < BATCH; ++w)
439 {
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000440 VEC_TYPE_IN in = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)(input_addr + w * input_stride_w)), VEC_TYPE_IN);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100441
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000442 VEC_TYPE_OUT cond_conv = CONVERT(CONDITION_TO_USE(in, res), VEC_TYPE_OUT);
Giorgio Arenae36208c2021-01-21 14:53:56 +0000443 indx0 = select(indx0, (VEC_TYPE_OUT)w, cond_conv);
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000444 res = select(res, in, CONDITION_TO_USE(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100445 }
446
447 // Store result
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000448 STORE_VECTOR_SELECT(indx, DATA_TYPE_OUTPUT, output_addr, VEC_SIZE, VEC_SIZE_LEFTOVER, VEC_SIZE_LEFTOVER != 0 && get_global_id(0) == 0);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100449}
450#endif /* defined(BATCH) && defined(DEPTH) */
Giorgio Arenae36208c2021-01-21 14:53:56 +0000451#endif // defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE_OUTPUT)