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