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 | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 43 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 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 | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 63 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 64 | } |
| 65 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 66 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *bias, ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 67 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 68 | // Note: This kernel performs 16 elements per iteration. |
| 69 | // However, since we use a left-over for loop, we cannot have any read or write out of memory |
| 70 | // For this reason num_elems_processed_per_iteration is set to 1 |
| 71 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 72 | |
| 73 | // Configure kernel window |
| 74 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 75 | |
| 76 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 77 | |
| 78 | bool window_changed = update_window_and_padding(win, |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 79 | input_access); |
| 80 | |
| 81 | if(output->total_size() != 0) |
| 82 | { |
| 83 | AccessWindowHorizontal output_result_access(output, 0, num_elems_processed_per_iteration); |
| 84 | window_changed = window_changed || update_window_and_padding(win, output_result_access); |
| 85 | |
| 86 | output_result_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 87 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 88 | |
| 89 | if(bias != nullptr) |
| 90 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 91 | AccessWindowStatic bias_access(bias, 0, 0, bias->dimension(0), bias->dimension(1)); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 92 | window_changed = window_changed || update_window_and_padding(win, bias_access); |
| 93 | } |
| 94 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 95 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 96 | return std::make_pair(err, win); |
| 97 | } |
| 98 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 99 | inline void scale_input(int32x4x4_t &in_s32, int32x4_t result_offset_s32, int32_t result_mult_int) |
| 100 | { |
| 101 | // Add the offset terms to GEMM's result |
| 102 | in_s32.val[0] = vaddq_s32(in_s32.val[0], result_offset_s32); |
| 103 | in_s32.val[1] = vaddq_s32(in_s32.val[1], result_offset_s32); |
| 104 | in_s32.val[2] = vaddq_s32(in_s32.val[2], result_offset_s32); |
| 105 | in_s32.val[3] = vaddq_s32(in_s32.val[3], result_offset_s32); |
| 106 | |
| 107 | // Multiply by result_mult_int |
| 108 | in_s32.val[0] = vmulq_n_s32(in_s32.val[0], result_mult_int); |
| 109 | in_s32.val[1] = vmulq_n_s32(in_s32.val[1], result_mult_int); |
| 110 | in_s32.val[2] = vmulq_n_s32(in_s32.val[2], result_mult_int); |
| 111 | in_s32.val[3] = vmulq_n_s32(in_s32.val[3], result_mult_int); |
| 112 | } |
| 113 | |
| 114 | template <bool is_bounded_relu> |
| 115 | inline uint8x16_t finalize_quantization(int32x4x4_t &in_s32, int32x4_t result_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8) |
| 116 | { |
| 117 | const static int32x4_t zero_s32 = vdupq_n_s32(0); |
| 118 | |
| 119 | // Shift final result (negative value shift right) |
| 120 | in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32); |
| 121 | in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32); |
| 122 | in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32); |
| 123 | in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32); |
| 124 | |
| 125 | // Saturate negative values |
| 126 | in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32); |
| 127 | in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32); |
| 128 | in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32); |
| 129 | in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32); |
| 130 | |
| 131 | // Convert S32 to S16 |
| 132 | const int16x8x2_t in_s16 = |
| 133 | { |
| 134 | { |
| 135 | vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])), |
| 136 | vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3])) |
| 137 | } |
| 138 | }; |
| 139 | |
| 140 | // Convert S16 to U8 |
| 141 | uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1])); |
| 142 | |
| 143 | if(is_bounded_relu) |
| 144 | { |
| 145 | out_u8 = vmaxq_u8(out_u8, min_u8); |
| 146 | out_u8 = vminq_u8(out_u8, max_u8); |
| 147 | } |
| 148 | |
| 149 | return out_u8; |
| 150 | } |
| 151 | } // namespace |
| 152 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 153 | namespace arm_compute |
| 154 | { |
| 155 | class Coordinates; |
| 156 | } // namespace arm_compute |
| 157 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 158 | template <bool is_bounded_relu> |
| 159 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window) |
| 160 | { |
| 161 | const int32x4_t result_offset_s32 = vdupq_n_s32(_result_offset); |
| 162 | const int32x4_t result_shift_s32 = vdupq_n_s32(-_result_shift); |
| 163 | const uint8x16_t min_u8 = vdupq_n_u8(static_cast<uint8_t>(_min)); |
| 164 | const uint8x16_t max_u8 = vdupq_n_u8(static_cast<uint8_t>(_max)); |
| 165 | |
| 166 | ARM_COMPUTE_UNUSED(min_u8); |
| 167 | ARM_COMPUTE_UNUSED(max_u8); |
| 168 | |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 169 | const int window_step_x = 16; |
| 170 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 171 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 172 | |
| 173 | Window win(window); |
| 174 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 175 | |
| 176 | Iterator in(_input, win); |
| 177 | Iterator out(_output, win); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 178 | |
| 179 | if(_bias != nullptr) |
| 180 | { |
| 181 | Window win_biases; |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 182 | win_biases.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 183 | win_biases.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 184 | |
| 185 | Iterator bias(_bias, win_biases); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 186 | execute_window_loop(win, [&](const Coordinates & id) |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 187 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 188 | // Compute 16 elements per iteration |
| 189 | int x = window_start_x; |
| 190 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 191 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 192 | int32x4x4_t in_s32 = |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 193 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 194 | { |
| 195 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0), |
| 196 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4), |
| 197 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8), |
| 198 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12) |
| 199 | } |
| 200 | }; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 201 | |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 202 | const int32x4x4_t bias_s32 = |
| 203 | { |
| 204 | { |
| 205 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 0), |
| 206 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 4), |
| 207 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 8), |
| 208 | vld1q_s32(reinterpret_cast<const int32_t *>(bias.ptr()) + x + 12) |
| 209 | } |
| 210 | }; |
| 211 | |
| 212 | // Add the bias to GEMM's result |
| 213 | in_s32.val[0] = vaddq_s32(in_s32.val[0], bias_s32.val[0]); |
| 214 | in_s32.val[1] = vaddq_s32(in_s32.val[1], bias_s32.val[1]); |
| 215 | in_s32.val[2] = vaddq_s32(in_s32.val[2], bias_s32.val[2]); |
| 216 | in_s32.val[3] = vaddq_s32(in_s32.val[3], bias_s32.val[3]); |
| 217 | |
| 218 | // Add the offset terms to GEMM's result and multiply by result_mult_int |
| 219 | scale_input(in_s32, result_offset_s32, _result_mult_int); |
| 220 | |
| 221 | vst1q_u8(out.ptr() + x, finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8)); |
| 222 | } |
| 223 | |
| 224 | // Compute left-over elements |
| 225 | for(; x < window_end_x; ++x) |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 226 | { |
Anthony Barbier | c2708ec | 2017-12-08 22:24:32 +0000 | [diff] [blame] | 227 | const int bias_value = *(reinterpret_cast<const int *>(bias.ptr()) + x); |
| 228 | int in_value = *(reinterpret_cast<const int *>(in.ptr()) + x); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 229 | |
| 230 | // Quantize |
| 231 | in_value = ((in_value + bias_value + _result_offset) * _result_mult_int) >> _result_shift; |
| 232 | |
| 233 | // Finalize and store the result |
| 234 | if(is_bounded_relu) |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 235 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 236 | *(out.ptr() + x) = static_cast<uint8_t>(std::max(_min, std::min(_max, in_value))); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 237 | } |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 238 | else |
| 239 | { |
| 240 | *(out.ptr() + x) = static_cast<uint8_t>(std::max(0, std::min(255, in_value))); |
| 241 | } |
| 242 | } |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 243 | }, |
| 244 | in, bias, out); |
| 245 | } |
| 246 | else |
| 247 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 248 | execute_window_loop(win, [&](const Coordinates & id) |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 249 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 250 | // Compute 16 elements per iteration |
| 251 | int x = window_start_x; |
| 252 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 253 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 254 | int32x4x4_t in_s32 = |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 255 | { |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 256 | { |
| 257 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 0), |
| 258 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 4), |
| 259 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 8), |
| 260 | vld1q_s32(reinterpret_cast<const int32_t *>(in.ptr()) + x + 12) |
| 261 | } |
| 262 | }; |
| 263 | |
| 264 | // Add the offset terms to GEMM's result and multiply by result_mult_int |
| 265 | scale_input(in_s32, result_offset_s32, _result_mult_int); |
| 266 | |
| 267 | vst1q_u8(out.ptr() + x, finalize_quantization<is_bounded_relu>(in_s32, result_shift_s32, min_u8, max_u8)); |
| 268 | } |
| 269 | |
| 270 | // Compute left-over elements |
| 271 | for(; x < window_end_x; ++x) |
| 272 | { |
Anthony Barbier | c2708ec | 2017-12-08 22:24:32 +0000 | [diff] [blame] | 273 | int in_value = *(reinterpret_cast<const int *>(in.ptr()) + x); |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 274 | |
| 275 | // Quantize |
| 276 | in_value = ((in_value + _result_offset) * _result_mult_int) >> _result_shift; |
| 277 | |
| 278 | // Finalize and store the result |
| 279 | if(is_bounded_relu) |
| 280 | { |
| 281 | *(out.ptr() + x) = static_cast<uint8_t>(std::max(_min, std::min(_max, in_value))); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 282 | } |
Gian Marco | 7f0f790 | 2017-12-07 09:26:56 +0000 | [diff] [blame] | 283 | else |
| 284 | { |
| 285 | *(out.ptr() + x) = static_cast<uint8_t>(std::max(0, std::min(255, in_value))); |
| 286 | } |
| 287 | } |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 288 | }, |
| 289 | in, out); |
| 290 | } |
| 291 | } |
| 292 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 293 | NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel() |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 294 | : _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] | 295 | { |
| 296 | } |
| 297 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 298 | 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] | 299 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 300 | // Perform validate step |
| 301 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Gian Marco | 58c5794 | 2017-11-28 09:10:03 +0000 | [diff] [blame] | 302 | |
| 303 | // Output auto inizialitation if not yet initialized |
| 304 | auto_init_if_empty(*output->info(), input->info()->clone()->set_data_type(DataType::QASYMM8)); |
| 305 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 306 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), |
| 307 | (bias != nullptr) ? bias->info() : nullptr, |
| 308 | output->info(), |
| 309 | min, |
| 310 | max)); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 311 | |
| 312 | _input = input; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 313 | _bias = bias; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 314 | _output = output; |
| 315 | _result_offset = result_offset; |
| 316 | _result_mult_int = result_mult_int; |
| 317 | _result_shift = result_shift; |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 318 | _min = min; |
| 319 | _max = max; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 320 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 321 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 322 | auto win_config = validate_and_configure_window(input->info(), (bias != nullptr) ? bias->info() : nullptr, output->info()); |
| 323 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 324 | INEKernel::configure(win_config.second); |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 325 | |
| 326 | // Check if we need to clamp the result using min and max |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 327 | const bool is_bounded_relu = ((min != max) && !(min == 0 && max == 255)); |
| 328 | _func = is_bounded_relu ? &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<true> : &NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run<false>; |
| 329 | } |
| 330 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 331 | Status NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::validate(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, int min, int max) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 332 | { |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 333 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 334 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, bias, output, min, max)); |
| 335 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), |
| 336 | (bias != nullptr) ? bias->clone().get() : nullptr, |
| 337 | output->clone().get()) |
| 338 | .first); |
| 339 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 340 | return Status{}; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void NEGEMMLowpQuantizeDownInt32ToUint8ScaleKernel::run(const Window &window, const ThreadInfo &info) |
| 344 | { |
| 345 | ARM_COMPUTE_UNUSED(info); |
| 346 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 347 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 348 | |
Gian Marco | 6b77e91 | 2017-11-17 09:27:57 +0000 | [diff] [blame] | 349 | (this->*_func)(window); |
Anthony Barbier | c2708ec | 2017-12-08 22:24:32 +0000 | [diff] [blame] | 350 | } |