Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 995f552 | 2018-01-29 13:43:35 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Utils.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
| 32 | |
| 33 | #include <arm_neon.h> |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame^] | 37 | namespace |
| 38 | { |
| 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) |
| 40 | { |
| 41 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output, min_max); |
| 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() < 3); |
| 44 | |
| 45 | if(output->tensor_shape().total_size() > 0) |
| 46 | { |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 49 | } |
| 50 | |
| 51 | return Status{}; |
| 52 | } |
| 53 | |
| 54 | std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max) |
| 55 | { |
| 56 | // Output tensor auto initialization if not yet initialized |
| 57 | auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::U8, 0); |
| 58 | |
| 59 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 60 | |
| 61 | // Configure window |
| 62 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration)); |
| 63 | AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration); |
| 64 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 65 | AccessWindowStatic min_max_access(min_max, 0, 0, 2, min_max->dimension(1)); |
| 66 | |
| 67 | // Update window and padding |
| 68 | bool window_changed = update_window_and_padding(win, input_access, output_access, min_max_access); |
| 69 | |
| 70 | output_access.set_valid_region(win, input->valid_region()); |
| 71 | |
| 72 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 73 | return std::make_tuple(err, win); |
| 74 | } |
| 75 | } // namespace |
| 76 | |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 77 | NEQuantizationLayerKernel::NEQuantizationLayerKernel() |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 78 | : _input(nullptr), _output(nullptr), _min_max(nullptr) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 79 | { |
| 80 | } |
| 81 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 82 | void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 83 | { |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame^] | 84 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, min_max); |
| 85 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), min_max->info())); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 86 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 87 | _input = input; |
| 88 | _output = output; |
| 89 | _min_max = min_max; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 90 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame^] | 91 | // Configure kernel window |
| 92 | auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info()); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 93 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame^] | 94 | ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config)); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 95 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame^] | 96 | INEKernel::configure(std::get<1>(win_config)); |
| 97 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 98 | |
Alex Gilday | 60954c6 | 2018-03-05 16:22:48 +0000 | [diff] [blame^] | 99 | Status NEQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const ITensorInfo *min_max) |
| 100 | { |
| 101 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, min_max)); |
| 102 | ARM_COMPUTE_RETURN_ON_ERROR(std::get<0>(validate_and_configure_window(input->clone().get(), output->clone().get(), min_max->clone().get()))); |
| 103 | |
| 104 | return Status{}; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 105 | } |
| 106 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 107 | void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 108 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 109 | ARM_COMPUTE_UNUSED(info); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 110 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 111 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 112 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 113 | Window window_input_output(window); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 114 | window_input_output.set(3, Window::Dimension(0, 1, 1)); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 115 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 116 | Window window_min_max; |
| 117 | window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape()); |
| 118 | window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 119 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 120 | Iterator input(_input, window_input_output); |
| 121 | Iterator output(_output, window_input_output); |
| 122 | Iterator min_max(_min_max, window_min_max); |
| 123 | |
| 124 | execute_window_loop(window_min_max, [&](const Coordinates & id_batch) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 125 | { |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 126 | // Get the min and max |
| 127 | float min = *(reinterpret_cast<const float *>(min_max.ptr()) + 0); |
| 128 | float max = *(reinterpret_cast<const float *>(min_max.ptr()) + 1); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 129 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 130 | // Saturate the result if min = max |
| 131 | if(min == max) |
| 132 | { |
| 133 | min = 0.0f; |
| 134 | max = 1.0f; |
| 135 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 136 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 137 | const float32x4_t vmin = vdupq_n_f32(min); |
| 138 | const float32x4_t inv_range = vdupq_n_f32(1.0f / (max - min)); |
| 139 | const float32x4_t quantization_max = vdupq_n_f32(255.0f); |
| 140 | const float32x4_t quantization_mul = vdupq_n_f32(256.0f); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 141 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 142 | // Uniformly map values to range 8bit integers, i.e. [min, max] -> [0, 255] |
| 143 | execute_window_loop(window_input_output, [&](const Coordinates & id) |
| 144 | { |
| 145 | // Get the input values |
| 146 | const auto input_ptr = reinterpret_cast<const float *>(input.ptr() + id_batch[1] * _input->info()->strides_in_bytes()[3]); |
| 147 | float32x4x2_t val = vld2q_f32(input_ptr); |
| 148 | |
| 149 | // Map float values to range [0.0, 1.0] |
| 150 | val.val[0] = vsubq_f32(val.val[0], vmin); |
| 151 | val.val[1] = vsubq_f32(val.val[1], vmin); |
| 152 | val.val[0] = vmulq_f32(val.val[0], inv_range); |
| 153 | val.val[1] = vmulq_f32(val.val[1], inv_range); |
| 154 | |
| 155 | // Quantize |
| 156 | val.val[0] = vmulq_f32(val.val[0], quantization_mul); |
| 157 | val.val[1] = vmulq_f32(val.val[1], quantization_mul); |
| 158 | val.val[0] = vminq_f32(val.val[0], quantization_max); |
| 159 | val.val[1] = vminq_f32(val.val[1], quantization_max); |
| 160 | |
| 161 | const uint32x4_t val_u32_low = vcvtq_u32_f32(val.val[0]); |
| 162 | const uint32x4_t val_u32_high = vcvtq_u32_f32(val.val[1]); |
| 163 | const uint16x4x2_t val_u16 = vzip_u16(vmovn_u32(val_u32_low), vmovn_u32(val_u32_high)); |
| 164 | |
| 165 | const uint8x8_t quantized = vmovn_u16(vcombine_u16(val_u16.val[0], val_u16.val[1])); |
| 166 | |
| 167 | // Store the quantized values |
| 168 | auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr() + id_batch[1] * _output->info()->strides_in_bytes()[3]); |
| 169 | vst1_u8(output_ptr, quantized); |
| 170 | }, |
| 171 | input, output); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 172 | }, |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 173 | min_max); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 174 | } |