blob: 70984f0a7560ffd00807a10a969ba2778636f15e [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
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/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
37NEDequantizationLayerKernel::NEDequantizationLayerKernel()
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010038 : _input(nullptr), _output(nullptr), _min_max(nullptr)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010039{
40}
41
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010042void NEDequantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010043{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
45 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010046 ARM_COMPUTE_ERROR_ON_NULLPTR(min_max);
47 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010048
49 // Output tensor auto initialization if not yet initialized
50 auto_init_if_empty(*output->info(), input->info()->tensor_shape(), 1, DataType::F32, 0);
51
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::F32);
53 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(input, output);
54
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010055 _input = input;
56 _output = output;
57 _min_max = min_max;
Michele Di Giorgio32982d82017-07-07 14:44:43 +010058
59 constexpr unsigned int num_elems_processed_per_iteration = 8;
60
61 // Configure window
62 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010063 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010064 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010065 AccessWindowStatic min_max_access(min_max->info(), 0, 0, 2, min_max->info()->dimension(1));
66
67 // Update window and padding
68 update_window_and_padding(win, input_access, output_access, min_max_access);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010069 output_access.set_valid_region(win, input->info()->valid_region());
70
71 INEKernel::configure(win);
72}
73
Moritz Pflanzerc186b572017-09-07 09:48:04 +010074void NEDequantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010075{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010076 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010077 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
78 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
79
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010080 Window window_input_output(window);
81 window_input_output.collapse_if_possible(INEKernel::window(), 3);
82 window_input_output.set(3, Window::Dimension(0, 1, 1));
Michele Di Giorgio32982d82017-07-07 14:44:43 +010083
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010084 Window window_min_max;
85 window_min_max.use_tensor_dimensions(_min_max->info()->tensor_shape());
86 window_min_max.set(Window::DimX, Window::Dimension(0, 1, 1));
87 window_min_max.collapse_if_possible(INEKernel::window(), 1);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010088
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010089 Iterator input(_input, window_input_output);
90 Iterator output(_output, window_input_output);
91 Iterator min_max(_min_max, window_min_max);
92
93 execute_window_loop(window_min_max, [&](const Coordinates & id_batch)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010094 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010095 // Get the min and max
96 const float min = *(reinterpret_cast<const float *>(min_max.ptr()) + 0);
97 const float max = *(reinterpret_cast<const float *>(min_max.ptr()) + 1);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010098
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010099 const float32x4_t vmin = vdupq_n_f32(min);
100 const float range = max - min;
101 const float32x4_t scaling = vdupq_n_f32(range / 255.0f);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100102
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100103 // Uniformly map values to range 8bit integers, i.e. [min, max] -> [0, 255]
104 execute_window_loop(window_input_output, [&](const Coordinates & id)
105 {
106 // Get the input values
107 const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr() + id_batch[1] * _input->info()->strides_in_bytes()[3]);
108
109 const uint8x8_t val_u8 = vld1_u8(input_ptr);
110 const uint16x8_t val_u16 = vmovl_u8(val_u8);
111 const uint32x4_t val_u32_low = vmovl_u16(vget_low_u16(val_u16));
112 const uint32x4_t val_u32_high = vmovl_u16(vget_high_u16(val_u16));
113 float32x4_t val_low = vcvtq_f32_u32(val_u32_low);
114 float32x4_t val_high = vcvtq_f32_u32(val_u32_high);
115
116 // Dequantize -> (q / 255.0 * range) + min
117 val_low = vmulq_f32(val_low, scaling);
118 val_high = vmulq_f32(val_high, scaling);
119 val_low = vaddq_f32(val_low, vmin);
120 val_high = vaddq_f32(val_high, vmin);
121
122 const float32x4x2_t dequantized = vuzpq_f32(val_low, val_high);
123
124 // Store the dequantized values
125 auto output_ptr = reinterpret_cast<float *>(output.ptr() + id_batch[1] * _output->info()->strides_in_bytes()[3]);
126 vst2q_f32(output_ptr, dequantized);
127 },
128 input, output);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100129 },
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100130 min_max);
131}