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