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