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> |
| 37 | |
| 38 | using namespace arm_compute; |
| 39 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 40 | namespace |
| 41 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 43 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 50 | |
| 51 | return Status{}; |
| 52 | } |
| 53 | |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 54 | inline float32x4x4_t load_value(const float *input_ptr) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 55 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 56 | return { wrapper::vloadq(input_ptr), |
| 57 | wrapper::vloadq(input_ptr + 4), |
| 58 | wrapper::vloadq(input_ptr + 8), |
| 59 | wrapper::vloadq(input_ptr + 12) }; |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 60 | } |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 61 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 62 | inline const float32x4x4_t load_value(const float16_t *input_ptr) |
| 63 | { |
| 64 | return { vcvt_f32_f16(wrapper::vload(input_ptr)), |
| 65 | vcvt_f32_f16(wrapper::vload(input_ptr + 4)), |
| 66 | vcvt_f32_f16(wrapper::vload(input_ptr + 8)), |
| 67 | vcvt_f32_f16(wrapper::vload(input_ptr + 12)) }; |
| 68 | } |
| 69 | |
| 70 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 71 | } // namespace |
| 72 | |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 73 | NEQuantizationLayerKernel::NEQuantizationLayerKernel() |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 74 | : _input(nullptr), _output(nullptr) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 75 | { |
| 76 | } |
| 77 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 78 | void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 79 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 80 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 81 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 82 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 83 | _input = input; |
| 84 | _output = output; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 85 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 86 | // Configure kernel window |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 87 | Window win_config = calculate_max_window(*input->info(), Steps()); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 88 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 89 | Coordinates coord; |
| 90 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 91 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 92 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 93 | INEKernel::configure(win_config); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 94 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 95 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 96 | Status NEQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 97 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 98 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output)); |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame] | 99 | |
| 100 | return Status{}; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 101 | } |
| 102 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 103 | template <typename T> |
| 104 | void NEQuantizationLayerKernel::quantize(const Window &window, const QuantizationInfo &qinfo) |
| 105 | { |
| 106 | constexpr auto window_step = 16; |
| 107 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 108 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 109 | |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 110 | const UniformQuantizationInfo uqinfo = qinfo.uniform(); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 111 | #ifdef __aarch64__ |
| 112 | constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN; |
| 113 | #else //__aarch64__ |
| 114 | constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO; |
| 115 | #endif //__aarch64__ |
| 116 | |
| 117 | // Collapse window and reset first dimension to handle tail calculations manually |
| 118 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 119 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 120 | |
| 121 | Iterator input(_input, win_collapsed); |
| 122 | Iterator output(_output, win_collapsed); |
Michalis Spyrou | a4f378d | 2019-04-26 14:54:54 +0100 | [diff] [blame] | 123 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 124 | { |
| 125 | auto input_ptr = reinterpret_cast<const T *>(input.ptr()); |
| 126 | auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 127 | |
| 128 | int x = window_start_x; |
| 129 | for(; x <= (window_end_x - window_step); x += window_step) |
| 130 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 131 | wrapper::vstore(&output_ptr[x], vquantize(load_value(&input_ptr[x]), uqinfo)); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 132 | } |
| 133 | // Compute left-over elements |
| 134 | for(; x < window_end_x; ++x) |
| 135 | { |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 136 | output_ptr[x] = quantize_qasymm8(input_ptr[x], uqinfo, rounding_policy); |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 137 | } |
| 138 | }, |
| 139 | input, output); |
| 140 | } |
| 141 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 142 | void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 143 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_UNUSED(info); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 145 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 146 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 147 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 148 | const QuantizationInfo &qinfo = _output->info()->quantization_info(); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 149 | |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 150 | switch(_input->info()->data_type()) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 151 | { |
John Kesapides | adfb273 | 2019-03-04 16:29:22 +0000 | [diff] [blame] | 152 | case DataType::F32: |
| 153 | NEQuantizationLayerKernel::quantize<float>(window, qinfo); |
| 154 | break; |
| 155 | #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 156 | case DataType::F16: |
| 157 | NEQuantizationLayerKernel::quantize<float16_t>(window, qinfo); |
| 158 | break; |
| 159 | #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC |
| 160 | default: |
| 161 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 162 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 163 | } |