blob: a5fd0b36221af98071823dcbd06af9644a619efe [file] [log] [blame]
Michalis Spyrou04f089c2017-08-08 17:42:38 +01001/*
Michalis Spyrou6c89ffa2020-01-24 12:05:05 +00002 * 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 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100170 * @param[in] src_ptr Pointer to the source tensor. Supported data types: S32/F16/F32 and QASYMM8 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
174 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
175 * @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)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000199 res = select(res, in, CONVERT(ISGREATER(in, res), COND_DATA_TYPE));
200#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
Usama Arif048b0f32019-05-22 16:32:27 +0100226#if defined(MIN) || defined(MAX)
Usama Arifb2890502019-05-21 11:48:37 +0100227 *((__global DATA_TYPE_PROMOTED *)output.ptr) = res;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000228#else // !(defined(MIN) || defined(MAX))
229 *((__global DATA_TYPE *)output.ptr) = CONVERT_SAT(res, DATA_TYPE);
Usama Arif048b0f32019-05-22 16:32:27 +0100230#endif // defined(MIN) || defined(MAX)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100231}
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100232#endif // defined(WIDTH)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100233
234#if defined(HEIGHT)
235/** This kernel performs reduction on y-axis.
236 *
Michalis Spyrou7930db42018-11-22 17:36:28 +0000237 * @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 +0100238 * @note The height size must be passed at compile time using -DHEIGHT e.g. -DHEIGHT=128
239 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100240 * @param[in] src_ptr Pointer to the source tensor. Supported data types: QASYMM8/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100241 * @param[in] src_stride_x Stride of the source tensor in X dimension (in bytes)
242 * @param[in] src_step_x src_stride_x * number of elements along X processed per workitem(in bytes)
243 * @param[in] src_stride_y Stride of the source tensor in Y dimension (in bytes)
244 * @param[in] src_step_y src_stride_y * number of elements along Y processed per workitem(in bytes)
245 * @param[in] src_offset_first_element_in_bytes The offset of the first element in the source tensor
246 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p src_ptt
247 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
248 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
249 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
250 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
251 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
252 */
253__kernel void reduction_operation_y(
254 IMAGE_DECLARATION(src),
255 IMAGE_DECLARATION(output))
256{
257 Image src = CONVERT_TO_IMAGE_STRUCT(src);
258 Image output = CONVERT_TO_IMAGE_STRUCT(output);
259
260 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000261 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 +0100262
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000263 // Convert input into F32 in order to perform quantized multiplication
264#if defined(PROD) && defined(OFFSET) && defined(SCALE)
265 float16 res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
266#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
267
Michalis Spyrou7930db42018-11-22 17:36:28 +0000268#if defined(SUM_SQUARE)
269 res *= res;
270#endif // defined(SUM_SQUARE)
271
Michalis Spyrou7930db42018-11-22 17:36:28 +0000272 for(unsigned int y = 1; y < HEIGHT; ++y)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100273 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100274 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
275 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 +0100276#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100277 res = select(res, in, ISLESS(in, res));
278#elif defined(MAX)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000279 res = select(res, in, ISGREATER(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100280#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100281#if defined(SUM_SQUARE)
282 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000283#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000284#if defined(PROD)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000285
286#if defined(OFFSET) && defined(SCALE)
287 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
288#else // !(defined(OFFSET) && defined(SCALE))
Manuel Bottinib412fab2018-12-10 17:40:23 +0000289 res *= in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000290#endif // defined(OFFSET) && defined(SCALE)
291
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100292#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100293 res += in;
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100294#endif // defined(PROD)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100295#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100296 }
297
298#if defined(MEAN)
299 res /= HEIGHT;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000300#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000301
302 // Subtract the offsets in case of quantized SUM
303#if defined(SUM) && defined(OFFSET) && defined(SCALE)
304 res -= (HEIGHT - 1) * OFFSET;
305#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
306
307 // Re-quantize
308#if defined(PROD) && defined(OFFSET) && defined(SCALE)
309 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
310#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
311
312 // Store result
313 vstore16(CONVERT_SAT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100314}
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100315#endif // defined(HEIGHT)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100316
317#if defined(DEPTH)
318/** This kernel performs reduction on z-axis.
319 *
320 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
321 * @note The depth size must be passed at compile time using -DDEPTH e.g. -DDEPTH=128
322 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100323 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100324 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
325 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
326 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
327 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
328 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
329 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
330 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
331 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptt
332 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
333 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
334 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
335 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
336 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
337 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
338 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
339 */
340__kernel void reduction_operation_z(
341 TENSOR3D_DECLARATION(input),
342 TENSOR3D_DECLARATION(output))
343{
344 Tensor3D input = CONVERT_TO_TENSOR3D_STRUCT(input);
345 Tensor3D output = CONVERT_TO_TENSOR3D_STRUCT(output);
346
347 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000348 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 +0100349
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000350 // Convert input into F32 in order to perform quantized multiplication
351#if defined(PROD) && defined(OFFSET) && defined(SCALE)
352 float16 res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
353#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
354
Georgios Pinitas8be91482019-03-26 17:23:28 +0000355#if defined(COMPLEX)
356 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
357 res1 = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 8, 0, 0)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
358#endif // defined(COMPLEX)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000359#if defined(SUM_SQUARE)
360 res *= res;
361#endif // defined(SUM_SQUARE)
362
Michalis Spyrou7930db42018-11-22 17:36:28 +0000363 for(unsigned int z = 1; z < DEPTH; ++z)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100364 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100365 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
366 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 +0000367
Georgios Pinitas8be91482019-03-26 17:23:28 +0000368#if defined(COMPLEX)
369 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
370 in1 = CONVERT(vload16(0, (__global DATA_TYPE *)tensor3D_offset(&input, 8, 0, z)), VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16));
371#endif // defined(COMPLEX)
372
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100373#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100374 res = select(res, in, ISLESS(in, res));
375#elif defined(MAX)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000376 res = select(res, in, ISGREATER(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100377#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100378#if defined(SUM_SQUARE)
379 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000380#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000381#if defined(PROD)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000382
383#if defined(OFFSET) && defined(SCALE)
384 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
385#else // !(defined(OFFSET) && defined(SCALE))
Manuel Bottinib412fab2018-12-10 17:40:23 +0000386 res *= in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000387#endif // defined(OFFSET) && defined(SCALE)
388
389#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100390 res += in;
Georgios Pinitas8be91482019-03-26 17:23:28 +0000391#if defined(COMPLEX)
392 res1 += in1;
393#endif // defined(COMPLEX)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100394#endif // defined(PROD)
395#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100396 }
397
398#if defined(MEAN)
399 res /= DEPTH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000400#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000401
402 // Subtract the offsets in case of quantized SUM
403#if defined(SUM) && defined(OFFSET) && defined(SCALE)
404 res -= (DEPTH - 1) * OFFSET;
405#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
406
407 // Re-quantize
408#if defined(PROD) && defined(OFFSET) && defined(SCALE)
409 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
410#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
411
412 // Store result
413 vstore16(CONVERT_SAT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Georgios Pinitas8be91482019-03-26 17:23:28 +0000414#if defined(COMPLEX)
415 vstore16(CONVERT(res1, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)tensor3D_offset(&output, 8, 0, 0));
416#endif // defined(COMPLEX)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100417}
418#endif /* defined(DEPTH) */
419
420#if defined(BATCH) && defined(DEPTH)
421/** This kernel performs reduction on w-axis.
422 *
423 * @note The data type must be passed at compile time using -DDATA_TYPE: e.g. -DDATA_TYPE=float
424 * @note The batch size must be passed at compile time using -DBATCH e.g. -DBATCH=128
Manuel Bottini34f88dd2019-10-18 10:37:46 +0000425 * @note The depth size must be passed at compile time using -DBATCH e.g. -DDEPTH=128
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100426 *
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100427 * @param[in] input_ptr Pointer to the source tensor. Supported data types: QASYMM8/S32/F16/F32
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100428 * @param[in] input_stride_x Stride of the source tensor in X dimension (in bytes)
429 * @param[in] input_step_x input_stride_x * number of elements along X processed per workitem(in bytes)
430 * @param[in] input_stride_y Stride of the source tensor in Y dimension (in bytes)
431 * @param[in] input_step_y input_stride_y * number of elements along Y processed per workitem(in bytes)
432 * @param[in] input_stride_z Stride of the source tensor in Z dimension (in bytes)
433 * @param[in] input_step_z input_stride_z * number of elements along Z processed per workitem(in bytes)
434 * @param[in] input_stride_w Stride of the source tensor in W dimension (in bytes)
435 * @param[in] input_step_w input_stride_w * number of elements along W processed per workitem(in bytes)
436 * @param[in] input_offset_first_element_in_bytes The offset of the first element in the source tensor
437 * @param[in] output_ptr The local buffer to hold sumed values. Supported data types: same as @p input_ptt
438 * @param[in] output_stride_x Stride of the output tensor in X dimension (in bytes)
439 * @param[in] output_step_x output_stride_x * number of elements along X processed per workitem(in bytes)
440 * @param[in] output_stride_y Stride of the output tensor in Y dimension (in bytes)
441 * @param[in] output_step_y output_stride_y * number of elements along Y processed per workitem(in bytes)
442 * @param[in] output_stride_z Stride of the output tensor in Z dimension (in bytes)
443 * @param[in] output_step_z output_stride_z * number of elements along Z processed per workitem(in bytes)
444 * @param[in] output_stride_w Stride of the output tensor in W dimension (in bytes)
445 * @param[in] output_step_w output_stride_w * number of elements along W processed per workitem(in bytes)
446 * @param[in] output_offset_first_element_in_bytes The offset of the first element in the source tensor
447 */
448__kernel void reduction_operation_w(
449 TENSOR4D_DECLARATION(input),
450 TENSOR4D_DECLARATION(output))
451{
452 Tensor4D input = CONVERT_TO_TENSOR4D_STRUCT(input, DEPTH);
453 Tensor4D output = CONVERT_TO_TENSOR4D_STRUCT(output, DEPTH);
454
455 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
Michalis Spyrou7930db42018-11-22 17:36:28 +0000456 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 +0100457
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000458 // Convert input into F32 in order to perform quantized multiplication
459#if defined(PROD) && defined(OFFSET) && defined(SCALE)
460 float16 res_f = DEQUANTIZE(res, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
461#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
462
Michalis Spyrou7930db42018-11-22 17:36:28 +0000463#if defined(SUM_SQUARE)
464 res *= res;
465#endif // defined(SUM_SQUARE)
466
Michalis Spyrou7930db42018-11-22 17:36:28 +0000467 for(unsigned int w = 1; w < BATCH; ++w)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100468 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100469 VEC_DATA_TYPE(DATA_TYPE_PROMOTED, 16)
470 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 +0000471
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100472#if defined(MIN)
Usama Arif048b0f32019-05-22 16:32:27 +0100473 res = select(res, in, ISLESS(in, res));
474#elif defined(MAX)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000475 res = select(res, in, ISGREATER(in, res));
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100476#else // !(defined(MAX) || defined(MIN))
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100477#if defined(SUM_SQUARE)
478 in *= in;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000479#endif // defined(SUM_SQUARE)
Manuel Bottinib412fab2018-12-10 17:40:23 +0000480#if defined(PROD)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000481
482#if defined(OFFSET) && defined(SCALE)
483 res_f *= DEQUANTIZE(in, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
484#else // !(defined(OFFSET) && defined(SCALE))
Manuel Bottinib412fab2018-12-10 17:40:23 +0000485 res *= in;
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000486#endif // defined(OFFSET) && defined(SCALE)
487
488#else // !defined(PROD)
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100489 res += in;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000490#endif //defined(PROD)
Manuel Bottini7b9998d2019-10-21 17:59:07 +0100491#endif // defined(MAX) || defined(MIN)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100492 }
493
494#if defined(MEAN)
495 res /= BATCH;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000496#endif // defined(MEAN)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000497
498 // Subtract the offsets in case of quantized SUM
499#if defined(SUM) && defined(OFFSET) && defined(SCALE)
500 res -= (BATCH - 1) * OFFSET;
501#endif // defined(OFFSET) && defined(OFFSET) && defined(SCALE)
502
503 // Re-quantize
504#if defined(PROD) && defined(OFFSET) && defined(SCALE)
505 res = QUANTIZE(res_f, OFFSET, SCALE, DATA_TYPE_PROMOTED, 16);
506#endif // defined(PROD) && defined(OFFSET) && defined(SCALE)
507
508 // Store result
509 vstore16(CONVERT_SAT(res, VEC_DATA_TYPE(DATA_TYPE, 16)), 0, (__global DATA_TYPE *)output.ptr);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100510}
Manuel Bottinib412fab2018-12-10 17:40:23 +0000511#endif /* defined(BATCH) && defined(DEPTH) */