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