blob: 0aa34cd4113ce4aa1cf41a261efeacc620a8c1a4 [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01001/*
John Kesapidesadfb2732019-03-04 16:29:22 +00002 * Copyright (c) 2017-2019 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
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
John Kesapidesadfb2732019-03-04 16:29:22 +000028#include "arm_compute/core/NEON/NEAsymm.h"
29#include "arm_compute/core/NEON/wrapper/wrapper.h"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010030#include "arm_compute/core/Utils.h"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
33
John Kesapidesadfb2732019-03-04 16:29:22 +000034#include "arm_compute/core/CPP/Validate.h"
35
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010036#include <arm_neon.h>
37
38using namespace arm_compute;
39
Alex Gilday60954c62018-03-05 16:22:48 +000040namespace
41{
John Kesapidesadfb2732019-03-04 16:29:22 +000042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +000043{
John Kesapidesadfb2732019-03-04 16:29:22 +000044 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
47 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0);
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8);
49 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Alex Gilday60954c62018-03-05 16:22:48 +000050
51 return Status{};
52}
53
Michalis Spyroua4f378d2019-04-26 14:54:54 +010054inline float32x4x4_t load_value(const float *input_ptr)
Alex Gilday60954c62018-03-05 16:22:48 +000055{
John Kesapidesadfb2732019-03-04 16:29:22 +000056 return { wrapper::vloadq(input_ptr),
57 wrapper::vloadq(input_ptr + 4),
58 wrapper::vloadq(input_ptr + 8),
59 wrapper::vloadq(input_ptr + 12) };
Alex Gilday60954c62018-03-05 16:22:48 +000060}
John Kesapidesadfb2732019-03-04 16:29:22 +000061#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
62inline const float32x4x4_t load_value(const float16_t *input_ptr)
63{
64 return { vcvt_f32_f16(wrapper::vload(input_ptr)),
65 vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
66 vcvt_f32_f16(wrapper::vload(input_ptr + 8)),
67 vcvt_f32_f16(wrapper::vload(input_ptr + 12)) };
68}
69
70#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Alex Gilday60954c62018-03-05 16:22:48 +000071} // namespace
72
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010073NEQuantizationLayerKernel::NEQuantizationLayerKernel()
John Kesapidesadfb2732019-03-04 16:29:22 +000074 : _input(nullptr), _output(nullptr)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010075{
76}
77
John Kesapidesadfb2732019-03-04 16:29:22 +000078void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010079{
John Kesapidesadfb2732019-03-04 16:29:22 +000080 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
81 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010082
John Kesapidesadfb2732019-03-04 16:29:22 +000083 _input = input;
84 _output = output;
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010085
Alex Gilday60954c62018-03-05 16:22:48 +000086 // Configure kernel window
John Kesapidesadfb2732019-03-04 16:29:22 +000087 Window win_config = calculate_max_window(*input->info(), Steps());
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010088
John Kesapidesadfb2732019-03-04 16:29:22 +000089 Coordinates coord;
90 coord.set_num_dimensions(output->info()->num_dimensions());
91 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
Gian Marco Iodice06b184a2017-08-29 16:05:25 +010092
John Kesapidesadfb2732019-03-04 16:29:22 +000093 INEKernel::configure(win_config);
Alex Gilday60954c62018-03-05 16:22:48 +000094}
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010095
John Kesapidesadfb2732019-03-04 16:29:22 +000096Status NEQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +000097{
John Kesapidesadfb2732019-03-04 16:29:22 +000098 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
Alex Gilday60954c62018-03-05 16:22:48 +000099
100 return Status{};
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100101}
102
John Kesapidesadfb2732019-03-04 16:29:22 +0000103template <typename T>
104void NEQuantizationLayerKernel::quantize(const Window &window, const QuantizationInfo &qinfo)
105{
106 constexpr auto window_step = 16;
107 const auto window_start_x = static_cast<int>(window.x().start());
108 const auto window_end_x = static_cast<int>(window.x().end());
109
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100110 const UniformQuantizationInfo uqinfo = qinfo.uniform();
John Kesapidesadfb2732019-03-04 16:29:22 +0000111#ifdef __aarch64__
112 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
113#else //__aarch64__
114 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
115#endif //__aarch64__
116
117 // Collapse window and reset first dimension to handle tail calculations manually
118 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
119 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
120
121 Iterator input(_input, win_collapsed);
122 Iterator output(_output, win_collapsed);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100123 execute_window_loop(win_collapsed, [&](const Coordinates &)
John Kesapidesadfb2732019-03-04 16:29:22 +0000124 {
125 auto input_ptr = reinterpret_cast<const T *>(input.ptr());
126 auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
127
128 int x = window_start_x;
129 for(; x <= (window_end_x - window_step); x += window_step)
130 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100131 wrapper::vstore(&output_ptr[x], vquantize(load_value(&input_ptr[x]), uqinfo));
John Kesapidesadfb2732019-03-04 16:29:22 +0000132 }
133 // Compute left-over elements
134 for(; x < window_end_x; ++x)
135 {
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100136 output_ptr[x] = quantize_qasymm8(input_ptr[x], uqinfo, rounding_policy);
John Kesapidesadfb2732019-03-04 16:29:22 +0000137 }
138 },
139 input, output);
140}
141
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100142void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100143{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100144 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100145 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
146 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
147
John Kesapidesadfb2732019-03-04 16:29:22 +0000148 const QuantizationInfo &qinfo = _output->info()->quantization_info();
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100149
John Kesapidesadfb2732019-03-04 16:29:22 +0000150 switch(_input->info()->data_type())
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100151 {
John Kesapidesadfb2732019-03-04 16:29:22 +0000152 case DataType::F32:
153 NEQuantizationLayerKernel::quantize<float>(window, qinfo);
154 break;
155#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
156 case DataType::F16:
157 NEQuantizationLayerKernel::quantize<float16_t>(window, qinfo);
158 break;
159#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
160 default:
161 ARM_COMPUTE_ERROR("Unsupported data type.");
162 }
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100163}