giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1 | /* |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEElementwiseOperationKernel.h" |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 25 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/IAccessWindow.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 28 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 29 | #include "src/core/NEON/NEAsymm.h" |
| 30 | #include "src/core/NEON/NEFixedPoint.h" |
| 31 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 32 | #include "src/core/SVE/kernels/elementwise/impl/elementwise_list.h" |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 33 | #include "src/core/SVE/kernels/elementwise/impl/elementwise_quantized_list.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 36 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 37 | #include <arm_neon.h> |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 38 | #include <map> |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 42 | namespace |
| 43 | { |
| 44 | float32x4x4_t load_quantized(const uint8_t *input1_ptr, const int32x4_t &offset, const float32x4_t &scale) |
| 45 | { |
| 46 | qasymm8x16_t x = vld1q_u8(input1_ptr); |
| 47 | const float32x4x4_t out = |
| 48 | { |
| 49 | { |
| 50 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(x))))), offset)), scale), |
| 51 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(x))))), offset)), scale), |
| 52 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(x))))), offset)), scale), |
| 53 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(x))))), offset)), scale), |
| 54 | } |
| 55 | }; |
| 56 | return out; |
| 57 | } |
| 58 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 59 | float32x4x4_t load_quantized_signed(const int8_t *input1_ptr, const int32x4_t &offset, const float32x4_t &scale) |
| 60 | { |
| 61 | qasymm8x16_signed_t x = vld1q_s8(input1_ptr); |
| 62 | const float32x4x4_t out = |
| 63 | { |
| 64 | { |
| 65 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(x)))), offset)), scale), |
| 66 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(x)))), offset)), scale), |
| 67 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(x)))), offset)), scale), |
| 68 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(x)))), offset)), scale), |
| 69 | } |
| 70 | }; |
| 71 | return out; |
| 72 | } |
| 73 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 74 | void store_quantized(uint8_t *output_ptr, const uint32x4x4_t &out) |
| 75 | { |
| 76 | const uint8x8_t pa = vqmovn_u16(vcombine_u16(vqmovn_u32(out.val[0]), vqmovn_u32(out.val[1]))); |
| 77 | const uint8x8_t pb = vqmovn_u16(vcombine_u16(vqmovn_u32(out.val[2]), vqmovn_u32(out.val[3]))); |
| 78 | vst1q_u8(output_ptr, vcombine_u8(pa, pb)); |
| 79 | } |
| 80 | |
| 81 | void store_quantized(uint8_t *output_ptr, const int32x4x4_t &out) |
| 82 | { |
| 83 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(out.val[0]), vqmovn_s32(out.val[1]))); |
| 84 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(out.val[2]), vqmovn_s32(out.val[3]))); |
| 85 | vst1q_u8(output_ptr, vcombine_u8(pa, pb)); |
| 86 | } |
| 87 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 88 | void store_quantized(uint8_t *output_ptr, const float32x4x4_t &rf, const float32x4_t &offset, const float32x4_t &invscale) |
| 89 | { |
| 90 | int32x4x4_t out = |
| 91 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 92 | { |
| 93 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[0], invscale)), |
| 94 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[1], invscale)), |
| 95 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[2], invscale)), |
| 96 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[3], invscale)), |
| 97 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 98 | }; |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 99 | store_quantized(output_ptr, out); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 102 | void store_quantized_signed(int8_t *output_ptr, const int32x4x4_t &out) |
| 103 | { |
| 104 | const int8x8_t pa = vqmovn_s16(vcombine_s16(vqmovn_s32(out.val[0]), vqmovn_s32(out.val[1]))); |
| 105 | const int8x8_t pb = vqmovn_s16(vcombine_s16(vqmovn_s32(out.val[2]), vqmovn_s32(out.val[3]))); |
| 106 | vst1q_s8(output_ptr, vcombine_s8(pa, pb)); |
| 107 | } |
| 108 | |
| 109 | void store_quantized_signed(int8_t *output_ptr, const float32x4x4_t &rf, const float32x4_t &offset, const float32x4_t &invscale) |
| 110 | { |
| 111 | int32x4x4_t out = |
| 112 | { |
| 113 | { |
| 114 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[0], invscale)), |
| 115 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[1], invscale)), |
| 116 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[2], invscale)), |
| 117 | vcvtq_s32_f32(vmlaq_f32(offset, rf.val[3], invscale)), |
| 118 | } |
| 119 | }; |
| 120 | store_quantized_signed(output_ptr, out); |
| 121 | } |
| 122 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 123 | template <ArithmeticOperation op, typename ScalarType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 124 | inline ScalarType elementwise_arithm_op_scalar(const ScalarType &a, const ScalarType &b) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 125 | { |
| 126 | auto res = ScalarType(0); |
| 127 | |
| 128 | switch(op) |
| 129 | { |
| 130 | case ArithmeticOperation::MAX: |
| 131 | res = std::max(a, b); |
| 132 | break; |
| 133 | case ArithmeticOperation::MIN: |
| 134 | res = std::min(a, b); |
| 135 | break; |
| 136 | case ArithmeticOperation::SQUARED_DIFF: |
| 137 | { |
| 138 | res = (a - b) * (a - b); |
| 139 | break; |
| 140 | } |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 141 | case ArithmeticOperation::PRELU: |
| 142 | { |
| 143 | res = (a > 0 ? a : a * b); |
| 144 | break; |
| 145 | } |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 146 | case ArithmeticOperation::DIV: |
| 147 | { |
| 148 | res = a / b; |
Georgios Pinitas | 1813422 | 2020-09-03 21:00:23 +0100 | [diff] [blame] | 149 | if(std::is_integral<ScalarType>::value) |
| 150 | { |
| 151 | res = (b == 0) ? 0 : res; |
| 152 | if(static_cast<int32_t>(a) % static_cast<int32_t>(b) != 0 && ((a < 0) != (b < 0))) |
| 153 | { |
| 154 | --res; |
| 155 | } |
| 156 | } |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 157 | break; |
| 158 | } |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 159 | case ArithmeticOperation::POWER: |
| 160 | { |
| 161 | res = std::pow(a, b); |
| 162 | break; |
| 163 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 164 | default: |
| 165 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 166 | } |
| 167 | return res; |
| 168 | } |
| 169 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 170 | template <ArithmeticOperation op> |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 171 | inline uint8_t elementwise_arithm_op_quantized_scalar(const float &a, const float &b, UniformQuantizationInfo qinfo) |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 172 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 173 | return quantize_qasymm8(elementwise_arithm_op_scalar<op>(a, b), qinfo); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 176 | template <ArithmeticOperation op> |
| 177 | inline int8_t elementwise_arithm_op_quantized_signed_scalar(const float &a, const float &b, UniformQuantizationInfo qinfo) |
| 178 | { |
| 179 | return quantize_qasymm8_signed(elementwise_arithm_op_scalar<op>(a, b), qinfo); |
| 180 | } |
| 181 | |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 182 | template <ArithmeticOperation op, typename VectorType> |
| 183 | inline typename VectorType::type elementwise_arithm_op(const typename VectorType::type &a, const typename VectorType::type &b) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 184 | { |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 185 | using vec_type = typename VectorType::type; |
| 186 | using scalar_type = typename VectorType::scalar_type; |
| 187 | using tag_type = typename VectorType::tag_type; |
| 188 | |
| 189 | vec_type res = wrapper::vdup_n(static_cast<scalar_type>(0), tag_type{}); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 190 | |
| 191 | switch(op) |
| 192 | { |
| 193 | case ArithmeticOperation::MAX: |
| 194 | res = wrapper::vmax(a, b); |
| 195 | break; |
| 196 | case ArithmeticOperation::MIN: |
| 197 | res = wrapper::vmin(a, b); |
| 198 | break; |
| 199 | case ArithmeticOperation::SQUARED_DIFF: |
| 200 | { |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 201 | const vec_type tmp = wrapper::vsub(a, b); |
| 202 | res = wrapper::vmul(tmp, tmp); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 203 | break; |
| 204 | } |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 205 | case ArithmeticOperation::PRELU: |
| 206 | { |
| 207 | const vec_type zero = wrapper::vdup_n(static_cast<scalar_type>(0), tag_type{}); |
| 208 | const vec_type tmp = wrapper::vmul(a, b); |
| 209 | const auto gt = wrapper::vcgt(a, zero); |
| 210 | |
| 211 | res = wrapper::vbsl(gt, a, tmp); |
| 212 | break; |
| 213 | } |
| 214 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 215 | default: |
| 216 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 217 | } |
| 218 | |
| 219 | return res; |
| 220 | } |
| 221 | |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 222 | template <> |
Georgios Pinitas | 1813422 | 2020-09-03 21:00:23 +0100 | [diff] [blame] | 223 | inline int32x4_t elementwise_arithm_op<ArithmeticOperation::DIV, typename wrapper::traits::neon_vector<int32_t, 4>>(const int32x4_t &a, const int32x4_t &b) |
| 224 | { |
| 225 | return vcvtq_s32_f32(vfloorq_f32(wrapper::vdiv(vcvtq_f32_s32(a), vcvtq_f32_s32(b)))); |
| 226 | } |
| 227 | |
| 228 | template <> |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 229 | inline float32x4_t elementwise_arithm_op<ArithmeticOperation::DIV, typename wrapper::traits::neon_vector<float, 4>>(const float32x4_t &a, const float32x4_t &b) |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 230 | { |
| 231 | return wrapper::vdiv(a, b); |
| 232 | } |
| 233 | |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 234 | template <> |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 235 | inline float32x4_t elementwise_arithm_op<ArithmeticOperation::POWER, typename wrapper::traits::neon_vector<float, 4>>(const float32x4_t &a, const float32x4_t &b) |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 236 | { |
| 237 | return wrapper::vpow(a, b); |
| 238 | } |
| 239 | |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 240 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 241 | template <> |
Michele Di Giorgio | b3a0a60 | 2019-06-13 15:35:00 +0100 | [diff] [blame] | 242 | inline float16x8_t elementwise_arithm_op<ArithmeticOperation::DIV, typename wrapper::traits::neon_vector<float16_t, 8>>(const float16x8_t &a, const float16x8_t &b) |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 243 | { |
| 244 | return wrapper::vdiv(a, b); |
| 245 | } |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 246 | |
| 247 | template <> |
Michele Di Giorgio | b3a0a60 | 2019-06-13 15:35:00 +0100 | [diff] [blame] | 248 | inline float16x8_t elementwise_arithm_op<ArithmeticOperation::POWER, typename wrapper::traits::neon_vector<float16_t, 8>>(const float16x8_t &a, const float16x8_t &b) |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 249 | { |
| 250 | return wrapper::vpow(a, b); |
| 251 | } |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 252 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 253 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 254 | template <ArithmeticOperation op> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 255 | 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] | 256 | { |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 257 | using neon_vector_float = wrapper::traits::neon_vector<float, 4>; |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 258 | float32x4x4_t out = |
| 259 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 260 | { |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 261 | elementwise_arithm_op<op, neon_vector_float>(a.val[0], b.val[0]), |
| 262 | elementwise_arithm_op<op, neon_vector_float>(a.val[1], b.val[1]), |
| 263 | elementwise_arithm_op<op, neon_vector_float>(a.val[2], b.val[2]), |
| 264 | elementwise_arithm_op<op, neon_vector_float>(a.val[3], b.val[3]), |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 265 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 266 | }; |
| 267 | return out; |
| 268 | } |
| 269 | |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 270 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 271 | inline typename VectorType::type elementwise_arithm_op_broadcast(const typename VectorType::type &a, const ScalarType &broadcast_value, const bool reorder) |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 272 | { |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 273 | using tag_type = typename VectorType::tag_type; |
| 274 | using vec_type = typename VectorType::type; |
| 275 | |
| 276 | vec_type broadcast_vector = wrapper::vdup_n(broadcast_value, tag_type{}); |
| 277 | return elementwise_arithm_op<op, VectorType>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | template <ComparisonOperation op, typename InputScalarType> |
| 281 | inline uint8_t elementwise_comp_op_scalar(const InputScalarType &a, const InputScalarType &b) |
| 282 | { |
| 283 | bool res = false; |
| 284 | |
| 285 | switch(op) |
| 286 | { |
| 287 | case ComparisonOperation::Equal: |
| 288 | res = (a == b); |
| 289 | break; |
| 290 | case ComparisonOperation::NotEqual: |
| 291 | res = (a != b); |
| 292 | break; |
| 293 | case ComparisonOperation::Greater: |
| 294 | res = (a > b); |
| 295 | break; |
| 296 | case ComparisonOperation::GreaterEqual: |
| 297 | res = (a >= b); |
| 298 | break; |
| 299 | case ComparisonOperation::Less: |
| 300 | res = (a < b); |
| 301 | break; |
| 302 | case ComparisonOperation::LessEqual: |
| 303 | res = (a <= b); |
| 304 | break; |
| 305 | default: |
| 306 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 307 | } |
| 308 | return res ? ~static_cast<uint8_t>(0) : static_cast<uint8_t>(0); |
| 309 | } |
| 310 | |
| 311 | template <ComparisonOperation op> |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 312 | inline uint8_t elementwise_comp_op_quantized_scalar(const float &a, const float &b, UniformQuantizationInfo qinfo) |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 313 | { |
| 314 | ARM_COMPUTE_UNUSED(qinfo); |
| 315 | return elementwise_comp_op_scalar<op>(a, b); |
| 316 | } |
| 317 | |
| 318 | template <ComparisonOperation op, typename InputVectorType, typename OutputVectorType> |
| 319 | inline OutputVectorType elementwise_comp_op(const InputVectorType &a, const InputVectorType &b) |
| 320 | { |
| 321 | OutputVectorType res = { 0, 0, 0, 0 }; |
| 322 | |
| 323 | switch(op) |
| 324 | { |
| 325 | case ComparisonOperation::Equal: |
| 326 | res = wrapper::vceq(a, b); |
| 327 | break; |
| 328 | case ComparisonOperation::NotEqual: |
| 329 | res = wrapper::vnot(wrapper::vceq(a, b)); |
| 330 | break; |
| 331 | case ComparisonOperation::Greater: |
| 332 | res = wrapper::vcgt(a, b); |
| 333 | break; |
| 334 | case ComparisonOperation::GreaterEqual: |
| 335 | res = wrapper::vcge(a, b); |
| 336 | break; |
| 337 | case ComparisonOperation::Less: |
| 338 | res = wrapper::vcgt(b, a); |
| 339 | break; |
| 340 | case ComparisonOperation::LessEqual: |
| 341 | res = wrapper::vcge(b, a); |
| 342 | break; |
| 343 | default: |
| 344 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 345 | } |
| 346 | |
| 347 | return res; |
| 348 | } |
| 349 | |
| 350 | template <ComparisonOperation op> |
| 351 | inline uint32x4x4_t elementwise_comp_op(const float32x4x4_t &a, const float32x4x4_t &b) |
| 352 | { |
| 353 | uint32x4x4_t out = |
| 354 | { |
Georgios Pinitas | d57891a | 2019-02-19 18:10:03 +0000 | [diff] [blame] | 355 | { |
| 356 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[0], b.val[0]), |
| 357 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[1], b.val[1]), |
| 358 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[2], b.val[2]), |
| 359 | elementwise_comp_op<op, float32x4_t, uint32x4_t>(a.val[3], b.val[3]) |
| 360 | } |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 361 | }; |
| 362 | return out; |
| 363 | } |
| 364 | |
| 365 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType, typename OutputVectorType> |
| 366 | inline OutputVectorType elementwise_comp_op_broadcast(const InputVectorType &a, const InputScalarType &broadcast_value, const bool reorder) |
| 367 | { |
| 368 | InputVectorType broadcast_vector = wrapper::vdup_n(broadcast_value, wrapper::traits::vector_128_tag()); |
| 369 | return elementwise_comp_op<op, InputVectorType, OutputVectorType>(reorder ? broadcast_vector : a, reorder ? a : broadcast_vector); |
| 370 | } |
| 371 | |
| 372 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 373 | inline int elementwise_arithm_op_loop(int window_start_x, int window_end_x, int window_step_x, |
| 374 | const ScalarType *input1_ptr, const ScalarType *input2_ptr, ScalarType *output_ptr) |
| 375 | { |
| 376 | int x = window_start_x; |
| 377 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 378 | { |
| 379 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 380 | const auto b = wrapper::vloadq(input2_ptr + x); |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 381 | wrapper::vstore(output_ptr + x, elementwise_arithm_op<op, VectorType>(a, b)); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 382 | } |
| 383 | return x; |
| 384 | } |
| 385 | |
| 386 | template <ArithmeticOperation op> |
| 387 | inline int elementwise_arithm_op_quantized_loop(int window_start_x, int window_end_x, int window_step_x, |
| 388 | const uint8_t *input1_ptr, const uint8_t *input2_ptr, uint8_t *output_ptr, |
| 389 | int32x4_t voffset1, int32x4_t voffset2, float32x4_t vscale1, float32x4_t vscale2, |
| 390 | float32x4_t voffseto, float32x4_t invvscaleo) |
| 391 | { |
| 392 | int x = window_start_x; |
| 393 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 394 | { |
| 395 | // Get inputs and compute output |
| 396 | const float32x4x4_t af = load_quantized(input1_ptr + x, voffset1, vscale1); |
| 397 | const float32x4x4_t bf = load_quantized(input2_ptr + x, voffset2, vscale2); |
| 398 | const float32x4x4_t rf = elementwise_arithm_op<op>(af, bf); |
| 399 | store_quantized(output_ptr + x, rf, voffseto, invvscaleo); |
| 400 | } |
| 401 | return x; |
| 402 | } |
| 403 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 404 | template <ArithmeticOperation op> |
| 405 | inline int elementwise_arithm_op_quantized_singed_loop(int window_start_x, int window_end_x, int window_step_x, |
| 406 | const int8_t *input1_ptr, const int8_t *input2_ptr, int8_t *output_ptr, |
| 407 | int32x4_t voffset1, int32x4_t voffset2, float32x4_t vscale1, float32x4_t vscale2, |
| 408 | float32x4_t voffseto, float32x4_t invvscaleo) |
| 409 | { |
| 410 | int x = window_start_x; |
| 411 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 412 | { |
| 413 | // Get inputs and compute output |
| 414 | const float32x4x4_t af = load_quantized_signed(input1_ptr + x, voffset1, vscale1); |
| 415 | const float32x4x4_t bf = load_quantized_signed(input2_ptr + x, voffset2, vscale2); |
| 416 | const float32x4x4_t rf = elementwise_arithm_op<op>(af, bf); |
| 417 | store_quantized_signed(output_ptr + x, rf, voffseto, invvscaleo); |
| 418 | } |
| 419 | return x; |
| 420 | } |
| 421 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 422 | template <ArithmeticOperation op, typename ScalarType, typename VectorType> |
| 423 | inline int elementwise_arithm_op_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 424 | const ScalarType *non_broadcast_input_ptr, const ScalarType &broadcast_value, ScalarType *output_ptr, const bool reorder) |
| 425 | { |
| 426 | int x = window_start_x; |
| 427 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 428 | { |
| 429 | const auto a = wrapper::vloadq((non_broadcast_input_ptr + x)); |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 430 | wrapper::vstore(output_ptr + x, elementwise_arithm_op_broadcast<op, ScalarType, VectorType>(a, broadcast_value, reorder)); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 431 | } |
| 432 | return x; |
| 433 | } |
| 434 | |
| 435 | template <ArithmeticOperation op> |
| 436 | inline int elementwise_arithm_op_quantized_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 437 | const uint8_t *non_broadcast_input_ptr, float32x4x4_t broadcast_vector, uint8_t *output_ptr, |
| 438 | int32x4_t voffset_non_broadcast, float32x4_t vscale_non_broadcast, |
| 439 | float32x4_t voffseto, float32x4_t invvscaleo, bool reorder) |
| 440 | { |
| 441 | int x = window_start_x; |
| 442 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 443 | { |
| 444 | const float32x4x4_t af = load_quantized(non_broadcast_input_ptr + x, voffset_non_broadcast, vscale_non_broadcast); |
| 445 | const float32x4x4_t rf = elementwise_arithm_op<op>(reorder ? broadcast_vector : af, reorder ? af : broadcast_vector); |
| 446 | store_quantized(output_ptr + x, rf, voffseto, invvscaleo); |
| 447 | } |
| 448 | return x; |
| 449 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 450 | template <ArithmeticOperation op> |
| 451 | inline int elementwise_arithm_op_quantized_signed_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 452 | const int8_t *non_broadcast_input_ptr, float32x4x4_t broadcast_vector, int8_t *output_ptr, |
| 453 | int32x4_t voffset_non_broadcast, float32x4_t vscale_non_broadcast, |
| 454 | float32x4_t voffseto, float32x4_t invvscaleo, 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 float32x4x4_t af = load_quantized_signed(non_broadcast_input_ptr + x, voffset_non_broadcast, vscale_non_broadcast); |
| 460 | const float32x4x4_t rf = elementwise_arithm_op<op>(reorder ? broadcast_vector : af, reorder ? af : broadcast_vector); |
| 461 | store_quantized_signed(output_ptr + x, rf, voffseto, invvscaleo); |
| 462 | } |
| 463 | return x; |
| 464 | } |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 465 | |
| 466 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
Michele Di Giorgio | 1c76c1d | 2020-08-28 13:25:31 +0100 | [diff] [blame] | 467 | inline int elementwise_comp_op_8_loop(int window_start_x, int window_end_x, int window_step_x, |
| 468 | const InputScalarType *input1_ptr, const InputScalarType *input2_ptr, uint8_t *output_ptr) |
| 469 | { |
| 470 | int x = window_start_x; |
| 471 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 472 | { |
| 473 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 474 | const auto b = wrapper::vloadq(input2_ptr + x); |
| 475 | const auto res = elementwise_comp_op<op, InputVectorType, uint8x16_t>(a, b); |
| 476 | wrapper::vstore(output_ptr + x, res); |
| 477 | } |
| 478 | return x; |
| 479 | } |
| 480 | |
| 481 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 482 | inline int elementwise_comp_op_16_loop(int window_start_x, int window_end_x, int window_step_x, |
| 483 | const InputScalarType *input1_ptr, const InputScalarType *input2_ptr, uint8_t *output_ptr) |
| 484 | { |
| 485 | int x = window_start_x; |
| 486 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 487 | { |
| 488 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 489 | const auto b = wrapper::vloadq(input2_ptr + x); |
| 490 | const auto res = elementwise_comp_op<op, InputVectorType, uint16x8_t>(a, b); |
| 491 | wrapper::vstore(output_ptr + x, wrapper::vmovn(res)); |
| 492 | } |
| 493 | return x; |
| 494 | } |
| 495 | |
| 496 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 497 | inline int elementwise_comp_op_32_loop(int window_start_x, int window_end_x, int window_step_x, |
| 498 | const InputScalarType *input1_ptr, const InputScalarType *input2_ptr, uint8_t *output_ptr) |
| 499 | { |
| 500 | int x = window_start_x; |
| 501 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 502 | { |
| 503 | auto a = wrapper::vloadq(input1_ptr + x); |
| 504 | auto b = wrapper::vloadq(input2_ptr + x); |
| 505 | const auto res = elementwise_comp_op<op, InputVectorType, uint32x4_t>(a, b); |
| 506 | a = wrapper::vloadq(input1_ptr + x + 4); |
| 507 | b = wrapper::vloadq(input2_ptr + x + 4); |
| 508 | const auto res2 = elementwise_comp_op<op, InputVectorType, uint32x4_t>(a, b); |
| 509 | wrapper::vstore(output_ptr + x, wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(res), wrapper::vmovn(res2)))); |
| 510 | } |
| 511 | if(x <= window_end_x - 4) |
| 512 | { |
| 513 | const auto a = wrapper::vloadq(input1_ptr + x); |
| 514 | const auto b = wrapper::vloadq(input2_ptr + x); |
| 515 | const auto res = elementwise_comp_op<op, InputVectorType, uint32x4_t>(a, b); |
| 516 | for(int i = 0; i < 4; i++) |
| 517 | { |
| 518 | *(output_ptr + x + i) = wrapper::vgetlane(res, i); |
| 519 | } |
| 520 | x = +4; |
| 521 | } |
| 522 | return x; |
| 523 | } |
| 524 | |
| 525 | template <ComparisonOperation op> |
| 526 | inline int elementwise_comp_op_quantized_loop(int window_start_x, int window_end_x, int window_step_x, |
| 527 | const uint8_t *input1_ptr, const uint8_t *input2_ptr, uint8_t *output_ptr, |
| 528 | int32x4_t voffset1, int32x4_t voffset2, float32x4_t vscale1, float32x4_t vscale2, |
| 529 | float32x4_t voffseto, float32x4_t invvscaleo) |
| 530 | { |
| 531 | ARM_COMPUTE_UNUSED(voffseto, invvscaleo); |
| 532 | int x = window_start_x; |
| 533 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 534 | { |
| 535 | const float32x4x4_t af = load_quantized(input1_ptr + x, voffset1, vscale1); |
| 536 | const float32x4x4_t bf = load_quantized(input2_ptr + x, voffset2, vscale2); |
| 537 | const uint32x4x4_t rf = elementwise_comp_op<op>(af, bf); |
| 538 | store_quantized(output_ptr + x, rf); |
| 539 | } |
| 540 | return x; |
| 541 | } |
| 542 | |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 543 | template <ComparisonOperation op> |
| 544 | inline int elementwise_comp_op_quantized_signed_loop(int window_start_x, int window_end_x, int window_step_x, |
| 545 | const int8_t *input1_ptr, const int8_t *input2_ptr, uint8_t *output_ptr, |
| 546 | int32x4_t voffset1, int32x4_t voffset2, float32x4_t vscale1, float32x4_t vscale2, |
| 547 | float32x4_t voffseto, float32x4_t invvscaleo) |
| 548 | { |
| 549 | ARM_COMPUTE_UNUSED(voffseto, invvscaleo); |
| 550 | int x = window_start_x; |
| 551 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 552 | { |
| 553 | const float32x4x4_t af = load_quantized_signed(input1_ptr + x, voffset1, vscale1); |
| 554 | const float32x4x4_t bf = load_quantized_signed(input2_ptr + x, voffset2, vscale2); |
| 555 | const uint32x4x4_t rf = elementwise_comp_op<op>(af, bf); |
| 556 | store_quantized(output_ptr + x, rf); |
| 557 | } |
| 558 | return x; |
| 559 | } |
| 560 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 561 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
Michele Di Giorgio | 1c76c1d | 2020-08-28 13:25:31 +0100 | [diff] [blame] | 562 | inline int elementwise_comp_op_broadcast_8_loop(int window_start_x, int window_end_x, int window_step_x, |
| 563 | const InputScalarType *non_broadcast_input_ptr, const InputScalarType &broadcast_value, uint8_t *output_ptr, const bool reorder) |
| 564 | { |
| 565 | int x = window_start_x; |
| 566 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 567 | { |
| 568 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint8x16_t>(wrapper::vloadq((non_broadcast_input_ptr + x)), broadcast_value, reorder); |
| 569 | wrapper::vstore(output_ptr + x, a); |
| 570 | } |
| 571 | return x; |
| 572 | } |
| 573 | |
| 574 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 575 | inline int elementwise_comp_op_broadcast_16_loop(int window_start_x, int window_end_x, int window_step_x, |
| 576 | const InputScalarType *non_broadcast_input_ptr, const InputScalarType &broadcast_value, uint8_t *output_ptr, const bool reorder) |
| 577 | { |
| 578 | int x = window_start_x; |
| 579 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 580 | { |
| 581 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint16x8_t>(wrapper::vloadq((non_broadcast_input_ptr + x)), broadcast_value, reorder); |
| 582 | wrapper::vstore(output_ptr + x, wrapper::vmovn(a)); |
| 583 | } |
| 584 | return x; |
| 585 | } |
| 586 | |
| 587 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 588 | inline int elementwise_comp_op_broadcast_32_loop(int window_start_x, int window_end_x, int window_step_x, |
| 589 | const InputScalarType *non_broadcast_input_ptr, const InputScalarType &broadcast_value, uint8_t *output_ptr, const bool reorder) |
| 590 | { |
| 591 | int x = window_start_x; |
| 592 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 593 | { |
| 594 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint32x4_t>(wrapper::vloadq(non_broadcast_input_ptr + x), broadcast_value, reorder); |
| 595 | const auto b = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint32x4_t>(wrapper::vloadq(non_broadcast_input_ptr + x + 4), broadcast_value, reorder); |
| 596 | wrapper::vstore(output_ptr + x, wrapper::vmovn(wrapper::vcombine(wrapper::vmovn(a), wrapper::vmovn(b)))); |
| 597 | } |
| 598 | if(x <= window_end_x - 4) |
| 599 | { |
| 600 | const auto a = elementwise_comp_op_broadcast<op, InputScalarType, InputVectorType, uint32x4_t>(wrapper::vloadq((non_broadcast_input_ptr + x)), broadcast_value, reorder); |
| 601 | for(int i = 0; i < 4; i++) |
| 602 | { |
| 603 | *(output_ptr + x + i) = wrapper::vgetlane(a, i); |
| 604 | } |
| 605 | x = +4; |
| 606 | } |
| 607 | return x; |
| 608 | } |
| 609 | |
| 610 | template <ComparisonOperation op> |
| 611 | inline int elementwise_comp_op_quantized_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 612 | const uint8_t *non_broadcast_input_ptr, float32x4x4_t broadcast_vector, uint8_t *output_ptr, |
| 613 | int32x4_t voffset_non_broadcast, float32x4_t vscale_non_broadcast, |
| 614 | float32x4_t voffseto, float32x4_t invvscaleo, bool reorder) |
| 615 | { |
| 616 | ARM_COMPUTE_UNUSED(voffseto, invvscaleo); |
| 617 | int x = window_start_x; |
| 618 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 619 | { |
| 620 | const float32x4x4_t af = load_quantized(non_broadcast_input_ptr + x, voffset_non_broadcast, vscale_non_broadcast); |
| 621 | const uint32x4x4_t rf = elementwise_comp_op<op>(reorder ? broadcast_vector : af, reorder ? af : broadcast_vector); |
| 622 | store_quantized(output_ptr + x, rf); |
| 623 | } |
| 624 | return x; |
| 625 | } |
| 626 | |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 627 | template <ComparisonOperation op> |
| 628 | inline int elementwise_comp_op_quantized_signed_broadcast_loop(int window_start_x, int window_end_x, int window_step_x, |
| 629 | const int8_t *non_broadcast_input_ptr, float32x4x4_t broadcast_vector, uint8_t *output_ptr, |
| 630 | int32x4_t voffset_non_broadcast, float32x4_t vscale_non_broadcast, |
| 631 | float32x4_t voffseto, float32x4_t invvscaleo, bool reorder) |
| 632 | { |
| 633 | ARM_COMPUTE_UNUSED(voffseto, invvscaleo); |
| 634 | int x = window_start_x; |
| 635 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 636 | { |
| 637 | const float32x4x4_t af = load_quantized_signed(non_broadcast_input_ptr + x, voffset_non_broadcast, vscale_non_broadcast); |
| 638 | const uint32x4x4_t rf = elementwise_comp_op<op>(reorder ? broadcast_vector : af, reorder ? af : broadcast_vector); |
| 639 | store_quantized(output_ptr + x, rf); |
| 640 | } |
| 641 | return x; |
| 642 | } |
| 643 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 644 | template <typename InputScalarType, typename OutputScalarType, typename InputVectorType> |
| 645 | void elementwise_op(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
| 646 | OutputScalarType (*scalar_func)(const InputScalarType &, const InputScalarType &), |
| 647 | int (*broadcast_func)(int, int, int, const InputScalarType *, const InputScalarType &, OutputScalarType *, const bool), |
| 648 | int (*neon_func)(int, int, int, const InputScalarType *, const InputScalarType *, OutputScalarType *)) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 649 | { |
| 650 | // Create input windows |
| 651 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 652 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 653 | |
| 654 | // Clear X Dimension on execution window as we handle manually |
| 655 | Window win = window; |
| 656 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 657 | |
Michalis Spyrou | e8c0c43 | 2019-01-22 11:08:31 +0000 | [diff] [blame] | 658 | 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] | 659 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 660 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 661 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 662 | |
| 663 | if(is_broadcast_across_x) |
| 664 | { |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 665 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 666 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 667 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 668 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 669 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 670 | |
| 671 | // Clear X Dimension on execution window as we handle manually |
| 672 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 673 | |
| 674 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 675 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 676 | Iterator output(out, win); |
| 677 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 678 | execute_window_loop(win, [&](const Coordinates &) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 679 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 680 | auto output_ptr = reinterpret_cast<OutputScalarType *>(output.ptr()); |
| 681 | const auto non_broadcast_input_ptr = reinterpret_cast<const InputScalarType *>(non_broadcast_input.ptr()); |
| 682 | const InputScalarType broadcast_value = *reinterpret_cast<const InputScalarType *>(broadcast_input.ptr()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 683 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 684 | 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] | 685 | for(; x < window_end_x; ++x) |
| 686 | { |
| 687 | const auto a = *(non_broadcast_input_ptr + x); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 688 | *(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] | 689 | } |
| 690 | }, |
| 691 | broadcast_input, non_broadcast_input, output); |
| 692 | } |
| 693 | else |
| 694 | { |
| 695 | // Clear X Dimension on execution window as we handle manually |
| 696 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 697 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 698 | |
| 699 | Iterator input1(in1, input1_win); |
| 700 | Iterator input2(in2, input2_win); |
| 701 | Iterator output(out, win); |
| 702 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 703 | execute_window_loop(win, [&](const Coordinates &) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 704 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 705 | auto output_ptr = reinterpret_cast<OutputScalarType *>(output.ptr()); |
| 706 | const auto input1_ptr = reinterpret_cast<const InputScalarType *>(input1.ptr()); |
| 707 | const auto input2_ptr = reinterpret_cast<const InputScalarType *>(input2.ptr()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 708 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 709 | 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] | 710 | for(; x < window_end_x; ++x) |
| 711 | { |
| 712 | const auto a = *(input1_ptr + x); |
| 713 | const auto b = *(input2_ptr + x); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 714 | *(output_ptr + x) = (*scalar_func)(a, b); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 715 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 716 | }, |
| 717 | input1, input2, output); |
| 718 | } |
| 719 | } |
| 720 | |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 721 | #if !defined(__ARM_FEATURE_SVE2) |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 722 | void elementwise_op_quantized(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 723 | uint8_t (*scalar_func)(const float &, const float &, UniformQuantizationInfo), |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 724 | int (*broadcast_func)(int, int, int, const uint8_t *, float32x4x4_t, uint8_t *, int32x4_t, float32x4_t, |
| 725 | float32x4_t, float32x4_t, const bool), |
| 726 | int (*neon_func)(int, int, int, const uint8_t *, const uint8_t *, uint8_t *, |
| 727 | int32x4_t, int32x4_t, float32x4_t, float32x4_t, |
| 728 | float32x4_t, float32x4_t)) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 729 | { |
| 730 | // Create input windows |
| 731 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 732 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 733 | |
| 734 | // Clear X Dimension on execution window as we handle manually |
| 735 | Window win = window; |
| 736 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 737 | |
| 738 | const int window_step_x = 16; |
| 739 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 740 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 741 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 742 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 743 | const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform(); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 744 | |
| 745 | // Output quantization info (add 0.5 to round toward the nearest integer - 0.5 rounds away from zero) |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 746 | const float32x4_t voffseto = vdupq_n_f32(output_qinfo.offset + 0.5f); |
| 747 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / output_qinfo.scale); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 748 | |
| 749 | if(is_broadcast_across_x) |
| 750 | { |
| 751 | // Select the broadcast input on the X axis |
| 752 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 753 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 754 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 755 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 756 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 757 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 758 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
| 759 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 760 | |
| 761 | const int32x4_t voffset_non_broadcast = vdupq_n_s32(non_broadcast_qinfo.offset); |
| 762 | const float32x4_t vscale_non_broadcast = vdupq_n_f32(non_broadcast_qinfo.scale); |
| 763 | |
| 764 | // Clear X Dimension on execution window as we handle manually |
| 765 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 766 | |
| 767 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 768 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 769 | Iterator output(out, win); |
| 770 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 771 | execute_window_loop(win, [&](const Coordinates &) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 772 | { |
| 773 | const auto non_broadcast_input_ptr = reinterpret_cast<const uint8_t *>(non_broadcast_input.ptr()); |
| 774 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 775 | |
| 776 | const uint8_t broadcast_value = *reinterpret_cast<const uint8_t *>(broadcast_input.ptr()); |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 777 | const float32x4x4_t broadcast_vector = vdequantize(vdupq_n_u8(broadcast_value), broadcast_qinfo); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 778 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 779 | int x = (*broadcast_func)(window_start_x, window_end_x, window_step_x, non_broadcast_input_ptr, broadcast_vector, output_ptr, |
| 780 | voffset_non_broadcast, vscale_non_broadcast, voffseto, invvscaleo, !is_broadcast_input_2); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 781 | for(; x < window_end_x; ++x) |
| 782 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 783 | const float afs = dequantize_qasymm8(*(non_broadcast_input_ptr + x), non_broadcast_qinfo); |
| 784 | const float bfs = dequantize_qasymm8(broadcast_value, broadcast_qinfo); |
| 785 | *(output_ptr + x) = (*scalar_func)(!is_broadcast_input_2 ? bfs : afs, !is_broadcast_input_2 ? afs : bfs, output_qinfo); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 786 | } |
| 787 | }, |
| 788 | broadcast_input, non_broadcast_input, output); |
| 789 | } |
| 790 | else |
| 791 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 792 | const UniformQuantizationInfo input1_qinfo = in1->info()->quantization_info().uniform(); |
| 793 | const UniformQuantizationInfo input2_qinfo = in2->info()->quantization_info().uniform(); |
| 794 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 795 | // Input1 quantization info |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 796 | const int32x4_t voffset1 = vdupq_n_s32(input1_qinfo.offset); |
| 797 | const float32x4_t vscale1 = vdupq_n_f32(input1_qinfo.scale); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 798 | |
| 799 | // Input2 quantization info |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 800 | const int32x4_t voffset2 = vdupq_n_s32(input2_qinfo.offset); |
| 801 | const float32x4_t vscale2 = vdupq_n_f32(input2_qinfo.scale); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 802 | |
| 803 | // Clear X Dimension on execution window as we handle manually |
| 804 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 805 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 806 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 807 | Iterator input1(in1, input1_win); |
| 808 | Iterator input2(in2, input2_win); |
| 809 | Iterator output(out, win); |
| 810 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 811 | execute_window_loop(win, [&](const Coordinates &) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 812 | { |
| 813 | const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr()); |
| 814 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 815 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 816 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 817 | int x = (*neon_func)(window_start_x, window_end_x, window_step_x, input1_ptr, input2_ptr, output_ptr, voffset1, voffset2, |
| 818 | vscale1, vscale2, voffseto, invvscaleo); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 819 | for(; x < window_end_x; ++x) |
| 820 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 821 | const float afs = dequantize_qasymm8(*(input1_ptr + x), input1_qinfo); |
| 822 | const float bfs = dequantize_qasymm8(*(input2_ptr + x), input2_qinfo); |
| 823 | *(output_ptr + x) = (*scalar_func)(afs, bfs, output_qinfo); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 824 | } |
| 825 | }, |
| 826 | input1, input2, output); |
| 827 | } |
| 828 | } |
| 829 | |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 830 | void elementwise_comp_quantized_signed(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
| 831 | uint8_t (*scalar_func)(const float &, const float &, UniformQuantizationInfo), |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 832 | int (*broadcast_func)(int, int, int, const int8_t *, float32x4x4_t, uint8_t *, int32x4_t, float32x4_t, |
| 833 | float32x4_t, float32x4_t, const bool), |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 834 | int (*neon_func)(int, int, int, const int8_t *, const int8_t *, uint8_t *, |
| 835 | int32x4_t, int32x4_t, float32x4_t, float32x4_t, |
| 836 | float32x4_t, float32x4_t)) |
| 837 | { |
| 838 | // Create input windows |
| 839 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 840 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 841 | |
| 842 | // Clear X Dimension on execution window as we handle manually |
| 843 | Window win = window; |
| 844 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 845 | |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 846 | const int window_step_x = 16; |
| 847 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 848 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 849 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 850 | |
| 851 | const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform(); |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 852 | |
| 853 | const float32x4_t voffseto = vdupq_n_f32(output_qinfo.offset); |
| 854 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / output_qinfo.scale); |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 855 | |
| 856 | if(is_broadcast_across_x) |
| 857 | { |
| 858 | // Select the broadcast input on the X axis |
| 859 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 860 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 861 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 862 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 863 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 864 | |
| 865 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
| 866 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
| 867 | |
| 868 | const int32x4_t voffset_non_broadcast = vdupq_n_s32(non_broadcast_qinfo.offset); |
| 869 | const float32x4_t vscale_non_broadcast = vdupq_n_f32(non_broadcast_qinfo.scale); |
| 870 | |
| 871 | // Clear X Dimension on execution window as we handle manually |
| 872 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 873 | |
| 874 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 875 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 876 | Iterator output(out, win); |
| 877 | |
| 878 | execute_window_loop(win, [&](const Coordinates &) |
| 879 | { |
| 880 | const auto non_broadcast_input_ptr = reinterpret_cast<const int8_t *>(non_broadcast_input.ptr()); |
| 881 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 882 | |
| 883 | const int8_t broadcast_value = *reinterpret_cast<const int8_t *>(broadcast_input.ptr()); |
| 884 | const float32x4x4_t broadcast_vector = vdequantize(vdupq_n_s8(broadcast_value), broadcast_qinfo); |
| 885 | |
| 886 | int x = (*broadcast_func)(window_start_x, window_end_x, window_step_x, non_broadcast_input_ptr, broadcast_vector, output_ptr, |
| 887 | voffset_non_broadcast, vscale_non_broadcast, voffseto, invvscaleo, !is_broadcast_input_2); |
| 888 | for(; x < window_end_x; ++x) |
| 889 | { |
| 890 | const float afs = dequantize_qasymm8_signed(*(non_broadcast_input_ptr + x), non_broadcast_qinfo); |
| 891 | const float bfs = dequantize_qasymm8_signed(broadcast_value, broadcast_qinfo); |
| 892 | *(output_ptr + x) = (*scalar_func)(!is_broadcast_input_2 ? bfs : afs, !is_broadcast_input_2 ? afs : bfs, output_qinfo); |
| 893 | } |
| 894 | }, |
| 895 | broadcast_input, non_broadcast_input, output); |
| 896 | } |
| 897 | else |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 898 | { |
| 899 | const UniformQuantizationInfo input1_qinfo = in1->info()->quantization_info().uniform(); |
| 900 | const UniformQuantizationInfo input2_qinfo = in2->info()->quantization_info().uniform(); |
| 901 | |
| 902 | // Input1 quantization info |
| 903 | const int32x4_t voffset1 = vdupq_n_s32(input1_qinfo.offset); |
| 904 | const float32x4_t vscale1 = vdupq_n_f32(input1_qinfo.scale); |
| 905 | |
| 906 | // Input2 quantization info |
| 907 | const int32x4_t voffset2 = vdupq_n_s32(input2_qinfo.offset); |
| 908 | const float32x4_t vscale2 = vdupq_n_f32(input2_qinfo.scale); |
| 909 | |
| 910 | // Clear X Dimension on execution window as we handle manually |
| 911 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 912 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 913 | |
| 914 | Iterator input1(in1, input1_win); |
| 915 | Iterator input2(in2, input2_win); |
| 916 | Iterator output(out, win); |
| 917 | |
| 918 | execute_window_loop(win, [&](const Coordinates &) |
| 919 | { |
| 920 | const auto input1_ptr = reinterpret_cast<const int8_t *>(input1.ptr()); |
| 921 | const auto input2_ptr = reinterpret_cast<const int8_t *>(input2.ptr()); |
| 922 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 923 | |
| 924 | int x = (*neon_func)(window_start_x, window_end_x, window_step_x, input1_ptr, input2_ptr, output_ptr, voffset1, voffset2, |
| 925 | vscale1, vscale2, voffseto, invvscaleo); |
| 926 | for(; x < window_end_x; ++x) |
| 927 | { |
| 928 | const float afs = dequantize_qasymm8_signed(*(input1_ptr + x), input1_qinfo); |
| 929 | const float bfs = dequantize_qasymm8_signed(*(input2_ptr + x), input2_qinfo); |
| 930 | *(output_ptr + x) = (*scalar_func)(afs, bfs, output_qinfo); |
| 931 | } |
| 932 | }, |
| 933 | input1, input2, output); |
| 934 | } |
| 935 | } |
| 936 | |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 937 | void elementwise_op_quantized_signed(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window, |
| 938 | int8_t (*scalar_func)(const float &, const float &, UniformQuantizationInfo), |
| 939 | int (*broadcast_func)(int, int, int, const int8_t *, float32x4x4_t, int8_t *, int32x4_t, float32x4_t, |
| 940 | float32x4_t, float32x4_t, const bool), |
| 941 | int (*neon_func)(int, int, int, const int8_t *, const int8_t *, int8_t *, |
| 942 | int32x4_t, int32x4_t, float32x4_t, float32x4_t, |
| 943 | float32x4_t, float32x4_t)) |
| 944 | { |
| 945 | // Create input windows |
| 946 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 947 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 948 | |
| 949 | // Clear X Dimension on execution window as we handle manually |
| 950 | Window win = window; |
| 951 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 952 | |
| 953 | const int window_step_x = 16; |
| 954 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 955 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 956 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 957 | |
| 958 | const UniformQuantizationInfo output_qinfo = out->info()->quantization_info().uniform(); |
| 959 | |
morgolock | a359805 | 2019-12-31 12:20:47 +0000 | [diff] [blame] | 960 | const float32x4_t voffseto = vdupq_n_f32(output_qinfo.offset); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 961 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / output_qinfo.scale); |
| 962 | |
| 963 | if(is_broadcast_across_x) |
| 964 | { |
| 965 | // Select the broadcast input on the X axis |
| 966 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 967 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 968 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 969 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 970 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 971 | |
| 972 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
| 973 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
| 974 | |
| 975 | const int32x4_t voffset_non_broadcast = vdupq_n_s32(non_broadcast_qinfo.offset); |
| 976 | const float32x4_t vscale_non_broadcast = vdupq_n_f32(non_broadcast_qinfo.scale); |
| 977 | |
| 978 | // Clear X Dimension on execution window as we handle manually |
| 979 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 980 | |
| 981 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 982 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 983 | Iterator output(out, win); |
| 984 | |
| 985 | execute_window_loop(win, [&](const Coordinates &) |
| 986 | { |
| 987 | const auto non_broadcast_input_ptr = reinterpret_cast<const int8_t *>(non_broadcast_input.ptr()); |
| 988 | const auto output_ptr = reinterpret_cast<int8_t *>(output.ptr()); |
| 989 | |
| 990 | const int8_t broadcast_value = *reinterpret_cast<const int8_t *>(broadcast_input.ptr()); |
Sheri Zhang | 5eaf57c | 2020-05-04 21:38:17 +0100 | [diff] [blame] | 991 | const float32x4x4_t broadcast_vector = vdequantize(vdupq_n_s8(broadcast_value), broadcast_qinfo); |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 992 | |
| 993 | int x = (*broadcast_func)(window_start_x, window_end_x, window_step_x, non_broadcast_input_ptr, broadcast_vector, output_ptr, |
| 994 | voffset_non_broadcast, vscale_non_broadcast, voffseto, invvscaleo, !is_broadcast_input_2); |
| 995 | for(; x < window_end_x; ++x) |
| 996 | { |
| 997 | const float afs = dequantize_qasymm8_signed(*(non_broadcast_input_ptr + x), non_broadcast_qinfo); |
| 998 | const float bfs = dequantize_qasymm8_signed(broadcast_value, broadcast_qinfo); |
| 999 | *(output_ptr + x) = (*scalar_func)(!is_broadcast_input_2 ? bfs : afs, !is_broadcast_input_2 ? afs : bfs, output_qinfo); |
| 1000 | } |
| 1001 | }, |
| 1002 | broadcast_input, non_broadcast_input, output); |
| 1003 | } |
| 1004 | else |
| 1005 | { |
| 1006 | const UniformQuantizationInfo input1_qinfo = in1->info()->quantization_info().uniform(); |
| 1007 | const UniformQuantizationInfo input2_qinfo = in2->info()->quantization_info().uniform(); |
| 1008 | |
| 1009 | // Input1 quantization info |
| 1010 | const int32x4_t voffset1 = vdupq_n_s32(input1_qinfo.offset); |
| 1011 | const float32x4_t vscale1 = vdupq_n_f32(input1_qinfo.scale); |
| 1012 | |
| 1013 | // Input2 quantization info |
| 1014 | const int32x4_t voffset2 = vdupq_n_s32(input2_qinfo.offset); |
| 1015 | const float32x4_t vscale2 = vdupq_n_f32(input2_qinfo.scale); |
| 1016 | |
| 1017 | // Clear X Dimension on execution window as we handle manually |
| 1018 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1019 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1020 | |
| 1021 | Iterator input1(in1, input1_win); |
| 1022 | Iterator input2(in2, input2_win); |
| 1023 | Iterator output(out, win); |
| 1024 | |
| 1025 | execute_window_loop(win, [&](const Coordinates &) |
| 1026 | { |
| 1027 | const auto input1_ptr = reinterpret_cast<const int8_t *>(input1.ptr()); |
| 1028 | const auto input2_ptr = reinterpret_cast<const int8_t *>(input2.ptr()); |
| 1029 | const auto output_ptr = reinterpret_cast<int8_t *>(output.ptr()); |
| 1030 | |
| 1031 | int x = (*neon_func)(window_start_x, window_end_x, window_step_x, input1_ptr, input2_ptr, output_ptr, voffset1, voffset2, |
| 1032 | vscale1, vscale2, voffseto, invvscaleo); |
| 1033 | for(; x < window_end_x; ++x) |
| 1034 | { |
| 1035 | const float afs = dequantize_qasymm8_signed(*(input1_ptr + x), input1_qinfo); |
| 1036 | const float bfs = dequantize_qasymm8_signed(*(input2_ptr + x), input2_qinfo); |
| 1037 | *(output_ptr + x) = (*scalar_func)(afs, bfs, output_qinfo); |
| 1038 | } |
| 1039 | }, |
| 1040 | input1, input2, output); |
| 1041 | } |
| 1042 | } |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 1043 | #endif /* !defined(__ARM_FEATURE_SVE2) */ |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 1044 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1045 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
Michele Di Giorgio | 1c76c1d | 2020-08-28 13:25:31 +0100 | [diff] [blame] | 1046 | void elementwise_comp_op_8(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1047 | { |
| 1048 | elementwise_op<InputScalarType, uint8_t, InputVectorType>(in1, in2, out, window, |
| 1049 | &elementwise_comp_op_scalar<op, InputScalarType>, |
| 1050 | &elementwise_comp_op_broadcast_8_loop<op, InputScalarType, InputVectorType>, |
| 1051 | &elementwise_comp_op_8_loop<op, InputScalarType, InputVectorType>); |
| 1052 | } |
| 1053 | |
| 1054 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1055 | 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] | 1056 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1057 | elementwise_op<InputScalarType, uint8_t, InputVectorType>(in1, in2, out, window, |
| 1058 | &elementwise_comp_op_scalar<op, InputScalarType>, |
| 1059 | &elementwise_comp_op_broadcast_16_loop<op, InputScalarType, InputVectorType>, |
| 1060 | &elementwise_comp_op_16_loop<op, InputScalarType, InputVectorType>); |
| 1061 | } |
| 1062 | |
| 1063 | template <ComparisonOperation op, typename InputScalarType, typename InputVectorType> |
| 1064 | void elementwise_comp_op_32(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1065 | { |
| 1066 | elementwise_op<InputScalarType, uint8_t, InputVectorType>(in1, in2, out, window, |
| 1067 | &elementwise_comp_op_scalar<op, InputScalarType>, |
| 1068 | &elementwise_comp_op_broadcast_32_loop<op, InputScalarType, InputVectorType>, |
| 1069 | &elementwise_comp_op_32_loop<op, InputScalarType, InputVectorType>); |
| 1070 | } |
| 1071 | |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1072 | template <ArithmeticOperation op, typename VectorType> |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1073 | void elementwise_arithm_op(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1074 | { |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1075 | using scalar_type = typename VectorType::scalar_type; |
| 1076 | |
| 1077 | elementwise_op<scalar_type, scalar_type, VectorType>(in1, in2, out, window, |
| 1078 | &elementwise_arithm_op_scalar<op, scalar_type>, |
| 1079 | &elementwise_arithm_op_broadcast_loop<op, scalar_type, VectorType>, |
| 1080 | &elementwise_arithm_op_loop<op, scalar_type, VectorType>); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1081 | } |
| 1082 | |
| 1083 | template <ArithmeticOperation op> |
| 1084 | void elementwise_arithm_op_quantized(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1085 | { |
| 1086 | elementwise_op_quantized(in1, in2, out, window, &elementwise_arithm_op_quantized_scalar<op>, |
| 1087 | &elementwise_arithm_op_quantized_broadcast_loop<op>, |
| 1088 | &elementwise_arithm_op_quantized_loop<op>); |
| 1089 | } |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 1090 | template <ArithmeticOperation op> |
| 1091 | void elementwise_arithm_op_quantized_signed(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1092 | { |
| 1093 | elementwise_op_quantized_signed(in1, in2, out, window, &elementwise_arithm_op_quantized_signed_scalar<op>, |
| 1094 | &elementwise_arithm_op_quantized_signed_broadcast_loop<op>, |
| 1095 | &elementwise_arithm_op_quantized_singed_loop<op>); |
| 1096 | } |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1097 | |
| 1098 | template <ComparisonOperation op> |
| 1099 | void elementwise_comp_op_quantized(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1100 | { |
| 1101 | elementwise_op_quantized(in1, in2, out, window, &elementwise_comp_op_quantized_scalar<op>, |
| 1102 | &elementwise_comp_op_quantized_broadcast_loop<op>, |
| 1103 | &elementwise_comp_op_quantized_loop<op>); |
| 1104 | } |
| 1105 | |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 1106 | template <ComparisonOperation op> |
| 1107 | void elementwise_comp_op_quantized_signed(const ITensor *in1, const ITensor *in2, ITensor *out, const Window &window) |
| 1108 | { |
Michele Di Giorgio | 81870c0 | 2020-04-30 12:02:20 +0100 | [diff] [blame] | 1109 | elementwise_comp_quantized_signed(in1, in2, out, window, &elementwise_comp_op_quantized_scalar<op>, |
| 1110 | &elementwise_comp_op_quantized_signed_broadcast_loop<op>, |
| 1111 | &elementwise_comp_op_quantized_signed_loop<op>); |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 1112 | } |
| 1113 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1114 | std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1115 | configure_func(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output, |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1116 | std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function) |
| 1117 | { |
| 1118 | std::string function_to_call("op_"); |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1119 | function_to_call += string_from_data_type(input1->data_type()) + "_"; |
| 1120 | function_to_call += string_from_data_type(input2->data_type()) + "_"; |
| 1121 | function_to_call += string_from_data_type(output->data_type()); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1122 | |
| 1123 | auto it = map_function.find(function_to_call); |
| 1124 | |
| 1125 | if(it != map_function.end()) |
| 1126 | { |
| 1127 | auto func = it->second; |
| 1128 | return [func](const ITensor * input1, const ITensor * input2, ITensor * output, const Window & window) |
| 1129 | { |
| 1130 | func(input1, input2, output, window); |
| 1131 | }; |
| 1132 | } |
| 1133 | return nullptr; |
| 1134 | } |
| 1135 | |
| 1136 | template <ArithmeticOperation op> |
| 1137 | std::function<void(const ITensor *, const ITensor *, ITensor *, const Window &)> |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1138 | configure_arithm_func(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1139 | { |
| 1140 | static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = |
| 1141 | { |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1142 | #if defined(__ARM_FEATURE_SVE) |
| 1143 | { "op_F32_F32_F32", &arm_compute::cpu::sve::elementwise_arithmetic_op<op, float32_t> }, |
| 1144 | { "op_S32_S32_S32", &arm_compute::cpu::sve::elementwise_arithmetic_op<op, int32_t> }, |
| 1145 | #else /* defined(__ARM_FEATURE_SVE) */ |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1146 | { "op_F32_F32_F32", &elementwise_arithm_op<op, typename wrapper::traits::neon_vector<float, 4>> }, |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1147 | { "op_S32_S32_S32", &elementwise_arithm_op<op, typename wrapper::traits::neon_vector<int32_t, 4>> }, |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1148 | #endif /* defined(__ARM_FEATURE_SVE) */ |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 1149 | #if defined(__ARM_FEATURE_SVE2) |
| 1150 | { "op_QASYMM8_QASYMM8_QASYMM8", &arm_compute::cpu::sve::elementwise_arithmetic_quantized_op<op, uint8_t> }, |
| 1151 | { "op_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED", &arm_compute::cpu::sve::elementwise_arithmetic_quantized_op<op, int8_t> }, |
| 1152 | #else /* defined(__ARM_FEATURE_SVE2) */ |
Michalis Spyrou | 8d4d1b8 | 2019-11-28 11:31:23 +0000 | [diff] [blame] | 1153 | { "op_QASYMM8_QASYMM8_QASYMM8", &elementwise_arithm_op_quantized<op> }, |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 1154 | { "op_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED", &elementwise_arithm_op_quantized_signed<op> }, |
| 1155 | #endif /* defined(__ARM_FEATURE_SVE2) */ |
| 1156 | { "op_S16_S16_S16", &elementwise_arithm_op<op, typename wrapper::traits::neon_vector<int16_t, 8>> }, |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1157 | }; |
| 1158 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1159 | #if defined(__ARM_FEATURE_SVE) |
| 1160 | map_function["op_F16_F16_F16"] = &arm_compute::cpu::sve::elementwise_arithmetic_op<op, float16_t>; |
| 1161 | #else /* defined(__ARM_FEATURE_SVE) */ |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1162 | map_function["op_F16_F16_F16"] = &elementwise_arithm_op<op, typename wrapper::traits::neon_vector<float16_t, 8>>; |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1163 | #endif /* defined(__ARM_FEATURE_SVE) */ |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1164 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 1165 | |
| 1166 | return configure_func(input1, input2, output, map_function); |
| 1167 | } |
| 1168 | |
| 1169 | template <ComparisonOperation op> |
| 1170 | std::function<void(const ITensor *input1, const ITensor *input2, ITensor *output, const Window &window)> |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1171 | configure_comp_func(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1172 | { |
| 1173 | static std::map<std::string, NEElementwiseOperationKernel::ElementwiseFunction *> map_function = |
| 1174 | { |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1175 | #if defined(__ARM_FEATURE_SVE) |
| 1176 | { "op_U8_U8_U8", &arm_compute::cpu::sve::elementwise_comparison_op<op, uint8_t> }, |
| 1177 | { "op_F32_F32_U8", &arm_compute::cpu::sve::elementwise_comparison_op<op, float> }, |
| 1178 | { "op_S16_S16_U8", &arm_compute::cpu::sve::elementwise_comparison_op<op, int16_t> }, |
| 1179 | { "op_S32_S32_U8", &arm_compute::cpu::sve::elementwise_comparison_op<op, int32_t> }, |
| 1180 | #else /* defined(__ARM_FEATURE_SVE) */ |
Michele Di Giorgio | 1c76c1d | 2020-08-28 13:25:31 +0100 | [diff] [blame] | 1181 | { "op_U8_U8_U8", &elementwise_comp_op_8<op, uint8_t, uint8x16_t> }, |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1182 | { "op_F32_F32_U8", &elementwise_comp_op_32<op, float, float32x4_t> }, |
| 1183 | { "op_S16_S16_U8", &elementwise_comp_op_16<op, int16_t, int16x8_t> }, |
| 1184 | { "op_S32_S32_U8", &elementwise_comp_op_32<op, int32_t, int32x4_t> }, |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1185 | #endif /* defined(__ARM_FEATURE_SVE) */ |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 1186 | #if defined(__ARM_FEATURE_SVE2) |
| 1187 | { "op_QASYMM8_SIGNED_QASYMM8_SIGNED_U8", &arm_compute::cpu::sve::elementwise_comparison_quantized_op<op, int8_t> }, |
| 1188 | { "op_QASYMM8_QASYMM8_U8", &arm_compute::cpu::sve::elementwise_comparison_quantized_op<op, uint8_t> } |
| 1189 | #else /* defined(__ARM_FEATURE_SVE2) */ |
morgolock | 74a1696 | 2020-01-15 11:40:49 +0000 | [diff] [blame] | 1190 | { "op_QASYMM8_SIGNED_QASYMM8_SIGNED_U8", &elementwise_comp_op_quantized_signed<op> }, |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1191 | { "op_QASYMM8_QASYMM8_U8", &elementwise_comp_op_quantized<op> } |
Sang-Hoon Park | d23a251 | 2021-01-11 22:19:49 +0000 | [diff] [blame^] | 1192 | #endif /* defined(__ARM_FEATURE_SVE2) */ |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1193 | }; |
| 1194 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Sang-Hoon Park | 5db75c3 | 2021-01-07 16:59:32 +0000 | [diff] [blame] | 1195 | #if defined(__ARM_FEATURE_SVE) |
| 1196 | map_function["op_F16_F16_U8"] = &arm_compute::cpu::sve::elementwise_comparison_op<op, float16_t>; |
| 1197 | #else /* defined(__ARM_FEATURE_SVE) */ |
| 1198 | map_function["op_F16_F16_U8"] = &elementwise_comp_op_16<op, float16_t, float16x8_t>; |
| 1199 | #endif /* defined(__ARM_FEATURE_SVE) */ |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1200 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 1201 | |
| 1202 | return configure_func(input1, input2, output, map_function); |
| 1203 | } |
| 1204 | } // namespace |
| 1205 | |
| 1206 | NEElementwiseOperationKernel::NEElementwiseOperationKernel() |
| 1207 | : _function(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr) |
| 1208 | { |
| 1209 | } |
| 1210 | |
| 1211 | Status NEElementwiseOperationKernel::validate_arguments_common(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 1212 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1213 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input1); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1214 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &input2); |
| 1215 | |
| 1216 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
| 1217 | |
| 1218 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 1219 | |
| 1220 | // Validate in case of configured output |
| 1221 | if(output.total_size() > 0) |
| 1222 | { |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1223 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
| 1224 | "Wrong shape for output"); |
| 1225 | } |
| 1226 | |
| 1227 | return Status{}; |
| 1228 | } |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1229 | |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1230 | void NEElementwiseOperationKernel::configure_common(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1231 | { |
| 1232 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1233 | |
| 1234 | // Configure kernel window |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1235 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1236 | const TensorShape &out_shape = broadcast_pair.first; |
| 1237 | const ValidRegion &valid_region = broadcast_pair.second; |
| 1238 | |
| 1239 | // Auto initialize output if not initialized |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1240 | auto_init_if_empty(*output, out_shape, 1, input1->data_type()); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1241 | |
| 1242 | Window win = calculate_max_window(valid_region); |
| 1243 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1244 | INEKernel::configure(win); |
| 1245 | } |
| 1246 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 1247 | void NEElementwiseOperationKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1248 | { |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1249 | ARM_COMPUTE_UNUSED(info, window); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1250 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 1251 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1252 | ARM_COMPUTE_ERROR_ON(_function == nullptr); |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 1253 | _function(tensors.get_const_tensor(TensorType::ACL_SRC_0), |
| 1254 | tensors.get_const_tensor(TensorType::ACL_SRC_1), |
| 1255 | tensors.get_tensor(TensorType::ACL_DST), window); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /** Arithmetic operators (min, max, squared_diff) */ |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1259 | void NEArithmeticOperationKernel::configure(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1260 | { |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1261 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1, *input2, *output)); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1262 | configure_common(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1263 | switch(op) |
| 1264 | { |
| 1265 | case ArithmeticOperation::MAX: |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1266 | _function = configure_arithm_func<ArithmeticOperation::MAX>(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1267 | break; |
| 1268 | case ArithmeticOperation::MIN: |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1269 | _function = configure_arithm_func<ArithmeticOperation::MIN>(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1270 | break; |
| 1271 | case ArithmeticOperation::SQUARED_DIFF: |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1272 | _function = configure_arithm_func<ArithmeticOperation::SQUARED_DIFF>(input1, input2, output); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1273 | break; |
giuros01 | d513436 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 1274 | case ArithmeticOperation::PRELU: |
| 1275 | _function = configure_arithm_func<ArithmeticOperation::PRELU>(input1, input2, output); |
| 1276 | break; |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1277 | default: |
| 1278 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 1279 | } |
| 1280 | } |
| 1281 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1282 | Status NEArithmeticOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 1283 | { |
Michele Di Giorgio | 1c76c1d | 2020-08-28 13:25:31 +0100 | [diff] [blame] | 1284 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S16, DataType::F16, DataType::S32, DataType::F32); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1285 | // Validate in case of configured output |
| 1286 | if(output.total_size() > 0) |
| 1287 | { |
| 1288 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(&input1, &output); |
| 1289 | } |
| 1290 | return validate_arguments_common(input1, input2, output); |
| 1291 | } |
| 1292 | |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1293 | Status NEArithmeticOperationKernel::validate(ArithmeticOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 1294 | { |
| 1295 | ARM_COMPUTE_UNUSED(op); |
| 1296 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1297 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1298 | return Status{}; |
| 1299 | } |
| 1300 | |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 1301 | /** The division operator */ |
| 1302 | |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1303 | void NEDivisionOperationKernel::configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 1304 | { |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1305 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1, *input2, *output)); |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 1306 | configure_common(input1, input2, output); |
| 1307 | _function = configure_arithm_func<ArithmeticOperation::DIV>(input1, input2, output); |
| 1308 | } |
| 1309 | |
| 1310 | Status NEDivisionOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 1311 | { |
Georgios Pinitas | 1813422 | 2020-09-03 21:00:23 +0100 | [diff] [blame] | 1312 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::S32, DataType::F16, DataType::F32); |
George Wort | a1e7e28 | 2019-01-15 11:00:29 +0000 | [diff] [blame] | 1313 | return NEArithmeticOperationKernel::validate_arguments(input1, input2, output); |
| 1314 | } |
| 1315 | |
| 1316 | Status NEDivisionOperationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 1317 | { |
| 1318 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 1319 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
| 1320 | return Status{}; |
| 1321 | } |
| 1322 | |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 1323 | /** The power operator */ |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1324 | void NEPowerOperationKernel::configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 1325 | { |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1326 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1, *input2, *output)); |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 1327 | configure_common(input1, input2, output); |
| 1328 | _function = configure_arithm_func<ArithmeticOperation::POWER>(input1, input2, output); |
| 1329 | } |
| 1330 | |
| 1331 | Status NEPowerOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 1332 | { |
| 1333 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::F16, DataType::F32); |
| 1334 | return NEArithmeticOperationKernel::validate_arguments(input1, input2, output); |
| 1335 | } |
| 1336 | |
| 1337 | Status NEPowerOperationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 1338 | { |
| 1339 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 1340 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
| 1341 | return Status{}; |
| 1342 | } |
| 1343 | |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1344 | /** Comparison operators (equal, not equal, less than, greater than, less than or equal, greater than or equal) */ |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1345 | void NEComparisonOperationKernel::configure(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output) |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1346 | { |
Michalis Spyrou | ce0c675 | 2020-06-18 10:14:57 +0100 | [diff] [blame] | 1347 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1, *input2, *output)); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1348 | configure_common(input1, input2, output); |
| 1349 | switch(op) |
| 1350 | { |
| 1351 | case ComparisonOperation::Equal: |
| 1352 | _function = configure_comp_func<ComparisonOperation::Equal>(input1, input2, output); |
| 1353 | break; |
| 1354 | case ComparisonOperation::NotEqual: |
| 1355 | _function = configure_comp_func<ComparisonOperation::NotEqual>(input1, input2, output); |
| 1356 | break; |
| 1357 | case ComparisonOperation::Greater: |
| 1358 | _function = configure_comp_func<ComparisonOperation::Greater>(input1, input2, output); |
| 1359 | break; |
| 1360 | case ComparisonOperation::GreaterEqual: |
| 1361 | _function = configure_comp_func<ComparisonOperation::GreaterEqual>(input1, input2, output); |
| 1362 | break; |
| 1363 | case ComparisonOperation::Less: |
| 1364 | _function = configure_comp_func<ComparisonOperation::Less>(input1, input2, output); |
| 1365 | break; |
| 1366 | case ComparisonOperation::LessEqual: |
| 1367 | _function = configure_comp_func<ComparisonOperation::LessEqual>(input1, input2, output); |
| 1368 | break; |
| 1369 | default: |
| 1370 | ARM_COMPUTE_ERROR("NOT_SUPPORTED!"); |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | Status NEComparisonOperationKernel::validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output) |
| 1375 | { |
Michele Di Giorgio | 1c76c1d | 2020-08-28 13:25:31 +0100 | [diff] [blame] | 1376 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::S16, DataType::F16, DataType::S32, DataType::F32); |
George Wort | d88590f | 2018-12-12 17:39:58 +0000 | [diff] [blame] | 1377 | // Validate in case of configured output |
| 1378 | if(output.total_size() > 0) |
| 1379 | { |
| 1380 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&output, 1, DataType::U8); |
| 1381 | } |
| 1382 | return validate_arguments_common(input1, input2, output); |
| 1383 | } |
| 1384 | |
| 1385 | Status NEComparisonOperationKernel::validate(ComparisonOperation op, const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output) |
| 1386 | { |
| 1387 | ARM_COMPUTE_UNUSED(op); |
| 1388 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 1389 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output)); |
| 1390 | return Status{}; |
giuros01 | 92fd943 | 2018-12-03 17:30:00 +0000 | [diff] [blame] | 1391 | } |
| 1392 | } // namespace arm_compute |