blob: bff79f0f0c9a0953d8d9e4e422a9a58f2a0e5f1e [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +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/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
37NEQuantizationLayerKernel::NEQuantizationLayerKernel()
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010038 : _input(nullptr), _output(nullptr), _min_max(nullptr)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010039{
40}
41
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010042void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output, const ITensor *min_max)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010043{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32);
45 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010046 ARM_COMPUTE_ERROR_ON(input->info()->num_dimensions() < 3);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010047
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 Iodice06b184a2017-08-29 16:05:25 +010054 _input = input;
55 _output = output;
56 _min_max = min_max;
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010057
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 Iodice06b184a2017-08-29 16:05:25 +010062 AccessWindowHorizontal input_access(input->info(), 0, num_elems_processed_per_iteration);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010063 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010064 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 Giorgio4e09b382017-07-05 18:20:02 +010068 output_access.set_valid_region(win, input->info()->valid_region());
69
70 INEKernel::configure(win);
71}
72
Moritz Pflanzerc186b572017-09-07 09:48:04 +010073void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010074{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010075 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010076 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
77 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
78
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010079 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 Giorgio4e09b382017-07-05 18:20:02 +010082
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010083 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 Giorgio4e09b382017-07-05 18:20:02 +010087
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010088 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 Giorgio4e09b382017-07-05 18:20:02 +010093 {
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010094 // 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 Giorgio4e09b382017-07-05 18:20:02 +010097
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010098 // Saturate the result if min = max
99 if(min == max)
100 {
101 min = 0.0f;
102 max = 1.0f;
103 }
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100104
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100105 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 Giorgio4e09b382017-07-05 18:20:02 +0100109
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100110 // 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 Giorgio4e09b382017-07-05 18:20:02 +0100140 },
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100141 min_max);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100142}