Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 0795878 | 2020-01-09 14:43:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 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 | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 35 | #include "arm_compute/core/utils/misc/SaturateCast.h" |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 36 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 37 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 38 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 39 | #include <arm_neon.h> |
| 40 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 41 | namespace arm_compute |
| 42 | { |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 43 | namespace |
| 44 | { |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 45 | template <typename T> |
| 46 | uint32x4x4_t calculate_index(uint32_t idx, T a, T b, uint32x4x4_t c, ReductionOperation op, int axis) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 47 | { |
| 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 Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 63 | 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] | 64 | |
| 65 | return res; |
| 66 | } |
| 67 | |
Georgios Pinitas | fad1838 | 2019-06-05 15:12:22 +0100 | [diff] [blame] | 68 | template <> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 69 | uint32x4x4_t calculate_index(uint32_t idx, uint8x16_t a, uint8x16_t b, uint32x4x4_t c, ReductionOperation op, int axis) |
| 70 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 71 | uint32x4x4_t mask{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 72 | 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 Spyrou | 254a48a | 2019-01-14 17:27:39 +0000 | [diff] [blame] | 81 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 88 | 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 Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 101 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 110 | |
| 111 | return res; |
| 112 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 113 | |
| 114 | // Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value. |
| 115 | float32x2_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. |
| 122 | float32x2_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. |
| 128 | int32x2_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. |
| 135 | int32x2_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 Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 141 | template <typename T> |
| 142 | uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, T vec_res_value, ReductionOperation op) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 143 | { |
| 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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 149 | auto pmin = calculate_min(vec_res_value); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 150 | 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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 155 | auto pmax = calculate_max(vec_res_value); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 156 | auto mask = wrapper::vceq(vec_res_value, wrapper::vcombine(pmax, pmax)); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 157 | 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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 168 | // Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value. |
| 169 | inline 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. |
| 177 | inline 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 Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 185 | template <> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 186 | uint32_t calculate_vector_index(uint32x4x4_t vec_res_idx, uint8x16_t vec_res_value, ReductionOperation op) |
| 187 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 188 | uint32x4x4_t res_idx_mask{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 189 | uint32x4_t mask_ones = vdupq_n_u32(0xFFFFFFFF); |
| 190 | uint8x16_t mask_u8{ 0 }; |
| 191 | if(op == ReductionOperation::ARG_IDX_MIN) |
| 192 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 193 | auto pmin = calculate_min(vec_res_value); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 194 | mask_u8 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin)); |
| 195 | } |
| 196 | else |
| 197 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 198 | auto pmax = calculate_max(vec_res_value); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 199 | 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 Pinitas | fad1838 | 2019-06-05 15:12:22 +0100 | [diff] [blame] | 232 | template <> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 233 | uint32x4x4_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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 264 | // Helper function to calculate the minimum value of the input vector. All the elements in the output vector contain the min value. |
| 265 | inline 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. |
| 272 | inline 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 Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 279 | template <> |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 280 | uint32_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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 287 | auto pmin = calculate_min(vec_res_value); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 288 | mask_u16 = wrapper::vceq(vec_res_value, wrapper::vcombine(pmin, pmin)); |
| 289 | } |
| 290 | else |
| 291 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 292 | auto pmax = calculate_max(vec_res_value); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 293 | 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 Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 319 | template <class F> |
| 320 | class Reducer |
| 321 | { |
| 322 | public: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 323 | 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] | 324 | { |
| 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 338 | f(in, out, in_slice, out_slice, *input->info(), op); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 339 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 340 | while(window.slide_window_slice_1D(in_slice) && out_window.slide_window_slice_1D(out_slice)); |
| 341 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 342 | 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] | 343 | { |
| 344 | // Set in window |
| 345 | Window in_window(window); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 346 | Window out_window(window); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 347 | |
| 348 | in_window.set(Window::DimY, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 349 | 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] | 350 | |
| 351 | // Get first input and output slices |
| 352 | Window in_slice = in_window.first_slice_window_2D(); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 353 | Window out_slice = out_window.first_slice_window_2D(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 354 | |
| 355 | do |
| 356 | { |
| 357 | Iterator in(input, in_slice); |
| 358 | Iterator out(output, out_slice); |
| 359 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 360 | f(in, out, in_slice, out_slice, *input->info(), 1, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 361 | } |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 362 | 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] | 363 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 364 | 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] | 365 | { |
| 366 | // Set in window |
| 367 | Window in_window(window); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 368 | Window out_window(window); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 369 | |
| 370 | in_window.set(Window::DimZ, Window::Dimension(0, 1, 1)); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 371 | 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] | 372 | |
| 373 | // Get first input and output slices |
| 374 | Window in_slice = in_window.first_slice_window_3D(); |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 375 | Window out_slice = out_window.first_slice_window_3D(); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 376 | |
| 377 | do |
| 378 | { |
| 379 | Iterator in(input, in_slice); |
| 380 | Iterator out(output, out_slice); |
| 381 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 382 | f(in, out, in_slice, out_slice, *input->info(), 2, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 383 | } |
Michalis Spyrou | 2897e61 | 2018-11-20 18:38:29 +0000 | [diff] [blame] | 384 | 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] | 385 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 386 | 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] | 387 | { |
| 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 404 | f(in, out, in_slice, out_slice, *input->info(), 3, op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 405 | } |
| 406 | 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] | 407 | } |
| 408 | }; |
| 409 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 410 | template <typename T, int S> |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 411 | struct RedOpX |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 412 | { |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 413 | /** NEON vector tag type. */ |
| 414 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
| 415 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 416 | 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] | 417 | { |
| 418 | ARM_COMPUTE_UNUSED(out_slice); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 419 | auto init_res_value = static_cast<T>(0.f); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 420 | switch(op) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 421 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 422 | case ReductionOperation::ARG_IDX_MAX: |
| 423 | case ReductionOperation::ARG_IDX_MIN: |
| 424 | case ReductionOperation::MIN: |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 425 | case ReductionOperation::MAX: |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 426 | { |
| 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 437 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 438 | auto vec_res_value = wrapper::vdup_n(init_res_value, ExactTagType{}); |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 439 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 440 | |
| 441 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 442 | { |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 443 | const auto in_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 444 | const auto vec_elements = wrapper::vloadq(in_ptr); |
| 445 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 446 | switch(op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 447 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 448 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 455 | case ReductionOperation::PROD: |
| 456 | vec_res_value = wrapper::vmul(vec_elements, vec_res_value); |
| 457 | break; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 458 | case ReductionOperation::ARG_IDX_MIN: |
| 459 | { |
| 460 | auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 461 | vec_res_idx = calculate_index<decltype(vec_res_value)>(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 462 | 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 Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 468 | vec_res_idx = calculate_index<decltype(vec_res_value)>(id.x(), temp_vec_res_value, vec_res_value, vec_res_idx, op, 0); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 469 | vec_res_value = temp_vec_res_value; |
| 470 | break; |
| 471 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 472 | case ReductionOperation::MIN: |
| 473 | { |
| 474 | vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 475 | break; |
| 476 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 477 | case ReductionOperation::MAX: |
| 478 | { |
| 479 | vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 480 | break; |
| 481 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 482 | default: |
| 483 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 484 | } |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 485 | }, |
| 486 | input); |
| 487 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 488 | switch(op) |
Michele Di Giorgio | 1c948d4 | 2018-11-20 16:03:01 +0000 | [diff] [blame] | 489 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 490 | 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 Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 500 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 501 | if(op == ReductionOperation::MEAN_SUM) |
| 502 | { |
| 503 | res /= in_info.dimension(0); |
| 504 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 505 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 506 | *(reinterpret_cast<T *>(output.ptr())) = res; |
| 507 | break; |
| 508 | } |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 509 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 520 | case ReductionOperation::ARG_IDX_MIN: |
| 521 | case ReductionOperation::ARG_IDX_MAX: |
| 522 | { |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 523 | auto res = calculate_vector_index<decltype(vec_res_value)>(vec_res_idx, vec_res_value, op); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 524 | *(reinterpret_cast<uint32_t *>(output.ptr())) = res; |
| 525 | break; |
| 526 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 527 | case ReductionOperation::MIN: |
| 528 | { |
| 529 | *(reinterpret_cast<T *>(output.ptr())) = wrapper::vgetlane(calculate_min(vec_res_value), 0); |
| 530 | break; |
| 531 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 532 | case ReductionOperation::MAX: |
| 533 | { |
| 534 | *(reinterpret_cast<T *>(output.ptr())) = wrapper::vgetlane(calculate_max(vec_res_value), 0); |
| 535 | break; |
| 536 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 537 | default: |
| 538 | ARM_COMPUTE_ERROR("Not supported"); |
| 539 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 540 | } |
| 541 | }; |
| 542 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 543 | struct RedOpX_qasymm8 |
| 544 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 545 | 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] | 546 | { |
| 547 | ARM_COMPUTE_UNUSED(out_slice); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 548 | |
| 549 | const UniformQuantizationInfo iq_info = in_info.quantization_info().uniform(); |
| 550 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 551 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 555 | |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 556 | 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 Spyrou | 728d6f7 | 2019-01-16 13:57:58 +0000 | [diff] [blame] | 561 | uint8x16_t vec_res_value = { 0 }; |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 562 | |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 563 | if(op == ReductionOperation::ARG_IDX_MAX || op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::MIN || op == ReductionOperation::MAX) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 564 | { |
| 565 | vec_res_value = wrapper::vdup_n(*input.ptr(), wrapper::traits::vector_128_tag{}); |
| 566 | } |
| 567 | |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 568 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 569 | execute_window_loop(in_slice, [&](const Coordinates & id) |
| 570 | { |
| 571 | const auto vec_elements = wrapper::vloadq(input.ptr()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 572 | 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 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 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 584 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 585 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 591 | case ReductionOperation::PROD: |
| 592 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 593 | const auto offset32x4f_4 = vdupq_n_f32(iq_info.offset); |
| 594 | const auto scale32x4f_4 = vdupq_n_f32(iq_info.scale); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 595 | |
| 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 621 | 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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 635 | case ReductionOperation::MIN: |
| 636 | { |
| 637 | vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 638 | break; |
| 639 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 640 | case ReductionOperation::MAX: |
| 641 | { |
| 642 | vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 643 | break; |
| 644 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 645 | default: |
| 646 | ARM_COMPUTE_ERROR("Not supported"); |
| 647 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 648 | }, |
| 649 | input); |
| 650 | |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 651 | switch(op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 652 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 653 | case ReductionOperation::ARG_IDX_MIN: |
| 654 | case ReductionOperation::ARG_IDX_MAX: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 655 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 656 | auto res = calculate_vector_index(vec_res_idx, vec_res_value, op); |
| 657 | *(reinterpret_cast<uint32_t *>(output.ptr())) = res; |
| 658 | break; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 659 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 660 | case ReductionOperation::MIN: |
| 661 | { |
| 662 | *(output.ptr()) = static_cast<uint8_t>(wrapper::vgetlane(calculate_min(vec_res_value), 0)); |
| 663 | break; |
| 664 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 665 | case ReductionOperation::MAX: |
| 666 | { |
| 667 | *(output.ptr()) = static_cast<uint8_t>(wrapper::vgetlane(calculate_max(vec_res_value), 0)); |
| 668 | break; |
| 669 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 670 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 675 | |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 676 | 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 Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 682 | res = quantize_qasymm8(res, iq_info); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 683 | *(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 Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 694 | auto res = static_cast<int32_t>(wrapper::vgetlane(carry_paddition, 0)); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 695 | |
| 696 | if(op == ReductionOperation::MEAN_SUM) |
| 697 | { |
| 698 | res /= in_info.dimension(0); |
| 699 | } |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 700 | else |
| 701 | { |
| 702 | // Subtract accumulated offsets |
| 703 | res -= (in_info.dimension(0) - 1) * iq_info.offset; |
| 704 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 705 | |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 706 | *(output.ptr()) = utils::cast::saturate_cast<uint8_t>(res); |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 707 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 708 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 709 | } |
| 710 | }; |
| 711 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 712 | template <typename T, int S> |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 713 | struct RedOpYZW |
| 714 | { |
| 715 | /** NEON vector tag type. */ |
| 716 | using ExactTagType = typename wrapper::traits::neon_vector<T, S>::tag_type; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 717 | using neon_vector = typename wrapper::traits::neon_vector<T, S>::type; |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 718 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 719 | 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] | 720 | { |
| 721 | ARM_COMPUTE_UNUSED(out_slice); |
| 722 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 723 | execute_window_loop(in_slice, [&](const Coordinates &) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 724 | { |
Michalis Spyrou | 728d6f7 | 2019-01-16 13:57:58 +0000 | [diff] [blame] | 725 | neon_vector vec_res_value = { 0 }; |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 726 | switch(op) |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 727 | { |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 728 | case ReductionOperation::ARG_IDX_MAX: |
| 729 | case ReductionOperation::ARG_IDX_MIN: |
| 730 | case ReductionOperation::MIN: |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 731 | case ReductionOperation::MAX: |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 732 | { |
| 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 746 | } |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 747 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 748 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 749 | for(unsigned int dim = 0; dim < in_info.dimension(axis); ++dim) |
| 750 | { |
Pablo Tello | 0795878 | 2020-01-09 14:43:36 +0000 | [diff] [blame] | 751 | const T *in_ptr = reinterpret_cast<T *>(input.ptr() + in_info.strides_in_bytes()[axis] * dim); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 752 | const auto vec_elements = wrapper::vloadq(in_ptr); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 753 | switch(op) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 754 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 755 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 762 | case ReductionOperation::PROD: |
| 763 | vec_res_value = wrapper::vmul(vec_elements, vec_res_value); |
| 764 | break; |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 765 | 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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 779 | case ReductionOperation::MIN: |
| 780 | { |
| 781 | vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 782 | break; |
| 783 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 784 | case ReductionOperation::MAX: |
| 785 | { |
| 786 | vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 787 | break; |
| 788 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 789 | default: |
| 790 | ARM_COMPUTE_ERROR("Not supported"); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 791 | } |
| 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 797 | vec_res_value = wrapper::vmul(vec_res_value, vec_width_inv); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 798 | } |
| 799 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 800 | 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 Giorgio | 81d7e78 | 2019-08-16 18:03:35 +0100 | [diff] [blame] | 803 | #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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 809 | } |
| 810 | else |
| 811 | { |
| 812 | wrapper::vstore(reinterpret_cast<T *>(output.ptr()), vec_res_value); |
| 813 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 814 | }, |
| 815 | input, output); |
| 816 | } |
| 817 | }; |
| 818 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 819 | template <typename T, int S, int axis, ReductionOperation op> |
| 820 | struct 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 876 | struct RedOpYZW_qasymm8 |
| 877 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 878 | 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] | 879 | { |
| 880 | ARM_COMPUTE_UNUSED(out_slice); |
| 881 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 882 | const UniformQuantizationInfo iq_info = in_info.quantization_info().uniform(); |
| 883 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 884 | execute_window_loop(in_slice, [&](const Coordinates &) |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 885 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 886 | uint32x4x4_t vec_res_idx{ { 0 } }; |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 887 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 891 | |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 892 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 896 | |
| 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 900 | { |
Pablo Tello | 0795878 | 2020-01-09 14:43:36 +0000 | [diff] [blame] | 901 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 903 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 910 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 911 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 915 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 916 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 922 | case ReductionOperation::PROD: |
| 923 | { |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 924 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 926 | |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 927 | const auto temp16x8t_1 = wrapper::vmovl(wrapper::vgetlow(vec_elements)); |
| 928 | const auto temp16x8t_2 = wrapper::vmovl(wrapper::vgethigh(vec_elements)); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 929 | |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 930 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 934 | |
| 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 Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 941 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 945 | |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 946 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 950 | break; |
| 951 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 952 | case ReductionOperation::ARG_IDX_MIN: |
| 953 | { |
| 954 | auto temp_vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 955 | 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] | 956 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 962 | 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] | 963 | vec_res_value = temp_vec_res_value; |
| 964 | break; |
| 965 | } |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 966 | case ReductionOperation::MIN: |
| 967 | { |
| 968 | vec_res_value = wrapper::vmin(vec_elements, vec_res_value); |
| 969 | break; |
| 970 | } |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 971 | case ReductionOperation::MAX: |
| 972 | { |
| 973 | vec_res_value = wrapper::vmax(vec_elements, vec_res_value); |
| 974 | break; |
| 975 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 976 | default: |
| 977 | ARM_COMPUTE_ERROR("Not supported"); |
| 978 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 979 | } |
| 980 | |
| 981 | if(op == ReductionOperation::MEAN_SUM) |
| 982 | { |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 983 | const auto vec_width_inv = wrapper::vinv(wrapper::vdup_n(static_cast<float>(in_info.dimension(axis)), wrapper::traits::vector_128_tag{})); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 984 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 988 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 989 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 994 | else if(op == ReductionOperation::PROD) |
| 995 | { |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 996 | const auto offset32x4f_4 = wrapper::vdup_n(static_cast<float>(iq_info.offset), wrapper::traits::vector_128_tag{}); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 997 | const auto iscale32x4f_4 = vinvq_f32(vdupq_n_f32(iq_info.scale)); |
Manuel Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 998 | |
| 999 | //re-quantize |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 1000 | 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 Bottini | 1d4f385 | 2019-01-14 15:14:43 +0000 | [diff] [blame] | 1004 | |
| 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1011 | 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 Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 1018 | else if(op == ReductionOperation::ARG_IDX_MIN) |
| 1019 | { |
| 1020 | wrapper::vstore(output.ptr(), vec_res_value); |
| 1021 | } |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1022 | else |
| 1023 | { |
Michalis Spyrou | 19bd412 | 2020-01-22 10:27:06 +0000 | [diff] [blame] | 1024 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1051 | } |
| 1052 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1053 | }, |
| 1054 | input, output); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1055 | } |
| 1056 | }; |
| 1057 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1058 | 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] | 1059 | { |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1060 | 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 Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1085 | switch(axis) |
| 1086 | { |
| 1087 | case 0: |
| 1088 | switch(input->info()->data_type()) |
| 1089 | { |
| 1090 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1091 | return Reducer<RedOpX_qasymm8>::reduceX(window, input, output, RedOpX_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1092 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1093 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1094 | 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] | 1095 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1096 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1097 | return Reducer<RedOpX<float, 4>>::reduceX(window, input, output, RedOpX<float, 4>(), op); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 1098 | case DataType::S32: |
| 1099 | return Reducer<RedOpX<int32_t, 4>>::reduceX(window, input, output, RedOpX<int32_t, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1100 | default: |
| 1101 | ARM_COMPUTE_ERROR("Not supported"); |
| 1102 | } |
| 1103 | case 1: |
| 1104 | switch(input->info()->data_type()) |
| 1105 | { |
| 1106 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1107 | return Reducer<RedOpYZW_qasymm8>::reduceY(window, input, output, RedOpYZW_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1108 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1109 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1110 | 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] | 1111 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1112 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1113 | return Reducer<RedOpYZW<float, 4>>::reduceY(window, input, output, RedOpYZW<float, 4>(), op); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 1114 | case DataType::S32: |
| 1115 | return Reducer<RedOpYZW<int32_t, 4>>::reduceY(window, input, output, RedOpYZW<int32_t, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1116 | default: |
| 1117 | ARM_COMPUTE_ERROR("Not supported"); |
| 1118 | } |
| 1119 | case 2: |
| 1120 | switch(input->info()->data_type()) |
| 1121 | { |
| 1122 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1123 | return Reducer<RedOpYZW_qasymm8>::reduceZ(window, input, output, RedOpYZW_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1124 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1125 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1126 | 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] | 1127 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1128 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1129 | return Reducer<RedOpYZW<float, 4>>::reduceZ(window, input, output, RedOpYZW<float, 4>(), op); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 1130 | case DataType::S32: |
| 1131 | return Reducer<RedOpYZW<int32_t, 4>>::reduceZ(window, input, output, RedOpYZW<int32_t, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1132 | default: |
| 1133 | ARM_COMPUTE_ERROR("Not supported"); |
| 1134 | } |
| 1135 | case 3: |
| 1136 | switch(input->info()->data_type()) |
| 1137 | { |
| 1138 | case DataType::QASYMM8: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1139 | return Reducer<RedOpYZW_qasymm8>::reduceW(window, input, output, RedOpYZW_qasymm8(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1140 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1141 | case DataType::F16: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1142 | 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] | 1143 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 1144 | case DataType::F32: |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1145 | return Reducer<RedOpYZW<float, 4>>::reduceW(window, input, output, RedOpYZW<float, 4>(), op); |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 1146 | case DataType::S32: |
| 1147 | return Reducer<RedOpYZW<int32_t, 4>>::reduceW(window, input, output, RedOpYZW<int32_t, 4>(), op); |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1148 | default: |
| 1149 | ARM_COMPUTE_ERROR("Not supported"); |
| 1150 | } |
| 1151 | default: |
| 1152 | ARM_COMPUTE_ERROR("Unsupported reduction axis"); |
| 1153 | } |
| 1154 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1155 | |
| 1156 | Status 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 Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 1161 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1162 | |
| 1163 | if(input->num_channels() == 1) |
| 1164 | { |
Michalis Spyrou | b9626ab | 2019-05-13 17:41:01 +0100 | [diff] [blame] | 1165 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::S32, DataType::F16, DataType::F32); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1166 | } |
| 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 Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1173 | |
| 1174 | 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] | 1175 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(axis > 3, "Unsupported reduction axis"); |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1176 | |
| 1177 | if(output->total_size() != 0) |
| 1178 | { |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1179 | 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 Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 1183 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1184 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_channels() != output->num_channels()); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1185 | } |
| 1186 | else |
| 1187 | { |
Michele Di Giorgio | 9637b2e | 2019-09-23 16:49:49 +0100 | [diff] [blame] | 1188 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U32, DataType::S32); |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1189 | } |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1190 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1191 | 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] | 1192 | 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1199 | 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] | 1200 | { |
| 1201 | // Calculate output shape and set if empty |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1202 | 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] | 1203 | |
| 1204 | // Output auto initialization if not yet initialized |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1205 | const bool is_arg_min_max = (op == ReductionOperation::ARG_IDX_MIN || op == ReductionOperation::ARG_IDX_MAX); |
Sang-Hoon Park | eaa01ab | 2019-11-11 17:33:28 +0000 | [diff] [blame] | 1206 | DataType output_data_type = is_arg_min_max ? DataType::S32 : input->data_type(); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1207 | 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] | 1208 | |
| 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 Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1223 | } // namespace |
| 1224 | |
| 1225 | NEReductionOperationKernel::NEReductionOperationKernel() |
| 1226 | : _input(nullptr), _output(nullptr), _reduction_axis(0), _op(ReductionOperation::SUM_SQUARE), _border_size() |
| 1227 | { |
| 1228 | } |
| 1229 | |
| 1230 | BorderSize NEReductionOperationKernel::border_size() const |
| 1231 | { |
| 1232 | return _border_size; |
| 1233 | } |
| 1234 | |
| 1235 | void NEReductionOperationKernel::configure(const ITensor *input, ITensor *output, unsigned int axis, ReductionOperation op) |
| 1236 | { |
| 1237 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1238 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1239 | 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] | 1240 | |
| 1241 | unsigned int num_elems_processed_per_iteration = 16 / data_size_from_type(input->info()->data_type()); |
| 1242 | |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1243 | _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 Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1248 | |
| 1249 | // Configure kernel window |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1250 | 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] | 1251 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1252 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1253 | |
John Richardson | 73d4aef | 2018-05-08 14:34:33 +0100 | [diff] [blame] | 1254 | INEKernel::configure(std::get<1>(win_config)); |
| 1255 | } |
| 1256 | |
| 1257 | Status 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 Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1260 | 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] | 1261 | |
| 1262 | return Status{}; |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1263 | } |
| 1264 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1265 | void NEReductionOperationKernel::run(const Window &window, const ThreadInfo &info) |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1266 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1267 | ARM_COMPUTE_UNUSED(info); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1268 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 1269 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 1270 | |
Michalis Spyrou | aea14c6 | 2019-01-03 11:10:25 +0000 | [diff] [blame] | 1271 | reduce_op(window, _input, _output, _reduction_axis, _op); |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 1272 | } |
Michalis Spyrou | bcf8a96 | 2018-10-12 10:51:31 +0100 | [diff] [blame] | 1273 | } // namespace arm_compute |