Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEDirectConvolutionLayerOutputStageKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/Traits.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/AccessWindowStatic.h" |
| 34 | #include "src/core/CPP/Validate.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 35 | #include "src/core/NEON/NEAsymm.h" |
| 36 | #include "src/core/NEON/NEFixedPoint.h" |
| 37 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 38 | #include "src/core/helpers/AutoConfiguration.h" |
| 39 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 40 | |
| 41 | #include <arm_neon.h> |
| 42 | #include <cstddef> |
| 43 | #include <cstdint> |
| 44 | |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 45 | namespace arm_compute |
| 46 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 47 | namespace |
| 48 | { |
Michele Di Giorgio | ff27192 | 2019-07-17 15:59:32 +0100 | [diff] [blame] | 49 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 50 | const DirectConvolutionLayerOutputStageKernelInfo &info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 51 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::S32, DataType::F32); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 56 | |
| 57 | if(bias != nullptr) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 58 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON(bias->dimension(0) != input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL))); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 61 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 62 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 63 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 64 | if(input->data_type() == DataType::S32) |
| 65 | { |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(output == nullptr, "In-place computation not allowed for quantized output"); |
| 67 | } |
| 68 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 69 | // Checks performed when output is configured |
| 70 | if((output != nullptr) && (output->total_size() != 0)) |
| 71 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 72 | if(is_data_type_float(input->data_type())) |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 73 | { |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 75 | } |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 76 | else |
| 77 | { |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
| 79 | } |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 81 | } |
| 82 | else if(input->data_type() == DataType::S32) |
| 83 | { |
| 84 | // In case of quantized computation and unconfigured output, the output data type must be provided through DirectConvolutionLayerOutputStageKernelInfo |
| 85 | ARM_COMPUTE_RETURN_ERROR_ON((info.output_data_type != DataType::QASYMM8) && (info.output_data_type != DataType::QASYMM8_SIGNED)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 86 | } |
| 87 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 88 | return Status{}; |
| 89 | } |
| 90 | |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 91 | template <typename T> |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 92 | typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type |
| 93 | output_stage_nchw(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 94 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift, bool has_bias) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | { |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 96 | /** Neon vector tag type. */ |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 97 | using ExactTagType = typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 98 | |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 99 | ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::UNKNOWN); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 100 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 101 | ARM_COMPUTE_UNUSED(result_shift); |
| 102 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 103 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 104 | const int window_start_x = window.x().start(); |
| 105 | const int window_end_x = window.x().end(); |
| 106 | const int window_step_x = 16 / input->info()->element_size(); |
| 107 | Window win = window; |
| 108 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 109 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 110 | Iterator in(input, win); |
| 111 | Iterator out(output, win); |
| 112 | execute_window_loop(win, [&](const Coordinates & id) |
| 113 | { |
| 114 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 115 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 117 | // Get bias and pointer to input |
| 118 | const auto in_ptr = reinterpret_cast<const T *>(in.ptr()) + x; |
| 119 | auto v_in = wrapper::vloadq(in_ptr); |
| 120 | |
| 121 | // Accumulate bias |
| 122 | if(has_bias) |
| 123 | { |
| 124 | const auto vb = wrapper::vdup_n(*reinterpret_cast<const T *>(bias->ptr_to_element(Coordinates(id.z()))), ExactTagType{}); |
| 125 | v_in = wrapper::vadd(v_in, vb); |
| 126 | } |
| 127 | |
| 128 | const auto out_ptr = reinterpret_cast<T *>(out.ptr()) + x; |
| 129 | wrapper::vstore(out_ptr, v_in); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 130 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 131 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 132 | // Left-overs loop |
| 133 | for(; x < window_end_x; ++x) |
| 134 | { |
| 135 | // Get bias and pointer to input |
| 136 | auto s_in = *(reinterpret_cast<const T *>(in.ptr()) + x); |
| 137 | |
| 138 | // Accumulate bias |
| 139 | if(has_bias) |
| 140 | { |
| 141 | const auto b = *reinterpret_cast<const T *>(bias->ptr_to_element(Coordinates(id.z()))); |
| 142 | s_in += b; |
| 143 | } |
| 144 | |
| 145 | *(reinterpret_cast<T *>(out.ptr()) + x) = s_in; |
| 146 | } |
| 147 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 148 | }, |
| 149 | in, out); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 150 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 151 | |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 152 | template <typename T> |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 153 | typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type |
| 154 | output_stage_nhwc(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 155 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift, bool has_bias) |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 156 | { |
| 157 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 158 | ARM_COMPUTE_UNUSED(result_shift); |
| 159 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 160 | |
| 161 | Window window_bias = window; |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 162 | window_bias.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 163 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 164 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 165 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 166 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 167 | const int window_start_x = window.x().start(); |
| 168 | const int window_end_x = window.x().end(); |
| 169 | const int window_step_x = 16 / input->info()->element_size(); |
| 170 | Window win = window; |
| 171 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 172 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 173 | Iterator in(input, win); |
| 174 | Iterator bi(bias, window_bias); |
| 175 | Iterator out(output, win); |
| 176 | |
| 177 | execute_window_loop(win, [&](const Coordinates &) |
| 178 | { |
| 179 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 180 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 181 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 182 | // Get bias and pointer to input |
| 183 | const auto in_ptr = reinterpret_cast<const T *>(in.ptr()); |
| 184 | auto v_in = wrapper::vloadq(in_ptr + x); |
| 185 | |
| 186 | // Accumulate bias |
| 187 | if(has_bias) |
| 188 | { |
| 189 | const auto bias_ptr = reinterpret_cast<T *>(bi.ptr()) + x; |
| 190 | v_in = wrapper::vadd(v_in, wrapper::vloadq(bias_ptr)); |
| 191 | } |
| 192 | |
| 193 | const auto out_ptr = reinterpret_cast<T *>(out.ptr()); |
| 194 | wrapper::vstore(out_ptr + x, v_in); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 195 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 196 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 197 | // Left-overs loop |
| 198 | for(; x < window_end_x; ++x) |
| 199 | { |
| 200 | // Get bias and pointer to input |
| 201 | auto s_in = *(reinterpret_cast<const T *>(in.ptr()) + x); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 202 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 203 | // Accumulate bias |
| 204 | if(has_bias) |
| 205 | { |
| 206 | const auto bias_ptr = reinterpret_cast<T *>(bi.ptr()) + x; |
| 207 | s_in += *bias_ptr; |
| 208 | } |
| 209 | |
| 210 | const auto out_ptr = reinterpret_cast<T *>(out.ptr()); |
| 211 | *(out_ptr + x) = s_in; |
| 212 | } |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 213 | }, |
| 214 | in, bi, out); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 217 | // Quantized case |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 218 | template < typename TOut, typename std::enable_if < std::is_same<TOut, uint8_t>::value || std::is_same<TOut, int8_t>::value, int >::type = 0 > |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 219 | void output_stage_nchw(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 220 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift, bool has_bias) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 221 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 222 | using VectorType = typename wrapper::traits::neon_bitvector_t<TOut, wrapper::traits::BitWidth::W128>; |
| 223 | using TagType = typename wrapper::traits::neon_bitvector_tag_t<TOut, wrapper::traits::BitWidth::W128>; |
| 224 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 225 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 226 | |
| 227 | const VectorType min = wrapper::vdup_n(std::numeric_limits<TOut>::lowest(), TagType{}); |
| 228 | const VectorType max = wrapper::vdup_n(std::numeric_limits<TOut>::max(), TagType{}); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 229 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 230 | const int window_start_x = window.x().start(); |
| 231 | const int window_end_x = window.x().end(); |
| 232 | const int window_step_x = 16 / input->info()->element_size(); |
| 233 | Window win = window; |
| 234 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 235 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 236 | Iterator in(input, win); |
| 237 | Iterator out(output, win); |
| 238 | |
| 239 | execute_window_loop(win, [&](const Coordinates & id) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 240 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 241 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 242 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 243 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 244 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 245 | // Get bias and pointer to input |
| 246 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()) + x; |
| 247 | int32x4x4_t v_in = |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 248 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 249 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 250 | wrapper::vloadq(in_ptr), |
| 251 | wrapper::vloadq(in_ptr + 4), |
| 252 | wrapper::vloadq(in_ptr + 8), |
| 253 | wrapper::vloadq(in_ptr + 12) |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 254 | } |
| 255 | }; |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 256 | |
| 257 | // Accumulate bias |
| 258 | if(has_bias) |
| 259 | { |
| 260 | const auto vb = wrapper::vdup_n(*reinterpret_cast<const int32_t *>(bias->ptr_to_element(Coordinates(id.z()))), TagType{}); |
| 261 | v_in = |
| 262 | { |
| 263 | { |
| 264 | wrapper::vadd(v_in.val[0], vb), |
| 265 | wrapper::vadd(v_in.val[1], vb), |
| 266 | wrapper::vadd(v_in.val[2], vb), |
| 267 | wrapper::vadd(v_in.val[3], vb) |
| 268 | } |
| 269 | }; |
| 270 | } |
| 271 | |
| 272 | const auto out_ptr = reinterpret_cast<TOut *>(out.ptr()) + x; |
| 273 | wrapper::vstore(out_ptr, finalize_quantization(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, |
| 274 | min, max, false)); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 275 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 276 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 277 | // Left-overs loop |
| 278 | for(; x < window_end_x; ++x) |
| 279 | { |
| 280 | // Get bias and pointer to input |
| 281 | int32_t s_in = *(reinterpret_cast<const int32_t *>(in.ptr()) + x); |
| 282 | |
| 283 | // Accumulate bias |
| 284 | if(has_bias) |
| 285 | { |
| 286 | const auto b = *reinterpret_cast<const int32_t *>(bias->ptr_to_element(Coordinates(id.z()))); |
| 287 | s_in += b; |
| 288 | } |
| 289 | |
| 290 | const auto out_ptr = reinterpret_cast<TOut *>(out.ptr()) + x; |
| 291 | *out_ptr = finalize_quantization(s_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, |
| 292 | std::numeric_limits<TOut>::lowest(), std::numeric_limits<TOut>::max(), false); |
| 293 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 294 | }, |
| 295 | in, out); |
| 296 | } |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 297 | template < typename TOut, typename std::enable_if < std::is_same<TOut, uint8_t>::value || std::is_same<TOut, int8_t>::value, int >::type = 0 > |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 298 | void output_stage_nhwc(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 299 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift, bool has_bias) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 300 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 301 | using VectorType = typename wrapper::traits::neon_bitvector_t<TOut, wrapper::traits::BitWidth::W128>; |
| 302 | using TagType = typename wrapper::traits::neon_bitvector_tag_t<TOut, wrapper::traits::BitWidth::W128>; |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 303 | |
| 304 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 305 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 306 | const VectorType min = wrapper::vdup_n(std::numeric_limits<TOut>::lowest(), TagType{}); |
| 307 | const VectorType max = wrapper::vdup_n(std::numeric_limits<TOut>::max(), TagType{}); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 308 | |
| 309 | Window window_bias = window; |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 310 | window_bias.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 311 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 312 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 313 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 314 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 315 | const int window_start_x = window.x().start(); |
| 316 | const int window_end_x = window.x().end(); |
| 317 | const int window_step_x = 16 / input->info()->element_size(); |
| 318 | Window win = window; |
| 319 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 320 | |
| 321 | Iterator in(input, win); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 322 | Iterator bi(bias, window_bias); |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 323 | Iterator out(output, win); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 324 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 325 | execute_window_loop(win, [&](const Coordinates &) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 326 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 327 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 328 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 329 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 330 | // Get bias and pointer to input |
| 331 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()) + x; |
| 332 | int32x4x4_t v_in = |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 333 | { |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 334 | { |
| 335 | wrapper::vloadq(in_ptr), |
| 336 | wrapper::vloadq(in_ptr + 4), |
| 337 | wrapper::vloadq(in_ptr + 8), |
| 338 | wrapper::vloadq(in_ptr + 12), |
| 339 | } |
| 340 | }; |
| 341 | |
| 342 | // Accumulate bias |
| 343 | if(has_bias) |
| 344 | { |
| 345 | const auto bias_ptr = reinterpret_cast<int32_t *>(bi.ptr()) + x; |
| 346 | |
| 347 | wrapper::vadd(v_in.val[0], wrapper::vloadq(bias_ptr)); |
| 348 | wrapper::vadd(v_in.val[1], wrapper::vloadq(bias_ptr + 4)); |
| 349 | wrapper::vadd(v_in.val[2], wrapper::vloadq(bias_ptr + 8)); |
| 350 | wrapper::vadd(v_in.val[3], wrapper::vloadq(bias_ptr + 12)); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 351 | } |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 352 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 353 | const auto out_ptr = reinterpret_cast<TOut *>(out.ptr()) + x; |
| 354 | wrapper::vstore(out_ptr, finalize_quantization(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max, false)); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 357 | // Left-overs loop |
| 358 | for(; x < window_end_x; ++x) |
| 359 | { |
| 360 | // Get bias and pointer to input |
| 361 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()) + x; |
| 362 | int32_t s_in = *in_ptr; |
| 363 | |
| 364 | // Accumulate bias |
| 365 | if(has_bias) |
| 366 | { |
| 367 | const auto bias_ptr = reinterpret_cast<int32_t *>(bi.ptr()) + x; |
| 368 | s_in += *bias_ptr; |
| 369 | } |
| 370 | |
| 371 | const auto out_ptr = reinterpret_cast<TOut *>(out.ptr()) + x; |
| 372 | *out_ptr = finalize_quantization(s_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift, |
| 373 | std::numeric_limits<TOut>::lowest(), std::numeric_limits<TOut>::max(), false); |
| 374 | } |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 375 | }, |
| 376 | in, bi, out); |
| 377 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 378 | } // namespace |
| 379 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 380 | NEDirectConvolutionLayerOutputStageKernel::NEDirectConvolutionLayerOutputStageKernel() |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 381 | : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 382 | { |
| 383 | } |
| 384 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 385 | void NEDirectConvolutionLayerOutputStageKernel::configure(ITensor *input, const ITensor *bias, ITensor *output, |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 386 | const DirectConvolutionLayerOutputStageKernelInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 387 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 388 | // Perform validation step |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 389 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 390 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info(), info)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 391 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 392 | _func = nullptr; |
| 393 | _bias = bias; |
| 394 | _input = input; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 395 | _output = (output != nullptr) ? output : input; |
| 396 | _result_fixedpoint_multiplier = info.result_fixedpoint_multiplier; |
| 397 | _result_shift = info.result_shift; |
| 398 | _result_offset_after_shift = info.result_offset_after_shift; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 399 | |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 400 | // Auto-initialize output output if required |
| 401 | if(output != nullptr && output->info() != nullptr) |
| 402 | { |
| 403 | // Work out expected output data type |
| 404 | const DataType output_dt = (input->info()->data_type() == DataType::S32) ? info.output_data_type : DataType::S32; |
| 405 | // Output tensor auto initialization if not yet initialized |
| 406 | auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_dt)); |
| 407 | } |
| 408 | |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame^] | 409 | Window win = calculate_max_window(*input->info(), Steps()); |
Michalis Spyrou | 017ead2 | 2020-09-28 23:28:08 +0100 | [diff] [blame] | 410 | |
| 411 | INEKernel::configure(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 412 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 413 | const bool is_qasymm8_signed = (output != nullptr) ? is_data_type_quantized_asymmetric_signed(output->info()->data_type()) : false; |
Gian Marco Iodice | 618493d | 2018-11-27 16:38:33 +0000 | [diff] [blame] | 414 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 415 | // Set appropriate function |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 416 | if(input->info()->data_layout() == DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 417 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 418 | switch(input->info()->data_type()) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 419 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 420 | case DataType::S32: |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 421 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 422 | if(is_qasymm8_signed) |
| 423 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 424 | _func = &output_stage_nchw<int8_t>; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 425 | } |
| 426 | else |
| 427 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 428 | _func = &output_stage_nchw<uint8_t>; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 429 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 430 | break; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 431 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 432 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 433 | case DataType::F16: |
| 434 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 435 | _func = &output_stage_nchw<float16_t>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 436 | break; |
| 437 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 438 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 439 | case DataType::F32: |
| 440 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 441 | _func = &output_stage_nchw<float>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 442 | break; |
| 443 | } |
| 444 | default: |
| 445 | { |
| 446 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
| 447 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 448 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 449 | } |
| 450 | else |
| 451 | { |
| 452 | switch(input->info()->data_type()) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 453 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 454 | case DataType::S32: |
| 455 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 456 | if(is_qasymm8_signed) |
| 457 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 458 | _func = &output_stage_nhwc<int8_t>; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 459 | } |
| 460 | else |
| 461 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 462 | _func = &output_stage_nhwc<uint8_t>; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 463 | } |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 464 | break; |
| 465 | } |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 466 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 467 | case DataType::F16: |
| 468 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 469 | _func = &output_stage_nhwc<float16_t>; |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 470 | break; |
| 471 | } |
| 472 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 473 | case DataType::F32: |
| 474 | { |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 475 | _func = &output_stage_nhwc<float>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | default: |
| 479 | { |
| 480 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
| 481 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 482 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
Michele Di Giorgio | ff27192 | 2019-07-17 15:59:32 +0100 | [diff] [blame] | 486 | Status NEDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 487 | const DirectConvolutionLayerOutputStageKernelInfo &info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 488 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 489 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, info)); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 490 | |
| 491 | return Status{}; |
| 492 | } |
| 493 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 494 | void NEDirectConvolutionLayerOutputStageKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 495 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 496 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 497 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 498 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 499 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 500 | |
Michalis Spyrou | 14e868e | 2020-09-30 00:33:05 +0100 | [diff] [blame] | 501 | const bool has_bias = _bias != nullptr; |
| 502 | (*_func)(_input, _bias, window, _output, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, has_bias); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 503 | } |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 504 | } // namespace arm_compute |