blob: da82bc2f6f386eed7b68e91ebd6660c6bb5793c9 [file] [log] [blame]
Georgios Pinitasd9769582017-08-03 10:19:40 +01001/*
Pablo Tello07958782020-01-09 14:43:36 +00002 * Copyright (c) 2017-2020 ARM Limited.
Georgios Pinitasd9769582017-08-03 10:19:40 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#include "arm_compute/core/NEON/kernels/NEReductionOperationKernel.h"
25
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000026#include "arm_compute/core/CPP/Validate.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010027#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/IAccessWindow.h"
30#include "arm_compute/core/ITensor.h"
31#include "arm_compute/core/NEON/INEKernel.h"
32#include "arm_compute/core/NEON/NEMath.h"
John Richardson73d4aef2018-05-08 14:34:33 +010033#include "arm_compute/core/TensorInfo.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010034#include "arm_compute/core/Validate.h"
Michalis Spyrou19bd4122020-01-22 10:27:06 +000035#include "arm_compute/core/utils/misc/SaturateCast.h"
Michalis Spyrouaea14c62019-01-03 11:10:25 +000036#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010037
Michalis Spyroubcf8a962018-10-12 10:51:31 +010038#include "arm_compute/core/NEON/wrapper/wrapper.h"
Georgios Pinitasd9769582017-08-03 10:19:40 +010039#include <arm_neon.h>
40
Michalis Spyroubcf8a962018-10-12 10:51:31 +010041namespace arm_compute
42{
Georgios Pinitasd9769582017-08-03 10:19:40 +010043namespace
44{
Michalis Spyroub9626ab2019-05-13 17:41:01 +010045template <typename T>
46uint32x4x4_t calculate_index(uint32_t idx, T a, T b, uint32x4x4_t c, ReductionOperation op, int axis)
Michalis Spyrouaea14c62019-01-03 11:10:25 +000047{
48 uint32x4_t mask{ 0 };
49 if(op == ReductionOperation::ARG_IDX_MIN)
50 {
51 mask = wrapper::vcgt(b, a);
52 }
53 else
54 {
55 mask = wrapper::vclt(b, a);
56 }
57
58 uint32x4_t vec_idx = { idx, idx + 1, idx + 2, idx + 3 };
59 if(axis != 0)
60 {
61 vec_idx = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
62 }
Georgios Pinitasd57891a2019-02-19 18:10:03 +000063 uint32x4x4_t res = { { wrapper::vbsl(mask, vec_idx, c.val[0]), 0, 0, 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +000064
65 return res;
66}
67
Georgios Pinitasfad18382019-06-05 15:12:22 +010068template <>
Michalis Spyrouaea14c62019-01-03 11:10:25 +000069uint32x4x4_t calculate_index(uint32_t idx, uint8x16_t a, uint8x16_t b, uint32x4x4_t c, ReductionOperation op, int axis)
70{
Georgios Pinitasd57891a2019-02-19 18:10:03 +000071 uint32x4x4_t mask{ { 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +000072 uint8x16_t mask_u8{ 0 };
73 if(op == ReductionOperation::ARG_IDX_MIN)
74 {
75 mask_u8 = wrapper::vcgt(b, a);
76 }
77 else
78 {
79 mask_u8 = wrapper::vclt(b, a);
80 }
Michalis Spyrou254a48a2019-01-14 17:27:39 +000081 auto wide_u16_1 = wrapper::vorr(vshll_n_u8(wrapper::vgetlow(mask_u8), 8), wrapper::vmovl(wrapper::vgetlow(mask_u8)));
82 auto wide_u16_2 = wrapper::vorr(vshll_n_u8(wrapper::vgethigh(mask_u8), 8), wrapper::vmovl(wrapper::vgethigh(mask_u8)));
83 mask.val[0] = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_1), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_1)));
84 mask.val[1] = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_1), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_1)));
85 mask.val[2] = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_2), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_2)));
86 mask.val[3] = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_2), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_2)));
87
Michalis Spyrouaea14c62019-01-03 11:10:25 +000088 uint32x4x4_t vec_idx = { { { idx + 0, idx + 1, idx + 2, idx + 3 },
89 { idx + 4, idx + 5, idx + 6, idx + 7 },
90 { idx + 8, idx + 9, idx + 10, idx + 11 },
91 { idx + 12, idx + 13, idx + 14, idx + 15 }
92 }
93 };
94 if(axis != 0)
95 {
96 vec_idx.val[0] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
97 vec_idx.val[1] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
98 vec_idx.val[2] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
99 vec_idx.val[3] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
100 }
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000101 uint32x4x4_t res =
102 {
103 {
104 vbslq_u32(mask.val[0], vec_idx.val[0], c.val[0]),
105 vbslq_u32(mask.val[1], vec_idx.val[1], c.val[1]),
106 vbslq_u32(mask.val[2], vec_idx.val[2], c.val[2]),
107 vbslq_u32(mask.val[3], vec_idx.val[3], c.val[3])
108 }
109 };
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000110
111 return res;
112}
Usama Arifa4a08ad2019-05-20 12:38:33 +0100113
114// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
115float32x2_t calculate_min(float32x4_t in)
116{
117 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
118 return wrapper::vpmin(pmin, pmin);
119}
120
121// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
122float32x2_t calculate_max(float32x4_t in)
123{
124 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
125 return wrapper::vpmax(pmax, pmax);
126}
127// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
128int32x2_t calculate_min(int32x4_t in)
129{
130 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
131 return wrapper::vpmin(pmin, pmin);
132}
133
134// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
135int32x2_t calculate_max(int32x4_t in)
136{
137 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
138 return wrapper::vpmax(pmax, pmax);
139}
140
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100141template <typename T>
142uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, T vec_res_value, ReductionOperation op)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000143{
144 uint32x4_t res_idx_mask{ 0 };
145 uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF);
146
147 if(op == ReductionOperation::ARG_IDX_MIN)
148 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100149 auto pmin = calculate_min(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000150 auto mask = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin));
151 res_idx_mask = wrapper::vand(vec_res_idx.val[0], mask);
152 }
153 else
154 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100155 auto pmax = calculate_max(vec_res_value);
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100156 auto mask = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax));
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000157 res_idx_mask = wrapper::vand(vec_res_idx.val[0], mask);
158 }
159
160 res_idx_mask = wrapper::vadd(res_idx_mask, mask_ones);
161 auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask), wrapper::vgetlow(res_idx_mask));
162 pmin = wrapper::vpmin(pmin, pmin);
163 uint32_t res = wrapper::vgetlane(pmin, 0);
164
165 return (res - 0xFFFFFFFF);
166}
167
Usama Arifa4a08ad2019-05-20 12:38:33 +0100168// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
169inline uint8x8_t calculate_min(uint8x16_t in)
170{
171 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
172 pmin = wrapper::vpmin(pmin, pmin);
173 pmin = wrapper::vpmin(pmin, pmin);
174 return wrapper::vpmin(pmin, pmin);
175}
176// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
177inline uint8x8_t calculate_max(uint8x16_t in)
178{
179 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
180 pmax = wrapper::vpmax(pmax, pmax);
181 pmax = wrapper::vpmax(pmax, pmax);
182 return wrapper::vpmax(pmax, pmax);
183}
184
Usama Arif0a5a57a2019-05-23 14:20:33 +0100185template <>
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000186uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, uint8x16_t vec_res_value, ReductionOperation op)
187{
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000188 uint32x4x4_t res_idx_mask{ { 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000189 uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF);
190 uint8x16_t mask_u8{ 0 };
191 if(op == ReductionOperation::ARG_IDX_MIN)
192 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100193 auto pmin = calculate_min(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000194 mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin));
195 }
196 else
197 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100198 auto pmax = calculate_max(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000199 mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax));
200 }
201
202 // Widen vectors
203 auto wide_u16_1 = wrapper::vorr(vshll_n_u8(wrapper::vgetlow(mask_u8), 8), wrapper::vmovl(wrapper::vgetlow(mask_u8)));
204 auto wide_u16_2 = wrapper::vorr(vshll_n_u8(wrapper::vgethigh(mask_u8), 8), wrapper::vmovl(wrapper::vgethigh(mask_u8)));
205 auto wide_u32_1 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_1), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_1)));
206 auto wide_u32_2 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_1), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_1)));
207 auto wide_u32_3 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_2), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_2)));
208 auto wide_u32_4 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_2), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_2)));
209 res_idx_mask.val[0] = wrapper::vand(vec_res_idx.val[0], wide_u32_1);
210 res_idx_mask.val[1] = wrapper::vand(vec_res_idx.val[1], wide_u32_2);
211 res_idx_mask.val[2] = wrapper::vand(vec_res_idx.val[2], wide_u32_3);
212 res_idx_mask.val[3] = wrapper::vand(vec_res_idx.val[3], wide_u32_4);
213 res_idx_mask.val[0] = wrapper::vadd(res_idx_mask.val[0], mask_ones);
214 res_idx_mask.val[1] = wrapper::vadd(res_idx_mask.val[1], mask_ones);
215 res_idx_mask.val[2] = wrapper::vadd(res_idx_mask.val[2], mask_ones);
216 res_idx_mask.val[3] = wrapper::vadd(res_idx_mask.val[3], mask_ones);
217
218 uint32_t res = 0xFFFFFFFF;
219 int iter = 0;
220 do
221 {
222 auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask.val[iter]), wrapper::vgetlow(res_idx_mask.val[iter]));
223 pmin = wrapper::vpmin(pmin, pmin);
224 res = std::min(wrapper::vgetlane(pmin, 0), res);
225 iter++;
226 }
227 while(iter < 4);
228
229 return (res - 0xFFFFFFFF);
230}
231#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitasfad18382019-06-05 15:12:22 +0100232template <>
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000233uint32x4x4_t calculate_index(uint32_t idx, float16x8_t a, float16x8_t b, uint32x4x4_t c, ReductionOperation op, int axis)
234{
235 uint32x4x2_t mask{ 0 };
236 uint16x8_t mask_u16{ 0 };
237 if(op == ReductionOperation::ARG_IDX_MIN)
238 {
239 mask_u16 = wrapper::vcgt(b, a);
240 }
241 else
242 {
243 mask_u16 = wrapper::vclt(b, a);
244 }
245 mask.val[0] = wrapper::vmovl(wrapper::vgetlow(mask_u16));
246 mask.val[1] = wrapper::vmovl(wrapper::vgethigh(mask_u16));
247 uint32x4x2_t vec_idx = { { { idx + 0, idx + 1, idx + 2, idx + 3 },
248 { idx + 4, idx + 5, idx + 6, idx + 7 }
249 }
250 };
251 if(axis != 0)
252 {
253 vec_idx.val[0] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
254 vec_idx.val[1] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{});
255 }
256 uint32x4x4_t res = { wrapper::vbsl(mask.val[0], vec_idx.val[0], c.val[0]),
257 wrapper::vbsl(mask.val[1], vec_idx.val[1], c.val[1]),
258 0, 0
259 };
260
261 return res;
262}
263
Usama Arifa4a08ad2019-05-20 12:38:33 +0100264// Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value.
265inline float16x4_t calculate_min(float16x8_t in)
266{
267 auto pmin = wrapper::vpmin(wrapper::vgethigh(in), wrapper::vgetlow(in));
268 pmin = wrapper::vpmin(pmin, pmin);
269 return wrapper::vpmin(pmin, pmin);
270}
271// Helper function to calculate the maximum value of the input vector. All the elements in the output vector contain the max value.
272inline float16x4_t calculate_max(float16x8_t in)
273{
274 auto pmax = wrapper::vpmax(wrapper::vgethigh(in), wrapper::vgetlow(in));
275 pmax = wrapper::vpmax(pmax, pmax);
276 return wrapper::vpmax(pmax, pmax);
277}
278
Usama Arif0a5a57a2019-05-23 14:20:33 +0100279template <>
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000280uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, float16x8_t vec_res_value, ReductionOperation op)
281{
282 uint32x4x2_t res_idx_mask{ 0 };
283 uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF);
284 uint16x8_t mask_u16;
285 if(op == ReductionOperation::ARG_IDX_MIN)
286 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100287 auto pmin = calculate_min(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000288 mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin));
289 }
290 else
291 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100292 auto pmax = calculate_max(vec_res_value);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000293 mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax));
294 }
295
296 // Widen vectors
297 auto wide_u32_1 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(mask_u16), 8), wrapper::vmovl(wrapper::vgetlow(mask_u16)));
298 auto wide_u32_2 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(mask_u16), 8), wrapper::vmovl(wrapper::vgethigh(mask_u16)));
299 res_idx_mask.val[0] = wrapper::vand(vec_res_idx.val[0], wide_u32_1);
300 res_idx_mask.val[1] = wrapper::vand(vec_res_idx.val[1], wide_u32_2);
301 res_idx_mask.val[0] = wrapper::vadd(res_idx_mask.val[0], mask_ones);
302 res_idx_mask.val[1] = wrapper::vadd(res_idx_mask.val[1], mask_ones);
303
304 uint32_t res = 0xFFFFFFFF;
305 int iter = 0;
306 do
307 {
308 auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask.val[iter]), wrapper::vgetlow(res_idx_mask.val[iter]));
309 pmin = wrapper::vpmin(pmin, pmin);
310 res = std::min(wrapper::vgetlane(pmin, 0), res);
311 iter++;
312 }
313 while(iter < 2);
314
315 return (res - 0xFFFFFFFF);
316}
317#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
318
Georgios Pinitasd9769582017-08-03 10:19:40 +0100319template <class F>
320class Reducer
321{
322public:
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000323 static void reduceX(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100324 {
325 // Set out window
326 Window out_window(window);
327 out_window.set(Window::DimX, Window::Dimension(0, 0, 0));
328
329 // Get first input and output slices
330 Window in_slice = window.first_slice_window_1D();
331 Window out_slice = out_window.first_slice_window_1D();
332
333 do
334 {
335 Iterator in(input, in_slice);
336 Iterator out(output, out_slice);
337
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000338 f(in, out, in_slice, out_slice, *input->info(), op);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100339 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100340 while(window.slide_window_slice_1D(in_slice) && out_window.slide_window_slice_1D(out_slice));
341 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000342 static void reduceY(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100343 {
344 // Set in window
345 Window in_window(window);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000346 Window out_window(window);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100347
348 in_window.set(Window::DimY, Window::Dimension(0, 1, 1));
Michalis Spyrou2897e612018-11-20 18:38:29 +0000349 out_window.set(Window::DimY, Window::Dimension(0, output->info()->dimension(1), output->info()->dimension(1)));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100350
351 // Get first input and output slices
352 Window in_slice = in_window.first_slice_window_2D();
Michalis Spyrou2897e612018-11-20 18:38:29 +0000353 Window out_slice = out_window.first_slice_window_2D();
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100354
355 do
356 {
357 Iterator in(input, in_slice);
358 Iterator out(output, out_slice);
359
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000360 f(in, out, in_slice, out_slice, *input->info(), 1, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100361 }
Michalis Spyrou2897e612018-11-20 18:38:29 +0000362 while(in_window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100363 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000364 static void reduceZ(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100365 {
366 // Set in window
367 Window in_window(window);
Michalis Spyrou2897e612018-11-20 18:38:29 +0000368 Window out_window(window);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100369
370 in_window.set(Window::DimZ, Window::Dimension(0, 1, 1));
Michalis Spyrou2897e612018-11-20 18:38:29 +0000371 out_window.set(Window::DimZ, Window::Dimension(0, output->info()->dimension(2), output->info()->dimension(2)));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100372
373 // Get first input and output slices
374 Window in_slice = in_window.first_slice_window_3D();
Michalis Spyrou2897e612018-11-20 18:38:29 +0000375 Window out_slice = out_window.first_slice_window_3D();
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100376
377 do
378 {
379 Iterator in(input, in_slice);
380 Iterator out(output, out_slice);
381
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000382 f(in, out, in_slice, out_slice, *input->info(), 2, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100383 }
Michalis Spyrou2897e612018-11-20 18:38:29 +0000384 while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_3D(out_slice));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100385 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000386 static void reduceW(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100387 {
388 // Set in/out window
389 Window in_window(window);
390 Window out_window(window);
391
392 in_window.set(3, Window::Dimension(0, 1, 1));
393 out_window.set(3, Window::Dimension(0, 1, 1));
394
395 // Get first input and output slices
396 Window in_slice = in_window.first_slice_window_4D();
397 Window out_slice = out_window.first_slice_window_4D();
398
399 do
400 {
401 Iterator in(input, in_slice);
402 Iterator out(output, out_slice);
403
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000404 f(in, out, in_slice, out_slice, *input->info(), 3, op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100405 }
406 while(in_window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_4D(out_slice));
Georgios Pinitasd9769582017-08-03 10:19:40 +0100407 }
408};
409
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000410template <typename T, int S>
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100411struct RedOpX
Georgios Pinitasd9769582017-08-03 10:19:40 +0100412{
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100413 /** NEON vector tag type. */
414 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
415
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000416 inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, const ReductionOperation op)
Georgios Pinitasd9769582017-08-03 10:19:40 +0100417 {
418 ARM_COMPUTE_UNUSED(out_slice);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000419 auto init_res_value = static_cast<T>(0.f);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100420 switch(op)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000421 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100422 case ReductionOperation::ARG_IDX_MAX:
423 case ReductionOperation::ARG_IDX_MIN:
424 case ReductionOperation::MIN:
Usama Arif28f0dd92019-05-20 13:44:34 +0100425 case ReductionOperation::MAX:
Usama Arifa4a08ad2019-05-20 12:38:33 +0100426 {
427 init_res_value = *reinterpret_cast<T *>(input.ptr());
428 break;
429 }
430 case ReductionOperation::PROD:
431 {
432 init_res_value = static_cast<T>(1.f);
433 break;
434 }
435 default:
436 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000437 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000438 auto vec_res_value = wrapper::vdup_n(init_res_value, ExactTagType{});
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000439 uint32x4x4_t vec_res_idx{ { 0 } };
Georgios Pinitasd9769582017-08-03 10:19:40 +0100440
441 execute_window_loop(in_slice, [&](const Coordinates & id)
442 {
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100443 const auto in_ptr = reinterpret_cast<const T *>(input.ptr());
444 const auto vec_elements = wrapper::vloadq(in_ptr);
445
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000446 switch(op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100447 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000448 case ReductionOperation::SUM_SQUARE:
449 vec_res_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_res_value);
450 break;
451 case ReductionOperation::MEAN_SUM:
452 case ReductionOperation::SUM:
453 vec_res_value = wrapper::vadd(vec_elements, vec_res_value);
454 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000455 case ReductionOperation::PROD:
456 vec_res_value = wrapper::vmul(vec_elements, vec_res_value);
457 break;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000458 case ReductionOperation::ARG_IDX_MIN:
459 {
460 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100461 vec_res_idx = calculate_index<decltype(vec_res_value)>(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000462 vec_res_value = temp_vec_res_value;
463 break;
464 }
465 case ReductionOperation::ARG_IDX_MAX:
466 {
467 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100468 vec_res_idx = calculate_index<decltype(vec_res_value)>(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000469 vec_res_value = temp_vec_res_value;
470 break;
471 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100472 case ReductionOperation::MIN:
473 {
474 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
475 break;
476 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100477 case ReductionOperation::MAX:
478 {
479 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
480 break;
481 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000482 default:
483 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100484 }
Georgios Pinitasd9769582017-08-03 10:19:40 +0100485 },
486 input);
487
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000488 switch(op)
Michele Di Giorgio1c948d42018-11-20 16:03:01 +0000489 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000490 case ReductionOperation::SUM:
491 case ReductionOperation::SUM_SQUARE:
492 case ReductionOperation::MEAN_SUM:
493 {
494 auto carry_res = wrapper::vpadd(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value));
495 for(int i = 0; i < S / 4; ++i)
496 {
497 carry_res = wrapper::vpadd(carry_res, carry_res);
498 }
499 auto res = wrapper::vgetlane(carry_res, 0);
Georgios Pinitasd9769582017-08-03 10:19:40 +0100500
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000501 if(op == ReductionOperation::MEAN_SUM)
502 {
503 res /= in_info.dimension(0);
504 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100505
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000506 *(reinterpret_cast<T *>(output.ptr())) = res;
507 break;
508 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000509 case ReductionOperation::PROD:
510 {
511 auto carry_res = wrapper::vmul(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value));
512 T res = 1;
513 for(int i = 0; i < S / 2; ++i)
514 {
515 res *= wrapper::vgetlane(carry_res, i);
516 }
517 *(reinterpret_cast<T *>(output.ptr())) = res;
518 break;
519 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000520 case ReductionOperation::ARG_IDX_MIN:
521 case ReductionOperation::ARG_IDX_MAX:
522 {
Michalis Spyroub9626ab2019-05-13 17:41:01 +0100523 auto res = calculate_vector_index<decltype(vec_res_value)>(vec_res_idx, vec_res_value, op);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000524 *(reinterpret_cast<uint32_t *>(output.ptr())) = res;
525 break;
526 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100527 case ReductionOperation::MIN:
528 {
529 *(reinterpret_cast<T *>(output.ptr())) = wrapper::vgetlane(calculate_min(vec_res_value), 0);
530 break;
531 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100532 case ReductionOperation::MAX:
533 {
534 *(reinterpret_cast<T *>(output.ptr())) = wrapper::vgetlane(calculate_max(vec_res_value), 0);
535 break;
536 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000537 default:
538 ARM_COMPUTE_ERROR("Not supported");
539 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100540 }
541};
542
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100543struct RedOpX_qasymm8
544{
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000545 inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100546 {
547 ARM_COMPUTE_UNUSED(out_slice);
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100548
549 const UniformQuantizationInfo iq_info = in_info.quantization_info().uniform();
550
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000551 auto vec_res_value1 = vdupq_n_u32(static_cast<uint32_t>(0.f));
552 auto vec_res_value2 = vdupq_n_u32(static_cast<uint32_t>(0.f));
553 auto vec_res_value3 = vdupq_n_u32(static_cast<uint32_t>(0.f));
554 auto vec_res_value4 = vdupq_n_u32(static_cast<uint32_t>(0.f));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100555
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000556 auto vec_res_value1_f = vdupq_n_f32(static_cast<float>(1.f));
557 auto vec_res_value2_f = vdupq_n_f32(static_cast<float>(1.f));
558 auto vec_res_value3_f = vdupq_n_f32(static_cast<float>(1.f));
559 auto vec_res_value4_f = vdupq_n_f32(static_cast<float>(1.f));
560
Michalis Spyrou728d6f72019-01-16 13:57:58 +0000561 uint8x16_t vec_res_value = { 0 };
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000562
Usama Arif28f0dd92019-05-20 13:44:34 +0100563 if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::MIN || op == ReductionOperation::MAX)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000564 {
565 vec_res_value = wrapper::vdup_n(*input.ptr(), wrapper::traits::vector_128_tag{});
566 }
567
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000568 uint32x4x4_t vec_res_idx{ { 0 } };
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100569 execute_window_loop(in_slice, [&](const Coordinates & id)
570 {
571 const auto vec_elements = wrapper::vloadq(input.ptr());
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000572 switch(op)
573 {
574 case ReductionOperation::SUM:
575 case ReductionOperation::MEAN_SUM:
576 {
577 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
578 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100579
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000580 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
581 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
582 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
583 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100584
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000585 vec_res_value1 = wrapper::vadd(temp32x4t_1, vec_res_value1);
586 vec_res_value2 = wrapper::vadd(temp32x4t_2, vec_res_value2);
587 vec_res_value3 = wrapper::vadd(temp32x4t_3, vec_res_value3);
588 vec_res_value4 = wrapper::vadd(temp32x4t_4, vec_res_value4);
589 break;
590 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000591 case ReductionOperation::PROD:
592 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100593 const auto offset32x4f_4 = vdupq_n_f32(iq_info.offset);
594 const auto scale32x4f_4 = vdupq_n_f32(iq_info.scale);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000595
596 const auto temp16x8t_1 = vmovl_u8(vget_low_u8(vec_elements));
597 const auto temp16x8t_2 = vmovl_u8(vget_high_u8(vec_elements));
598
599 const auto temp32x4t_1 = vmovl_u16(vget_low_u16(temp16x8t_1));
600 const auto temp32x4t_2 = vmovl_u16(vget_high_u16(temp16x8t_1));
601 const auto temp32x4t_3 = vmovl_u16(vget_low_u16(temp16x8t_2));
602 const auto temp32x4t_4 = vmovl_u16(vget_high_u16(temp16x8t_2));
603
604 auto temp32x4f_1 = vcvtq_f32_u32(temp32x4t_1);
605 auto temp32x4f_2 = vcvtq_f32_u32(temp32x4t_2);
606 auto temp32x4f_3 = vcvtq_f32_u32(temp32x4t_3);
607 auto temp32x4f_4 = vcvtq_f32_u32(temp32x4t_4);
608
609 //de-quantize vec_elements
610 temp32x4f_1 = vmulq_f32(vsubq_f32(temp32x4f_1, offset32x4f_4), scale32x4f_4);
611 temp32x4f_2 = vmulq_f32(vsubq_f32(temp32x4f_2, offset32x4f_4), scale32x4f_4);
612 temp32x4f_3 = vmulq_f32(vsubq_f32(temp32x4f_3, offset32x4f_4), scale32x4f_4);
613 temp32x4f_4 = vmulq_f32(vsubq_f32(temp32x4f_4, offset32x4f_4), scale32x4f_4);
614
615 vec_res_value1_f = vmulq_f32(temp32x4f_1, vec_res_value1_f);
616 vec_res_value2_f = vmulq_f32(temp32x4f_2, vec_res_value2_f);
617 vec_res_value3_f = vmulq_f32(temp32x4f_3, vec_res_value3_f);
618 vec_res_value4_f = vmulq_f32(temp32x4f_4, vec_res_value4_f);
619 break;
620 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000621 case ReductionOperation::ARG_IDX_MIN:
622 {
623 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
624 vec_res_idx = calculate_index(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
625 vec_res_value = temp_vec_res_value;
626 break;
627 }
628 case ReductionOperation::ARG_IDX_MAX:
629 {
630 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
631 vec_res_idx = calculate_index(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0);
632 vec_res_value = temp_vec_res_value;
633 break;
634 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100635 case ReductionOperation::MIN:
636 {
637 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
638 break;
639 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100640 case ReductionOperation::MAX:
641 {
642 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
643 break;
644 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000645 default:
646 ARM_COMPUTE_ERROR("Not supported");
647 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100648 },
649 input);
650
Usama Arifa4a08ad2019-05-20 12:38:33 +0100651 switch(op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100652 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100653 case ReductionOperation::ARG_IDX_MIN:
654 case ReductionOperation::ARG_IDX_MAX:
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000655 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100656 auto res = calculate_vector_index(vec_res_idx, vec_res_value, op);
657 *(reinterpret_cast<uint32_t *>(output.ptr())) = res;
658 break;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000659 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100660 case ReductionOperation::MIN:
661 {
662 *(output.ptr()) = static_cast<uint8_t>(wrapper::vgetlane(calculate_min(vec_res_value), 0));
663 break;
664 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100665 case ReductionOperation::MAX:
666 {
667 *(output.ptr()) = static_cast<uint8_t>(wrapper::vgetlane(calculate_max(vec_res_value), 0));
668 break;
669 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100670 case ReductionOperation::PROD:
671 {
672 auto carry_res = wrapper::vmul(vec_res_value1_f, vec_res_value2_f);
673 carry_res = wrapper::vmul(carry_res, vec_res_value3_f);
674 carry_res = wrapper::vmul(carry_res, vec_res_value4_f);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000675
Usama Arifa4a08ad2019-05-20 12:38:33 +0100676 float res = wrapper::vgetlane(carry_res, 0);
677 res *= wrapper::vgetlane(carry_res, 1);
678 res *= wrapper::vgetlane(carry_res, 2);
679 res *= wrapper::vgetlane(carry_res, 3);
680
681 //re-quantize result
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100682 res = quantize_qasymm8(res, iq_info);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100683 *(output.ptr()) = static_cast<uint8_t>(res);
684 break;
685 }
686 default:
687 {
688 auto carry_res = wrapper::vadd(vec_res_value1, vec_res_value2);
689 carry_res = wrapper::vadd(carry_res, vec_res_value3);
690 carry_res = wrapper::vadd(carry_res, vec_res_value4);
691
692 auto carry_paddition = wrapper::vpadd(wrapper::vgethigh(carry_res), wrapper::vgetlow(carry_res));
693 carry_paddition = wrapper::vpadd(carry_paddition, carry_paddition);
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000694 auto res = static_cast<int32_t>(wrapper::vgetlane(carry_paddition, 0));
Usama Arifa4a08ad2019-05-20 12:38:33 +0100695
696 if(op == ReductionOperation::MEAN_SUM)
697 {
698 res /= in_info.dimension(0);
699 }
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000700 else
701 {
702 // Subtract accumulated offsets
703 res -= (in_info.dimension(0) - 1) * iq_info.offset;
704 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100705
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000706 *(output.ptr()) = utils::cast::saturate_cast<uint8_t>(res);
Usama Arifa4a08ad2019-05-20 12:38:33 +0100707 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000708 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100709 }
710};
711
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000712template <typename T, int S>
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100713struct RedOpYZW
714{
715 /** NEON vector tag type. */
716 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000717 using neon_vector = typename wrapper::traits::neon_vector<T, S>::type;
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100718
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000719 inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int axis, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100720 {
721 ARM_COMPUTE_UNUSED(out_slice);
722
giuros01154bc1c2019-03-26 17:44:40 +0000723 execute_window_loop(in_slice, [&](const Coordinates &)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100724 {
Michalis Spyrou728d6f72019-01-16 13:57:58 +0000725 neon_vector vec_res_value = { 0 };
Usama Arifa4a08ad2019-05-20 12:38:33 +0100726 switch(op)
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000727 {
Usama Arifa4a08ad2019-05-20 12:38:33 +0100728 case ReductionOperation::ARG_IDX_MAX:
729 case ReductionOperation::ARG_IDX_MIN:
730 case ReductionOperation::MIN:
Usama Arif28f0dd92019-05-20 13:44:34 +0100731 case ReductionOperation::MAX:
Usama Arifa4a08ad2019-05-20 12:38:33 +0100732 {
733 vec_res_value = wrapper::vloadq(reinterpret_cast<T *>(input.ptr()));
734 break;
735 }
736 case ReductionOperation::PROD:
737 {
738 vec_res_value = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{});
739 break;
740 }
741 default:
742 {
743 vec_res_value = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
744 break;
745 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000746 }
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000747 uint32x4x4_t vec_res_idx{ { 0 } };
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000748
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100749 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
750 {
Pablo Tello07958782020-01-09 14:43:36 +0000751 const T *in_ptr = reinterpret_cast<T *>(input.ptr() + in_info.strides_in_bytes()[axis] * dim);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100752 const auto vec_elements = wrapper::vloadq(in_ptr);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000753 switch(op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100754 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000755 case ReductionOperation::SUM:
756 case ReductionOperation::MEAN_SUM:
757 vec_res_value = wrapper::vadd(vec_elements, vec_res_value);
758 break;
759 case ReductionOperation::SUM_SQUARE:
760 vec_res_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_res_value);
761 break;
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000762 case ReductionOperation::PROD:
763 vec_res_value = wrapper::vmul(vec_elements, vec_res_value);
764 break;
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000765 case ReductionOperation::ARG_IDX_MIN:
766 {
767 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
768 vec_res_idx = calculate_index(dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
769 vec_res_value = temp_vec_res_value;
770 break;
771 }
772 case ReductionOperation::ARG_IDX_MAX:
773 {
774 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
775 vec_res_idx = calculate_index(dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
776 vec_res_value = temp_vec_res_value;
777 break;
778 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100779 case ReductionOperation::MIN:
780 {
781 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
782 break;
783 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100784 case ReductionOperation::MAX:
785 {
786 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
787 break;
788 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000789 default:
790 ARM_COMPUTE_ERROR("Not supported");
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100791 }
792 }
793
794 if(op == ReductionOperation::MEAN_SUM)
795 {
796 auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast<T>(in_info.dimension(axis)), ExactTagType{}));
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000797 vec_res_value = wrapper::vmul(vec_res_value, vec_width_inv);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100798 }
799
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000800 if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX)
801 {
802 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()), vec_res_idx.val[0]);
Michele Di Giorgio81d7e782019-08-16 18:03:35 +0100803#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
804 if(std::is_same<T, float16_t>::value)
805 {
806 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 4, vec_res_idx.val[1]);
807 }
808#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000809 }
810 else
811 {
812 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), vec_res_value);
813 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100814 },
815 input, output);
816 }
817};
818
giuros01154bc1c2019-03-26 17:44:40 +0000819template <typename T, int S, int axis, ReductionOperation op>
820struct RedOpYZW_complex
821{
822 /** NEON vector tag type. */
823 using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type;
824 using neon_vector = typename wrapper::traits::neon_vector<T, S>::type;
825
826 inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int, const ReductionOperation)
827 {
828 ARM_COMPUTE_UNUSED(out_slice);
829 ARM_COMPUTE_ERROR_ON(axis != 2);
830
831 const size_t stride_z = in_info.strides_in_bytes()[axis];
832
833 execute_window_loop(in_slice, [&](const Coordinates &)
834 {
835 neon_vector vec_res_value_0 = { 0 };
836 neon_vector vec_res_value_1 = { 0 };
837
838 vec_res_value_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
839 vec_res_value_1 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{});
840
841 for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim)
842 {
843 T *in_ptr_0;
844 T *in_ptr_1;
845 switch(axis)
846 {
847 case 2:
848 in_ptr_0 = reinterpret_cast<T *>(input.ptr() + stride_z * dim);
849 in_ptr_1 = reinterpret_cast<T *>(input.ptr() + 16 + stride_z * dim);
850 break;
851 default:
852 ARM_COMPUTE_ERROR("Not supported");
853 }
854 const auto vec_elements_0 = wrapper::vloadq(in_ptr_0);
855 const auto vec_elements_1 = wrapper::vloadq(in_ptr_1);
856
857 switch(op)
858 {
859 case ReductionOperation::SUM:
860 vec_res_value_0 = wrapper::vadd(vec_elements_0, vec_res_value_0);
861 vec_res_value_1 = wrapper::vadd(vec_elements_1, vec_res_value_1);
862 break;
863 default:
864 ARM_COMPUTE_ERROR("Not supported");
865 }
866 }
867
868 wrapper::vstore(reinterpret_cast<T *>(output.ptr()), vec_res_value_0);
869 wrapper::vstore(reinterpret_cast<T *>(output.ptr() + 16), vec_res_value_1);
870
871 },
872 input, output);
873 }
874};
875
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100876struct RedOpYZW_qasymm8
877{
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000878 inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int axis, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100879 {
880 ARM_COMPUTE_UNUSED(out_slice);
881
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100882 const UniformQuantizationInfo iq_info = in_info.quantization_info().uniform();
883
giuros01154bc1c2019-03-26 17:44:40 +0000884 execute_window_loop(in_slice, [&](const Coordinates &)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100885 {
Georgios Pinitasd57891a2019-02-19 18:10:03 +0000886 uint32x4x4_t vec_res_idx{ { 0 } };
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000887 auto vec_res_value1 = wrapper::vdup_n(static_cast<uint32_t>(0), wrapper::traits::vector_128_tag{});
888 auto vec_res_value2 = wrapper::vdup_n(static_cast<uint32_t>(0), wrapper::traits::vector_128_tag{});
889 auto vec_res_value3 = wrapper::vdup_n(static_cast<uint32_t>(0), wrapper::traits::vector_128_tag{});
890 auto vec_res_value4 = wrapper::vdup_n(static_cast<uint32_t>(0), wrapper::traits::vector_128_tag{});
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000891
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000892 auto vec_res_value1_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
893 auto vec_res_value2_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
894 auto vec_res_value3_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
895 auto vec_res_value4_f = wrapper::vdup_n(static_cast<float>(1), wrapper::traits::vector_128_tag{});
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000896
897 auto vec_res_value = wrapper::vloadq(input.ptr());
898
899 for(unsigned int index_dim = 0; index_dim < in_info.dimension(axis); ++index_dim)
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100900 {
Pablo Tello07958782020-01-09 14:43:36 +0000901 const uint8_t *in_ptr = input.ptr() + in_info.strides_in_bytes()[axis] * index_dim;
902 const auto vec_elements = wrapper::vloadq(in_ptr);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000903 switch(op)
904 {
905 case ReductionOperation::SUM:
906 case ReductionOperation::MEAN_SUM:
907 {
908 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
909 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100910
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000911 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
912 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
913 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
914 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100915
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000916 vec_res_value1 = wrapper::vadd(temp32x4t_1, vec_res_value1);
917 vec_res_value2 = wrapper::vadd(temp32x4t_2, vec_res_value2);
918 vec_res_value3 = wrapper::vadd(temp32x4t_3, vec_res_value3);
919 vec_res_value4 = wrapper::vadd(temp32x4t_4, vec_res_value4);
920 break;
921 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000922 case ReductionOperation::PROD:
923 {
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000924 const auto offset32x4f_4 = wrapper::vdup_n(static_cast<float>(iq_info.offset), wrapper::traits::vector_128_tag{});
925 const auto scale32x4f_4 = wrapper::vdup_n(iq_info.scale, wrapper::traits::vector_128_tag{});
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000926
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000927 const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements));
928 const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements));
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000929
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000930 const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1));
931 const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1));
932 const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2));
933 const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2));
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000934
935 auto temp32x4f_1 = vcvtq_f32_u32(temp32x4t_1);
936 auto temp32x4f_2 = vcvtq_f32_u32(temp32x4t_2);
937 auto temp32x4f_3 = vcvtq_f32_u32(temp32x4t_3);
938 auto temp32x4f_4 = vcvtq_f32_u32(temp32x4t_4);
939
940 //de-quantize vec_elements
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000941 temp32x4f_1 = wrapper::vmul(wrapper::vsub(temp32x4f_1, offset32x4f_4), scale32x4f_4);
942 temp32x4f_2 = wrapper::vmul(wrapper::vsub(temp32x4f_2, offset32x4f_4), scale32x4f_4);
943 temp32x4f_3 = wrapper::vmul(wrapper::vsub(temp32x4f_3, offset32x4f_4), scale32x4f_4);
944 temp32x4f_4 = wrapper::vmul(wrapper::vsub(temp32x4f_4, offset32x4f_4), scale32x4f_4);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000945
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000946 vec_res_value1_f = wrapper::vmul(temp32x4f_1, vec_res_value1_f);
947 vec_res_value2_f = wrapper::vmul(temp32x4f_2, vec_res_value2_f);
948 vec_res_value3_f = wrapper::vmul(temp32x4f_3, vec_res_value3_f);
949 vec_res_value4_f = wrapper::vmul(temp32x4f_4, vec_res_value4_f);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000950 break;
951 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000952 case ReductionOperation::ARG_IDX_MIN:
953 {
954 auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000955 vec_res_idx = calculate_index(index_dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000956 vec_res_value = temp_vec_res_value;
957 break;
958 }
959 case ReductionOperation::ARG_IDX_MAX:
960 {
961 auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000962 vec_res_idx = calculate_index(index_dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis);
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000963 vec_res_value = temp_vec_res_value;
964 break;
965 }
Usama Arifa4a08ad2019-05-20 12:38:33 +0100966 case ReductionOperation::MIN:
967 {
968 vec_res_value = wrapper::vmin(vec_elements, vec_res_value);
969 break;
970 }
Usama Arif28f0dd92019-05-20 13:44:34 +0100971 case ReductionOperation::MAX:
972 {
973 vec_res_value = wrapper::vmax(vec_elements, vec_res_value);
974 break;
975 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000976 default:
977 ARM_COMPUTE_ERROR("Not supported");
978 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100979 }
980
981 if(op == ReductionOperation::MEAN_SUM)
982 {
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000983 const auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast<float>(in_info.dimension(axis)), wrapper::traits::vector_128_tag{}));
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000984 vec_res_value1_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value1), vec_width_inv);
985 vec_res_value2_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value2), vec_width_inv);
986 vec_res_value3_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value3), vec_width_inv);
987 vec_res_value4_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value4), vec_width_inv);
Michalis Spyroubcf8a962018-10-12 10:51:31 +0100988
Michalis Spyrouaea14c62019-01-03 11:10:25 +0000989 vec_res_value1 = vcvtq_u32_f32(vec_res_value1_f);
990 vec_res_value2 = vcvtq_u32_f32(vec_res_value2_f);
991 vec_res_value3 = vcvtq_u32_f32(vec_res_value3_f);
992 vec_res_value4 = vcvtq_u32_f32(vec_res_value4_f);
993 }
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000994 else if(op == ReductionOperation::PROD)
995 {
Michalis Spyrou19bd4122020-01-22 10:27:06 +0000996 const auto offset32x4f_4 = wrapper::vdup_n(static_cast<float>(iq_info.offset), wrapper::traits::vector_128_tag{});
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100997 const auto iscale32x4f_4 = vinvq_f32(vdupq_n_f32(iq_info.scale));
Manuel Bottini1d4f3852019-01-14 15:14:43 +0000998
999 //re-quantize
Michalis Spyrou19bd4122020-01-22 10:27:06 +00001000 vec_res_value1_f = wrapper::vadd(wrapper::vmul(vec_res_value1_f, iscale32x4f_4), offset32x4f_4);
1001 vec_res_value2_f = wrapper::vadd(wrapper::vmul(vec_res_value2_f, iscale32x4f_4), offset32x4f_4);
1002 vec_res_value3_f = wrapper::vadd(wrapper::vmul(vec_res_value3_f, iscale32x4f_4), offset32x4f_4);
1003 vec_res_value4_f = wrapper::vadd(wrapper::vmul(vec_res_value4_f, iscale32x4f_4), offset32x4f_4);
Manuel Bottini1d4f3852019-01-14 15:14:43 +00001004
1005 vec_res_value1 = vcvtq_u32_f32(vec_res_value1_f);
1006 vec_res_value2 = vcvtq_u32_f32(vec_res_value2_f);
1007 vec_res_value3 = vcvtq_u32_f32(vec_res_value3_f);
1008 vec_res_value4 = vcvtq_u32_f32(vec_res_value4_f);
1009 }
1010
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001011 if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX)
1012 {
1013 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()), vec_res_idx.val[0]);
1014 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 4, vec_res_idx.val[1]);
1015 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 8, vec_res_idx.val[2]);
1016 wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 12, vec_res_idx.val[3]);
1017 }
Usama Arifa4a08ad2019-05-20 12:38:33 +01001018 else if(op == ReductionOperation::ARG_IDX_MIN)
1019 {
1020 wrapper::vstore(output.ptr(), vec_res_value);
1021 }
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001022 else
1023 {
Michalis Spyrou19bd4122020-01-22 10:27:06 +00001024 if(op == ReductionOperation::SUM)
1025 {
1026 // Subtract offsets
1027 auto offsets = vdupq_n_s32((in_info.dimension(axis) - 1) * iq_info.offset);
1028
1029 auto vec_res_s_value1 = vreinterpretq_s32_u32(vec_res_value1);
1030 auto vec_res_s_value2 = vreinterpretq_s32_u32(vec_res_value2);
1031 auto vec_res_s_value3 = vreinterpretq_s32_u32(vec_res_value3);
1032 auto vec_res_s_value4 = vreinterpretq_s32_u32(vec_res_value4);
1033
1034 vec_res_s_value1 = wrapper::vsub(vec_res_s_value1, offsets);
1035 vec_res_s_value2 = wrapper::vsub(vec_res_s_value2, offsets);
1036 vec_res_s_value3 = wrapper::vsub(vec_res_s_value3, offsets);
1037 vec_res_s_value4 = wrapper::vsub(vec_res_s_value4, offsets);
1038
1039 const auto temp16x8t_1 = wrapper::vcombine(wrapper::vqmovn(vec_res_s_value1), wrapper::vqmovn(vec_res_s_value2));
1040 const auto temp16x8t_2 = wrapper::vcombine(wrapper::vqmovn(vec_res_s_value3), wrapper::vqmovn(vec_res_s_value4));
1041 auto res = wrapper::vcombine(wrapper::vqmovun(temp16x8t_1), wrapper::vqmovun(temp16x8t_2));
1042 wrapper::vstore(output.ptr(), res);
1043 }
1044 else
1045 {
1046 const auto temp16x8t_1 = wrapper::vcombine(wrapper::vqmovn(vec_res_value1), wrapper::vqmovn(vec_res_value2));
1047 const auto temp16x8t_2 = wrapper::vcombine(wrapper::vqmovn(vec_res_value3), wrapper::vqmovn(vec_res_value4));
1048 auto res = wrapper::vcombine(wrapper::vqmovn(temp16x8t_1), wrapper::vqmovn(temp16x8t_2));
1049 wrapper::vstore(output.ptr(), res);
1050 }
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001051 }
1052
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001053 },
1054 input, output);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001055 }
1056};
1057
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001058void reduce_op(const Window &window, const ITensor *input, ITensor *output, unsigned int axis, const ReductionOperation op)
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001059{
giuros01154bc1c2019-03-26 17:44:40 +00001060 const bool is_complex = (input->info()->num_channels() == 2);
1061
1062 if(is_complex)
1063 {
1064 switch(axis)
1065 {
1066 case 2:
1067 switch(input->info()->data_type())
1068 {
1069 case DataType::F32:
1070 switch(op)
1071 {
1072 case ReductionOperation::SUM:
1073 return Reducer<RedOpYZW_complex<float, 4, 2, ReductionOperation::SUM>>::reduceZ(window, input, output, RedOpYZW_complex<float, 4, 2, ReductionOperation::SUM>(), op);
1074 default:
1075 ARM_COMPUTE_ERROR("Not supported");
1076 }
1077 default:
1078 ARM_COMPUTE_ERROR("Not supported");
1079 }
1080 default:
1081 ARM_COMPUTE_ERROR("Not supported");
1082 }
1083 }
1084
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001085 switch(axis)
1086 {
1087 case 0:
1088 switch(input->info()->data_type())
1089 {
1090 case DataType::QASYMM8:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001091 return Reducer<RedOpX_qasymm8>::reduceX(window, input, output, RedOpX_qasymm8(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001092#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1093 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001094 return Reducer<RedOpX<float16_t, 8>>::reduceX(window, input, output, RedOpX<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001095#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1096 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001097 return Reducer<RedOpX<float, 4>>::reduceX(window, input, output, RedOpX<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001098 case DataType::S32:
1099 return Reducer<RedOpX<int32_t, 4>>::reduceX(window, input, output, RedOpX<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001100 default:
1101 ARM_COMPUTE_ERROR("Not supported");
1102 }
1103 case 1:
1104 switch(input->info()->data_type())
1105 {
1106 case DataType::QASYMM8:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001107 return Reducer<RedOpYZW_qasymm8>::reduceY(window, input, output, RedOpYZW_qasymm8(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001108#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1109 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001110 return Reducer<RedOpYZW<float16_t, 8>>::reduceY(window, input, output, RedOpYZW<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001111#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1112 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001113 return Reducer<RedOpYZW<float, 4>>::reduceY(window, input, output, RedOpYZW<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001114 case DataType::S32:
1115 return Reducer<RedOpYZW<int32_t, 4>>::reduceY(window, input, output, RedOpYZW<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001116 default:
1117 ARM_COMPUTE_ERROR("Not supported");
1118 }
1119 case 2:
1120 switch(input->info()->data_type())
1121 {
1122 case DataType::QASYMM8:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001123 return Reducer<RedOpYZW_qasymm8>::reduceZ(window, input, output, RedOpYZW_qasymm8(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001124#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1125 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001126 return Reducer<RedOpYZW<float16_t, 8>>::reduceZ(window, input, output, RedOpYZW<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001127#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1128 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001129 return Reducer<RedOpYZW<float, 4>>::reduceZ(window, input, output, RedOpYZW<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001130 case DataType::S32:
1131 return Reducer<RedOpYZW<int32_t, 4>>::reduceZ(window, input, output, RedOpYZW<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001132 default:
1133 ARM_COMPUTE_ERROR("Not supported");
1134 }
1135 case 3:
1136 switch(input->info()->data_type())
1137 {
1138 case DataType::QASYMM8:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001139 return Reducer<RedOpYZW_qasymm8>::reduceW(window, input, output, RedOpYZW_qasymm8(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001140#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1141 case DataType::F16:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001142 return Reducer<RedOpYZW<float16_t, 8>>::reduceW(window, input, output, RedOpYZW<float16_t, 8>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001143#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
1144 case DataType::F32:
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001145 return Reducer<RedOpYZW<float, 4>>::reduceW(window, input, output, RedOpYZW<float, 4>(), op);
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001146 case DataType::S32:
1147 return Reducer<RedOpYZW<int32_t, 4>>::reduceW(window, input, output, RedOpYZW<int32_t, 4>(), op);
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001148 default:
1149 ARM_COMPUTE_ERROR("Not supported");
1150 }
1151 default:
1152 ARM_COMPUTE_ERROR("Unsupported reduction axis");
1153 }
1154}
John Richardson73d4aef2018-05-08 14:34:33 +01001155
1156Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
1157{
1158 ARM_COMPUTE_UNUSED(op);
1159
1160 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +00001161 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
giuros01154bc1c2019-03-26 17:44:40 +00001162
1163 if(input->num_channels() == 1)
1164 {
Michalis Spyroub9626ab2019-05-13 17:41:01 +01001165 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32);
giuros01154bc1c2019-03-26 17:44:40 +00001166 }
1167 else
1168 {
1169 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32);
1170 ARM_COMPUTE_RETURN_ERROR_ON(op != ReductionOperation::SUM);
1171 ARM_COMPUTE_RETURN_ERROR_ON(axis != 2);
1172 }
John Richardson73d4aef2018-05-08 14:34:33 +01001173
1174 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 +01001175 ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis");
John Richardson73d4aef2018-05-08 14:34:33 +01001176
1177 if(output->total_size() != 0)
1178 {
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001179 bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN);
1180 if(!is_arg_min_max)
1181 {
1182 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00001183 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
giuros01154bc1c2019-03-26 17:44:40 +00001184 ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() != output->num_channels());
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001185 }
1186 else
1187 {
Michele Di Giorgio9637b2e2019-09-23 16:49:49 +01001188 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32);
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001189 }
John Richardson73d4aef2018-05-08 14:34:33 +01001190
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001191 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis);
John Richardson73d4aef2018-05-08 14:34:33 +01001192 const TensorInfo tensor_info_reshaped = input->clone()->set_tensor_shape(output_shape);
1193 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_reshaped);
1194 }
1195
1196 return Status{};
1197}
1198
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001199std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int axis, ReductionOperation op)
John Richardson73d4aef2018-05-08 14:34:33 +01001200{
1201 // Calculate output shape and set if empty
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001202 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis);
John Richardson73d4aef2018-05-08 14:34:33 +01001203
1204 // Output auto initialization if not yet initialized
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001205 const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX);
Sang-Hoon Parkeaa01ab2019-11-11 17:33:28 +00001206 DataType output_data_type = is_arg_min_max ? DataType::S32 : input->data_type();
giuros01154bc1c2019-03-26 17:44:40 +00001207 auto_init_if_empty(*output, input->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 +01001208
1209 unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->data_type());
1210
1211 // Configure kernel window
1212 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
1213 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
1214 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
1215
1216 bool window_changed = update_window_and_padding(win, input_access, output_access);
1217 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
1218
1219 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
1220
1221 return std::make_tuple(err, win);
1222}
Georgios Pinitasd9769582017-08-03 10:19:40 +01001223} // namespace
1224
1225NEReductionOperationKernel::NEReductionOperationKernel()
1226 : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size()
1227{
1228}
1229
1230BorderSize NEReductionOperationKernel::border_size() const
1231{
1232 return _border_size;
1233}
1234
1235void NEReductionOperationKernel::configure(const ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op)
1236{
1237 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001238
John Richardson73d4aef2018-05-08 14:34:33 +01001239 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op));
Georgios Pinitasd9769582017-08-03 10:19:40 +01001240
1241 unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->info()->data_type());
1242
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001243 _input = input;
1244 _output = output;
1245 _border_size = (axis == 0) ? BorderSize(0, num_elems_processed_per_iteration - (input->info()->dimension(0) % num_elems_processed_per_iteration), 0, 0) : BorderSize();
1246 _op = op;
1247 _reduction_axis = axis;
Georgios Pinitasd9769582017-08-03 10:19:40 +01001248
1249 // Configure kernel window
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001250 auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis, op);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001251
John Richardson73d4aef2018-05-08 14:34:33 +01001252 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Georgios Pinitasd9769582017-08-03 10:19:40 +01001253
John Richardson73d4aef2018-05-08 14:34:33 +01001254 INEKernel::configure(std::get<1>(win_config));
1255}
1256
1257Status NEReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op)
1258{
1259 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op));
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001260 ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), axis, op)));
John Richardson73d4aef2018-05-08 14:34:33 +01001261
1262 return Status{};
Georgios Pinitasd9769582017-08-03 10:19:40 +01001263}
1264
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001265void NEReductionOperationKernel::run(const Window &window, const ThreadInfo &info)
Georgios Pinitasd9769582017-08-03 10:19:40 +01001266{
Moritz Pflanzerc186b572017-09-07 09:48:04 +01001267 ARM_COMPUTE_UNUSED(info);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001268 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
1269 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
1270
Michalis Spyrouaea14c62019-01-03 11:10:25 +00001271 reduce_op(window, _input, _output, _reduction_axis, _op);
Georgios Pinitasd9769582017-08-03 10:19:40 +01001272}
Michalis Spyroubcf8a962018-10-12 10:51:31 +01001273} // namespace arm_compute