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