blob: c189bc2d475f5ea7378aaea333c68237da7928ae [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +00002 * Copyright (c) 2017-2020, 2023 Arm Limited.
Georgios Pinitasd9769582017-08-03 10:19:40 +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 "ReductionOperation.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010025#include "tests/validation/Helpers.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010026
27#include <algorithm>
28#include <cmath>
29
30namespace arm_compute
31{
32namespace test
33{
34namespace validation
35{
36namespace reference
37{
38namespace
39{
Michalis Spyrou7930db42018-11-22 17:36:28 +000040template <typename T, typename OT>
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +000041OT reduce_operation(const T *ptr, int reduce_elements, ReductionOperation op, int stride, RoundingPolicy policy)
Georgios Pinitasd9769582017-08-03 10:19:40 +010042{
Michalis Spyrou7930db42018-11-22 17:36:28 +000043 using type = typename std::remove_cv<OT>::type;
Usama Arifa4a08ad2019-05-20 12:38:33 +010044 T res;
45 switch(op)
46 {
47 case ReductionOperation::PROD:
48 {
49 res = type(1);
50 }
51 break;
52 case ReductionOperation::MIN:
Usama Arif28f0dd92019-05-20 13:44:34 +010053 case ReductionOperation::MAX:
Usama Arifa4a08ad2019-05-20 12:38:33 +010054 {
55 res = *ptr;
56 }
57 break;
58 default:
59 {
60 res = type(0);
61 }
62 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010063
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010064 if(std::is_integral<type>::value)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010065 {
Luca Foschianid2390532020-02-20 12:19:12 +000066 auto int_res = static_cast<int32_t>(res);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010067 for(int i = 0; i < reduce_elements; ++i)
68 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +000069 auto elem = *(ptr + stride * i);
Michalis Spyrou7930db42018-11-22 17:36:28 +000070
71 switch(op)
72 {
Usama Arifa4a08ad2019-05-20 12:38:33 +010073 case ReductionOperation::MIN:
74 if(static_cast<T>(int_res) > elem)
75 {
76 int_res = elem;
77 }
78 break;
Usama Arif28f0dd92019-05-20 13:44:34 +010079 case ReductionOperation::MAX:
80 if(static_cast<T>(int_res) < elem)
81 {
82 int_res = elem;
83 }
84 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +000085 case ReductionOperation::SUM_SQUARE:
86 int_res += elem * elem;
87 break;
88 case ReductionOperation::MEAN_SUM:
89 case ReductionOperation::SUM:
90 int_res += elem;
91 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000092 case ReductionOperation::PROD:
93 int_res *= elem;
94 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +000095 default:
96 ARM_COMPUTE_ERROR("Operation not supported");
97 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010098 }
99 if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0)
100 {
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000101 // Only use rounding in aarch64 to be consistent with kernel
102#ifdef __aarch64__
103 // Divide in float format, then rounded to nearest and implicitly cast back to int
104 int_res = round(static_cast<float>(int_res) / static_cast<float>(reduce_elements), policy);
105#else // defined(__aarch64__)
106 ARM_COMPUTE_UNUSED(policy);
107 int_res /= reduce_elements; // Legacy compatibility
108#endif // __aarch64
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100109 }
Luca Foschianid2390532020-02-20 12:19:12 +0000110 res = static_cast<type>(int_res);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100111 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100112 else
113 {
114 for(int i = 0; i < reduce_elements; ++i)
115 {
116 auto elem = *(ptr + stride * i);
Michalis Spyrou7930db42018-11-22 17:36:28 +0000117 switch(op)
118 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100119 case ReductionOperation::MIN:
120 if(res > elem)
121 {
122 res = elem;
123 }
124 break;
Usama Arif28f0dd92019-05-20 13:44:34 +0100125 case ReductionOperation::MAX:
126 if(res < elem)
127 {
128 res = elem;
129 }
130 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000131 case ReductionOperation::SUM_SQUARE:
132 res += elem * elem;
133 break;
134 case ReductionOperation::MEAN_SUM:
135 case ReductionOperation::SUM:
136 res += elem;
137 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000138 case ReductionOperation::PROD:
139 res *= elem;
140 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000141 default:
142 ARM_COMPUTE_ERROR("Operation not supported");
143 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100144 }
145 if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0)
146 {
147 res /= reduce_elements;
148 }
149 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100150 return res;
Georgios Pinitasd9769582017-08-03 10:19:40 +0100151}
Manuel Bottini62404a52019-06-10 17:06:39 +0100152
153template <typename T, typename OT>
154OT reduce_operation_arg_min_max(const T *ptr, int reduce_elements, ReductionOperation op, int stride)
155{
156 uint32_t res = 0;
157 for(int i = 0; i < reduce_elements; ++i)
158 {
159 auto elem = *(ptr + stride * i);
160 switch(op)
161 {
162 case ReductionOperation::ARG_IDX_MIN:
163 if(*(ptr + stride * res) > elem)
164 {
165 res = static_cast<uint32_t>(i);
166 }
167 break;
168 case ReductionOperation::ARG_IDX_MAX:
169 if(*(ptr + stride * res) < elem)
170 {
171 res = static_cast<uint32_t>(i);
172 }
173 break;
174 default:
175 ARM_COMPUTE_ERROR("Operation not supported");
176 }
177 }
178 return static_cast<OT>(res);
179}
180
Georgios Pinitasd9769582017-08-03 10:19:40 +0100181} // namespace
182
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000183template <typename T, typename OT>
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100184SimpleTensor<OT> compute_reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
185 DataType output_type, RoundingPolicy policy)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100186{
187 // Create reference
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100188 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
189 SimpleTensor<OT> dst{ dst_shape, output_type, 1, src.quantization_info() };
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100190 const unsigned int src_width = src.shape().x();
191 const unsigned int src_height = src.shape().y();
192 const unsigned int src_depth = src.shape().z();
193 const unsigned int src_batch = src.shape()[3];
194 const int reduce_elems = src.shape()[axis];
Georgios Pinitasd9769582017-08-03 10:19:40 +0100195
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100196 switch(axis)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100197 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100198 case 0:
Georgios Pinitasd9769582017-08-03 10:19:40 +0100199 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100200 const unsigned int upper_dims = src.shape().total_size_upper(1);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100201 for(unsigned int du = 0; du < upper_dims; ++du)
202 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100203 const T *src_row_ptr = src.data() + du * reduce_elems;
Manuel Bottini62404a52019-06-10 17:06:39 +0100204 dst[du] = is_arg_min_max ?
205 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, 1) :
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000206 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, 1, policy);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100207 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100208 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100209 break;
210 case 1:
Georgios Pinitasd9769582017-08-03 10:19:40 +0100211 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100212 const unsigned int upper_dims = src.shape().total_size_upper(2);
213 for(unsigned int du = 0; du < upper_dims; ++du)
214 {
215 for(unsigned int x = 0; x < src_width; ++x)
216 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100217 const int in_offset = du * src_height * src_width + x;
218 const int out_offset = du * src_width + x;
219 const T *src_row_ptr = src.data() + in_offset;
Manuel Bottini62404a52019-06-10 17:06:39 +0100220 dst[out_offset] = is_arg_min_max ?
221 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width) :
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000222 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width, policy);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100223 }
224 }
225 }
226 break;
227 case 2:
228 {
229 const unsigned int upper_dims = src.shape().total_size_upper(3);
230 for(unsigned int du = 0; du < upper_dims; ++du)
231 {
232 for(unsigned int x = 0; x < src_width; ++x)
233 {
234 for(unsigned int y = 0; y < src_height; ++y)
235 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100236 const int in_offset = du * src_depth * src_height * src_width + y * src_width + x;
237 const int out_offset = du * src_width * src_height + y * src_width + x;
238 const T *src_row_ptr = src.data() + in_offset;
Manuel Bottini62404a52019-06-10 17:06:39 +0100239 dst[out_offset] = is_arg_min_max ?
240 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height) :
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000241 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height, policy);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100242 }
243 }
244 }
245 }
246 break;
247 case 3:
248 {
249 const unsigned int upper_dims = src.shape().total_size_upper(4);
250 for(unsigned int du = 0; du < upper_dims; ++du)
251 {
252 for(unsigned int z = 0; z < src_depth; ++z)
253 {
254 for(unsigned int y = 0; y < src_height; ++y)
255 {
256 for(unsigned int x = 0; x < src_width; ++x)
257 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100258 const int in_offset = du * src_batch * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x;
259 const int out_offset = du * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x;
260 const T *src_row_ptr = src.data() + in_offset;
Manuel Bottini62404a52019-06-10 17:06:39 +0100261 dst[out_offset] = is_arg_min_max ?
262 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth) :
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000263 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth, policy);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100264 }
265 }
266 }
267 }
268 }
269 break;
270 default:
Georgios Pinitasd9769582017-08-03 10:19:40 +0100271 ARM_COMPUTE_ERROR("Unsupported reduction axis");
Georgios Pinitasd9769582017-08-03 10:19:40 +0100272 }
273
274 return dst;
275}
276
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000277template <typename T, typename OT>
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100278SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
279 DataType output_type, QuantizationInfo quantization_info_output, RoundingPolicy policy)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000280{
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100281 ARM_COMPUTE_UNUSED(quantization_info_output);
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100282 return compute_reduction_operation<T, OT>(src, dst_shape, axis, op, output_type, policy);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000283}
284
285template <>
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100286SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
287 DataType output_type, QuantizationInfo quantization_info_output, RoundingPolicy policy)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000288{
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000289 if(src.data_type() == DataType::QASYMM8)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000290 {
Luca Foschianid2390532020-02-20 12:19:12 +0000291 // If the operation is MEAN_SUM, we can directly use the uint8 implementation without taking into account scale and offset
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100292 if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
Luca Foschianid2390532020-02-20 12:19:12 +0000293 {
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100294 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, output_type, policy);
Luca Foschianid2390532020-02-20 12:19:12 +0000295 }
296 else
297 {
298 SimpleTensor<float> src_f = convert_from_asymmetric(src);
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100299 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op, output_type);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100300 return convert_to_asymmetric<uint8_t>(dst_f, quantization_info_output);
Luca Foschianid2390532020-02-20 12:19:12 +0000301 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000302 }
303 else
304 {
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100305 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, output_type, policy);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000306 }
307}
308
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000309template <>
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100310SimpleTensor<int8_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis,
311 ReductionOperation op, DataType output_type, QuantizationInfo quantization_info_output, RoundingPolicy policy)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000312{
313 if(src.data_type() == DataType::QASYMM8_SIGNED)
314 {
Luca Foschianid2390532020-02-20 12:19:12 +0000315 // If the operation is MEAN_SUM, we can directly use the int8 implementation without taking into account scale and offset
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100316 if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
Luca Foschianid2390532020-02-20 12:19:12 +0000317 {
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100318 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, output_type, policy);
Luca Foschianid2390532020-02-20 12:19:12 +0000319 }
320 else
321 {
322 SimpleTensor<float> src_f = convert_from_asymmetric(src);
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100323 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op, output_type);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100324 return convert_to_asymmetric<int8_t>(dst_f, quantization_info_output);
Luca Foschianid2390532020-02-20 12:19:12 +0000325 }
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000326 }
327 else
328 {
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100329 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, output_type, policy);
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000330 }
331}
332
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100333template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100334 DataType output_type = DataType::S32, QuantizationInfo quantization_info_output = QuantizationInfo(),
335 RoundingPolicy policy = RoundingPolicy::TO_ZERO);
336
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100337template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100338 DataType output_type = DataType::S32,
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000339 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000340
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100341template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100342 DataType output_type = DataType::S32,
Mohammed Suhail Munshiecaa10a2023-02-09 11:52:06 +0000343 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000344
Pablo Marquez Tello4cb0bd42023-07-27 18:02:37 +0100345template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int32_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
346 DataType output_type = DataType::S32,
347 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
348template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
349 DataType output_type = DataType::S32,
350 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
351template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
352 DataType output_type = DataType::S32,
353 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
354template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
355 DataType output_type = DataType::S32,
356 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
357
358template SimpleTensor<int64_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
359 DataType output_type = DataType::S32, QuantizationInfo quantization_info_output = QuantizationInfo(),
360 RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100361} // namespace reference
362} // namespace validation
363} // namespace test
364} // namespace arm_compute