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