blob: e2890afb9f2f491aeff753e6c4089d1e78316ccc [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Mohammed Suhail Munshi470cc5d2023-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 Munshi470cc5d2023-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 Munshi470cc5d2023-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>
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000184SimpleTensor<OT> compute_reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, RoundingPolicy policy)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100185{
186 // Create reference
Michalis Spyrou7930db42018-11-22 17:36:28 +0000187 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +0000188 DataType output_data_type = is_arg_min_max ? DataType::S32 : src.data_type();
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000189 SimpleTensor<OT> dst{ dst_shape, output_data_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 Munshi470cc5d2023-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 Munshi470cc5d2023-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 Munshi470cc5d2023-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 Munshi470cc5d2023-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>
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000278SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000279{
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100280 ARM_COMPUTE_UNUSED(quantization_info_output);
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000281 return compute_reduction_operation<T, OT>(src, dst_shape, axis, op, policy);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000282}
283
284template <>
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000285SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000286{
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000287 if(src.data_type() == DataType::QASYMM8)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000288 {
Luca Foschianid2390532020-02-20 12:19:12 +0000289 // 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 +0100290 if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
Luca Foschianid2390532020-02-20 12:19:12 +0000291 {
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000292 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, policy);
Luca Foschianid2390532020-02-20 12:19:12 +0000293 }
294 else
295 {
296 SimpleTensor<float> src_f = convert_from_asymmetric(src);
297 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100298 return convert_to_asymmetric<uint8_t>(dst_f, quantization_info_output);
Luca Foschianid2390532020-02-20 12:19:12 +0000299 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000300 }
301 else
302 {
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000303 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, policy);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000304 }
305}
306
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000307template <>
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000308SimpleTensor<int8_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000309{
310 if(src.data_type() == DataType::QASYMM8_SIGNED)
311 {
Luca Foschianid2390532020-02-20 12:19:12 +0000312 // 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 +0100313 if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
Luca Foschianid2390532020-02-20 12:19:12 +0000314 {
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000315 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, policy);
Luca Foschianid2390532020-02-20 12:19:12 +0000316 }
317 else
318 {
319 SimpleTensor<float> src_f = convert_from_asymmetric(src);
320 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100321 return convert_to_asymmetric<int8_t>(dst_f, quantization_info_output);
Luca Foschianid2390532020-02-20 12:19:12 +0000322 }
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000323 }
324 else
325 {
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000326 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, policy);
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000327 }
328}
329
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100330template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000331 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100332template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000333 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000334
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100335template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000336 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100337template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int32_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000338 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100339template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000340 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100341template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000342 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100343template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
Mohammed Suhail Munshi470cc5d2023-02-09 11:52:06 +0000344 QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000345
Georgios Pinitasd9769582017-08-03 10:19:40 +0100346} // namespace reference
347} // namespace validation
348} // namespace test
349} // namespace arm_compute