blob: a0b6a8eaed4b9c1c2e9f3c89b169b19595877e09 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Michalis Spyrou19bd4122020-01-22 10:27:06 +00002 * 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 Foschianic6173fe2020-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 Foschianic6173fe2020-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>
272SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op)
273{
274 return compute_reduction_operation<T, OT>(src, dst_shape, axis, op);
275}
276
277template <>
278SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op)
279{
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000280 if(src.data_type() == DataType::QASYMM8)
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000281 {
Luca Foschianic6173fe2020-02-20 12:19:12 +0000282 // If the operation is MEAN_SUM, we can directly use the uint8 implementation without taking into account scale and offset
283 if(op == ReductionOperation::MEAN_SUM)
284 {
285 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op);
286 }
287 else
288 {
289 SimpleTensor<float> src_f = convert_from_asymmetric(src);
290 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
291 return convert_to_asymmetric<uint8_t>(dst_f, src.quantization_info());
292 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000293 }
294 else
295 {
296 return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op);
297 }
298}
299
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000300template <>
301SimpleTensor<int8_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op)
302{
303 if(src.data_type() == DataType::QASYMM8_SIGNED)
304 {
Luca Foschianic6173fe2020-02-20 12:19:12 +0000305 // If the operation is MEAN_SUM, we can directly use the int8 implementation without taking into account scale and offset
306 if(op == ReductionOperation::MEAN_SUM)
307 {
308 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op);
309 }
310 else
311 {
312 SimpleTensor<float> src_f = convert_from_asymmetric(src);
313 SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
314 return convert_to_asymmetric<int8_t>(dst_f, src.quantization_info());
315 }
Michalis Spyrou0b18d972020-01-30 18:11:13 +0000316 }
317 else
318 {
319 return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op);
320 }
321}
322
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000323template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
324template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
325
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +0000326template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
327template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int32_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
328template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
329template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000330
Georgios Pinitasd9769582017-08-03 10:19:40 +0100331} // namespace reference
332} // namespace validation
333} // namespace test
334} // namespace arm_compute