Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 1 | /* |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 ARM Limited. |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +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 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEQuantizationLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/NEON/NEAsymm.h" |
| 29 | #include "arm_compute/core/NEON/wrapper/wrapper.h" |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 34 | #include "arm_compute/core/CPP/Validate.h" |
| 35 | |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 36 | #include <arm_neon.h> |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 37 | #include <map> |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 38 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 43 | constexpr auto window_step = 16; |
| 44 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 46 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0); |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QASYMM16); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 53 | |
| 54 | return Status{}; |
| 55 | } |
| 56 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 57 | inline float32x4x4_t load_value(const float *input_ptr) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 58 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 59 | return { wrapper::vloadq(input_ptr), |
| 60 | wrapper::vloadq(input_ptr + 4), |
| 61 | wrapper::vloadq(input_ptr + 8), |
| 62 | wrapper::vloadq(input_ptr + 12) }; |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 63 | } |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 64 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 65 | inline const float32x4x4_t load_value(const float16_t *input_ptr) |
| 66 | { |
| 67 | return { vcvt_f32_f16(wrapper::vload(input_ptr)), |
| 68 | vcvt_f32_f16(wrapper::vload(input_ptr + 4)), |
| 69 | vcvt_f32_f16(wrapper::vload(input_ptr + 8)), |
| 70 | vcvt_f32_f16(wrapper::vload(input_ptr + 12)) }; |
| 71 | } |
| 72 | |
| 73 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 74 | |
| 75 | template <typename element_type> |
| 76 | using vector_type = wrapper::traits::neon_vector_t<element_type, window_step>; |
| 77 | |
| 78 | template <typename quantized_type> |
| 79 | vector_type<quantized_type> vquantize_qasymm8(const float32x4x4_t &qv, const UniformQuantizationInfo &qi); |
| 80 | |
| 81 | template <> |
| 82 | vector_type<uint8_t> vquantize_qasymm8<uint8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 83 | { |
| 84 | return vquantize(qv, qi); |
| 85 | } |
| 86 | |
| 87 | template <> |
| 88 | vector_type<int8_t> vquantize_qasymm8<int8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi) |
| 89 | { |
| 90 | return vquantize_signed(qv, qi); |
| 91 | } |
| 92 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 93 | } // namespace |
| 94 | |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 95 | NEQuantizationLayerKernel::NEQuantizationLayerKernel() |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 96 | : _input(nullptr), _output(nullptr), _func(nullptr) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 97 | { |
| 98 | } |
| 99 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 100 | void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 101 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 102 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 103 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 104 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 105 | _input = input; |
| 106 | _output = output; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 107 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 108 | static std::map<DataType, QuantizationFunctionExecutorPtr> quant_map_f32 = |
| 109 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 110 | { DataType::QASYMM8, &NEQuantizationLayerKernel::run_quantize_qasymm8<float, uint8_t> }, |
| 111 | { DataType::QASYMM8_SIGNED, &NEQuantizationLayerKernel::run_quantize_qasymm8<float, int8_t> }, |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 112 | { DataType::QASYMM16, &NEQuantizationLayerKernel::run_quantize_qasymm16<float> }, |
| 113 | }; |
| 114 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 115 | static std::map<DataType, QuantizationFunctionExecutorPtr> quant_map_f16 = |
| 116 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 117 | { DataType::QASYMM8, &NEQuantizationLayerKernel::run_quantize_qasymm8<float16_t, uint8_t> }, |
| 118 | { DataType::QASYMM8_SIGNED, &NEQuantizationLayerKernel::run_quantize_qasymm8<float16_t, int8_t> }, |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 119 | { DataType::QASYMM16, &NEQuantizationLayerKernel::run_quantize_qasymm16<float16_t> }, |
| 120 | }; |
| 121 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/ |
| 122 | |
| 123 | switch(input->info()->data_type()) |
| 124 | { |
| 125 | case DataType::F32: |
| 126 | _func = quant_map_f32[output->info()->data_type()]; |
| 127 | break; |
| 128 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 129 | case DataType::F16: |
| 130 | _func = quant_map_f16[output->info()->data_type()]; |
| 131 | break; |
| 132 | #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */ |
| 133 | default: |
| 134 | ARM_COMPUTE_ERROR("Unsupported input data type."); |
| 135 | } |
| 136 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 137 | // Configure kernel window |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 138 | Window win_config = calculate_max_window(*input->info(), Steps()); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 139 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 140 | Coordinates coord; |
| 141 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 142 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 143 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 144 | INEKernel::configure(win_config); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 145 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 146 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 147 | Status NEQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 148 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 149 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 150 | return Status{}; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 151 | } |
| 152 | |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 153 | template <typename TIn, typename TOut> |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 154 | void NEQuantizationLayerKernel::run_quantize_qasymm8(const Window &window) |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 155 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 156 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 157 | const auto window_end_x = static_cast<int>(window.x().end()); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 158 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 159 | const UniformQuantizationInfo uqinfo = _output->info()->quantization_info().uniform(); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 160 | #ifdef __aarch64__ |
| 161 | constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN; |
| 162 | #else //__aarch64__ |
| 163 | constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO; |
| 164 | #endif //__aarch64__ |
| 165 | |
| 166 | // Collapse window and reset first dimension to handle tail calculations manually |
| 167 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 168 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 169 | |
| 170 | Iterator input(_input, win_collapsed); |
| 171 | Iterator output(_output, win_collapsed); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 172 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 173 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 174 | auto input_ptr = reinterpret_cast<const TIn *>(input.ptr()); |
| 175 | auto output_ptr = reinterpret_cast<TOut *>(output.ptr()); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 176 | |
| 177 | int x = window_start_x; |
| 178 | for(; x <= (window_end_x - window_step); x += window_step) |
| 179 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 180 | wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo)); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 181 | } |
| 182 | // Compute left-over elements |
| 183 | for(; x < window_end_x; ++x) |
| 184 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 185 | output_ptr[x] = Qasymm8QuantizationHelper<TOut>::quantize(input_ptr[x], uqinfo, rounding_policy); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 186 | } |
| 187 | }, |
| 188 | input, output); |
| 189 | } |
| 190 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 191 | template <typename T> |
| 192 | void NEQuantizationLayerKernel::run_quantize_qasymm16(const Window &window) |
| 193 | { |
Sang-Hoon Park | fb6aaeb | 2019-11-27 15:26:44 +0000 | [diff] [blame] | 194 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 195 | const auto window_end_x = static_cast<int>(window.x().end()); |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 196 | |
| 197 | const UniformQuantizationInfo uqinfo = _output->info()->quantization_info().uniform(); |
| 198 | #ifdef __aarch64__ |
| 199 | constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN; |
| 200 | #else //__aarch64__ |
| 201 | constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO; |
| 202 | #endif //__aarch64__ |
| 203 | |
| 204 | // Collapse window and reset first dimension to handle tail calculations manually |
| 205 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 206 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 207 | |
| 208 | Iterator input(_input, win_collapsed); |
| 209 | Iterator output(_output, win_collapsed); |
| 210 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 211 | { |
| 212 | auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 213 | auto output_ptr = reinterpret_cast<uint16_t *>(output.ptr()); |
| 214 | |
| 215 | int x = window_start_x; |
| 216 | for(; x <= (window_end_x - window_step); x += window_step) |
| 217 | { |
| 218 | uint16x8x2_t tmp = vquantize_qasymm16(load_value(&input_ptr[x]), uqinfo); |
| 219 | vst1q_u16(&output_ptr[x], tmp.val[0]); |
| 220 | vst1q_u16(&output_ptr[x + 8], tmp.val[1]); |
| 221 | } |
| 222 | // Compute left-over elements |
| 223 | for(; x < window_end_x; ++x) |
| 224 | { |
| 225 | output_ptr[x] = quantize_qasymm16(input_ptr[x], uqinfo, rounding_policy); |
| 226 | } |
| 227 | }, |
| 228 | input, output); |
| 229 | } |
| 230 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 231 | void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 232 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 233 | ARM_COMPUTE_UNUSED(info); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 234 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 235 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 236 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 237 | |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 238 | (this->*_func)(window); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 239 | } |
Michele Di Giorgio | d64a46c | 2019-10-01 12:25:49 +0100 | [diff] [blame] | 240 | } // namespace arm_compute |