blob: ffb79f86c5d1501163228c8ec6a026c1eec0583c [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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"
25
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010026#include "tests/validation/Helpers.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010027
28#include <algorithm>
29#include <cmath>
30
31namespace arm_compute
32{
33namespace test
34{
35namespace validation
36{
37namespace reference
38{
39namespace
40{
Michalis Spyrou7930db42018-11-22 17:36:28 +000041template <typename T, typename OT>
42OT reduce_operation(const T *ptr, int reduce_elements, ReductionOperation op, int stride)
Georgios Pinitasd9769582017-08-03 10:19:40 +010043{
Michalis Spyrou7930db42018-11-22 17:36:28 +000044 using type = typename std::remove_cv<OT>::type;
Usama Arifa4a08ad2019-05-20 12:38:33 +010045 T res;
46 switch(op)
47 {
48 case ReductionOperation::PROD:
49 {
50 res = type(1);
51 }
52 break;
53 case ReductionOperation::MIN:
Usama Arif28f0dd92019-05-20 13:44:34 +010054 case ReductionOperation::MAX:
Usama Arifa4a08ad2019-05-20 12:38:33 +010055 {
56 res = *ptr;
57 }
58 break;
59 default:
60 {
61 res = type(0);
62 }
63 }
Georgios Pinitasd9769582017-08-03 10:19:40 +010064
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010065 if(std::is_integral<type>::value)
Michalis Spyrou7e9391b2018-10-05 14:49:28 +010066 {
Luca Foschianid2390532020-02-20 12:19:12 +000067 auto int_res = static_cast<int32_t>(res);
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010068 for(int i = 0; i < reduce_elements; ++i)
69 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +000070 auto elem = *(ptr + stride * i);
Michalis Spyrou7930db42018-11-22 17:36:28 +000071
72 switch(op)
73 {
Usama Arifa4a08ad2019-05-20 12:38:33 +010074 case ReductionOperation::MIN:
75 if(static_cast<T>(int_res) > elem)
76 {
77 int_res = elem;
78 }
79 break;
Usama Arif28f0dd92019-05-20 13:44:34 +010080 case ReductionOperation::MAX:
81 if(static_cast<T>(int_res) < elem)
82 {
83 int_res = elem;
84 }
85 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +000086 case ReductionOperation::SUM_SQUARE:
87 int_res += elem * elem;
88 break;
89 case ReductionOperation::MEAN_SUM:
90 case ReductionOperation::SUM:
91 int_res += elem;
92 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +000093 case ReductionOperation::PROD:
94 int_res *= elem;
95 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +000096 default:
97 ARM_COMPUTE_ERROR("Operation not supported");
98 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +010099 }
100 if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0)
101 {
102 int_res /= reduce_elements;
103 }
Luca Foschianid2390532020-02-20 12:19:12 +0000104 res = static_cast<type>(int_res);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100105 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100106 else
107 {
108 for(int i = 0; i < reduce_elements; ++i)
109 {
110 auto elem = *(ptr + stride * i);
Michalis Spyrou7930db42018-11-22 17:36:28 +0000111 switch(op)
112 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100113 case ReductionOperation::MIN:
114 if(res > elem)
115 {
116 res = elem;
117 }
118 break;
Usama Arif28f0dd92019-05-20 13:44:34 +0100119 case ReductionOperation::MAX:
120 if(res < elem)
121 {
122 res = elem;
123 }
124 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000125 case ReductionOperation::SUM_SQUARE:
126 res += elem * elem;
127 break;
128 case ReductionOperation::MEAN_SUM:
129 case ReductionOperation::SUM:
130 res += elem;
131 break;
Manuel Bottinib412fab2018-12-10 17:40:23 +0000132 case ReductionOperation::PROD:
133 res *= elem;
134 break;
Michalis Spyrou7930db42018-11-22 17:36:28 +0000135 default:
136 ARM_COMPUTE_ERROR("Operation not supported");
137 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100138 }
139 if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0)
140 {
141 res /= reduce_elements;
142 }
143 }
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100144 return res;
Georgios Pinitasd9769582017-08-03 10:19:40 +0100145}
Manuel Bottini62404a52019-06-10 17:06:39 +0100146
147template <typename T, typename OT>
148OT reduce_operation_arg_min_max(const T *ptr, int reduce_elements, ReductionOperation op, int stride)
149{
150 uint32_t res = 0;
151 for(int i = 0; i < reduce_elements; ++i)
152 {
153 auto elem = *(ptr + stride * i);
154 switch(op)
155 {
156 case ReductionOperation::ARG_IDX_MIN:
157 if(*(ptr + stride * res) > elem)
158 {
159 res = static_cast<uint32_t>(i);
160 }
161 break;
162 case ReductionOperation::ARG_IDX_MAX:
163 if(*(ptr + stride * res) < elem)
164 {
165 res = static_cast<uint32_t>(i);
166 }
167 break;
168 default:
169 ARM_COMPUTE_ERROR("Operation not supported");
170 }
171 }
172 return static_cast<OT>(res);
173}
174
Georgios Pinitasd9769582017-08-03 10:19:40 +0100175} // namespace
176
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000177template <typename T, typename OT>
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000178SimpleTensor<OT> compute_reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100179{
180 // Create reference
Michalis Spyrou7930db42018-11-22 17:36:28 +0000181 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +0000182 DataType output_data_type = is_arg_min_max ? DataType::S32 : src.data_type();
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000183 SimpleTensor<OT> dst{ dst_shape, output_data_type, 1, src.quantization_info() };
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100184 const unsigned int src_width = src.shape().x();
185 const unsigned int src_height = src.shape().y();
186 const unsigned int src_depth = src.shape().z();
187 const unsigned int src_batch = src.shape()[3];
188 const int reduce_elems = src.shape()[axis];
Georgios Pinitasd9769582017-08-03 10:19:40 +0100189
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100190 switch(axis)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100191 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100192 case 0:
Georgios Pinitasd9769582017-08-03 10:19:40 +0100193 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100194 const unsigned int upper_dims = src.shape().total_size_upper(1);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100195 for(unsigned int du = 0; du < upper_dims; ++du)
196 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100197 const T *src_row_ptr = src.data() + du * reduce_elems;
Manuel Bottini62404a52019-06-10 17:06:39 +0100198 dst[du] = is_arg_min_max ?
199 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, 1) :
200 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, 1);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100201 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100202 }
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100203 break;
204 case 1:
Georgios Pinitasd9769582017-08-03 10:19:40 +0100205 {
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100206 const unsigned int upper_dims = src.shape().total_size_upper(2);
207 for(unsigned int du = 0; du < upper_dims; ++du)
208 {
209 for(unsigned int x = 0; x < src_width; ++x)
210 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100211 const int in_offset = du * src_height * src_width + x;
212 const int out_offset = du * src_width + x;
213 const T *src_row_ptr = src.data() + in_offset;
Manuel Bottini62404a52019-06-10 17:06:39 +0100214 dst[out_offset] = is_arg_min_max ?
215 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width) :
216 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100217 }
218 }
219 }
220 break;
221 case 2:
222 {
223 const unsigned int upper_dims = src.shape().total_size_upper(3);
224 for(unsigned int du = 0; du < upper_dims; ++du)
225 {
226 for(unsigned int x = 0; x < src_width; ++x)
227 {
228 for(unsigned int y = 0; y < src_height; ++y)
229 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100230 const int in_offset = du * src_depth * src_height * src_width + y * src_width + x;
231 const int out_offset = du * src_width * src_height + y * src_width + x;
232 const T *src_row_ptr = src.data() + in_offset;
Manuel Bottini62404a52019-06-10 17:06:39 +0100233 dst[out_offset] = is_arg_min_max ?
234 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height) :
235 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100236 }
237 }
238 }
239 }
240 break;
241 case 3:
242 {
243 const unsigned int upper_dims = src.shape().total_size_upper(4);
244 for(unsigned int du = 0; du < upper_dims; ++du)
245 {
246 for(unsigned int z = 0; z < src_depth; ++z)
247 {
248 for(unsigned int y = 0; y < src_height; ++y)
249 {
250 for(unsigned int x = 0; x < src_width; ++x)
251 {
Michalis Spyrou8aaf93e2018-10-11 17:33:32 +0100252 const int in_offset = du * src_batch * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x;
253 const int out_offset = du * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x;
254 const T *src_row_ptr = src.data() + in_offset;
Manuel Bottini62404a52019-06-10 17:06:39 +0100255 dst[out_offset] = is_arg_min_max ?
256 reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth) :
257 reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth);
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100258 }
259 }
260 }
261 }
262 }
263 break;
264 default:
Georgios Pinitasd9769582017-08-03 10:19:40 +0100265 ARM_COMPUTE_ERROR("Unsupported reduction axis");
Georgios Pinitasd9769582017-08-03 10:19:40 +0100266 }
267
268 return dst;
269}
270
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000271template <typename T, typename OT>
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100272SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000273{
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100274 ARM_COMPUTE_UNUSED(quantization_info_output);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000275 return compute_reduction_operation<T, OT>(src, dst_shape, axis, op);
276}
277
278template <>
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100279SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000280{
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000281 if(src.data_type() == DataType::QASYMM8)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000282 {
Luca Foschianid2390532020-02-20 12:19:12 +0000283 // 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 +0100284 if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
Luca Foschianid2390532020-02-20 12:19:12 +0000285 {
286 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op);
287 }
288 else
289 {
290 SimpleTensor<float> src_f = convert_from_asymmetric(src);
291 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100292 return convert_to_asymmetric<uint8_t>(dst_f, quantization_info_output);
Luca Foschianid2390532020-02-20 12:19:12 +0000293 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000294 }
295 else
296 {
297 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op);
298 }
299}
300
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000301template <>
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100302SimpleTensor<int8_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output)
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000303{
304 if(src.data_type() == DataType::QASYMM8_SIGNED)
305 {
Luca Foschianid2390532020-02-20 12:19:12 +0000306 // 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 +0100307 if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
Luca Foschianid2390532020-02-20 12:19:12 +0000308 {
309 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op);
310 }
311 else
312 {
313 SimpleTensor<float> src_f = convert_from_asymmetric(src);
314 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100315 return convert_to_asymmetric<int8_t>(dst_f, quantization_info_output);
Luca Foschianid2390532020-02-20 12:19:12 +0000316 }
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000317 }
318 else
319 {
320 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op);
321 }
322}
323
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100324template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
325 QuantizationInfo quantization_info_output = QuantizationInfo());
326template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
327 QuantizationInfo quantization_info_output = QuantizationInfo());
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000328
Manuel Bottinic58f0ad2020-08-07 16:49:15 +0100329template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
330 QuantizationInfo quantization_info_output = QuantizationInfo());
331template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int32_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
332 QuantizationInfo quantization_info_output = QuantizationInfo());
333template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
334 QuantizationInfo quantization_info_output = QuantizationInfo());
335template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
336 QuantizationInfo quantization_info_output = QuantizationInfo());
337template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
338 QuantizationInfo quantization_info_output = QuantizationInfo());
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000339
Georgios Pinitasd9769582017-08-03 10:19:40 +0100340} // namespace reference
341} // namespace validation
342} // namespace test
343} // namespace arm_compute