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