blob: b2e56928d07e231a5996eb281ec6b333906edb03 [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Michalis Spyrou04f089c2017-08-08 17:42:38 +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"
Michalis Spyrou0b18d972020-01-30 18:11:13 +000025#include "helpers_asymm.h"
Michalis Spyrou04f089c2017-08-08 17:42:38 +010026
Michalis Spyrou7317e392020-01-17 11:27:49 +000027#if defined(FLOAT_DATA_TYPE)
28#define ISGREATER(x, y) isgreater(x, y)
29#define ISLESS(x, y) isless(x, y)
30#else // !FLOAT_DATA_TYPE
31#if defined(WIDTH)
32#define ISGREATER(x, y) (x > y) ? 1 : 0
33#define ISLESS(x, y) (x < y) ? 1 : 0
34#else // !defined(WIDTH)
Michalis Spyrou6c89ffa2020-01-24 12:05:05 +000035#define ISGREATER(x, y) select((int16)0, (int16)-1, x > y)
36#define ISLESS(x, y) select((int16)0, (int16)-1, x < y)
Michalis Spyrou7317e392020-01-17 11:27:49 +000037#endif // defined(WIDTH)
38#endif // defined(FLOAT_DATA_TYPE)
39
Michalis Spyrou04f089c2017-08-08 17:42:38 +010040/** Calculate square sum of a vector
41 *
42 * @param[in] input Pointer to the first pixel.
43 *
44 * @return square sum of vector.
45 */
46inline DATA_TYPE square_sum(__global const DATA_TYPE *input)
47{
48 VEC_DATA_TYPE(DATA_TYPE, 16)
49 in = vload16(0, input);
50
51 in *= in;
52
53 in.s01234567 += in.s89ABCDEF;
54 in.s0123 += in.s4567;
55 in.s01 += in.s23;
56
57 return (in.s0 + in.s1);
58}
59
60/** Calculate sum of a vector
61 *
62 * @param[in] input Pointer to the first pixel.
63 *
64 * @return sum of vector.
65 */
66inline DATA_TYPE sum(__global const DATA_TYPE *input)
67{
68 VEC_DATA_TYPE(DATA_TYPE, 16)
69 in = vload16(0, input);
70
71 in.s01234567 += in.s89ABCDEF;
72 in.s0123 += in.s4567;
73 in.s01 += in.s23;
74
75 return (in.s0 + in.s1);
76}
Manuel Bottinib412fab2018-12-10 17:40:23 +000077
78/** Calculate product of a vector
79 *
80 * @param[in] input Pointer to the first pixel.
81 *
82 * @return product of vector.
83 */
84inline DATA_TYPE product(__global const DATA_TYPE *input)
85{
86 VEC_DATA_TYPE(DATA_TYPE, 16)
87 in = vload16(0, input);
88
89 in.s01234567 *= in.s89ABCDEF;
90 in.s0123 *= in.s4567;
91 in.s01 *= in.s23;
92
93 return (in.s0 * in.s1);
94}
Manuel Bottini34f88dd2019-10-18 10:37:46 +000095#if defined(OPERATION)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010096/** This kernel performs parallel reduction given an operation on x-axis.
Michalis Spyrou04f089c2017-08-08 17:42:38 +010097 *
98 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou04f089c2017-08-08 17:42:38 +010099 * @note The operation we want to perform must be passed at compile time using -DOPERATION e.g. -DOPERATION=square_sum
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100100 * @note The mean flag must be passed at compile time using -DMEAN if we want to compute the mean value
Manuel Bottinib412fab2018-12-10 17:40:23 +0000101 * @note The product flag must be passed at compile time using -DPROD if we want to compute the product, otherwise sum will be used
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100102 * @note The width size must be passed at compile time using -DWIDTH e.g. -DWIDTH=128 if we want to compute the mean value
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100103 *
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100104 * @param[in] src_ptr Pointer to the source tensor. Supported data types: F16/F32
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100105 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
106 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000107 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
108 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100109 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Manuel Bottinib412fab2018-12-10 17:40:23 +0000110 * @param[in] partial_res_ptr The local buffer to hold partial result values. Supported data types: same as @p src_ptr
111 * @param[in] partial_res_stride_x Stride of the output tensor in X dimension (in bytes)
112 * @param[in] partial_res_step_x partial_res_stride_x * number of elements along X processed per workitem(in bytes)
113 * @param[in] partial_res_stride_y Stride of the output tensor in Y dimension (in bytes)
114 * @param[in] partial_res_step_y partial_res_stride_y * number of elements along Y processed per workitem(in bytes)
115 * @param[in] partial_res_offset_first_element_in_bytes The offset of the first element in the source tensor
116 * @param[in] local_results Local buffer for storing the partial result
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100117 */
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100118__kernel void reduction_operation_x(
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000119 IMAGE_DECLARATION(src),
Manuel Bottinib412fab2018-12-10 17:40:23 +0000120 IMAGE_DECLARATION(partial_res),
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000121 __local DATA_TYPE *local_results)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100122{
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000123 Image src = CONVERT_TO_IMAGE_STRUCT(src);
Manuel Bottinib412fab2018-12-10 17:40:23 +0000124 Image partial_res = CONVERT_TO_IMAGE_STRUCT(partial_res);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100125
126 unsigned int lsize = get_local_size(0);
127 unsigned int lid = get_local_id(0);
128
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000129 for(unsigned int y = 0; y < get_local_size(1); ++y)
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100130 {
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000131 local_results[lid] = OPERATION((__global DATA_TYPE *)offset(&src, 0, y));
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100132 barrier(CLK_LOCAL_MEM_FENCE);
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100133
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000134 // Perform parallel reduction
135 for(unsigned int i = lsize >> 1; i > 0; i >>= 1)
136 {
137 if(lid < i)
138 {
Manuel Bottinib412fab2018-12-10 17:40:23 +0000139#if defined(PROD)
140 local_results[lid] *= local_results[lid + i];
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000141#else // !defined(PROD)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000142 local_results[lid] += local_results[lid + i];
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000143#endif // defined(PROD)
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000144 }
145 barrier(CLK_LOCAL_MEM_FENCE);
146 }
147
148 if(lid == 0)
149 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100150#if defined(MEAN) && defined(WIDTH)
151 if(y == get_local_size(1) - 1)
152 {
Manuel Bottinib412fab2018-12-10 17:40:23 +0000153 local_results[0] /= WIDTH;
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100154 }
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100155#endif // defined(MEAN) && defined(WIDTH)
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000156 ((__global DATA_TYPE *)offset(&partial_res, get_group_id(0), y))[0] = local_results[0];
Michalis Spyrouf6402dd2018-01-26 15:06:19 +0000157 }
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100158 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100159}
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000160#endif // defined(OPERATION)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100161
162#if defined(WIDTH)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000163/** This kernel performs reduction on x-axis. (Non parallel)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100164 *
Michalis Spyrou7930db42018-11-22 17:36:28 +0000165 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100166 * @note The width size must be passed at compile time using -DWIDTH e.g. -DWIDTH=128
Manuel Bottinib412fab2018-12-10 17:40:23 +0000167 * @note The product flag must be passed at compile time using -DPROD if we want to compute the product, otherwise sum will be used
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100168 * @note In case of MIN and MAX the condition data type must be passed at compile time using -DCOND_DATA_TYPE e.g. -DCOND_DATA_TYPE=short
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100169 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100170 * @param[in] src_ptr Pointer to the source tensor. Supported data types: S32/F16/F32 and QASYMM8/QASYMM8_SIGNED for operation MEAN
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100171 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
172 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
173 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100174 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptr
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100175 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
176 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
177 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
178 */
Michalis Spyrou7930db42018-11-22 17:36:28 +0000179__kernel void reduction_operation_non_parallel_x(
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100180 VECTOR_DECLARATION(src),
181 VECTOR_DECLARATION(output))
182{
183 Vector src = CONVERT_TO_VECTOR_STRUCT(src);
184 Vector output = CONVERT_TO_VECTOR_STRUCT(output);
185
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000186 DATA_TYPE_PROMOTED res = CONVERT(*((__global DATA_TYPE *)vector_offset(&src, 0)), DATA_TYPE_PROMOTED);
187
188 // Convert input into F32 in order to perform quantized multiplication
189#if defined(PROD) && defined(OFFSET) && defined(SCALE)
190 float res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 1);
191#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100192
Michalis Spyrou7930db42018-11-22 17:36:28 +0000193 for(unsigned int x = 1; x < WIDTH; ++x)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100194 {
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000195 DATA_TYPE_PROMOTED in = CONVERT(*((__global DATA_TYPE *)vector_offset(&src, x)), DATA_TYPE_PROMOTED);
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100196#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100197 res = select(res, in, CONVERT(ISLESS(in, res), COND_DATA_TYPE));
198#elif defined(MAX)
Michele Di Giorgio03337042020-02-26 17:47:55 +0000199 res = select(res, in, CONVERT(ISGREATER(in, res), COND_DATA_TYPE));
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000200#elif defined(PROD)
201#if defined(OFFSET) && defined(SCALE)
202 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 1);
203#else // !(defined(OFFSET) && defined(SCALE))
204 res *= in;
205#endif // defined(OFFSET) && defined(SCALE)
206#else // defined(SUM))
Michalis Spyrou7930db42018-11-22 17:36:28 +0000207 res += in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000208#endif // defined(MAX) || defined(MIN) || defined(PROD)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100209 }
210
Michalis Spyrou7930db42018-11-22 17:36:28 +0000211 // Store result
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100212#if defined(MEAN)
213 res /= WIDTH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000214#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000215
216 // Subtract the offsets in case of quantized SUM
217#if defined(SUM) && defined(OFFSET) && defined(SCALE)
218 res -= (WIDTH - 1) * OFFSET;
219#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
220
221 // Re-quantize
222#if defined(PROD) && defined(OFFSET) && defined(SCALE)
223 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 1);
224#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
225
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000226 *((__global DATA_TYPE *)output.ptr) = CONVERT_SAT(res, DATA_TYPE);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100227}
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100228#endif // defined(WIDTH)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100229
230#if defined(HEIGHT)
231/** This kernel performs reduction on y-axis.
232 *
Michalis Spyrou7930db42018-11-22 17:36:28 +0000233 * @note The input data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100234 * @note The height size must be passed at compile time using -DHEIGHT e.g. -DHEIGHT=128
235 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100236 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100237 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
238 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
239 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
240 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
241 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100242 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptr
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100243 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
244 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
245 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
246 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
247 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
248 */
249__kernel void reduction_operation_y(
250 IMAGE_DECLARATION(src),
251 IMAGE_DECLARATION(output))
252{
253 Image src = CONVERT_TO_IMAGE_STRUCT(src);
254 Image output = CONVERT_TO_IMAGE_STRUCT(output);
255
256 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000257 res = CONVERT(vload16(0, (__global DATA_TYPE *)offset(&src, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100258
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000259 // Convert input into F32 in order to perform quantized multiplication
260#if defined(PROD) && defined(OFFSET) && defined(SCALE)
261 float16 res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
262#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
263
Michalis Spyrou7930db42018-11-22 17:36:28 +0000264#if defined(SUM_SQUARE)
265 res *= res;
266#endif // defined(SUM_SQUARE)
267
Michalis Spyrou7930db42018-11-22 17:36:28 +0000268 for(unsigned int y = 1; y < HEIGHT; ++y)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100269 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100270 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
271 in = CONVERT(vload16(0, (__global DATA_TYPE *)offset(&src, 0, y)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100272#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100273 res = select(res, in, ISLESS(in, res));
274#elif defined(MAX)
Michele Di Giorgio03337042020-02-26 17:47:55 +0000275 res = select(res, in, ISGREATER(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100276#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100277#if defined(SUM_SQUARE)
278 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000279#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000280#if defined(PROD)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000281
282#if defined(OFFSET) && defined(SCALE)
283 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
284#else // !(defined(OFFSET) && defined(SCALE))
Manuel Bottinib412fab2018-12-10 17:40:23 +0000285 res *= in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000286#endif // defined(OFFSET) && defined(SCALE)
287
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100288#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100289 res += in;
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100290#endif // defined(PROD)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100291#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100292 }
293
294#if defined(MEAN)
295 res /= HEIGHT;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000296#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000297
298 // Subtract the offsets in case of quantized SUM
299#if defined(SUM) && defined(OFFSET) && defined(SCALE)
300 res -= (HEIGHT - 1) * OFFSET;
301#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
302
303 // Re-quantize
304#if defined(PROD) && defined(OFFSET) && defined(SCALE)
305 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
306#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
307
308 // Store result
309 vstore16(CONVERT_SAT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100310}
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100311#endif // defined(HEIGHT)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100312
313#if defined(DEPTH)
314/** This kernel performs reduction on z-axis.
315 *
316 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
317 * @note The depth size must be passed at compile time using -DDEPTH e.g. -DDEPTH=128
318 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100319 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100320 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
321 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
322 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
323 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
324 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
325 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
326 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100327 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptr
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100328 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
329 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
330 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
331 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
332 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
333 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
334 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
335 */
336__kernel void reduction_operation_z(
337 TENSOR3D_DECLARATION(input),
338 TENSOR3D_DECLARATION(output))
339{
340 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
341 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
342
343 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000344 res = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100345
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000346 // Convert input into F32 in order to perform quantized multiplication
347#if defined(PROD) && defined(OFFSET) && defined(SCALE)
348 float16 res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
349#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
350
Georgios Pinitas8be91482019-03-26 17:23:28 +0000351#if defined(COMPLEX)
352 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
353 res1 = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 8, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
354#endif // defined(COMPLEX)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000355#if defined(SUM_SQUARE)
356 res *= res;
357#endif // defined(SUM_SQUARE)
358
Michalis Spyrou7930db42018-11-22 17:36:28 +0000359 for(unsigned int z = 1; z < DEPTH; ++z)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100360 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100361 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
362 in = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 0, 0, z)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7930db42018-11-22 17:36:28 +0000363
Georgios Pinitas8be91482019-03-26 17:23:28 +0000364#if defined(COMPLEX)
365 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
366 in1 = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 8, 0, z)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
367#endif // defined(COMPLEX)
368
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100369#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100370 res = select(res, in, ISLESS(in, res));
371#elif defined(MAX)
Michele Di Giorgio03337042020-02-26 17:47:55 +0000372 res = select(res, in, ISGREATER(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100373#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100374#if defined(SUM_SQUARE)
375 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000376#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000377#if defined(PROD)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000378
379#if defined(OFFSET) && defined(SCALE)
380 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
381#else // !(defined(OFFSET) && defined(SCALE))
Manuel Bottinib412fab2018-12-10 17:40:23 +0000382 res *= in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000383#endif // defined(OFFSET) && defined(SCALE)
384
385#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100386 res += in;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000387#if defined(COMPLEX)
388 res1 += in1;
389#endif // defined(COMPLEX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100390#endif // defined(PROD)
391#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100392 }
393
394#if defined(MEAN)
395 res /= DEPTH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000396#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000397
398 // Subtract the offsets in case of quantized SUM
399#if defined(SUM) && defined(OFFSET) && defined(SCALE)
400 res -= (DEPTH - 1) * OFFSET;
401#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
402
403 // Re-quantize
404#if defined(PROD) && defined(OFFSET) && defined(SCALE)
405 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
406#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
407
408 // Store result
409 vstore16(CONVERT_SAT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000410#if defined(COMPLEX)
411 vstore16(CONVERT(res1, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)tensor3D_offset(&output, 8, 0, 0));
412#endif // defined(COMPLEX)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100413}
414#endif /* defined(DEPTH) */
415
416#if defined(BATCH) && defined(DEPTH)
417/** This kernel performs reduction on w-axis.
418 *
419 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
420 * @note The batch size must be passed at compile time using -DBATCH e.g. -DBATCH=128
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000421 * @note The depth size must be passed at compile time using -DBATCH e.g. -DDEPTH=128
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100422 *
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100423 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/QASYMM8_SIGNED/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100424 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
425 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
426 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
427 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
428 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
429 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
430 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
431 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
432 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
Michele Di Giorgiof6f78762020-07-06 11:27:21 +0100433 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptr
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100434 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
435 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
436 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
437 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
438 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
439 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
440 * @param[in] output_stride_w Stride of the output tensor in W dimension (in bytes)
441 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
442 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
443 */
444__kernel void reduction_operation_w(
445 TENSOR4D_DECLARATION(input),
446 TENSOR4D_DECLARATION(output))
447{
448 Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT(input, DEPTH);
449 Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DEPTH);
450
451 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000452 res = CONVERT(vload16(0, (__global DATA_TYPE *)tensor4D_offset(&input, 0, 0, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100453
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000454 // Convert input into F32 in order to perform quantized multiplication
455#if defined(PROD) && defined(OFFSET) && defined(SCALE)
456 float16 res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
457#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
458
Michalis Spyrou7930db42018-11-22 17:36:28 +0000459#if defined(SUM_SQUARE)
460 res *= res;
461#endif // defined(SUM_SQUARE)
462
Michalis Spyrou7930db42018-11-22 17:36:28 +0000463 for(unsigned int w = 1; w < BATCH; ++w)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100464 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100465 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
466 in = CONVERT(vload16(0, (__global DATA_TYPE *)tensor4D_offset(&input, 0, 0, 0, w)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
Michalis Spyrou7930db42018-11-22 17:36:28 +0000467
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100468#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100469 res = select(res, in, ISLESS(in, res));
470#elif defined(MAX)
Michele Di Giorgio03337042020-02-26 17:47:55 +0000471 res = select(res, in, ISGREATER(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100472#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100473#if defined(SUM_SQUARE)
474 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000475#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000476#if defined(PROD)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000477
478#if defined(OFFSET) && defined(SCALE)
479 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
480#else // !(defined(OFFSET) && defined(SCALE))
Manuel Bottinib412fab2018-12-10 17:40:23 +0000481 res *= in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000482#endif // defined(OFFSET) && defined(SCALE)
483
484#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100485 res += in;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000486#endif //defined(PROD)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100487#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100488 }
489
490#if defined(MEAN)
491 res /= BATCH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000492#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000493
494 // Subtract the offsets in case of quantized SUM
495#if defined(SUM) && defined(OFFSET) && defined(SCALE)
496 res -= (BATCH - 1) * OFFSET;
497#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
498
499 // Re-quantize
500#if defined(PROD) && defined(OFFSET) && defined(SCALE)
501 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
502#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
503
504 // Store result
505 vstore16(CONVERT_SAT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100506}
Manuel Bottinib412fab2018-12-10 17:40:23 +0000507#endif /* defined(BATCH) && defined(DEPTH) */