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