Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEPixelWiseMultiplicationKernel.h" |
| 25 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CPP/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/IAccessWindow.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
| 31 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
| 33 | #include "arm_compute/core/Validate.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
| 35 | #include <arm_neon.h> |
| 36 | #include <climits> |
| 37 | #include <cmath> |
| 38 | #include <cstdint> |
| 39 | #include <cstdlib> |
| 40 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 41 | #if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 42 | #include <arm_fp16.h> // needed for float16_t |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 43 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 44 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 45 | using namespace arm_compute; |
| 46 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | class Coordinates; |
| 50 | } // namespace arm_compute |
| 51 | |
| 52 | namespace |
| 53 | { |
| 54 | const float scale255_constant = 1.f / 255.f; |
| 55 | const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant); |
| 56 | const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f); |
| 57 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 58 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 59 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 60 | inline Status validate_arguments(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 61 | { |
| 62 | ARM_COMPUTE_UNUSED(overflow_policy); |
| 63 | ARM_COMPUTE_UNUSED(rounding_policy); |
| 64 | |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input1); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input2, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8, DataType::S16, DataType::F16, DataType::F32); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 69 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output->data_type() == DataType::U8 && (input1->data_type() != DataType::U8 || input2->data_type() != DataType::U8), |
| 70 | "Output can only be U8 if both inputs are U8"); |
| 71 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 72 | const TensorShape &out_shape = TensorShape::broadcast_shape(input1->tensor_shape(), input2->tensor_shape()); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output->tensor_shape(), 0), "Wrong shape for output"); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 75 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 76 | if(std::abs(scale - scale255_constant) < 0.00001f) |
| 77 | { |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_NEAREST_UP && rounding_policy != RoundingPolicy::TO_NEAREST_EVEN); |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_ZERO); |
| 83 | |
| 84 | int exponent = 0; |
| 85 | const float normalized_mantissa = std::frexp(scale, &exponent); |
| 86 | |
| 87 | // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15 |
| 88 | // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14 |
| 89 | // Moreover, it will be negative as we deal with 1/2^n |
| 90 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1)), "Scale value not supported (Should be 1/(2^n) or 1/255"); |
| 91 | } |
| 92 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 93 | return Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 96 | inline std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input1, ITensorInfo *input2, ITensorInfo *output) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 97 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 98 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(*input1, *input2); |
| 99 | const ValidRegion &valid_region = broadcast_pair.second; |
| 100 | |
| 101 | // Auto initialize output if not initialized |
| 102 | { |
| 103 | set_shape_if_empty(*output, input1->tensor_shape()); |
| 104 | |
| 105 | if(input1->data_type() == DataType::S16 || input2->data_type() == DataType::S16) |
| 106 | { |
| 107 | set_format_if_unknown(*output, Format::S16); |
| 108 | } |
| 109 | else if(input1->data_type() == DataType::F32 || input2->data_type() == DataType::F32) |
| 110 | { |
| 111 | set_format_if_unknown(*output, Format::F32); |
| 112 | } |
| 113 | else if(input1->data_type() == DataType::F16 || input2->data_type() == DataType::F16) |
| 114 | { |
| 115 | set_format_if_unknown(*output, Format::F16); |
| 116 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 117 | } |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 118 | |
| 119 | // Configure kernel window |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 120 | Window win = calculate_max_window(valid_region, Steps(num_elems_processed_per_iteration)); |
| 121 | Window win_input1 = win.broadcast_if_dimension_le_one(*input1); |
| 122 | Window win_input2 = win.broadcast_if_dimension_le_one(*input2); |
| 123 | |
| 124 | AccessWindowHorizontal input1_access(input1, 0, num_elems_processed_per_iteration); |
| 125 | AccessWindowHorizontal input2_access(input2, 0, num_elems_processed_per_iteration); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 126 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 127 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 128 | bool window_changed = update_window_and_padding(win_input1, input1_access) |
| 129 | || update_window_and_padding(win_input2, input2_access) |
| 130 | || update_window_and_padding(win, output_access); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 131 | |
| 132 | output_access.set_valid_region(win, valid_region); |
| 133 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 134 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 135 | return std::make_pair(err, win); |
| 136 | } |
| 137 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | /* Scales a given vector by 1/255. |
| 139 | * |
| 140 | * @note This does not work for all cases. e.g. for float of 0.49999999999999994 and large floats. |
| 141 | * |
| 142 | * @param in Input vector to scale. |
| 143 | * @return Scaled output rounded to nearest (round half up). |
| 144 | */ |
| 145 | inline int32x4_t scale255_S32_S32(int32x4_t in) |
| 146 | { |
| 147 | // Scale |
| 148 | const float32x4_t tmp = vmulq_f32(vcvtq_f32_s32(in), scale255_constant_f32q); |
| 149 | // Round to nearest (round half up) |
| 150 | // Add +0.5 for all values |
| 151 | // Afterwards vcvt rounds toward zero |
| 152 | return vcvtq_s32_f32(vaddq_f32(tmp, positive_round_f32q)); |
| 153 | } |
| 154 | |
| 155 | inline uint16x8_t scale255_U16_U16(uint16x8_t in) |
| 156 | { |
| 157 | const int32x4_t tmp_s1 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(in)))); |
| 158 | const int32x4_t tmp_s2 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(in)))); |
| 159 | return vreinterpretq_u16_s16(vcombine_s16(vmovn_s32(tmp_s2), vmovn_s32(tmp_s1))); |
| 160 | } |
| 161 | |
| 162 | template <bool is_scale255, bool is_sat> |
| 163 | void mul_U8_U8_U8_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 164 | { |
| 165 | const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr); |
| 166 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 167 | const auto output = static_cast<uint8_t *__restrict>(output_ptr); |
| 168 | |
| 169 | const uint8x16_t ta1 = vld1q_u8(input1); |
| 170 | const uint8x16_t ta2 = vld1q_u8(input2); |
| 171 | |
| 172 | uint16x8_t tmp1_high = vmovl_u8(vget_high_u8(ta1)); |
| 173 | const uint16x8_t tmp2_high = vmovl_u8(vget_high_u8(ta2)); |
| 174 | uint16x8_t tmp1_low = vmovl_u8(vget_low_u8(ta1)); |
| 175 | const uint16x8_t tmp2_low = vmovl_u8(vget_low_u8(ta2)); |
| 176 | |
| 177 | tmp1_high = vmulq_u16(tmp1_high, tmp2_high); |
| 178 | tmp1_low = vmulq_u16(tmp1_low, tmp2_low); |
| 179 | |
| 180 | if(is_scale255) |
| 181 | { |
| 182 | tmp1_high = scale255_U16_U16(tmp1_high); |
| 183 | tmp1_low = scale255_U16_U16(tmp1_low); |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | const int16x8_t vn = vdupq_n_s16(-n); |
| 188 | |
| 189 | if(is_sat) |
| 190 | { |
| 191 | tmp1_high = vqshlq_u16(tmp1_high, vn); |
| 192 | tmp1_low = vqshlq_u16(tmp1_low, vn); |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | tmp1_high = vshlq_u16(tmp1_high, vn); |
| 197 | tmp1_low = vshlq_u16(tmp1_low, vn); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | if(is_sat) |
| 202 | { |
| 203 | vst1q_u8(output, vcombine_u8(vqmovn_u16(tmp1_low), vqmovn_u16(tmp1_high))); |
| 204 | } |
| 205 | else |
| 206 | { |
| 207 | vst1q_u8(output, vcombine_u8(vmovn_u16(tmp1_low), vmovn_u16(tmp1_high))); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | template <bool is_scale255, bool is_sat> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 212 | inline int16x8_t mul_S16_S16_S16_n_loop(const int16x8_t &input1, const int16x8_t &input2, int n) |
| 213 | { |
| 214 | int32x4_t tmp1_high = vmovl_s16(vget_high_s16(input1)); |
| 215 | const int32x4_t tmp2_high = vmovl_s16(vget_high_s16(input2)); |
| 216 | int32x4_t tmp1_low = vmovl_s16(vget_low_s16(input1)); |
| 217 | const int32x4_t tmp2_low = vmovl_s16(vget_low_s16(input2)); |
| 218 | |
| 219 | tmp1_high = vmulq_s32(tmp1_high, tmp2_high); |
| 220 | tmp1_low = vmulq_s32(tmp1_low, tmp2_low); |
| 221 | |
| 222 | if(is_scale255) |
| 223 | { |
| 224 | tmp1_high = scale255_S32_S32(tmp1_high); |
| 225 | tmp1_low = scale255_S32_S32(tmp1_low); |
| 226 | } |
| 227 | else |
| 228 | { |
| 229 | // Right shift amount |
| 230 | const int32x4_t vn = vdupq_n_s32(-n); |
| 231 | // Left shift amount |
| 232 | const int32x4_t vnl = vdupq_n_s32(n); |
| 233 | // Calculate conversion bit |
| 234 | const uint32x4_t tmp1_high_u = vreinterpretq_u32_s32(tmp1_high); |
| 235 | const uint32x4_t tmp1_low_u = vreinterpretq_u32_s32(tmp1_low); |
| 236 | const uint32x4_t sign_high = vshrq_n_u32(tmp1_high_u, 31); |
| 237 | const uint32x4_t sign_low = vshrq_n_u32(tmp1_low_u, 31); |
| 238 | const int32x4_t sign_high_s = vreinterpretq_s32_u32(sign_high); |
| 239 | const int32x4_t sign_low_s = vreinterpretq_s32_u32(sign_low); |
| 240 | const int32x4_t convert_high = vsubq_s32(vshlq_s32(sign_high_s, vnl), sign_high_s); |
| 241 | const int32x4_t convert_low = vsubq_s32(vshlq_s32(sign_low_s, vnl), sign_low_s); |
| 242 | if(is_sat) |
| 243 | { |
| 244 | tmp1_high = vqshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 245 | tmp1_low = vqshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 246 | } |
| 247 | else |
| 248 | { |
| 249 | tmp1_high = vshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 250 | tmp1_low = vshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | if(is_sat) |
| 255 | { |
| 256 | return vcombine_s16(vqmovn_s32(tmp1_low), vqmovn_s32(tmp1_high)); |
| 257 | } |
| 258 | else |
| 259 | { |
| 260 | return vcombine_s16(vmovn_s32(tmp1_low), vmovn_s32(tmp1_high)); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | template <bool is_scale255, bool is_sat> |
| 265 | inline int16x8x2_t mul_S16_S16_S16_n_k(const int16x8x2_t &input1, const int16x8x2_t &input2, int n) |
| 266 | { |
| 267 | const int16x8x2_t result = |
| 268 | { |
| 269 | { |
| 270 | // First 8 elements |
| 271 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[0], input2.val[0], n), |
| 272 | // Second 8 elements |
| 273 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(input1.val[1], input2.val[1], n) |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | return result; |
| 278 | } |
| 279 | |
| 280 | template <bool is_scale255, bool is_sat> |
| 281 | void mul_S16_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 282 | { |
| 283 | const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr); |
| 284 | const auto input2 = static_cast<const int16_t *__restrict>(input2_ptr); |
| 285 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 286 | |
| 287 | const int16x8x2_t ta1 = vld2q_s16(input1); |
| 288 | const int16x8x2_t ta2 = vld2q_s16(input2); |
| 289 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
| 290 | |
| 291 | vst2q_s16(output, result); |
| 292 | } |
| 293 | |
| 294 | template <bool is_scale255, bool is_sat> |
| 295 | void mul_F32_F32_F32_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale) |
| 296 | { |
| 297 | const auto input1 = static_cast<const float *__restrict>(input1_ptr); |
| 298 | const auto input2 = static_cast<const float *__restrict>(input2_ptr); |
| 299 | const auto output = static_cast<float *__restrict>(output_ptr); |
| 300 | |
| 301 | const float32x4x4_t ta1 = vld4q_f32(input1); |
| 302 | const float32x4x4_t ta2 = vld4q_f32(input2); |
| 303 | const float32x4_t scale_vec = vdupq_n_f32(scale); |
| 304 | const float32x4x4_t result = |
| 305 | { |
| 306 | { |
| 307 | vmulq_f32(vmulq_f32(ta1.val[0], ta2.val[0]), scale_vec), |
| 308 | vmulq_f32(vmulq_f32(ta1.val[1], ta2.val[1]), scale_vec), |
| 309 | vmulq_f32(vmulq_f32(ta1.val[2], ta2.val[2]), scale_vec), |
| 310 | vmulq_f32(vmulq_f32(ta1.val[3], ta2.val[3]), scale_vec) |
| 311 | } |
| 312 | }; |
| 313 | vst4q_f32(output, result); |
| 314 | } |
| 315 | |
| 316 | template <bool is_scale255, bool is_sat> |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 317 | void mul_F16_F16_F16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, float scale) |
| 318 | { |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 319 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 320 | const auto input1 = static_cast<const float16_t *__restrict>(input1_ptr); |
| 321 | const auto input2 = static_cast<const float16_t *__restrict>(input2_ptr); |
| 322 | const auto output = static_cast<float16_t *__restrict>(output_ptr); |
| 323 | const float16x8x2_t ta1 = vld2q_f16(input1); |
| 324 | const float16x8x2_t ta2 = vld2q_f16(input2); |
| 325 | const float16x8_t scale_vec = vdupq_n_f16(scale); |
| 326 | const float16x8x2_t result = |
| 327 | { |
| 328 | { |
| 329 | vmulq_f16(vmulq_f16(ta1.val[0], ta2.val[0]), scale_vec), |
| 330 | vmulq_f16(vmulq_f16(ta1.val[1], ta2.val[1]), scale_vec), |
| 331 | } |
| 332 | }; |
| 333 | vst2q_f16(output, result); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 334 | #else /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Georgios Pinitas | 30f0215 | 2017-09-27 11:20:48 +0100 | [diff] [blame] | 335 | ARM_COMPUTE_UNUSED(input1_ptr); |
| 336 | ARM_COMPUTE_UNUSED(input2_ptr); |
| 337 | ARM_COMPUTE_UNUSED(output_ptr); |
| 338 | ARM_COMPUTE_UNUSED(scale); |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 339 | ARM_COMPUTE_ERROR("Not supported. Recompile the library with arch=arm64-v8.2-a."); |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 340 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | template <bool is_scale255, bool is_sat> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 344 | void mul_U8_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 345 | { |
| 346 | const auto input1 = static_cast<const uint8_t *__restrict>(input1_ptr); |
| 347 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 348 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 349 | |
| 350 | const uint8x16_t bv = vld1q_u8(input2); |
| 351 | const uint8x16_t av = vld1q_u8(input1); |
| 352 | |
| 353 | uint16x8_t tmp_low = vmovl_u8(vget_low_u8(av)); |
| 354 | uint16x8_t tmp_high = vmovl_u8(vget_high_u8(av)); |
| 355 | tmp_low = vmulq_u16(tmp_low, vmovl_u8(vget_low_u8(bv))); |
| 356 | tmp_high = vmulq_u16(tmp_high, vmovl_u8(vget_high_u8(bv))); |
| 357 | |
| 358 | if(is_scale255) |
| 359 | { |
| 360 | tmp_low = scale255_U16_U16(tmp_low); |
| 361 | tmp_high = scale255_U16_U16(tmp_high); |
| 362 | } |
| 363 | else |
| 364 | { |
| 365 | const int16x8_t vn = vdupq_n_s16(-n); |
| 366 | |
| 367 | if(is_sat) |
| 368 | { |
| 369 | tmp_low = vqshlq_u16(tmp_low, vn); |
| 370 | tmp_high = vqshlq_u16(tmp_high, vn); |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | tmp_low = vshlq_u16(tmp_low, vn); |
| 375 | tmp_high = vshlq_u16(tmp_high, vn); |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | if(is_sat) |
| 380 | { |
| 381 | static const uint16x8_t max = vdupq_n_u16(SHRT_MAX); |
| 382 | |
| 383 | tmp_low = vminq_u16(tmp_low, max); |
| 384 | tmp_high = vminq_u16(tmp_high, max); |
| 385 | } |
| 386 | |
| 387 | vst1q_s16(output, vreinterpretq_s16_u16(tmp_low)); |
| 388 | vst1q_s16(output + 8, vreinterpretq_s16_u16(tmp_high)); |
| 389 | } |
| 390 | |
| 391 | template <bool is_scale255, bool is_sat> |
| 392 | void mul_S16_U8_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 393 | { |
| 394 | const auto input1 = static_cast<const int16_t *__restrict>(input1_ptr); |
| 395 | const auto input2 = static_cast<const uint8_t *__restrict>(input2_ptr); |
| 396 | const auto output = static_cast<int16_t *__restrict>(output_ptr); |
| 397 | |
| 398 | const int16x8x2_t ta1 = vld2q_s16(input1); |
| 399 | const uint8x8x2_t ta2u = vld2_u8(input2); |
| 400 | const int16x8x2_t ta2 = |
| 401 | { |
| 402 | { |
| 403 | vreinterpretq_s16_u16(vmovl_u8(ta2u.val[0])), |
| 404 | vreinterpretq_s16_u16(vmovl_u8(ta2u.val[1])) |
| 405 | } |
| 406 | }; |
| 407 | |
| 408 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
| 409 | |
| 410 | vst2q_s16(output, result); |
| 411 | } |
| 412 | |
| 413 | template <bool is_scale255, bool is_sat> |
| 414 | void mul_U8_S16_S16_n(const void *__restrict input1_ptr, const void *__restrict input2_ptr, void *__restrict output_ptr, int n) |
| 415 | { |
| 416 | // Simply swap the two input buffers |
| 417 | mul_S16_U8_S16_n<is_scale255, is_sat>(input2_ptr, input1_ptr, output_ptr, n); |
| 418 | } |
| 419 | } // namespace |
| 420 | |
| 421 | NEPixelWiseMultiplicationKernel::NEPixelWiseMultiplicationKernel() |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 422 | : _func_float(nullptr), _func_int(nullptr), _input1(nullptr), _input2(nullptr), _output(nullptr), _scale{ 0 }, _scale_exponent{ 0 } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 423 | { |
| 424 | } |
| 425 | |
| 426 | void NEPixelWiseMultiplicationKernel::configure(const ITensor *input1, const ITensor *input2, ITensor *output, float scale, ConvertPolicy overflow_policy, RoundingPolicy rounding_policy) |
| 427 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 428 | ARM_COMPUTE_UNUSED(rounding_policy); |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 429 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 430 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 431 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), scale, overflow_policy, rounding_policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 432 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 433 | // Configure kernel window |
| 434 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info()); |
| 435 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 436 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 437 | _input1 = input1; |
| 438 | _input2 = input2; |
| 439 | _output = output; |
| 440 | _scale = scale; |
| 441 | _scale_exponent = 0; |
| 442 | _func_int = nullptr; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 443 | _func_float = nullptr; |
| 444 | |
| 445 | bool is_scale_255 = false; |
| 446 | // Check and validate scaling factor |
| 447 | if(std::abs(scale - scale255_constant) < 0.00001f) |
| 448 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 449 | is_scale_255 = true; |
| 450 | } |
| 451 | else |
| 452 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 453 | int exponent = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 454 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 455 | std::frexp(scale, &exponent); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 456 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 457 | // Store the positive exponent. We know that we compute 1/2^n |
| 458 | // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 |
| 459 | _scale_exponent = std::abs(exponent - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | const DataType dt_input1 = input1->info()->data_type(); |
| 463 | const DataType dt_input2 = input2->info()->data_type(); |
| 464 | const DataType dt_output = output->info()->data_type(); |
| 465 | const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE); |
| 466 | |
| 467 | if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::U8 == dt_output) |
| 468 | { |
| 469 | if(is_scale_255) |
| 470 | { |
| 471 | _func_int = is_sat ? &mul_U8_U8_U8_n<true, true> : &mul_U8_U8_U8_n<true, false>; |
| 472 | } |
| 473 | else |
| 474 | { |
| 475 | _func_int = is_sat ? &mul_U8_U8_U8_n<false, true> : &mul_U8_U8_U8_n<false, false>; |
| 476 | } |
| 477 | } |
| 478 | else if(DataType::S16 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
| 479 | { |
| 480 | if(is_scale_255) |
| 481 | { |
| 482 | _func_int = is_sat ? &mul_S16_S16_S16_n<true, true> : &mul_S16_S16_S16_n<true, false>; |
| 483 | } |
| 484 | else |
| 485 | { |
| 486 | _func_int = is_sat ? &mul_S16_S16_S16_n<false, true> : &mul_S16_S16_S16_n<false, false>; |
| 487 | } |
| 488 | } |
| 489 | else if(DataType::S16 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
| 490 | { |
| 491 | if(is_scale_255) |
| 492 | { |
| 493 | _func_int = is_sat ? &mul_S16_U8_S16_n<true, true> : &mul_S16_U8_S16_n<true, false>; |
| 494 | } |
| 495 | else |
| 496 | { |
| 497 | _func_int = is_sat ? &mul_S16_U8_S16_n<false, true> : &mul_S16_U8_S16_n<false, false>; |
| 498 | } |
| 499 | } |
| 500 | else if(DataType::U8 == dt_input1 && DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
| 501 | { |
| 502 | if(is_scale_255) |
| 503 | { |
| 504 | _func_int = is_sat ? &mul_U8_S16_S16_n<true, true> : &mul_U8_S16_S16_n<true, false>; |
| 505 | } |
| 506 | else |
| 507 | { |
| 508 | _func_int = is_sat ? &mul_U8_S16_S16_n<false, true> : &mul_U8_S16_S16_n<false, false>; |
| 509 | } |
| 510 | } |
| 511 | else if(DataType::U8 == dt_input1 && DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
| 512 | { |
| 513 | if(is_scale_255) |
| 514 | { |
| 515 | _func_int = is_sat ? &mul_U8_U8_S16_n<true, true> : &mul_U8_U8_S16_n<true, false>; |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | _func_int = is_sat ? &mul_U8_U8_S16_n<false, true> : &mul_U8_U8_S16_n<false, false>; |
| 520 | } |
| 521 | } |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 522 | else if(DataType::F16 == dt_input1 && DataType::F16 == dt_input2 && DataType::F16 == dt_output) |
| 523 | { |
| 524 | _func_float = &mul_F16_F16_F16_n<false, false>; |
| 525 | _func_int = nullptr; |
| 526 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 527 | else if(DataType::F32 == dt_input1 && DataType::F32 == dt_input2 && DataType::F32 == dt_output) |
| 528 | { |
| 529 | _func_float = &mul_F32_F32_F32_n<false, false>; |
| 530 | _func_int = nullptr; |
| 531 | } |
| 532 | else |
| 533 | { |
| 534 | ARM_COMPUTE_ERROR("You called with the wrong img formats"); |
| 535 | } |
| 536 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 537 | INEKernel::configure(win_config.second); |
| 538 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 539 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 540 | Status NEPixelWiseMultiplicationKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, float scale, ConvertPolicy overflow_policy, |
| 541 | RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 542 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 543 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 544 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, scale, overflow_policy, rounding_policy)); |
| 545 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), output->clone().get()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 546 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 547 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 548 | } |
| 549 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 550 | void NEPixelWiseMultiplicationKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 551 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 552 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 553 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 554 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 555 | |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 556 | const TensorShape &in_shape1 = _input1->info()->tensor_shape(); |
| 557 | const TensorShape &in_shape2 = _input2->info()->tensor_shape(); |
| 558 | const TensorShape &out_shape = _output->info()->tensor_shape(); |
| 559 | |
| 560 | bool can_collapse = true; |
| 561 | if(std::min(in_shape1.total_size(), in_shape2.total_size()) > 1) |
| 562 | { |
| 563 | can_collapse = (std::min(in_shape1.num_dimensions(), in_shape2.num_dimensions()) > Window::DimZ); |
| 564 | for(size_t d = Window::DimZ; can_collapse && (d < out_shape.num_dimensions()); ++d) |
| 565 | { |
| 566 | can_collapse = (in_shape1[d] == in_shape2[d]); |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | bool has_collapsed = false; |
| 571 | Window collapsed = can_collapse ? window.collapse_if_possible(INEKernel::window(), Window::DimZ, &has_collapsed) : window; |
| 572 | |
| 573 | const TensorShape &in_shape1_collapsed = has_collapsed ? in_shape1.collapsed_from(Window::DimZ) : in_shape1; |
| 574 | const TensorShape &in_shape2_collapsed = has_collapsed ? in_shape2.collapsed_from(Window::DimZ) : in_shape2; |
| 575 | |
| 576 | Window slice = collapsed.first_slice_window_3D(); |
| 577 | Window slice_input1 = slice.broadcast_if_dimension_le_one(in_shape1_collapsed); |
| 578 | Window slice_input2 = slice.broadcast_if_dimension_le_one(in_shape2_collapsed); |
| 579 | |
| 580 | Iterator input1(_input1, slice_input1); |
| 581 | Iterator input2(_input2, slice_input2); |
| 582 | Iterator output(_output, slice); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 583 | |
| 584 | if(_func_int != nullptr) |
| 585 | { |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 586 | execute_window_loop(collapsed, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 587 | { |
| 588 | (*_func_int)(input1.ptr(), input2.ptr(), output.ptr(), _scale_exponent); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 589 | collapsed.slide_window_slice_3D(slice_input1); |
| 590 | collapsed.slide_window_slice_3D(slice_input2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 591 | }, |
| 592 | input1, input2, output); |
| 593 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 594 | else |
| 595 | { |
| 596 | ARM_COMPUTE_ERROR_ON(_func_float == nullptr); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 597 | execute_window_loop(collapsed, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 598 | { |
| 599 | (*_func_float)(input1.ptr(), input2.ptr(), output.ptr(), _scale); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 600 | collapsed.slide_window_slice_3D(slice_input1); |
| 601 | collapsed.slide_window_slice_3D(slice_input2); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 602 | }, |
| 603 | input1, input2, output); |
| 604 | } |
| 605 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 606 | |
| 607 | BorderSize NEPixelWiseMultiplicationKernel::border_size() const |
| 608 | { |
| 609 | const unsigned int replicateSize = _output->info()->dimension(0) - std::min(_input1->info()->dimension(0), _input2->info()->dimension(0)); |
| 610 | const unsigned int border = std::min<unsigned int>(num_elems_processed_per_iteration - 1U, replicateSize); |
| 611 | return BorderSize(0, border, 0, 0); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 612 | } |