Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 3 | * |
| 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 Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 27 | #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 Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/TensorInfo.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Validate.h" |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 36 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 37 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 38 | #include <arm_neon.h> |
| 39 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 40 | namespace arm_compute |
| 41 | { |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 44 | uint32x4x4_t calculate_index(uint32_t idx, float32x4_t a, float32x4_t b, uint32x4x4_t c, ReductionOperation op, int axis) |
| 45 | { |
| 46 | uint32x4_t mask{ 0 }; |
| 47 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 48 | { |
| 49 | mask = wrapper::vcgt(b, a); |
| 50 | } |
| 51 | else |
| 52 | { |
| 53 | mask = wrapper::vclt(b, a); |
| 54 | } |
| 55 | |
| 56 | uint32x4_t vec_idx = { idx, idx + 1, idx + 2, idx + 3 }; |
| 57 | if(axis != 0) |
| 58 | { |
| 59 | vec_idx = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 60 | } |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 61 | uint32x4x4_t res = { { wrapper::vbsl(mask, vec_idx, c.val[0]), 0, 0, 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 62 | |
| 63 | return res; |
| 64 | } |
| 65 | |
| 66 | uint32x4x4_t calculate_index(uint32_t idx, uint8x16_t a, uint8x16_t b, uint32x4x4_t c, ReductionOperation op, int axis) |
| 67 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 68 | uint32x4x4_t mask{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 69 | uint8x16_t mask_u8{ 0 }; |
| 70 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 71 | { |
| 72 | mask_u8 = wrapper::vcgt(b, a); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | mask_u8 = wrapper::vclt(b, a); |
| 77 | } |
Michalis Spyrou | 254a48a | 2019-01-14 17:27:39 +0000 | [diff] [blame] | 78 | auto wide_u16_1 = wrapper::vorr(vshll_n_u8(wrapper::vgetlow(mask_u8), 8), wrapper::vmovl(wrapper::vgetlow(mask_u8))); |
| 79 | auto wide_u16_2 = wrapper::vorr(vshll_n_u8(wrapper::vgethigh(mask_u8), 8), wrapper::vmovl(wrapper::vgethigh(mask_u8))); |
| 80 | mask.val[0] = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_1), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_1))); |
| 81 | mask.val[1] = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_1), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_1))); |
| 82 | mask.val[2] = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_2), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_2))); |
| 83 | mask.val[3] = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_2), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_2))); |
| 84 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 85 | uint32x4x4_t vec_idx = { { { idx + 0, idx + 1, idx + 2, idx + 3 }, |
| 86 | { idx + 4, idx + 5, idx + 6, idx + 7 }, |
| 87 | { idx + 8, idx + 9, idx + 10, idx + 11 }, |
| 88 | { idx + 12, idx + 13, idx + 14, idx + 15 } |
| 89 | } |
| 90 | }; |
| 91 | if(axis != 0) |
| 92 | { |
| 93 | vec_idx.val[0] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 94 | vec_idx.val[1] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 95 | vec_idx.val[2] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 96 | vec_idx.val[3] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 97 | } |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 98 | uint32x4x4_t res = |
| 99 | { |
| 100 | { |
| 101 | vbslq_u32(mask.val[0], vec_idx.val[0], c.val[0]), |
| 102 | vbslq_u32(mask.val[1], vec_idx.val[1], c.val[1]), |
| 103 | vbslq_u32(mask.val[2], vec_idx.val[2], c.val[2]), |
| 104 | vbslq_u32(mask.val[3], vec_idx.val[3], c.val[3]) |
| 105 | } |
| 106 | }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 107 | |
| 108 | return res; |
| 109 | } |
| 110 | |
| 111 | uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, float32x4_t vec_res_value, ReductionOperation op) |
| 112 | { |
| 113 | uint32x4_t res_idx_mask{ 0 }; |
| 114 | uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF); |
| 115 | |
| 116 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 117 | { |
| 118 | auto pmin = wrapper::vpmin(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 119 | pmin = wrapper::vpmin(pmin, pmin); |
| 120 | auto mask = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin)); |
| 121 | res_idx_mask = wrapper::vand(vec_res_idx.val[0], mask); |
| 122 | } |
| 123 | else |
| 124 | { |
| 125 | auto pmax = wrapper::vpmax(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 126 | pmax = wrapper::vpmax(pmax, pmax); |
| 127 | auto mask = vceqq_f32(vec_res_value, wrapper::vcombine(pmax, pmax)); |
| 128 | res_idx_mask = wrapper::vand(vec_res_idx.val[0], mask); |
| 129 | } |
| 130 | |
| 131 | res_idx_mask = wrapper::vadd(res_idx_mask, mask_ones); |
| 132 | auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask), wrapper::vgetlow(res_idx_mask)); |
| 133 | pmin = wrapper::vpmin(pmin, pmin); |
| 134 | uint32_t res = wrapper::vgetlane(pmin, 0); |
| 135 | |
| 136 | return (res - 0xFFFFFFFF); |
| 137 | } |
| 138 | |
| 139 | uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, uint8x16_t vec_res_value, ReductionOperation op) |
| 140 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 141 | uint32x4x4_t res_idx_mask{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 142 | uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF); |
| 143 | uint8x16_t mask_u8{ 0 }; |
| 144 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 145 | { |
| 146 | auto pmin = wrapper::vpmin(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 147 | pmin = wrapper::vpmin(pmin, pmin); |
| 148 | pmin = wrapper::vpmin(pmin, pmin); |
| 149 | pmin = wrapper::vpmin(pmin, pmin); |
| 150 | mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin)); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | auto pmax = wrapper::vpmax(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 155 | pmax = wrapper::vpmax(pmax, pmax); |
| 156 | pmax = wrapper::vpmax(pmax, pmax); |
| 157 | pmax = wrapper::vpmax(pmax, pmax); |
| 158 | mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax)); |
| 159 | } |
| 160 | |
| 161 | // Widen vectors |
| 162 | auto wide_u16_1 = wrapper::vorr(vshll_n_u8(wrapper::vgetlow(mask_u8), 8), wrapper::vmovl(wrapper::vgetlow(mask_u8))); |
| 163 | auto wide_u16_2 = wrapper::vorr(vshll_n_u8(wrapper::vgethigh(mask_u8), 8), wrapper::vmovl(wrapper::vgethigh(mask_u8))); |
| 164 | auto wide_u32_1 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_1), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_1))); |
| 165 | auto wide_u32_2 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_1), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_1))); |
| 166 | auto wide_u32_3 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(wide_u16_2), 16), wrapper::vmovl(wrapper::vgetlow(wide_u16_2))); |
| 167 | auto wide_u32_4 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(wide_u16_2), 16), wrapper::vmovl(wrapper::vgethigh(wide_u16_2))); |
| 168 | res_idx_mask.val[0] = wrapper::vand(vec_res_idx.val[0], wide_u32_1); |
| 169 | res_idx_mask.val[1] = wrapper::vand(vec_res_idx.val[1], wide_u32_2); |
| 170 | res_idx_mask.val[2] = wrapper::vand(vec_res_idx.val[2], wide_u32_3); |
| 171 | res_idx_mask.val[3] = wrapper::vand(vec_res_idx.val[3], wide_u32_4); |
| 172 | res_idx_mask.val[0] = wrapper::vadd(res_idx_mask.val[0], mask_ones); |
| 173 | res_idx_mask.val[1] = wrapper::vadd(res_idx_mask.val[1], mask_ones); |
| 174 | res_idx_mask.val[2] = wrapper::vadd(res_idx_mask.val[2], mask_ones); |
| 175 | res_idx_mask.val[3] = wrapper::vadd(res_idx_mask.val[3], mask_ones); |
| 176 | |
| 177 | uint32_t res = 0xFFFFFFFF; |
| 178 | int iter = 0; |
| 179 | do |
| 180 | { |
| 181 | auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask.val[iter]), wrapper::vgetlow(res_idx_mask.val[iter])); |
| 182 | pmin = wrapper::vpmin(pmin, pmin); |
| 183 | res = std::min(wrapper::vgetlane(pmin, 0), res); |
| 184 | iter++; |
| 185 | } |
| 186 | while(iter < 4); |
| 187 | |
| 188 | return (res - 0xFFFFFFFF); |
| 189 | } |
| 190 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 191 | uint32x4x4_t calculate_index(uint32_t idx, float16x8_t a, float16x8_t b, uint32x4x4_t c, ReductionOperation op, int axis) |
| 192 | { |
| 193 | uint32x4x2_t mask{ 0 }; |
| 194 | uint16x8_t mask_u16{ 0 }; |
| 195 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 196 | { |
| 197 | mask_u16 = wrapper::vcgt(b, a); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | mask_u16 = wrapper::vclt(b, a); |
| 202 | } |
| 203 | mask.val[0] = wrapper::vmovl(wrapper::vgetlow(mask_u16)); |
| 204 | mask.val[1] = wrapper::vmovl(wrapper::vgethigh(mask_u16)); |
| 205 | uint32x4x2_t vec_idx = { { { idx + 0, idx + 1, idx + 2, idx + 3 }, |
| 206 | { idx + 4, idx + 5, idx + 6, idx + 7 } |
| 207 | } |
| 208 | }; |
| 209 | if(axis != 0) |
| 210 | { |
| 211 | vec_idx.val[0] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 212 | vec_idx.val[1] = wrapper::vdup_n(idx, wrapper::traits::vector_128_tag{}); |
| 213 | } |
| 214 | uint32x4x4_t res = { wrapper::vbsl(mask.val[0], vec_idx.val[0], c.val[0]), |
| 215 | wrapper::vbsl(mask.val[1], vec_idx.val[1], c.val[1]), |
| 216 | 0, 0 |
| 217 | }; |
| 218 | |
| 219 | return res; |
| 220 | } |
| 221 | |
| 222 | uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, float16x8_t vec_res_value, ReductionOperation op) |
| 223 | { |
| 224 | uint32x4x2_t res_idx_mask{ 0 }; |
| 225 | uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF); |
| 226 | uint16x8_t mask_u16; |
| 227 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 228 | { |
| 229 | auto pmin = wrapper::vpmin(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 230 | pmin = wrapper::vpmin(pmin, pmin); |
| 231 | pmin = wrapper::vpmin(pmin, pmin); |
| 232 | mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin)); |
| 233 | } |
| 234 | else |
| 235 | { |
| 236 | auto pmax = wrapper::vpmax(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 237 | pmax = wrapper::vpmax(pmax, pmax); |
| 238 | pmax = wrapper::vpmax(pmax, pmax); |
| 239 | mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax)); |
| 240 | } |
| 241 | |
| 242 | // Widen vectors |
| 243 | auto wide_u32_1 = wrapper::vorr(vshll_n_u16(wrapper::vgetlow(mask_u16), 8), wrapper::vmovl(wrapper::vgetlow(mask_u16))); |
| 244 | auto wide_u32_2 = wrapper::vorr(vshll_n_u16(wrapper::vgethigh(mask_u16), 8), wrapper::vmovl(wrapper::vgethigh(mask_u16))); |
| 245 | res_idx_mask.val[0] = wrapper::vand(vec_res_idx.val[0], wide_u32_1); |
| 246 | res_idx_mask.val[1] = wrapper::vand(vec_res_idx.val[1], wide_u32_2); |
| 247 | res_idx_mask.val[0] = wrapper::vadd(res_idx_mask.val[0], mask_ones); |
| 248 | res_idx_mask.val[1] = wrapper::vadd(res_idx_mask.val[1], mask_ones); |
| 249 | |
| 250 | uint32_t res = 0xFFFFFFFF; |
| 251 | int iter = 0; |
| 252 | do |
| 253 | { |
| 254 | auto pmin = wrapper::vpmin(wrapper::vgethigh(res_idx_mask.val[iter]), wrapper::vgetlow(res_idx_mask.val[iter])); |
| 255 | pmin = wrapper::vpmin(pmin, pmin); |
| 256 | res = std::min(wrapper::vgetlane(pmin, 0), res); |
| 257 | iter++; |
| 258 | } |
| 259 | while(iter < 2); |
| 260 | |
| 261 | return (res - 0xFFFFFFFF); |
| 262 | } |
| 263 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 264 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 265 | template <class F> |
| 266 | class Reducer |
| 267 | { |
| 268 | public: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 269 | static void reduceX(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 270 | { |
| 271 | // Set out window |
| 272 | Window out_window(window); |
| 273 | out_window.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 274 | |
| 275 | // Get first input and output slices |
| 276 | Window in_slice = window.first_slice_window_1D(); |
| 277 | Window out_slice = out_window.first_slice_window_1D(); |
| 278 | |
| 279 | do |
| 280 | { |
| 281 | Iterator in(input, in_slice); |
| 282 | Iterator out(output, out_slice); |
| 283 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 284 | f(in, out, in_slice, out_slice, *input->info(), op); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 285 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 286 | while(window.slide_window_slice_1D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 287 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 288 | static void reduceY(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 289 | { |
| 290 | // Set in window |
| 291 | Window in_window(window); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 292 | Window out_window(window); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 293 | |
| 294 | in_window.set(Window::DimY, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 295 | out_window.set(Window::DimY, Window::Dimension(0, output->info()->dimension(1), output->info()->dimension(1))); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 296 | |
| 297 | // Get first input and output slices |
| 298 | Window in_slice = in_window.first_slice_window_2D(); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 299 | Window out_slice = out_window.first_slice_window_2D(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 300 | |
| 301 | do |
| 302 | { |
| 303 | Iterator in(input, in_slice); |
| 304 | Iterator out(output, out_slice); |
| 305 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 306 | f(in, out, in_slice, out_slice, *input->info(), 1, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 307 | } |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 308 | while(in_window.slide_window_slice_2D(in_slice) && out_window.slide_window_slice_2D(out_slice)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 309 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 310 | static void reduceZ(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 311 | { |
| 312 | // Set in window |
| 313 | Window in_window(window); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 314 | Window out_window(window); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 315 | |
| 316 | in_window.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 317 | out_window.set(Window::DimZ, Window::Dimension(0, output->info()->dimension(2), output->info()->dimension(2))); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 318 | |
| 319 | // Get first input and output slices |
| 320 | Window in_slice = in_window.first_slice_window_3D(); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 321 | Window out_slice = out_window.first_slice_window_3D(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 322 | |
| 323 | do |
| 324 | { |
| 325 | Iterator in(input, in_slice); |
| 326 | Iterator out(output, out_slice); |
| 327 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 328 | f(in, out, in_slice, out_slice, *input->info(), 2, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 329 | } |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 330 | while(in_window.slide_window_slice_3D(in_slice) && out_window.slide_window_slice_3D(out_slice)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 331 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 332 | static void reduceW(const Window &window, const ITensor *input, ITensor *output, F f, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 333 | { |
| 334 | // Set in/out window |
| 335 | Window in_window(window); |
| 336 | Window out_window(window); |
| 337 | |
| 338 | in_window.set(3, Window::Dimension(0, 1, 1)); |
| 339 | out_window.set(3, Window::Dimension(0, 1, 1)); |
| 340 | |
| 341 | // Get first input and output slices |
| 342 | Window in_slice = in_window.first_slice_window_4D(); |
| 343 | Window out_slice = out_window.first_slice_window_4D(); |
| 344 | |
| 345 | do |
| 346 | { |
| 347 | Iterator in(input, in_slice); |
| 348 | Iterator out(output, out_slice); |
| 349 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 350 | f(in, out, in_slice, out_slice, *input->info(), 3, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 351 | } |
| 352 | while(in_window.slide_window_slice_4D(in_slice) && out_window.slide_window_slice_4D(out_slice)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 353 | } |
| 354 | }; |
| 355 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 356 | template <typename T, int S> |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 357 | struct RedOpX |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 358 | { |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 359 | /** NEON vector tag type. */ |
| 360 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 361 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 362 | inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, const ReductionOperation op) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 363 | { |
| 364 | ARM_COMPUTE_UNUSED(out_slice); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 365 | auto init_res_value = static_cast<T>(0.f); |
| 366 | if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN) |
| 367 | { |
| 368 | init_res_value = *reinterpret_cast<T *>(input.ptr()); |
| 369 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 370 | else if(op == ReductionOperation::PROD) |
| 371 | { |
| 372 | init_res_value = static_cast<T>(1.f); |
| 373 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 374 | auto vec_res_value = wrapper::vdup_n(init_res_value, ExactTagType{}); |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 375 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 376 | |
| 377 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 378 | { |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 379 | const auto in_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 380 | const auto vec_elements = wrapper::vloadq(in_ptr); |
| 381 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 382 | switch(op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 383 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 384 | case ReductionOperation::SUM_SQUARE: |
| 385 | vec_res_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_res_value); |
| 386 | break; |
| 387 | case ReductionOperation::MEAN_SUM: |
| 388 | case ReductionOperation::SUM: |
| 389 | vec_res_value = wrapper::vadd(vec_elements, vec_res_value); |
| 390 | break; |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 391 | case ReductionOperation::PROD: |
| 392 | vec_res_value = wrapper::vmul(vec_elements, vec_res_value); |
| 393 | break; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 394 | case ReductionOperation::ARG_IDX_MIN: |
| 395 | { |
| 396 | auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 397 | vec_res_idx = calculate_index(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0); |
| 398 | vec_res_value = temp_vec_res_value; |
| 399 | break; |
| 400 | } |
| 401 | case ReductionOperation::ARG_IDX_MAX: |
| 402 | { |
| 403 | auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 404 | vec_res_idx = calculate_index(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0); |
| 405 | vec_res_value = temp_vec_res_value; |
| 406 | break; |
| 407 | } |
| 408 | default: |
| 409 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 410 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 411 | }, |
| 412 | input); |
| 413 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 414 | switch(op) |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 415 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 416 | case ReductionOperation::SUM: |
| 417 | case ReductionOperation::SUM_SQUARE: |
| 418 | case ReductionOperation::MEAN_SUM: |
| 419 | { |
| 420 | auto carry_res = wrapper::vpadd(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 421 | for(int i = 0; i < S / 4; ++i) |
| 422 | { |
| 423 | carry_res = wrapper::vpadd(carry_res, carry_res); |
| 424 | } |
| 425 | auto res = wrapper::vgetlane(carry_res, 0); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 426 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 427 | if(op == ReductionOperation::MEAN_SUM) |
| 428 | { |
| 429 | res /= in_info.dimension(0); |
| 430 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 431 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 432 | *(reinterpret_cast<T *>(output.ptr())) = res; |
| 433 | break; |
| 434 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 435 | case ReductionOperation::PROD: |
| 436 | { |
| 437 | auto carry_res = wrapper::vmul(wrapper::vgethigh(vec_res_value), wrapper::vgetlow(vec_res_value)); |
| 438 | T res = 1; |
| 439 | for(int i = 0; i < S / 2; ++i) |
| 440 | { |
| 441 | res *= wrapper::vgetlane(carry_res, i); |
| 442 | } |
| 443 | *(reinterpret_cast<T *>(output.ptr())) = res; |
| 444 | break; |
| 445 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 446 | case ReductionOperation::ARG_IDX_MIN: |
| 447 | case ReductionOperation::ARG_IDX_MAX: |
| 448 | { |
| 449 | auto res = calculate_vector_index(vec_res_idx, vec_res_value, op); |
| 450 | *(reinterpret_cast<uint32_t *>(output.ptr())) = res; |
| 451 | break; |
| 452 | } |
| 453 | default: |
| 454 | ARM_COMPUTE_ERROR("Not supported"); |
| 455 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 456 | } |
| 457 | }; |
| 458 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 459 | struct RedOpX_qasymm8 |
| 460 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 461 | inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 462 | { |
| 463 | ARM_COMPUTE_UNUSED(out_slice); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 464 | auto vec_res_value1 = vdupq_n_u32(static_cast<uint32_t>(0.f)); |
| 465 | auto vec_res_value2 = vdupq_n_u32(static_cast<uint32_t>(0.f)); |
| 466 | auto vec_res_value3 = vdupq_n_u32(static_cast<uint32_t>(0.f)); |
| 467 | auto vec_res_value4 = vdupq_n_u32(static_cast<uint32_t>(0.f)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 468 | |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 469 | auto vec_res_value1_f = vdupq_n_f32(static_cast<float>(1.f)); |
| 470 | auto vec_res_value2_f = vdupq_n_f32(static_cast<float>(1.f)); |
| 471 | auto vec_res_value3_f = vdupq_n_f32(static_cast<float>(1.f)); |
| 472 | auto vec_res_value4_f = vdupq_n_f32(static_cast<float>(1.f)); |
| 473 | |
Michalis Spyrou | 728d6f7 | 2019-01-16 13:57:58 +0000 | [diff] [blame] | 474 | uint8x16_t vec_res_value = { 0 }; |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 475 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 476 | if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN) |
| 477 | { |
| 478 | vec_res_value = wrapper::vdup_n(*input.ptr(), wrapper::traits::vector_128_tag{}); |
| 479 | } |
| 480 | |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 481 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 482 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 483 | { |
| 484 | const auto vec_elements = wrapper::vloadq(input.ptr()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 485 | switch(op) |
| 486 | { |
| 487 | case ReductionOperation::SUM: |
| 488 | case ReductionOperation::MEAN_SUM: |
| 489 | { |
| 490 | const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements)); |
| 491 | const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 492 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 493 | const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1)); |
| 494 | const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1)); |
| 495 | const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2)); |
| 496 | const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 497 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 498 | vec_res_value1 = wrapper::vadd(temp32x4t_1, vec_res_value1); |
| 499 | vec_res_value2 = wrapper::vadd(temp32x4t_2, vec_res_value2); |
| 500 | vec_res_value3 = wrapper::vadd(temp32x4t_3, vec_res_value3); |
| 501 | vec_res_value4 = wrapper::vadd(temp32x4t_4, vec_res_value4); |
| 502 | break; |
| 503 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 504 | case ReductionOperation::PROD: |
| 505 | { |
| 506 | const auto offset32x4f_4 = vdupq_n_f32(in_info.quantization_info().offset); |
| 507 | const auto scale32x4f_4 = vdupq_n_f32(in_info.quantization_info().scale); |
| 508 | |
| 509 | const auto temp16x8t_1 = vmovl_u8(vget_low_u8(vec_elements)); |
| 510 | const auto temp16x8t_2 = vmovl_u8(vget_high_u8(vec_elements)); |
| 511 | |
| 512 | const auto temp32x4t_1 = vmovl_u16(vget_low_u16(temp16x8t_1)); |
| 513 | const auto temp32x4t_2 = vmovl_u16(vget_high_u16(temp16x8t_1)); |
| 514 | const auto temp32x4t_3 = vmovl_u16(vget_low_u16(temp16x8t_2)); |
| 515 | const auto temp32x4t_4 = vmovl_u16(vget_high_u16(temp16x8t_2)); |
| 516 | |
| 517 | auto temp32x4f_1 = vcvtq_f32_u32(temp32x4t_1); |
| 518 | auto temp32x4f_2 = vcvtq_f32_u32(temp32x4t_2); |
| 519 | auto temp32x4f_3 = vcvtq_f32_u32(temp32x4t_3); |
| 520 | auto temp32x4f_4 = vcvtq_f32_u32(temp32x4t_4); |
| 521 | |
| 522 | //de-quantize vec_elements |
| 523 | temp32x4f_1 = vmulq_f32(vsubq_f32(temp32x4f_1, offset32x4f_4), scale32x4f_4); |
| 524 | temp32x4f_2 = vmulq_f32(vsubq_f32(temp32x4f_2, offset32x4f_4), scale32x4f_4); |
| 525 | temp32x4f_3 = vmulq_f32(vsubq_f32(temp32x4f_3, offset32x4f_4), scale32x4f_4); |
| 526 | temp32x4f_4 = vmulq_f32(vsubq_f32(temp32x4f_4, offset32x4f_4), scale32x4f_4); |
| 527 | |
| 528 | vec_res_value1_f = vmulq_f32(temp32x4f_1, vec_res_value1_f); |
| 529 | vec_res_value2_f = vmulq_f32(temp32x4f_2, vec_res_value2_f); |
| 530 | vec_res_value3_f = vmulq_f32(temp32x4f_3, vec_res_value3_f); |
| 531 | vec_res_value4_f = vmulq_f32(temp32x4f_4, vec_res_value4_f); |
| 532 | break; |
| 533 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 534 | case ReductionOperation::ARG_IDX_MIN: |
| 535 | { |
| 536 | auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 537 | vec_res_idx = calculate_index(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0); |
| 538 | vec_res_value = temp_vec_res_value; |
| 539 | break; |
| 540 | } |
| 541 | case ReductionOperation::ARG_IDX_MAX: |
| 542 | { |
| 543 | auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 544 | vec_res_idx = calculate_index(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0); |
| 545 | vec_res_value = temp_vec_res_value; |
| 546 | break; |
| 547 | } |
| 548 | default: |
| 549 | ARM_COMPUTE_ERROR("Not supported"); |
| 550 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 551 | }, |
| 552 | input); |
| 553 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 554 | if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 555 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 556 | auto res = calculate_vector_index(vec_res_idx, vec_res_value, op); |
| 557 | *(reinterpret_cast<uint32_t *>(output.ptr())) = res; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 558 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 559 | else if(op == ReductionOperation::PROD) |
| 560 | { |
| 561 | auto carry_res = wrapper::vmul(vec_res_value1_f, vec_res_value2_f); |
| 562 | carry_res = wrapper::vmul(carry_res, vec_res_value3_f); |
| 563 | carry_res = wrapper::vmul(carry_res, vec_res_value4_f); |
| 564 | |
| 565 | float res = wrapper::vgetlane(carry_res, 0); |
| 566 | res *= wrapper::vgetlane(carry_res, 1); |
| 567 | res *= wrapper::vgetlane(carry_res, 2); |
| 568 | res *= wrapper::vgetlane(carry_res, 3); |
| 569 | |
| 570 | //re-quantize result |
| 571 | res = sqcvt_qasymm8_f32(res, in_info.quantization_info().scale, in_info.quantization_info().offset); |
| 572 | *(output.ptr()) = static_cast<uint8_t>(res); |
| 573 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 574 | else |
| 575 | { |
| 576 | auto carry_res = wrapper::vadd(vec_res_value1, vec_res_value2); |
| 577 | carry_res = wrapper::vadd(carry_res, vec_res_value3); |
| 578 | carry_res = wrapper::vadd(carry_res, vec_res_value4); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 579 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 580 | auto carry_paddition = wrapper::vpadd(wrapper::vgethigh(carry_res), wrapper::vgetlow(carry_res)); |
| 581 | carry_paddition = wrapper::vpadd(carry_paddition, carry_paddition); |
| 582 | auto res = wrapper::vgetlane(carry_paddition, 0); |
| 583 | |
| 584 | if(op == ReductionOperation::MEAN_SUM) |
| 585 | { |
| 586 | res /= in_info.dimension(0); |
| 587 | } |
| 588 | |
| 589 | *(output.ptr()) = static_cast<uint8_t>(res); |
| 590 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 591 | } |
| 592 | }; |
| 593 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 594 | template <typename T, int S> |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 595 | struct RedOpYZW |
| 596 | { |
| 597 | /** NEON vector tag type. */ |
| 598 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 599 | using neon_vector = typename wrapper::traits::neon_vector<T, S>::type; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 600 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 601 | inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int axis, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 602 | { |
| 603 | ARM_COMPUTE_UNUSED(out_slice); |
| 604 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 605 | execute_window_loop(in_slice, [&](const Coordinates &) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 606 | { |
Michalis Spyrou | 728d6f7 | 2019-01-16 13:57:58 +0000 | [diff] [blame] | 607 | neon_vector vec_res_value = { 0 }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 608 | if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN) |
| 609 | { |
| 610 | vec_res_value = wrapper::vloadq(reinterpret_cast<T *>(input.ptr())); |
| 611 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 612 | else if(op == ReductionOperation::PROD) |
| 613 | { |
| 614 | vec_res_value = wrapper::vdup_n(static_cast<T>(1.f), ExactTagType{}); |
| 615 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 616 | else |
| 617 | { |
| 618 | vec_res_value = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}); |
| 619 | } |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 620 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 621 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 622 | for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim) |
| 623 | { |
| 624 | T *in_ptr; |
| 625 | switch(axis) |
| 626 | { |
| 627 | case 1: |
| 628 | in_ptr = reinterpret_cast<T *>(input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, dim))); |
| 629 | break; |
| 630 | case 2: |
| 631 | in_ptr = reinterpret_cast<T *>(input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, dim))); |
| 632 | break; |
| 633 | case 3: |
| 634 | in_ptr = reinterpret_cast<T *>(input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, 0, dim))); |
| 635 | break; |
| 636 | default: |
| 637 | ARM_COMPUTE_ERROR("Not supported"); |
| 638 | } |
| 639 | const auto vec_elements = wrapper::vloadq(in_ptr); |
| 640 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 641 | switch(op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 642 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 643 | case ReductionOperation::SUM: |
| 644 | case ReductionOperation::MEAN_SUM: |
| 645 | vec_res_value = wrapper::vadd(vec_elements, vec_res_value); |
| 646 | break; |
| 647 | case ReductionOperation::SUM_SQUARE: |
| 648 | vec_res_value = wrapper::vadd(wrapper::vmul(vec_elements, vec_elements), vec_res_value); |
| 649 | break; |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 650 | case ReductionOperation::PROD: |
| 651 | vec_res_value = wrapper::vmul(vec_elements, vec_res_value); |
| 652 | break; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 653 | case ReductionOperation::ARG_IDX_MIN: |
| 654 | { |
| 655 | auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 656 | vec_res_idx = calculate_index(dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis); |
| 657 | vec_res_value = temp_vec_res_value; |
| 658 | break; |
| 659 | } |
| 660 | case ReductionOperation::ARG_IDX_MAX: |
| 661 | { |
| 662 | auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 663 | vec_res_idx = calculate_index(dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis); |
| 664 | vec_res_value = temp_vec_res_value; |
| 665 | break; |
| 666 | } |
| 667 | default: |
| 668 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 669 | } |
| 670 | } |
| 671 | |
| 672 | if(op == ReductionOperation::MEAN_SUM) |
| 673 | { |
| 674 | auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast<T>(in_info.dimension(axis)), ExactTagType{})); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 675 | vec_res_value = wrapper::vmul(vec_res_value, vec_width_inv); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 676 | } |
| 677 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 678 | if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX) |
| 679 | { |
| 680 | wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()), vec_res_idx.val[0]); |
| 681 | } |
| 682 | else |
| 683 | { |
| 684 | wrapper::vstore(reinterpret_cast<T *>(output.ptr()), vec_res_value); |
| 685 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 686 | }, |
| 687 | input, output); |
| 688 | } |
| 689 | }; |
| 690 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 691 | template <typename T, int S, int axis, ReductionOperation op> |
| 692 | struct RedOpYZW_complex |
| 693 | { |
| 694 | /** NEON vector tag type. */ |
| 695 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 696 | using neon_vector = typename wrapper::traits::neon_vector<T, S>::type; |
| 697 | |
| 698 | inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int, const ReductionOperation) |
| 699 | { |
| 700 | ARM_COMPUTE_UNUSED(out_slice); |
| 701 | ARM_COMPUTE_ERROR_ON(axis != 2); |
| 702 | |
| 703 | const size_t stride_z = in_info.strides_in_bytes()[axis]; |
| 704 | |
| 705 | execute_window_loop(in_slice, [&](const Coordinates &) |
| 706 | { |
| 707 | neon_vector vec_res_value_0 = { 0 }; |
| 708 | neon_vector vec_res_value_1 = { 0 }; |
| 709 | |
| 710 | vec_res_value_0 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}); |
| 711 | vec_res_value_1 = wrapper::vdup_n(static_cast<T>(0.f), ExactTagType{}); |
| 712 | |
| 713 | for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim) |
| 714 | { |
| 715 | T *in_ptr_0; |
| 716 | T *in_ptr_1; |
| 717 | switch(axis) |
| 718 | { |
| 719 | case 2: |
| 720 | in_ptr_0 = reinterpret_cast<T *>(input.ptr() + stride_z * dim); |
| 721 | in_ptr_1 = reinterpret_cast<T *>(input.ptr() + 16 + stride_z * dim); |
| 722 | break; |
| 723 | default: |
| 724 | ARM_COMPUTE_ERROR("Not supported"); |
| 725 | } |
| 726 | const auto vec_elements_0 = wrapper::vloadq(in_ptr_0); |
| 727 | const auto vec_elements_1 = wrapper::vloadq(in_ptr_1); |
| 728 | |
| 729 | switch(op) |
| 730 | { |
| 731 | case ReductionOperation::SUM: |
| 732 | vec_res_value_0 = wrapper::vadd(vec_elements_0, vec_res_value_0); |
| 733 | vec_res_value_1 = wrapper::vadd(vec_elements_1, vec_res_value_1); |
| 734 | break; |
| 735 | default: |
| 736 | ARM_COMPUTE_ERROR("Not supported"); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | wrapper::vstore(reinterpret_cast<T *>(output.ptr()), vec_res_value_0); |
| 741 | wrapper::vstore(reinterpret_cast<T *>(output.ptr() + 16), vec_res_value_1); |
| 742 | |
| 743 | }, |
| 744 | input, output); |
| 745 | } |
| 746 | }; |
| 747 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 748 | struct RedOpYZW_qasymm8 |
| 749 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 750 | inline void operator()(Iterator &input, Iterator &output, Window &in_slice, Window &out_slice, const TensorInfo &in_info, int axis, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 751 | { |
| 752 | ARM_COMPUTE_UNUSED(out_slice); |
| 753 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 754 | execute_window_loop(in_slice, [&](const Coordinates &) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 755 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 756 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 757 | auto vec_res_value1 = vdupq_n_u32(0); |
| 758 | auto vec_res_value2 = vdupq_n_u32(0); |
| 759 | auto vec_res_value3 = vdupq_n_u32(0); |
| 760 | auto vec_res_value4 = vdupq_n_u32(0); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 761 | |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 762 | auto vec_res_value1_f = vdupq_n_f32(1); |
| 763 | auto vec_res_value2_f = vdupq_n_f32(1); |
| 764 | auto vec_res_value3_f = vdupq_n_f32(1); |
| 765 | auto vec_res_value4_f = vdupq_n_f32(1); |
| 766 | |
| 767 | auto vec_res_value = wrapper::vloadq(input.ptr()); |
| 768 | |
| 769 | for(unsigned int index_dim = 0; index_dim < in_info.dimension(axis); ++index_dim) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 770 | { |
| 771 | uint8_t *in_ptr; |
| 772 | switch(axis) |
| 773 | { |
| 774 | case 1: |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 775 | in_ptr = input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, index_dim)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 776 | break; |
| 777 | case 2: |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 778 | in_ptr = input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, index_dim)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 779 | break; |
| 780 | case 3: |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 781 | in_ptr = input.ptr() + in_info.offset_element_in_bytes(Coordinates(0, 0, 0, index_dim)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 782 | break; |
| 783 | default: |
| 784 | ARM_COMPUTE_ERROR("Not supported"); |
| 785 | } |
| 786 | const auto vec_elements = wrapper::vloadq(in_ptr); |
| 787 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 788 | switch(op) |
| 789 | { |
| 790 | case ReductionOperation::SUM: |
| 791 | case ReductionOperation::MEAN_SUM: |
| 792 | { |
| 793 | const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements)); |
| 794 | const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 795 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 796 | const auto temp32x4t_1 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_1)); |
| 797 | const auto temp32x4t_2 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_1)); |
| 798 | const auto temp32x4t_3 = wrapper::vmovl(wrapper::vgetlow(temp16x8t_2)); |
| 799 | const auto temp32x4t_4 = wrapper::vmovl(wrapper::vgethigh(temp16x8t_2)); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 800 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 801 | vec_res_value1 = wrapper::vadd(temp32x4t_1, vec_res_value1); |
| 802 | vec_res_value2 = wrapper::vadd(temp32x4t_2, vec_res_value2); |
| 803 | vec_res_value3 = wrapper::vadd(temp32x4t_3, vec_res_value3); |
| 804 | vec_res_value4 = wrapper::vadd(temp32x4t_4, vec_res_value4); |
| 805 | break; |
| 806 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 807 | case ReductionOperation::PROD: |
| 808 | { |
| 809 | const auto offset32x4f_4 = vdupq_n_f32(in_info.quantization_info().offset); |
| 810 | const auto scale32x4f_4 = vdupq_n_f32(in_info.quantization_info().scale); |
| 811 | |
| 812 | const auto temp16x8t_1 = vmovl_u8(vget_low_u8(vec_elements)); |
| 813 | const auto temp16x8t_2 = vmovl_u8(vget_high_u8(vec_elements)); |
| 814 | |
| 815 | const auto temp32x4t_1 = vmovl_u16(vget_low_u16(temp16x8t_1)); |
| 816 | const auto temp32x4t_2 = vmovl_u16(vget_high_u16(temp16x8t_1)); |
| 817 | const auto temp32x4t_3 = vmovl_u16(vget_low_u16(temp16x8t_2)); |
| 818 | const auto temp32x4t_4 = vmovl_u16(vget_high_u16(temp16x8t_2)); |
| 819 | |
| 820 | auto temp32x4f_1 = vcvtq_f32_u32(temp32x4t_1); |
| 821 | auto temp32x4f_2 = vcvtq_f32_u32(temp32x4t_2); |
| 822 | auto temp32x4f_3 = vcvtq_f32_u32(temp32x4t_3); |
| 823 | auto temp32x4f_4 = vcvtq_f32_u32(temp32x4t_4); |
| 824 | |
| 825 | //de-quantize vec_elements |
| 826 | temp32x4f_1 = vmulq_f32(vsubq_f32(temp32x4f_1, offset32x4f_4), scale32x4f_4); |
| 827 | temp32x4f_2 = vmulq_f32(vsubq_f32(temp32x4f_2, offset32x4f_4), scale32x4f_4); |
| 828 | temp32x4f_3 = vmulq_f32(vsubq_f32(temp32x4f_3, offset32x4f_4), scale32x4f_4); |
| 829 | temp32x4f_4 = vmulq_f32(vsubq_f32(temp32x4f_4, offset32x4f_4), scale32x4f_4); |
| 830 | |
| 831 | vec_res_value1_f = vmulq_f32(temp32x4f_1, vec_res_value1_f); |
| 832 | vec_res_value2_f = vmulq_f32(temp32x4f_2, vec_res_value2_f); |
| 833 | vec_res_value3_f = vmulq_f32(temp32x4f_3, vec_res_value3_f); |
| 834 | vec_res_value4_f = vmulq_f32(temp32x4f_4, vec_res_value4_f); |
| 835 | break; |
| 836 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 837 | case ReductionOperation::ARG_IDX_MIN: |
| 838 | { |
| 839 | auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 840 | vec_res_idx = calculate_index(index_dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 841 | vec_res_value = temp_vec_res_value; |
| 842 | break; |
| 843 | } |
| 844 | case ReductionOperation::ARG_IDX_MAX: |
| 845 | { |
| 846 | auto temp_vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 847 | vec_res_idx = calculate_index(index_dim, temp_vec_res_value, vec_res_value, vec_res_idx, op, axis); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 848 | vec_res_value = temp_vec_res_value; |
| 849 | break; |
| 850 | } |
| 851 | default: |
| 852 | ARM_COMPUTE_ERROR("Not supported"); |
| 853 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | if(op == ReductionOperation::MEAN_SUM) |
| 857 | { |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 858 | const auto vec_width_inv = wrapper::vinv(vdupq_n_f32(in_info.dimension(axis))); |
| 859 | vec_res_value1_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value1), vec_width_inv); |
| 860 | vec_res_value2_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value2), vec_width_inv); |
| 861 | vec_res_value3_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value3), vec_width_inv); |
| 862 | vec_res_value4_f = wrapper::vmul(vcvtq_f32_u32(vec_res_value4), vec_width_inv); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 863 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 864 | vec_res_value1 = vcvtq_u32_f32(vec_res_value1_f); |
| 865 | vec_res_value2 = vcvtq_u32_f32(vec_res_value2_f); |
| 866 | vec_res_value3 = vcvtq_u32_f32(vec_res_value3_f); |
| 867 | vec_res_value4 = vcvtq_u32_f32(vec_res_value4_f); |
| 868 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 869 | else if(op == ReductionOperation::PROD) |
| 870 | { |
| 871 | const auto offset32x4f_4 = vdupq_n_f32(in_info.quantization_info().offset); |
| 872 | const auto iscale32x4f_4 = vinvq_f32(vdupq_n_f32(in_info.quantization_info().scale)); |
| 873 | |
| 874 | //re-quantize |
| 875 | vec_res_value1_f = vaddq_f32(vmulq_f32(vec_res_value1_f, iscale32x4f_4), offset32x4f_4); |
| 876 | vec_res_value2_f = vaddq_f32(vmulq_f32(vec_res_value2_f, iscale32x4f_4), offset32x4f_4); |
| 877 | vec_res_value3_f = vaddq_f32(vmulq_f32(vec_res_value3_f, iscale32x4f_4), offset32x4f_4); |
| 878 | vec_res_value4_f = vaddq_f32(vmulq_f32(vec_res_value4_f, iscale32x4f_4), offset32x4f_4); |
| 879 | |
| 880 | vec_res_value1 = vcvtq_u32_f32(vec_res_value1_f); |
| 881 | vec_res_value2 = vcvtq_u32_f32(vec_res_value2_f); |
| 882 | vec_res_value3 = vcvtq_u32_f32(vec_res_value3_f); |
| 883 | vec_res_value4 = vcvtq_u32_f32(vec_res_value4_f); |
| 884 | } |
| 885 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 886 | if(op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX) |
| 887 | { |
| 888 | wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()), vec_res_idx.val[0]); |
| 889 | wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 4, vec_res_idx.val[1]); |
| 890 | wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 8, vec_res_idx.val[2]); |
| 891 | wrapper::vstore(reinterpret_cast<uint32_t *>(output.ptr()) + 12, vec_res_idx.val[3]); |
| 892 | } |
| 893 | else |
| 894 | { |
| 895 | const auto temp16x8t_1 = vcombine_u16(wrapper::vqmovn(vec_res_value1), wrapper::vqmovn(vec_res_value2)); |
| 896 | const auto temp16x8t_2 = vcombine_u16(wrapper::vqmovn(vec_res_value3), wrapper::vqmovn(vec_res_value4)); |
| 897 | auto res = vcombine_u8(wrapper::vqmovn(temp16x8t_1), wrapper::vqmovn(temp16x8t_2)); |
| 898 | wrapper::vstore(output.ptr(), res); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 899 | } |
| 900 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 901 | }, |
| 902 | input, output); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 903 | } |
| 904 | }; |
| 905 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 906 | void reduce_op(const Window &window, const ITensor *input, ITensor *output, unsigned int axis, const ReductionOperation op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 907 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 908 | const bool is_complex = (input->info()->num_channels() == 2); |
| 909 | |
| 910 | if(is_complex) |
| 911 | { |
| 912 | switch(axis) |
| 913 | { |
| 914 | case 2: |
| 915 | switch(input->info()->data_type()) |
| 916 | { |
| 917 | case DataType::F32: |
| 918 | switch(op) |
| 919 | { |
| 920 | case ReductionOperation::SUM: |
| 921 | return Reducer<RedOpYZW_complex<float, 4, 2, ReductionOperation::SUM>>::reduceZ(window, input, output, RedOpYZW_complex<float, 4, 2, ReductionOperation::SUM>(), op); |
| 922 | default: |
| 923 | ARM_COMPUTE_ERROR("Not supported"); |
| 924 | } |
| 925 | default: |
| 926 | ARM_COMPUTE_ERROR("Not supported"); |
| 927 | } |
| 928 | default: |
| 929 | ARM_COMPUTE_ERROR("Not supported"); |
| 930 | } |
| 931 | } |
| 932 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 933 | switch(axis) |
| 934 | { |
| 935 | case 0: |
| 936 | switch(input->info()->data_type()) |
| 937 | { |
| 938 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 939 | return Reducer<RedOpX_qasymm8>::reduceX(window, input, output, RedOpX_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 940 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 941 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 942 | return Reducer<RedOpX<float16_t, 8>>::reduceX(window, input, output, RedOpX<float16_t, 8>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 943 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 944 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 945 | return Reducer<RedOpX<float, 4>>::reduceX(window, input, output, RedOpX<float, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 946 | default: |
| 947 | ARM_COMPUTE_ERROR("Not supported"); |
| 948 | } |
| 949 | case 1: |
| 950 | switch(input->info()->data_type()) |
| 951 | { |
| 952 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 953 | return Reducer<RedOpYZW_qasymm8>::reduceY(window, input, output, RedOpYZW_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 954 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 955 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 956 | return Reducer<RedOpYZW<float16_t, 8>>::reduceY(window, input, output, RedOpYZW<float16_t, 8>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 957 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 958 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 959 | return Reducer<RedOpYZW<float, 4>>::reduceY(window, input, output, RedOpYZW<float, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 960 | default: |
| 961 | ARM_COMPUTE_ERROR("Not supported"); |
| 962 | } |
| 963 | case 2: |
| 964 | switch(input->info()->data_type()) |
| 965 | { |
| 966 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 967 | return Reducer<RedOpYZW_qasymm8>::reduceZ(window, input, output, RedOpYZW_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 968 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 969 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 970 | return Reducer<RedOpYZW<float16_t, 8>>::reduceZ(window, input, output, RedOpYZW<float16_t, 8>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 971 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 972 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 973 | return Reducer<RedOpYZW<float, 4>>::reduceZ(window, input, output, RedOpYZW<float, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 974 | default: |
| 975 | ARM_COMPUTE_ERROR("Not supported"); |
| 976 | } |
| 977 | case 3: |
| 978 | switch(input->info()->data_type()) |
| 979 | { |
| 980 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 981 | return Reducer<RedOpYZW_qasymm8>::reduceW(window, input, output, RedOpYZW_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 982 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 983 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 984 | return Reducer<RedOpYZW<float16_t, 8>>::reduceW(window, input, output, RedOpYZW<float16_t, 8>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 985 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 986 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 987 | return Reducer<RedOpYZW<float, 4>>::reduceW(window, input, output, RedOpYZW<float, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 988 | default: |
| 989 | ARM_COMPUTE_ERROR("Not supported"); |
| 990 | } |
| 991 | default: |
| 992 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
| 993 | } |
| 994 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 995 | |
| 996 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
| 997 | { |
| 998 | ARM_COMPUTE_UNUSED(op); |
| 999 | |
| 1000 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 1001 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1002 | |
| 1003 | if(input->num_channels() == 1) |
| 1004 | { |
| 1005 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32); |
| 1006 | } |
| 1007 | else |
| 1008 | { |
| 1009 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 2, DataType::F32); |
| 1010 | ARM_COMPUTE_RETURN_ERROR_ON(op != ReductionOperation::SUM); |
| 1011 | ARM_COMPUTE_RETURN_ERROR_ON(axis != 2); |
| 1012 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1013 | |
| 1014 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis >= TensorShape::num_max_dimensions, "Reduction axis greater than max number of dimensions"); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1015 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1016 | |
| 1017 | if(output->total_size() != 0) |
| 1018 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1019 | bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN); |
| 1020 | if(!is_arg_min_max) |
| 1021 | { |
| 1022 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 1023 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1024 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() != output->num_channels()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1025 | } |
| 1026 | else |
| 1027 | { |
| 1028 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32); |
| 1029 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1030 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1031 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1032 | const TensorInfo tensor_info_reshaped = input->clone()->set_tensor_shape(output_shape); |
| 1033 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_reshaped); |
| 1034 | } |
| 1035 | |
| 1036 | return Status{}; |
| 1037 | } |
| 1038 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1039 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, unsigned int axis, ReductionOperation op) |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1040 | { |
| 1041 | // Calculate output shape and set if empty |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1042 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_reduced_shape(input->tensor_shape(), axis); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1043 | |
| 1044 | // Output auto initialization if not yet initialized |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1045 | const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX); |
| 1046 | DataType output_data_type = is_arg_min_max ? DataType::U32 : input->data_type(); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1047 | 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 Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1048 | |
| 1049 | unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->data_type()); |
| 1050 | |
| 1051 | // Configure kernel window |
| 1052 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 1053 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 1054 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 1055 | |
| 1056 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 1057 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 1058 | |
| 1059 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 1060 | |
| 1061 | return std::make_tuple(err, win); |
| 1062 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1063 | } // namespace |
| 1064 | |
| 1065 | NEReductionOperationKernel::NEReductionOperationKernel() |
| 1066 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 1067 | { |
| 1068 | } |
| 1069 | |
| 1070 | BorderSize NEReductionOperationKernel::border_size() const |
| 1071 | { |
| 1072 | return _border_size; |
| 1073 | } |
| 1074 | |
| 1075 | void NEReductionOperationKernel::configure(const ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op) |
| 1076 | { |
| 1077 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1078 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1079 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), axis, op)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1080 | |
| 1081 | unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->info()->data_type()); |
| 1082 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1083 | _input = input; |
| 1084 | _output = output; |
| 1085 | _border_size = (axis == 0) ? BorderSize(0, num_elems_processed_per_iteration - (input->info()->dimension(0) % num_elems_processed_per_iteration), 0, 0) : BorderSize(); |
| 1086 | _op = op; |
| 1087 | _reduction_axis = axis; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1088 | |
| 1089 | // Configure kernel window |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1090 | auto win_config = validate_and_configure_window(_input->info(), _output->info(), axis, op); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1091 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1092 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1093 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1094 | INEKernel::configure(std::get<1>(win_config)); |
| 1095 | } |
| 1096 | |
| 1097 | Status NEReductionOperationKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int axis, ReductionOperation op) |
| 1098 | { |
| 1099 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, axis, op)); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1100 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), axis, op))); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1101 | |
| 1102 | return Status{}; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1103 | } |
| 1104 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1105 | void NEReductionOperationKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1106 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1107 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1108 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 1109 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 1110 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1111 | reduce_op(window, _input, _output, _reduction_axis, _op); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1112 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1113 | } // namespace arm_compute |