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