Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 1 | /* |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame^] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +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 | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel.h" |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 25 | |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
| 34 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/AccessWindowStatic.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 36 | #include "src/core/NEON/NESymm.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 37 | #include "src/core/helpers/AutoConfiguration.h" |
| 38 | #include "src/core/helpers/WindowHelpers.h" |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 39 | |
| 40 | #include <arm_neon.h> |
| 41 | #include <cstddef> |
| 42 | #include <cstdint> |
| 43 | |
| 44 | namespace arm_compute |
| 45 | { |
| 46 | namespace |
| 47 | { |
| 48 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
| 49 | { |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32); |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(min > max); |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 52 | |
| 53 | // Check biases if exist |
| 54 | if(bias != nullptr) |
| 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0)); |
| 59 | } |
| 60 | |
| 61 | if(output->total_size() != 0) |
| 62 | { |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QSYMM16); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input); |
| 65 | } |
| 66 | |
| 67 | return Status{}; |
| 68 | } |
| 69 | |
| 70 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 71 | { |
| 72 | // Output auto inizialitation if not yet initialized |
| 73 | auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QSYMM16)); |
| 74 | |
| 75 | // Configure kernel window |
| 76 | Window win = calculate_max_window(*input, Steps()); |
| 77 | |
| 78 | // NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel doesn't need padding so update_window_and_padding() can be skipped |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 79 | |
| 80 | return std::make_pair(Status{}, win); |
| 81 | } |
| 82 | } // namespace |
| 83 | |
| 84 | class Coordinates; |
| 85 | |
| 86 | template <bool is_bounded_relu> |
| 87 | void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run(const Window &window) |
| 88 | { |
| 89 | const int16x8_t min_s16 = vdupq_n_s16(static_cast<int16_t>(_min)); |
| 90 | const int16x8_t max_s16 = vdupq_n_s16(static_cast<int16_t>(_max)); |
| 91 | |
| 92 | ARM_COMPUTE_UNUSED(min_s16); |
| 93 | ARM_COMPUTE_UNUSED(max_s16); |
| 94 | |
| 95 | const int window_step_x = 8; |
| 96 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 97 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 98 | |
| 99 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 100 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 101 | |
| 102 | Iterator in(_input, win_collapsed); |
| 103 | Iterator out(_output, win_collapsed); |
| 104 | if(_bias != nullptr) |
| 105 | { |
| 106 | Window win_biases; |
| 107 | win_biases.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 108 | win_biases.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 109 | |
| 110 | Iterator bias(_bias, win_biases); |
| 111 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 112 | { |
| 113 | // Compute 16 elements per iteration |
| 114 | int x = window_start_x; |
| 115 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 116 | { |
| 117 | int32x4x2_t in_s32 = |
| 118 | { |
| 119 | { |
| 120 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0), |
| 121 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4) |
| 122 | } |
| 123 | }; |
| 124 | |
| 125 | const int32x4x2_t bias_s32 = |
| 126 | { |
| 127 | { |
| 128 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0), |
| 129 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4) |
| 130 | } |
| 131 | }; |
| 132 | |
| 133 | // Add the bias to GEMM's result |
| 134 | in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]); |
| 135 | in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]); |
| 136 | |
| 137 | vst1q_s16(reinterpret_cast<int16_t *>(out.ptr()) + x, finalize_quantization_int16<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, min_s16, max_s16)); |
| 138 | } |
| 139 | |
| 140 | // Compute left-over elements |
| 141 | for(; x < window_end_x; ++x) |
| 142 | { |
| 143 | const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x); |
| 144 | int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x); |
| 145 | |
| 146 | // Add bias |
| 147 | in_value += bias_value; |
| 148 | // Finalize and store the result |
| 149 | *(reinterpret_cast<int16_t *>(out.ptr()) + x) = finalize_quantization_int16<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, static_cast<int16_t>(_min), |
| 150 | static_cast<int16_t>(_max)); |
| 151 | } |
| 152 | }, |
| 153 | in, out, bias); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 158 | { |
| 159 | // Compute 16 elements per iteration |
| 160 | int x = window_start_x; |
| 161 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 162 | { |
| 163 | int32x4x2_t in_s32 = |
| 164 | { |
| 165 | { |
| 166 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0), |
| 167 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4) |
| 168 | } |
| 169 | }; |
| 170 | |
| 171 | vst1q_s16(reinterpret_cast<int16_t *>(out.ptr()) + x, finalize_quantization_int16<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, min_s16, max_s16)); |
| 172 | } |
| 173 | |
| 174 | // Compute left-over elements |
| 175 | for(; x < window_end_x; ++x) |
| 176 | { |
| 177 | const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x); |
| 178 | ARM_COMPUTE_UNUSED(in_value); |
| 179 | // Finalize and store the result |
| 180 | *(reinterpret_cast<int16_t *>(out.ptr()) + x) = finalize_quantization_int16<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, static_cast<int16_t>(_min), |
| 181 | static_cast<int16_t>(_max)); |
| 182 | } |
| 183 | }, |
| 184 | in, out); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel() |
| 189 | : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _min(0), _max(0) |
| 190 | { |
| 191 | } |
| 192 | |
| 193 | void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, |
| 194 | int min, int max) |
| 195 | { |
| 196 | // Perform validate step |
| 197 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 198 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max)); |
| 199 | |
| 200 | _input = input; |
| 201 | _bias = bias; |
| 202 | _output = output; |
| 203 | _result_fixedpoint_multiplier = result_fixedpoint_multiplier; |
| 204 | _result_shift = result_shift; |
| 205 | _min = min; |
| 206 | _max = max; |
| 207 | |
| 208 | // Configure kernel window |
| 209 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 210 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 211 | INEKernel::configure(win_config.second); |
| 212 | |
| 213 | // Check if we need to clamp the result using min and max |
Giorgio Arena | 1856ff7 | 2020-02-07 13:46:45 +0000 | [diff] [blame] | 214 | const bool is_bounded_relu = !(min <= -32768 && max >= 32767); |
Gian Marco Iodice | bc415af | 2019-06-13 15:58:32 +0100 | [diff] [blame] | 215 | _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run<false>; |
| 216 | } |
| 217 | |
| 218 | Status NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
| 219 | { |
| 220 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 221 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max)); |
| 222 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
| 223 | |
| 224 | return Status{}; |
| 225 | } |
| 226 | |
| 227 | void NEGEMMLowpQuantizeDownInt32ToInt16ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info) |
| 228 | { |
| 229 | ARM_COMPUTE_UNUSED(info); |
| 230 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 231 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 232 | |
| 233 | (this->*_func)(window); |
| 234 | } |
| 235 | } // namespace arm_compute |