blob: 47c895c59467b1bbb9ed35eaf7cd2f72259d9148 [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Michalis Spyrou995f5522018-01-29 13:43:35 +00002 * Copyright (c) 2017-2018 ARM Limited.
Michele Di Giorgio32982d82017-07-07 14:44:43 +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/NEDequantizationLayerKernel.h"
25
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010026#include "arm_compute/core/AccessWindowStatic.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +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::U8);
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::F32);
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::F32);
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 Giorgio32982d82017-07-07 14:44:43 +010077NEDequantizationLayerKernel::NEDequantizationLayerKernel()
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010078 : _input(nullptr), _output(nullptr), _min_max(nullptr)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010079{
80}
81
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010082void NEDequantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max)
Michele Di Giorgio32982d82017-07-07 14:44:43 +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 Giorgio32982d82017-07-07 14:44:43 +010086
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010087 _input = input;
88 _output = output;
89 _min_max = min_max;
Michele Di Giorgio32982d82017-07-07 14:44:43 +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 Giorgio32982d82017-07-07 14:44:43 +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 Giorgio32982d82017-07-07 14:44:43 +010098
Alex Gilday60954c62018-03-05 16:22:48 +000099Status NEDequantizationLayerKernel::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 Giorgio32982d82017-07-07 14:44:43 +0100105}
106
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100107void NEDequantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100108{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100109 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio32982d82017-07-07 14:44:43 +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 Giorgio32982d82017-07-07 14:44:43 +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 Giorgio32982d82017-07-07 14:44:43 +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 Giorgio32982d82017-07-07 14:44:43 +0100125 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100126 // Get the min and max
127 const float min = *(reinterpret_cast<const float *>(min_max.ptr()) + 0);
128 const float max = *(reinterpret_cast<const float *>(min_max.ptr()) + 1);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100129
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100130 const float32x4_t vmin = vdupq_n_f32(min);
131 const float range = max - min;
132 const float32x4_t scaling = vdupq_n_f32(range / 255.0f);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100133
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100134 // Uniformly map values to range 8bit integers, i.e. [min, max] -> [0, 255]
135 execute_window_loop(window_input_output, [&](const Coordinates & id)
136 {
137 // Get the input values
138 const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr() + id_batch[1] * _input->info()->strides_in_bytes()[3]);
139
140 const uint8x8_t val_u8 = vld1_u8(input_ptr);
141 const uint16x8_t val_u16 = vmovl_u8(val_u8);
142 const uint32x4_t val_u32_low = vmovl_u16(vget_low_u16(val_u16));
143 const uint32x4_t val_u32_high = vmovl_u16(vget_high_u16(val_u16));
144 float32x4_t val_low = vcvtq_f32_u32(val_u32_low);
145 float32x4_t val_high = vcvtq_f32_u32(val_u32_high);
146
147 // Dequantize -> (q / 255.0 * range) + min
148 val_low = vmulq_f32(val_low, scaling);
149 val_high = vmulq_f32(val_high, scaling);
150 val_low = vaddq_f32(val_low, vmin);
151 val_high = vaddq_f32(val_high, vmin);
152
153 const float32x4x2_t dequantized = vuzpq_f32(val_low, val_high);
154
155 // Store the dequantized values
156 auto output_ptr = reinterpret_cast<float *>(output.ptr() + id_batch[1] * _output->info()->strides_in_bytes()[3]);
157 vst2q_f32(output_ptr, dequantized);
158 },
159 input, output);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100160 },
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100161 min_max);
162}