1*c217d954SCole Faust /*
2*c217d954SCole Faust * Copyright (c) 2017-2020, 2023 Arm Limited.
3*c217d954SCole Faust *
4*c217d954SCole Faust * SPDX-License-Identifier: MIT
5*c217d954SCole Faust *
6*c217d954SCole Faust * Permission is hereby granted, free of charge, to any person obtaining a copy
7*c217d954SCole Faust * of this software and associated documentation files (the "Software"), to
8*c217d954SCole Faust * deal in the Software without restriction, including without limitation the
9*c217d954SCole Faust * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10*c217d954SCole Faust * sell copies of the Software, and to permit persons to whom the Software is
11*c217d954SCole Faust * furnished to do so, subject to the following conditions:
12*c217d954SCole Faust *
13*c217d954SCole Faust * The above copyright notice and this permission notice shall be included in all
14*c217d954SCole Faust * copies or substantial portions of the Software.
15*c217d954SCole Faust *
16*c217d954SCole Faust * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17*c217d954SCole Faust * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18*c217d954SCole Faust * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19*c217d954SCole Faust * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20*c217d954SCole Faust * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21*c217d954SCole Faust * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22*c217d954SCole Faust * SOFTWARE.
23*c217d954SCole Faust */
24*c217d954SCole Faust #include "ReductionOperation.h"
25*c217d954SCole Faust #include "tests/validation/Helpers.h"
26*c217d954SCole Faust
27*c217d954SCole Faust #include <algorithm>
28*c217d954SCole Faust #include <cmath>
29*c217d954SCole Faust
30*c217d954SCole Faust namespace arm_compute
31*c217d954SCole Faust {
32*c217d954SCole Faust namespace test
33*c217d954SCole Faust {
34*c217d954SCole Faust namespace validation
35*c217d954SCole Faust {
36*c217d954SCole Faust namespace reference
37*c217d954SCole Faust {
38*c217d954SCole Faust namespace
39*c217d954SCole Faust {
40*c217d954SCole Faust template <typename T, typename OT>
reduce_operation(const T * ptr,int reduce_elements,ReductionOperation op,int stride,RoundingPolicy policy)41*c217d954SCole Faust OT reduce_operation(const T *ptr, int reduce_elements, ReductionOperation op, int stride, RoundingPolicy policy)
42*c217d954SCole Faust {
43*c217d954SCole Faust using type = typename std::remove_cv<OT>::type;
44*c217d954SCole Faust T res;
45*c217d954SCole Faust switch(op)
46*c217d954SCole Faust {
47*c217d954SCole Faust case ReductionOperation::PROD:
48*c217d954SCole Faust {
49*c217d954SCole Faust res = type(1);
50*c217d954SCole Faust }
51*c217d954SCole Faust break;
52*c217d954SCole Faust case ReductionOperation::MIN:
53*c217d954SCole Faust case ReductionOperation::MAX:
54*c217d954SCole Faust {
55*c217d954SCole Faust res = *ptr;
56*c217d954SCole Faust }
57*c217d954SCole Faust break;
58*c217d954SCole Faust default:
59*c217d954SCole Faust {
60*c217d954SCole Faust res = type(0);
61*c217d954SCole Faust }
62*c217d954SCole Faust }
63*c217d954SCole Faust
64*c217d954SCole Faust if(std::is_integral<type>::value)
65*c217d954SCole Faust {
66*c217d954SCole Faust auto int_res = static_cast<int32_t>(res);
67*c217d954SCole Faust for(int i = 0; i < reduce_elements; ++i)
68*c217d954SCole Faust {
69*c217d954SCole Faust auto elem = *(ptr + stride * i);
70*c217d954SCole Faust
71*c217d954SCole Faust switch(op)
72*c217d954SCole Faust {
73*c217d954SCole Faust case ReductionOperation::MIN:
74*c217d954SCole Faust if(static_cast<T>(int_res) > elem)
75*c217d954SCole Faust {
76*c217d954SCole Faust int_res = elem;
77*c217d954SCole Faust }
78*c217d954SCole Faust break;
79*c217d954SCole Faust case ReductionOperation::MAX:
80*c217d954SCole Faust if(static_cast<T>(int_res) < elem)
81*c217d954SCole Faust {
82*c217d954SCole Faust int_res = elem;
83*c217d954SCole Faust }
84*c217d954SCole Faust break;
85*c217d954SCole Faust case ReductionOperation::SUM_SQUARE:
86*c217d954SCole Faust int_res += elem * elem;
87*c217d954SCole Faust break;
88*c217d954SCole Faust case ReductionOperation::MEAN_SUM:
89*c217d954SCole Faust case ReductionOperation::SUM:
90*c217d954SCole Faust int_res += elem;
91*c217d954SCole Faust break;
92*c217d954SCole Faust case ReductionOperation::PROD:
93*c217d954SCole Faust int_res *= elem;
94*c217d954SCole Faust break;
95*c217d954SCole Faust default:
96*c217d954SCole Faust ARM_COMPUTE_ERROR("Operation not supported");
97*c217d954SCole Faust }
98*c217d954SCole Faust }
99*c217d954SCole Faust if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0)
100*c217d954SCole Faust {
101*c217d954SCole Faust // Only use rounding in aarch64 to be consistent with kernel
102*c217d954SCole Faust #ifdef __aarch64__
103*c217d954SCole Faust // Divide in float format, then rounded to nearest and implicitly cast back to int
104*c217d954SCole Faust int_res = round(static_cast<float>(int_res) / static_cast<float>(reduce_elements), policy);
105*c217d954SCole Faust #else // defined(__aarch64__)
106*c217d954SCole Faust ARM_COMPUTE_UNUSED(policy);
107*c217d954SCole Faust int_res /= reduce_elements; // Legacy compatibility
108*c217d954SCole Faust #endif // __aarch64
109*c217d954SCole Faust }
110*c217d954SCole Faust res = static_cast<type>(int_res);
111*c217d954SCole Faust }
112*c217d954SCole Faust else
113*c217d954SCole Faust {
114*c217d954SCole Faust for(int i = 0; i < reduce_elements; ++i)
115*c217d954SCole Faust {
116*c217d954SCole Faust auto elem = *(ptr + stride * i);
117*c217d954SCole Faust switch(op)
118*c217d954SCole Faust {
119*c217d954SCole Faust case ReductionOperation::MIN:
120*c217d954SCole Faust if(res > elem)
121*c217d954SCole Faust {
122*c217d954SCole Faust res = elem;
123*c217d954SCole Faust }
124*c217d954SCole Faust break;
125*c217d954SCole Faust case ReductionOperation::MAX:
126*c217d954SCole Faust if(res < elem)
127*c217d954SCole Faust {
128*c217d954SCole Faust res = elem;
129*c217d954SCole Faust }
130*c217d954SCole Faust break;
131*c217d954SCole Faust case ReductionOperation::SUM_SQUARE:
132*c217d954SCole Faust res += elem * elem;
133*c217d954SCole Faust break;
134*c217d954SCole Faust case ReductionOperation::MEAN_SUM:
135*c217d954SCole Faust case ReductionOperation::SUM:
136*c217d954SCole Faust res += elem;
137*c217d954SCole Faust break;
138*c217d954SCole Faust case ReductionOperation::PROD:
139*c217d954SCole Faust res *= elem;
140*c217d954SCole Faust break;
141*c217d954SCole Faust default:
142*c217d954SCole Faust ARM_COMPUTE_ERROR("Operation not supported");
143*c217d954SCole Faust }
144*c217d954SCole Faust }
145*c217d954SCole Faust if(op == ReductionOperation::MEAN_SUM && reduce_elements > 0)
146*c217d954SCole Faust {
147*c217d954SCole Faust res /= reduce_elements;
148*c217d954SCole Faust }
149*c217d954SCole Faust }
150*c217d954SCole Faust return res;
151*c217d954SCole Faust }
152*c217d954SCole Faust
153*c217d954SCole Faust template <typename T, typename OT>
reduce_operation_arg_min_max(const T * ptr,int reduce_elements,ReductionOperation op,int stride)154*c217d954SCole Faust OT reduce_operation_arg_min_max(const T *ptr, int reduce_elements, ReductionOperation op, int stride)
155*c217d954SCole Faust {
156*c217d954SCole Faust uint32_t res = 0;
157*c217d954SCole Faust for(int i = 0; i < reduce_elements; ++i)
158*c217d954SCole Faust {
159*c217d954SCole Faust auto elem = *(ptr + stride * i);
160*c217d954SCole Faust switch(op)
161*c217d954SCole Faust {
162*c217d954SCole Faust case ReductionOperation::ARG_IDX_MIN:
163*c217d954SCole Faust if(*(ptr + stride * res) > elem)
164*c217d954SCole Faust {
165*c217d954SCole Faust res = static_cast<uint32_t>(i);
166*c217d954SCole Faust }
167*c217d954SCole Faust break;
168*c217d954SCole Faust case ReductionOperation::ARG_IDX_MAX:
169*c217d954SCole Faust if(*(ptr + stride * res) < elem)
170*c217d954SCole Faust {
171*c217d954SCole Faust res = static_cast<uint32_t>(i);
172*c217d954SCole Faust }
173*c217d954SCole Faust break;
174*c217d954SCole Faust default:
175*c217d954SCole Faust ARM_COMPUTE_ERROR("Operation not supported");
176*c217d954SCole Faust }
177*c217d954SCole Faust }
178*c217d954SCole Faust return static_cast<OT>(res);
179*c217d954SCole Faust }
180*c217d954SCole Faust
181*c217d954SCole Faust } // namespace
182*c217d954SCole Faust
183*c217d954SCole Faust template <typename T, typename OT>
compute_reduction_operation(const SimpleTensor<T> & src,const TensorShape & dst_shape,unsigned int axis,ReductionOperation op,RoundingPolicy policy)184*c217d954SCole Faust SimpleTensor<OT> compute_reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, RoundingPolicy policy)
185*c217d954SCole Faust {
186*c217d954SCole Faust // Create reference
187*c217d954SCole Faust const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
188*c217d954SCole Faust DataType output_data_type = is_arg_min_max ? DataType::S32 : src.data_type();
189*c217d954SCole Faust SimpleTensor<OT> dst{ dst_shape, output_data_type, 1, src.quantization_info() };
190*c217d954SCole Faust const unsigned int src_width = src.shape().x();
191*c217d954SCole Faust const unsigned int src_height = src.shape().y();
192*c217d954SCole Faust const unsigned int src_depth = src.shape().z();
193*c217d954SCole Faust const unsigned int src_batch = src.shape()[3];
194*c217d954SCole Faust const int reduce_elems = src.shape()[axis];
195*c217d954SCole Faust
196*c217d954SCole Faust switch(axis)
197*c217d954SCole Faust {
198*c217d954SCole Faust case 0:
199*c217d954SCole Faust {
200*c217d954SCole Faust const unsigned int upper_dims = src.shape().total_size_upper(1);
201*c217d954SCole Faust for(unsigned int du = 0; du < upper_dims; ++du)
202*c217d954SCole Faust {
203*c217d954SCole Faust const T *src_row_ptr = src.data() + du * reduce_elems;
204*c217d954SCole Faust dst[du] = is_arg_min_max ?
205*c217d954SCole Faust reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, 1) :
206*c217d954SCole Faust reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, 1, policy);
207*c217d954SCole Faust }
208*c217d954SCole Faust }
209*c217d954SCole Faust break;
210*c217d954SCole Faust case 1:
211*c217d954SCole Faust {
212*c217d954SCole Faust const unsigned int upper_dims = src.shape().total_size_upper(2);
213*c217d954SCole Faust for(unsigned int du = 0; du < upper_dims; ++du)
214*c217d954SCole Faust {
215*c217d954SCole Faust for(unsigned int x = 0; x < src_width; ++x)
216*c217d954SCole Faust {
217*c217d954SCole Faust const int in_offset = du * src_height * src_width + x;
218*c217d954SCole Faust const int out_offset = du * src_width + x;
219*c217d954SCole Faust const T *src_row_ptr = src.data() + in_offset;
220*c217d954SCole Faust dst[out_offset] = is_arg_min_max ?
221*c217d954SCole Faust reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width) :
222*c217d954SCole Faust reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width, policy);
223*c217d954SCole Faust }
224*c217d954SCole Faust }
225*c217d954SCole Faust }
226*c217d954SCole Faust break;
227*c217d954SCole Faust case 2:
228*c217d954SCole Faust {
229*c217d954SCole Faust const unsigned int upper_dims = src.shape().total_size_upper(3);
230*c217d954SCole Faust for(unsigned int du = 0; du < upper_dims; ++du)
231*c217d954SCole Faust {
232*c217d954SCole Faust for(unsigned int x = 0; x < src_width; ++x)
233*c217d954SCole Faust {
234*c217d954SCole Faust for(unsigned int y = 0; y < src_height; ++y)
235*c217d954SCole Faust {
236*c217d954SCole Faust const int in_offset = du * src_depth * src_height * src_width + y * src_width + x;
237*c217d954SCole Faust const int out_offset = du * src_width * src_height + y * src_width + x;
238*c217d954SCole Faust const T *src_row_ptr = src.data() + in_offset;
239*c217d954SCole Faust dst[out_offset] = is_arg_min_max ?
240*c217d954SCole Faust reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height) :
241*c217d954SCole Faust reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height, policy);
242*c217d954SCole Faust }
243*c217d954SCole Faust }
244*c217d954SCole Faust }
245*c217d954SCole Faust }
246*c217d954SCole Faust break;
247*c217d954SCole Faust case 3:
248*c217d954SCole Faust {
249*c217d954SCole Faust const unsigned int upper_dims = src.shape().total_size_upper(4);
250*c217d954SCole Faust for(unsigned int du = 0; du < upper_dims; ++du)
251*c217d954SCole Faust {
252*c217d954SCole Faust for(unsigned int z = 0; z < src_depth; ++z)
253*c217d954SCole Faust {
254*c217d954SCole Faust for(unsigned int y = 0; y < src_height; ++y)
255*c217d954SCole Faust {
256*c217d954SCole Faust for(unsigned int x = 0; x < src_width; ++x)
257*c217d954SCole Faust {
258*c217d954SCole Faust const int in_offset = du * src_batch * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x;
259*c217d954SCole Faust const int out_offset = du * src_depth * src_height * src_width + z * src_width * src_height + y * src_width + x;
260*c217d954SCole Faust const T *src_row_ptr = src.data() + in_offset;
261*c217d954SCole Faust dst[out_offset] = is_arg_min_max ?
262*c217d954SCole Faust reduce_operation_arg_min_max<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth) :
263*c217d954SCole Faust reduce_operation<T, OT>(src_row_ptr, reduce_elems, op, src_width * src_height * src_depth, policy);
264*c217d954SCole Faust }
265*c217d954SCole Faust }
266*c217d954SCole Faust }
267*c217d954SCole Faust }
268*c217d954SCole Faust }
269*c217d954SCole Faust break;
270*c217d954SCole Faust default:
271*c217d954SCole Faust ARM_COMPUTE_ERROR("Unsupported reduction axis");
272*c217d954SCole Faust }
273*c217d954SCole Faust
274*c217d954SCole Faust return dst;
275*c217d954SCole Faust }
276*c217d954SCole Faust
277*c217d954SCole Faust template <typename T, typename OT>
reduction_operation(const SimpleTensor<T> & src,const TensorShape & dst_shape,unsigned int axis,ReductionOperation op,QuantizationInfo quantization_info_output,RoundingPolicy policy)278*c217d954SCole Faust SimpleTensor<OT> reduction_operation(const SimpleTensor<T> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy)
279*c217d954SCole Faust {
280*c217d954SCole Faust ARM_COMPUTE_UNUSED(quantization_info_output);
281*c217d954SCole Faust return compute_reduction_operation<T, OT>(src, dst_shape, axis, op, policy);
282*c217d954SCole Faust }
283*c217d954SCole Faust
284*c217d954SCole Faust template <>
reduction_operation(const SimpleTensor<uint8_t> & src,const TensorShape & dst_shape,unsigned int axis,ReductionOperation op,QuantizationInfo quantization_info_output,RoundingPolicy policy)285*c217d954SCole Faust SimpleTensor<uint8_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy)
286*c217d954SCole Faust {
287*c217d954SCole Faust if(src.data_type() == DataType::QASYMM8)
288*c217d954SCole Faust {
289*c217d954SCole Faust // If the operation is MEAN_SUM, we can directly use the uint8 implementation without taking into account scale and offset
290*c217d954SCole Faust if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
291*c217d954SCole Faust {
292*c217d954SCole Faust return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, policy);
293*c217d954SCole Faust }
294*c217d954SCole Faust else
295*c217d954SCole Faust {
296*c217d954SCole Faust SimpleTensor<float> src_f = convert_from_asymmetric(src);
297*c217d954SCole Faust SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
298*c217d954SCole Faust return convert_to_asymmetric<uint8_t>(dst_f, quantization_info_output);
299*c217d954SCole Faust }
300*c217d954SCole Faust }
301*c217d954SCole Faust else
302*c217d954SCole Faust {
303*c217d954SCole Faust return compute_reduction_operation<uint8_t, uint8_t>(src, dst_shape, axis, op, policy);
304*c217d954SCole Faust }
305*c217d954SCole Faust }
306*c217d954SCole Faust
307*c217d954SCole Faust template <>
reduction_operation(const SimpleTensor<int8_t> & src,const TensorShape & dst_shape,unsigned int axis,ReductionOperation op,QuantizationInfo quantization_info_output,RoundingPolicy policy)308*c217d954SCole Faust SimpleTensor<int8_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op, QuantizationInfo quantization_info_output, RoundingPolicy policy)
309*c217d954SCole Faust {
310*c217d954SCole Faust if(src.data_type() == DataType::QASYMM8_SIGNED)
311*c217d954SCole Faust {
312*c217d954SCole Faust // If the operation is MEAN_SUM, we can directly use the int8 implementation without taking into account scale and offset
313*c217d954SCole Faust if(op == ReductionOperation::MEAN_SUM && src.quantization_info() == quantization_info_output)
314*c217d954SCole Faust {
315*c217d954SCole Faust return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, policy);
316*c217d954SCole Faust }
317*c217d954SCole Faust else
318*c217d954SCole Faust {
319*c217d954SCole Faust SimpleTensor<float> src_f = convert_from_asymmetric(src);
320*c217d954SCole Faust SimpleTensor<float> dst_f = reference::reduction_operation<float, float>(src_f, dst_shape, axis, op);
321*c217d954SCole Faust return convert_to_asymmetric<int8_t>(dst_f, quantization_info_output);
322*c217d954SCole Faust }
323*c217d954SCole Faust }
324*c217d954SCole Faust else
325*c217d954SCole Faust {
326*c217d954SCole Faust return compute_reduction_operation<int8_t, int8_t>(src, dst_shape, axis, op, policy);
327*c217d954SCole Faust }
328*c217d954SCole Faust }
329*c217d954SCole Faust
330*c217d954SCole Faust template SimpleTensor<float> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
331*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
332*c217d954SCole Faust template SimpleTensor<half> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
333*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
334*c217d954SCole Faust
335*c217d954SCole Faust template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<float> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
336*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
337*c217d954SCole Faust template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int32_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
338*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
339*c217d954SCole Faust template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<half> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
340*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
341*c217d954SCole Faust template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<uint8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
342*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
343*c217d954SCole Faust template SimpleTensor<int32_t> reduction_operation(const SimpleTensor<int8_t> &src, const TensorShape &dst_shape, unsigned int axis, ReductionOperation op,
344*c217d954SCole Faust QuantizationInfo quantization_info_output = QuantizationInfo(), RoundingPolicy policy = RoundingPolicy::TO_ZERO);
345*c217d954SCole Faust
346*c217d954SCole Faust } // namespace reference
347*c217d954SCole Faust } // namespace validation
348*c217d954SCole Faust } // namespace test
349*c217d954SCole Faust } // namespace arm_compute
350