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" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/NEON/NEAsymm.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/NEON/NEFixedPoint.h" |
| 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | #include <cstddef> |
| 38 | #include <cstdint> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | namespace |
| 43 | { |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output) |
| 45 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QASYMM8, |
| 48 | DataType::QS16, DataType::F16, |
| 49 | DataType::QS32, DataType::S32, DataType::F32); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 50 | |
| 51 | if(bias != nullptr) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 52 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(bias, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::QS32, DataType::S32, DataType::F32); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 54 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 55 | if(is_data_type_fixed_point(input->data_type())) |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QS8 && bias->data_type() != DataType::QS8, "Wrong data type for bias"); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QS16 && bias->data_type() != DataType::QS8, "Wrong data type for bias"); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QS32 && bias->data_type() != DataType::QS16, "Wrong data type for bias"); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, bias); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 61 | } |
| 62 | else |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 65 | } |
| 66 | |
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 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 71 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_data_type_quantized(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 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 77 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QS8, DataType::QASYMM8, DataType::QS16, DataType::F32); |
| 78 | if(is_data_type_fixed_point(input->data_type())) |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 79 | { |
| 80 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QS8 && output->data_type() != DataType::QS8, "Wrong data type for output"); |
| 81 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QS16 && output->data_type() != DataType::QS8, "Wrong data type for output"); |
| 82 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->data_type() == DataType::QS32 && output->data_type() != DataType::QS16, "Wrong data type for output"); |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 83 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output); |
| 84 | } |
| 85 | else if(is_data_type_quantized_asymmetric(output->data_type())) |
| 86 | { |
| 87 | 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] | 88 | } |
| 89 | else |
| 90 | { |
| 91 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 92 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 95 | return Status{}; |
| 96 | } |
| 97 | |
| 98 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output) |
| 99 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 100 | bool window_changed = false; |
| 101 | unsigned int num_elems_processed_per_iteration = 16 / element_size_from_data_type(input->data_type()); |
| 102 | |
| 103 | // Update processed elements when input is S32 (comes from quantization input) |
| 104 | if(input->data_type() == DataType::S32) |
| 105 | { |
| 106 | num_elems_processed_per_iteration = 16; |
| 107 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 108 | |
| 109 | // Configure kernel window |
| 110 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 111 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 112 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 113 | if(output != nullptr && (output->total_size() != 0)) |
| 114 | { |
| 115 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 116 | |
| 117 | if(bias == nullptr) |
| 118 | { |
| 119 | window_changed = update_window_and_padding(win, input_access, output_access); |
| 120 | } |
| 121 | else |
| 122 | { |
| 123 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 124 | window_changed = update_window_and_padding(win, input_access, output_access, bias_access); |
| 125 | } |
| 126 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 127 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 128 | } |
| 129 | else |
| 130 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 131 | if(bias == nullptr) |
| 132 | { |
| 133 | window_changed = update_window_and_padding(win, input_access); |
| 134 | } |
| 135 | else |
| 136 | { |
| 137 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
| 138 | window_changed = update_window_and_padding(win, input_access, bias_access); |
| 139 | } |
| 140 | |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 141 | input_access.set_valid_region(win, ValidRegion(Coordinates(), input->tensor_shape())); |
| 142 | } |
| 143 | |
| 144 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 145 | return std::make_pair(err, win); |
| 146 | } |
| 147 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | // Internal load |
| 149 | inline float32x4_t internal_vld1q(const float *in) |
| 150 | { |
| 151 | return vld1q_f32(in); |
| 152 | } |
| 153 | inline qint8x16_t internal_vld1q(const qint8_t *in) |
| 154 | { |
| 155 | return vld1q_qs8(in); |
| 156 | } |
| 157 | inline qint16x8_t internal_vld1q(const qint16_t *in) |
| 158 | { |
| 159 | return vld1q_qs16(in); |
| 160 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 161 | inline qint32x4_t internal_vld1q(const qint32_t *in) |
| 162 | { |
| 163 | return vld1q_s32(in); |
| 164 | } |
| 165 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 166 | // Internal store |
| 167 | inline void internal_vst1q(float *p, const float32x4_t &v) |
| 168 | { |
| 169 | vst1q_f32(p, v); |
| 170 | } |
| 171 | inline void internal_vst1q(qint8_t *p, const qint8x16_t &v) |
| 172 | { |
| 173 | vst1q_qs8(p, v); |
| 174 | } |
| 175 | inline void internal_vst1q(qint8_t *p, const qint16x8_t &v) |
| 176 | { |
| 177 | vst1_qs8(p, vqmovn_s16(v)); |
| 178 | } |
| 179 | inline void internal_vst1q(qint16_t *p, const qint16x8_t &v) |
| 180 | { |
| 181 | vst1q_qs16(p, v); |
| 182 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 183 | inline void internal_vst1q(qint32_t *p, const qint32x4_t &v) |
| 184 | { |
| 185 | vst1q_s32(p, v); |
| 186 | } |
| 187 | |
| 188 | inline void internal_vst1q(qint16_t *p, const qint32x4_t &v) |
| 189 | { |
| 190 | vst1_qs16(p, vqmovn_qs32(v)); |
| 191 | } |
| 192 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | // Internal vdup |
| 194 | inline float32x4_t internal_vdupq_n(float v) |
| 195 | { |
| 196 | return vdupq_n_f32(v); |
| 197 | } |
| 198 | inline qint8x16_t internal_vdupq_n(qint8_t v) |
| 199 | { |
| 200 | return vdupq_n_qs8(v); |
| 201 | } |
| 202 | inline qint16x8_t internal_vdupq_n(qint16_t v) |
| 203 | { |
| 204 | return vdupq_n_qs16(v); |
| 205 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 206 | inline qint32x4_t internal_vdupq_n(qint32_t v) |
| 207 | { |
| 208 | return vdupq_n_qs32(v); |
| 209 | } |
| 210 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 211 | // Internal vadd |
| 212 | inline float32x4_t internal_vqaddq(const float32x4_t &x, const float32x4_t &y) |
| 213 | { |
| 214 | return vaddq_f32(x, y); |
| 215 | } |
| 216 | inline qint8x16_t internal_vqaddq(const qint8x16_t &x, const qint8x16_t &y) |
| 217 | { |
| 218 | return vqaddq_qs8(x, y); |
| 219 | } |
| 220 | inline qint16x8_t internal_vqaddq(const qint16x8_t &x, const qint16x8_t &y) |
| 221 | { |
| 222 | return vqaddq_qs16(x, y); |
| 223 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 224 | inline qint32x4_t internal_vqaddq(const qint32x4_t &x, const qint32x4_t &y) |
| 225 | { |
| 226 | return vqaddq_qs32(x, y); |
| 227 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 228 | |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 229 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | 0d17614 | 2017-07-06 16:43:14 +0100 | [diff] [blame] | 230 | inline float16x8_t internal_vld1q(const float16_t *in) |
| 231 | { |
| 232 | return vld1q_f16(in); |
| 233 | } |
| 234 | inline void internal_vst1q(float16_t *p, const float16x8_t &v) |
| 235 | { |
| 236 | vst1q_f16(p, v); |
| 237 | } |
| 238 | inline float16x8_t internal_vdupq_n(float16_t v) |
| 239 | { |
| 240 | return vdupq_n_f16(v); |
| 241 | } |
| 242 | inline float16x8_t internal_vqaddq(const float16x8_t &x, const float16x8_t &y) |
| 243 | { |
| 244 | return vaddq_f16(x, y); |
| 245 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 246 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | 0d17614 | 2017-07-06 16:43:14 +0100 | [diff] [blame] | 247 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 248 | template <typename T1, typename T2, bool in_place, bool has_bias> |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 249 | void output_stage(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 250 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 251 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 252 | ARM_COMPUTE_UNUSED(result_fixedpoint_multiplier); |
| 253 | ARM_COMPUTE_UNUSED(result_shift); |
| 254 | ARM_COMPUTE_UNUSED(result_offset_after_shift); |
| 255 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 256 | Iterator in(input, window); |
| 257 | |
| 258 | if(in_place) // In place accumulate |
| 259 | { |
| 260 | execute_window_loop(window, [&](const Coordinates & id) |
| 261 | { |
| 262 | // Get bias and pointer to input |
| 263 | const auto in_ptr = reinterpret_cast<T1 *>(in.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 264 | |
| 265 | // Accumulate bias |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 266 | if(has_bias) |
| 267 | { |
| 268 | const auto vb = internal_vdupq_n(static_cast<T1>(*reinterpret_cast<const T2 *>(bias->ptr_to_element(Coordinates(id.z()))))); |
| 269 | internal_vst1q(in_ptr, internal_vqaddq(internal_vld1q(in_ptr), vb)); |
| 270 | } |
| 271 | else |
| 272 | { |
| 273 | internal_vst1q(in_ptr, internal_vld1q(in_ptr)); |
| 274 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 275 | }, |
| 276 | in); |
| 277 | } |
| 278 | else // Out of place accumulate |
| 279 | { |
| 280 | Iterator out(output, window); |
| 281 | execute_window_loop(window, [&](const Coordinates & id) |
| 282 | { |
| 283 | // Get bias and pointer to input |
| 284 | const auto in_ptr = reinterpret_cast<const T1 *>(in.ptr()); |
| 285 | const auto out_ptr = reinterpret_cast<T2 *>(out.ptr()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 286 | |
| 287 | // Accumulate bias |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 288 | if(has_bias) |
| 289 | { |
| 290 | const auto vb = internal_vdupq_n(static_cast<T1>(*reinterpret_cast<const T2 *>(bias->ptr_to_element(Coordinates(id.z()))))); |
| 291 | internal_vst1q(out_ptr, internal_vqaddq(internal_vld1q(in_ptr), vb)); |
| 292 | } |
| 293 | else |
| 294 | { |
| 295 | internal_vst1q(out_ptr, internal_vld1q(in_ptr)); |
| 296 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 297 | }, |
| 298 | in, out); |
| 299 | } |
| 300 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 301 | |
| 302 | // QASYMM8 specializations |
| 303 | template <> |
| 304 | void output_stage<int32_t, uint8_t, false, true>(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 305 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
| 306 | { |
| 307 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
| 308 | uint8x16_t min = vdupq_n_u8(0); |
| 309 | uint8x16_t max = vdupq_n_u8(255); |
| 310 | |
| 311 | Iterator in(input, window); |
| 312 | Iterator out(output, window); |
| 313 | |
| 314 | execute_window_loop(window, [&](const Coordinates & id) |
| 315 | { |
| 316 | // Get bias and pointer to input |
| 317 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 318 | int32x4x4_t v_in = |
| 319 | { |
| 320 | { |
| 321 | vld1q_s32(in_ptr), |
| 322 | vld1q_s32(in_ptr + 4), |
| 323 | vld1q_s32(in_ptr + 8), |
| 324 | vld1q_s32(in_ptr + 12) |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | // Accumulate bias |
| 329 | const auto vb = vdupq_n_s32(*reinterpret_cast<const int32_t *>(bias->ptr_to_element(Coordinates(id.z())))); |
| 330 | v_in = |
| 331 | { |
| 332 | { |
| 333 | vaddq_s32(v_in.val[0], vb), |
| 334 | vaddq_s32(v_in.val[1], vb), |
| 335 | vaddq_s32(v_in.val[2], vb), |
| 336 | vaddq_s32(v_in.val[3], vb) |
| 337 | } |
| 338 | }; |
| 339 | |
| 340 | const auto out_ptr = reinterpret_cast<uint8_t *>(out.ptr()); |
| 341 | vst1q_u8(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
| 342 | }, |
| 343 | in, out); |
| 344 | } |
| 345 | template <> |
| 346 | void output_stage<int32_t, uint8_t, false, false>(ITensor *input, const ITensor *bias, const Window &window, ITensor *output, |
| 347 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
| 348 | { |
| 349 | ARM_COMPUTE_UNUSED(bias); |
| 350 | |
| 351 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(result_offset_after_shift); |
| 352 | uint8x16_t min = vdupq_n_u8(0); |
| 353 | uint8x16_t max = vdupq_n_u8(255); |
| 354 | |
| 355 | Iterator in(input, window); |
| 356 | Iterator out(output, window); |
| 357 | execute_window_loop(window, [&](const Coordinates & id) |
| 358 | { |
| 359 | // Get bias and pointer to input |
| 360 | const auto in_ptr = reinterpret_cast<int32_t *>(in.ptr()); |
| 361 | int32x4x4_t v_in = |
| 362 | { |
| 363 | { |
| 364 | vld1q_s32(in_ptr), |
| 365 | vld1q_s32(in_ptr + 4), |
| 366 | vld1q_s32(in_ptr + 8), |
| 367 | vld1q_s32(in_ptr + 12) |
| 368 | } |
| 369 | }; |
| 370 | |
| 371 | const auto out_ptr = reinterpret_cast<uint8_t *>(out.ptr()); |
| 372 | vst1q_u8(out_ptr, finalize_quantization<false>(v_in, result_fixedpoint_multiplier, result_shift, result_offset_after_shift_s32, min, max)); |
| 373 | }, |
| 374 | in, 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, |
| 384 | int result_fixedpoint_multiplier, int result_shift, int result_offset_after_shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 385 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 386 | ARM_COMPUTE_ERROR_ON_NULLPTR(input); |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 387 | |
Georgios Pinitas | 0223a78 | 2017-12-12 11:44:44 +0000 | [diff] [blame] | 388 | // Auto-initialize output output if required |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 389 | if(output != nullptr) |
| 390 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 391 | // Work out expected output data type |
| 392 | 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] | 393 | // Output tensor auto initialization if not yet initialized |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 394 | 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] | 395 | } |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 396 | |
| 397 | // Perform validation step |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 398 | 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] | 399 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 400 | _func = nullptr; |
| 401 | _bias = bias; |
| 402 | _input = input; |
| 403 | _output = output; |
| 404 | _result_fixedpoint_multiplier = result_fixedpoint_multiplier; |
| 405 | _result_shift = result_shift; |
| 406 | _result_offset_after_shift = result_offset_after_shift; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 407 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 408 | // Configure kernel window |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 409 | 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] | 410 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 411 | INEKernel::configure(win_config.second); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 412 | |
| 413 | // Set appropriate function |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 414 | switch(input->info()->data_type()) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 415 | { |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 416 | case DataType::QS8: |
| 417 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 418 | if(bias == nullptr) |
| 419 | { |
| 420 | _func = (output == nullptr) ? &output_stage<qint8_t, qint8_t, true, false> : &output_stage<qint8_t, qint8_t, false, false>; |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | _func = (output == nullptr) ? &output_stage<qint8_t, qint8_t, true, true> : &output_stage<qint8_t, qint8_t, false, true>; |
| 425 | } |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 426 | break; |
| 427 | } |
| 428 | case DataType::QS16: |
| 429 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 430 | if(bias != nullptr && bias->info()->data_type() == DataType::QS8) |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 431 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 432 | _func = (output == nullptr) ? &output_stage<qint16_t, qint8_t, true, true> : &output_stage<qint16_t, qint8_t, false, true>; |
| 433 | } |
| 434 | else if(bias == nullptr) |
| 435 | { |
| 436 | _func = (output == nullptr) ? &output_stage<qint16_t, qint8_t, true, false> : &output_stage<qint16_t, qint8_t, false, false>; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 437 | } |
| 438 | else |
| 439 | { |
| 440 | ARM_COMPUTE_ERROR("Not implemented"); |
| 441 | } |
| 442 | break; |
| 443 | } |
| 444 | case DataType::QS32: |
| 445 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 446 | _func = (output == nullptr) ? &output_stage<qint32_t, qint16_t, true, true> : &output_stage<qint32_t, qint16_t, false, true>; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 447 | break; |
| 448 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 449 | case DataType::S32: |
| 450 | _func = (bias == nullptr) ? &output_stage<int32_t, uint8_t, false, false> : &output_stage<int32_t, uint8_t, false, true>; |
| 451 | break; |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 452 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 453 | case DataType::F16: |
| 454 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 455 | _func = (output == nullptr) ? &output_stage<float16_t, float16_t, true, true> : &output_stage<float16_t, float16_t, false, true>; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 456 | break; |
| 457 | } |
Ioan-Cristian Szabo | 5edbd1c | 2017-11-13 13:34:08 +0000 | [diff] [blame] | 458 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 459 | case DataType::F32: |
| 460 | { |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 461 | _func = (output == nullptr) ? &output_stage<float, float, true, true> : &output_stage<float, float, false, true>; |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 462 | break; |
| 463 | } |
| 464 | default: |
| 465 | { |
| 466 | ARM_COMPUTE_ERROR("Unsupported combination of types among the inputs."); |
Pablo Tello | f87cc7f | 2017-07-26 10:28:40 +0100 | [diff] [blame] | 467 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 471 | Status NEDirectConvolutionLayerOutputStageKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output) |
Michalis Spyrou | afa5d81 | 2017-11-30 14:25:57 +0000 | [diff] [blame] | 472 | { |
| 473 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output)); |
| 474 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), bias->clone().get(), output == nullptr ? nullptr : output->clone().get()).first); |
| 475 | |
| 476 | return Status{}; |
| 477 | } |
| 478 | |
Michalis Spyrou | b91e34c | 2017-12-20 15:50:55 +0000 | [diff] [blame] | 479 | void NEDirectConvolutionLayerOutputStageKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 480 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 481 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 482 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 483 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 484 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 485 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 486 | (*_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] | 487 | } |