Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Pablo Marquez Tello | 568aab6 | 2023-11-20 14:20:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2023 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuMulKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/TensorInfo.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Pablo Marquez Tello | 568aab6 | 2023-11-20 14:20:01 +0000 | [diff] [blame] | 29 | #include "src/core/common/Registrars.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/CPP/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/AutoConfiguration.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 33 | #include "src/core/NEON/NEAsymm.h" |
| 34 | #include "src/core/NEON/NESymm.h" |
| 35 | #include "src/core/NEON/wrapper/wrapper.h" |
Pablo Marquez Tello | 568aab6 | 2023-11-20 14:20:01 +0000 | [diff] [blame] | 36 | #include "src/cpu/kernels/mul/generic/neon/list.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | #include <arm_neon.h> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
Gunes Bayir | f16973b | 2022-11-29 13:12:08 +0000 | [diff] [blame] | 42 | #if defined(ENABLE_FP32_KERNELS) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 43 | static constexpr size_t default_mws_N1_fp32_neon = 22447; |
| 44 | static constexpr size_t default_mws_V1_fp32_neon = 38982; |
Gunes Bayir | f16973b | 2022-11-29 13:12:08 +0000 | [diff] [blame] | 45 | #endif /* ENABLE_FP32_KERNELS */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 46 | static constexpr size_t default_mws_other_platforms_1d_tensor = 10240; |
| 47 | } // namespace |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 48 | namespace arm_compute |
| 49 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 50 | namespace cpu |
| 51 | { |
| 52 | namespace kernels |
| 53 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 54 | namespace |
| 55 | { |
| 56 | const float scale255_constant = 1.f / 255.f; |
| 57 | const float32x4_t scale255_constant_f32q = vdupq_n_f32(scale255_constant); |
| 58 | const float32x4_t positive_round_f32q = vdupq_n_f32(0.5f); |
| 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | inline Status validate_arguments(const ITensorInfo *src1, |
| 61 | const ITensorInfo *src2, |
| 62 | const ITensorInfo *dst, |
| 63 | float scale, |
| 64 | ConvertPolicy overflow_policy, |
| 65 | RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 66 | { |
| 67 | ARM_COMPUTE_UNUSED(overflow_policy); |
| 68 | ARM_COMPUTE_UNUSED(rounding_policy); |
| 69 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 1, DataType::U8, DataType::QASYMM8, |
| 72 | DataType::QASYMM8_SIGNED, DataType::S16, DataType::S32, |
| 73 | DataType::QSYMM16, DataType::F16, DataType::F32); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 1, DataType::U8, DataType::QASYMM8, |
| 75 | DataType::QASYMM8_SIGNED, DataType::S16, DataType::S32, |
| 76 | DataType::QSYMM16, DataType::F16, DataType::F32); |
| 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::U8, DataType::QASYMM8, |
| 78 | DataType::QASYMM8_SIGNED, DataType::S16, DataType::QSYMM16, |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 79 | DataType::S32, DataType::F16, DataType::F32); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | if (is_data_type_quantized(src1->data_type()) || is_data_type_quantized(src2->data_type())) |
Pablo Tello | 52ea9c2 | 2019-12-10 11:28:53 +0000 | [diff] [blame] | 81 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src1, src2); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 83 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(overflow_policy == ConvertPolicy::WRAP, |
| 84 | "ConvertPolicy cannot be WRAP if datatype is quantized"); |
Pablo Tello | 52ea9c2 | 2019-12-10 11:28:53 +0000 | [diff] [blame] | 85 | } |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | if (dst->total_size() > 0) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 88 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 89 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), |
| 91 | "Wrong shape for dst"); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 92 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 93 | // clang-format off |
| 94 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 95 | !(src1->data_type() == src2->data_type() && src2->data_type() == dst->data_type()) && |
| 96 | !(src1->data_type() == DataType::U8 && src2->data_type() == DataType::U8 && dst->data_type() == DataType::S16) && |
| 97 | !(src1->data_type() == DataType::U8 && src2->data_type() == DataType::S16 && dst->data_type() == DataType::S16) && |
| 98 | !(src1->data_type() == DataType::S16 && src2->data_type() == DataType::U8 && dst->data_type() == DataType::S16) && |
| 99 | !(src1->data_type() == DataType::S16 && src2->data_type() == DataType::U8 && dst->data_type() == DataType::S16) && |
| 100 | !(src1->data_type() == DataType::QSYMM16 && src2->data_type() == DataType::QSYMM16 && dst->data_type() == DataType::S32) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 101 | , "Invalid data type combination"); |
| 102 | // clang-format on |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->data_type() == DataType::S16 && dst->data_type() == DataType::S32 && |
| 104 | scale != 1.f, |
| 105 | "Unsupported scale for QSYMM16 inputs and S32 dst"); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 106 | } |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 107 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 108 | if (std::abs(scale - scale255_constant) < 0.00001f) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 109 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_NEAREST_UP && |
| 111 | rounding_policy != RoundingPolicy::TO_NEAREST_EVEN); |
| 112 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src1->data_type() == DataType::S32 && src2->data_type() == DataType::S32 && |
| 113 | dst->data_type() == DataType::S32, |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 114 | "Scale == 1/255 is not supported if input and dst are of data type S32"); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 115 | } |
| 116 | else |
| 117 | { |
| 118 | ARM_COMPUTE_RETURN_ERROR_ON(rounding_policy != RoundingPolicy::TO_ZERO); |
| 119 | |
| 120 | int exponent = 0; |
| 121 | const float normalized_mantissa = std::frexp(scale, &exponent); |
| 122 | |
| 123 | // Use int scaling if factor is equal to 1/2^n for 0 <= n <= 15 |
| 124 | // frexp returns 0.5 as mantissa which means that the exponent will be in the range of -1 <= e <= 14 |
| 125 | // Moreover, it will be negative as we deal with 1/2^n |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!((normalized_mantissa == 0.5f) && (-14 <= exponent) && (exponent <= 1)), |
| 127 | "Scale value not supported (Should be 1/(2^n) or 1/255"); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 128 | } |
| 129 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 130 | return Status{}; |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 133 | /* Scales a given vector by 1/255. |
| 134 | * |
| 135 | * @note This does not work for all cases. e.g. for float of 0.49999999999999994 and large floats. |
| 136 | * |
| 137 | * @param in Input vector to scale. |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 138 | * @return Scaled dst rounded to nearest (round half up). |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | */ |
| 140 | inline int32x4_t scale255_S32_S32(int32x4_t in) |
| 141 | { |
| 142 | // Scale |
| 143 | const float32x4_t tmp = vmulq_f32(vcvtq_f32_s32(in), scale255_constant_f32q); |
| 144 | // Round to nearest (round half up) |
| 145 | // Add +0.5 for all values |
| 146 | // Afterwards vcvt rounds toward zero |
| 147 | return vcvtq_s32_f32(vaddq_f32(tmp, positive_round_f32q)); |
| 148 | } |
| 149 | |
| 150 | inline uint16x8_t scale255_U16_U16(uint16x8_t in) |
| 151 | { |
| 152 | const int32x4_t tmp_s1 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(in)))); |
| 153 | const int32x4_t tmp_s2 = scale255_S32_S32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(in)))); |
| 154 | return vreinterpretq_u16_s16(vcombine_s16(vmovn_s32(tmp_s2), vmovn_s32(tmp_s1))); |
| 155 | } |
| 156 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 157 | template <typename T> |
| 158 | inline typename std::enable_if<std::is_same<T, int8_t>::value, int8x16_t>::type |
| 159 | vquantize(float32x4x4_t val, const UniformQuantizationInfo &info) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 160 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 161 | return vquantize_signed(val, info); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 164 | template <typename T> |
| 165 | inline typename std::enable_if<std::is_same<T, uint8_t>::value, uint8x16_t>::type |
| 166 | vquantize(float32x4x4_t val, const UniformQuantizationInfo &info) |
Pablo Tello | 52ea9c2 | 2019-12-10 11:28:53 +0000 | [diff] [blame] | 167 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 168 | return vquantize(val, info); |
Pablo Tello | 52ea9c2 | 2019-12-10 11:28:53 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 171 | template <typename T> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 172 | void mul_saturate_quantized_8(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, float scale) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 173 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 174 | // Create input windows |
| 175 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 176 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 177 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 178 | |
| 179 | // Clear X Dimension on execution window as we handle manually |
| 180 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 181 | |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 182 | const int window_step_x = 16 / sizeof(T); |
| 183 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 184 | const auto window_end_x = static_cast<int>(window.x().end()); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 185 | const bool is_broadcast_across_x = src1->info()->tensor_shape().x() != src2->info()->tensor_shape().x(); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 186 | |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 187 | const UniformQuantizationInfo output_qua_info = out->info()->quantization_info().uniform(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 188 | const UniformQuantizationInfo tmp_qua_info = {output_qua_info.scale / scale, output_qua_info.offset}; |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 189 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 190 | if (is_broadcast_across_x) |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 191 | { |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 192 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 193 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 194 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 195 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? src2 : src1; |
| 196 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src2 : src1; |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 197 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 199 | |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 200 | // Clear X Dimension on execution window as we handle manually |
| 201 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 202 | |
| 203 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 204 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 205 | Iterator dst(out, win); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 206 | |
| 207 | using ExactTagType = typename wrapper::traits::neon_vector<T, window_step_x>::tag_type; |
| 208 | |
Omar Al Khatib | b230a1f | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 209 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 210 | win, |
| 211 | [&](const Coordinates &) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 212 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 213 | const auto non_broadcast_input_ptr = reinterpret_cast<const T *>(non_broadcast_input.ptr()); |
| 214 | const auto output_ptr = reinterpret_cast<T *>(dst.ptr()); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 215 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 216 | const auto broadcast_value = *reinterpret_cast<const T *>(broadcast_input.ptr()); |
| 217 | const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{}); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 218 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 219 | // Compute window_step_x elements per iteration |
| 220 | int x = window_start_x; |
| 221 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 222 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 223 | const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 224 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 225 | // Dequantize inputs |
| 226 | const float32x4x4_t in1_f32x4x4 = vdequantize(non_broadcast_v, non_broadcast_qinfo); |
| 227 | const float32x4x4_t in2_f32x4x4 = vdequantize(broadcast_value_vec, broadcast_qinfo); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 228 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | const float32x4x4_t out_f32x4x4 = { |
| 230 | vmulq_f32(in1_f32x4x4.val[0], in2_f32x4x4.val[0]), |
| 231 | vmulq_f32(in1_f32x4x4.val[1], in2_f32x4x4.val[1]), |
| 232 | vmulq_f32(in1_f32x4x4.val[2], in2_f32x4x4.val[2]), |
| 233 | vmulq_f32(in1_f32x4x4.val[3], in2_f32x4x4.val[3]), |
| 234 | }; |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 235 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 236 | // Quantize dst |
| 237 | const auto result = vquantize<T>(out_f32x4x4, tmp_qua_info); |
| 238 | wrapper::vstore(output_ptr + x, result); |
| 239 | } |
| 240 | |
| 241 | // Compute left-over elements |
| 242 | for (; x < window_end_x; ++x) |
| 243 | { |
| 244 | // Dequantize inputs |
| 245 | const T src1 = *(non_broadcast_input_ptr + x); |
| 246 | const float tmp_in1 = Qasymm8QuantizationHelper<T>::dequantize(src1, non_broadcast_qinfo); |
| 247 | const float tmp_in2 = Qasymm8QuantizationHelper<T>::dequantize(broadcast_value, broadcast_qinfo); |
| 248 | const float tmp_f = tmp_in1 * tmp_in2; |
| 249 | |
| 250 | // Quantize dst |
| 251 | const auto tmp_qua = Qasymm8QuantizationHelper<T>::quantize(tmp_f, tmp_qua_info); |
| 252 | *(output_ptr + x) = tmp_qua; |
| 253 | } |
| 254 | }, |
| 255 | broadcast_input, non_broadcast_input, dst); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 256 | } |
| 257 | else |
| 258 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 259 | const UniformQuantizationInfo input1_qua_info = src1->info()->quantization_info().uniform(); |
| 260 | const UniformQuantizationInfo input2_qua_info = src2->info()->quantization_info().uniform(); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 261 | |
| 262 | // Clear X Dimension on execution window as we handle manually |
| 263 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 264 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 265 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 266 | Iterator input1(src1, input1_win); |
| 267 | Iterator input2(src2, input2_win); |
| 268 | Iterator dst(out, win); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 269 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 270 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 271 | win, |
| 272 | [&](const Coordinates &) |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 273 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 274 | const auto input1_ptr = reinterpret_cast<const T *>(input1.ptr()); |
| 275 | const auto input2_ptr = reinterpret_cast<const T *>(input2.ptr()); |
| 276 | const auto output_ptr = reinterpret_cast<T *>(dst.ptr()); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 277 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 278 | // Compute window_step_x elements per iteration |
| 279 | int x = window_start_x; |
| 280 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 281 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 282 | const auto input1_q = wrapper::vloadq(input1_ptr + x); |
| 283 | const auto input2_q = wrapper::vloadq(input2_ptr + x); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 284 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 285 | // Dequantize inputs |
| 286 | const float32x4x4_t in1_f32x4x4 = vdequantize(input1_q, input1_qua_info); |
| 287 | const float32x4x4_t in2_f32x4x4 = vdequantize(input2_q, input2_qua_info); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 288 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 289 | const float32x4x4_t out_f32x4x4 = { |
| 290 | vmulq_f32(in1_f32x4x4.val[0], in2_f32x4x4.val[0]), |
| 291 | vmulq_f32(in1_f32x4x4.val[1], in2_f32x4x4.val[1]), |
| 292 | vmulq_f32(in1_f32x4x4.val[2], in2_f32x4x4.val[2]), |
| 293 | vmulq_f32(in1_f32x4x4.val[3], in2_f32x4x4.val[3]), |
| 294 | }; |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 295 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 296 | // Quantize dst |
| 297 | const auto result = vquantize<T>(out_f32x4x4, tmp_qua_info); |
| 298 | wrapper::vstore(output_ptr + x, result); |
| 299 | } |
| 300 | |
| 301 | // Compute left-over elements |
| 302 | for (; x < window_end_x; ++x) |
| 303 | { |
| 304 | // Dequantize inputs |
| 305 | const T src1 = *(input1_ptr + x); |
| 306 | const T src2 = *(input2_ptr + x); |
| 307 | const float tmp_in1 = Qasymm8QuantizationHelper<T>::dequantize(src1, input1_qua_info); |
| 308 | const float tmp_in2 = Qasymm8QuantizationHelper<T>::dequantize(src2, input2_qua_info); |
| 309 | const float tmp_f = tmp_in1 * tmp_in2; |
| 310 | |
| 311 | // Quantize dst |
| 312 | const auto tmp_qua = Qasymm8QuantizationHelper<T>::quantize(tmp_f, tmp_qua_info); |
| 313 | *(output_ptr + x) = tmp_qua; |
| 314 | } |
| 315 | }, |
| 316 | input1, input2, dst); |
Sheri Zhang | a449a36 | 2020-07-16 15:52:25 +0100 | [diff] [blame] | 317 | } |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 318 | } |
| 319 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 320 | bool mul_q8_neon_fixedpoint_possible(const ITensorInfo *src0, |
| 321 | const ITensorInfo *src1, |
| 322 | const ITensorInfo *dst, |
| 323 | float scale) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 324 | { |
| 325 | const auto iq0 = src0->quantization_info().uniform(); |
| 326 | const auto iq1 = src1->quantization_info().uniform(); |
| 327 | const auto oq = dst->quantization_info().uniform(); |
| 328 | |
| 329 | const auto multiplier = ((iq0.scale * iq1.scale) / oq.scale) * scale; |
| 330 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 331 | if (multiplier < -8191.f || multiplier > 8191.f) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 332 | { |
| 333 | //The multiplier cannot be stored as a 14.18 signed fixed-point number |
| 334 | return false; |
| 335 | } |
| 336 | |
| 337 | const auto offset_out = float(oq.offset); |
| 338 | |
| 339 | const auto max_result = multiplier * (256) * (256) + offset_out; |
| 340 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 341 | if (max_result > 8191.f) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 342 | { |
| 343 | //It might not be possible to store the result as a 14.18 signed fixed-point number. |
| 344 | return false; |
| 345 | } |
| 346 | |
| 347 | return true; |
| 348 | } |
| 349 | |
| 350 | template <typename ScalarType> |
| 351 | void mul_q8_neon_fixedpoint(const ITensor *src0, const ITensor *src1, ITensor *dst, const Window &window, float scale) |
| 352 | { |
| 353 | const auto in0_info = src0->info(); |
| 354 | const auto in1_info = src1->info(); |
| 355 | |
| 356 | const auto &in0_shape = in0_info->tensor_shape(); |
| 357 | const auto &in1_shape = in1_info->tensor_shape(); |
| 358 | |
| 359 | // Create input windows. |
| 360 | Window in0_win = window.broadcast_if_dimension_le_one(in0_shape); |
| 361 | Window in1_win = window.broadcast_if_dimension_le_one(in1_shape); |
| 362 | |
| 363 | // Clear the x dimension on the execution window as we process the whole row each iteration. |
| 364 | Window win = window; |
| 365 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 366 | |
| 367 | constexpr int window_step_x = 16; |
| 368 | const auto window_start_x = window.x().start(); |
| 369 | const auto window_end_x = window.x().end(); |
| 370 | const auto is_broadcast_across_x = in0_shape.x() != in1_shape.x(); |
| 371 | |
| 372 | const auto iq0_info = in0_info->quantization_info().uniform(); |
| 373 | const auto iq1_info = in1_info->quantization_info().uniform(); |
| 374 | const auto oq_info = dst->info()->quantization_info().uniform(); |
| 375 | |
| 376 | const auto in0_offset = iq0_info.offset; |
| 377 | const auto in1_offset = iq1_info.offset; |
| 378 | const auto out_offset = oq_info.offset; |
| 379 | const auto multiplier = ((iq0_info.scale * iq1_info.scale) / oq_info.scale) * scale; |
| 380 | |
| 381 | constexpr int32_t two_pwr18i = 262144; |
| 382 | constexpr float two_pwr18f = 262144.f; |
| 383 | |
| 384 | const auto in0_offset_16p0 = static_cast<int16_t>(in0_offset); |
| 385 | const auto in1_offset_16p0 = static_cast<int16_t>(in1_offset); |
| 386 | const auto out_offset_14p18 = static_cast<int32_t>(out_offset * two_pwr18i); |
| 387 | const auto multiplier_14p18 = static_cast<int32_t>(multiplier * two_pwr18f); |
| 388 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 389 | if (is_broadcast_across_x) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 390 | { |
| 391 | // Prefix: a = non-broadcast, b = broadcast. |
| 392 | |
| 393 | const auto is_broadcast_input_1 = in1_win.x().step() == 0; |
| 394 | auto a_win = is_broadcast_input_1 ? in0_win : in1_win; |
| 395 | auto b_win = is_broadcast_input_1 ? in1_win : in0_win; |
| 396 | const auto a_tensor = is_broadcast_input_1 ? src0 : src1; |
| 397 | const auto b_tensor = is_broadcast_input_1 ? src1 : src0; |
| 398 | |
| 399 | const auto a_offset_16p0 = is_broadcast_input_1 ? in0_offset_16p0 : in1_offset_16p0; |
| 400 | const auto b_offset_16p0 = is_broadcast_input_1 ? in1_offset : in0_offset; |
| 401 | #ifndef __aarch64__ |
| 402 | const auto a_offset = is_broadcast_input_1 ? in0_offset : in1_offset; |
| 403 | const auto b_offset = is_broadcast_input_1 ? in1_offset : in0_offset; |
| 404 | #endif //__aarch64__ |
| 405 | const auto a_voffset_16p0 = wrapper::vdup_n(a_offset_16p0, wrapper::traits::vector_64_tag()); |
| 406 | |
| 407 | // Clear the x dimension on the execution window as we process the whole row each iteration. |
| 408 | a_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 409 | |
| 410 | Iterator a_input_it(a_tensor, a_win); |
| 411 | Iterator b_input_it(b_tensor, b_win); |
| 412 | Iterator out_it(dst, win); |
| 413 | |
| 414 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 415 | win, |
| 416 | [&](const Coordinates &) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 417 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 418 | const auto a_ptr = reinterpret_cast<const ScalarType *>(a_input_it.ptr()); |
| 419 | const auto b_ptr = reinterpret_cast<const ScalarType *>(b_input_it.ptr()); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 420 | const auto out_ptr = reinterpret_cast<ScalarType *>(out_it.ptr()); |
| 421 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 422 | const auto b_val = *b_ptr; |
| 423 | const auto b_offseted_32p0 = static_cast<int32_t>(b_val - b_offset_16p0); |
| 424 | const auto b_voffseted_32p0 = wrapper::vdup_n(b_offseted_32p0, wrapper::traits::vector_128_tag()); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 425 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 426 | const auto vmultiplier_14p18 = wrapper::vdup_n(multiplier_14p18, wrapper::traits::vector_128_tag()); |
| 427 | const auto voffsetout_14p18 = wrapper::vdup_n(out_offset_14p18, wrapper::traits::vector_128_tag()); |
| 428 | |
| 429 | int x = window_start_x; |
| 430 | |
| 431 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 432 | { |
| 433 | // Load the inputs. |
| 434 | const auto a_vin_8p0 = wrapper::vloadq(a_ptr + x); |
| 435 | |
| 436 | // Widen the non-broadcast elements to signed 16-bit regardless of the input signedness. |
| 437 | const auto a_vin_16p0_0 = wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(a_vin_8p0))); |
| 438 | const auto a_vin_16p0_1 = wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(a_vin_8p0))); |
| 439 | |
| 440 | const auto voffseted_32p0_00 = wrapper::vsubl(wrapper::vgetlow(a_vin_16p0_0), a_voffset_16p0); |
| 441 | const auto voffseted_32p0_01 = wrapper::vsubl(wrapper::vgethigh(a_vin_16p0_0), a_voffset_16p0); |
| 442 | const auto voffseted_32p0_10 = wrapper::vsubl(wrapper::vgetlow(a_vin_16p0_1), a_voffset_16p0); |
| 443 | const auto voffseted_32p0_11 = wrapper::vsubl(wrapper::vgethigh(a_vin_16p0_1), a_voffset_16p0); |
| 444 | |
| 445 | const auto vinnermul_32p0_00 = wrapper::vmul(voffseted_32p0_00, b_voffseted_32p0); |
| 446 | const auto vinnermul_32p0_01 = wrapper::vmul(voffseted_32p0_01, b_voffseted_32p0); |
| 447 | const auto vinnermul_32p0_10 = wrapper::vmul(voffseted_32p0_10, b_voffseted_32p0); |
| 448 | const auto vinnermul_32p0_11 = wrapper::vmul(voffseted_32p0_11, b_voffseted_32p0); |
| 449 | |
| 450 | const auto vout_14p18_00 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_00, vmultiplier_14p18); |
| 451 | const auto vout_14p18_01 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_01, vmultiplier_14p18); |
| 452 | const auto vout_14p18_10 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_10, vmultiplier_14p18); |
| 453 | const auto vout_14p18_11 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_11, vmultiplier_14p18); |
| 454 | |
| 455 | // These shift rights are to revert the multiplication by twopwr18. Hard limit of a maximum shift by 8 requires multiple shift instructions to achieve this. |
| 456 | const auto vout_15p1_00 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_00)); |
| 457 | const auto vout_15p1_01 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_01)); |
| 458 | const auto vout_15p1_10 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_10)); |
| 459 | const auto vout_15p1_11 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_11)); |
| 460 | |
| 461 | const auto vout_15p1_0 = wrapper::vcombine(vout_15p1_00, vout_15p1_01); |
| 462 | |
| 463 | const auto vout_15p1_1 = wrapper::vcombine(vout_15p1_10, vout_15p1_11); |
| 464 | const auto out_ptr = reinterpret_cast<ScalarType *>(out_it.ptr()); |
| 465 | |
| 466 | const auto vout_8p0 = |
| 467 | wrapper::vcombine(wrapper::vqrshrn<2>(vout_15p1_0), wrapper::vqrshrn<2>(vout_15p1_1)); |
| 468 | wrapper::vstore(out_ptr + x, vout_8p0); |
| 469 | } |
| 470 | |
| 471 | //Process the left-over elements. |
| 472 | for (; x < window_end_x; ++x) |
| 473 | { |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 474 | #ifdef __aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 475 | out_ptr[x] = wrapper::vqrshrn<2>(wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>( |
| 476 | (multiplier_14p18 * (int32_t(a_ptr[x]) - a_offset_16p0) * (int32_t(b_val) - b_offset_16p0)) + |
| 477 | out_offset_14p18))); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 478 | #else //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 479 | out_ptr[x] = utility::clamp<int32_t, ScalarType>(support::cpp11::lround( |
| 480 | multiplier * ((float(a_ptr[x]) - a_offset) * (float(b_val) - b_offset)) + float(out_offset))); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 481 | #endif //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 482 | } |
| 483 | }, |
| 484 | a_input_it, b_input_it, out_it); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 485 | } |
| 486 | else |
| 487 | { |
| 488 | const auto voffset0_16p0 = wrapper::vdup_n(in0_offset_16p0, wrapper::traits::vector_64_tag()); |
| 489 | const auto voffset1_16p0 = wrapper::vdup_n(in1_offset_16p0, wrapper::traits::vector_64_tag()); |
| 490 | const auto voffsetout_14p18 = wrapper::vdup_n(out_offset_14p18, wrapper::traits::vector_128_tag()); |
| 491 | const auto vmultiplier_14p18 = wrapper::vdup_n(multiplier_14p18, wrapper::traits::vector_128_tag()); |
| 492 | |
| 493 | // Clear the x dimension on the execution window as we process the whole row each iteration. |
| 494 | in0_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 495 | in1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 496 | |
| 497 | Iterator in0_it(src0, in0_win); |
| 498 | Iterator in1_it(src1, in1_win); |
| 499 | Iterator out_it(dst, win); |
| 500 | |
| 501 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 502 | win, |
| 503 | [&](const Coordinates &) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 504 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 505 | const auto in0_ptr = reinterpret_cast<const ScalarType *>(in0_it.ptr()); |
| 506 | const auto in1_ptr = reinterpret_cast<const ScalarType *>(in1_it.ptr()); |
| 507 | const auto out_ptr = reinterpret_cast<ScalarType *>(out_it.ptr()); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 508 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 509 | int x = window_start_x; |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 510 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 511 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 512 | { |
| 513 | // Load the inputs. |
| 514 | const auto vin0_8p0 = wrapper::vloadq(in0_ptr + x); |
| 515 | const auto vin1_8p0 = wrapper::vloadq(in1_ptr + x); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 516 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 517 | // Widen the input elements to signed 16-bit regardless of the input signedness. |
| 518 | const auto vin0_16p0_0 = wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(vin0_8p0))); |
| 519 | const auto vin0_16p0_1 = wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(vin0_8p0))); |
| 520 | const auto vin1_16p0_0 = wrapper::vreinterpret(wrapper::vmovl(wrapper::vgetlow(vin1_8p0))); |
| 521 | const auto vin1_16p0_1 = wrapper::vreinterpret(wrapper::vmovl(wrapper::vgethigh(vin1_8p0))); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 522 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 523 | const auto voffseted0_32p0_00 = wrapper::vsubl(wrapper::vgetlow(vin0_16p0_0), voffset0_16p0); |
| 524 | const auto voffseted0_32p0_01 = wrapper::vsubl(wrapper::vgethigh(vin0_16p0_0), voffset0_16p0); |
| 525 | const auto voffseted0_32p0_10 = wrapper::vsubl(wrapper::vgetlow(vin0_16p0_1), voffset0_16p0); |
| 526 | const auto voffseted0_32p0_11 = wrapper::vsubl(wrapper::vgethigh(vin0_16p0_1), voffset0_16p0); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 527 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 528 | const auto voffseted1_32p0_00 = wrapper::vsubl(wrapper::vgetlow(vin1_16p0_0), voffset1_16p0); |
| 529 | const auto voffseted1_32p0_01 = wrapper::vsubl(wrapper::vgethigh(vin1_16p0_0), voffset1_16p0); |
| 530 | const auto voffseted1_32p0_10 = wrapper::vsubl(wrapper::vgetlow(vin1_16p0_1), voffset1_16p0); |
| 531 | const auto voffseted1_32p0_11 = wrapper::vsubl(wrapper::vgethigh(vin1_16p0_1), voffset1_16p0); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 532 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 533 | const auto vinnermul_32p0_00 = wrapper::vmul(voffseted0_32p0_00, voffseted1_32p0_00); |
| 534 | const auto vinnermul_32p0_01 = wrapper::vmul(voffseted0_32p0_01, voffseted1_32p0_01); |
| 535 | const auto vinnermul_32p0_10 = wrapper::vmul(voffseted0_32p0_10, voffseted1_32p0_10); |
| 536 | const auto vinnermul_32p0_11 = wrapper::vmul(voffseted0_32p0_11, voffseted1_32p0_11); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 537 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 538 | const auto vout_14p18_00 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_00, vmultiplier_14p18); |
| 539 | const auto vout_14p18_01 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_01, vmultiplier_14p18); |
| 540 | const auto vout_14p18_10 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_10, vmultiplier_14p18); |
| 541 | const auto vout_14p18_11 = wrapper::vmla(voffsetout_14p18, vinnermul_32p0_11, vmultiplier_14p18); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 542 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 543 | // These shift rights are to revert the multiplication by twopwr18. Hard limit of a maximum shift by 8 requires multiple shift instructions to achieve this. |
| 544 | const auto vout_14p2_00 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_00)); |
| 545 | const auto vout_14p2_01 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_01)); |
| 546 | const auto vout_14p2_10 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_10)); |
| 547 | const auto vout_14p2_11 = wrapper::vqrshrn_ex<8, ScalarType>(wrapper::vshrq_n<8>(vout_14p18_11)); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 548 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 549 | const auto vout_14p2_0 = wrapper::vcombine(vout_14p2_00, vout_14p2_01); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 550 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 551 | const auto vout_14p2_1 = wrapper::vcombine(vout_14p2_10, vout_14p2_11); |
| 552 | |
| 553 | const auto vout_8p0 = |
| 554 | wrapper::vcombine(wrapper::vqrshrn<2>(vout_14p2_0), wrapper::vqrshrn<2>(vout_14p2_1)); |
| 555 | wrapper::vstore(out_ptr + x, vout_8p0); |
| 556 | } |
| 557 | |
| 558 | //Process the left-over elements. |
| 559 | for (; x < window_end_x; ++x) |
| 560 | { |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 561 | #ifdef __aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 562 | out_ptr[x] = wrapper::vqrshrn<2>(wrapper::vqrshrn_ex<8, ScalarType>( |
| 563 | wrapper::vshrq_n<8>((multiplier_14p18 * (int32_t(in0_ptr[x]) - in0_offset_16p0) * |
| 564 | (int32_t(in1_ptr[x]) - in1_offset_16p0)) + |
| 565 | out_offset_14p18))); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 566 | #else //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 567 | out_ptr[x] = utility::clamp<int32_t, ScalarType>(support::cpp11::lround( |
| 568 | multiplier * ((float(in0_ptr[x]) - in0_offset) * (float(in1_ptr[x]) - in1_offset)) + |
| 569 | float(out_offset))); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 570 | #endif //__aarch64__ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 571 | } |
| 572 | }, |
| 573 | in0_it, in1_it, out_it); |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 574 | } |
| 575 | } |
| 576 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 577 | void mul_saturate_QSYMM16_QSYMM16_QSYMM16( |
| 578 | const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, float scale) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 579 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 580 | const UniformQuantizationInfo input1_qua_info = src1->info()->quantization_info().uniform(); |
| 581 | const UniformQuantizationInfo input2_qua_info = src2->info()->quantization_info().uniform(); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 582 | const UniformQuantizationInfo output_qua_info = out->info()->quantization_info().uniform(); |
| 583 | |
| 584 | // Create input windows |
| 585 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 586 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 587 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 588 | |
| 589 | // Clear X Dimension on execution window as we handle manually |
| 590 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 591 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 592 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 593 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 594 | Iterator input1(src1, input1_win); |
| 595 | Iterator input2(src2, input2_win); |
| 596 | Iterator dst(out, win); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 597 | |
| 598 | const int window_step_x = 16; |
| 599 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 600 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 601 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 602 | const UniformQuantizationInfo tmp_qua_info = {output_qua_info.scale / scale, output_qua_info.offset}; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 603 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 604 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 605 | win, |
| 606 | [&](const Coordinates &) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 607 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 608 | const auto input1_ptr = reinterpret_cast<const qsymm16_t *>(input1.ptr()); |
| 609 | const auto input2_ptr = reinterpret_cast<const qsymm16_t *>(input2.ptr()); |
| 610 | const auto output_ptr = reinterpret_cast<qsymm16_t *>(dst.ptr()); |
| 611 | |
| 612 | // Compute window_step_x elements per iteration |
| 613 | int x = window_start_x; |
| 614 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 615 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 616 | const qsymm16x8x2_t input1_q = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 617 | vld1q_s16(input1_ptr + x), |
| 618 | vld1q_s16(input1_ptr + x + 8), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 619 | }}; |
| 620 | const qsymm16x8x2_t input2_q = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 621 | vld1q_s16(input2_ptr + x), |
| 622 | vld1q_s16(input2_ptr + x + 8), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 623 | }}; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 624 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 625 | // Dequantize inputs |
| 626 | const float32x4x4_t in1_f32x4x4 = vdequantize(input1_q, input1_qua_info); |
| 627 | const float32x4x4_t in2_f32x4x4 = vdequantize(input2_q, input2_qua_info); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 628 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 629 | const float32x4x4_t out_f32x4x4 = { |
| 630 | vmulq_f32(in1_f32x4x4.val[0], in2_f32x4x4.val[0]), |
| 631 | vmulq_f32(in1_f32x4x4.val[1], in2_f32x4x4.val[1]), |
| 632 | vmulq_f32(in1_f32x4x4.val[2], in2_f32x4x4.val[2]), |
| 633 | vmulq_f32(in1_f32x4x4.val[3], in2_f32x4x4.val[3]), |
| 634 | }; |
| 635 | |
| 636 | const qsymm16x8x2_t result = vquantize_qsymm16(out_f32x4x4, tmp_qua_info); |
| 637 | vst1q_s16(output_ptr + x, result.val[0]); |
| 638 | vst1q_s16(output_ptr + x + 8, result.val[1]); |
| 639 | } |
| 640 | |
| 641 | // Compute left-over elements |
| 642 | for (; x < window_end_x; ++x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 643 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 644 | // Dequantize inputs |
| 645 | float tmp_in1 = static_cast<float>(*(input1_ptr + x)) * input1_qua_info.scale; |
| 646 | float tmp_in2 = static_cast<float>(*(input2_ptr + x)) * input2_qua_info.scale; |
| 647 | float tmp_f = tmp_in1 * tmp_in2; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 648 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 649 | // Quantize dst, lrintf() has same rounding mode as vcombine_s16 |
| 650 | int32_t tmp = lrintf(tmp_f / tmp_qua_info.scale); |
| 651 | qsymm16_t tmp_qua = |
| 652 | static_cast<qsymm16_t>(tmp > SHRT_MAX) ? SHRT_MAX : ((tmp < SHRT_MIN) ? SHRT_MIN : tmp); |
| 653 | *(output_ptr + x) = tmp_qua; |
| 654 | } |
| 655 | }, |
| 656 | input1, input2, dst); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 657 | } |
| 658 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 659 | void mul_QSYMM16_QSYMM16_S32(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int scale) |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 660 | { |
| 661 | ARM_COMPUTE_UNUSED(scale); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 662 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 663 | // Create input windows |
| 664 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 665 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 666 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 667 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 668 | // Clear X Dimension on execution window as we handle manually |
| 669 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 670 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 671 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 672 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 673 | Iterator input1(src1, input1_win); |
| 674 | Iterator input2(src2, input2_win); |
| 675 | Iterator dst(out, win); |
Michele Di Giorgio | 9428a18 | 2020-03-30 14:10:20 +0100 | [diff] [blame] | 676 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 677 | const int window_step_x = 16; |
| 678 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 679 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 680 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 681 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 682 | win, |
| 683 | [&](const Coordinates &) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 684 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 685 | const auto input1_ptr = reinterpret_cast<const qsymm16_t *>(input1.ptr()); |
| 686 | const auto input2_ptr = reinterpret_cast<const qsymm16_t *>(input2.ptr()); |
| 687 | const auto output_ptr = reinterpret_cast<int32_t *>(dst.ptr()); |
| 688 | |
| 689 | // Compute window_step_x elements per iteration |
| 690 | int x = window_start_x; |
| 691 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 692 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 693 | const qsymm16x8x2_t input1_q = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 694 | vld1q_s16(input1_ptr + x), |
| 695 | vld1q_s16(input1_ptr + x + 8), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 696 | }}; |
| 697 | const qsymm16x8x2_t input2_q = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 698 | vld1q_s16(input2_ptr + x), |
| 699 | vld1q_s16(input2_ptr + x + 8), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 700 | }}; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 701 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 702 | const int32x4x4_t in1_s32 = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 703 | vmovl_s16(vget_low_s16(input1_q.val[0])), |
| 704 | vmovl_s16(vget_high_s16(input1_q.val[0])), |
| 705 | vmovl_s16(vget_low_s16(input1_q.val[1])), |
| 706 | vmovl_s16(vget_high_s16(input1_q.val[1])), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 707 | }}; |
| 708 | const int32x4x4_t in2_s32 = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 709 | vmovl_s16(vget_low_s16(input2_q.val[0])), |
| 710 | vmovl_s16(vget_high_s16(input2_q.val[0])), |
| 711 | vmovl_s16(vget_low_s16(input2_q.val[1])), |
| 712 | vmovl_s16(vget_high_s16(input2_q.val[1])), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 713 | }}; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 714 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 715 | const int32x4x4_t result = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 716 | vmulq_s32(in1_s32.val[0], in2_s32.val[0]), |
| 717 | vmulq_s32(in1_s32.val[1], in2_s32.val[1]), |
| 718 | vmulq_s32(in1_s32.val[2], in2_s32.val[2]), |
| 719 | vmulq_s32(in1_s32.val[3], in2_s32.val[3]), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 720 | }}; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 721 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 722 | vst1q_s32(output_ptr + x, result.val[0]); |
| 723 | vst1q_s32(output_ptr + x + 4, result.val[1]); |
| 724 | vst1q_s32(output_ptr + x + 8, result.val[2]); |
| 725 | vst1q_s32(output_ptr + x + 12, result.val[3]); |
| 726 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 727 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 728 | // Compute left-over elements |
| 729 | for (; x < window_end_x; ++x) |
| 730 | { |
| 731 | int32_t tmp = static_cast<int32_t>(*(input1_ptr + x)) * static_cast<int32_t>(*(input2_ptr + x)); |
| 732 | *(output_ptr + x) = tmp; |
| 733 | } |
| 734 | }, |
| 735 | input1, input2, dst); |
Manuel Bottini | 7bb56c6 | 2019-06-26 15:17:09 +0100 | [diff] [blame] | 736 | } |
| 737 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 738 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 739 | void mul_U8_U8_U8(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 740 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 741 | // Create input windows |
| 742 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 743 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 744 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 745 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 746 | // Clear X Dimension on execution window as we handle manually |
| 747 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 748 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 749 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 750 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 751 | Iterator input1(src1, input1_win); |
| 752 | Iterator input2(src2, input2_win); |
| 753 | Iterator dst(out, win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 754 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 755 | const int window_step_x = 16 / sizeof(uint8_t); |
| 756 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 757 | const auto window_end_x = static_cast<int>(window.x().end()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 758 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 759 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 760 | win, |
| 761 | [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 762 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 763 | const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr()); |
| 764 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 765 | const auto output_ptr = reinterpret_cast<uint8_t *>(dst.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 766 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 767 | // Compute window_step_x elements per iteration |
| 768 | int x = window_start_x; |
| 769 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 770 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 771 | const uint8x16_t ta1 = wrapper::vloadq(input1_ptr + x); |
| 772 | const uint8x16_t ta2 = wrapper::vloadq(input2_ptr + x); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 773 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 774 | uint16x8_t tmp1_high = vmovl_u8(vget_high_u8(ta1)); |
| 775 | const uint16x8_t tmp2_high = vmovl_u8(vget_high_u8(ta2)); |
| 776 | uint16x8_t tmp1_low = vmovl_u8(vget_low_u8(ta1)); |
| 777 | const uint16x8_t tmp2_low = vmovl_u8(vget_low_u8(ta2)); |
| 778 | |
| 779 | tmp1_high = vmulq_u16(tmp1_high, tmp2_high); |
| 780 | tmp1_low = vmulq_u16(tmp1_low, tmp2_low); |
| 781 | |
| 782 | if (is_scale255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 783 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 784 | tmp1_high = scale255_U16_U16(tmp1_high); |
| 785 | tmp1_low = scale255_U16_U16(tmp1_low); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 786 | } |
| 787 | else |
| 788 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 789 | const int16x8_t vn = vdupq_n_s16(-n); |
| 790 | |
| 791 | if (is_sat) |
| 792 | { |
| 793 | tmp1_high = vqshlq_u16(tmp1_high, vn); |
| 794 | tmp1_low = vqshlq_u16(tmp1_low, vn); |
| 795 | } |
| 796 | else |
| 797 | { |
| 798 | tmp1_high = vshlq_u16(tmp1_high, vn); |
| 799 | tmp1_low = vshlq_u16(tmp1_low, vn); |
| 800 | } |
| 801 | } |
| 802 | if (is_sat) |
| 803 | { |
| 804 | vst1q_u8(output_ptr + x, vcombine_u8(vqmovn_u16(tmp1_low), vqmovn_u16(tmp1_high))); |
| 805 | } |
| 806 | else |
| 807 | { |
| 808 | vst1q_u8(output_ptr + x, vcombine_u8(vmovn_u16(tmp1_low), vmovn_u16(tmp1_high))); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 809 | } |
| 810 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 811 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 812 | // Compute left-over elements |
| 813 | for (; x < window_end_x; ++x) |
| 814 | { |
| 815 | uint16_t tmp = static_cast<uint16_t>(*(input1_ptr + x)) * static_cast<uint16_t>(*(input2_ptr + x)); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 816 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 817 | if (is_scale255) |
| 818 | { |
| 819 | float tmp_f = static_cast<float>(tmp) * scale255_constant; |
| 820 | tmp = static_cast<uint16_t>(tmp_f + 0.5f); |
| 821 | } |
| 822 | else |
| 823 | { |
| 824 | tmp >>= n; |
| 825 | } |
| 826 | if (is_sat && tmp > 255) |
| 827 | { |
| 828 | tmp = 255; |
| 829 | } |
| 830 | *(output_ptr + x) = static_cast<uint8_t>(tmp); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 831 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 832 | }, |
| 833 | input1, input2, dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 837 | inline int16x8_t mul_S16_S16_S16_n_loop(const int16x8_t &src1, const int16x8_t &src2, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 838 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 839 | int32x4_t tmp1_high = vmovl_s16(vget_high_s16(src1)); |
| 840 | const int32x4_t tmp2_high = vmovl_s16(vget_high_s16(src2)); |
| 841 | int32x4_t tmp1_low = vmovl_s16(vget_low_s16(src1)); |
| 842 | const int32x4_t tmp2_low = vmovl_s16(vget_low_s16(src2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 843 | |
| 844 | tmp1_high = vmulq_s32(tmp1_high, tmp2_high); |
| 845 | tmp1_low = vmulq_s32(tmp1_low, tmp2_low); |
| 846 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 847 | if (is_scale255) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 848 | { |
| 849 | tmp1_high = scale255_S32_S32(tmp1_high); |
| 850 | tmp1_low = scale255_S32_S32(tmp1_low); |
| 851 | } |
| 852 | else |
| 853 | { |
| 854 | // Right shift amount |
| 855 | const int32x4_t vn = vdupq_n_s32(-n); |
| 856 | // Left shift amount |
| 857 | const int32x4_t vnl = vdupq_n_s32(n); |
| 858 | // Calculate conversion bit |
| 859 | const uint32x4_t tmp1_high_u = vreinterpretq_u32_s32(tmp1_high); |
| 860 | const uint32x4_t tmp1_low_u = vreinterpretq_u32_s32(tmp1_low); |
| 861 | const uint32x4_t sign_high = vshrq_n_u32(tmp1_high_u, 31); |
| 862 | const uint32x4_t sign_low = vshrq_n_u32(tmp1_low_u, 31); |
| 863 | const int32x4_t sign_high_s = vreinterpretq_s32_u32(sign_high); |
| 864 | const int32x4_t sign_low_s = vreinterpretq_s32_u32(sign_low); |
| 865 | const int32x4_t convert_high = vsubq_s32(vshlq_s32(sign_high_s, vnl), sign_high_s); |
| 866 | const int32x4_t convert_low = vsubq_s32(vshlq_s32(sign_low_s, vnl), sign_low_s); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 867 | if (is_sat) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 868 | { |
| 869 | tmp1_high = vqshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 870 | tmp1_low = vqshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 871 | } |
| 872 | else |
| 873 | { |
| 874 | tmp1_high = vshlq_s32(vaddq_s32(tmp1_high, convert_high), vn); |
| 875 | tmp1_low = vshlq_s32(vaddq_s32(tmp1_low, convert_low), vn); |
| 876 | } |
| 877 | } |
| 878 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 879 | if (is_sat) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 880 | { |
| 881 | return vcombine_s16(vqmovn_s32(tmp1_low), vqmovn_s32(tmp1_high)); |
| 882 | } |
| 883 | else |
| 884 | { |
| 885 | return vcombine_s16(vmovn_s32(tmp1_low), vmovn_s32(tmp1_high)); |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 890 | inline int16x8x2_t mul_S16_S16_S16_n_k(const int16x8x2_t &src1, const int16x8x2_t &src2, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 891 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 892 | const int16x8x2_t result = {{// First 8 elements |
| 893 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(src1.val[0], src2.val[0], n), |
| 894 | // Second 8 elements |
| 895 | mul_S16_S16_S16_n_loop<is_scale255, is_sat>(src1.val[1], src2.val[1], n)}}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 896 | |
| 897 | return result; |
| 898 | } |
| 899 | |
| 900 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 901 | void mul_S16_S16_S16(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 902 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 903 | // Create input windows |
| 904 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 905 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 906 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 907 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 908 | // Clear X Dimension on execution window as we handle manually |
| 909 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 910 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 911 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 912 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 913 | Iterator input1(src1, input1_win); |
| 914 | Iterator input2(src2, input2_win); |
| 915 | Iterator dst(out, win); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 916 | |
| 917 | const int window_step_x = 16; |
| 918 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 919 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 920 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 921 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 922 | win, |
| 923 | [&](const Coordinates &) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 924 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 925 | const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr()); |
| 926 | const auto input2_ptr = reinterpret_cast<const int16_t *>(input2.ptr()); |
| 927 | const auto output_ptr = reinterpret_cast<int16_t *>(dst.ptr()); |
| 928 | |
| 929 | // Compute window_step_x elements per iteration |
| 930 | int x = window_start_x; |
| 931 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 932 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 933 | const int16x8x2_t ta1 = {{ |
| 934 | vld1q_s16(input1_ptr + x), |
| 935 | vld1q_s16(input1_ptr + x + 8), |
| 936 | }}; |
| 937 | const int16x8x2_t ta2 = {{ |
| 938 | vld1q_s16(input2_ptr + x), |
| 939 | vld1q_s16(input2_ptr + x + 8), |
| 940 | }}; |
| 941 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 942 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 943 | vst1q_s16(output_ptr + x, result.val[0]); |
| 944 | vst1q_s16(output_ptr + x + 8, result.val[1]); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 945 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 946 | |
| 947 | // Compute left-over elements |
| 948 | for (; x < window_end_x; ++x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 949 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 950 | int32_t tmp = static_cast<int32_t>(*(input1_ptr + x)) * static_cast<int32_t>(*(input2_ptr + x)); |
| 951 | |
| 952 | if (is_scale255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 953 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 954 | float tmp_f = static_cast<float>(tmp) * scale255_constant; |
| 955 | |
| 956 | tmp = static_cast<int32_t>(tmp_f + 0.5f); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 957 | } |
| 958 | else |
| 959 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 960 | if (tmp >= 0) |
| 961 | { |
| 962 | tmp >>= n; |
| 963 | } |
| 964 | else |
| 965 | { |
| 966 | uint32_t mask = (1u << n) - 1; |
| 967 | tmp = (tmp + static_cast<int32_t>(mask)) >> n; |
| 968 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 969 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 970 | if (is_sat) |
| 971 | { |
| 972 | tmp = (tmp > SHRT_MAX) ? SHRT_MAX : ((tmp < SHRT_MIN) ? SHRT_MIN : tmp); |
| 973 | } |
| 974 | *(output_ptr + x) = static_cast<int16_t>(tmp); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 975 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 976 | }, |
| 977 | input1, input2, dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 978 | } |
| 979 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 980 | template <bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 981 | inline int32x4_t mul_S32_S32_S32_n_loop(const int32x4_t &src1, const int32x4_t &src2, int n) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 982 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 983 | const int32x2_t input1_1 = vget_low_s32(src1); |
| 984 | const int32x2_t input2_1 = vget_low_s32(src2); |
| 985 | const int32x2_t input1_2 = vget_high_s32(src1); |
| 986 | const int32x2_t input2_2 = vget_high_s32(src2); |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 987 | |
| 988 | int64x2_t tmp_1 = vmull_s32(input1_1, input2_1); |
| 989 | int64x2_t tmp_2 = vmull_s32(input1_2, input2_2); |
| 990 | |
| 991 | // Apply scaling, conversion and rounding (round to zero) |
| 992 | // Right shift amount |
| 993 | const int64x2_t vn = vdupq_n_s64(-n); |
| 994 | // Left shift amount |
| 995 | const int64x2_t vnl = vdupq_n_s64(n); |
| 996 | // Calculate conversion bit |
| 997 | const uint64x2_t tmp_1_u = vreinterpretq_u64_s64(tmp_1); |
| 998 | const uint64x2_t sign_1 = vshrq_n_u64(tmp_1_u, 63); |
| 999 | const int64x2_t sign_1_s = vreinterpretq_s64_u64(sign_1); |
| 1000 | const int64x2_t convert_1 = vsubq_s64(vshlq_s64(sign_1_s, vnl), sign_1_s); |
| 1001 | |
| 1002 | const uint64x2_t tmp_2_u = vreinterpretq_u64_s64(tmp_2); |
| 1003 | const uint64x2_t sign_2 = vshrq_n_u64(tmp_2_u, 63); |
| 1004 | const int64x2_t sign_2_s = vreinterpretq_s64_u64(sign_2); |
| 1005 | const int64x2_t convert_2 = vsubq_s64(vshlq_s64(sign_2_s, vnl), sign_2_s); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1006 | if (is_sat) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1007 | { |
| 1008 | tmp_1 = vqshlq_s64(vaddq_s64(tmp_1, convert_1), vn); |
| 1009 | tmp_2 = vqshlq_s64(vaddq_s64(tmp_2, convert_2), vn); |
| 1010 | return vcombine_s32(vqmovn_s64(tmp_1), vqmovn_s64(tmp_2)); |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | tmp_1 = vshlq_s64(vaddq_s64(tmp_1, convert_1), vn); |
| 1015 | tmp_2 = vshlq_s64(vaddq_s64(tmp_2, convert_2), vn); |
| 1016 | return vcombine_s32(vmovn_s64(tmp_1), vmovn_s64(tmp_2)); |
| 1017 | } |
| 1018 | } |
| 1019 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1020 | template <bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1021 | inline int32x4x2_t mul_S32_S32_S32_n_k(const int32x4x2_t &src1, const int32x4x2_t &src2, int n) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1022 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1023 | const int32x4x2_t result = {{// First 4 elements |
| 1024 | mul_S32_S32_S32_n_loop<is_sat>(src1.val[0], src2.val[0], n), |
| 1025 | // Second 4 elements |
| 1026 | mul_S32_S32_S32_n_loop<is_sat>(src1.val[1], src2.val[1], n)}}; |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1027 | |
| 1028 | return result; |
| 1029 | } |
| 1030 | |
| 1031 | template <bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1032 | void mul_S32_S32_S32(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int n) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1033 | { |
| 1034 | // Create input windows |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1035 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 1036 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1037 | |
| 1038 | // Clear X Dimension on execution window as we handle manually |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1039 | Window win = window; |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1040 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1041 | |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1042 | const int window_step_x = 8; |
| 1043 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 1044 | const auto window_end_x = static_cast<int>(window.x().end()); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1045 | const bool is_broadcast_across_x = src1->info()->tensor_shape().x() != src2->info()->tensor_shape().x(); |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1046 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1047 | if (is_broadcast_across_x) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1048 | { |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1049 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 1050 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 1051 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1052 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? src2 : src1; |
| 1053 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src2 : src1; |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1054 | |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1055 | // Clear X Dimension on execution window as we handle manually |
| 1056 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1057 | |
| 1058 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 1059 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1060 | Iterator dst(out, win); |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1061 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1062 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1063 | win, |
| 1064 | [&](const Coordinates &) |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1065 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1066 | const auto non_broadcast_input_ptr = reinterpret_cast<const int32_t *>(non_broadcast_input.ptr()); |
| 1067 | const auto output_ptr = reinterpret_cast<int32_t *>(dst.ptr()); |
| 1068 | |
| 1069 | const int32_t broadcast_value = *reinterpret_cast<const int32_t *>(broadcast_input.ptr()); |
| 1070 | const auto broadcast_value_vec = vdupq_n_s32(broadcast_value); |
| 1071 | |
| 1072 | // Compute window_step_x elements per iteration |
| 1073 | int x = window_start_x; |
| 1074 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1075 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1076 | const int32x4x2_t broadcast_v = {{ |
| 1077 | broadcast_value_vec, |
| 1078 | broadcast_value_vec, |
| 1079 | }}; |
| 1080 | const int32x4x2_t non_broadcast_v = {{ |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1081 | vld1q_s32(non_broadcast_input_ptr + x), |
| 1082 | vld1q_s32(non_broadcast_input_ptr + x + 4), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1083 | }}; |
| 1084 | const int32x4x2_t result = mul_S32_S32_S32_n_k<is_sat>(broadcast_v, non_broadcast_v, n); |
| 1085 | |
| 1086 | vst1q_s32(output_ptr + x, result.val[0]); |
| 1087 | vst1q_s32(output_ptr + x + 4, result.val[1]); |
| 1088 | } |
| 1089 | |
| 1090 | // Compute left-over elements |
| 1091 | for (; x < window_end_x; ++x) |
| 1092 | { |
| 1093 | int64_t tmp = |
| 1094 | static_cast<int64_t>(broadcast_value) * static_cast<int64_t>(*(non_broadcast_input_ptr + x)); |
| 1095 | |
| 1096 | if (tmp >= 0) |
| 1097 | { |
| 1098 | tmp >>= n; |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1099 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1100 | else |
| 1101 | { |
| 1102 | uint64_t mask = ((uint64_t)1u << n) - 1; |
| 1103 | tmp = (tmp + static_cast<int64_t>(mask)) >> n; |
| 1104 | } |
| 1105 | if (is_sat) |
| 1106 | { |
| 1107 | tmp = utility::clamp<int64_t, int32_t>(tmp); |
| 1108 | } |
| 1109 | *(output_ptr + x) = static_cast<int32_t>(tmp); |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1110 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1111 | }, |
| 1112 | broadcast_input, non_broadcast_input, dst); |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1113 | } |
| 1114 | else |
| 1115 | { |
| 1116 | // Clear X Dimension on execution window as we handle manually |
| 1117 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1118 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1119 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1120 | Iterator input1(src1, input1_win); |
| 1121 | Iterator input2(src2, input2_win); |
| 1122 | Iterator dst(out, win); |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1123 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1124 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1125 | win, |
| 1126 | [&](const Coordinates &) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1127 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1128 | const auto input1_ptr = reinterpret_cast<const int32_t *>(input1.ptr()); |
| 1129 | const auto input2_ptr = reinterpret_cast<const int32_t *>(input2.ptr()); |
| 1130 | const auto output_ptr = reinterpret_cast<int32_t *>(dst.ptr()); |
| 1131 | |
| 1132 | // Compute window_step_x elements per iteration |
| 1133 | int x = window_start_x; |
| 1134 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1135 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1136 | const int32x4x2_t ta1 = {{ |
| 1137 | vld1q_s32(input1_ptr + x), |
| 1138 | vld1q_s32(input1_ptr + x + 4), |
| 1139 | }}; |
| 1140 | const int32x4x2_t ta2 = {{ |
| 1141 | vld1q_s32(input2_ptr + x), |
| 1142 | vld1q_s32(input2_ptr + x + 4), |
| 1143 | }}; |
| 1144 | const int32x4x2_t result = mul_S32_S32_S32_n_k<is_sat>(ta1, ta2, n); |
| 1145 | |
| 1146 | vst1q_s32(output_ptr + x, result.val[0]); |
| 1147 | vst1q_s32(output_ptr + x + 4, result.val[1]); |
| 1148 | } |
| 1149 | |
| 1150 | // Compute left-over elements |
| 1151 | for (; x < window_end_x; ++x) |
| 1152 | { |
| 1153 | int64_t tmp = static_cast<int64_t>(*(input1_ptr + x)) * static_cast<int64_t>(*(input2_ptr + x)); |
| 1154 | |
| 1155 | if (tmp >= 0) |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1156 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1157 | tmp >>= n; |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1158 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1159 | else |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1160 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1161 | uint64_t mask = ((uint64_t)1u << n) - 1; |
| 1162 | tmp = (tmp + static_cast<int64_t>(mask)) >> n; |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1163 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1164 | if (is_sat) |
| 1165 | { |
| 1166 | tmp = utility::clamp<int64_t, int32_t>(tmp); |
| 1167 | } |
| 1168 | *(output_ptr + x) = static_cast<int32_t>(tmp); |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1169 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1170 | }, |
| 1171 | input1, input2, dst); |
SiCong Li | d6d1b36 | 2020-09-24 17:34:23 +0100 | [diff] [blame] | 1172 | } |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1173 | } |
| 1174 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1175 | void c_mul_F32_F32_F32_n(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1176 | { |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1177 | // Create input windows |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1178 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 1179 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1180 | |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1181 | // Clear X Dimension on execution window as we handle manually |
| 1182 | Window win = window; |
| 1183 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1184 | |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1185 | constexpr int window_step_x = 8 / sizeof(float); |
| 1186 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 1187 | const auto window_end_x = static_cast<int>(window.x().end()); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1188 | const bool is_broadcast_across_x = src1->info()->tensor_shape().x() != src2->info()->tensor_shape().x(); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1189 | |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1190 | using ExactTagType = typename wrapper::traits::neon_vector<float, 2>::tag_type; |
| 1191 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1192 | if (is_broadcast_across_x) |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1193 | { |
| 1194 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 1195 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 1196 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1197 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? src2 : src1; |
| 1198 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? src2 : src1; |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1199 | |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1200 | // Clear X Dimension on execution window as we handle manually |
| 1201 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1202 | |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1203 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 1204 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1205 | Iterator dst(out, win); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1206 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1207 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1208 | win, |
| 1209 | [&](const Coordinates &) |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1210 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1211 | const auto non_broadcast_input_ptr = reinterpret_cast<const float *>(non_broadcast_input.ptr()); |
| 1212 | const auto output_ptr = reinterpret_cast<float *>(dst.ptr()); |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1213 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1214 | const float broadcast_value = *reinterpret_cast<const float *>(broadcast_input.ptr()); |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1215 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1216 | // Compute window_step_x elements per iteration |
| 1217 | int x = window_start_x; |
| 1218 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 1219 | { |
| 1220 | const auto a = wrapper::vloadq(non_broadcast_input_ptr + 2 * x); |
| 1221 | float32x4_t b = vdupq_n_f32(broadcast_value); |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1222 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1223 | const float32x4_t mask = {-1.0f, 1.0f, -1.0f, 1.0f}; |
| 1224 | const float32x2_t tmp00 = wrapper::vdup_n(wrapper::vgetlane(a, 0), ExactTagType{}); |
| 1225 | const float32x2_t tmp01 = wrapper::vdup_n(wrapper::vgetlane(a, 1), ExactTagType{}); |
| 1226 | const float32x2_t tmp10 = wrapper::vdup_n(wrapper::vgetlane(a, 2), ExactTagType{}); |
| 1227 | const float32x2_t tmp11 = wrapper::vdup_n(wrapper::vgetlane(a, 3), ExactTagType{}); |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1228 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1229 | const float32x4_t tmp0 = wrapper::vcombine(tmp00, tmp10); |
| 1230 | const float32x4_t tmp1 = wrapper::vcombine(tmp01, tmp11); |
Sheri Zhang | 2cb05d9 | 2020-11-09 15:12:32 +0000 | [diff] [blame] | 1231 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1232 | float32x4_t res = wrapper::vmul(tmp0, b); |
| 1233 | b = wrapper::vmul(b, mask); |
| 1234 | |
| 1235 | res = wrapper::vmla(res, tmp1, b); |
| 1236 | wrapper::vstore(output_ptr + 2 * x, res); |
| 1237 | } |
| 1238 | |
| 1239 | // Compute left-over elements |
| 1240 | for (; x < window_end_x; ++x) |
| 1241 | { |
| 1242 | const auto non_broadcast_value0 = *(non_broadcast_input_ptr + 2 * x); |
| 1243 | const auto non_broadcast_value1 = *(non_broadcast_input_ptr + 2 * x + 1); |
| 1244 | auto res1 = broadcast_value * (non_broadcast_value0 - non_broadcast_value1); |
| 1245 | auto res2 = broadcast_value * (non_broadcast_value1 + non_broadcast_value0); |
| 1246 | *(output_ptr + 2 * x) = res1; |
| 1247 | *(output_ptr + 2 * x + 1) = res2; |
| 1248 | } |
| 1249 | }, |
| 1250 | broadcast_input, non_broadcast_input, dst); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1251 | } |
| 1252 | else |
| 1253 | { |
| 1254 | // Clear X Dimension on execution window as we handle manually |
| 1255 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1256 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1257 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1258 | Iterator input1(src1, input1_win); |
| 1259 | Iterator input2(src2, input2_win); |
| 1260 | Iterator dst(out, win); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1261 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1262 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1263 | win, |
| 1264 | [&](const Coordinates &) |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1265 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1266 | const auto input1_ptr = reinterpret_cast<const float *>(input1.ptr()); |
| 1267 | const auto input2_ptr = reinterpret_cast<const float *>(input2.ptr()); |
| 1268 | const auto output_ptr = reinterpret_cast<float *>(dst.ptr()); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1269 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1270 | // Compute window_step_x elements per iteration |
| 1271 | int x = window_start_x; |
| 1272 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 1273 | { |
| 1274 | const float32x4_t a = wrapper::vloadq(input1_ptr + 2 * x); |
| 1275 | float32x4_t b = wrapper::vloadq(input2_ptr + 2 * x); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1276 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1277 | const float32x4_t mask = {-1.0f, 1.0f, -1.0f, 1.0f}; |
| 1278 | const float32x2_t tmp00 = wrapper::vdup_n(wrapper::vgetlane(a, 0), ExactTagType{}); |
| 1279 | const float32x2_t tmp01 = wrapper::vdup_n(wrapper::vgetlane(a, 1), ExactTagType{}); |
| 1280 | const float32x2_t tmp10 = wrapper::vdup_n(wrapper::vgetlane(a, 2), ExactTagType{}); |
| 1281 | const float32x2_t tmp11 = wrapper::vdup_n(wrapper::vgetlane(a, 3), ExactTagType{}); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1282 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1283 | const float32x4_t tmp0 = wrapper::vcombine(tmp00, tmp10); |
| 1284 | const float32x4_t tmp1 = wrapper::vcombine(tmp01, tmp11); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1285 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1286 | float32x4_t res = wrapper::vmul(tmp0, b); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1287 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1288 | b = wrapper::vrev64(b); |
| 1289 | b = wrapper::vmul(b, mask); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1290 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1291 | res = wrapper::vmla(res, tmp1, b); |
| 1292 | wrapper::vstore(output_ptr + 2 * x, res); |
| 1293 | } |
| 1294 | |
| 1295 | // Compute left-over elements |
| 1296 | for (; x < window_end_x; ++x) |
| 1297 | { |
| 1298 | const auto a0 = *(input1_ptr + 2 * x); |
| 1299 | const auto a1 = *(input1_ptr + 2 * x + 1); |
| 1300 | const auto b0 = *(input2_ptr + 2 * x); |
| 1301 | const auto b1 = *(input2_ptr + 2 * x + 1); |
| 1302 | auto res1 = a0 * b0 - a1 * b1; |
| 1303 | auto res2 = a0 * b1 + a1 * b0; |
| 1304 | *(output_ptr + 2 * x) = res1; |
| 1305 | *(output_ptr + 2 * x + 1) = res2; |
| 1306 | } |
| 1307 | }, |
| 1308 | input1, input2, dst); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1309 | } |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
Pablo Tello | df24618 | 2017-07-03 16:25:09 +0100 | [diff] [blame] | 1312 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1313 | void mul_U8_U8_S16(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1314 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1315 | // Create input windows |
| 1316 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1317 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 1318 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1319 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1320 | // Clear X Dimension on execution window as we handle manually |
| 1321 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1322 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1323 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1324 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1325 | Iterator input1(src1, input1_win); |
| 1326 | Iterator input2(src2, input2_win); |
| 1327 | Iterator dst(out, win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1328 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1329 | const int window_step_x = 16 / sizeof(uint8_t); |
| 1330 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 1331 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 1332 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1333 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1334 | win, |
| 1335 | [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1336 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1337 | const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr()); |
| 1338 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 1339 | const auto output_ptr = reinterpret_cast<int16_t *>(dst.ptr()); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1340 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1341 | // Compute window_step_x elements per iteration |
| 1342 | int x = window_start_x; |
| 1343 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1344 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1345 | const uint8x16_t bv = wrapper::vloadq(input2_ptr + x); |
| 1346 | const uint8x16_t av = wrapper::vloadq(input1_ptr + x); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1347 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1348 | uint16x8_t tmp_low = vmovl_u8(vget_low_u8(av)); |
| 1349 | uint16x8_t tmp_high = vmovl_u8(vget_high_u8(av)); |
| 1350 | tmp_low = vmulq_u16(tmp_low, vmovl_u8(vget_low_u8(bv))); |
| 1351 | tmp_high = vmulq_u16(tmp_high, vmovl_u8(vget_high_u8(bv))); |
| 1352 | |
| 1353 | if (is_scale255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1354 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1355 | tmp_low = scale255_U16_U16(tmp_low); |
| 1356 | tmp_high = scale255_U16_U16(tmp_high); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1357 | } |
| 1358 | else |
| 1359 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1360 | const int16x8_t vn = vdupq_n_s16(-n); |
| 1361 | |
| 1362 | if (is_sat) |
| 1363 | { |
| 1364 | tmp_low = vqshlq_u16(tmp_low, vn); |
| 1365 | tmp_high = vqshlq_u16(tmp_high, vn); |
| 1366 | } |
| 1367 | else |
| 1368 | { |
| 1369 | tmp_low = vshlq_u16(tmp_low, vn); |
| 1370 | tmp_high = vshlq_u16(tmp_high, vn); |
| 1371 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1372 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1373 | |
| 1374 | if (is_sat) |
| 1375 | { |
| 1376 | static const uint16x8_t max = vdupq_n_u16(SHRT_MAX); |
| 1377 | |
| 1378 | tmp_low = vminq_u16(tmp_low, max); |
| 1379 | tmp_high = vminq_u16(tmp_high, max); |
| 1380 | } |
| 1381 | |
| 1382 | vst1q_s16(output_ptr + x, vreinterpretq_s16_u16(tmp_low)); |
| 1383 | vst1q_s16(output_ptr + x + 8, vreinterpretq_s16_u16(tmp_high)); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1384 | } |
| 1385 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1386 | // Compute left-over elements |
| 1387 | for (; x < window_end_x; ++x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1388 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1389 | int32_t tmp = static_cast<int32_t>(*(input1_ptr + x)) * static_cast<int32_t>(*(input2_ptr + x)); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1390 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1391 | if (is_scale255) |
| 1392 | { |
| 1393 | float tmp_f = static_cast<float>(tmp) * scale255_constant; |
| 1394 | tmp = static_cast<int32_t>(tmp_f + 0.5f); |
| 1395 | } |
| 1396 | else |
| 1397 | { |
| 1398 | tmp >>= n; |
| 1399 | } |
| 1400 | |
| 1401 | if (is_sat) |
| 1402 | { |
| 1403 | tmp = (tmp > SHRT_MAX) ? SHRT_MAX : tmp; |
| 1404 | } |
| 1405 | |
| 1406 | *(output_ptr + x) = static_cast<int16_t>(tmp); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1407 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1408 | }, |
| 1409 | input1, input2, dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1413 | void mul_S16_U8_S16(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1414 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1415 | // Create input windows |
| 1416 | Window win = window; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1417 | Window input1_win = window.broadcast_if_dimension_le_one(src1->info()->tensor_shape()); |
| 1418 | Window input2_win = window.broadcast_if_dimension_le_one(src2->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1419 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1420 | // Clear X Dimension on execution window as we handle manually |
| 1421 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1422 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 1423 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1424 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1425 | Iterator input1(src1, input1_win); |
| 1426 | Iterator input2(src2, input2_win); |
| 1427 | Iterator dst(out, win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1428 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1429 | const int window_step_x = 16; |
| 1430 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 1431 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 1432 | |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1433 | execute_window_loop( |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1434 | win, |
| 1435 | [&](const Coordinates &) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1436 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1437 | const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr()); |
| 1438 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 1439 | const auto output_ptr = reinterpret_cast<int16_t *>(dst.ptr()); |
| 1440 | |
| 1441 | // Compute window_step_x elements per iteration |
| 1442 | int x = window_start_x; |
| 1443 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1444 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1445 | const int16x8x2_t ta1 = {{ |
| 1446 | vld1q_s16(input1_ptr + x), |
| 1447 | vld1q_s16(input1_ptr + x + 8), |
| 1448 | }}; |
| 1449 | const uint8x8x2_t ta2u = {{ |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1450 | vld1_u8(input2_ptr + x), |
| 1451 | vld1_u8(input2_ptr + x + 8), |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1452 | }}; |
| 1453 | const int16x8x2_t ta2 = { |
| 1454 | {vreinterpretq_s16_u16(vmovl_u8(ta2u.val[0])), vreinterpretq_s16_u16(vmovl_u8(ta2u.val[1]))}}; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1455 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1456 | const int16x8x2_t result = mul_S16_S16_S16_n_k<is_scale255, is_sat>(ta1, ta2, n); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1457 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1458 | vst1q_s16(output_ptr + x, result.val[0]); |
| 1459 | vst1q_s16(output_ptr + x + 8, result.val[1]); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1460 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1461 | |
| 1462 | // Compute left-over elements |
| 1463 | for (; x < window_end_x; ++x) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1464 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1465 | int32_t tmp = static_cast<int32_t>(*(input1_ptr + x)) * static_cast<int32_t>(*(input2_ptr + x)); |
| 1466 | |
| 1467 | if (is_scale255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1468 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1469 | float tmp_f = static_cast<float>(tmp) * scale255_constant; |
| 1470 | |
| 1471 | tmp = static_cast<int32_t>(tmp_f + 0.5f); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1472 | } |
| 1473 | else |
| 1474 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1475 | if (tmp >= 0) |
| 1476 | { |
| 1477 | tmp >>= n; |
| 1478 | } |
| 1479 | else |
| 1480 | { |
| 1481 | uint32_t mask = (1u << n) - 1; |
| 1482 | tmp = (tmp + static_cast<int32_t>(mask)) >> n; |
| 1483 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1484 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1485 | if (is_sat) |
| 1486 | { |
| 1487 | tmp = (tmp > SHRT_MAX) ? SHRT_MAX : ((tmp < SHRT_MIN) ? SHRT_MIN : tmp); |
| 1488 | } |
| 1489 | *(output_ptr + x) = static_cast<int16_t>(tmp); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1490 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1491 | }, |
| 1492 | input1, input2, dst); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | template <bool is_scale255, bool is_sat> |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1496 | void mul_U8_S16_S16(const ITensor *src1, const ITensor *src2, ITensor *out, const Window &window, int n) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1497 | { |
| 1498 | // Simply swap the two input buffers |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1499 | mul_S16_U8_S16<is_scale255, is_sat>(src2, src1, out, window, n); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1500 | } |
| 1501 | } // namespace |
| 1502 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1503 | void CpuMulKernel::configure(ITensorInfo *src1, |
| 1504 | ITensorInfo *src2, |
| 1505 | ITensorInfo *dst, |
| 1506 | float scale, |
| 1507 | ConvertPolicy overflow_policy, |
| 1508 | RoundingPolicy rounding_policy) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1509 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 1510 | ARM_COMPUTE_UNUSED(rounding_policy); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1511 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
Georgios Pinitas | f0dea70 | 2017-07-03 18:17:28 +0100 | [diff] [blame] | 1512 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1513 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1514 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1515 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 1516 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1517 | // Auto initialize dst if not initialized |
| 1518 | set_shape_if_empty(*dst, out_shape); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1519 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1520 | _scale = scale; |
| 1521 | _scale_exponent = 0; |
| 1522 | _func_quantized = nullptr; |
| 1523 | _func_int = nullptr; |
| 1524 | _func_float = nullptr; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1525 | |
| 1526 | bool is_scale_255 = false; |
| 1527 | // Check and validate scaling factor |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1528 | if (std::abs(scale - scale255_constant) < 0.00001f) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1529 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1530 | is_scale_255 = true; |
| 1531 | } |
| 1532 | else |
| 1533 | { |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 1534 | int exponent = 0; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1535 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 1536 | std::frexp(scale, &exponent); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1537 | |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 1538 | // Store the positive exponent. We know that we compute 1/2^n |
| 1539 | // Additionally we need to subtract 1 to compensate that frexp used a mantissa of 0.5 |
| 1540 | _scale_exponent = std::abs(exponent - 1); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1541 | } |
| 1542 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1543 | const DataType dt_input1 = src1->data_type(); |
| 1544 | const DataType dt_input2 = src2->data_type(); |
| 1545 | const DataType dt_output = dst->data_type(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1546 | const bool is_sat = (overflow_policy == ConvertPolicy::SATURATE); |
| 1547 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1548 | switch (dt_input1) |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 1549 | { |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1550 | case DataType::QASYMM8: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1551 | if (dt_input2 == DataType::QASYMM8 && dt_output == DataType::QASYMM8) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1552 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1553 | if (mul_q8_neon_fixedpoint_possible(src1, src2, dst, scale)) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1554 | { |
| 1555 | _func_quantized = &mul_q8_neon_fixedpoint<uint8_t>; |
| 1556 | } |
| 1557 | else |
| 1558 | { |
| 1559 | _func_quantized = &mul_saturate_quantized_8<uint8_t>; |
| 1560 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1561 | } |
| 1562 | break; |
| 1563 | case DataType::QASYMM8_SIGNED: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1564 | if (dt_input2 == DataType::QASYMM8_SIGNED) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1565 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1566 | if (mul_q8_neon_fixedpoint_possible(src1, src2, dst, scale)) |
Omar Al Khatib | 605a928 | 2022-11-01 17:01:24 +0000 | [diff] [blame] | 1567 | { |
| 1568 | _func_quantized = &mul_q8_neon_fixedpoint<int8_t>; |
| 1569 | } |
| 1570 | else |
| 1571 | { |
| 1572 | _func_quantized = &mul_saturate_quantized_8<int8_t>; |
| 1573 | } |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1574 | } |
| 1575 | break; |
| 1576 | case DataType::QSYMM16: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1577 | if (dt_input2 == DataType::QSYMM16 && dt_output == DataType::QSYMM16) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1578 | { |
| 1579 | _func_quantized = &mul_saturate_QSYMM16_QSYMM16_QSYMM16; |
| 1580 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1581 | else if (dt_input2 == DataType::QSYMM16 && dt_output == DataType::S32) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1582 | { |
| 1583 | _func_int = &mul_QSYMM16_QSYMM16_S32; |
| 1584 | } |
| 1585 | break; |
| 1586 | case DataType::S16: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1587 | if (DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1588 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1589 | if (is_scale_255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1590 | { |
| 1591 | _func_int = is_sat ? &mul_S16_U8_S16<true, true> : &mul_S16_U8_S16<true, false>; |
| 1592 | } |
| 1593 | else |
| 1594 | { |
| 1595 | _func_int = is_sat ? &mul_S16_U8_S16<false, true> : &mul_S16_U8_S16<false, false>; |
| 1596 | } |
| 1597 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1598 | if (DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1599 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1600 | if (is_scale_255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1601 | { |
| 1602 | _func_int = is_sat ? &mul_S16_S16_S16<true, true> : &mul_S16_S16_S16<true, false>; |
| 1603 | } |
| 1604 | else |
| 1605 | { |
| 1606 | _func_int = is_sat ? &mul_S16_S16_S16<false, true> : &mul_S16_S16_S16<false, false>; |
| 1607 | } |
| 1608 | } |
| 1609 | break; |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1610 | case DataType::S32: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1611 | if (DataType::S32 == dt_input2 && DataType::S32 == dt_output) |
SiCong Li | bb88f89 | 2020-08-28 11:18:47 +0100 | [diff] [blame] | 1612 | { |
| 1613 | _func_int = is_sat ? &mul_S32_S32_S32<true> : &mul_S32_S32_S32<false>; |
| 1614 | } |
| 1615 | break; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1616 | case DataType::U8: |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1617 | if (DataType::U8 == dt_input2 && DataType::U8 == dt_output) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1618 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1619 | if (is_scale_255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1620 | { |
| 1621 | _func_int = is_sat ? &mul_U8_U8_U8<true, true> : &mul_U8_U8_U8<true, false>; |
| 1622 | } |
| 1623 | else |
| 1624 | { |
| 1625 | _func_int = is_sat ? &mul_U8_U8_U8<false, true> : &mul_U8_U8_U8<false, false>; |
| 1626 | } |
| 1627 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1628 | else if (DataType::U8 == dt_input2 && DataType::S16 == dt_output) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1629 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1630 | if (is_scale_255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1631 | { |
| 1632 | _func_int = is_sat ? &mul_U8_U8_S16<true, true> : &mul_U8_U8_S16<true, false>; |
| 1633 | } |
| 1634 | else |
| 1635 | { |
| 1636 | _func_int = is_sat ? &mul_U8_U8_S16<false, true> : &mul_U8_U8_S16<false, false>; |
| 1637 | } |
| 1638 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1639 | else if (DataType::S16 == dt_input2 && DataType::S16 == dt_output) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1640 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1641 | if (is_scale_255) |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1642 | { |
| 1643 | _func_int = is_sat ? &mul_U8_S16_S16<true, true> : &mul_U8_S16_S16<true, false>; |
| 1644 | } |
| 1645 | else |
| 1646 | { |
| 1647 | _func_int = is_sat ? &mul_U8_S16_S16<false, true> : &mul_U8_S16_S16<false, false>; |
| 1648 | } |
| 1649 | } |
| 1650 | break; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1651 | case DataType::F16: |
Pablo Marquez Tello | 568aab6 | 2023-11-20 14:20:01 +0000 | [diff] [blame] | 1652 | _func_float = REGISTER_FP16_NEON(cpu::mul_F16_F16_F16); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1653 | break; |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1654 | case DataType::F32: |
Pablo Marquez Tello | 568aab6 | 2023-11-20 14:20:01 +0000 | [diff] [blame] | 1655 | _func_float = REGISTER_FP32_NEON(cpu::mul_F32_F32_F32); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1656 | break; |
| 1657 | default: |
| 1658 | ARM_COMPUTE_ERROR("You called with the wrong img formats"); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1659 | } |
| 1660 | |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1661 | // Configure kernel window |
Viet-Hoa Do | 0d05b66 | 2022-09-09 15:39:05 +0100 | [diff] [blame] | 1662 | Window win; |
| 1663 | std::tie(win, _split_dimension) = calculate_squashed_or_max_window(*src1, *src2); |
Sheri Zhang | fcf6f4e | 2020-06-25 20:01:00 +0100 | [diff] [blame] | 1664 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1665 | ICpuKernel::configure(win); |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 1666 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1667 | |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1668 | size_t CpuMulKernel::get_mws(const CPUInfo &platform, size_t thread_count) const |
| 1669 | { |
| 1670 | ARM_COMPUTE_UNUSED(thread_count); |
| 1671 | |
| 1672 | #if defined(ENABLE_FP32_KERNELS) |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1673 | if (this->_func_float == &mul_F32_F32_F32) |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1674 | { |
| 1675 | size_t mws = ICPPKernel::default_mws; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1676 | if (platform.get_cpu_model() == CPUModel::N1) |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1677 | { |
| 1678 | mws = default_mws_N1_fp32_neon; |
| 1679 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1680 | else if (platform.get_cpu_model() == CPUModel::V1) |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1681 | { |
| 1682 | mws = default_mws_V1_fp32_neon; |
| 1683 | } |
| 1684 | else |
| 1685 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1686 | if (_split_dimension == Window::DimX) |
fadara01 | e112ef1 | 2022-11-22 18:25:55 +0000 | [diff] [blame] | 1687 | { |
| 1688 | // Don't split the work load too small if the tensor has been reinterpreted as 1D. |
| 1689 | // This number is loosely chosen as threading overhead in each platform varies wildly. |
| 1690 | return default_mws_other_platforms_1d_tensor; |
| 1691 | } |
| 1692 | return default_mws; |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1693 | } |
| 1694 | |
| 1695 | // tensor is 1D or was re-interpreted as 1D |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1696 | if (this->window().shape().num_dimensions() == 1) |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1697 | { |
| 1698 | return mws; |
| 1699 | } |
| 1700 | else |
| 1701 | { |
| 1702 | // scale mws down by the number of elements along all the dimensions (x, z, w, etc) except the one |
| 1703 | // that we parallelize along (the y dimension). This allows for parallelization when the Y_SIZE is small |
| 1704 | // but the other sizes are large, which boosts performance. |
| 1705 | mws = static_cast<size_t>(mws / (this->window().num_iterations_total() / this->window().num_iterations(1))); |
| 1706 | return std::max(static_cast<size_t>(1), mws); |
| 1707 | } |
| 1708 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1709 | #else /* ENABLE_FP32_KERNELS */ |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1710 | ARM_COMPUTE_UNUSED(platform); |
| 1711 | #endif /* ENABLE_FP32_KERNELS */ |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1712 | if (_split_dimension == Window::DimX) |
fadara01 | e112ef1 | 2022-11-22 18:25:55 +0000 | [diff] [blame] | 1713 | { |
| 1714 | // Don't split the work load too small if the tensor has been reinterpreted as 1D. |
| 1715 | // This number is loosely chosen as threading overhead in each platform varies wildly. |
| 1716 | return default_mws_other_platforms_1d_tensor; |
| 1717 | } |
| 1718 | return default_mws; |
Fadi Arafeh | 73bb6b7 | 2022-10-06 16:20:14 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1721 | Status CpuMulKernel::validate(const ITensorInfo *src1, |
| 1722 | const ITensorInfo *src2, |
| 1723 | const ITensorInfo *dst, |
| 1724 | float scale, |
| 1725 | ConvertPolicy overflow_policy, |
| 1726 | RoundingPolicy rounding_policy) |
Ioan-Cristian Szabo | 754e952 | 2017-11-28 18:29:43 +0000 | [diff] [blame] | 1727 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1728 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 1729 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src1, src2, dst, scale, overflow_policy, rounding_policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1730 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 1731 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1732 | } |
| 1733 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1734 | void CpuMulKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1735 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 1736 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1737 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1738 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1739 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1740 | auto src1 = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 1741 | auto src2 = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 1742 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
Michalis Spyrou | 6eb7345 | 2020-07-02 17:39:25 +0100 | [diff] [blame] | 1743 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1744 | if (_func_quantized != nullptr) |
Michalis Spyrou | 861f0db | 2018-02-26 16:47:58 +0000 | [diff] [blame] | 1745 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1746 | (*_func_quantized)(src1, src2, dst, window, _scale); |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 1747 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1748 | else if (_func_int != nullptr) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1749 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1750 | (*_func_int)(src1, src2, dst, window, _scale_exponent); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1751 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1752 | else |
| 1753 | { |
| 1754 | ARM_COMPUTE_ERROR_ON(_func_float == nullptr); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1755 | (*_func_float)(src1, src2, dst, window, _scale); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1756 | } |
| 1757 | } |
Viet-Hoa Do | d4a9cc0 | 2022-11-08 12:01:21 +0000 | [diff] [blame] | 1758 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1759 | const char *CpuMulKernel::name() const |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1760 | { |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1761 | return "CpuMulKernel"; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1762 | } |
Viet-Hoa Do | d4a9cc0 | 2022-11-08 12:01:21 +0000 | [diff] [blame] | 1763 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1764 | namespace |
| 1765 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1766 | Status validate_arguments_complex(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1767 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1768 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src1, 2, DataType::F32); |
| 1769 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src2, 2, DataType::F32); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1770 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1771 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1772 | |
| 1773 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
| 1774 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1775 | // Validate in case of configured dst |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1776 | if (dst->total_size() > 0) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1777 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1778 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 2, DataType::F32); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 1779 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, dst->tensor_shape(), 0), |
| 1780 | "Wrong shape for dst"); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | return Status{}; |
| 1784 | } |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1785 | } // namespace |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1786 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1787 | void CpuComplexMulKernel::configure(ITensorInfo *src1, ITensorInfo *src2, ITensorInfo *dst) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1788 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1789 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 1790 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_complex(src1, src2, dst)); |
Sheri Zhang | 4d91dc6 | 2020-09-23 11:22:50 +0100 | [diff] [blame] | 1791 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1792 | const TensorShape &out_shape = TensorShape::broadcast_shape(src1->tensor_shape(), src2->tensor_shape()); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1793 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1794 | // Auto initialize dst if not initialized |
| 1795 | const TensorInfo out_info(out_shape, src1->num_channels(), src1->data_type()); |
| 1796 | auto_init_if_empty(*dst, out_info); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1797 | |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1798 | // Configure kernel window |
SiCongLi | c7b1e84 | 2021-02-22 14:28:33 +0000 | [diff] [blame] | 1799 | Window win = calculate_max_window(out_shape); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1800 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1801 | ICpuKernel::configure(win); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1804 | Status CpuComplexMulKernel::validate(const ITensorInfo *src1, const ITensorInfo *src2, const ITensorInfo *dst) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1805 | { |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1806 | ARM_COMPUTE_ERROR_ON_NULLPTR(src1, src2, dst); |
| 1807 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_complex(src1, src2, dst)); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1808 | |
| 1809 | return Status{}; |
| 1810 | } |
| 1811 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1812 | void CpuComplexMulKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1813 | { |
| 1814 | ARM_COMPUTE_UNUSED(info); |
| 1815 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1816 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1817 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1818 | auto src1 = tensors.get_const_tensor(TensorType::ACL_SRC_0); |
| 1819 | auto src2 = tensors.get_const_tensor(TensorType::ACL_SRC_1); |
| 1820 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
Michalis Spyrou | 6eb7345 | 2020-07-02 17:39:25 +0100 | [diff] [blame] | 1821 | |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1822 | c_mul_F32_F32_F32_n(src1, src2, dst, window); |
giuros01 | 154bc1c | 2019-03-26 17:44:40 +0000 | [diff] [blame] | 1823 | } |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1824 | |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1825 | const char *CpuComplexMulKernel::name() const |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1826 | { |
Georgios Pinitas | 0dc0d8e | 2021-04-30 03:18:37 +0100 | [diff] [blame] | 1827 | return "CpuComplexMulKernel"; |
Sheri Zhang | 1e3ab42 | 2021-03-16 17:35:08 +0000 | [diff] [blame] | 1828 | } |
| 1829 | } // namespace kernels |
| 1830 | } // namespace cpu |
Manuel Bottini | 79fa9a2 | 2019-02-22 17:54:22 +0000 | [diff] [blame] | 1831 | } // namespace arm_compute |