Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [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" |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/NEAsymm.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Types.h" |
| 35 | #include "arm_compute/core/Validate.h" |
| 36 | #include "arm_compute/core/Window.h" |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/Traits.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 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 89 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output, |
| 90 | const DirectConvolutionLayerOutputStageKernelInfo &info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 91 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 92 | ARM_COMPUTE_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
| 93 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 94 | const DataType data_type = input->data_type(); |
| 95 | |
| 96 | // Auto-initialize output output if required |
| 97 | if(output != nullptr) |
| 98 | { |
| 99 | // Work out expected output data type |
| 100 | const DataType output_dt = (data_type == DataType::S32) ? info.output_data_type : data_type; |
| 101 | // Output tensor auto initialization if not yet initialized |
| 102 | auto_init_if_empty(*output, input->clone()->set_data_type(output_dt)); |
| 103 | } |
| 104 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 105 | bool window_changed = false; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 106 | unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(data_type); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 107 | |
| 108 | // Update processed elements when input is S32 (comes from quantization input) |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 109 | if(data_type == DataType::S32) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 110 | { |
| 111 | num_elems_processed_per_iteration = 16; |
| 112 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 113 | |
| 114 | // Configure kernel window |
| 115 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 116 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 117 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 118 | if(output != nullptr && (output->total_size() != 0)) |
| 119 | { |
| 120 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 121 | |
| 122 | if(bias == nullptr) |
| 123 | { |
| 124 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 129 | window_changed = update_window_and_padding(win, input_access, output_access, bias_access); |
| 130 | } |
| 131 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 132 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 133 | } |
| 134 | else |
| 135 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 136 | if(bias == nullptr) |
| 137 | { |
| 138 | window_changed = update_window_and_padding(win, input_access); |
| 139 | } |
| 140 | else |
| 141 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 142 | if(input->data_layout() == DataLayout::NCHW) |
| 143 | { |
| 144 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 145 | window_changed = update_window_and_padding(win, input_access, bias_access); |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | AccessWindowHorizontal bias_access(bias, 0, num_elems_processed_per_iteration); |
| 150 | window_changed = update_window_and_padding(win, input_access, bias_access); |
| 151 | } |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 152 | } |
| 153 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 154 | input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape())); |
| 155 | } |
| 156 | |
| 157 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 158 | return std::make_pair(err, win); |
| 159 | } |
| 160 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 161 | template <typename T, bool has_bias> |
| 162 | typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type |
| 163 | output_stage_nchw(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 164 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 166 | /** NEON vector tag type. */ |
| 167 | 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] | 168 | |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 169 | ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::UNKNOWN); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 170 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 171 | ARM_COMPUTE_UNUSED(result_shift); |
| 172 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 173 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 174 | Iterator in(input, window); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 175 | Iterator out(output, window); |
| 176 | execute_window_loop(window, [&](const Coordinates & id) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 177 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 178 | // Get bias and pointer to input |
| 179 | const auto in_ptr = reinterpret_cast<const T *>(in.ptr()); |
| 180 | auto v_in = wrapper::vloadq(in_ptr); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 182 | // Accumulate bias |
| 183 | if(has_bias) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 184 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 185 | const auto vb = wrapper::vdup_n(*reinterpret_cast<const T *>(bias->ptr_to_element(Coordinates(id.z()))), ExactTagType{}); |
| 186 | v_in = wrapper::vadd(v_in, vb); |
| 187 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 188 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 189 | const auto out_ptr = reinterpret_cast<T *>(out.ptr()); |
| 190 | wrapper::vstore(out_ptr, v_in); |
| 191 | }, |
| 192 | in, out); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 194 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 195 | template <typename T, bool has_bias> |
| 196 | typename std::enable_if<arm_compute::utils::traits::is_floating_point<T>::value, void>::type |
| 197 | output_stage_nhwc(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 198 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 199 | { |
| 200 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 201 | ARM_COMPUTE_UNUSED(result_shift); |
| 202 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 203 | |
| 204 | Window window_bias = window; |
| 205 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 206 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 207 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 208 | |
| 209 | Iterator in(input, window); |
| 210 | Iterator bi(bias, window_bias); |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 211 | Iterator out(output, window); |
| 212 | execute_window_loop(window, [&](const Coordinates &) |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 213 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 214 | // Get bias and pointer to input |
| 215 | const auto in_ptr = reinterpret_cast<const T *>(in.ptr()); |
| 216 | auto v_in = wrapper::vloadq(in_ptr); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 217 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 218 | // Accumulate bias |
| 219 | if(has_bias) |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 220 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 221 | const auto bias_ptr = reinterpret_cast<T *>(bi.ptr()); |
| 222 | v_in = wrapper::vadd(v_in, wrapper::vloadq(bias_ptr)); |
| 223 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 224 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 225 | const auto out_ptr = reinterpret_cast<T *>(out.ptr()); |
| 226 | wrapper::vstore(out_ptr, v_in); |
| 227 | |
| 228 | }, |
| 229 | in, bi, out); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 232 | // Quantized case |
| 233 | template < typename TOut, bool has_bias, typename std::enable_if < std::is_same<TOut, uint8_t>::value || std::is_same<TOut, int8_t>::value, int >::type = 0 > |
| 234 | void output_stage_nchw(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 235 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 236 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 237 | using VectorType = typename wrapper::traits::neon_bitvector_t<TOut, wrapper::traits::BitWidth::W128>; |
| 238 | using TagType = typename wrapper::traits::neon_bitvector_tag_t<TOut, wrapper::traits::BitWidth::W128>; |
| 239 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 240 | 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] | 241 | |
| 242 | const VectorType min = wrapper::vdup_n(std::numeric_limits<TOut>::lowest(), TagType{}); |
| 243 | const VectorType max = wrapper::vdup_n(std::numeric_limits<TOut>::max(), TagType{}); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 244 | |
| 245 | Iterator in(input, window); |
| 246 | Iterator out(output, window); |
| 247 | |
| 248 | execute_window_loop(window, [&](const Coordinates & id) |
| 249 | { |
| 250 | // Get bias and pointer to input |
| 251 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 252 | int32x4x4_t v_in = |
| 253 | { |
| 254 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 255 | wrapper::vloadq(in_ptr), |
| 256 | wrapper::vloadq(in_ptr + 4), |
| 257 | wrapper::vloadq(in_ptr + 8), |
| 258 | wrapper::vloadq(in_ptr + 12) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 259 | } |
| 260 | }; |
| 261 | |
| 262 | // Accumulate bias |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 263 | if(has_bias) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 264 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 265 | const auto vb = wrapper::vdup_n(*reinterpret_cast<const int32_t *>(bias->ptr_to_element(Coordinates(id.z()))), TagType{}); |
| 266 | v_in = |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 267 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 268 | { |
| 269 | wrapper::vadd(v_in.val[0], vb), |
| 270 | wrapper::vadd(v_in.val[1], vb), |
| 271 | wrapper::vadd(v_in.val[2], vb), |
| 272 | wrapper::vadd(v_in.val[3], vb) |
| 273 | } |
| 274 | }; |
| 275 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 276 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 277 | const auto out_ptr = reinterpret_cast<TOut *>(out.ptr()); |
| 278 | wrapper::vstore(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 279 | }, |
| 280 | in, out); |
| 281 | } |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 282 | template < typename TOut, bool has_bias, typename std::enable_if < std::is_same<TOut, uint8_t>::value || std::is_same<TOut, int8_t>::value, int >::type = 0 > |
| 283 | void output_stage_nhwc(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 284 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 285 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 286 | using VectorType = typename wrapper::traits::neon_bitvector_t<TOut, wrapper::traits::BitWidth::W128>; |
| 287 | 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] | 288 | |
| 289 | 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] | 290 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 291 | const VectorType min = wrapper::vdup_n(std::numeric_limits<TOut>::lowest(), TagType{}); |
| 292 | const VectorType max = wrapper::vdup_n(std::numeric_limits<TOut>::max(), TagType{}); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 293 | |
| 294 | Window window_bias = window; |
| 295 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 296 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 297 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 298 | |
| 299 | Iterator in(input, window); |
| 300 | Iterator bi(bias, window_bias); |
| 301 | |
| 302 | Iterator out(output, window); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 303 | execute_window_loop(window, [&](const Coordinates &) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 304 | { |
| 305 | // Get bias and pointer to input |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 306 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 307 | int32x4x4_t v_in = |
| 308 | { |
| 309 | { |
| 310 | wrapper::vloadq(in_ptr), |
| 311 | wrapper::vloadq(in_ptr + 4), |
| 312 | wrapper::vloadq(in_ptr + 8), |
| 313 | wrapper::vloadq(in_ptr + 12), |
| 314 | } |
| 315 | }; |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 316 | |
| 317 | // Accumulate bias |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 318 | if(has_bias) |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 319 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 320 | const auto bias_ptr = reinterpret_cast<int32_t *>(bi.ptr()); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 321 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 322 | wrapper::vadd(v_in.val[0], wrapper::vloadq(bias_ptr)); |
| 323 | wrapper::vadd(v_in.val[1], wrapper::vloadq(bias_ptr + 4)); |
| 324 | wrapper::vadd(v_in.val[2], wrapper::vloadq(bias_ptr + 8)); |
| 325 | wrapper::vadd(v_in.val[3], wrapper::vloadq(bias_ptr + 12)); |
| 326 | } |
| 327 | |
| 328 | const auto out_ptr = reinterpret_cast<TOut *>(out.ptr()); |
| 329 | wrapper::vstore(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 330 | }, |
| 331 | in, bi, out); |
| 332 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | } // namespace |
| 334 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 335 | NEDirectConvolutionLayerOutputStageKernel::NEDirectConvolutionLayerOutputStageKernel() |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 336 | : _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] | 337 | { |
| 338 | } |
| 339 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 340 | void NEDirectConvolutionLayerOutputStageKernel::configure(ITensor *input, const ITensor *bias, ITensor *output, |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 341 | const DirectConvolutionLayerOutputStageKernelInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 342 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 343 | // Perform validation step |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 344 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
| 345 | 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] | 346 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 347 | _func = nullptr; |
| 348 | _bias = bias; |
| 349 | _input = input; |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 350 | _output = (output != nullptr) ? output : input; |
| 351 | _result_fixedpoint_multiplier = info.result_fixedpoint_multiplier; |
| 352 | _result_shift = info.result_shift; |
| 353 | _result_offset_after_shift = info.result_offset_after_shift; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 354 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 355 | // Configure kernel window |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 356 | auto win_config = validate_and_configure_window(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info(), info); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 357 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 358 | INEKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 359 | |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 360 | const bool has_bias = bias != nullptr; |
| 361 | 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] | 362 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 363 | // Set appropriate function |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 364 | if(input->info()->data_layout() == DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 365 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 366 | switch(input->info()->data_type()) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 367 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 368 | case DataType::S32: |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 369 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 370 | if(is_qasymm8_signed) |
| 371 | { |
| 372 | _func = (has_bias) ? &output_stage_nchw<int8_t, true> : &output_stage_nchw<int8_t, false>; |
| 373 | } |
| 374 | else |
| 375 | { |
| 376 | _func = (has_bias) ? &output_stage_nchw<uint8_t, true> : &output_stage_nchw<uint8_t, false>; |
| 377 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 378 | break; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 379 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 380 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 381 | case DataType::F16: |
| 382 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 383 | _func = (has_bias) ? &output_stage_nchw<float16_t, true> : &output_stage_nchw<float16_t, false>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 384 | break; |
| 385 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 386 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 387 | case DataType::F32: |
| 388 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 389 | _func = (has_bias) ? &output_stage_nchw<float, true> : &output_stage_nchw<float, false>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 390 | break; |
| 391 | } |
| 392 | default: |
| 393 | { |
| 394 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
| 395 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 396 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 397 | } |
| 398 | else |
| 399 | { |
| 400 | switch(input->info()->data_type()) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 401 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 402 | case DataType::S32: |
| 403 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 404 | if(is_qasymm8_signed) |
| 405 | { |
| 406 | _func = (has_bias) ? &output_stage_nhwc<int8_t, true> : &output_stage_nhwc<int8_t, false>; |
| 407 | } |
| 408 | else |
| 409 | { |
| 410 | _func = (has_bias) ? &output_stage_nhwc<uint8_t, true> : &output_stage_nhwc<uint8_t, false>; |
| 411 | } |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 412 | break; |
| 413 | } |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 414 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 415 | case DataType::F16: |
| 416 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 417 | _func = (has_bias) ? &output_stage_nhwc<float16_t, true> : &output_stage_nhwc<float16_t, false>; |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 418 | break; |
| 419 | } |
| 420 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 421 | case DataType::F32: |
| 422 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 423 | _func = (has_bias) ? &output_stage_nhwc<float, true> : &output_stage_nhwc<float, false>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 424 | break; |
| 425 | } |
| 426 | default: |
| 427 | { |
| 428 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
| 429 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 430 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 431 | } |
| 432 | } |
| 433 | |
Michele Di Giorgio | ff27192 | 2019-07-17 15:59:32 +0100 | [diff] [blame] | 434 | 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] | 435 | const DirectConvolutionLayerOutputStageKernelInfo &info) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 436 | { |
Michele Di Giorgio | 4536193 | 2019-12-19 13:53:44 +0000 | [diff] [blame] | 437 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, info)); |
| 438 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), |
| 439 | bias == nullptr ? nullptr : bias->clone().get(), |
| 440 | output == nullptr ? nullptr : output->clone().get(), |
| 441 | info) |
| 442 | .first); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 443 | |
| 444 | return Status{}; |
| 445 | } |
| 446 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 447 | void NEDirectConvolutionLayerOutputStageKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 448 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 449 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 450 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 451 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 452 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 453 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 454 | (*_func)(_input, _bias, window, _output, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 455 | } |
Michele Di Giorgio | f29d1b7 | 2019-10-29 10:58:13 +0000 | [diff] [blame] | 456 | } // namespace arm_compute |