giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1 | /* |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [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/NEElementwiseOperationKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CPP/Validate.h" |
| 27 | #include "arm_compute/core/Error.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/NEAsymm.h" |
| 32 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 33 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
| 34 | #include "arm_compute/core/TensorInfo.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | |
| 37 | #include <algorithm> |
| 38 | #include <arm_neon.h> |
| 39 | #include <cstdint> |
| 40 | #include <map> |
| 41 | #include <string> |
| 42 | |
| 43 | namespace arm_compute |
| 44 | { |
| 45 | class Coordinates; |
| 46 | |
| 47 | namespace |
| 48 | { |
| 49 | float32x4x4_t load_quantized(const uint8_t *input1_ptr, const int32x4_t &offset, const float32x4_t &scale) |
| 50 | { |
| 51 | qasymm8x16_t x = vld1q_u8(input1_ptr); |
| 52 | const float32x4x4_t out = |
| 53 | { |
| 54 | { |
| 55 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(x))))), offset)), scale), |
| 56 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(x))))), offset)), scale), |
| 57 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(x))))), offset)), scale), |
| 58 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(x))))), offset)), scale), |
| 59 | } |
| 60 | }; |
| 61 | return out; |
| 62 | } |
| 63 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 64 | void store_quantized(uint8_t *output_ptr, const uint32x4x4_t &out) |
| 65 | { |
| 66 | const uint8x8_t pa = vqmovn_u16(vcombine_u16(vqmovn_u32(out.val[0]), vqmovn_u32(out.val[1]))); |
| 67 | const uint8x8_t pb = vqmovn_u16(vcombine_u16(vqmovn_u32(out.val[2]), vqmovn_u32(out.val[3]))); |
| 68 | vst1q_u8(output_ptr, vcombine_u8(pa, pb)); |
| 69 | } |
| 70 | |
| 71 | void store_quantized(uint8_t *output_ptr, const int32x4x4_t &out) |
| 72 | { |
| 73 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(out.val[0]), vqmovn_s32(out.val[1]))); |
| 74 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(out.val[2]), vqmovn_s32(out.val[3]))); |
| 75 | vst1q_u8(output_ptr, vcombine_u8(pa, pb)); |
| 76 | } |
| 77 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 78 | void store_quantized(uint8_t *output_ptr, const float32x4x4_t &rf, const float32x4_t &offset, const float32x4_t &invscale) |
| 79 | { |
| 80 | int32x4x4_t out = |
| 81 | { |
| 82 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[0], invscale)), |
| 83 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[1], invscale)), |
| 84 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[2], invscale)), |
| 85 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[3], invscale)), |
| 86 | }; |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 87 | store_quantized(output_ptr, out); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | float32x4x4_t dup_quantized(qasymm8_t broadcast_value, int offset, float scale) |
| 91 | { |
| 92 | const qasymm8x16_t broadcast_value_vec = vdupq_n_u8(broadcast_value); |
| 93 | const int32x4_t voffset = vdupq_n_s32(offset); |
| 94 | const float32x4_t vscale = vdupq_n_f32(scale); |
| 95 | |
| 96 | const float32x4x4_t broadcast_vector = |
| 97 | { |
| 98 | { |
| 99 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(broadcast_value_vec))))), voffset)), vscale), |
| 100 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(broadcast_value_vec))))), voffset)), vscale), |
| 101 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(broadcast_value_vec))))), voffset)), vscale), |
| 102 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(broadcast_value_vec))))), voffset)), vscale), |
| 103 | } |
| 104 | }; |
| 105 | return broadcast_vector; |
| 106 | } |
| 107 | |
| 108 | template <ArithmeticOperation op, typename ScalarType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 109 | inline ScalarType elementwise_arithm_op_scalar(const ScalarType &a, const ScalarType &b) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 110 | { |
| 111 | auto res = ScalarType(0); |
| 112 | |
| 113 | switch(op) |
| 114 | { |
| 115 | case ArithmeticOperation::MAX: |
| 116 | res = std::max(a, b); |
| 117 | break; |
| 118 | case ArithmeticOperation::MIN: |
| 119 | res = std::min(a, b); |
| 120 | break; |
| 121 | case ArithmeticOperation::SQUARED_DIFF: |
| 122 | { |
| 123 | res = (a - b) * (a - b); |
| 124 | break; |
| 125 | } |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 126 | case ArithmeticOperation::DIV: |
| 127 | { |
| 128 | res = a / b; |
| 129 | break; |
| 130 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 131 | default: |
| 132 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 133 | } |
| 134 | return res; |
| 135 | } |
| 136 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 137 | template <ArithmeticOperation op> |
| 138 | inline uint8_t elementwise_arithm_op_quantized_scalar(const float &a, const float &b, QuantizationInfo qinfo) |
| 139 | { |
| 140 | return qinfo.quantize(elementwise_arithm_op_scalar<op>(a, b), RoundingPolicy::TO_NEAREST_UP); |
| 141 | } |
| 142 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 143 | template <ArithmeticOperation op, typename VectorType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 144 | inline VectorType elementwise_arithm_op(const VectorType &a, const VectorType &b) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 145 | { |
| 146 | VectorType res = { 0, 0, 0, 0 }; |
| 147 | |
| 148 | switch(op) |
| 149 | { |
| 150 | case ArithmeticOperation::MAX: |
| 151 | res = wrapper::vmax(a, b); |
| 152 | break; |
| 153 | case ArithmeticOperation::MIN: |
| 154 | res = wrapper::vmin(a, b); |
| 155 | break; |
| 156 | case ArithmeticOperation::SQUARED_DIFF: |
| 157 | { |
| 158 | const VectorType tmp = wrapper::vsub(a, b); |
| 159 | res = wrapper::vmul(tmp, tmp); |
| 160 | break; |
| 161 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 162 | default: |
| 163 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 164 | } |
| 165 | |
| 166 | return res; |
| 167 | } |
| 168 | |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 169 | template <> |
| 170 | inline float32x4_t elementwise_arithm_op<ArithmeticOperation::DIV, float32x4_t>(const float32x4_t &a, const float32x4_t &b) |
| 171 | { |
| 172 | return wrapper::vdiv(a, b); |
| 173 | } |
| 174 | |
| 175 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 176 | template <> |
| 177 | inline float16x8_t elementwise_arithm_op<ArithmeticOperation::DIV, float16x8_t>(const float16x8_t &a, const float16x8_t &b) |
| 178 | { |
| 179 | return wrapper::vdiv(a, b); |
| 180 | } |
| 181 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 182 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 183 | template <ArithmeticOperation op> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 184 | inline float32x4x4_t elementwise_arithm_op(const float32x4x4_t &a, const float32x4x4_t &b) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 185 | { |
| 186 | float32x4x4_t out = |
| 187 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 188 | elementwise_arithm_op<op>(a.val[0], b.val[0]), |
| 189 | elementwise_arithm_op<op>(a.val[1], b.val[1]), |
| 190 | elementwise_arithm_op<op>(a.val[2], b.val[2]), |
| 191 | elementwise_arithm_op<op>(a.val[3], b.val[3]), |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 192 | }; |
| 193 | return out; |
| 194 | } |
| 195 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 196 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 197 | inline VectorType elementwise_arithm_op_broadcast(const VectorType &a, const ScalarType &broadcast_value, const bool reorder) |
| 198 | { |
| 199 | VectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag()); |
| 200 | return elementwise_arithm_op<op>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector); |
| 201 | } |
| 202 | |
| 203 | template <ComparisonOperation op, typename InputScalarType> |
| 204 | inline uint8_t elementwise_comp_op_scalar(const InputScalarType &a, const InputScalarType &b) |
| 205 | { |
| 206 | bool res = false; |
| 207 | |
| 208 | switch(op) |
| 209 | { |
| 210 | case ComparisonOperation::Equal: |
| 211 | res = (a == b); |
| 212 | break; |
| 213 | case ComparisonOperation::NotEqual: |
| 214 | res = (a != b); |
| 215 | break; |
| 216 | case ComparisonOperation::Greater: |
| 217 | res = (a > b); |
| 218 | break; |
| 219 | case ComparisonOperation::GreaterEqual: |
| 220 | res = (a >= b); |
| 221 | break; |
| 222 | case ComparisonOperation::Less: |
| 223 | res = (a < b); |
| 224 | break; |
| 225 | case ComparisonOperation::LessEqual: |
| 226 | res = (a <= b); |
| 227 | break; |
| 228 | default: |
| 229 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 230 | } |
| 231 | return res ? ~static_cast<uint8_t>(0) : static_cast<uint8_t>(0); |
| 232 | } |
| 233 | |
| 234 | template <ComparisonOperation op> |
| 235 | inline uint8_t elementwise_comp_op_quantized_scalar(const float &a, const float &b, QuantizationInfo qinfo) |
| 236 | { |
| 237 | ARM_COMPUTE_UNUSED(qinfo); |
| 238 | return elementwise_comp_op_scalar<op>(a, b); |
| 239 | } |
| 240 | |
| 241 | template <ComparisonOperation op, typename InputVectorType, typename OutputVectorType> |
| 242 | inline OutputVectorType elementwise_comp_op(const InputVectorType &a, const InputVectorType &b) |
| 243 | { |
| 244 | OutputVectorType res = { 0, 0, 0, 0 }; |
| 245 | |
| 246 | switch(op) |
| 247 | { |
| 248 | case ComparisonOperation::Equal: |
| 249 | res = wrapper::vceq(a, b); |
| 250 | break; |
| 251 | case ComparisonOperation::NotEqual: |
| 252 | res = wrapper::vnot(wrapper::vceq(a, b)); |
| 253 | break; |
| 254 | case ComparisonOperation::Greater: |
| 255 | res = wrapper::vcgt(a, b); |
| 256 | break; |
| 257 | case ComparisonOperation::GreaterEqual: |
| 258 | res = wrapper::vcge(a, b); |
| 259 | break; |
| 260 | case ComparisonOperation::Less: |
| 261 | res = wrapper::vcgt(b, a); |
| 262 | break; |
| 263 | case ComparisonOperation::LessEqual: |
| 264 | res = wrapper::vcge(b, a); |
| 265 | break; |
| 266 | default: |
| 267 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 268 | } |
| 269 | |
| 270 | return res; |
| 271 | } |
| 272 | |
| 273 | template <ComparisonOperation op> |
| 274 | inline uint32x4x4_t elementwise_comp_op(const float32x4x4_t &a, const float32x4x4_t &b) |
| 275 | { |
| 276 | uint32x4x4_t out = |
| 277 | { |
| 278 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[0], b.val[0]), |
| 279 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[1], b.val[1]), |
| 280 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[2], b.val[2]), |
| 281 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[3], b.val[3]) |
| 282 | }; |
| 283 | return out; |
| 284 | } |
| 285 | |
| 286 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType, typename OutputVectorType> |
| 287 | inline OutputVectorType elementwise_comp_op_broadcast(const InputVectorType &a, const InputScalarType &broadcast_value, const bool reorder) |
| 288 | { |
| 289 | InputVectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag()); |
| 290 | return elementwise_comp_op<op, InputVectorType, OutputVectorType>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector); |
| 291 | } |
| 292 | |
| 293 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 294 | inline int elementwise_arithm_op_loop(int window_start_x, int window_end_x, int window_step_x, |
| 295 | const ScalarType *input1_ptr, const ScalarType *input2_ptr, ScalarType *output_ptr) |
| 296 | { |
| 297 | int x = window_start_x; |
| 298 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 299 | { |
| 300 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 301 | const auto b = wrapper::vloadq(input2_ptr + x); |
| 302 | wrapper::vstore(output_ptr + x, elementwise_arithm_op<op>(a, b)); |
| 303 | } |
| 304 | return x; |
| 305 | } |
| 306 | |
| 307 | template <ArithmeticOperation op> |
| 308 | inline int elementwise_arithm_op_quantized_loop(int window_start_x, int window_end_x, int window_step_x, |
| 309 | const uint8_t *input1_ptr, const uint8_t *input2_ptr, uint8_t *output_ptr, |
| 310 | int32x4_t voffset1, int32x4_t voffset2, float32x4_t vscale1, float32x4_t vscale2, |
| 311 | float32x4_t voffseto, float32x4_t invvscaleo) |
| 312 | { |
| 313 | int x = window_start_x; |
| 314 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 315 | { |
| 316 | // Get inputs and compute output |
| 317 | const float32x4x4_t af = load_quantized(input1_ptr + x, voffset1, vscale1); |
| 318 | const float32x4x4_t bf = load_quantized(input2_ptr + x, voffset2, vscale2); |
| 319 | const float32x4x4_t rf = elementwise_arithm_op<op>(af, bf); |
| 320 | store_quantized(output_ptr + x, rf, voffseto, invvscaleo); |
| 321 | } |
| 322 | return x; |
| 323 | } |
| 324 | |
| 325 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 326 | inline int elementwise_arithm_op_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 327 | const ScalarType *non_broadcast_input_ptr, const ScalarType &broadcast_value, ScalarType *output_ptr, const bool reorder) |
| 328 | { |
| 329 | int x = window_start_x; |
| 330 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 331 | { |
| 332 | const auto a = wrapper::vloadq((non_broadcast_input_ptr + x)); |
| 333 | wrapper::vstore(output_ptr + x, elementwise_arithm_op_broadcast<op>(a, broadcast_value, reorder)); |
| 334 | } |
| 335 | return x; |
| 336 | } |
| 337 | |
| 338 | template <ArithmeticOperation op> |
| 339 | inline int elementwise_arithm_op_quantized_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 340 | const uint8_t *non_broadcast_input_ptr, float32x4x4_t broadcast_vector, uint8_t *output_ptr, |
| 341 | int32x4_t voffset_non_broadcast, float32x4_t vscale_non_broadcast, |
| 342 | float32x4_t voffseto, float32x4_t invvscaleo, bool reorder) |
| 343 | { |
| 344 | int x = window_start_x; |
| 345 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 346 | { |
| 347 | const float32x4x4_t af = load_quantized(non_broadcast_input_ptr + x, voffset_non_broadcast, vscale_non_broadcast); |
| 348 | const float32x4x4_t rf = elementwise_arithm_op<op>(reorder ? broadcast_vector : af, reorder ? af : broadcast_vector); |
| 349 | store_quantized(output_ptr + x, rf, voffseto, invvscaleo); |
| 350 | } |
| 351 | return x; |
| 352 | } |
| 353 | |
| 354 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 355 | inline int elementwise_comp_op_16_loop(int window_start_x, int window_end_x, int window_step_x, |
| 356 | const InputScalarType *input1_ptr, const InputScalarType *input2_ptr, uint8_t *output_ptr) |
| 357 | { |
| 358 | int x = window_start_x; |
| 359 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 360 | { |
| 361 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 362 | const auto b = wrapper::vloadq(input2_ptr + x); |
| 363 | const auto res = elementwise_comp_op<op, InputVectorType, uint16x8_t>(a, b); |
| 364 | wrapper::vstore(output_ptr + x, wrapper::vmovn(res)); |
| 365 | } |
| 366 | return x; |
| 367 | } |
| 368 | |
| 369 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 370 | inline int elementwise_comp_op_32_loop(int window_start_x, int window_end_x, int window_step_x, |
| 371 | const InputScalarType *input1_ptr, const InputScalarType *input2_ptr, uint8_t *output_ptr) |
| 372 | { |
| 373 | int x = window_start_x; |
| 374 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 375 | { |
| 376 | auto a = wrapper::vloadq(input1_ptr + x); |
| 377 | auto b = wrapper::vloadq(input2_ptr + x); |
| 378 | const auto res = elementwise_comp_op<op, InputVectorType, uint32x4_t>(a, b); |
| 379 | a = wrapper::vloadq(input1_ptr + x + 4); |
| 380 | b = wrapper::vloadq(input2_ptr + x + 4); |
| 381 | const auto res2 = elementwise_comp_op<op, InputVectorType, uint32x4_t>(a, b); |
| 382 | wrapper::vstore(output_ptr + x, wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(res), wrapper::vmovn(res2)))); |
| 383 | } |
| 384 | if(x <= window_end_x - 4) |
| 385 | { |
| 386 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 387 | const auto b = wrapper::vloadq(input2_ptr + x); |
| 388 | const auto res = elementwise_comp_op<op, InputVectorType, uint32x4_t>(a, b); |
| 389 | for(int i = 0; i < 4; i++) |
| 390 | { |
| 391 | *(output_ptr + x + i) = wrapper::vgetlane(res, i); |
| 392 | } |
| 393 | x = +4; |
| 394 | } |
| 395 | return x; |
| 396 | } |
| 397 | |
| 398 | template <ComparisonOperation op> |
| 399 | inline int elementwise_comp_op_quantized_loop(int window_start_x, int window_end_x, int window_step_x, |
| 400 | const uint8_t *input1_ptr, const uint8_t *input2_ptr, uint8_t *output_ptr, |
| 401 | int32x4_t voffset1, int32x4_t voffset2, float32x4_t vscale1, float32x4_t vscale2, |
| 402 | float32x4_t voffseto, float32x4_t invvscaleo) |
| 403 | { |
| 404 | ARM_COMPUTE_UNUSED(voffseto, invvscaleo); |
| 405 | int x = window_start_x; |
| 406 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 407 | { |
| 408 | const float32x4x4_t af = load_quantized(input1_ptr + x, voffset1, vscale1); |
| 409 | const float32x4x4_t bf = load_quantized(input2_ptr + x, voffset2, vscale2); |
| 410 | const uint32x4x4_t rf = elementwise_comp_op<op>(af, bf); |
| 411 | store_quantized(output_ptr + x, rf); |
| 412 | } |
| 413 | return x; |
| 414 | } |
| 415 | |
| 416 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 417 | inline int elementwise_comp_op_broadcast_16_loop(int window_start_x, int window_end_x, int window_step_x, |
| 418 | const InputScalarType *non_broadcast_input_ptr, const InputScalarType &broadcast_value, uint8_t *output_ptr, const bool reorder) |
| 419 | { |
| 420 | int x = window_start_x; |
| 421 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 422 | { |
| 423 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint16x8_t>(wrapper::vloadq((non_broadcast_input_ptr + x)), broadcast_value, reorder); |
| 424 | wrapper::vstore(output_ptr + x, wrapper::vmovn(a)); |
| 425 | } |
| 426 | return x; |
| 427 | } |
| 428 | |
| 429 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 430 | inline int elementwise_comp_op_broadcast_32_loop(int window_start_x, int window_end_x, int window_step_x, |
| 431 | const InputScalarType *non_broadcast_input_ptr, const InputScalarType &broadcast_value, uint8_t *output_ptr, const bool reorder) |
| 432 | { |
| 433 | int x = window_start_x; |
| 434 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 435 | { |
| 436 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint32x4_t>(wrapper::vloadq(non_broadcast_input_ptr + x), broadcast_value, reorder); |
| 437 | const auto b = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint32x4_t>(wrapper::vloadq(non_broadcast_input_ptr + x + 4), broadcast_value, reorder); |
| 438 | wrapper::vstore(output_ptr + x, wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(a), wrapper::vmovn(b)))); |
| 439 | } |
| 440 | if(x <= window_end_x - 4) |
| 441 | { |
| 442 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint32x4_t>(wrapper::vloadq((non_broadcast_input_ptr + x)), broadcast_value, reorder); |
| 443 | for(int i = 0; i < 4; i++) |
| 444 | { |
| 445 | *(output_ptr + x + i) = wrapper::vgetlane(a, i); |
| 446 | } |
| 447 | x = +4; |
| 448 | } |
| 449 | return x; |
| 450 | } |
| 451 | |
| 452 | template <ComparisonOperation op> |
| 453 | inline int elementwise_comp_op_quantized_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 454 | const uint8_t *non_broadcast_input_ptr, float32x4x4_t broadcast_vector, uint8_t *output_ptr, |
| 455 | int32x4_t voffset_non_broadcast, float32x4_t vscale_non_broadcast, |
| 456 | float32x4_t voffseto, float32x4_t invvscaleo, bool reorder) |
| 457 | { |
| 458 | ARM_COMPUTE_UNUSED(voffseto, invvscaleo); |
| 459 | int x = window_start_x; |
| 460 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 461 | { |
| 462 | const float32x4x4_t af = load_quantized(non_broadcast_input_ptr + x, voffset_non_broadcast, vscale_non_broadcast); |
| 463 | const uint32x4x4_t rf = elementwise_comp_op<op>(reorder ? broadcast_vector : af, reorder ? af : broadcast_vector); |
| 464 | store_quantized(output_ptr + x, rf); |
| 465 | } |
| 466 | return x; |
| 467 | } |
| 468 | |
| 469 | template <typename InputScalarType, typename OutputScalarType, typename InputVectorType> |
| 470 | void elementwise_op(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
| 471 | OutputScalarType (*scalar_func)(const InputScalarType &, const InputScalarType &), |
| 472 | int (*broadcast_func)(int, int, int, const InputScalarType *, const InputScalarType &, OutputScalarType *, const bool), |
| 473 | int (*neon_func)(int, int, int, const InputScalarType *, const InputScalarType *, OutputScalarType *)) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 474 | { |
| 475 | // Create input windows |
| 476 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 477 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 478 | |
| 479 | // Clear X Dimension on execution window as we handle manually |
| 480 | Window win = window; |
| 481 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 482 | |
Michalis Spyrou | e8c0c43 | 2019-01-22 11:08:31 +0000 | [diff] [blame] | 483 | const int window_step_x = std::min(16 / static_cast<int>(sizeof(OutputScalarType)), 8); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 484 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 485 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 486 | const bool is_broadcast_across_x = (input1_win.x().step() == 0) || (input2_win.x().step() == 0); |
| 487 | |
| 488 | if(is_broadcast_across_x) |
| 489 | { |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 490 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 491 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 492 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 493 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 494 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 495 | |
| 496 | // Clear X Dimension on execution window as we handle manually |
| 497 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 498 | |
| 499 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 500 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 501 | Iterator output(out, win); |
| 502 | |
| 503 | execute_window_loop(win, [&](const Coordinates & id) |
| 504 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 505 | auto output_ptr = reinterpret_cast<OutputScalarType *>(output.ptr()); |
| 506 | const auto non_broadcast_input_ptr = reinterpret_cast<const InputScalarType *>(non_broadcast_input.ptr()); |
| 507 | const InputScalarType broadcast_value = *reinterpret_cast<const InputScalarType *>(broadcast_input.ptr()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 508 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 509 | int x = (*broadcast_func)(window_start_x, window_end_x, window_step_x, non_broadcast_input_ptr, broadcast_value, output_ptr, !is_broadcast_input_2); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 510 | for(; x < window_end_x; ++x) |
| 511 | { |
| 512 | const auto a = *(non_broadcast_input_ptr + x); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 513 | *(output_ptr + x) = (*scalar_func)(!is_broadcast_input_2 ? broadcast_value : a, !is_broadcast_input_2 ? a : broadcast_value); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 514 | } |
| 515 | }, |
| 516 | broadcast_input, non_broadcast_input, output); |
| 517 | } |
| 518 | else |
| 519 | { |
| 520 | // Clear X Dimension on execution window as we handle manually |
| 521 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 522 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 523 | |
| 524 | Iterator input1(in1, input1_win); |
| 525 | Iterator input2(in2, input2_win); |
| 526 | Iterator output(out, win); |
| 527 | |
| 528 | execute_window_loop(win, [&](const Coordinates & id) |
| 529 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 530 | auto output_ptr = reinterpret_cast<OutputScalarType *>(output.ptr()); |
| 531 | const auto input1_ptr = reinterpret_cast<const InputScalarType *>(input1.ptr()); |
| 532 | const auto input2_ptr = reinterpret_cast<const InputScalarType *>(input2.ptr()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 533 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 534 | int x = (*neon_func)(window_start_x, window_end_x, window_step_x, input1_ptr, input2_ptr, output_ptr); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 535 | for(; x < window_end_x; ++x) |
| 536 | { |
| 537 | const auto a = *(input1_ptr + x); |
| 538 | const auto b = *(input2_ptr + x); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 539 | *(output_ptr + x) = (*scalar_func)(a, b); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 540 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 541 | }, |
| 542 | input1, input2, output); |
| 543 | } |
| 544 | } |
| 545 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 546 | void elementwise_op_quantized(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
| 547 | uint8_t (*scalar_func)(const float &, const float &, QuantizationInfo), |
| 548 | int (*broadcast_func)(int, int, int, const uint8_t *, float32x4x4_t, uint8_t *, int32x4_t, float32x4_t, |
| 549 | float32x4_t, float32x4_t, const bool), |
| 550 | int (*neon_func)(int, int, int, const uint8_t *, const uint8_t *, uint8_t *, |
| 551 | int32x4_t, int32x4_t, float32x4_t, float32x4_t, |
| 552 | float32x4_t, float32x4_t)) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 553 | { |
| 554 | // Create input windows |
| 555 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 556 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 557 | |
| 558 | // Clear X Dimension on execution window as we handle manually |
| 559 | Window win = window; |
| 560 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 561 | |
| 562 | const int window_step_x = 16; |
| 563 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 564 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 565 | const bool is_broadcast_across_x = (input1_win.x().step() == 0) || (input2_win.x().step() == 0); |
| 566 | |
| 567 | const float output_scale = out->info()->quantization_info().scale; |
| 568 | const int output_offset = out->info()->quantization_info().offset; |
| 569 | |
| 570 | // Output quantization info (add 0.5 to round toward the nearest integer - 0.5 rounds away from zero) |
| 571 | const float32x4_t voffseto = vdupq_n_f32(output_offset + 0.5f); |
| 572 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / output_scale); |
| 573 | |
| 574 | if(is_broadcast_across_x) |
| 575 | { |
| 576 | // Select the broadcast input on the X axis |
| 577 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 578 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 579 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 580 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 581 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 582 | |
| 583 | const QuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info(); |
| 584 | const QuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info(); |
| 585 | |
| 586 | const int32x4_t voffset_non_broadcast = vdupq_n_s32(non_broadcast_qinfo.offset); |
| 587 | const float32x4_t vscale_non_broadcast = vdupq_n_f32(non_broadcast_qinfo.scale); |
| 588 | |
| 589 | // Clear X Dimension on execution window as we handle manually |
| 590 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 591 | |
| 592 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 593 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 594 | Iterator output(out, win); |
| 595 | |
| 596 | execute_window_loop(win, [&](const Coordinates & id) |
| 597 | { |
| 598 | const auto non_broadcast_input_ptr = reinterpret_cast<const uint8_t *>(non_broadcast_input.ptr()); |
| 599 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 600 | |
| 601 | const uint8_t broadcast_value = *reinterpret_cast<const uint8_t *>(broadcast_input.ptr()); |
| 602 | const float32x4x4_t broadcast_vector = dup_quantized(broadcast_value, broadcast_qinfo.offset, broadcast_qinfo.scale); |
| 603 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 604 | int x = (*broadcast_func)(window_start_x, window_end_x, window_step_x, non_broadcast_input_ptr, broadcast_vector, output_ptr, |
| 605 | voffset_non_broadcast, vscale_non_broadcast, voffseto, invvscaleo, !is_broadcast_input_2); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 606 | for(; x < window_end_x; ++x) |
| 607 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 608 | const float afs = scvt_f32_qasymm8(*(non_broadcast_input_ptr + x), non_broadcast_qinfo.scale, non_broadcast_qinfo.offset); |
| 609 | const float bfs = scvt_f32_qasymm8(broadcast_value, broadcast_qinfo.scale, broadcast_qinfo.offset); |
| 610 | *(output_ptr + x) = (*scalar_func)(!is_broadcast_input_2 ? bfs : afs, !is_broadcast_input_2 ? afs : bfs, |
| 611 | out->info()->quantization_info()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 612 | } |
| 613 | }, |
| 614 | broadcast_input, non_broadcast_input, output); |
| 615 | } |
| 616 | else |
| 617 | { |
| 618 | // Input1 quantization info |
| 619 | const int32x4_t voffset1 = vdupq_n_s32(in1->info()->quantization_info().offset); |
| 620 | const float32x4_t vscale1 = vdupq_n_f32(in1->info()->quantization_info().scale); |
| 621 | |
| 622 | // Input2 quantization info |
| 623 | const int32x4_t voffset2 = vdupq_n_s32(in2->info()->quantization_info().offset); |
| 624 | const float32x4_t vscale2 = vdupq_n_f32(in2->info()->quantization_info().scale); |
| 625 | |
| 626 | // Clear X Dimension on execution window as we handle manually |
| 627 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 628 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 629 | |
| 630 | const QuantizationInfo input1_qinfo = in1->info()->quantization_info(); |
| 631 | const QuantizationInfo input2_qinfo = in2->info()->quantization_info(); |
| 632 | |
| 633 | Iterator input1(in1, input1_win); |
| 634 | Iterator input2(in2, input2_win); |
| 635 | Iterator output(out, win); |
| 636 | |
| 637 | execute_window_loop(win, [&](const Coordinates & id) |
| 638 | { |
| 639 | const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr()); |
| 640 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 641 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 642 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 643 | int x = (*neon_func)(window_start_x, window_end_x, window_step_x, input1_ptr, input2_ptr, output_ptr, voffset1, voffset2, |
| 644 | vscale1, vscale2, voffseto, invvscaleo); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 645 | for(; x < window_end_x; ++x) |
| 646 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 647 | const float afs = scvt_f32_qasymm8(*(input1_ptr + x), input1_qinfo.scale, input1_qinfo.offset); |
| 648 | const float bfs = scvt_f32_qasymm8(*(input2_ptr + x), input2_qinfo.scale, input2_qinfo.offset); |
| 649 | *(output_ptr + x) = (*scalar_func)(afs, bfs, out->info()->quantization_info()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 650 | } |
| 651 | }, |
| 652 | input1, input2, output); |
| 653 | } |
| 654 | } |
| 655 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 656 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 657 | void elementwise_comp_op_16(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 658 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 659 | elementwise_op<InputScalarType, uint8_t, InputVectorType>(in1, in2, out, window, |
| 660 | &elementwise_comp_op_scalar<op, InputScalarType>, |
| 661 | &elementwise_comp_op_broadcast_16_loop<op, InputScalarType, InputVectorType>, |
| 662 | &elementwise_comp_op_16_loop<op, InputScalarType, InputVectorType>); |
| 663 | } |
| 664 | |
| 665 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 666 | void elementwise_comp_op_32(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 667 | { |
| 668 | elementwise_op<InputScalarType, uint8_t, InputVectorType>(in1, in2, out, window, |
| 669 | &elementwise_comp_op_scalar<op, InputScalarType>, |
| 670 | &elementwise_comp_op_broadcast_32_loop<op, InputScalarType, InputVectorType>, |
| 671 | &elementwise_comp_op_32_loop<op, InputScalarType, InputVectorType>); |
| 672 | } |
| 673 | |
| 674 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 675 | void elementwise_arithm_op(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 676 | { |
| 677 | elementwise_op<ScalarType, ScalarType, VectorType>(in1, in2, out, window, |
| 678 | &elementwise_arithm_op_scalar<op, ScalarType>, |
| 679 | &elementwise_arithm_op_broadcast_loop<op, ScalarType, VectorType>, |
| 680 | &elementwise_arithm_op_loop<op, ScalarType, VectorType>); |
| 681 | } |
| 682 | |
| 683 | template <ArithmeticOperation op> |
| 684 | void elementwise_arithm_op_quantized(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 685 | { |
| 686 | elementwise_op_quantized(in1, in2, out, window, &elementwise_arithm_op_quantized_scalar<op>, |
| 687 | &elementwise_arithm_op_quantized_broadcast_loop<op>, |
| 688 | &elementwise_arithm_op_quantized_loop<op>); |
| 689 | } |
| 690 | |
| 691 | template <ComparisonOperation op> |
| 692 | void elementwise_comp_op_quantized(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 693 | { |
| 694 | elementwise_op_quantized(in1, in2, out, window, &elementwise_comp_op_quantized_scalar<op>, |
| 695 | &elementwise_comp_op_quantized_broadcast_loop<op>, |
| 696 | &elementwise_comp_op_quantized_loop<op>); |
| 697 | } |
| 698 | |
| 699 | std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> |
| 700 | configure_func(const ITensor *input1, const ITensor *input2, ITensor *output, |
| 701 | std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function) |
| 702 | { |
| 703 | std::string function_to_call("op_"); |
| 704 | function_to_call += string_from_data_type(input1->info()->data_type()) + "_"; |
| 705 | function_to_call += string_from_data_type(input2->info()->data_type()) + "_"; |
| 706 | function_to_call += string_from_data_type(output->info()->data_type()); |
| 707 | |
| 708 | auto it = map_function.find(function_to_call); |
| 709 | |
| 710 | if(it != map_function.end()) |
| 711 | { |
| 712 | auto func = it->second; |
| 713 | return [func](const ITensor * input1, const ITensor * input2, ITensor * output, const Window & window) |
| 714 | { |
| 715 | func(input1, input2, output, window); |
| 716 | }; |
| 717 | } |
| 718 | return nullptr; |
| 719 | } |
| 720 | |
| 721 | template <ArithmeticOperation op> |
| 722 | std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> |
| 723 | configure_arithm_func(const ITensor *input1, const ITensor *input2, ITensor *output) |
| 724 | { |
| 725 | static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = |
| 726 | { |
| 727 | { "op_F32_F32_F32", &elementwise_arithm_op<op, float, float32x4_t> }, |
| 728 | { "op_S16_S16_S16", &elementwise_arithm_op<op, int16_t, int16x8_t> }, |
| 729 | { "op_S32_S32_S32", &elementwise_arithm_op<op, int32_t, int32x4_t> }, |
| 730 | { "op_QASYMM8_QASYMM8_QASYMM8", &elementwise_arithm_op_quantized<op> } |
| 731 | }; |
| 732 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 733 | map_function["op_F16_F16_F16"] = &elementwise_arithm_op<op, float16_t, float16x8_t>; |
| 734 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 735 | |
| 736 | return configure_func(input1, input2, output, map_function); |
| 737 | } |
| 738 | |
| 739 | template <ComparisonOperation op> |
| 740 | std::function<void(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window)> |
| 741 | configure_comp_func(const ITensor *input1, const ITensor *input2, ITensor *output) |
| 742 | { |
| 743 | static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = |
| 744 | { |
| 745 | { "op_F32_F32_U8", &elementwise_comp_op_32<op, float, float32x4_t> }, |
| 746 | { "op_S16_S16_U8", &elementwise_comp_op_16<op, int16_t, int16x8_t> }, |
| 747 | { "op_S32_S32_U8", &elementwise_comp_op_32<op, int32_t, int32x4_t> }, |
| 748 | { "op_QASYMM8_QASYMM8_U8", &elementwise_comp_op_quantized<op> } |
| 749 | }; |
| 750 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 751 | map_function["op_F16_F16_U8"] = &elementwise_comp_op_16<op, float16_t, float16x8_t>; |
| 752 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 753 | |
| 754 | return configure_func(input1, input2, output, map_function); |
| 755 | } |
| 756 | } // namespace |
| 757 | |
| 758 | NEElementwiseOperationKernel::NEElementwiseOperationKernel() |
| 759 | : _function(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 760 | { |
| 761 | } |
| 762 | |
| 763 | Status NEElementwiseOperationKernel::validate_arguments_common(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 764 | { |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 765 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::S32, DataType::F32); |
| 766 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::QASYMM8, DataType::S16, DataType::F16, DataType::S32, DataType::F32); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 767 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input1); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 768 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2); |
| 769 | |
| 770 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
| 771 | |
| 772 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 773 | |
| 774 | // Validate in case of configured output |
| 775 | if(output.total_size() > 0) |
| 776 | { |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 777 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
| 778 | "Wrong shape for output"); |
| 779 | } |
| 780 | |
| 781 | return Status{}; |
| 782 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 783 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 784 | void NEElementwiseOperationKernel::configure_common(const ITensor *input1, const ITensor *input2, ITensor *output) |
| 785 | { |
| 786 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 787 | |
| 788 | // Configure kernel window |
| 789 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1->info(), *input2->info()); |
| 790 | const TensorShape &out_shape = broadcast_pair.first; |
| 791 | const ValidRegion &valid_region = broadcast_pair.second; |
| 792 | |
| 793 | // Auto initialize output if not initialized |
| 794 | auto_init_if_empty(*output->info(), out_shape, 1, input1->info()->data_type()); |
| 795 | |
| 796 | Window win = calculate_max_window(valid_region); |
| 797 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 798 | _input1 = input1; |
| 799 | _input2 = input2; |
| 800 | _output = output; |
| 801 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 802 | INEKernel::configure(win); |
| 803 | } |
| 804 | |
| 805 | void NEElementwiseOperationKernel::run(const Window &window, const ThreadInfo &info) |
| 806 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 807 | ARM_COMPUTE_UNUSED(info, window); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 808 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 809 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 810 | ARM_COMPUTE_ERROR_ON(_function == nullptr); |
| 811 | _function(_input1, _input2, _output, window); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 812 | } |
| 813 | |
| 814 | /** Arithmetic operators (min, max, squared_diff) */ |
| 815 | |
| 816 | void NEArithmeticOperationKernel::configure(ArithmeticOperation op, const ITensor *input1, const ITensor *input2, ITensor *output) |
| 817 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 818 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info())); |
| 819 | configure_common(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 820 | switch(op) |
| 821 | { |
| 822 | case ArithmeticOperation::MAX: |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 823 | _function = configure_arithm_func<ArithmeticOperation::MAX>(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 824 | break; |
| 825 | case ArithmeticOperation::MIN: |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 826 | _function = configure_arithm_func<ArithmeticOperation::MIN>(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 827 | break; |
| 828 | case ArithmeticOperation::SQUARED_DIFF: |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 829 | _function = configure_arithm_func<ArithmeticOperation::SQUARED_DIFF>(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 830 | break; |
| 831 | default: |
| 832 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 833 | } |
| 834 | } |
| 835 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 836 | Status NEArithmeticOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 837 | { |
| 838 | // Validate in case of configured output |
| 839 | if(output.total_size() > 0) |
| 840 | { |
| 841 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &output); |
| 842 | } |
| 843 | return validate_arguments_common(input1, input2, output); |
| 844 | } |
| 845 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 846 | Status NEArithmeticOperationKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 847 | { |
| 848 | ARM_COMPUTE_UNUSED(op); |
| 849 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 850 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 851 | return Status{}; |
| 852 | } |
| 853 | |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 854 | /** The division operator */ |
| 855 | |
| 856 | void NEDivisionOperationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output) |
| 857 | { |
| 858 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info())); |
| 859 | configure_common(input1, input2, output); |
| 860 | _function = configure_arithm_func<ArithmeticOperation::DIV>(input1, input2, output); |
| 861 | } |
| 862 | |
| 863 | Status NEDivisionOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 864 | { |
| 865 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::F16, DataType::F32); |
| 866 | return NEArithmeticOperationKernel::validate_arguments(input1, input2, output); |
| 867 | } |
| 868 | |
| 869 | Status NEDivisionOperationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 870 | { |
| 871 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 872 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
| 873 | return Status{}; |
| 874 | } |
| 875 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 876 | /** Comparison operators (equal, not equal, less than, greater than, less than or equal, greater than or equal) */ |
| 877 | |
| 878 | void NEComparisonOperationKernel::configure(ComparisonOperation op, const ITensor *input1, const ITensor *input2, ITensor *output) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 879 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 880 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1->info(), *input2->info(), *output->info())); |
| 881 | configure_common(input1, input2, output); |
| 882 | switch(op) |
| 883 | { |
| 884 | case ComparisonOperation::Equal: |
| 885 | _function = configure_comp_func<ComparisonOperation::Equal>(input1, input2, output); |
| 886 | break; |
| 887 | case ComparisonOperation::NotEqual: |
| 888 | _function = configure_comp_func<ComparisonOperation::NotEqual>(input1, input2, output); |
| 889 | break; |
| 890 | case ComparisonOperation::Greater: |
| 891 | _function = configure_comp_func<ComparisonOperation::Greater>(input1, input2, output); |
| 892 | break; |
| 893 | case ComparisonOperation::GreaterEqual: |
| 894 | _function = configure_comp_func<ComparisonOperation::GreaterEqual>(input1, input2, output); |
| 895 | break; |
| 896 | case ComparisonOperation::Less: |
| 897 | _function = configure_comp_func<ComparisonOperation::Less>(input1, input2, output); |
| 898 | break; |
| 899 | case ComparisonOperation::LessEqual: |
| 900 | _function = configure_comp_func<ComparisonOperation::LessEqual>(input1, input2, output); |
| 901 | break; |
| 902 | default: |
| 903 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 904 | } |
| 905 | } |
| 906 | |
| 907 | Status NEComparisonOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 908 | { |
| 909 | // Validate in case of configured output |
| 910 | if(output.total_size() > 0) |
| 911 | { |
| 912 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8); |
| 913 | } |
| 914 | return validate_arguments_common(input1, input2, output); |
| 915 | } |
| 916 | |
| 917 | Status NEComparisonOperationKernel::validate(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 918 | { |
| 919 | ARM_COMPUTE_UNUSED(op); |
| 920 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 921 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
| 922 | return Status{}; |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 923 | } |
| 924 | } // namespace arm_compute |