Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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" |
| 33 | #include "arm_compute/core/Types.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
| 36 | |
| 37 | #include <arm_neon.h> |
| 38 | #include <cstddef> |
| 39 | #include <cstdint> |
| 40 | |
| 41 | using namespace arm_compute; |
| 42 | |
| 43 | namespace |
| 44 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output) |
| 46 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, |
| 50 | DataType::F16, |
Vidhya Sudhan Loganathan | f4cb81b | 2018-07-04 15:13:14 +0100 | [diff] [blame] | 51 | DataType::S32, DataType::F32); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 52 | |
| 53 | if(bias != nullptr) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 54 | { |
Vidhya Sudhan Loganathan | f4cb81b | 2018-07-04 15:13:14 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::F16, DataType::S32, DataType::F32); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 56 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 57 | if(is_data_type_quantized_asymmetric(input->data_type())) |
Georgios Pinitas | 19d0547 | 2018-02-01 16:44:12 +0000 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::S32); |
| 60 | } |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 61 | else |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 64 | } |
| 65 | |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 66 | 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] | 67 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 68 | } |
| 69 | else |
| 70 | { |
Georgios Pinitas | 19d0547 | 2018-02-01 16:44:12 +0000 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(is_data_type_float(input->data_type()), "Calling output stage kernel with floating point arguments"); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 72 | } |
| 73 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 74 | // Checks performed when output is configured |
| 75 | if((output != nullptr) && (output->total_size() != 0)) |
| 76 | { |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::F32); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 79 | |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 80 | if(is_data_type_quantized_asymmetric(output->data_type())) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 81 | { |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::S32 && output->data_type() != DataType::QASYMM8, "Wrong data type for bias"); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 83 | } |
| 84 | else |
| 85 | { |
| 86 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 87 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 88 | } |
| 89 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 90 | return Status{}; |
| 91 | } |
| 92 | |
| 93 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output) |
| 94 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
| 96 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 97 | bool window_changed = false; |
| 98 | unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(input->data_type()); |
| 99 | |
| 100 | // Update processed elements when input is S32 (comes from quantization input) |
| 101 | if(input->data_type() == DataType::S32) |
| 102 | { |
| 103 | num_elems_processed_per_iteration = 16; |
| 104 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 105 | |
| 106 | // Configure kernel window |
| 107 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 108 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 109 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 110 | if(output != nullptr && (output->total_size() != 0)) |
| 111 | { |
| 112 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 113 | |
| 114 | if(bias == nullptr) |
| 115 | { |
| 116 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 121 | window_changed = update_window_and_padding(win, input_access, output_access, bias_access); |
| 122 | } |
| 123 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 124 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 125 | } |
| 126 | else |
| 127 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 128 | if(bias == nullptr) |
| 129 | { |
| 130 | window_changed = update_window_and_padding(win, input_access); |
| 131 | } |
| 132 | else |
| 133 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 134 | if(input->data_layout() == DataLayout::NCHW) |
| 135 | { |
| 136 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 137 | window_changed = update_window_and_padding(win, input_access, bias_access); |
| 138 | } |
| 139 | else |
| 140 | { |
| 141 | AccessWindowHorizontal bias_access(bias, 0, num_elems_processed_per_iteration); |
| 142 | window_changed = update_window_and_padding(win, input_access, bias_access); |
| 143 | } |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 146 | input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape())); |
| 147 | } |
| 148 | |
| 149 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 150 | return std::make_pair(err, win); |
| 151 | } |
| 152 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | // Internal load |
| 154 | inline float32x4_t internal_vld1q(const float *in) |
| 155 | { |
| 156 | return vld1q_f32(in); |
| 157 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 158 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | // Internal store |
| 160 | inline void internal_vst1q(float *p, const float32x4_t &v) |
| 161 | { |
| 162 | vst1q_f32(p, v); |
| 163 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 164 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 165 | // Internal vdup |
| 166 | inline float32x4_t internal_vdupq_n(float v) |
| 167 | { |
| 168 | return vdupq_n_f32(v); |
| 169 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 170 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 171 | // Internal vadd |
| 172 | inline float32x4_t internal_vqaddq(const float32x4_t &x, const float32x4_t &y) |
| 173 | { |
| 174 | return vaddq_f32(x, y); |
| 175 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 177 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 0d17614 | 2017-07-06 16:43:14 +0100 | [diff] [blame] | 178 | inline float16x8_t internal_vld1q(const float16_t *in) |
| 179 | { |
| 180 | return vld1q_f16(in); |
| 181 | } |
| 182 | inline void internal_vst1q(float16_t *p, const float16x8_t &v) |
| 183 | { |
| 184 | vst1q_f16(p, v); |
| 185 | } |
| 186 | inline float16x8_t internal_vdupq_n(float16_t v) |
| 187 | { |
| 188 | return vdupq_n_f16(v); |
| 189 | } |
| 190 | inline float16x8_t internal_vqaddq(const float16x8_t &x, const float16x8_t &y) |
| 191 | { |
| 192 | return vaddq_f16(x, y); |
| 193 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 194 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 0d17614 | 2017-07-06 16:43:14 +0100 | [diff] [blame] | 195 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 196 | template <typename T1, typename T2, bool in_place, bool has_bias> |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 197 | void output_stage_nchw(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 198 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 200 | ARM_COMPUTE_ERROR_ON(input->info()->data_layout() == DataLayout::UNKNOWN); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 201 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 202 | ARM_COMPUTE_UNUSED(result_shift); |
| 203 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 204 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 205 | Iterator in(input, window); |
| 206 | |
| 207 | if(in_place) // In place accumulate |
| 208 | { |
| 209 | execute_window_loop(window, [&](const Coordinates & id) |
| 210 | { |
| 211 | // Get bias and pointer to input |
| 212 | const auto in_ptr = reinterpret_cast<T1 *>(in.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | |
| 214 | // Accumulate bias |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 215 | if(has_bias) |
| 216 | { |
| 217 | const auto vb = internal_vdupq_n(static_cast<T1>(*reinterpret_cast<const T2 *>(bias->ptr_to_element(Coordinates(id.z()))))); |
| 218 | internal_vst1q(in_ptr, internal_vqaddq(internal_vld1q(in_ptr), vb)); |
| 219 | } |
| 220 | else |
| 221 | { |
| 222 | internal_vst1q(in_ptr, internal_vld1q(in_ptr)); |
| 223 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 224 | }, |
| 225 | in); |
| 226 | } |
| 227 | else // Out of place accumulate |
| 228 | { |
| 229 | Iterator out(output, window); |
| 230 | execute_window_loop(window, [&](const Coordinates & id) |
| 231 | { |
| 232 | // Get bias and pointer to input |
| 233 | const auto in_ptr = reinterpret_cast<const T1 *>(in.ptr()); |
| 234 | const auto out_ptr = reinterpret_cast<T2 *>(out.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | |
| 236 | // Accumulate bias |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 237 | if(has_bias) |
| 238 | { |
| 239 | const auto vb = internal_vdupq_n(static_cast<T1>(*reinterpret_cast<const T2 *>(bias->ptr_to_element(Coordinates(id.z()))))); |
| 240 | internal_vst1q(out_ptr, internal_vqaddq(internal_vld1q(in_ptr), vb)); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | internal_vst1q(out_ptr, internal_vld1q(in_ptr)); |
| 245 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 246 | }, |
| 247 | in, out); |
| 248 | } |
| 249 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 250 | |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 251 | template <typename T1, typename T2, bool in_place, bool has_bias> |
| 252 | void output_stage_nhwc(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 253 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
| 254 | { |
| 255 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 256 | ARM_COMPUTE_UNUSED(result_shift); |
| 257 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 258 | |
| 259 | Window window_bias = window; |
| 260 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 261 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 262 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 263 | |
| 264 | Iterator in(input, window); |
| 265 | Iterator bi(bias, window_bias); |
| 266 | |
| 267 | if(in_place) // In place accumulate |
| 268 | { |
| 269 | execute_window_loop(window, [&](const Coordinates & id) |
| 270 | { |
| 271 | // Get bias and pointer to input |
| 272 | const auto in_ptr = reinterpret_cast<T1 *>(in.ptr()); |
| 273 | const auto bias_ptr = reinterpret_cast<T2 *>(bi.ptr()); |
| 274 | |
| 275 | // Accumulate bias |
| 276 | if(has_bias) |
| 277 | { |
| 278 | internal_vst1q(in_ptr, internal_vqaddq(internal_vld1q(in_ptr), internal_vld1q(bias_ptr))); |
| 279 | } |
| 280 | else |
| 281 | { |
| 282 | internal_vst1q(in_ptr, internal_vld1q(in_ptr)); |
| 283 | } |
| 284 | }, |
| 285 | in, bi); |
| 286 | } |
| 287 | else // Out of place accumulate |
| 288 | { |
| 289 | Iterator out(output, window); |
| 290 | execute_window_loop(window, [&](const Coordinates & id) |
| 291 | { |
| 292 | // Get bias and pointer to input |
| 293 | const auto in_ptr = reinterpret_cast<T1 *>(in.ptr()); |
| 294 | const auto out_ptr = reinterpret_cast<T2 *>(out.ptr()); |
| 295 | const auto bias_ptr = reinterpret_cast<T2 *>(bi.ptr()); |
| 296 | |
| 297 | // Accumulate bias |
| 298 | if(has_bias) |
| 299 | { |
| 300 | internal_vst1q(out_ptr, internal_vqaddq(internal_vld1q(in_ptr), internal_vld1q(bias_ptr))); |
| 301 | } |
| 302 | else |
| 303 | { |
| 304 | internal_vst1q(out_ptr, internal_vld1q(in_ptr)); |
| 305 | } |
| 306 | }, |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 307 | in, bi, out); |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 308 | } |
| 309 | } |
| 310 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 311 | // QASYMM8 specializations |
| 312 | template <> |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 313 | void output_stage_nchw<int32_t, uint8_t, false, true>(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 314 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 315 | { |
| 316 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
| 317 | uint8x16_t min = vdupq_n_u8(0); |
| 318 | uint8x16_t max = vdupq_n_u8(255); |
| 319 | |
| 320 | Iterator in(input, window); |
| 321 | Iterator out(output, window); |
| 322 | |
| 323 | execute_window_loop(window, [&](const Coordinates & id) |
| 324 | { |
| 325 | // Get bias and pointer to input |
| 326 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 327 | int32x4x4_t v_in = |
| 328 | { |
| 329 | { |
| 330 | vld1q_s32(in_ptr), |
| 331 | vld1q_s32(in_ptr + 4), |
| 332 | vld1q_s32(in_ptr + 8), |
| 333 | vld1q_s32(in_ptr + 12) |
| 334 | } |
| 335 | }; |
| 336 | |
| 337 | // Accumulate bias |
| 338 | const auto vb = vdupq_n_s32(*reinterpret_cast<const int32_t *>(bias->ptr_to_element(Coordinates(id.z())))); |
| 339 | v_in = |
| 340 | { |
| 341 | { |
| 342 | vaddq_s32(v_in.val[0], vb), |
| 343 | vaddq_s32(v_in.val[1], vb), |
| 344 | vaddq_s32(v_in.val[2], vb), |
| 345 | vaddq_s32(v_in.val[3], vb) |
| 346 | } |
| 347 | }; |
| 348 | |
| 349 | const auto out_ptr = reinterpret_cast<uint8_t *>(out.ptr()); |
| 350 | vst1q_u8(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
| 351 | }, |
| 352 | in, out); |
| 353 | } |
| 354 | template <> |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 355 | void output_stage_nchw<int32_t, uint8_t, false, false>(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 356 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 357 | { |
| 358 | ARM_COMPUTE_UNUSED(bias); |
| 359 | |
| 360 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
| 361 | uint8x16_t min = vdupq_n_u8(0); |
| 362 | uint8x16_t max = vdupq_n_u8(255); |
| 363 | |
| 364 | Iterator in(input, window); |
| 365 | Iterator out(output, window); |
| 366 | execute_window_loop(window, [&](const Coordinates & id) |
| 367 | { |
| 368 | // Get bias and pointer to input |
| 369 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 370 | int32x4x4_t v_in = |
| 371 | { |
| 372 | { |
| 373 | vld1q_s32(in_ptr), |
| 374 | vld1q_s32(in_ptr + 4), |
| 375 | vld1q_s32(in_ptr + 8), |
| 376 | vld1q_s32(in_ptr + 12) |
| 377 | } |
| 378 | }; |
| 379 | |
| 380 | const auto out_ptr = reinterpret_cast<uint8_t *>(out.ptr()); |
| 381 | vst1q_u8(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
| 382 | }, |
| 383 | in, out); |
| 384 | } |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 385 | template <> |
| 386 | void output_stage_nhwc<int32_t, uint8_t, false, true>(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 387 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
| 388 | { |
| 389 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
| 390 | uint8x16_t min = vdupq_n_u8(0); |
| 391 | uint8x16_t max = vdupq_n_u8(255); |
| 392 | |
| 393 | Window window_bias = window; |
| 394 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 395 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 396 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 397 | |
| 398 | Iterator in(input, window); |
| 399 | Iterator bi(bias, window_bias); |
| 400 | |
| 401 | Iterator out(output, window); |
| 402 | execute_window_loop(window, [&](const Coordinates & id) |
| 403 | { |
| 404 | // Get bias and pointer to input |
| 405 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 406 | const auto bias_ptr = reinterpret_cast<int32_t *>(bi.ptr()); |
| 407 | |
| 408 | // Accumulate bias |
| 409 | int32x4x4_t v_in = |
| 410 | { |
| 411 | { |
| 412 | vaddq_s32(vld1q_s32(in_ptr), vld1q_s32(bias_ptr)), |
| 413 | vaddq_s32(vld1q_s32(in_ptr + 4), vld1q_s32(bias_ptr + 4)), |
| 414 | vaddq_s32(vld1q_s32(in_ptr + 8), vld1q_s32(bias_ptr + 8)), |
| 415 | vaddq_s32(vld1q_s32(in_ptr + 12), vld1q_s32(bias_ptr + 12)) |
| 416 | } |
| 417 | }; |
| 418 | |
| 419 | const auto out_ptr = out.ptr(); |
| 420 | vst1q_u8(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
| 421 | }, |
| 422 | in, bi, out); |
| 423 | } |
| 424 | template <> |
| 425 | void output_stage_nhwc<int32_t, uint8_t, false, false>(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 426 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
| 427 | { |
| 428 | ARM_COMPUTE_UNUSED(bias); |
| 429 | |
| 430 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
| 431 | uint8x16_t min = vdupq_n_u8(0); |
| 432 | uint8x16_t max = vdupq_n_u8(255); |
| 433 | |
| 434 | Window window_bias = window; |
| 435 | window_bias.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 436 | window_bias.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 437 | window_bias.set(3, Window::Dimension(0, 0, 0)); |
| 438 | |
| 439 | Iterator in(input, window); |
| 440 | Iterator bi(bias, window_bias); |
| 441 | |
| 442 | Iterator out(output, window); |
| 443 | execute_window_loop(window, [&](const Coordinates & id) |
| 444 | { |
| 445 | // Get bias and pointer to input |
| 446 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 447 | |
| 448 | // Accumulate bias |
| 449 | int32x4x4_t v_in = |
| 450 | { |
| 451 | { |
| 452 | vld1q_s32(in_ptr), |
| 453 | vld1q_s32(in_ptr + 4), |
| 454 | vld1q_s32(in_ptr + 8), |
| 455 | vld1q_s32(in_ptr + 12) |
| 456 | } |
| 457 | }; |
| 458 | |
| 459 | const auto out_ptr = out.ptr(); |
| 460 | vst1q_u8(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
| 461 | }, |
| 462 | in, bi, out); |
| 463 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 464 | } // namespace |
| 465 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 466 | NEDirectConvolutionLayerOutputStageKernel::NEDirectConvolutionLayerOutputStageKernel() |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 467 | : _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] | 468 | { |
| 469 | } |
| 470 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 471 | void NEDirectConvolutionLayerOutputStageKernel::configure(ITensor *input, const ITensor *bias, ITensor *output, |
| 472 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 473 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 474 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 475 | |
Georgios Pinitas | 0223a78 | 2017-12-12 11:44:44 +0000 | [diff] [blame] | 476 | // Auto-initialize output output if required |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 477 | if(output != nullptr) |
| 478 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 479 | // Work out expected output data type |
| 480 | const DataType output_dt = (input->info()->data_type() == DataType::S32) ? DataType::QASYMM8 : input->info()->data_type(); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 481 | // Output tensor auto initialization if not yet initialized |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 482 | auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(output_dt)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 483 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 484 | |
| 485 | // Perform validation step |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 486 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 487 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 488 | _func = nullptr; |
| 489 | _bias = bias; |
| 490 | _input = input; |
| 491 | _output = output; |
| 492 | _result_fixedpoint_multiplier = result_fixedpoint_multiplier; |
| 493 | _result_shift = result_shift; |
| 494 | _result_offset_after_shift = result_offset_after_shift; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 495 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 496 | // Configure kernel window |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 497 | auto win_config = validate_and_configure_window(input->info(), (bias == nullptr) ? nullptr : bias->info(), (output == nullptr) ? nullptr : output->info()); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 498 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 499 | INEKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 500 | |
| 501 | // Set appropriate function |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 502 | if(input->info()->data_layout() == DataLayout::NCHW) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 503 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 504 | switch(input->info()->data_type()) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 505 | { |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 506 | case DataType::S32: |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 507 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 508 | _func = (bias == nullptr) ? &output_stage_nchw<int32_t, uint8_t, false, false> : &output_stage_nchw<int32_t, uint8_t, false, true>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 509 | break; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 510 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 511 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 512 | case DataType::F16: |
| 513 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 514 | _func = (output == nullptr) ? &output_stage_nchw<float16_t, float16_t, true, true> : &output_stage_nchw<float16_t, float16_t, false, true>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 515 | break; |
| 516 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 517 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 518 | case DataType::F32: |
| 519 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 520 | _func = (output == nullptr) ? &output_stage_nchw<float, float, true, true> : &output_stage_nchw<float, float, false, true>; |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | default: |
| 524 | { |
| 525 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
| 526 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 527 | } |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 528 | } |
| 529 | else |
| 530 | { |
| 531 | switch(input->info()->data_type()) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 532 | { |
Georgios Pinitas | a799ce0 | 2018-09-12 20:11:34 +0100 | [diff] [blame] | 533 | case DataType::S32: |
| 534 | { |
| 535 | _func = (output == nullptr) ? &output_stage_nhwc<int32_t, uint8_t, false, false> : &output_stage_nhwc<int32_t, uint8_t, false, true>; |
| 536 | break; |
| 537 | } |
Georgios Pinitas | 20c246a | 2018-09-12 16:45:53 +0100 | [diff] [blame] | 538 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 539 | case DataType::F16: |
| 540 | { |
| 541 | _func = (output == nullptr) ? &output_stage_nhwc<float16_t, float16_t, true, true> : &output_stage_nhwc<float16_t, float16_t, false, true>; |
| 542 | break; |
| 543 | } |
| 544 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Giorgio Arena | 1ed1fc6 | 2018-03-26 16:20:05 +0100 | [diff] [blame] | 545 | case DataType::F32: |
| 546 | { |
| 547 | _func = (output == nullptr) ? &output_stage_nhwc<float, float, true, true> : &output_stage_nhwc<float, float, false, true>; |
| 548 | break; |
| 549 | } |
| 550 | default: |
| 551 | { |
| 552 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
| 553 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 554 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 555 | } |
| 556 | } |
| 557 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 558 | Status NEDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 559 | { |
| 560 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output)); |
Anthony Barbier | de01468 | 2018-07-03 15:10:48 +0100 | [diff] [blame] | 561 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), bias == nullptr ? nullptr : bias->clone().get(), output == nullptr ? nullptr : output->clone().get()).first); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 562 | |
| 563 | return Status{}; |
| 564 | } |
| 565 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 566 | void NEDirectConvolutionLayerOutputStageKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 567 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 568 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 569 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 570 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 571 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 572 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 573 | (*_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] | 574 | } |