blob: b28b15b73ec713e0fc8bba07a4e4e429358e902f [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 Arena3d3a01c2021-01-11 15:31:15 +000026#if defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE_OUTPUT) && defined(DATA_TYPE_SELECT)
27
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)
30
Michalis Spyrou7317e392020-01-17 11:27:49 +000031#if defined(FLOAT_DATA_TYPE)
32#define ISGREATER(x, y) isgreater(x, y)
33#define ISLESS(x, y) isless(x, y)
34#else // !FLOAT_DATA_TYPE
35#if defined(WIDTH)
36#define ISGREATER(x, y) (x > y) ? 1 : 0
37#define ISLESS(x, y) (x < y) ? 1 : 0
38#else // !defined(WIDTH)
Giorgio Arena3d3a01c2021-01-11 15:31:15 +000039#define ISGREATER(x, y) select((VEC_DATA_TYPE(DATA_TYPE_SELECT, VEC_SIZE))0, (VEC_DATA_TYPE(DATA_TYPE_SELECT, VEC_SIZE)) - 1, x > y)
40#define ISLESS(x, y) select((VEC_DATA_TYPE(DATA_TYPE_SELECT, VEC_SIZE))0, (VEC_DATA_TYPE(DATA_TYPE_SELECT, VEC_SIZE)) - 1, x < y)
Michalis Spyrou7317e392020-01-17 11:27:49 +000041#endif // defined(WIDTH)
42#endif // defined(FLOAT_DATA_TYPE)
43
Manuel Bottini7b9998d2019-10-21 17:59:07 +010044#if defined(ARG_MAX)
45#define CONDITION_TO_USE(x, y) ISGREATER(x, y)
46#elif defined(ARG_MIN)
47#define CONDITION_TO_USE(x, y) ISLESS(x, y)
48#else // !(defined(ARG_MAX) || defined(ARG_MIN))
49#error "Unsupported reduction operation!"
50#endif // defined(ARG_MAX)
51
Manuel Bottini7b9998d2019-10-21 17:59:07 +010052#if defined(WIDTH)
53#if defined(ARG_MIN)
54#if defined(PREV_OUTPUT)
55/** Find index minimum value of a vector
56 *
57 * @param[in] input Pointer to the first value.
58 *
59 * @return index of the vector.
60 */
61inline DATA_TYPE_OUTPUT arg_idx_min_prev_out(__global const DATA_TYPE *input, __global const DATA_TYPE_OUTPUT *prev_res, const int x_idx)
62{
63 int end_elem = (x_idx + 1) * 16;
64 if(end_elem > WIDTH)
65 {
66 end_elem = WIDTH - x_idx * 16;
67 }
68 DATA_TYPE_OUTPUT res = prev_res[0];
69 for(int x_v = 1; x_v < end_elem; ++x_v)
70 {
71 res = select(res, prev_res[x_v], *(input + prev_res[x_v]) < * (input + res));
72 }
73 return res;
74}
75#else // !defined(PREV_OUTPUT)
76/** Find index minimum value of a vector
77 *
78 * @param[in] input Pointer to the first value.
79 *
80 * @return index of the vector.
81 */
82inline DATA_TYPE_OUTPUT arg_idx_min(__global const DATA_TYPE *input, const int x_idx)
83{
84#if WIDTH < 16
85 DATA_TYPE_OUTPUT res = 0;
86 for(DATA_TYPE_OUTPUT x_v = res + 1; x_v < WIDTH; ++x_v)
87 {
88 res = select(res, x_v, *(input + x_v) < * (input + res));
89 }
90 return res;
91#else // WIDTH >= 16
92 int x_elem = x_idx * 16;
93 const int x_goback = select(0, 16 - WIDTH % 16, x_elem + 16 > WIDTH);
94 x_elem -= x_goback;
95
96 VEC_DATA_TYPE(DATA_TYPE, 16)
97 in = vload16(0, input - x_goback);
98 VEC_DATA_TYPE(DATA_TYPE_OUTPUT, 16)
99 res = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
100
Michalis Spyrou7317e392020-01-17 11:27:49 +0000101 VEC_DATA_TYPE(DATA_TYPE_SELECT, 8)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100102 idx_sel = (in.s01234567 <= in.s89abcdef);
103 in.s01234567 = select(in.s89abcdef, in.s01234567, idx_sel);
104 res.s01234567 = select(res.s89abcdef, res.s01234567, CONVERT(idx_sel, int8));
105
Michalis Spyrou7317e392020-01-17 11:27:49 +0000106 idx_sel.s0123 = (in.s0123 < in.s4567) || (in.s0123 == in.s4567 && CONVERT((res.s0123 < res.s4567), VEC_DATA_TYPE(DATA_TYPE_SELECT, 4)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100107 in.s0123 = select(in.s4567, in.s0123, idx_sel.s0123);
108 res.s0123 = select(res.s4567, res.s0123, CONVERT(idx_sel.s0123, int4));
109
Michalis Spyrou7317e392020-01-17 11:27:49 +0000110 idx_sel.s01 = (in.s01 < in.s23) || (in.s01 == in.s23 && CONVERT((res.s01 < res.s23), VEC_DATA_TYPE(DATA_TYPE_SELECT, 2)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100111 in.s01 = select(in.s23, in.s01, idx_sel.s01);
112 res.s01 = select(res.s23, res.s01, CONVERT(idx_sel.s01, int2));
113
Michalis Spyrou7317e392020-01-17 11:27:49 +0000114 idx_sel.s0 = (in.s0 < in.s1) || (in.s0 == in.s1 && CONVERT((res.s0 < res.s1), DATA_TYPE_SELECT));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100115 res.s0 = select(res.s1, res.s0, CONVERT(idx_sel.s0, int));
116
117 return res.s0 + x_elem;
118#endif // WIDTH < 16
119}
120#endif // defined(PREV_OUTPUT)
121#endif // defined(ARG_MIN)
122#if defined(ARG_MAX)
123#if defined(PREV_OUTPUT)
124/** Find index maximum value of a vector
125 *
126 * @param[in] input Pointer to the first value.
127 *
128 * @return index of the vector.
129 */
130inline DATA_TYPE_OUTPUT arg_idx_max_prev_out(__global const DATA_TYPE *input, __global const DATA_TYPE_OUTPUT *prev_res, const int x_idx)
131{
132 int end_elem = (x_idx + 1) * 16;
133 if(end_elem > WIDTH)
134 {
135 end_elem = WIDTH - x_idx * 16;
136 }
137 DATA_TYPE_OUTPUT res = prev_res[0];
138 for(int x_v = 1; x_v < end_elem; ++x_v)
139 {
140 res = select(res, prev_res[x_v], *(input + prev_res[x_v]) > *(input + res));
141 }
142 return res;
143}
144#else // !defined(PREV_OUTPUT)
145/** Find index maximum value of a vector
146 *
147 * @param[in] input Pointer to the first value.
148 *
149 * @return index of the vector.
150 */
151inline DATA_TYPE_OUTPUT arg_idx_max(__global const DATA_TYPE *input, const int x_idx)
152{
153#if WIDTH < 16
154 DATA_TYPE_OUTPUT res = 0;
155 for(DATA_TYPE_OUTPUT x_v = res + 1; x_v < WIDTH; ++x_v)
156 {
157 res = select(res, x_v, *(input + x_v) > *(input + res));
158 }
159 return res;
160#else // WIDTH >= 16
161 int x_elem = x_idx * 16;
162 const int x_goback = select(0, 16 - WIDTH % 16, x_elem + 16 > WIDTH);
163 x_elem -= x_goback;
164
165 VEC_DATA_TYPE(DATA_TYPE, 16)
166 in = vload16(0, input - x_goback);
167 VEC_DATA_TYPE(DATA_TYPE_OUTPUT, 16)
168 res = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
169
Michalis Spyrou7317e392020-01-17 11:27:49 +0000170 VEC_DATA_TYPE(DATA_TYPE_SELECT, 8)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100171 idx_sel = (in.s01234567 >= in.s89abcdef);
172 in.s01234567 = select(in.s89abcdef, in.s01234567, idx_sel);
173 res.s01234567 = select(res.s89abcdef, res.s01234567, CONVERT(idx_sel, int8));
174
Michalis Spyrou7317e392020-01-17 11:27:49 +0000175 idx_sel.s0123 = (in.s0123 > in.s4567) || (in.s0123 == in.s4567 && CONVERT((res.s0123 < res.s4567), VEC_DATA_TYPE(DATA_TYPE_SELECT, 4)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100176 in.s0123 = select(in.s4567, in.s0123, idx_sel.s0123);
177 res.s0123 = select(res.s4567, res.s0123, CONVERT(idx_sel.s0123, int4));
178
Michalis Spyrou7317e392020-01-17 11:27:49 +0000179 idx_sel.s01 = (in.s01 > in.s23) || (in.s01 == in.s23 && CONVERT((res.s01 < res.s23), VEC_DATA_TYPE(DATA_TYPE_SELECT, 2)));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100180 in.s01 = select(in.s23, in.s01, idx_sel.s01);
181 res.s01 = select(res.s23, res.s01, CONVERT(idx_sel.s01, int2));
182
Michalis Spyrou7317e392020-01-17 11:27:49 +0000183 idx_sel.s0 = (in.s0 > in.s1) || (in.s0 == in.s1 && CONVERT((res.s0 < res.s1), DATA_TYPE_SELECT));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100184 res.s0 = select(res.s1, res.s0, CONVERT(idx_sel.s0, int));
185
186 return res.s0 + x_elem;
187#endif // WIDTH < 16
188}
189#endif // defined(PREV_OUTPUT)
190#endif // defined(ARG_MAX)
191
192/** This kernel performs parallel reduction given an operation on x-axis.
193 *
194 * @note In case the results of previous stages are passed the flag PREV_OUTPUT has to be passed using -DPREV_OUTPUT
195 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
196 * @note The data type of the output must be passed at compile time using -DDATA_TYPE_OUTPUT: e.g. -DDATA_TYPE_OUTPUT=uint
197 * @note The arg_max flag must be passed at compile time using -DARG_MAX if we want to compute the ArgMax
198 * @note The arg_min flag must be passed at compile time using -DARG_MIN if we want to compute the ArgMin
199 *
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100200 * @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 +0100201 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
202 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
203 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
204 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
205 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
206 * @param[in] prev_res_ptr (Optional) Pointer to previous results tensor. Supported data types: U32/S32
207 * @param[in] prev_res_stride_x (Optional) Stride of the output tensor in X dimension (in bytes)
208 * @param[in] prev_res_step_x (Optional) prev_res_stride_x * number of elements along X processed per workitem(in bytes)
209 * @param[in] prev_res_stride_y (Optional) Stride of the output tensor in Y dimension (in bytes)
210 * @param[in] prev_res_step_y (Optional) prev_res_stride_y * number of elements along Y processed per workitem(in bytes)
211 * @param[in] prev_res_offset_first_element_in_bytes (Optional) The offset of the first element in the previous results tensor
212 * @param[in] partial_res_ptr The local buffer to hold partial result values. Supported data types: U32/S32
213 * @param[in] partial_res_stride_x Stride of the output tensor in X dimension (in bytes)
214 * @param[in] partial_res_step_x partial_res_stride_x * number of elements along X processed per workitem(in bytes)
215 * @param[in] partial_res_stride_y Stride of the output tensor in Y dimension (in bytes)
216 * @param[in] partial_res_step_y partial_res_stride_y * number of elements along Y processed per workitem(in bytes)
217 * @param[in] partial_res_offset_first_element_in_bytes The offset of the first element in the source tensor
218 * @param[in] local_results Local buffer for storing the partial result
219 */
220__kernel void arg_min_max_x(
221 IMAGE_DECLARATION(src),
222#if defined(PREV_OUTPUT)
223 IMAGE_DECLARATION(prev_res),
224#endif // defined(PREV_OUTPUT)
225 IMAGE_DECLARATION(partial_res),
226 __local DATA_TYPE_OUTPUT *local_results)
227{
228#if defined(PREV_OUTPUT)
229 Image src = CONVERT_TO_IMAGE_STRUCT_NO_STEP(src);
230 Image prev_res = CONVERT_TO_IMAGE_STRUCT(prev_res);
231#else // !defined(PREV_OUTPUT)
232 Image src = CONVERT_TO_IMAGE_STRUCT(src);
233#endif // defined(PREV_OUTPUT)
234 Image partial_res = CONVERT_TO_IMAGE_STRUCT(partial_res);
235
236 unsigned int lsize = get_local_size(0);
237 unsigned int lid = get_local_id(0);
238
239 const uint x_idx = get_global_id(0);
240 const uint y_idx = get_global_id(1);
241 const __global DATA_TYPE *src_in_row = (const __global DATA_TYPE *)(src_ptr + src_offset_first_element_in_bytes + y_idx * src_step_y);
242
243 for(unsigned int y = 0; y < get_local_size(1); ++y)
244 {
245#if defined(ARG_MAX)
246#if defined(PREV_OUTPUT)
247 local_results[lid] = arg_idx_max_prev_out(src_in_row, (__global DATA_TYPE_OUTPUT *)offset(&prev_res, 0, y), x_idx);
248#else // !defined(PREV_OUTPUT)
249 local_results[lid] = arg_idx_max((__global DATA_TYPE *)offset(&src, 0, y), x_idx);
250#endif // defined(PREV_OUTPUT)
251#else // defined(ARG_MIN)
252#if defined(PREV_OUTPUT)
253 local_results[lid] = arg_idx_min_prev_out(src_in_row, (__global DATA_TYPE_OUTPUT *)offset(&prev_res, 0, y), x_idx);
254#else // !defined(PREV_OUTPUT)
255 local_results[lid] = arg_idx_min((__global DATA_TYPE *)offset(&src, 0, y), x_idx);
256#endif // defined(PREV_OUTPUT)
257#endif // defined(ARG_MAX) || defined(ARG_MIN)
258
259 barrier(CLK_LOCAL_MEM_FENCE);
260
Manuel Bottini5c829ca2020-01-28 17:25:48 +0000261 // Looking for the next highest power of 2 (maximum value of lsize is 8)
262 unsigned int middle = lsize - 1;
263 middle |= middle >> 1;
264 middle |= middle >> 2;
265 middle += 1;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100266 // Perform parallel reduction
Manuel Bottini5c829ca2020-01-28 17:25:48 +0000267 for(unsigned int i = middle; i > 0; i >>= 1)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100268 {
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100269 if(lid < i && lid + i < lsize)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100270 {
271 DATA_TYPE tmp0 = *(src_in_row + local_results[lid]);
272 DATA_TYPE tmp1 = *(src_in_row + local_results[lid + i]);
273#if defined(ARG_MAX)
274 local_results[lid] = select(
275 local_results[lid],
276 local_results[lid + i],
277 ((tmp0 == tmp1) && (local_results[lid + i] < local_results[lid])) || (tmp0 < tmp1));
278#else // defined(ARG_MIN)
279 local_results[lid] = select(
280 local_results[lid],
281 local_results[lid + i],
282 ((tmp0 == tmp1) && (local_results[lid + i] < local_results[lid])) || (tmp0 > tmp1));
283#endif // defined(ARG_MAX) || defined(ARG_MIN)
284 }
285 barrier(CLK_LOCAL_MEM_FENCE);
286 }
287
288 if(lid == 0)
289 {
290 ((__global DATA_TYPE_OUTPUT *)offset(&partial_res, get_group_id(0), y))[0] = local_results[0];
291 }
292 }
293}
294#endif // defined(WIDTH)
295
296#if defined(HEIGHT)
297/** This kernel performs reduction on y-axis.
298 *
299 * @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 +0000300 * @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 +0100301 * @note The data type of the output must be passed at compile time using -DDATA_TYPE_OUTPUT: e.g. -DDATA_TYPE_OUTPUT=uint
Michalis Spyrou7317e392020-01-17 11:27:49 +0000302 * @note The data type of the select results must be passed at compile time using -DDATA_TYPE_SELECT: e.g. -DDATA_TYPE_SELECT=int
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100303 * @note The height size must be passed at compile time using -DHEIGHT e.g. -DHEIGHT=128
304 *
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000305 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
306 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
307 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
308 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
309 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
310 * @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 +0100311 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: U32/S32
312 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
313 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
314 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
315 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
316 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
317 */
318__kernel void arg_min_max_y(
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000319 IMAGE_DECLARATION(input),
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100320 IMAGE_DECLARATION(output))
321{
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000322 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 +0100323
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000324 __global uchar *input_addr = input_ptr + input_offset_first_element_in_bytes + x_offs * sizeof(DATA_TYPE) + get_global_id(1) * input_stride_y;
325 __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 +0100326
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000327 VEC_TYPE_IN res = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr), VEC_TYPE_IN);
328
329 VEC_TYPE_OUT indx0 = 0;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100330 for(unsigned int y = 1; y < HEIGHT; ++y)
331 {
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000332 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 +0100333
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000334 VEC_TYPE_OUT cond_conv = CONVERT(CONDITION_TO_USE(in, res), VEC_TYPE_OUT);
335 indx0 = select(indx0, y, cond_conv);
336 res = select(res, in, CONDITION_TO_USE(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100337 }
338
339 // Store result
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000340 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 +0100341}
342#endif // defined(HEIGHT)
343
344#if defined(DEPTH)
345/** This kernel performs reduction on z-axis.
346 *
347 * @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 +0000348 * @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
Michalis Spyrou7317e392020-01-17 11:27:49 +0000349 * @note The data type of the select results must be passed at compile time using -DDATA_TYPE_SELECT: e.g. -DDATA_TYPE_SELECT=int
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);
386 indx0 = select(indx0, z, cond_conv);
387 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}
393#endif /* defined(DEPTH) */
394
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
Michalis Spyrou7317e392020-01-17 11:27:49 +0000400 * @note The data type of the select results must be passed at compile time using -DDATA_TYPE_SELECT: e.g. -DDATA_TYPE_SELECT=int
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100401 * @note The batch size must be passed at compile time using -DBATCH e.g. -DBATCH=128
402 * @note The depth size must be passed at compile time using -DBATCH e.g. -DDEPTH=128
403 *
Sheri Zhangc5b6d882020-06-26 14:46:59 +0100404 * @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 +0100405 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
406 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
407 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
408 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
409 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
410 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
411 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
412 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
413 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
414 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: U32/S32
415 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
416 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
417 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
418 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
419 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
420 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
421 * @param[in] output_stride_w Stride of the output tensor in W dimension (in bytes)
422 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
423 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
424 */
425__kernel void arg_min_max_w(
426 TENSOR4D_DECLARATION(input),
427 TENSOR4D_DECLARATION(output))
428{
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000429 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 +0100430
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000431 __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 +
432 (get_global_id(2) / DEPTH) * input_stride_w;
433 __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(
434 2) % DEPTH) * output_stride_z + (get_global_id(2) / DEPTH) * output_stride_w;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100435
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000436 VEC_TYPE_IN res = CONVERT(VLOAD(VEC_SIZE)(0, (__global DATA_TYPE *)input_addr), VEC_TYPE_IN);
437
438 VEC_TYPE_OUT indx0 = 0;
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100439 for(DATA_TYPE_OUTPUT w = 1; w < BATCH; ++w)
440 {
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000441 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 +0100442
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000443 VEC_TYPE_OUT cond_conv = CONVERT(CONDITION_TO_USE(in, res), VEC_TYPE_OUT);
444 indx0 = select(indx0, w, cond_conv);
445 res = select(res, in, CONDITION_TO_USE(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100446 }
447
448 // Store result
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000449 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 +0100450}
451#endif /* defined(BATCH) && defined(DEPTH) */
Giorgio Arena3d3a01c2021-01-11 15:31:15 +0000452#endif // defined(VEC_SIZE) && defined(DATA_TYPE) && defined(DATA_TYPE_OUTPUT) && defined(DATA_TYPE_SELECT)