Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [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/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 | |
| 37 | NEQuantizationLayerKernel::NEQuantizationLayerKernel() |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 38 | : _input(nullptr), _output(nullptr), _min_max(nullptr) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 39 | { |
| 40 | } |
| 41 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 42 | 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] | 43 | { |
| 44 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32); |
| 45 | ARM_COMPUTE_ERROR_ON_NULLPTR(output); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 47 | |
| 48 | // Output tensor auto initialization if not yet initialized |
| 49 | auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::U8, 0); |
| 50 | |
| 51 | ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::U8); |
| 52 | ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 53 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 54 | _input = input; |
| 55 | _output = output; |
| 56 | _min_max = min_max; |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 57 | |
| 58 | constexpr unsigned int num_elems_processed_per_iteration = 8; |
| 59 | |
| 60 | // Configure window |
| 61 | Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration)); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 62 | AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 63 | AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration); |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 64 | AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1)); |
| 65 | |
| 66 | // Update window and padding |
| 67 | update_window_and_padding(win, input_access, output_access, min_max_access); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 68 | output_access.set_valid_region(win, input->info()->valid_region()); |
| 69 | |
| 70 | INEKernel::configure(win); |
| 71 | } |
| 72 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 73 | void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 74 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 75 | ARM_COMPUTE_UNUSED(info); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 77 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 78 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 79 | Window window_input_output(window); |
| 80 | window_input_output.collapse_if_possible(INEKernel::window(), 3); |
| 81 | window_input_output.set(3, Window::Dimension(0, 1, 1)); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 82 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 83 | Window window_min_max; |
| 84 | window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape()); |
| 85 | window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 86 | window_min_max.collapse_if_possible(INEKernel::window(), 1); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 87 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 88 | Iterator input(_input, window_input_output); |
| 89 | Iterator output(_output, window_input_output); |
| 90 | Iterator min_max(_min_max, window_min_max); |
| 91 | |
| 92 | execute_window_loop(window_min_max, [&](const Coordinates & id_batch) |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 93 | { |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 94 | // Get the min and max |
| 95 | float min = *(reinterpret_cast<const float *>(min_max.ptr()) + 0); |
| 96 | float max = *(reinterpret_cast<const float *>(min_max.ptr()) + 1); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 97 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 98 | // Saturate the result if min = max |
| 99 | if(min == max) |
| 100 | { |
| 101 | min = 0.0f; |
| 102 | max = 1.0f; |
| 103 | } |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 104 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 105 | const float32x4_t vmin = vdupq_n_f32(min); |
| 106 | const float32x4_t inv_range = vdupq_n_f32(1.0f / (max - min)); |
| 107 | const float32x4_t quantization_max = vdupq_n_f32(255.0f); |
| 108 | const float32x4_t quantization_mul = vdupq_n_f32(256.0f); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 109 | |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 110 | // Uniformly map values to range 8bit integers, i.e. [min, max] -> [0, 255] |
| 111 | execute_window_loop(window_input_output, [&](const Coordinates & id) |
| 112 | { |
| 113 | // Get the input values |
| 114 | const auto input_ptr = reinterpret_cast<const float *>(input.ptr() + id_batch[1] * _input->info()->strides_in_bytes()[3]); |
| 115 | float32x4x2_t val = vld2q_f32(input_ptr); |
| 116 | |
| 117 | // Map float values to range [0.0, 1.0] |
| 118 | val.val[0] = vsubq_f32(val.val[0], vmin); |
| 119 | val.val[1] = vsubq_f32(val.val[1], vmin); |
| 120 | val.val[0] = vmulq_f32(val.val[0], inv_range); |
| 121 | val.val[1] = vmulq_f32(val.val[1], inv_range); |
| 122 | |
| 123 | // Quantize |
| 124 | val.val[0] = vmulq_f32(val.val[0], quantization_mul); |
| 125 | val.val[1] = vmulq_f32(val.val[1], quantization_mul); |
| 126 | val.val[0] = vminq_f32(val.val[0], quantization_max); |
| 127 | val.val[1] = vminq_f32(val.val[1], quantization_max); |
| 128 | |
| 129 | const uint32x4_t val_u32_low = vcvtq_u32_f32(val.val[0]); |
| 130 | const uint32x4_t val_u32_high = vcvtq_u32_f32(val.val[1]); |
| 131 | const uint16x4x2_t val_u16 = vzip_u16(vmovn_u32(val_u32_low), vmovn_u32(val_u32_high)); |
| 132 | |
| 133 | const uint8x8_t quantized = vmovn_u16(vcombine_u16(val_u16.val[0], val_u16.val[1])); |
| 134 | |
| 135 | // Store the quantized values |
| 136 | auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr() + id_batch[1] * _output->info()->strides_in_bytes()[3]); |
| 137 | vst1_u8(output_ptr, quantized); |
| 138 | }, |
| 139 | input, output); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 140 | }, |
Gian Marco Iodice | 06b184a | 2017-08-29 16:05:25 +0100 | [diff] [blame] | 141 | min_max); |
Michele Di Giorgio | 4e09b38 | 2017-07-05 18:20:02 +0100 | [diff] [blame] | 142 | } |