Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel.h" |
| 25 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/Types.h" |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Utils.h" |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | #include <arm_neon.h> |
| 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | |
| 39 | using namespace arm_compute; |
| 40 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 43 | Error validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(max > 255); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(min < 0 || min > max); |
| 48 | |
| 49 | // Check biases if exist |
| 50 | if(bias != nullptr) |
| 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0)); |
| 55 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 56 | |
| 57 | if(output->total_size() != 0) |
| 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 61 | } |
| 62 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 63 | return Error{}; |
| 64 | } |
| 65 | |
| 66 | std::pair<Error, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output) |
| 67 | { |
| 68 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 69 | |
| 70 | // Configure kernel window |
| 71 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 72 | |
| 73 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 74 | |
| 75 | bool window_changed = update_window_and_padding(win, |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 76 | input_access); |
| 77 | |
| 78 | if(output->total_size() != 0) |
| 79 | { |
| 80 | AccessWindowHorizontal output_result_access(output, 0, num_elems_processed_per_iteration); |
| 81 | window_changed = window_changed || update_window_and_padding(win, output_result_access); |
| 82 | |
| 83 | output_result_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 84 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 85 | |
| 86 | if(bias != nullptr) |
| 87 | { |
| 88 | AccessWindowStatic bias_access(bias, 0, 0, ceil_to_multiple(bias->dimension(0), num_elems_processed_per_iteration), bias->tensor_shape()[1]); |
| 89 | window_changed = window_changed || update_window_and_padding(win, bias_access); |
| 90 | } |
| 91 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 92 | Error err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Error{}; |
| 93 | return std::make_pair(err, win); |
| 94 | } |
| 95 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 96 | inline void scale_input(int32x4x4_t &in_s32, int32x4_t result_offset_s32, int32_t result_mult_int) |
| 97 | { |
| 98 | // Add the offset terms to GEMM's result |
| 99 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_s32); |
| 100 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_s32); |
| 101 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_s32); |
| 102 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_s32); |
| 103 | |
| 104 | // Multiply by result_mult_int |
| 105 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], result_mult_int); |
| 106 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], result_mult_int); |
| 107 | in_s32.val[2] = vmulq_n_s32(in_s32.val[2], result_mult_int); |
| 108 | in_s32.val[3] = vmulq_n_s32(in_s32.val[3], result_mult_int); |
| 109 | } |
| 110 | |
| 111 | template <bool is_bounded_relu> |
| 112 | inline uint8x16_t finalize_quantization(int32x4x4_t &in_s32, int32x4_t result_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8) |
| 113 | { |
| 114 | const static int32x4_t zero_s32 = vdupq_n_s32(0); |
| 115 | |
| 116 | // Shift final result (negative value shift right) |
| 117 | in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32); |
| 118 | in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32); |
| 119 | in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32); |
| 120 | in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32); |
| 121 | |
| 122 | // Saturate negative values |
| 123 | in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32); |
| 124 | in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32); |
| 125 | in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32); |
| 126 | in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32); |
| 127 | |
| 128 | // Convert S32 to S16 |
| 129 | const int16x8x2_t in_s16 = |
| 130 | { |
| 131 | { |
| 132 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 133 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | // Convert S16 to U8 |
| 138 | uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1])); |
| 139 | |
| 140 | if(is_bounded_relu) |
| 141 | { |
| 142 | out_u8 = vmaxq_u8(out_u8, min_u8); |
| 143 | out_u8 = vminq_u8(out_u8, max_u8); |
| 144 | } |
| 145 | |
| 146 | return out_u8; |
| 147 | } |
| 148 | } // namespace |
| 149 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 150 | namespace arm_compute |
| 151 | { |
| 152 | class Coordinates; |
| 153 | } // namespace arm_compute |
| 154 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 155 | template <bool is_bounded_relu> |
| 156 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window) |
| 157 | { |
| 158 | const int32x4_t result_offset_s32 = vdupq_n_s32(_result_offset); |
| 159 | const int32x4_t result_shift_s32 = vdupq_n_s32(-_result_shift); |
| 160 | const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min)); |
| 161 | const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max)); |
| 162 | |
| 163 | ARM_COMPUTE_UNUSED(min_u8); |
| 164 | ARM_COMPUTE_UNUSED(max_u8); |
| 165 | |
| 166 | Iterator in(_input, window); |
| 167 | Iterator out(_output, window); |
| 168 | |
| 169 | if(_bias != nullptr) |
| 170 | { |
| 171 | Window win_biases; |
| 172 | win_biases.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().step())); |
| 173 | win_biases.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 174 | |
| 175 | Iterator bias(_bias, win_biases); |
| 176 | execute_window_loop(window, [&](const Coordinates & id) |
| 177 | { |
| 178 | int32x4x4_t in_s32 = |
| 179 | { |
| 180 | { |
| 181 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 0), |
| 182 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 4), |
| 183 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 8), |
| 184 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 12) |
| 185 | } |
| 186 | }; |
| 187 | |
| 188 | const int32x4x4_t bias_s32 = |
| 189 | { |
| 190 | { |
| 191 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 0), |
| 192 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 4), |
| 193 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 8), |
| 194 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + 12) |
| 195 | } |
| 196 | }; |
| 197 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 198 | // Add the bias to GEMM's result |
| 199 | in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]); |
| 200 | in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]); |
| 201 | in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]); |
| 202 | in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]); |
| 203 | |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 204 | // Add the offset terms to GEMM's result and multiply by result_mult_int |
| 205 | scale_input(in_s32, result_offset_s32, _result_mult_int); |
| 206 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 207 | vst1q_u8(out.ptr(), finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8)); |
| 208 | }, |
| 209 | in, bias, out); |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | execute_window_loop(window, [&](const Coordinates & id) |
| 214 | { |
| 215 | int32x4x4_t in_s32 = |
| 216 | { |
| 217 | { |
| 218 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 0), |
| 219 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 4), |
| 220 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 8), |
| 221 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + 12) |
| 222 | } |
| 223 | }; |
| 224 | |
| 225 | // Add the offset terms to GEMM's result and multiply by result_mult_int |
| 226 | scale_input(in_s32, result_offset_s32, _result_mult_int); |
| 227 | |
| 228 | vst1q_u8(out.ptr(), finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8)); |
| 229 | }, |
| 230 | in, out); |
| 231 | } |
| 232 | } |
| 233 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 234 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel() |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 235 | : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_offset(0), _result_mult_int(0), _result_shift(0), _min(0), _max(0) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 236 | { |
| 237 | } |
| 238 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 239 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_offset, int result_mult_int, int result_shift, int min, int max) |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 240 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 241 | // Perform validate step |
| 242 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 243 | |
| 244 | // Output auto inizialitation if not yet initialized |
| 245 | auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(DataType::QASYMM8)); |
| 246 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 247 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), |
| 248 | (bias != nullptr) ? bias->info() : nullptr, |
| 249 | output->info(), |
| 250 | min, |
| 251 | max)); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 252 | |
| 253 | _input = input; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 254 | _bias = bias; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 255 | _output = output; |
| 256 | _result_offset = result_offset; |
| 257 | _result_mult_int = result_mult_int; |
| 258 | _result_shift = result_shift; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 259 | _min = min; |
| 260 | _max = max; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 261 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 262 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 263 | auto win_config = validate_and_configure_window(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info()); |
| 264 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 265 | INEKernel::configure(win_config.second); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 266 | |
| 267 | // Check if we need to clamp the result using min and max |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 268 | const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255)); |
| 269 | _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<false>; |
| 270 | } |
| 271 | |
| 272 | Error NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
| 273 | { |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 274 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 275 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max)); |
| 276 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), |
| 277 | (bias != nullptr) ? bias->clone().get() : nullptr, |
| 278 | output->clone().get()) |
| 279 | .first); |
| 280 | |
| 281 | return Error{}; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window, const ThreadInfo &info) |
| 285 | { |
| 286 | ARM_COMPUTE_UNUSED(info); |
| 287 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 288 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 289 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 290 | (this->*_func)(window); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 291 | } |