blob: bfecccf94b32286c1b043cbd68f2c4ef82e8ea83 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Sheri Zhangac6499a2021-02-10 15:32:38 +00002 * Copyright (c) 2017-2021 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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEReductionOperationKernel.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010025
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Helpers.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010028#include "arm_compute/core/ITensor.h"
John Richardson73d4aef2018-05-08 14:34:33 +010029#include "arm_compute/core/TensorInfo.h"
Luca Foschianiee939fb2020-01-28 10:38:07 +000030#include "arm_compute/core/Utils.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010031#include "arm_compute/core/Validate.h"
Michalis Spyrouaea14c62019-01-03 11:10:25 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/CPP/Validate.h"
Michalis Spyrouebcebf12020-10-21 00:04:14 +010034#include "src/core/NEON/INEKernel.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010035#include "src/core/NEON/NEMath.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
38#include "support/SaturateCast.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010039
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010040#include "src/core/NEON/wrapper/wrapper.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010041#include <arm_neon.h>
42
Michalis Spyroubcf8a962018-10-12 10:51:31 +010043namespace arm_compute
44{
Georgios Pinitasd9769582017-08-03 10:19:40 +010045namespace
46{
Luca Foschianiee939fb2020-01-28 10:38:07 +000047// Helper function that calls vqmovun/vqmvn, vcombine and vstore, allows templating of RedOpYZW_quantized
48template <typename T>
Sheri Zhang4d91dc62020-09-23 11:22:50 +010049void combine_and_store(int16x8_t t1, int16x8_t t2, Iterator &output, int offset = 0)
Luca Foschianiee939fb2020-01-28 10:38:07 +000050{
51 if(std::is_same<T, uint8_t>::value)
52 {
53 auto res = wrapper::vcombine(wrapper::vqmovun(t1), wrapper::vqmovun(t2));
Sheri Zhang4d91dc62020-09-23 11:22:50 +010054 wrapper::vstore(output.ptr() + offset, res);
Luca Foschianiee939fb2020-01-28 10:38:07 +000055 }
56 else
57 {
58 auto res = wrapper::vcombine(wrapper::vqmovn(t1), wrapper::vqmovn(t2));
Sheri Zhang4d91dc62020-09-23 11:22:50 +010059 wrapper::vstore(reinterpret_cast<int8_t *>(output.ptr() + offset), res);
Luca Foschianiee939fb2020-01-28 10:38:07 +000060 }
61}
62
Michalis Spyroub9626ab2019-05-13 17:41:01 +010063template <typename T>
64uint32x4x4_t calculate_index(uint32_t idx, T a, T b, uint32x4x4_t c, ReductionOperation op, int axis)
Michalis Spyrouaea14c62019-01-03 11:10:25 +000065{
66 uint32x4_t mask{ 0 };
67 if(op == ReductionOperation::ARG_IDX_MIN)
68 {
69 mask = wrapper::vcgt(b, a);
70 }
71 else
72 {
73 mask = wrapper::vclt(b, a);
74 }
75
76 uint32x4_t vec_idx = { idx, idx + 1, idx + 2, idx + 3 };
77 if(axis != 0)
78 {
79 vec_idx = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
80 }
Georgios Pinitasd57891a2019-02-19 18:10:03 +000081 uint32x4x4_t res = { { wrapper::vbsl(mask, vec_idx, c.val[0]), 0, 0, 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +000082
83 return res;
84}
85
Luca Foschianiee939fb2020-01-28 10:38:07 +000086template <typename T>
87uint32x4x4_t calculate_index_quantized(uint32_t idx, T a, T b, uint32x4x4_t c, ReductionOperation op, int axis)
Michalis Spyrouaea14c62019-01-03 11:10:25 +000088{
Georgios Pinitasd57891a2019-02-19 18:10:03 +000089 uint32x4x4_t mask{ { 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +000090 uint8x16_t mask_u8{ 0 };
91 if(op == ReductionOperation::ARG_IDX_MIN)
92 {
93 mask_u8 = wrapper::vcgt(b, a);
94 }
95 else
96 {
97 mask_u8 = wrapper::vclt(b, a);
98 }
Michalis Spyrou254a48a2019-01-14 17:27:39 +000099 auto wide_u16_1 = wrapper::vorr(vshll_n_u8(wrapper::vgetlow(mask_u8), 8), wrapper::vmovl(wrapper::vgetlow(mask_u8)));
100 auto wide_u16_2 = wrapper::vorr(vshll_n_u8(wrapper::vgethigh(mask_u8), 8), wrapper::vmovl(wrapper::vgethigh(mask_u8)));
101 mask.val[0] = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_1), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_1)));
102 mask.val[1] = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_1), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_1)));
103 mask.val[2] = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_2), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_2)));
104 mask.val[3] = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_2), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_2)));
105
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000106 uint32x4x4_t vec_idx = { { { idx + 0, idx + 1, idx + 2, idx + 3 },
107 { idx + 4, idx + 5, idx + 6, idx + 7 },
108 { idx + 8, idx + 9, idx + 10, idx + 11 },
109 { idx + 12, idx + 13, idx + 14, idx + 15 }
110 }
111 };
112 if(axis != 0)
113 {
114 vec_idx.val[0] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
115 vec_idx.val[1] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
116 vec_idx.val[2] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
117 vec_idx.val[3] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
118 }
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000119 uint32x4x4_t res =
120 {
121 {
122 vbslq_u32(mask.val[0], vec_idx.val[0], c.val[0]),
123 vbslq_u32(mask.val[1], vec_idx.val[1], c.val[1]),
124 vbslq_u32(mask.val[2], vec_idx.val[2], c.val[2]),
125 vbslq_u32(mask.val[3], vec_idx.val[3], c.val[3])
126 }
127 };
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000128
129 return res;
130}
Usama Arifa4a08ad2019-05-20 12:38:33 +0100131
132// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
Luca Foschianiee939fb2020-01-28 10:38:07 +0000133template <typename T>
134inline typename std::enable_if < std::is_same<T, float32x4_t>::value || std::is_same<T, int32x4_t>::value,
135 typename std::conditional<std::is_same<T, float32x4_t>::value, float32x2_t, int32x2_t>::type >::type
136 calculate_min(T in)
Usama Arifa4a08ad2019-05-20 12:38:33 +0100137{
138 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
139 return wrapper::vpmin(pmin, pmin);
140}
141
Luca Foschianiee939fb2020-01-28 10:38:07 +0000142// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
143template <typename T>
144inline typename std::enable_if < std::is_same<T, uint8x16_t>::value || std::is_same<T, int8x16_t>::value,
145 typename std::conditional<std::is_same<T, uint8x16_t>::value, uint8x8_t, int8x8_t>::type >::type
146 calculate_min(T in)
147{
148 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
149 pmin = wrapper::vpmin(pmin, pmin);
150 pmin = wrapper::vpmin(pmin, pmin);
151 return wrapper::vpmin(pmin, pmin);
152}
153
Usama Arifa4a08ad2019-05-20 12:38:33 +0100154// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
Luca Foschianiee939fb2020-01-28 10:38:07 +0000155template <typename T>
156inline typename std::enable_if < std::is_same<T, float32x4_t>::value || std::is_same<T, int32x4_t>::value,
157 typename std::conditional<std::is_same<T, float32x4_t>::value, float32x2_t, int32x2_t>::type >::type
158 calculate_max(T in)
Usama Arifa4a08ad2019-05-20 12:38:33 +0100159{
160 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
161 return wrapper::vpmax(pmax, pmax);
162}
Usama Arifa4a08ad2019-05-20 12:38:33 +0100163
164// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
Luca Foschianiee939fb2020-01-28 10:38:07 +0000165template <typename T>
166inline typename std::enable_if < std::is_same<T, uint8x16_t>::value || std::is_same<T, int8x16_t>::value,
167 typename std::conditional<std::is_same<T, uint8x16_t>::value, uint8x8_t, int8x8_t>::type >::type
168 calculate_max(T in)
Usama Arifa4a08ad2019-05-20 12:38:33 +0100169{
170 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
Luca Foschianiee939fb2020-01-28 10:38:07 +0000171 pmax = wrapper::vpmax(pmax, pmax);
172 pmax = wrapper::vpmax(pmax, pmax);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100173 return wrapper::vpmax(pmax, pmax);
174}
175
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100176template <typename T>
177uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, T vec_res_value, ReductionOperation op)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000178{
179 uint32x4_t res_idx_mask{ 0 };
180 uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF);
181
182 if(op == ReductionOperation::ARG_IDX_MIN)
183 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100184 auto pmin = calculate_min(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000185 auto mask = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin));
186 res_idx_mask = wrapper::vand(vec_res_idx.val[0], mask);
187 }
188 else
189 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100190 auto pmax = calculate_max(vec_res_value);
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100191 auto mask = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax));
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000192 res_idx_mask = wrapper::vand(vec_res_idx.val[0], mask);
193 }
194
195 res_idx_mask = wrapper::vadd(res_idx_mask, mask_ones);
196 auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask), wrapper::vgetlow(res_idx_mask));
197 pmin = wrapper::vpmin(pmin, pmin);
198 uint32_t res = wrapper::vgetlane(pmin, 0);
199
200 return (res - 0xFFFFFFFF);
201}
202
Luca Foschianiee939fb2020-01-28 10:38:07 +0000203template <typename T>
204uint32_t calculate_vector_index_quantized(uint32x4x4_t vec_res_idx, T vec_res_value, ReductionOperation op)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000205{
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000206 uint32x4x4_t res_idx_mask{ { 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000207 uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF);
208 uint8x16_t mask_u8{ 0 };
209 if(op == ReductionOperation::ARG_IDX_MIN)
210 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100211 auto pmin = calculate_min(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000212 mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin));
213 }
214 else
215 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100216 auto pmax = calculate_max(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000217 mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax));
218 }
219
220 // Widen vectors
221 auto wide_u16_1 = wrapper::vorr(vshll_n_u8(wrapper::vgetlow(mask_u8), 8), wrapper::vmovl(wrapper::vgetlow(mask_u8)));
222 auto wide_u16_2 = wrapper::vorr(vshll_n_u8(wrapper::vgethigh(mask_u8), 8), wrapper::vmovl(wrapper::vgethigh(mask_u8)));
223 auto wide_u32_1 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_1), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_1)));
224 auto wide_u32_2 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_1), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_1)));
225 auto wide_u32_3 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_2), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_2)));
226 auto wide_u32_4 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_2), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_2)));
227 res_idx_mask.val[0] = wrapper::vand(vec_res_idx.val[0], wide_u32_1);
228 res_idx_mask.val[1] = wrapper::vand(vec_res_idx.val[1], wide_u32_2);
229 res_idx_mask.val[2] = wrapper::vand(vec_res_idx.val[2], wide_u32_3);
230 res_idx_mask.val[3] = wrapper::vand(vec_res_idx.val[3], wide_u32_4);
231 res_idx_mask.val[0] = wrapper::vadd(res_idx_mask.val[0], mask_ones);
232 res_idx_mask.val[1] = wrapper::vadd(res_idx_mask.val[1], mask_ones);
233 res_idx_mask.val[2] = wrapper::vadd(res_idx_mask.val[2], mask_ones);
234 res_idx_mask.val[3] = wrapper::vadd(res_idx_mask.val[3], mask_ones);
235
236 uint32_t res = 0xFFFFFFFF;
237 int iter = 0;
238 do
239 {
240 auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask.val[iter]), wrapper::vgetlow(res_idx_mask.val[iter]));
241 pmin = wrapper::vpmin(pmin, pmin);
242 res = std::min(wrapper::vgetlane(pmin, 0), res);
243 iter++;
244 }
245 while(iter < 4);
246
247 return (res - 0xFFFFFFFF);
248}
Luca Foschianiee939fb2020-01-28 10:38:07 +0000249
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000250#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitasfad18382019-06-05 15:12:22 +0100251template <>
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000252uint32x4x4_t calculate_index(uint32_t idx, float16x8_t a, float16x8_t b, uint32x4x4_t c, ReductionOperation op, int axis)
253{
254 uint32x4x2_t mask{ 0 };
255 uint16x8_t mask_u16{ 0 };
256 if(op == ReductionOperation::ARG_IDX_MIN)
257 {
258 mask_u16 = wrapper::vcgt(b, a);
259 }
260 else
261 {
262 mask_u16 = wrapper::vclt(b, a);
263 }
264 mask.val[0] = wrapper::vmovl(wrapper::vgetlow(mask_u16));
265 mask.val[1] = wrapper::vmovl(wrapper::vgethigh(mask_u16));
266 uint32x4x2_t vec_idx = { { { idx + 0, idx + 1, idx + 2, idx + 3 },
267 { idx + 4, idx + 5, idx + 6, idx + 7 }
268 }
269 };
270 if(axis != 0)
271 {
272 vec_idx.val[0] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
273 vec_idx.val[1] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
274 }
275 uint32x4x4_t res = { wrapper::vbsl(mask.val[0], vec_idx.val[0], c.val[0]),
276 wrapper::vbsl(mask.val[1], vec_idx.val[1], c.val[1]),
277 0, 0
278 };
279
280 return res;
281}
282
Usama Arifa4a08ad2019-05-20 12:38:33 +0100283// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
284inline float16x4_t calculate_min(float16x8_t in)
285{
286 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
287 pmin = wrapper::vpmin(pmin, pmin);
288 return wrapper::vpmin(pmin, pmin);
289}
290// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
291inline float16x4_t calculate_max(float16x8_t in)
292{
293 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
294 pmax = wrapper::vpmax(pmax, pmax);
295 return wrapper::vpmax(pmax, pmax);
296}
297
Usama Arif0a5a57a2019-05-23 14:20:33 +0100298template <>
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000299uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, float16x8_t vec_res_value, ReductionOperation op)
300{
301 uint32x4x2_t res_idx_mask{ 0 };
302 uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF);
303 uint16x8_t mask_u16;
304 if(op == ReductionOperation::ARG_IDX_MIN)
305 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100306 auto pmin = calculate_min(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000307 mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin));
308 }
309 else
310 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100311 auto pmax = calculate_max(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000312 mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax));
313 }
314
315 // Widen vectors
316 auto wide_u32_1 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(mask_u16), 8), wrapper::vmovl(wrapper::vgetlow(mask_u16)));
317 auto wide_u32_2 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(mask_u16), 8), wrapper::vmovl(wrapper::vgethigh(mask_u16)));
318 res_idx_mask.val[0] = wrapper::vand(vec_res_idx.val[0], wide_u32_1);
319 res_idx_mask.val[1] = wrapper::vand(vec_res_idx.val[1], wide_u32_2);
320 res_idx_mask.val[0] = wrapper::vadd(res_idx_mask.val[0], mask_ones);
321 res_idx_mask.val[1] = wrapper::vadd(res_idx_mask.val[1], mask_ones);
322
323 uint32_t res = 0xFFFFFFFF;
Michalis Spyrouc89998f2021-08-26 14:11:44 +0100324 uint32_t iter = 0;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000325 do
326 {
327 auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask.val[iter]), wrapper::vgetlow(res_idx_mask.val[iter]));
328 pmin = wrapper::vpmin(pmin, pmin);
329 res = std::min(wrapper::vgetlane(pmin, 0), res);
330 iter++;
331 }
332 while(iter < 2);
333
334 return (res - 0xFFFFFFFF);
335}
336#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
337
Georgios Pinitasd9769582017-08-03 10:19:40 +0100338template <class F>
339class Reducer
340{
341public:
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000342 static void reduceX(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100343 {
344 // Set out window
345 Window out_window(window);
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100346 out_window.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100347
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100348 f(window, out_window, input, output, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100349 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000350 static void reduceY(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100351 {
352 // Set in window
353 Window in_window(window);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000354 Window out_window(window);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100355
356 in_window.set(Window::DimY, Window::Dimension(0, 1, 1));
Michalis Spyrou2897e612018-11-20 18:38:29 +0000357 out_window.set(Window::DimY, Window::Dimension(0, output->info()->dimension(1), output->info()->dimension(1)));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100358
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100359 f(in_window, out_window, input, output, 1, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100360 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000361 static void reduceZ(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100362 {
363 // Set in window
364 Window in_window(window);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000365 Window out_window(window);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100366
367 in_window.set(Window::DimZ, Window::Dimension(0, 1, 1));
Michalis Spyrou2897e612018-11-20 18:38:29 +0000368 out_window.set(Window::DimZ, Window::Dimension(0, output->info()->dimension(2), output->info()->dimension(2)));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100369
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100370 f(in_window, out_window, input, output, 2, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100371 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000372 static void reduceW(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100373 {
374 // Set in/out window
375 Window in_window(window);
376 Window out_window(window);
377
378 in_window.set(3, Window::Dimension(0, 1, 1));
379 out_window.set(3, Window::Dimension(0, 1, 1));
380
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100381 f(in_window, out_window, input, output, 3, op);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100382 }
383};
384
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000385template <typename T, int S>
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100386struct RedOpX
Georgios Pinitasd9769582017-08-03 10:19:40 +0100387{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000388 /** SIMD vector tag type. */
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100389 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
390
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100391 inline void operator()(const Window &in_window, Window &out_window, const ITensor *in, ITensor *out, const ReductionOperation op)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100392 {
Manuel Bottini6a5eee72021-04-30 12:37:04 +0100393 const size_t input_dim_0 = in->info()->dimension(0);
394 const int window_step_x = 16 / sizeof(T);
395 const auto window_start_x = static_cast<int>(in_window.x().start());
396 const auto window_end_x = static_cast<int>(in_window.x().end());
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100397
Georgios Pinitas412b7892020-11-11 21:05:24 +0000398 Window in_win_no_pad = in_window;
399 in_win_no_pad.set(Window::DimX, Window::Dimension(0, 1, 1));
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100400
Georgios Pinitas412b7892020-11-11 21:05:24 +0000401 Iterator input(in, in_win_no_pad);
402 Iterator output(out, out_window);
403
404 execute_window_loop(in_win_no_pad, [&](const Coordinates &)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000405 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100406 const auto input_ptr = reinterpret_cast<const T *>(input.ptr());
Georgios Pinitasd9769582017-08-03 10:19:40 +0100407
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100408 auto init_res_value = static_cast<T>(0.f);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100409 switch(op)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000410 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100411 case ReductionOperation::ARG_IDX_MAX:
412 case ReductionOperation::ARG_IDX_MIN:
413 case ReductionOperation::MIN:
Usama Arif28f0dd92019-05-20 13:44:34 +0100414 case ReductionOperation::MAX:
Usama Arifa4a08ad2019-05-20 12:38:33 +0100415 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100416 init_res_value = static_cast<T>(*input_ptr);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100417 break;
418 }
419 case ReductionOperation::PROD:
420 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100421 init_res_value = static_cast<T>(1.f);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100422 break;
423 }
424 default:
Usama Arifa4a08ad2019-05-20 12:38:33 +0100425 break;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000426 }
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100427 auto vec_res_value = wrapper::vdup_n(init_res_value, ExactTagType{});
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000428 uint32x4x4_t vec_res_idx{ { 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000429
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100430 // Compute window_step_x elements per iteration
431 int x = window_start_x;
432 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100433 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100434 const auto vec_elements = wrapper::vloadq(input_ptr + x);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000435 switch(op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100436 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000437 case ReductionOperation::SUM_SQUARE:
438 vec_res_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_res_value);
439 break;
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100440 case ReductionOperation::MEAN_SUM:
441 case ReductionOperation::SUM:
442 vec_res_value = wrapper::vadd(vec_elements, vec_res_value);
443 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000444 case ReductionOperation::PROD:
445 vec_res_value = wrapper::vmul(vec_elements, vec_res_value);
446 break;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000447 case ReductionOperation::ARG_IDX_MIN:
448 {
449 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100450 vec_res_idx = calculate_index<decltype(vec_res_value)>(x, temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000451 vec_res_value = temp_vec_res_value;
452 break;
453 }
454 case ReductionOperation::ARG_IDX_MAX:
455 {
456 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100457 vec_res_idx = calculate_index<decltype(vec_res_value)>(x, temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000458 vec_res_value = temp_vec_res_value;
459 break;
460 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100461 case ReductionOperation::MIN:
462 {
463 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
464 break;
465 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100466 case ReductionOperation::MAX:
467 {
468 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
469 break;
470 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000471 default:
472 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100473 }
474 }
475
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100476 switch(op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100477 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100478 case ReductionOperation::SUM:
479 case ReductionOperation::MEAN_SUM:
480 case ReductionOperation::SUM_SQUARE:
Michele Di Giorgio81d7e782019-08-16 18:03:35 +0100481 {
Manuel Bottini6a5eee72021-04-30 12:37:04 +0100482#ifdef ARM_COMPUTE_DEBUG_ENABLED
483 auto res = static_cast<T>(0.f);
484 for(int i = 0; i < S; ++i)
485 {
486 res += wrapper::vgetlane(vec_res_value, i);
487 }
488#else // ARM_COMPUTE_DEBUG_ENABLED
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100489 auto carry_res = wrapper::vpadd(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value));
490 for(int i = 0; i < S / 4; ++i)
491 {
492 carry_res = wrapper::vpadd(carry_res, carry_res);
493 }
494 auto res = wrapper::vgetlane(carry_res, 0);
Manuel Bottini6a5eee72021-04-30 12:37:04 +0100495#endif // ARM_COMPUTE_DEBUG_ENABLED
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100496 if(op == ReductionOperation::SUM_SQUARE)
497 {
498 // Compute left-over elements
499 for(; x < window_end_x; ++x)
500 {
501 res += (*(input_ptr + x)) * (*(input_ptr + x));
502 }
503 }
504 else
505 {
506 // Compute left-over elements
507 for(; x < window_end_x; ++x)
508 {
509 res += *(input_ptr + x);
510 }
511 }
512
513 if(op == ReductionOperation::MEAN_SUM)
514 {
Manuel Bottini6a5eee72021-04-30 12:37:04 +0100515 res /= input_dim_0;
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100516 }
517
518 *(reinterpret_cast<T *>(output.ptr())) = res;
519 break;
Michele Di Giorgio81d7e782019-08-16 18:03:35 +0100520 }
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100521 case ReductionOperation::PROD:
giuros01154bc1c2019-03-26 17:44:40 +0000522 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100523 auto carry_res = wrapper::vmul(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value));
524 T res = 1;
525 for(int i = 0; i < S / 2; ++i)
526 {
527 res *= wrapper::vgetlane(carry_res, i);
528 }
giuros01154bc1c2019-03-26 17:44:40 +0000529
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100530 // Compute left-over elements
531 for(; x < window_end_x; ++x)
532 {
533 res *= *(input_ptr + x);
534 }
535
536 *(reinterpret_cast<T *>(output.ptr())) = res;
537 break;
538 }
539 case ReductionOperation::ARG_IDX_MIN:
giuros01154bc1c2019-03-26 17:44:40 +0000540 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100541 auto idx = calculate_vector_index<decltype(vec_res_value)>(vec_res_idx, vec_res_value, op);
542 auto res = static_cast<T>(wrapper::vgetlane(calculate_min(vec_res_value), 0));
543
544 // Compute left-over elements
545 for(; x < window_end_x; ++x)
546 {
547 if(*(input_ptr + x) < res)
548 {
549 idx = x;
550 res = *(input_ptr + x);
551 }
552 }
553 *(reinterpret_cast<uint32_t *>(output.ptr())) = idx;
554 break;
giuros01154bc1c2019-03-26 17:44:40 +0000555 }
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100556 case ReductionOperation::ARG_IDX_MAX:
557 {
558 auto idx = calculate_vector_index<decltype(vec_res_value)>(vec_res_idx, vec_res_value, op);
559 auto res = static_cast<T>(wrapper::vgetlane(calculate_max(vec_res_value), 0));
560
561 // Compute left-over elements
562 for(; x < window_end_x; ++x)
563 {
564 if(*(input_ptr + x) > res)
565 {
566 idx = x;
567 res = *(input_ptr + x);
568 }
569 }
570 *(reinterpret_cast<uint32_t *>(output.ptr())) = idx;
571 break;
572 }
573 case ReductionOperation::MIN:
574 {
575 auto res = static_cast<T>(wrapper::vgetlane(calculate_min(vec_res_value), 0));
576
577 // Compute left-over elements
578 for(; x < window_end_x; ++x)
579 {
580 res = *(input_ptr + x) < res ? *(input_ptr + x) : res;
581 }
582 *(reinterpret_cast<T *>(output.ptr())) = res;
583 break;
584 }
585 case ReductionOperation::MAX:
586 {
587 auto res = static_cast<T>(wrapper::vgetlane(calculate_max(vec_res_value), 0));
588
589 // Compute left-over elements
590 for(; x < window_end_x; ++x)
591 {
592 res = *(input_ptr + x) > res ? *(input_ptr + x) : res;
593 }
594 *(reinterpret_cast<T *>(output.ptr())) = res;
595 break;
596 }
597 default:
598 ARM_COMPUTE_ERROR("Not supported");
giuros01154bc1c2019-03-26 17:44:40 +0000599 }
giuros01154bc1c2019-03-26 17:44:40 +0000600 },
601 input, output);
602 }
603};
604
Luca Foschianiee939fb2020-01-28 10:38:07 +0000605template <typename T>
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100606struct RedOpX_quantized
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100607{
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100608 inline void operator()(const Window &in_window, Window &out_window, const ITensor *in, ITensor *out, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100609 {
Luca Foschianiee939fb2020-01-28 10:38:07 +0000610 using PromotedType = typename wrapper::traits::promote<typename wrapper::traits::promote<T>::type>::type;
611
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100612 const TensorInfo in_info = *(in->info());
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100613 const UniformQuantizationInfo iq_info = in_info.quantization_info().uniform();
614
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100615 const int window_step_x = 16 / sizeof(T);
616 const auto window_start_x = static_cast<int>(in_window.x().start());
617 const auto window_end_x = static_cast<int>(in_window.x().end());
618
Georgios Pinitas412b7892020-11-11 21:05:24 +0000619 Window in_win_no_pad = in_window;
620 in_win_no_pad.set(Window::DimX, Window::Dimension(0, 1, 1));
621
622 Iterator input(in, in_win_no_pad);
623 Iterator output(out, out_window);
624
625 execute_window_loop(in_win_no_pad, [&](const Coordinates &)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100626 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100627 const auto input_ptr = reinterpret_cast<T *>(input.ptr());
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000628
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100629 auto vec_res_value1 = wrapper::vdup_n(static_cast<PromotedType>(0.f), wrapper::traits::vector_128_tag{});
630 auto vec_res_value2 = wrapper::vdup_n(static_cast<PromotedType>(0.f), wrapper::traits::vector_128_tag{});
631 auto vec_res_value3 = wrapper::vdup_n(static_cast<PromotedType>(0.f), wrapper::traits::vector_128_tag{});
632 auto vec_res_value4 = wrapper::vdup_n(static_cast<PromotedType>(0.f), wrapper::traits::vector_128_tag{});
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000633
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100634 auto vec_res_value1_f = vdupq_n_f32(static_cast<float>(1.f));
635 auto vec_res_value2_f = vdupq_n_f32(static_cast<float>(1.f));
636 auto vec_res_value3_f = vdupq_n_f32(static_cast<float>(1.f));
637 auto vec_res_value4_f = vdupq_n_f32(static_cast<float>(1.f));
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000638
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100639 typename wrapper::traits::neon_vector<T, 16>::type vec_res_value = { 0 };
640
641 if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::MIN || op == ReductionOperation::MAX)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100642 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100643 vec_res_value = wrapper::vdup_n(*input_ptr, wrapper::traits::vector_128_tag{});
644 }
645
646 uint32x4x4_t vec_res_idx{ { 0 } };
647 // Compute window_step_x elements per iteration
648 int x = window_start_x;
649 for(; x <= (window_end_x - window_step_x); x += window_step_x)
650 {
Pablo Marquez Telloc4c595a2021-05-04 17:23:09 +0100651 const auto vec_elements = wrapper::vloadq(input_ptr + x);
652 switch(op)
653 {
654 case ReductionOperation::SUM:
655 case ReductionOperation::MEAN_SUM:
656 {
657 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
658 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100659
Pablo Marquez Telloc4c595a2021-05-04 17:23:09 +0100660 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
661 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
662 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
663 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
664
665 vec_res_value1 = wrapper::vadd(temp32x4t_1, vec_res_value1);
666 vec_res_value2 = wrapper::vadd(temp32x4t_2, vec_res_value2);
667 vec_res_value3 = wrapper::vadd(temp32x4t_3, vec_res_value3);
668 vec_res_value4 = wrapper::vadd(temp32x4t_4, vec_res_value4);
669 break;
670 }
671 case ReductionOperation::PROD:
672 {
673 const auto offset32x4f_4 = vdupq_n_f32(iq_info.offset);
674 const auto scale32x4f_4 = vdupq_n_f32(iq_info.scale);
675
676 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
677 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
678
679 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
680 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
681 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
682 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
683
684 auto temp32x4f_1 = wrapper::vcvt<float>(temp32x4t_1);
685 auto temp32x4f_2 = wrapper::vcvt<float>(temp32x4t_2);
686 auto temp32x4f_3 = wrapper::vcvt<float>(temp32x4t_3);
687 auto temp32x4f_4 = wrapper::vcvt<float>(temp32x4t_4);
688
689 //de-quantize vec_elements
690 temp32x4f_1 = vmulq_f32(vsubq_f32(temp32x4f_1, offset32x4f_4), scale32x4f_4);
691 temp32x4f_2 = vmulq_f32(vsubq_f32(temp32x4f_2, offset32x4f_4), scale32x4f_4);
692 temp32x4f_3 = vmulq_f32(vsubq_f32(temp32x4f_3, offset32x4f_4), scale32x4f_4);
693 temp32x4f_4 = vmulq_f32(vsubq_f32(temp32x4f_4, offset32x4f_4), scale32x4f_4);
694
695 vec_res_value1_f = vmulq_f32(temp32x4f_1, vec_res_value1_f);
696 vec_res_value2_f = vmulq_f32(temp32x4f_2, vec_res_value2_f);
697 vec_res_value3_f = vmulq_f32(temp32x4f_3, vec_res_value3_f);
698 vec_res_value4_f = vmulq_f32(temp32x4f_4, vec_res_value4_f);
699 break;
700 }
701 case ReductionOperation::ARG_IDX_MIN:
702 {
703 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
704 vec_res_idx = calculate_index_quantized<decltype(vec_res_value)>(x, temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
705 vec_res_value = temp_vec_res_value;
706 break;
707 }
708 case ReductionOperation::ARG_IDX_MAX:
709 {
710 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
711 vec_res_idx = calculate_index_quantized<decltype(vec_res_value)>(x, temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
712 vec_res_value = temp_vec_res_value;
713 break;
714 }
715 case ReductionOperation::MIN:
716 {
717 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
718 break;
719 }
720 case ReductionOperation::MAX:
721 {
722 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
723 break;
724 }
725 default:
726 ARM_COMPUTE_ERROR("Not supported");
727 }
728 }
729
730 switch(op)
731 {
732 case ReductionOperation::ARG_IDX_MIN:
733 {
734 auto idx = calculate_vector_index_quantized<decltype(vec_res_value)>(vec_res_idx, vec_res_value, op);
735 auto res = static_cast<T>(wrapper::vgetlane(calculate_min(vec_res_value), 0));
736
737 // Compute left-over elements
738 for(; x < window_end_x; ++x)
739 {
740 if(*(input_ptr + x) < res)
741 {
742 idx = x;
743 res = *(input_ptr + x);
744 }
745 }
746 *(reinterpret_cast<uint32_t *>(output.ptr())) = idx;
747 break;
748 }
749 case ReductionOperation::ARG_IDX_MAX:
750 {
751 auto idx = calculate_vector_index_quantized<decltype(vec_res_value)>(vec_res_idx, vec_res_value, op);
752 auto res = static_cast<T>(wrapper::vgetlane(calculate_max(vec_res_value), 0));
753
754 // Compute left-over elements
755 for(; x < window_end_x; ++x)
756 {
757 if(*(input_ptr + x) > res)
758 {
759 idx = x;
760 res = *(input_ptr + x);
761 }
762 }
763 *(reinterpret_cast<uint32_t *>(output.ptr())) = idx;
764 break;
765 }
766 case ReductionOperation::MIN:
767 {
768 auto res = static_cast<T>(wrapper::vgetlane(calculate_min(vec_res_value), 0));
769
770 // Compute left-over elements
771 for(; x < window_end_x; ++x)
772 {
773 res = *(input_ptr + x) < res ? *(input_ptr + x) : res;
774 }
775 *(reinterpret_cast<T *>(output.ptr())) = res;
776 break;
777 }
778 case ReductionOperation::MAX:
779 {
780 auto res = static_cast<T>(wrapper::vgetlane(calculate_max(vec_res_value), 0));
781
782 // Compute left-over elements
783 for(; x < window_end_x; ++x)
784 {
785 res = *(input_ptr + x) > res ? *(input_ptr + x) : res;
786 }
787 *(reinterpret_cast<T *>(output.ptr())) = res;
788 break;
789 }
790 case ReductionOperation::PROD:
791 {
792 auto carry_res = wrapper::vmul(vec_res_value1_f, vec_res_value2_f);
793 carry_res = wrapper::vmul(carry_res, vec_res_value3_f);
794 carry_res = wrapper::vmul(carry_res, vec_res_value4_f);
795
796 float res = wrapper::vgetlane(carry_res, 0);
797 res *= wrapper::vgetlane(carry_res, 1);
798 res *= wrapper::vgetlane(carry_res, 2);
799 res *= wrapper::vgetlane(carry_res, 3);
800
801 // Compute left-over elements
802 for(; x < window_end_x; ++x)
803 {
804 //de-quantize input
805 if(std::is_same<T, uint8_t>::value)
806 {
807 res *= dequantize_qasymm8(*(input_ptr + x), iq_info);
808 }
809 else
810 {
811 res *= dequantize_qasymm8_signed(*(input_ptr + x), iq_info);
812 }
813 }
814
815 //re-quantize result
816 if(std::is_same<T, uint8_t>::value)
817 {
818 res = quantize_qasymm8(res, iq_info);
819 }
820 else
821 {
822 res = quantize_qasymm8_signed(res, iq_info);
823 }
824
825 *reinterpret_cast<T *>(output.ptr()) = static_cast<T>(res);
826 break;
827 }
828 case ReductionOperation::SUM:
829 case ReductionOperation::MEAN_SUM:
830 {
831 auto carry_res = wrapper::vadd(vec_res_value1, vec_res_value2);
832 carry_res = wrapper::vadd(carry_res, vec_res_value3);
833 carry_res = wrapper::vadd(carry_res, vec_res_value4);
834
835 auto carry_paddition = wrapper::vpadd(wrapper::vgethigh(carry_res), wrapper::vgetlow(carry_res));
836 carry_paddition = wrapper::vpadd(carry_paddition, carry_paddition);
837 auto res = static_cast<int32_t>(wrapper::vgetlane(carry_paddition, 0));
838
839 // Compute left-over elements
840 for(; x < window_end_x; ++x)
841 {
842 res += *(input_ptr + x);
843 }
844
845 if(op == ReductionOperation::MEAN_SUM)
846 {
847 res /= static_cast<int32_t>(in_info.dimension(0));
848 }
849 else
850 {
851 // Subtract accumulated offsets
852 res -= (in_info.dimension(0) - 1) * iq_info.offset;
853 }
854 *reinterpret_cast<T *>(output.ptr()) = utils::cast::saturate_cast<T>(res);
855 break;
856 }
857 default:
858 ARM_COMPUTE_ERROR("Not supported");
859 }
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100860 },
861 input, output);
862 }
863};
864
865template <typename T, int S>
866struct RedOpYZW
867{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000868 /** SIMD vector tag type. */
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100869 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
870 using neon_vector = typename wrapper::traits::neon_vector<T, S>::type;
871
872 inline void operator()(const Window &in_window, Window &out_window, const ITensor *in, ITensor *out, int axis, const ReductionOperation op)
873 {
Georgios Pinitas412b7892020-11-11 21:05:24 +0000874 const TensorInfo in_info = *(in->info());
875 const int window_step_x = 16 / sizeof(T);
876 const auto window_start_x_tmp = static_cast<int>(in_window.x().start());
877 const auto window_end_x_tmp = static_cast<int>(in_window.x().end());
878 // As it split over x-axis, need to set the correct spiltted window start and end.
879 const auto window_start_x = static_cast<int>(0);
880 const auto window_end_x = static_cast<int>(in_window.shape().x());
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100881
Georgios Pinitas412b7892020-11-11 21:05:24 +0000882 Window in_win_no_pad = in_window;
883 in_win_no_pad.set(Window::DimX, Window::Dimension(window_start_x_tmp, window_end_x_tmp, in_window.shape().x()));
884 Window out_win_no_pad = out_window;
885 out_win_no_pad.set(Window::DimX, Window::Dimension(window_start_x_tmp, window_end_x_tmp, out_window.shape().x()));
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100886
Georgios Pinitas412b7892020-11-11 21:05:24 +0000887 Iterator input(in, in_win_no_pad);
888 Iterator output(out, out_win_no_pad);
889
890 execute_window_loop(in_win_no_pad, [&](const Coordinates &)
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100891 {
892 const auto input_ptr = reinterpret_cast<T *>(input.ptr());
893
894 // Compute window_step_x elements per iteration
895 int x = window_start_x;
896 for(; x <= (window_end_x - window_step_x); x += window_step_x)
897 {
898 neon_vector vec_res_value = { 0 };
899 switch(op)
900 {
901 case ReductionOperation::ARG_IDX_MAX:
902 case ReductionOperation::ARG_IDX_MIN:
903 case ReductionOperation::MIN:
904 case ReductionOperation::MAX:
905 {
906 vec_res_value = wrapper::vloadq(input_ptr + x);
907 break;
908 }
909 case ReductionOperation::PROD:
910 {
911 vec_res_value = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{});
912 break;
913 }
914 default:
915 {
916 vec_res_value = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
917 break;
918 }
919 }
920 uint32x4x4_t vec_res_idx{ { 0 } };
921
922 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
923 {
924 const T *in_ptr = reinterpret_cast<T *>(input.ptr() + x * sizeof(T) + in_info.strides_in_bytes()[axis] * dim);
925 const auto vec_elements = wrapper::vloadq(in_ptr);
926 switch(op)
927 {
928 case ReductionOperation::SUM:
929 case ReductionOperation::MEAN_SUM:
930 vec_res_value = wrapper::vadd(vec_elements, vec_res_value);
931 break;
932 case ReductionOperation::SUM_SQUARE:
933 vec_res_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_res_value);
934 break;
935 case ReductionOperation::PROD:
936 vec_res_value = wrapper::vmul(vec_elements, vec_res_value);
937 break;
938 case ReductionOperation::ARG_IDX_MIN:
939 {
940 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
941 vec_res_idx = calculate_index(dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
942 vec_res_value = temp_vec_res_value;
943 break;
944 }
945 case ReductionOperation::ARG_IDX_MAX:
946 {
947 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
948 vec_res_idx = calculate_index(dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
949 vec_res_value = temp_vec_res_value;
950 break;
951 }
952 case ReductionOperation::MIN:
953 {
954 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
955 break;
956 }
957 case ReductionOperation::MAX:
958 {
959 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
960 break;
961 }
962 default:
963 ARM_COMPUTE_ERROR("Not supported");
964 }
965 }
966
967 if(op == ReductionOperation::MEAN_SUM)
968 {
969 auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast<T>(in_info.dimension(axis)), ExactTagType{}));
970 vec_res_value = wrapper::vmul(vec_res_value, vec_width_inv);
971 }
972
973 if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX)
974 {
975 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + x, vec_res_idx.val[0]);
976#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
977 if(std::is_same<T, float16_t>::value)
978 {
979 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + x + 4, vec_res_idx.val[1]);
980 }
981#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000982 }
983 else
984 {
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100985 wrapper::vstore(reinterpret_cast<T *>(output.ptr() + x * sizeof(T)), vec_res_value);
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000986 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100987 }
988
Sheri Zhang4d91dc62020-09-23 11:22:50 +0100989 // Compute left-over elements
990 for(; x < window_end_x; ++x)
991 {
992 auto res_value = 0.f;
993 switch(op)
994 {
995 case ReductionOperation::ARG_IDX_MAX:
996 case ReductionOperation::ARG_IDX_MIN:
997 case ReductionOperation::MIN:
998 case ReductionOperation::MAX:
999 {
1000 res_value = *(input_ptr + x);
1001 break;
1002 }
1003 case ReductionOperation::PROD:
1004 {
1005 res_value = static_cast<T>(1.f);
1006 break;
1007 }
1008 default:
1009 {
1010 res_value = static_cast<T>(0.f);
1011 break;
1012 }
1013 }
1014
1015 uint32_t res_idx = 0;
1016 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
1017 {
1018 const T *in_ptr = reinterpret_cast<T *>(input.ptr() + x * sizeof(T) + in_info.strides_in_bytes()[axis] * dim);
1019
1020 switch(op)
1021 {
1022 case ReductionOperation::SUM:
1023 case ReductionOperation::MEAN_SUM:
1024 res_value += *in_ptr;
1025 break;
1026 case ReductionOperation::SUM_SQUARE:
1027 res_value += *in_ptr * *in_ptr;
1028 break;
1029 case ReductionOperation::PROD:
1030 res_value *= *in_ptr;
1031 break;
1032 case ReductionOperation::ARG_IDX_MIN:
1033 {
1034 if(*in_ptr < res_value)
1035 {
1036 res_value = *in_ptr;
1037 res_idx = dim;
1038 }
1039 break;
1040 }
1041 case ReductionOperation::ARG_IDX_MAX:
1042 {
1043 if(*in_ptr > res_value)
1044 {
1045 res_value = *in_ptr;
1046 res_idx = dim;
1047 }
1048 break;
1049 }
1050 case ReductionOperation::MIN:
1051 {
1052 res_value = *in_ptr < res_value ? *in_ptr : res_value;
1053 break;
1054 }
1055 case ReductionOperation::MAX:
1056 {
1057 res_value = *in_ptr > res_value ? *in_ptr : res_value;
1058 break;
1059 }
1060 default:
1061 ARM_COMPUTE_ERROR("Not supported");
1062 }
1063 }
1064
1065 if(op == ReductionOperation::MEAN_SUM)
1066 {
1067 res_value /= in_info.dimension(axis);
1068 }
1069
1070 if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX)
1071 {
1072 *(reinterpret_cast<uint32_t *>(output.ptr()) + x) = res_idx;
1073 }
1074 else
1075 {
1076 *(reinterpret_cast<T *>(output.ptr() + x * sizeof(T))) = res_value;
1077 }
1078 }
1079 },
1080 input, output);
1081 }
1082};
1083
1084template <typename T, int S, int axis, ReductionOperation op>
1085struct RedOpYZW_complex
1086{
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +00001087 /** SIMD vector tag type. */
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001088 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
1089 using neon_vector = typename wrapper::traits::neon_vector<T, S>::type;
1090
1091 inline void operator()(const Window &in_window, Window &out_window, const ITensor *in, ITensor *out, int, const ReductionOperation)
1092 {
1093 ARM_COMPUTE_ERROR_ON(axis != 2);
Georgios Pinitas412b7892020-11-11 21:05:24 +00001094 ARM_COMPUTE_ERROR_ON(op != ReductionOperation::SUM);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001095
Georgios Pinitas412b7892020-11-11 21:05:24 +00001096 const TensorInfo in_info = *(in->info());
1097 const size_t stride_z = in_info.strides_in_bytes()[axis];
1098 const int window_step_x = 16 / sizeof(T);
1099 const auto window_start_x_tmp = static_cast<int>(in_window.x().start());
1100 const auto window_end_x_tmp = static_cast<int>(in_window.x().end());
1101 // As it split over x-axis, need to set the correct spiltted window start and end.
1102 const auto window_start_x = static_cast<int>(0);
1103 const auto window_end_x = static_cast<int>(in_window.shape().x());
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001104
Georgios Pinitas412b7892020-11-11 21:05:24 +00001105 Window in_win_no_pad = in_window;
1106 in_win_no_pad.set(Window::DimX, Window::Dimension(window_start_x_tmp, window_end_x_tmp, in_window.shape().x()));
1107 Window out_win_no_pad = out_window;
1108 out_win_no_pad.set(Window::DimX, Window::Dimension(window_start_x_tmp, window_end_x_tmp, out_window.shape().x()));
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001109
Georgios Pinitas412b7892020-11-11 21:05:24 +00001110 Iterator input(in, in_win_no_pad);
1111 Iterator output(out, out_win_no_pad);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001112
Georgios Pinitas412b7892020-11-11 21:05:24 +00001113 execute_window_loop(in_win_no_pad, [&](const Coordinates &)
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001114 {
1115 // Compute window_step_x elements per iteration
1116 int x = window_start_x;
1117 for(; x <= (window_end_x - window_step_x); x += window_step_x)
1118 {
1119 neon_vector vec_res_value_0 = { 0 };
1120 neon_vector vec_res_value_1 = { 0 };
1121
1122 vec_res_value_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
1123 vec_res_value_1 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
1124
1125 T *out_ptr = reinterpret_cast<T *>(output.ptr() + 2 * x * sizeof(T));
1126 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
1127 {
Georgios Pinitas412b7892020-11-11 21:05:24 +00001128 T *in_ptr_0 = reinterpret_cast<T *>(input.ptr() + 2 * x * sizeof(T) + stride_z * dim);
1129 T *in_ptr_1 = reinterpret_cast<T *>(input.ptr() + 2 * x * sizeof(T) + 16 + stride_z * dim);
1130
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001131 const auto vec_elements_0 = wrapper::vloadq(in_ptr_0);
1132 const auto vec_elements_1 = wrapper::vloadq(in_ptr_1);
1133
Georgios Pinitas412b7892020-11-11 21:05:24 +00001134 vec_res_value_0 = wrapper::vadd(vec_elements_0, vec_res_value_0);
1135 vec_res_value_1 = wrapper::vadd(vec_elements_1, vec_res_value_1);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001136 }
1137
1138 wrapper::vstore(out_ptr, vec_res_value_0);
1139 wrapper::vstore(out_ptr + 4, vec_res_value_1);
1140 }
1141
1142 // Compute left-over elements
1143 for(; x < window_end_x; ++x)
1144 {
1145 auto res_value_0 = 0.f;
1146 auto res_value_1 = 0.f;
1147
1148 T *out_ptr = reinterpret_cast<T *>(output.ptr() + 2 * x * sizeof(T));
1149 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
1150 {
Georgios Pinitas412b7892020-11-11 21:05:24 +00001151 T *in_ptr = reinterpret_cast<T *>(input.ptr() + 2 * x * sizeof(T) + stride_z * dim);
1152 res_value_0 += *in_ptr;
1153 res_value_1 += *(in_ptr + 1);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001154 }
1155 *out_ptr = res_value_0;
1156 *(out_ptr + 1) = res_value_1;
1157 }
1158 },
1159 input, output);
1160 }
1161};
1162
1163template <typename T>
1164struct RedOpYZW_quantized
1165{
1166 inline void operator()(const Window &in_window, Window &out_window, const ITensor *in, ITensor *out, int axis, const ReductionOperation op)
1167 {
Georgios Pinitas412b7892020-11-11 21:05:24 +00001168 const TensorInfo in_info = *(in->info());
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001169 const UniformQuantizationInfo iq_info = in_info.quantization_info().uniform();
Georgios Pinitas412b7892020-11-11 21:05:24 +00001170 using PromotedType = typename wrapper::traits::promote<typename wrapper::traits::promote<T>::type>::type;
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001171
Georgios Pinitas412b7892020-11-11 21:05:24 +00001172 const int window_step_x = 16 / sizeof(T);
1173 const auto window_start_x_tmp = static_cast<int>(in_window.x().start());
1174 const auto window_end_x_tmp = static_cast<int>(in_window.x().end());
1175 // As it split over x-axis, need to set the correct spiltted window start and end.
1176 const auto window_start_x = static_cast<int>(0);
1177 const auto window_end_x = static_cast<int>(in_window.shape().x());
1178
1179 Window in_win_no_pad = in_window;
1180 in_win_no_pad.set(Window::DimX, Window::Dimension(window_start_x_tmp, window_end_x_tmp, in_window.shape().x()));
1181 Window out_win_no_pad = out_window;
1182 out_win_no_pad.set(Window::DimX, Window::Dimension(window_start_x_tmp, window_end_x_tmp, out_window.shape().x()));
1183
1184 Iterator input(in, in_win_no_pad);
1185 Iterator output(out, out_win_no_pad);
1186
Pablo Marquez Telloc4c595a2021-05-04 17:23:09 +01001187 using vector_type = typename wrapper::traits::neon_bitvector<PromotedType, wrapper::traits::BitWidth::W128>::type;
1188 using vector_type_f = typename wrapper::traits::neon_vector<float, 4>::type;
1189
1190 vector_type vec_res_value1{};
1191 vector_type vec_res_value2{};
1192 vector_type vec_res_value3{};
1193 vector_type vec_res_value4{};
1194
1195 vector_type_f vec_res_value1_f{};
1196 vector_type_f vec_res_value2_f{};
1197 vector_type_f vec_res_value3_f{};
1198 vector_type_f vec_res_value4_f{};
1199
Georgios Pinitas412b7892020-11-11 21:05:24 +00001200 execute_window_loop(in_win_no_pad, [&](const Coordinates &)
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001201 {
1202 const auto input_ptr = reinterpret_cast<T *>(input.ptr());
1203
1204 // Compute window_step_x elements per iteration
1205 int x = window_start_x;
1206 for(; x <= (window_end_x - window_step_x); x += window_step_x)
1207 {
1208 uint32x4x4_t vec_res_idx{ { 0 } };
Pablo Marquez Telloc4c595a2021-05-04 17:23:09 +01001209 vec_res_value1 = wrapper::vdup_n(static_cast<PromotedType>(0), wrapper::traits::vector_128_tag{});
1210 vec_res_value2 = wrapper::vdup_n(static_cast<PromotedType>(0), wrapper::traits::vector_128_tag{});
1211 vec_res_value3 = wrapper::vdup_n(static_cast<PromotedType>(0), wrapper::traits::vector_128_tag{});
1212 vec_res_value4 = wrapper::vdup_n(static_cast<PromotedType>(0), wrapper::traits::vector_128_tag{});
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001213
Pablo Marquez Telloc4c595a2021-05-04 17:23:09 +01001214 vec_res_value1_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
1215 vec_res_value2_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
1216 vec_res_value3_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
1217 vec_res_value4_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001218
1219 auto vec_res_value = wrapper::vloadq(input_ptr + x);
1220
1221 for(unsigned int index_dim = 0; index_dim < in_info.dimension(axis); ++index_dim)
1222 {
1223 const T *in_ptr = input_ptr + x + in_info.strides_in_bytes()[axis] * index_dim;
1224 const auto vec_elements = wrapper::vloadq(in_ptr);
1225 switch(op)
1226 {
1227 case ReductionOperation::SUM:
1228 case ReductionOperation::MEAN_SUM:
1229 {
1230 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
1231 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
1232
1233 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
1234 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
1235 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
1236 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
1237
1238 vec_res_value1 = wrapper::vadd(temp32x4t_1, vec_res_value1);
1239 vec_res_value2 = wrapper::vadd(temp32x4t_2, vec_res_value2);
1240 vec_res_value3 = wrapper::vadd(temp32x4t_3, vec_res_value3);
1241 vec_res_value4 = wrapper::vadd(temp32x4t_4, vec_res_value4);
1242 break;
1243 }
1244 case ReductionOperation::PROD:
1245 {
1246 const auto offset32x4f_4 = wrapper::vdup_n(static_cast<float>(iq_info.offset), wrapper::traits::vector_128_tag{});
1247 const auto scale32x4f_4 = wrapper::vdup_n(iq_info.scale, wrapper::traits::vector_128_tag{});
1248
1249 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
1250 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
1251
1252 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
1253 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
1254 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
1255 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
1256
1257 auto temp32x4f_1 = wrapper::vcvt<float>(temp32x4t_1);
1258 auto temp32x4f_2 = wrapper::vcvt<float>(temp32x4t_2);
1259 auto temp32x4f_3 = wrapper::vcvt<float>(temp32x4t_3);
1260 auto temp32x4f_4 = wrapper::vcvt<float>(temp32x4t_4);
1261
1262 //de-quantize vec_elements
1263 temp32x4f_1 = wrapper::vmul(wrapper::vsub(temp32x4f_1, offset32x4f_4), scale32x4f_4);
1264 temp32x4f_2 = wrapper::vmul(wrapper::vsub(temp32x4f_2, offset32x4f_4), scale32x4f_4);
1265 temp32x4f_3 = wrapper::vmul(wrapper::vsub(temp32x4f_3, offset32x4f_4), scale32x4f_4);
1266 temp32x4f_4 = wrapper::vmul(wrapper::vsub(temp32x4f_4, offset32x4f_4), scale32x4f_4);
1267
1268 vec_res_value1_f = wrapper::vmul(temp32x4f_1, vec_res_value1_f);
1269 vec_res_value2_f = wrapper::vmul(temp32x4f_2, vec_res_value2_f);
1270 vec_res_value3_f = wrapper::vmul(temp32x4f_3, vec_res_value3_f);
1271 vec_res_value4_f = wrapper::vmul(temp32x4f_4, vec_res_value4_f);
1272 break;
1273 }
1274 case ReductionOperation::ARG_IDX_MIN:
1275 {
1276 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
1277 vec_res_idx = calculate_index_quantized(index_dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
1278 vec_res_value = temp_vec_res_value;
1279 break;
1280 }
1281 case ReductionOperation::ARG_IDX_MAX:
1282 {
1283 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
1284 vec_res_idx = calculate_index_quantized(index_dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
1285 vec_res_value = temp_vec_res_value;
1286 break;
1287 }
1288 case ReductionOperation::MIN:
1289 {
1290 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
1291 break;
1292 }
1293 case ReductionOperation::MAX:
1294 {
1295 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
1296 break;
1297 }
1298 default:
1299 ARM_COMPUTE_ERROR("Not supported");
1300 }
1301 }
1302
1303 switch(op)
1304 {
1305 case ReductionOperation::ARG_IDX_MIN:
1306 case ReductionOperation::ARG_IDX_MAX:
1307 {
1308 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr() + 4 * x), vec_res_idx.val[0]);
1309 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr() + 4 * x) + 4, vec_res_idx.val[1]);
1310 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr() + 4 * x) + 8, vec_res_idx.val[2]);
1311 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr() + 4 * x) + 12, vec_res_idx.val[3]);
1312 break;
1313 }
1314 case ReductionOperation::MIN:
1315 case ReductionOperation::MAX:
1316 {
1317 wrapper::vstore(reinterpret_cast<T *>(output.ptr() + x), vec_res_value);
1318 break;
1319 }
1320 case ReductionOperation::SUM:
1321 {
1322 // Subtract offsets
1323 auto offsets = vdupq_n_s32((in_info.dimension(axis) - 1) * iq_info.offset);
1324
1325 auto vec_res_s_value1 = wrapper::vreinterpret(vec_res_value1);
1326 auto vec_res_s_value2 = wrapper::vreinterpret(vec_res_value2);
1327 auto vec_res_s_value3 = wrapper::vreinterpret(vec_res_value3);
1328 auto vec_res_s_value4 = wrapper::vreinterpret(vec_res_value4);
1329
1330 vec_res_s_value1 = wrapper::vsub(vec_res_s_value1, offsets);
1331 vec_res_s_value2 = wrapper::vsub(vec_res_s_value2, offsets);
1332 vec_res_s_value3 = wrapper::vsub(vec_res_s_value3, offsets);
1333 vec_res_s_value4 = wrapper::vsub(vec_res_s_value4, offsets);
1334
1335 const auto temp16x8t_1 = wrapper::vcombine(wrapper::vqmovn(vec_res_s_value1), wrapper::vqmovn(vec_res_s_value2));
1336 const auto temp16x8t_2 = wrapper::vcombine(wrapper::vqmovn(vec_res_s_value3), wrapper::vqmovn(vec_res_s_value4));
1337
1338 combine_and_store<T>(temp16x8t_1, temp16x8t_2, output, x);
1339 break;
1340 }
1341 case ReductionOperation::MEAN_SUM:
1342 {
1343 const auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast<float>(in_info.dimension(axis)), wrapper::traits::vector_128_tag{}));
1344 vec_res_value1_f = wrapper::vmul(wrapper::vcvt<float>(vec_res_value1), vec_width_inv);
1345 vec_res_value2_f = wrapper::vmul(wrapper::vcvt<float>(vec_res_value2), vec_width_inv);
1346 vec_res_value3_f = wrapper::vmul(wrapper::vcvt<float>(vec_res_value3), vec_width_inv);
1347 vec_res_value4_f = wrapper::vmul(wrapper::vcvt<float>(vec_res_value4), vec_width_inv);
1348
1349 vec_res_value1 = wrapper::vcvt<T>(vec_res_value1_f);
1350 vec_res_value2 = wrapper::vcvt<T>(vec_res_value2_f);
1351 vec_res_value3 = wrapper::vcvt<T>(vec_res_value3_f);
1352 vec_res_value4 = wrapper::vcvt<T>(vec_res_value4_f);
1353
1354 const auto temp16x8t_1 = wrapper::vcombine(wrapper::vqmovn(vec_res_value1), wrapper::vqmovn(vec_res_value2));
1355 const auto temp16x8t_2 = wrapper::vcombine(wrapper::vqmovn(vec_res_value3), wrapper::vqmovn(vec_res_value4));
1356 auto res = wrapper::vcombine(wrapper::vqmovn(temp16x8t_1), wrapper::vqmovn(temp16x8t_2));
1357
1358 wrapper::vstore(reinterpret_cast<T *>(output.ptr() + x), res);
1359 break;
1360 }
1361 case ReductionOperation::PROD:
1362 {
1363 const auto offset32x4f_4 = wrapper::vdup_n(static_cast<float>(iq_info.offset), wrapper::traits::vector_128_tag{});
1364 const auto iscale32x4f_4 = vinvq_f32(vdupq_n_f32(iq_info.scale));
1365
1366 //re-quantize
1367 vec_res_value1_f = wrapper::vadd(wrapper::vmul(vec_res_value1_f, iscale32x4f_4), offset32x4f_4);
1368 vec_res_value2_f = wrapper::vadd(wrapper::vmul(vec_res_value2_f, iscale32x4f_4), offset32x4f_4);
1369 vec_res_value3_f = wrapper::vadd(wrapper::vmul(vec_res_value3_f, iscale32x4f_4), offset32x4f_4);
1370 vec_res_value4_f = wrapper::vadd(wrapper::vmul(vec_res_value4_f, iscale32x4f_4), offset32x4f_4);
1371
1372 vec_res_value1 = wrapper::vcvt<T>(vec_res_value1_f);
1373 vec_res_value2 = wrapper::vcvt<T>(vec_res_value2_f);
1374 vec_res_value3 = wrapper::vcvt<T>(vec_res_value3_f);
1375 vec_res_value4 = wrapper::vcvt<T>(vec_res_value4_f);
1376
1377 const auto temp16x8t_1 = wrapper::vcombine(wrapper::vqmovn(vec_res_value1), wrapper::vqmovn(vec_res_value2));
1378 const auto temp16x8t_2 = wrapper::vcombine(wrapper::vqmovn(vec_res_value3), wrapper::vqmovn(vec_res_value4));
1379 auto res = wrapper::vcombine(wrapper::vqmovn(temp16x8t_1), wrapper::vqmovn(temp16x8t_2));
1380
1381 wrapper::vstore(reinterpret_cast<T *>(output.ptr() + x), res);
1382 break;
1383 }
1384 default:
1385 ARM_COMPUTE_ERROR("Not supported");
1386 }
1387 }
1388
1389 // Compute left-over elements
1390 for(; x < window_end_x; ++x)
1391 {
Michalis Spyrou272e4252020-10-06 17:44:40 +01001392 float res_value = 0.f;
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001393 switch(op)
1394 {
1395 case ReductionOperation::ARG_IDX_MAX:
1396 case ReductionOperation::ARG_IDX_MIN:
1397 case ReductionOperation::MIN:
1398 case ReductionOperation::MAX:
1399 {
1400 res_value = *(input_ptr + x);
1401 break;
1402 }
1403 case ReductionOperation::PROD:
1404 {
1405 res_value = static_cast<T>(1.0f);
1406 break;
1407 }
1408 default:
1409 {
1410 res_value = static_cast<T>(0.0f);
1411 break;
1412 }
1413 }
1414 uint32_t res_idx = 0;
1415
1416 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
1417 {
1418 const T *in_ptr = reinterpret_cast<T *>(input.ptr() + x + in_info.strides_in_bytes()[axis] * dim);
1419 switch(op)
1420 {
1421 case ReductionOperation::SUM:
1422 case ReductionOperation::MEAN_SUM:
1423 {
1424 res_value += *in_ptr;
1425 break;
1426 }
1427 case ReductionOperation::SUM_SQUARE:
1428 {
1429 res_value += *in_ptr * *in_ptr;
1430 break;
1431 }
1432 case ReductionOperation::PROD:
1433 {
1434 //de-quantize input
1435 if(std::is_same<T, uint8_t>::value)
1436 {
Michalis Spyrou272e4252020-10-06 17:44:40 +01001437 res_value *= dequantize_qasymm8(*in_ptr, iq_info);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001438 }
1439 else
1440 {
Michalis Spyrou272e4252020-10-06 17:44:40 +01001441 res_value *= dequantize_qasymm8_signed(*in_ptr, iq_info);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001442 }
1443 break;
1444 }
1445 case ReductionOperation::ARG_IDX_MIN:
1446 {
1447 if(*in_ptr < res_value)
1448 {
1449 res_value = *in_ptr;
1450 res_idx = dim;
1451 }
1452 break;
1453 }
1454 case ReductionOperation::ARG_IDX_MAX:
1455 {
1456 if(*in_ptr > res_value)
1457 {
1458 res_value = *in_ptr;
1459 res_idx = dim;
1460 }
1461 break;
1462 }
1463 case ReductionOperation::MIN:
1464 {
1465 res_value = *in_ptr < res_value ? *in_ptr : res_value;
1466 break;
1467 }
1468 case ReductionOperation::MAX:
1469 {
1470 res_value = *in_ptr > res_value ? *in_ptr : res_value;
1471 break;
1472 }
1473 default:
1474 ARM_COMPUTE_ERROR("Not supported");
1475 }
1476 }
1477
1478 switch(op)
1479 {
1480 case ReductionOperation::MEAN_SUM:
1481 {
Michalis Spyrou272e4252020-10-06 17:44:40 +01001482 int32_t res = static_cast<int32_t>(res_value);
Sang-Hoon Parkcbede282020-10-12 21:44:23 +01001483 res /= static_cast<int32_t>(in_info.dimension(axis));
Michalis Spyrou272e4252020-10-06 17:44:40 +01001484 *reinterpret_cast<T *>(output.ptr() + x) = utils::cast::saturate_cast<T>(res);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001485 break;
1486 }
1487 case ReductionOperation::SUM:
1488 {
1489 // Subtract accumulated offsets
1490 res_value -= (in_info.dimension(axis) - 1) * iq_info.offset;
1491 *reinterpret_cast<T *>(output.ptr() + x) = utils::cast::saturate_cast<T>(res_value);
1492 break;
1493 }
1494 case ReductionOperation::PROD:
1495 {
1496 //re-quantize result
Michalis Spyrou272e4252020-10-06 17:44:40 +01001497 T res = 0;
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001498 if(std::is_same<T, uint8_t>::value)
1499 {
Michalis Spyrou272e4252020-10-06 17:44:40 +01001500 res = quantize_qasymm8(res_value, iq_info);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001501 }
1502 else
1503 {
Michalis Spyrou272e4252020-10-06 17:44:40 +01001504 res = quantize_qasymm8_signed(res_value, iq_info);
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001505 }
Michalis Spyrou272e4252020-10-06 17:44:40 +01001506 *(reinterpret_cast<T *>(output.ptr() + x)) = res;
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001507 break;
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001508 }
1509 case ReductionOperation::ARG_IDX_MIN:
1510 case ReductionOperation::ARG_IDX_MAX:
1511 {
1512 *(reinterpret_cast<uint32_t *>(output.ptr() + x * 4)) = res_idx;
1513 break;
1514 }
1515 default:
1516 *(reinterpret_cast<T *>(output.ptr() + x)) = res_value;
1517 }
1518 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001519 },
1520 input, output);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001521 }
1522};
1523
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001524void reduce_op(const Window &window, const ITensor *input, ITensor *output, unsigned int axis, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001525{
giuros01154bc1c2019-03-26 17:44:40 +00001526 const bool is_complex = (input->info()->num_channels() == 2);
1527
1528 if(is_complex)
1529 {
1530 switch(axis)
1531 {
1532 case 2:
1533 switch(input->info()->data_type())
1534 {
1535 case DataType::F32:
1536 switch(op)
1537 {
1538 case ReductionOperation::SUM:
1539 return Reducer<RedOpYZW_complex<float, 4, 2, ReductionOperation::SUM>>::reduceZ(window, input, output, RedOpYZW_complex<float, 4, 2, ReductionOperation::SUM>(), op);
1540 default:
1541 ARM_COMPUTE_ERROR("Not supported");
1542 }
1543 default:
1544 ARM_COMPUTE_ERROR("Not supported");
1545 }
1546 default:
1547 ARM_COMPUTE_ERROR("Not supported");
1548 }
Manuel Bottini6a5eee72021-04-30 12:37:04 +01001549 return;
giuros01154bc1c2019-03-26 17:44:40 +00001550 }
1551
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001552 switch(axis)
1553 {
1554 case 0:
1555 switch(input->info()->data_type())
1556 {
1557 case DataType::QASYMM8:
Luca Foschianiee939fb2020-01-28 10:38:07 +00001558 return Reducer<RedOpX_quantized<uint8_t>>::reduceX(window, input, output, RedOpX_quantized<uint8_t>(), op);
1559 case DataType::QASYMM8_SIGNED:
1560 return Reducer<RedOpX_quantized<int8_t>>::reduceX(window, input, output, RedOpX_quantized<int8_t>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001561#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1562 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001563 return Reducer<RedOpX<float16_t, 8>>::reduceX(window, input, output, RedOpX<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001564#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1565 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001566 return Reducer<RedOpX<float, 4>>::reduceX(window, input, output, RedOpX<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001567 case DataType::S32:
1568 return Reducer<RedOpX<int32_t, 4>>::reduceX(window, input, output, RedOpX<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001569 default:
1570 ARM_COMPUTE_ERROR("Not supported");
1571 }
1572 case 1:
1573 switch(input->info()->data_type())
1574 {
1575 case DataType::QASYMM8:
Luca Foschianiee939fb2020-01-28 10:38:07 +00001576 return Reducer<RedOpYZW_quantized<uint8_t>>::reduceY(window, input, output, RedOpYZW_quantized<uint8_t>(), op);
1577 case DataType::QASYMM8_SIGNED:
1578 return Reducer<RedOpYZW_quantized<int8_t>>::reduceY(window, input, output, RedOpYZW_quantized<int8_t>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001579#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1580 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001581 return Reducer<RedOpYZW<float16_t, 8>>::reduceY(window, input, output, RedOpYZW<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001582#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1583 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001584 return Reducer<RedOpYZW<float, 4>>::reduceY(window, input, output, RedOpYZW<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001585 case DataType::S32:
1586 return Reducer<RedOpYZW<int32_t, 4>>::reduceY(window, input, output, RedOpYZW<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001587 default:
1588 ARM_COMPUTE_ERROR("Not supported");
1589 }
1590 case 2:
1591 switch(input->info()->data_type())
1592 {
1593 case DataType::QASYMM8:
Luca Foschianiee939fb2020-01-28 10:38:07 +00001594 return Reducer<RedOpYZW_quantized<uint8_t>>::reduceZ(window, input, output, RedOpYZW_quantized<uint8_t>(), op);
1595 case DataType::QASYMM8_SIGNED:
1596 return Reducer<RedOpYZW_quantized<int8_t>>::reduceZ(window, input, output, RedOpYZW_quantized<int8_t>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001597#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1598 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001599 return Reducer<RedOpYZW<float16_t, 8>>::reduceZ(window, input, output, RedOpYZW<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001600#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1601 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001602 return Reducer<RedOpYZW<float, 4>>::reduceZ(window, input, output, RedOpYZW<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001603 case DataType::S32:
1604 return Reducer<RedOpYZW<int32_t, 4>>::reduceZ(window, input, output, RedOpYZW<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001605 default:
1606 ARM_COMPUTE_ERROR("Not supported");
1607 }
1608 case 3:
1609 switch(input->info()->data_type())
1610 {
1611 case DataType::QASYMM8:
Luca Foschianiee939fb2020-01-28 10:38:07 +00001612 return Reducer<RedOpYZW_quantized<uint8_t>>::reduceW(window, input, output, RedOpYZW_quantized<uint8_t>(), op);
1613 case DataType::QASYMM8_SIGNED:
1614 return Reducer<RedOpYZW_quantized<int8_t>>::reduceW(window, input, output, RedOpYZW_quantized<int8_t>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001615#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1616 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001617 return Reducer<RedOpYZW<float16_t, 8>>::reduceW(window, input, output, RedOpYZW<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001618#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1619 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001620 return Reducer<RedOpYZW<float, 4>>::reduceW(window, input, output, RedOpYZW<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001621 case DataType::S32:
1622 return Reducer<RedOpYZW<int32_t, 4>>::reduceW(window, input, output, RedOpYZW<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001623 default:
1624 ARM_COMPUTE_ERROR("Not supported");
1625 }
1626 default:
1627 ARM_COMPUTE_ERROR("Unsupported reduction axis");
1628 }
1629}
John Richardson73d4aef2018-05-08 14:34:33 +01001630
1631Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
1632{
1633 ARM_COMPUTE_UNUSED(op);
1634
1635 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +00001636 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
giuros01154bc1c2019-03-26 17:44:40 +00001637
1638 if(input->num_channels() == 1)
1639 {
Luca Foschianiee939fb2020-01-28 10:38:07 +00001640 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8_SIGNED, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32);
giuros01154bc1c2019-03-26 17:44:40 +00001641 }
1642 else
1643 {
1644 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
1645 ARM_COMPUTE_RETURN_ERROR_ON(op != ReductionOperation::SUM);
1646 ARM_COMPUTE_RETURN_ERROR_ON(axis != 2);
1647 }
John Richardson73d4aef2018-05-08 14:34:33 +01001648
1649 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions");
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001650 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
John Richardson73d4aef2018-05-08 14:34:33 +01001651
1652 if(output->total_size() != 0)
1653 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001654 bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN);
1655 if(!is_arg_min_max)
1656 {
1657 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00001658 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
giuros01154bc1c2019-03-26 17:44:40 +00001659 ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() != output->num_channels());
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001660 }
1661 else
1662 {
Michele Di Giorgio9637b2e2019-09-23 16:49:49 +01001663 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001664 }
John Richardson73d4aef2018-05-08 14:34:33 +01001665
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001666 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis);
John Richardson73d4aef2018-05-08 14:34:33 +01001667 const TensorInfo tensor_info_reshaped = input->clone()->set_tensor_shape(output_shape);
1668 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_reshaped);
1669 }
1670
1671 return Status{};
1672}
Georgios Pinitasd9769582017-08-03 10:19:40 +01001673} // namespace
1674
1675NEReductionOperationKernel::NEReductionOperationKernel()
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001676 : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE)
Georgios Pinitasd9769582017-08-03 10:19:40 +01001677{
1678}
1679
Georgios Pinitasd9769582017-08-03 10:19:40 +01001680void NEReductionOperationKernel::configure(const ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op)
1681{
1682 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001683
John Richardson73d4aef2018-05-08 14:34:33 +01001684 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op));
Georgios Pinitasd9769582017-08-03 10:19:40 +01001685
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001686 _input = input;
1687 _output = output;
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001688 _op = op;
1689 _reduction_axis = axis;
Georgios Pinitasd9769582017-08-03 10:19:40 +01001690
1691 // Configure kernel window
Georgios Pinitas412b7892020-11-11 21:05:24 +00001692 Window win = calculate_max_window(*input->info(), Steps());
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001693 INEKernel::configure(win);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001694
Sheri Zhang4d91dc62020-09-23 11:22:50 +01001695 // Calculate output shape and set if empty
1696 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->info()->tensor_shape(), axis);
1697 // Output auto initialization if not yet initialized
1698 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
1699 DataType output_data_type = is_arg_min_max ? DataType::S32 : input->info()->data_type();
1700 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape).set_data_type(output_data_type).reset_padding().set_is_resizable(true));
John Richardson73d4aef2018-05-08 14:34:33 +01001701}
1702
1703Status NEReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
1704{
1705 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
John Richardson73d4aef2018-05-08 14:34:33 +01001706
1707 return Status{};
Georgios Pinitasd9769582017-08-03 10:19:40 +01001708}
1709
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001710void NEReductionOperationKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitasd9769582017-08-03 10:19:40 +01001711{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001712 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001713 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1714 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1715
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001716 reduce_op(window, _input, _output, _reduction_axis, _op);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001717}
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001718} // namespace arm_compute