blob: b49400ab7d87eb552ee4223b189c7488cbd0e8a8 [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01001/*
Michalis Spyrou995f5522018-01-29 13:43:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01003 *
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 Iodice06b184a2017-08-29 16:05:25 +010026#include "arm_compute/core/AccessWindowStatic.h"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010029#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
35using namespace arm_compute;
36
Alex Gilday60954c62018-03-05 16:22:48 +000037namespace
38{
39Status 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
54std::tuple<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, ITensorInfo *min_max)
55{
56 // Output tensor auto initialization if not yet initialized
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010057 auto_init_if_empty(*output, input->tensor_shape(), 1, DataType::U8);
Alex Gilday60954c62018-03-05 16:22:48 +000058
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 Giorgio4e09b382017-07-05 18:20:02 +010077NEQuantizationLayerKernel::NEQuantizationLayerKernel()
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010078 : _input(nullptr), _output(nullptr), _min_max(nullptr)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010079{
80}
81
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010082void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010083{
Alex Gilday60954c62018-03-05 16:22:48 +000084 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 Giorgio4e09b382017-07-05 18:20:02 +010086
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010087 _input = input;
88 _output = output;
89 _min_max = min_max;
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010090
Alex Gilday60954c62018-03-05 16:22:48 +000091 // Configure kernel window
92 auto win_config = validate_and_configure_window(input->info(), output->info(), min_max->info());
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010093
Alex Gilday60954c62018-03-05 16:22:48 +000094 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010095
Alex Gilday60954c62018-03-05 16:22:48 +000096 INEKernel::configure(std::get<1>(win_config));
97}
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010098
Alex Gilday60954c62018-03-05 16:22:48 +000099Status 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 Giorgio4e09b382017-07-05 18:20:02 +0100105}
106
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100107void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100108{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100109 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100110 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
111 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
112
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100113 Window window_input_output(window);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100114 window_input_output.set(3, Window::Dimension(0, 1, 1));
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100115
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100116 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 Giorgio4e09b382017-07-05 18:20:02 +0100119
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100120 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 Giorgio4e09b382017-07-05 18:20:02 +0100125 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100126 // 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 Giorgio4e09b382017-07-05 18:20:02 +0100129
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100130 // Saturate the result if min = max
131 if(min == max)
132 {
133 min = 0.0f;
134 max = 1.0f;
135 }
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100136
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100137 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 Giorgio4e09b382017-07-05 18:20:02 +0100141
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100142 // 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 Giorgio4e09b382017-07-05 18:20:02 +0100172 },
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100173 min_max);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100174}