blob: 3bf2b35a0914e41ce78feed84dc7a2d5290e6d3c [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
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/TensorInfo.h"
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
35using namespace arm_compute;
36
37NEDequantizationLayerKernel::NEDequantizationLayerKernel()
38 : _input(nullptr), _output(nullptr), _min(nullptr), _max(nullptr)
39{
40}
41
42void NEDequantizationLayerKernel::configure(const ITensor *input, ITensor *output, const float *min, const float *max)
43{
44 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8);
45 ARM_COMPUTE_ERROR_ON_NULLPTR(output);
46 ARM_COMPUTE_ERROR_ON_NULLPTR(min);
47 ARM_COMPUTE_ERROR_ON_NULLPTR(max);
48
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
55 _input = input;
56 _output = output;
57 _min = min;
58 _max = max;
59
60 constexpr unsigned int num_elems_processed_per_iteration = 8;
61
62 // Configure window
63 Window win = calculate_max_window(*input->info(), Steps(num_elems_processed_per_iteration));
64 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
65 update_window_and_padding(win, AccessWindowHorizontal(input->info(), 0, num_elems_processed_per_iteration), output_access);
66 output_access.set_valid_region(win, input->info()->valid_region());
67
68 INEKernel::configure(win);
69}
70
Moritz Pflanzerc186b572017-09-07 09:48:04 +010071void NEDequantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio32982d82017-07-07 14:44:43 +010072{
Moritz Pflanzerc186b572017-09-07 09:48:04 +010073 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio32982d82017-07-07 14:44:43 +010074 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
75 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
76
77 Iterator input(_input, window);
78 Iterator output(_output, window);
79
80 const float32x4_t vmin = vdupq_n_f32(*_min);
81 const float range = *_max - *_min;
82 const float32x4_t scaling = vdupq_n_f32(range / 255.0f);
83
84 // Uniformly map values to range 8bit integers, i.e. [min, max] -> [0, 255]
85 execute_window_loop(window, [&](const Coordinates & id)
86 {
87 const uint8x8_t val_u8 = vld1_u8(reinterpret_cast<uint8_t *>(input.ptr()));
88 const uint16x8_t val_u16 = vmovl_u8(val_u8);
89 const uint32x4_t val_u32_low = vmovl_u16(vget_low_u16(val_u16));
90 const uint32x4_t val_u32_high = vmovl_u16(vget_high_u16(val_u16));
91 float32x4_t val_low = vcvtq_f32_u32(val_u32_low);
92 float32x4_t val_high = vcvtq_f32_u32(val_u32_high);
93
94 // Dequantize -> (q / 255.0 * range) + min
95 val_low = vmulq_f32(val_low, scaling);
96 val_high = vmulq_f32(val_high, scaling);
97 val_low = vaddq_f32(val_low, vmin);
98 val_high = vaddq_f32(val_high, vmin);
99
100 const float32x4x2_t dequantized = vuzpq_f32(val_low, val_high);
101 vst2q_f32(reinterpret_cast<float *>(output.ptr()), dequantized);
102 },
103 input, output);
104}