Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEArithmeticAdditionKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 30 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 31 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include <map> |
| 36 | #include <string> |
| 37 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | namespace |
| 41 | { |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 42 | template <typename T> |
| 43 | void add_same(const ITensor *in1, const ITensor *in2, ITensor *out, const ConvertPolicy policy, const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 45 | /** NEON vector tag type. */ |
| 46 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
| 47 | |
| 48 | // Create input windows |
| 49 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 50 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 51 | |
| 52 | // Clear X Dimension on execution window as we handle manually |
| 53 | Window win = window; |
| 54 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 55 | |
| 56 | constexpr int window_step_x = 16 / sizeof(T); |
| 57 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 58 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 59 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 60 | |
| 61 | if(is_broadcast_across_x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 63 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 64 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 65 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 66 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 67 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 69 | // Clear X Dimension on execution window as we handle manually |
| 70 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 71 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 72 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 73 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 74 | Iterator output(out, win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 76 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 77 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 78 | const auto non_broadcast_input_ptr = reinterpret_cast<const T *>(non_broadcast_input.ptr()); |
| 79 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 80 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 81 | const T broadcast_value = *reinterpret_cast<const T *>(broadcast_input.ptr()); |
| 82 | const auto broadcast_value_vec = wrapper::vdup_n(broadcast_value, ExactTagType{}); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 83 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 84 | // Compute S elements per iteration |
| 85 | int x = window_start_x; |
| 86 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 87 | { |
| 88 | const auto non_broadcast_v = wrapper::vloadq(non_broadcast_input_ptr + x); |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 89 | const auto res = (policy == ConvertPolicy::SATURATE) ? wrapper::vqadd(broadcast_value_vec, non_broadcast_v) : wrapper::vadd(broadcast_value_vec, non_broadcast_v); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 90 | wrapper::vstore(output_ptr + x, res); |
| 91 | } |
| 92 | |
| 93 | // Compute left-over elements |
| 94 | for(; x < window_end_x; ++x) |
| 95 | { |
| 96 | const auto non_broadcast_v = *(non_broadcast_input_ptr + x); |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 97 | *(output_ptr + x) = (policy == ConvertPolicy::SATURATE) ? wrapper::add_sat(broadcast_value, non_broadcast_v) : broadcast_value + non_broadcast_v; |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 98 | } |
| 99 | }, |
| 100 | broadcast_input, non_broadcast_input, output); |
| 101 | } |
| 102 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 103 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 104 | // Clear X Dimension on execution window as we handle manually |
| 105 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 106 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 107 | |
| 108 | Iterator input1(in1, input1_win); |
| 109 | Iterator input2(in2, input2_win); |
| 110 | Iterator output(out, win); |
| 111 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 112 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 113 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 114 | const auto input1_ptr = reinterpret_cast<const T *>(input1.ptr()); |
| 115 | const auto input2_ptr = reinterpret_cast<const T *>(input2.ptr()); |
| 116 | const auto output_ptr = reinterpret_cast<T *>(output.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 117 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 118 | // Compute S elements per iteration |
| 119 | int x = window_start_x; |
| 120 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 121 | { |
| 122 | const auto val1 = wrapper::vloadq(input1_ptr + x); |
| 123 | const auto val2 = wrapper::vloadq(input2_ptr + x); |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 124 | const auto res = (policy == ConvertPolicy::SATURATE) ? wrapper::vqadd(val1, val2) : wrapper::vadd(val1, val2); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 125 | wrapper::vstore(output_ptr + x, res); |
| 126 | } |
| 127 | |
| 128 | // Compute left-over elements |
| 129 | for(; x < window_end_x; ++x) |
| 130 | { |
| 131 | const auto val1 = *(input1_ptr + x); |
| 132 | const auto val2 = *(input2_ptr + x); |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 133 | *(output_ptr + x) = (policy == ConvertPolicy::SATURATE) ? wrapper::add_sat(val1, val2) : val1 + val2; |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 134 | } |
| 135 | }, |
| 136 | input1, input2, output); |
| 137 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 140 | void add_QASYMM8_QASYMM8_QASYMM8(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 141 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 142 | ARM_COMPUTE_UNUSED(policy); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 143 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 144 | // Create input windows |
| 145 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 146 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 147 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 148 | // Clear X Dimension on execution window as we handle manually |
| 149 | Window win = window; |
| 150 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 151 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 152 | const int window_step_x = 16; |
| 153 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 154 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 155 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
Pablo Tello | d1b0ecc | 2017-07-11 11:27:04 +0100 | [diff] [blame] | 156 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 157 | const UniformQuantizationInfo iq1_info = in1->info()->quantization_info().uniform(); |
| 158 | const UniformQuantizationInfo iq2_info = in2->info()->quantization_info().uniform(); |
| 159 | const UniformQuantizationInfo oq_info = out->info()->quantization_info().uniform(); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 160 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 161 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / oq_info.scale); |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 162 | const float32x4_t voffseto = vdupq_n_f32(oq_info.offset); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 163 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 164 | if(is_broadcast_across_x) |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 165 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 166 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 167 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 168 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 169 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 170 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 171 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
| 172 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 173 | |
Michalis Spyrou | 85b7599 | 2020-07-16 11:47:12 +0100 | [diff] [blame] | 174 | const float32x4_t vscale1 = is_broadcast_input_2 ? vdupq_n_f32(iq1_info.scale) : vdupq_n_f32(iq2_info.scale); |
| 175 | const float32x4_t vscale2 = is_broadcast_input_2 ? vdupq_n_f32(iq2_info.scale) : vdupq_n_f32(iq1_info.scale); |
| 176 | const int32x4_t voffset1 = is_broadcast_input_2 ? vdupq_n_s32(iq1_info.offset) : vdupq_n_s32(iq2_info.offset); |
| 177 | const int32x4_t voffset2 = is_broadcast_input_2 ? vdupq_n_s32(iq2_info.offset) : vdupq_n_s32(iq1_info.offset); |
| 178 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 179 | // Clear X Dimension on execution window as we handle manually |
| 180 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 181 | |
| 182 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 183 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 184 | Iterator output(out, win); |
| 185 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 186 | execute_window_loop(win, [&](const Coordinates &) |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 187 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 188 | const auto non_broadcast_input_ptr = reinterpret_cast<const uint8_t *>(non_broadcast_input.ptr()); |
| 189 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 190 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 191 | const uint8_t broadcast_value = *reinterpret_cast<const uint8_t *>(broadcast_input.ptr()); |
| 192 | const uint8x16_t broadcast_value_vec = vdupq_n_u8(broadcast_value); |
| 193 | |
| 194 | const float32x4x4_t bf = |
| 195 | { |
| 196 | { |
| 197 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(broadcast_value_vec))))), voffset2)), vscale2), |
| 198 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(broadcast_value_vec))))), voffset2)), vscale2), |
| 199 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(broadcast_value_vec))))), voffset2)), vscale2), |
| 200 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(broadcast_value_vec))))), voffset2)), vscale2), |
| 201 | } |
| 202 | }; |
| 203 | const float bfs = static_cast<int32_t>(broadcast_value - broadcast_qinfo.offset) * broadcast_qinfo.scale; |
| 204 | |
| 205 | // Compute S elements per iteration |
| 206 | int x = window_start_x; |
| 207 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 208 | { |
| 209 | const uint8x16_t a = vld1q_u8(non_broadcast_input_ptr + x); |
| 210 | const float32x4x4_t af = |
| 211 | { |
| 212 | { |
| 213 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1), |
| 214 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1), |
| 215 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1), |
| 216 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1), |
| 217 | } |
| 218 | }; |
| 219 | |
| 220 | const int32x4x4_t rf = |
| 221 | { |
| 222 | { |
Vidhya Sudhan Loganathan | f8b6520 | 2019-02-01 09:49:50 +0000 | [diff] [blame] | 223 | #ifdef __aarch64__ |
| 224 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 225 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 226 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 227 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 228 | #else //__aarch64__ |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 229 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 230 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 231 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 232 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
Vidhya Sudhan Loganathan | f8b6520 | 2019-02-01 09:49:50 +0000 | [diff] [blame] | 233 | #endif //__aarch64__ |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 234 | } |
| 235 | }; |
| 236 | |
| 237 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 238 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 239 | vst1q_u8(output_ptr + x, vcombine_u8(pa, pb)); |
| 240 | } |
| 241 | |
| 242 | // Compute left-over elements |
| 243 | for(; x < window_end_x; ++x) |
| 244 | { |
| 245 | const float afs = static_cast<int32_t>(*(non_broadcast_input_ptr + x) - non_broadcast_qinfo.offset) * non_broadcast_qinfo.scale; |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 246 | *(output_ptr + x) = quantize_qasymm8((afs + bfs), oq_info); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 247 | } |
| 248 | }, |
| 249 | broadcast_input, non_broadcast_input, output); |
| 250 | } |
| 251 | else |
| 252 | { |
| 253 | // Clear X Dimension on execution window as we handle manually |
| 254 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 255 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 256 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 257 | Iterator input1(in1, input1_win); |
| 258 | Iterator input2(in2, input2_win); |
| 259 | Iterator output(out, win); |
| 260 | |
Michalis Spyrou | 85b7599 | 2020-07-16 11:47:12 +0100 | [diff] [blame] | 261 | const float32x4_t vscale1 = vdupq_n_f32(iq1_info.scale); |
| 262 | const float32x4_t vscale2 = vdupq_n_f32(iq2_info.scale); |
| 263 | const int32x4_t voffset1 = vdupq_n_s32(iq1_info.offset); |
| 264 | const int32x4_t voffset2 = vdupq_n_s32(iq2_info.offset); |
| 265 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 266 | execute_window_loop(win, [&](const Coordinates &) |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 267 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 268 | const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr()); |
| 269 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 270 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 271 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 272 | // Compute S elements per iteration |
| 273 | int x = window_start_x; |
| 274 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 275 | { |
| 276 | const uint8x16_t a = vld1q_u8(input1_ptr + x); |
| 277 | const uint8x16_t b = vld1q_u8(input2_ptr + x); |
| 278 | |
| 279 | const float32x4x4_t af = |
| 280 | { |
| 281 | { |
| 282 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1), |
| 283 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(a))))), voffset1)), vscale1), |
| 284 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1), |
| 285 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(a))))), voffset1)), vscale1), |
| 286 | } |
| 287 | }; |
| 288 | |
| 289 | const float32x4x4_t bf = |
| 290 | { |
| 291 | { |
| 292 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_low_u8(b))))), voffset2)), vscale2), |
| 293 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_low_u8(b))))), voffset2)), vscale2), |
| 294 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_low_u16(vmovl_u8(vget_high_u8(b))))), voffset2)), vscale2), |
| 295 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vreinterpretq_s32_u32(vmovl_u16(vget_high_u16(vmovl_u8(vget_high_u8(b))))), voffset2)), vscale2), |
| 296 | } |
| 297 | }; |
| 298 | |
| 299 | const int32x4x4_t rf = |
| 300 | { |
| 301 | { |
Vidhya Sudhan Loganathan | f8b6520 | 2019-02-01 09:49:50 +0000 | [diff] [blame] | 302 | #ifdef __aarch64__ |
| 303 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 304 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 305 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 306 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 307 | #else //__aarch64__ |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 308 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 309 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 310 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 311 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
Vidhya Sudhan Loganathan | f8b6520 | 2019-02-01 09:49:50 +0000 | [diff] [blame] | 312 | #endif //__aarch64__ |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 313 | } |
| 314 | }; |
| 315 | |
| 316 | const uint8x8_t pa = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 317 | const uint8x8_t pb = vqmovun_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 318 | vst1q_u8(output_ptr + x, vcombine_u8(pa, pb)); |
| 319 | } |
| 320 | |
| 321 | // Compute left-over elements |
| 322 | for(; x < window_end_x; ++x) |
| 323 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 324 | const float afs = static_cast<int32_t>((*(input1_ptr + x)) - iq1_info.offset) * iq1_info.scale; |
| 325 | const float bfs = static_cast<int32_t>((*(input2_ptr + x)) - iq2_info.offset) * iq2_info.scale; |
| 326 | *(output_ptr + x) = quantize_qasymm8((afs + bfs), out->info()->quantization_info()); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 327 | } |
| 328 | }, |
| 329 | input1, input2, output); |
| 330 | } |
| 331 | } |
| 332 | |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 333 | void add_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window) |
| 334 | { |
| 335 | ARM_COMPUTE_UNUSED(policy); |
| 336 | |
| 337 | // Create input windows |
| 338 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 339 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 340 | |
| 341 | // Clear X Dimension on execution window as we handle manually |
| 342 | Window win = window; |
| 343 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 344 | |
| 345 | const int window_step_x = 16; |
| 346 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 347 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 348 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 349 | |
| 350 | const UniformQuantizationInfo iq1_info = in1->info()->quantization_info().uniform(); |
| 351 | const UniformQuantizationInfo iq2_info = in2->info()->quantization_info().uniform(); |
| 352 | const UniformQuantizationInfo oq_info = out->info()->quantization_info().uniform(); |
| 353 | |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 354 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / oq_info.scale); |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 355 | const float32x4_t voffseto = vdupq_n_f32(oq_info.offset); |
| 356 | |
| 357 | if(is_broadcast_across_x) |
| 358 | { |
| 359 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 360 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 361 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 362 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 363 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 364 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
| 365 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
| 366 | |
Michalis Spyrou | 2232a20 | 2020-07-13 15:15:33 +0100 | [diff] [blame] | 367 | const float32x4_t vscale1 = is_broadcast_input_2 ? vdupq_n_f32(iq1_info.scale) : vdupq_n_f32(iq2_info.scale); |
| 368 | const float32x4_t vscale2 = is_broadcast_input_2 ? vdupq_n_f32(iq2_info.scale) : vdupq_n_f32(iq1_info.scale); |
| 369 | const int32x4_t voffset1 = is_broadcast_input_2 ? vdupq_n_s32(iq1_info.offset) : vdupq_n_s32(iq2_info.offset); |
| 370 | const int32x4_t voffset2 = is_broadcast_input_2 ? vdupq_n_s32(iq2_info.offset) : vdupq_n_s32(iq1_info.offset); |
| 371 | |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 372 | // Clear X Dimension on execution window as we handle manually |
| 373 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 374 | |
| 375 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 376 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 377 | Iterator output(out, win); |
| 378 | |
| 379 | execute_window_loop(win, [&](const Coordinates &) |
| 380 | { |
| 381 | const auto non_broadcast_input_ptr = reinterpret_cast<const int8_t *>(non_broadcast_input.ptr()); |
| 382 | const auto output_ptr = reinterpret_cast<int8_t *>(output.ptr()); |
| 383 | |
| 384 | const int8_t broadcast_value = *reinterpret_cast<const int8_t *>(broadcast_input.ptr()); |
| 385 | const int8x16_t broadcast_value_vec = vdupq_n_s8(broadcast_value); |
| 386 | |
| 387 | const float32x4x4_t bf = |
| 388 | { |
| 389 | { |
| 390 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(broadcast_value_vec)))), voffset2)), vscale2), |
| 391 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(broadcast_value_vec)))), voffset2)), vscale2), |
| 392 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(broadcast_value_vec)))), voffset2)), vscale2), |
| 393 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(broadcast_value_vec)))), voffset2)), vscale2), |
| 394 | } |
| 395 | }; |
| 396 | const float bfs = static_cast<int32_t>(broadcast_value - broadcast_qinfo.offset) * broadcast_qinfo.scale; |
| 397 | |
| 398 | // Compute S elements per iteration |
| 399 | int x = window_start_x; |
| 400 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 401 | { |
Georgios Pinitas | d7d7e90 | 2019-12-18 15:40:54 +0000 | [diff] [blame] | 402 | const int8x16_t a = vld1q_s8(non_broadcast_input_ptr + x); |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 403 | const float32x4x4_t af = |
| 404 | { |
| 405 | { |
| 406 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(a)))), voffset1)), vscale1), |
| 407 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(a)))), voffset1)), vscale1), |
| 408 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(a)))), voffset1)), vscale1), |
| 409 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(a)))), voffset1)), vscale1), |
| 410 | } |
| 411 | }; |
| 412 | |
| 413 | const int32x4x4_t rf = |
| 414 | { |
| 415 | { |
| 416 | #ifdef __aarch64__ |
| 417 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 418 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 419 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 420 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
| 421 | #else //__aarch64__ |
| 422 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 423 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 424 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 425 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
| 426 | #endif //__aarch64__ |
| 427 | } |
| 428 | }; |
| 429 | |
| 430 | const int8x8_t pa = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 431 | const int8x8_t pb = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 432 | vst1q_s8(output_ptr + x, vcombine_s8(pa, pb)); |
| 433 | } |
| 434 | |
| 435 | // Compute left-over elements |
| 436 | for(; x < window_end_x; ++x) |
| 437 | { |
| 438 | const float afs = static_cast<int32_t>(*(non_broadcast_input_ptr + x) - non_broadcast_qinfo.offset) * non_broadcast_qinfo.scale; |
| 439 | *(output_ptr + x) = quantize_qasymm8_signed((afs + bfs), oq_info); |
| 440 | } |
| 441 | }, |
| 442 | broadcast_input, non_broadcast_input, output); |
| 443 | } |
| 444 | else |
| 445 | { |
| 446 | // Clear X Dimension on execution window as we handle manually |
| 447 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 448 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 449 | |
| 450 | Iterator input1(in1, input1_win); |
| 451 | Iterator input2(in2, input2_win); |
| 452 | Iterator output(out, win); |
| 453 | |
Michalis Spyrou | 2232a20 | 2020-07-13 15:15:33 +0100 | [diff] [blame] | 454 | const float32x4_t vscale1 = vdupq_n_f32(iq1_info.scale); |
| 455 | const float32x4_t vscale2 = vdupq_n_f32(iq2_info.scale); |
| 456 | const int32x4_t voffset1 = vdupq_n_s32(iq1_info.offset); |
| 457 | const int32x4_t voffset2 = vdupq_n_s32(iq2_info.offset); |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 458 | execute_window_loop(win, [&](const Coordinates &) |
| 459 | { |
| 460 | const auto input1_ptr = reinterpret_cast<const int8_t *>(input1.ptr()); |
| 461 | const auto input2_ptr = reinterpret_cast<const int8_t *>(input2.ptr()); |
| 462 | const auto output_ptr = reinterpret_cast<int8_t *>(output.ptr()); |
| 463 | |
| 464 | // Compute S elements per iteration |
| 465 | int x = window_start_x; |
| 466 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 467 | { |
| 468 | const int8x16_t a = vld1q_s8(input1_ptr + x); |
| 469 | const int8x16_t b = vld1q_s8(input2_ptr + x); |
| 470 | |
| 471 | const float32x4x4_t af = |
| 472 | { |
| 473 | { |
| 474 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(a)))), voffset1)), vscale1), |
| 475 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(a)))), voffset1)), vscale1), |
| 476 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(a)))), voffset1)), vscale1), |
| 477 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(a)))), voffset1)), vscale1), |
| 478 | } |
| 479 | }; |
| 480 | |
| 481 | const float32x4x4_t bf = |
| 482 | { |
| 483 | { |
| 484 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_low_s8(b)))), voffset2)), vscale2), |
| 485 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_low_s8(b)))), voffset2)), vscale2), |
| 486 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_low_s16(vmovl_s8(vget_high_s8(b)))), voffset2)), vscale2), |
| 487 | vmulq_f32(vcvtq_f32_s32(vsubq_s32(vmovl_s16(vget_high_s16(vmovl_s8(vget_high_s8(b)))), voffset2)), vscale2), |
| 488 | } |
| 489 | }; |
| 490 | |
| 491 | const int32x4x4_t rf = |
| 492 | { |
| 493 | { |
| 494 | #ifdef __aarch64__ |
| 495 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 496 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 497 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 498 | vcvtnq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
| 499 | #else //__aarch64__ |
| 500 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 501 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 502 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[2], bf.val[2]), invvscaleo)), |
| 503 | vcvtq_s32_f32(vmlaq_f32(voffseto, vaddq_f32(af.val[3], bf.val[3]), invvscaleo)), |
| 504 | #endif //__aarch64__ |
| 505 | } |
| 506 | }; |
| 507 | |
| 508 | const int8x8_t pa = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1]))); |
| 509 | const int8x8_t pb = vqmovn_s16(vcombine_s16(vqmovn_s32(rf.val[2]), vqmovn_s32(rf.val[3]))); |
| 510 | vst1q_s8(output_ptr + x, vcombine_s8(pa, pb)); |
| 511 | } |
| 512 | |
| 513 | // Compute left-over elements |
| 514 | for(; x < window_end_x; ++x) |
| 515 | { |
| 516 | const float afs = static_cast<int32_t>((*(input1_ptr + x)) - iq1_info.offset) * iq1_info.scale; |
| 517 | const float bfs = static_cast<int32_t>((*(input2_ptr + x)) - iq2_info.offset) * iq2_info.scale; |
| 518 | *(output_ptr + x) = quantize_qasymm8_signed((afs + bfs), out->info()->quantization_info()); |
| 519 | } |
| 520 | }, |
| 521 | input1, input2, output); |
| 522 | } |
| 523 | } |
| 524 | |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 525 | void add_QSYMM16_QSYMM16_QSYMM16(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window) |
| 526 | { |
| 527 | ARM_COMPUTE_UNUSED(policy); |
| 528 | |
| 529 | // Create input windows |
| 530 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 531 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 532 | |
| 533 | // Clear X Dimension on execution window as we handle manually |
| 534 | Window win = window; |
| 535 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 536 | |
| 537 | const int window_step_x = 8; |
| 538 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 539 | const auto window_end_x = static_cast<int>(window.x().end()); |
Georgios Pinitas | d7341fb | 2020-11-12 15:05:01 +0000 | [diff] [blame] | 540 | const bool is_broadcast_across_x = in1->info()->tensor_shape().x() != in2->info()->tensor_shape().x(); |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 541 | |
| 542 | const UniformQuantizationInfo iq1_info = in1->info()->quantization_info().uniform(); |
| 543 | const UniformQuantizationInfo iq2_info = in2->info()->quantization_info().uniform(); |
| 544 | const UniformQuantizationInfo oq_info = out->info()->quantization_info().uniform(); |
| 545 | |
| 546 | const float32x4_t vscale1 = vdupq_n_f32(iq1_info.scale); |
| 547 | const float32x4_t vscale2 = vdupq_n_f32(iq2_info.scale); |
| 548 | const float32x4_t invvscaleo = vdupq_n_f32(1.f / oq_info.scale); |
| 549 | |
| 550 | if(is_broadcast_across_x) |
| 551 | { |
| 552 | const bool is_broadcast_input_2 = input2_win.x().step() == 0; |
| 553 | Window broadcast_win = is_broadcast_input_2 ? input2_win : input1_win; |
| 554 | Window non_broadcast_win = !is_broadcast_input_2 ? input2_win : input1_win; |
| 555 | const ITensor *broadcast_tensor = is_broadcast_input_2 ? in2 : in1; |
| 556 | const ITensor *non_broadcast_tensor = !is_broadcast_input_2 ? in2 : in1; |
| 557 | const UniformQuantizationInfo broadcast_qinfo = broadcast_tensor->info()->quantization_info().uniform(); |
| 558 | const UniformQuantizationInfo non_broadcast_qinfo = non_broadcast_tensor->info()->quantization_info().uniform(); |
| 559 | |
| 560 | // Clear X Dimension on execution window as we handle manually |
| 561 | non_broadcast_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 562 | |
| 563 | Iterator broadcast_input(broadcast_tensor, broadcast_win); |
| 564 | Iterator non_broadcast_input(non_broadcast_tensor, non_broadcast_win); |
| 565 | Iterator output(out, win); |
| 566 | |
| 567 | execute_window_loop(win, [&](const Coordinates &) |
| 568 | { |
| 569 | const auto non_broadcast_input_ptr = reinterpret_cast<const int16_t *>(non_broadcast_input.ptr()); |
| 570 | const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr()); |
| 571 | |
| 572 | const int16_t broadcast_value = *reinterpret_cast<const int16_t *>(broadcast_input.ptr()); |
| 573 | const int16x8_t broadcast_value_vec = vdupq_n_s16(broadcast_value); |
| 574 | |
| 575 | const float32x4x2_t bf = |
| 576 | { |
| 577 | { |
| 578 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(broadcast_value_vec))), vscale2), |
| 579 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(broadcast_value_vec))), vscale2), |
| 580 | } |
| 581 | }; |
| 582 | const float bfs = static_cast<int32_t>(broadcast_value) * broadcast_qinfo.scale; |
| 583 | |
| 584 | // Compute S elements per iteration |
| 585 | int x = window_start_x; |
| 586 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 587 | { |
| 588 | const int16x8_t a = vld1q_s16(non_broadcast_input_ptr + x); |
| 589 | const float32x4x2_t af = |
| 590 | { |
| 591 | { |
| 592 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(a))), vscale1), |
| 593 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(a))), vscale1), |
| 594 | } |
| 595 | }; |
| 596 | |
| 597 | const int32x4x4_t rf = |
| 598 | { |
| 599 | { |
| 600 | #ifdef __aarch64__ |
| 601 | vcvtnq_s32_f32(vmulq_f32(vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 602 | vcvtnq_s32_f32(vmulq_f32(vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 603 | #else //__aarch64__ |
| 604 | vcvtq_s32_f32(vmulq_f32(vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 605 | vcvtq_s32_f32(vmulq_f32(vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 606 | #endif //__aarch64__ |
| 607 | } |
| 608 | }; |
| 609 | |
| 610 | const int16x8_t pa = vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])); |
| 611 | vst1q_s16(output_ptr + x, pa); |
| 612 | } |
| 613 | |
| 614 | // Compute left-over elements |
| 615 | for(; x < window_end_x; ++x) |
| 616 | { |
| 617 | const float afs = static_cast<int32_t>(*(non_broadcast_input_ptr + x)) * non_broadcast_qinfo.scale; |
| 618 | *(output_ptr + x) = quantize_qsymm16((afs + bfs), oq_info); |
| 619 | } |
| 620 | }, |
| 621 | broadcast_input, non_broadcast_input, output); |
| 622 | } |
| 623 | else |
| 624 | { |
| 625 | // Clear X Dimension on execution window as we handle manually |
| 626 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 627 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 628 | |
| 629 | Iterator input1(in1, input1_win); |
| 630 | Iterator input2(in2, input2_win); |
| 631 | Iterator output(out, win); |
| 632 | |
| 633 | execute_window_loop(win, [&](const Coordinates &) |
| 634 | { |
| 635 | const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr()); |
| 636 | const auto input2_ptr = reinterpret_cast<const int16_t *>(input2.ptr()); |
| 637 | const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr()); |
| 638 | |
| 639 | // Compute S elements per iteration |
| 640 | int x = window_start_x; |
| 641 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 642 | { |
| 643 | const int16x8_t a = vld1q_s16(input1_ptr + x); |
| 644 | const int16x8_t b = vld1q_s16(input2_ptr + x); |
| 645 | |
| 646 | const float32x4x2_t af = |
| 647 | { |
| 648 | { |
| 649 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(a))), vscale1), |
| 650 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(a))), vscale1), |
| 651 | } |
| 652 | }; |
| 653 | |
| 654 | const float32x4x2_t bf = |
| 655 | { |
| 656 | { |
| 657 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_low_s16(b))), vscale2), |
| 658 | vmulq_f32(vcvtq_f32_s32(vmovl_s16(vget_high_s16(b))), vscale2), |
| 659 | } |
| 660 | }; |
| 661 | |
| 662 | const int32x4x2_t rf = |
| 663 | { |
| 664 | { |
| 665 | #ifdef __aarch64__ |
| 666 | vcvtnq_s32_f32(vmulq_f32(vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 667 | vcvtnq_s32_f32(vmulq_f32(vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 668 | #else //__aarch64__ |
| 669 | vcvtq_s32_f32(vmulq_f32(vaddq_f32(af.val[0], bf.val[0]), invvscaleo)), |
| 670 | vcvtq_s32_f32(vmulq_f32(vaddq_f32(af.val[1], bf.val[1]), invvscaleo)), |
| 671 | #endif //__aarch64__ |
| 672 | } |
| 673 | }; |
| 674 | |
| 675 | const int16x8_t pa = vcombine_s16(vqmovn_s32(rf.val[0]), vqmovn_s32(rf.val[1])); |
| 676 | vst1q_s16(output_ptr + x, pa); |
| 677 | } |
| 678 | |
| 679 | // Compute left-over elements |
| 680 | for(; x < window_end_x; ++x) |
| 681 | { |
| 682 | const float afs = static_cast<int32_t>((*(input1_ptr + x))) * iq1_info.scale; |
| 683 | const float bfs = static_cast<int32_t>((*(input2_ptr + x))) * iq2_info.scale; |
| 684 | *(output_ptr + x) = quantize_qsymm16((afs + bfs), out->info()->quantization_info()); |
| 685 | } |
| 686 | }, |
| 687 | input1, input2, output); |
| 688 | } |
| 689 | } |
| 690 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 691 | void add_S16_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window) |
| 692 | { |
| 693 | // Create input windows |
| 694 | Window win = window; |
| 695 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 696 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 697 | |
| 698 | // Clear X Dimension on execution window as we handle manually |
| 699 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 700 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 701 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 702 | |
| 703 | Iterator input1(in1, input1_win); |
| 704 | Iterator input2(in2, input2_win); |
| 705 | Iterator output(out, win); |
| 706 | |
| 707 | const int window_step_x = 8; |
| 708 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 709 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 710 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 711 | execute_window_loop(win, [&](const Coordinates &) |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 712 | { |
| 713 | const auto input1_ptr = reinterpret_cast<const int16_t *>(input1.ptr()); |
| 714 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 715 | const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr()); |
| 716 | |
| 717 | if(policy == ConvertPolicy::WRAP) |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 718 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 719 | // Compute S elements per iteration |
| 720 | int x = window_start_x; |
| 721 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 722 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 723 | const auto vin1 = wrapper::vloadq(input1_ptr + x); |
| 724 | const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x))); |
| 725 | wrapper::vstore(output_ptr + x, wrapper::vadd(vin1, vin2)); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 726 | } |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 727 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 728 | // Compute left-over elements |
| 729 | for(; x < window_end_x; ++x) |
| 730 | { |
| 731 | *(output_ptr + x) = *(input1_ptr + x) + static_cast<int16_t>(*(input2_ptr + x)); |
| 732 | } |
| 733 | } |
| 734 | else |
| 735 | { |
| 736 | // Compute S elements per iteration |
| 737 | int x = window_start_x; |
| 738 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 739 | { |
| 740 | const auto vin1 = wrapper::vloadq(input1_ptr + x); |
| 741 | const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x))); |
| 742 | wrapper::vstore(output_ptr + x, wrapper::vqadd(vin1, vin2)); |
| 743 | } |
| 744 | |
| 745 | // Compute left-over elements |
| 746 | for(; x < window_end_x; ++x) |
| 747 | { |
| 748 | *(output_ptr + x) = wrapper::add_sat(*(input1_ptr + x), static_cast<int16_t>(*(input2_ptr + x))); |
| 749 | } |
| 750 | } |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 751 | }, |
| 752 | input1, input2, output); |
| 753 | } |
| 754 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 755 | inline void add_U8_S16_S16(const ITensor *input1, const ITensor *input2, ITensor *output, ConvertPolicy policy, const Window &window) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 756 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 757 | // Simply swap the two input buffers: |
| 758 | add_S16_U8_S16(input2, input1, output, policy, window); |
| 759 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 760 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 761 | void add_U8_U8_S16(const ITensor *in1, const ITensor *in2, ITensor *out, ConvertPolicy policy, const Window &window) |
| 762 | { |
| 763 | // Create input windows |
| 764 | Window win = window; |
| 765 | Window input1_win = window.broadcast_if_dimension_le_one(in1->info()->tensor_shape()); |
| 766 | Window input2_win = window.broadcast_if_dimension_le_one(in2->info()->tensor_shape()); |
| 767 | |
| 768 | // Clear X Dimension on execution window as we handle manually |
| 769 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 770 | input1_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 771 | input2_win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 772 | |
| 773 | Iterator input1(in1, input1_win); |
| 774 | Iterator input2(in2, input2_win); |
| 775 | Iterator output(out, win); |
| 776 | |
| 777 | const int window_step_x = 8; |
| 778 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 779 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 780 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 781 | execute_window_loop(win, [&](const Coordinates &) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 782 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 783 | const auto input1_ptr = reinterpret_cast<const uint8_t *>(input1.ptr()); |
| 784 | const auto input2_ptr = reinterpret_cast<const uint8_t *>(input2.ptr()); |
| 785 | const auto output_ptr = reinterpret_cast<int16_t *>(output.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 786 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 787 | if(policy == ConvertPolicy::WRAP) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 788 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 789 | // Compute S elements per iteration |
| 790 | int x = window_start_x; |
| 791 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 792 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 793 | const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x))); |
| 794 | const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x))); |
| 795 | wrapper::vstore(output_ptr + x, wrapper::vadd(vin1, vin2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 796 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 797 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 798 | // Compute left-over elements |
| 799 | for(; x < window_end_x; ++x) |
| 800 | { |
| 801 | *(output_ptr + x) = static_cast<int16_t>(*(input1_ptr + x)) + static_cast<int16_t>(*(input2_ptr + x)); |
| 802 | } |
| 803 | } |
| 804 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 805 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 806 | // Compute S elements per iteration |
| 807 | int x = window_start_x; |
| 808 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 809 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 810 | const auto vin1 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input1_ptr + x))); |
| 811 | const auto vin2 = vreinterpretq_s16_u16(wrapper::vmovl(wrapper::vload(input2_ptr + x))); |
| 812 | wrapper::vstore(output_ptr + x, wrapper::vqadd(vin1, vin2)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 813 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 814 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 815 | // Compute left-over elements |
| 816 | for(; x < window_end_x; ++x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 817 | { |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 818 | *(output_ptr + x) = wrapper::add_sat(static_cast<int16_t>(*(input1_ptr + x)), |
| 819 | static_cast<int16_t>(*(input2_ptr + x))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 820 | } |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 821 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 822 | }, |
| 823 | input1, input2, output); |
| 824 | } |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 825 | |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 826 | Status validate_arguments(const ITensorInfo &input1, const ITensorInfo &input2, const ITensorInfo &output, ConvertPolicy policy) |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 827 | { |
| 828 | ARM_COMPUTE_UNUSED(policy); |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 829 | |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 830 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(&input1); |
| 831 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input1, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame] | 832 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 833 | DataType::S32, DataType::F32); |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 834 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(&input2, 1, DataType::U8, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame] | 835 | DataType::S16, DataType::QSYMM16, DataType::F16, |
| 836 | DataType::S32, DataType::F32); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 837 | |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 838 | const TensorShape out_shape = TensorShape::broadcast_shape(input1.tensor_shape(), input2.tensor_shape()); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 839 | |
| 840 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(out_shape.total_size() == 0, "Inputs are not broadcast compatible"); |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 841 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input1.tensor_shape().x() != input2.tensor_shape().x()) && ((input1.data_type() != input2.data_type()) || (input1.data_type() != output.data_type()) |
| 842 | || (input2.data_type() != output.data_type())), |
| 843 | "Broadcasting across width is supported on configurations where all tensors have the same data type"); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 844 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 845 | // Validate in case of configured output |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 846 | if(output.total_size() > 0) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 847 | { |
| 848 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 849 | !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::U8) |
| 850 | && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16) |
| 851 | && !(input1.data_type() == DataType::U8 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16) |
| 852 | && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::U8 && output.data_type() == DataType::S16) |
| 853 | && !(input1.data_type() == DataType::S16 && input2.data_type() == DataType::S16 && output.data_type() == DataType::S16) |
| 854 | && !(input1.data_type() == DataType::S32 && input2.data_type() == DataType::S32 && output.data_type() == DataType::S32) |
| 855 | && !(input1.data_type() == DataType::F32 && input2.data_type() == DataType::F32 && output.data_type() == DataType::F32) |
| 856 | && !(input1.data_type() == DataType::F16 && input2.data_type() == DataType::F16 && output.data_type() == DataType::F16) |
| 857 | && !(input1.data_type() == DataType::QASYMM8 && input2.data_type() == DataType::QASYMM8 && output.data_type() == DataType::QASYMM8) |
| 858 | && !(input1.data_type() == DataType::QASYMM8_SIGNED && input2.data_type() == DataType::QASYMM8_SIGNED && output.data_type() == DataType::QASYMM8_SIGNED) |
| 859 | && !(input1.data_type() == DataType::QSYMM16 && input2.data_type() == DataType::QSYMM16 && output.data_type() == DataType::QSYMM16), |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 860 | "You called addition with the wrong image formats"); |
| 861 | |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 862 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(detail::have_different_dimensions(out_shape, output.tensor_shape(), 0), |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 863 | "Wrong shape for output"); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 864 | } |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 865 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 866 | return Status{}; |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 867 | } |
| 868 | |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 869 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo &input1, const ITensorInfo &input2, ITensorInfo &output) |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 870 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 871 | const std::pair<TensorShape, ValidRegion> broadcast_pair = ITensorInfo::broadcast_shape_and_valid_region(input1, input2); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 872 | const TensorShape &out_shape = broadcast_pair.first; |
| 873 | const ValidRegion &valid_region = broadcast_pair.second; |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 874 | |
Michele Di Giorgio | 4a61653 | 2020-06-04 15:05:38 +0100 | [diff] [blame] | 875 | // Auto initialize output if not initialized |
Michele Di Giorgio | 4a61653 | 2020-06-04 15:05:38 +0100 | [diff] [blame] | 876 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 877 | set_shape_if_empty(output, out_shape); |
Michele Di Giorgio | 4a61653 | 2020-06-04 15:05:38 +0100 | [diff] [blame] | 878 | |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 879 | if(input1.data_type() == DataType::S16 || input2.data_type() == DataType::S16) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 880 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 881 | set_format_if_unknown(output, Format::S16); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 882 | } |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 883 | if(input1.data_type() == DataType::S32 || input2.data_type() == DataType::S32) |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame] | 884 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 885 | set_format_if_unknown(output, Format::S32); |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame] | 886 | } |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 887 | else if(input1.data_type() == DataType::F16 || input2.data_type() == DataType::F16) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 888 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 889 | set_format_if_unknown(output, Format::F16); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 890 | } |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 891 | else if(input1.data_type() == DataType::F32 || input2.data_type() == DataType::F32) |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 892 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 893 | set_format_if_unknown(output, Format::F32); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 894 | } |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 895 | else if(input1.data_type() == DataType::QASYMM8 || input2.data_type() == DataType::QASYMM8) |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 896 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 897 | set_data_type_if_unknown(output, DataType::QASYMM8); |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 898 | } |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 899 | else if(input1.data_type() == DataType::QASYMM8_SIGNED || input2.data_type() == DataType::QASYMM8_SIGNED) |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 900 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 901 | set_data_type_if_unknown(output, DataType::QASYMM8_SIGNED); |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 902 | } |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 903 | else if(input1.data_type() == DataType::QSYMM16 || input2.data_type() == DataType::QSYMM16) |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 904 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 905 | set_data_type_if_unknown(output, DataType::QSYMM16); |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 906 | } |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 907 | } |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 908 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 909 | Window win = calculate_max_window(valid_region, Steps()); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 910 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 911 | // NEArithmeticAdditionKernel doesn't need padding so update_window_and_padding() can be skipped |
| 912 | Coordinates coord; |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 913 | coord.set_num_dimensions(output.num_dimensions()); |
| 914 | output.set_valid_region(valid_region); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 915 | return std::make_pair(Status{}, win); |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 916 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 917 | } // namespace |
| 918 | |
| 919 | NEArithmeticAdditionKernel::NEArithmeticAdditionKernel() |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 920 | : _func(nullptr), _policy() |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 921 | { |
| 922 | } |
| 923 | |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 924 | void NEArithmeticAdditionKernel::configure(const ITensorInfo *input1, const ITensorInfo *input2, ITensorInfo *output, ConvertPolicy policy) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 925 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 926 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 927 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(*input1, *input2, *output, policy)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 928 | |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 929 | // Configure kernel window |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 930 | auto win_config = validate_and_configure_window(*input1, *input2, *output); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 931 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 932 | |
| 933 | static std::map<std::string, AddFunction *> map_function = |
| 934 | { |
Georgios Pinitas | a84faff | 2018-12-05 18:17:24 +0000 | [diff] [blame] | 935 | { "add_wrap_QASYMM8_QASYMM8_QASYMM8", &add_QASYMM8_QASYMM8_QASYMM8 }, |
| 936 | { "add_saturate_QASYMM8_QASYMM8_QASYMM8", &add_QASYMM8_QASYMM8_QASYMM8 }, |
Michalis Spyrou | bc4d7c2 | 2019-12-03 15:11:09 +0000 | [diff] [blame] | 937 | { "add_wrap_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED", &add_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED }, |
| 938 | { "add_saturate_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED", &add_QASYMM8_SIGNED_QASYMM8_SIGNED_QASYMM8_SIGNED }, |
Manuel Bottini | 3689fcd | 2019-06-14 17:18:12 +0100 | [diff] [blame] | 939 | { "add_wrap_QSYMM16_QSYMM16_QSYMM16", &add_QSYMM16_QSYMM16_QSYMM16 }, |
| 940 | { "add_saturate_QSYMM16_QSYMM16_QSYMM16", &add_QSYMM16_QSYMM16_QSYMM16 }, |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 941 | { "add_wrap_U8_U8_U8", &add_same<uint8_t> }, |
| 942 | { "add_saturate_U8_U8_U8", &add_same<uint8_t> }, |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 943 | { "add_wrap_S16_U8_S16", &add_S16_U8_S16 }, |
| 944 | { "add_saturate_S16_U8_S16", &add_S16_U8_S16 }, |
| 945 | { "add_wrap_U8_S16_S16", &add_U8_S16_S16 }, |
| 946 | { "add_saturate_U8_S16_S16", &add_U8_S16_S16 }, |
| 947 | { "add_wrap_U8_U8_S16", &add_U8_U8_S16 }, |
| 948 | { "add_saturate_U8_U8_S16", &add_U8_U8_S16 }, |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 949 | { "add_wrap_S16_S16_S16", &add_same<int16_t> }, |
| 950 | { "add_saturate_S16_S16_S16", &add_same<int16_t> }, |
Michele Di Giorgio | 11c562c | 2020-06-10 16:34:50 +0100 | [diff] [blame] | 951 | { "add_wrap_S32_S32_S32", &add_same<int32_t> }, |
| 952 | { "add_saturate_S32_S32_S32", &add_same<int32_t> }, |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 953 | { "add_wrap_F32_F32_F32", &add_same<float> }, |
| 954 | { "add_saturate_F32_F32_F32", &add_same<float> }, |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 955 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 956 | { "add_wrap_F16_F16_F16", &add_same<float16_t> }, |
| 957 | { "add_saturate_F16_F16_F16", &add_same<float16_t> }, |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 958 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 959 | }; |
| 960 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 961 | _policy = policy; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 962 | |
| 963 | std::string function_to_call("add_"); |
| 964 | function_to_call += policy == ConvertPolicy::WRAP ? "wrap_" : "saturate_"; |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 965 | function_to_call += string_from_data_type(input1->data_type()) + "_"; |
| 966 | function_to_call += string_from_data_type(input2->data_type()) + "_"; |
| 967 | function_to_call += string_from_data_type(output->data_type()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 968 | |
| 969 | auto it = map_function.find(function_to_call); |
| 970 | |
| 971 | if(it != map_function.end()) |
| 972 | { |
| 973 | _func = it->second; |
| 974 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 975 | |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 976 | INEKernel::configure(win_config.second); |
| 977 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 978 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 979 | Status NEArithmeticAdditionKernel::validate(const ITensorInfo *input1, const ITensorInfo *input2, const ITensorInfo *output, ConvertPolicy policy) |
Ioan-Cristian Szabo | 397d58a | 2017-11-30 15:19:11 +0000 | [diff] [blame] | 980 | { |
Michele Di Giorgio | 1902383 | 2020-06-17 16:08:10 +0000 | [diff] [blame] | 981 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 982 | |
| 983 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(*input1, *input2, *output, policy)); |
| 984 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(*input1->clone(), *input2->clone(), *output->clone()).first); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 985 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 986 | return Status{}; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 987 | } |
| 988 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 989 | void NEArithmeticAdditionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 990 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 991 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 992 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 993 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Michalis Spyrou | 173ba9b | 2020-06-23 17:25:43 +0100 | [diff] [blame] | 994 | // Dispatch kernel |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 995 | (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC_0), |
| 996 | tensors.get_const_tensor(TensorType::ACL_SRC_1), |
| 997 | tensors.get_tensor(TensorType::ACL_DST), |
| 998 | _policy, |
| 999 | window); |
Diego Lopez Recas | 0021d75 | 2017-12-18 14:42:56 +0000 | [diff] [blame] | 1000 | } |
Michalis Spyrou | 7a7fe65 | 2020-05-15 11:28:59 +0100 | [diff] [blame] | 1001 | } // namespace arm_compute |