Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel.h" |
| 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" |
| 30 | #include "arm_compute/core/NEON/NEAsymm.h" |
Georgios Pinitas | 932491f | 2018-09-21 16:33:15 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/Types.h" |
| 33 | #include "arm_compute/core/Utils.h" |
| 34 | #include "arm_compute/core/Validate.h" |
| 35 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 37 | |
| 38 | #include <arm_neon.h> |
| 39 | #include <cstddef> |
| 40 | #include <cstdint> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | namespace |
| 45 | { |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 47 | { |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::S32); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(max > 255); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON(min < 0 || min > max); |
| 51 | |
| 52 | // Check biases if exist |
| 53 | if(bias != nullptr) |
| 54 | { |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, bias); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(bias->num_dimensions() > 1); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) != bias->dimension(0)); |
| 58 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 59 | |
| 60 | if(output->total_size() != 0) |
| 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, input); |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 64 | } |
| 65 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 66 | return Status{}; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 69 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 70 | { |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 71 | // Output auto inizialitation if not yet initialized |
| 72 | auto_init_if_empty(*output, input->clone()->set_data_type(DataType::QASYMM8)); |
| 73 | |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 74 | // Configure kernel window |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 75 | Window win = calculate_max_window(*input, Steps()); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 76 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 77 | // NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel doesn't need padding so update_window_and_padding() can be skipped |
| 78 | Coordinates coord; |
| 79 | coord.set_num_dimensions(output->num_dimensions()); |
| 80 | output->set_valid_region(ValidRegion(coord, output->tensor_shape())); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 81 | |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 82 | return std::make_pair(Status{}, win); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 83 | } |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 84 | } // namespace |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 85 | |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 86 | namespace arm_compute |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 87 | { |
Georgios Pinitas | f72f936 | 2018-01-12 16:29:45 +0000 | [diff] [blame] | 88 | class Coordinates; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 89 | } // namespace arm_compute |
| 90 | |
| 91 | template <bool is_bounded_relu> |
| 92 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window) |
| 93 | { |
| 94 | const int32x4_t result_offset_after_shift_s32 = vdupq_n_s32(_result_offset_after_shift); |
| 95 | const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min)); |
| 96 | const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max)); |
| 97 | |
| 98 | ARM_COMPUTE_UNUSED(min_u8); |
| 99 | ARM_COMPUTE_UNUSED(max_u8); |
| 100 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 101 | const int window_step_x = 16; |
| 102 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 103 | const auto window_end_x = static_cast<int>(window.x().end()); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 104 | |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 105 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 106 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 107 | |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 108 | Iterator in(_input, win_collapsed); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 109 | Iterator out(_output, win_collapsed); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 110 | if(_bias != nullptr) |
| 111 | { |
| 112 | Window win_biases; |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 113 | win_biases.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 114 | win_biases.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 115 | |
| 116 | Iterator bias(_bias, win_biases); |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 117 | execute_window_loop(win_collapsed, [&](const Coordinates & id) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 118 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 119 | // Compute 16 elements per iteration |
| 120 | int x = window_start_x; |
| 121 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 122 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 123 | int32x4x4_t in_s32 = |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 124 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 125 | { |
| 126 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0), |
| 127 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4), |
| 128 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8), |
| 129 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12) |
| 130 | } |
| 131 | }; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 132 | |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 133 | const int32x4x4_t bias_s32 = |
| 134 | { |
| 135 | { |
| 136 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0), |
| 137 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4), |
| 138 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8), |
| 139 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12) |
| 140 | } |
| 141 | }; |
| 142 | |
| 143 | // Add the bias to GEMM's result |
| 144 | in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]); |
| 145 | in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]); |
| 146 | in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]); |
| 147 | in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]); |
| 148 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 149 | vst1q_u8(out.ptr() + x, finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8)); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // Compute left-over elements |
| 153 | for(; x < window_end_x; ++x) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 154 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 155 | const int32_t bias_value = *(reinterpret_cast<const int32_t *>(bias.ptr()) + x); |
| 156 | int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 157 | |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 158 | // Add bias |
| 159 | in_value += bias_value; |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 160 | // Finalize and store the result |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 161 | *(out.ptr() + x) = finalize_quantization<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max)); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 162 | } |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 163 | }, |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 164 | in, out, bias); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 165 | } |
| 166 | else |
| 167 | { |
Georgios Pinitas | 041f36d | 2018-09-18 18:38:37 +0100 | [diff] [blame] | 168 | execute_window_loop(win_collapsed, [&](const Coordinates & id) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 169 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 170 | // Compute 16 elements per iteration |
| 171 | int x = window_start_x; |
| 172 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 173 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 174 | int32x4x4_t in_s32 = |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 175 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 176 | { |
| 177 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0), |
| 178 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4), |
| 179 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8), |
| 180 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12) |
| 181 | } |
| 182 | }; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 183 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 184 | vst1q_u8(out.ptr() + x, finalize_quantization<is_bounded_relu>(in_s32, _result_fixedpoint_multiplier, _result_shift, result_offset_after_shift_s32, min_u8, max_u8)); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Compute left-over elements |
| 188 | for(; x < window_end_x; ++x) |
| 189 | { |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 190 | const int32_t in_value = *(reinterpret_cast<const int32_t *>(in.ptr()) + x); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 191 | |
| 192 | // Finalize and store the result |
George Wort | 2d7e683 | 2019-02-22 16:37:41 +0000 | [diff] [blame] | 193 | *(out.ptr() + x) = finalize_quantization<is_bounded_relu>(in_value, _result_fixedpoint_multiplier, _result_shift, _result_offset_after_shift, static_cast<uint8_t>(_min), static_cast<uint8_t>(_max)); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 194 | } |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 195 | }, |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 196 | in, out); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
| 200 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel() |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 201 | : _func(nullptr), _input(nullptr), _bias(nullptr), _output(nullptr), _result_fixedpoint_multiplier(0), _result_shift(0), _result_offset_after_shift(0), _min(0), _max(0) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 202 | { |
| 203 | } |
| 204 | |
| 205 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::configure(const ITensor *input, const ITensor *bias, ITensor *output, int result_fixedpoint_multiplier, int result_shift, |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 206 | int result_offset_after_shift, int min, int max) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 207 | { |
| 208 | // Perform validate step |
| 209 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 210 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info(), min, max)); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 211 | |
| 212 | _input = input; |
| 213 | _bias = bias; |
| 214 | _output = output; |
| 215 | _result_fixedpoint_multiplier = result_fixedpoint_multiplier; |
| 216 | _result_shift = result_shift; |
| 217 | _result_offset_after_shift = result_offset_after_shift; |
| 218 | _min = min; |
| 219 | _max = max; |
| 220 | |
| 221 | // Configure kernel window |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 222 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 223 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 224 | INEKernel::configure(win_config.second); |
| 225 | |
| 226 | // Check if we need to clamp the result using min and max |
| 227 | const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255)); |
| 228 | _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run<false>; |
| 229 | } |
| 230 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 231 | Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 232 | { |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 233 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 234 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max)); |
Georgios Pinitas | 5a59453 | 2018-12-03 14:30:05 +0000 | [diff] [blame] | 235 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 236 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 237 | return Status{}; |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleByFixedPointKernel::run(const Window &window, const ThreadInfo &info) |
| 241 | { |
| 242 | ARM_COMPUTE_UNUSED(info); |
| 243 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 244 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 245 | |
| 246 | (this->*_func)(window); |
| 247 | } |