blob: 6d5202d6b5dcf0d11789f9f610db2d6c06d6b257 [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010028#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/Window.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010031#include "src/core/NEON/NEAsymm.h"
32#include "src/core/NEON/NEMath.h"
33#include "src/core/NEON/wrapper/wrapper.h"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010034
John Kesapidesadfb2732019-03-04 16:29:22 +000035#include "arm_compute/core/CPP/Validate.h"
36
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010037#include <arm_neon.h>
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +010038#include <map>
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010039
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +010040namespace arm_compute
41{
Alex Gilday60954c62018-03-05 16:22:48 +000042namespace
43{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +000044constexpr auto window_step = 16;
45
John Kesapidesadfb2732019-03-04 16:29:22 +000046Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +000047{
John Kesapidesadfb2732019-03-04 16:29:22 +000048 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input);
Manuel Bottini4370cff2020-02-07 16:31:59 +000050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
John Kesapidesadfb2732019-03-04 16:29:22 +000051 ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape().total_size() == 0);
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +000052 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QASYMM16);
John Kesapidesadfb2732019-03-04 16:29:22 +000053 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output);
Alex Gilday60954c62018-03-05 16:22:48 +000054
55 return Status{};
56}
57
Manuel Bottini4370cff2020-02-07 16:31:59 +000058template <typename T>
59inline float32x4x4_t load_value(const T *input_ptr)
60{
61 using Tx16_t = typename wrapper::traits::neon_vector<T, 16>::type;
62 return arm_compute::convert_to_float32x4x4<Tx16_t>(wrapper::vloadq(input_ptr));
63}
64
65template <>
Michalis Spyroua4f378d2019-04-26 14:54:54 +010066inline float32x4x4_t load_value(const float *input_ptr)
Alex Gilday60954c62018-03-05 16:22:48 +000067{
John Kesapidesadfb2732019-03-04 16:29:22 +000068 return { wrapper::vloadq(input_ptr),
69 wrapper::vloadq(input_ptr + 4),
70 wrapper::vloadq(input_ptr + 8),
71 wrapper::vloadq(input_ptr + 12) };
Alex Gilday60954c62018-03-05 16:22:48 +000072}
John Kesapidesadfb2732019-03-04 16:29:22 +000073#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini4370cff2020-02-07 16:31:59 +000074template <>
75inline float32x4x4_t load_value(const float16_t *input_ptr)
John Kesapidesadfb2732019-03-04 16:29:22 +000076{
77 return { vcvt_f32_f16(wrapper::vload(input_ptr)),
78 vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
79 vcvt_f32_f16(wrapper::vload(input_ptr + 8)),
80 vcvt_f32_f16(wrapper::vload(input_ptr + 12)) };
81}
82
83#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +000084
85template <typename element_type>
86using vector_type = wrapper::traits::neon_vector_t<element_type, window_step>;
87
88template <typename quantized_type>
89vector_type<quantized_type> vquantize_qasymm8(const float32x4x4_t &qv, const UniformQuantizationInfo &qi);
90
91template <>
92vector_type<uint8_t> vquantize_qasymm8<uint8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
93{
94 return vquantize(qv, qi);
95}
96
97template <>
98vector_type<int8_t> vquantize_qasymm8<int8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
99{
100 return vquantize_signed(qv, qi);
101}
102
Alex Gilday60954c62018-03-05 16:22:48 +0000103} // namespace
104
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100105NEQuantizationLayerKernel::NEQuantizationLayerKernel()
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100106 : _input(nullptr), _output(nullptr), _func(nullptr)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100107{
108}
109
John Kesapidesadfb2732019-03-04 16:29:22 +0000110void NEQuantizationLayerKernel::configure(const ITensor *input, ITensor *output)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100111{
John Kesapidesadfb2732019-03-04 16:29:22 +0000112 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
113 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100114
John Kesapidesadfb2732019-03-04 16:29:22 +0000115 _input = input;
116 _output = output;
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100117
Manuel Bottini4370cff2020-02-07 16:31:59 +0000118 static const std::map<std::string, QuantizationFunctionExecutorPtr> quant_map =
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100119 {
Manuel Bottini4370cff2020-02-07 16:31:59 +0000120 { "op_QASYMM8_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<uint8_t, uint8_t> },
121 { "op_QASYMM8_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<uint8_t, int8_t> },
122 { "op_QASYMM8_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<uint8_t> },
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100123
Manuel Bottini4370cff2020-02-07 16:31:59 +0000124 { "op_QASYMM8_SIGNED_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<int8_t, uint8_t> },
125 { "op_QASYMM8_SIGNED_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<int8_t, int8_t> },
126 { "op_QASYMM8_SIGNED_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<int8_t> },
127
128 { "op_F32_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<float, uint8_t> },
129 { "op_F32_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<float, int8_t> },
130 { "op_F32_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<float> },
131
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100132#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini4370cff2020-02-07 16:31:59 +0000133 { "op_F16_QASYMM8", &NEQuantizationLayerKernel::run_quantize_qasymm8<float16_t, uint8_t> },
134 { "op_F16_QASYMM8_SIGNED", &NEQuantizationLayerKernel::run_quantize_qasymm8<float16_t, int8_t> },
135 { "op_F16_QASYMM16", &NEQuantizationLayerKernel::run_quantize_qasymm16<float16_t> },
136#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
137 };
138
139 std::string function_to_call("op_");
140 function_to_call += string_from_data_type(_input->info()->data_type()) + "_";
141 function_to_call += string_from_data_type(_output->info()->data_type());
142
143 auto it = quant_map.find(function_to_call);
144
145 if(it == quant_map.end())
146 {
147 ARM_COMPUTE_ERROR("Unsupported combination of input and output data types");
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100148 }
Manuel Bottini4370cff2020-02-07 16:31:59 +0000149 _func = it->second;
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100150
Alex Gilday60954c62018-03-05 16:22:48 +0000151 // Configure kernel window
John Kesapidesadfb2732019-03-04 16:29:22 +0000152 Window win_config = calculate_max_window(*input->info(), Steps());
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100153
John Kesapidesadfb2732019-03-04 16:29:22 +0000154 Coordinates coord;
155 coord.set_num_dimensions(output->info()->num_dimensions());
156 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100157
John Kesapidesadfb2732019-03-04 16:29:22 +0000158 INEKernel::configure(win_config);
Alex Gilday60954c62018-03-05 16:22:48 +0000159}
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100160
John Kesapidesadfb2732019-03-04 16:29:22 +0000161Status NEQuantizationLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Alex Gilday60954c62018-03-05 16:22:48 +0000162{
John Kesapidesadfb2732019-03-04 16:29:22 +0000163 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
Alex Gilday60954c62018-03-05 16:22:48 +0000164 return Status{};
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100165}
166
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000167template <typename TIn, typename TOut>
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100168void NEQuantizationLayerKernel::run_quantize_qasymm8(const Window &window)
John Kesapidesadfb2732019-03-04 16:29:22 +0000169{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000170 const auto window_start_x = static_cast<int>(window.x().start());
171 const auto window_end_x = static_cast<int>(window.x().end());
John Kesapidesadfb2732019-03-04 16:29:22 +0000172
Manuel Bottini4370cff2020-02-07 16:31:59 +0000173 const UniformQuantizationInfo uqinfo_in = _input->info()->quantization_info().uniform();
174 UniformQuantizationInfo uqinfo = _output->info()->quantization_info().uniform();
175 if(is_data_type_quantized_asymmetric(_input->info()->data_type()))
176 {
177 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
178 }
John Kesapidesadfb2732019-03-04 16:29:22 +0000179#ifdef __aarch64__
180 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
181#else //__aarch64__
182 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
183#endif //__aarch64__
184
185 // Collapse window and reset first dimension to handle tail calculations manually
186 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
187 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
188
189 Iterator input(_input, win_collapsed);
190 Iterator output(_output, win_collapsed);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100191 execute_window_loop(win_collapsed, [&](const Coordinates &)
John Kesapidesadfb2732019-03-04 16:29:22 +0000192 {
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000193 auto input_ptr = reinterpret_cast<const TIn *>(input.ptr());
194 auto output_ptr = reinterpret_cast<TOut *>(output.ptr());
John Kesapidesadfb2732019-03-04 16:29:22 +0000195
196 int x = window_start_x;
197 for(; x <= (window_end_x - window_step); x += window_step)
198 {
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000199 wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo));
John Kesapidesadfb2732019-03-04 16:29:22 +0000200 }
201 // Compute left-over elements
202 for(; x < window_end_x; ++x)
203 {
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000204 output_ptr[x] = Qasymm8QuantizationHelper<TOut>::quantize(input_ptr[x], uqinfo, rounding_policy);
John Kesapidesadfb2732019-03-04 16:29:22 +0000205 }
206 },
207 input, output);
208}
209
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100210template <typename T>
211void NEQuantizationLayerKernel::run_quantize_qasymm16(const Window &window)
212{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000213 const auto window_start_x = static_cast<int>(window.x().start());
214 const auto window_end_x = static_cast<int>(window.x().end());
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100215
Manuel Bottini4370cff2020-02-07 16:31:59 +0000216 const UniformQuantizationInfo uqinfo_in = _input->info()->quantization_info().uniform();
217 UniformQuantizationInfo uqinfo = _output->info()->quantization_info().uniform();
218 if(is_data_type_quantized_asymmetric(_input->info()->data_type()))
219 {
220 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
221 }
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100222#ifdef __aarch64__
223 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
224#else //__aarch64__
225 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
226#endif //__aarch64__
227
228 // Collapse window and reset first dimension to handle tail calculations manually
229 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
230 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
231
232 Iterator input(_input, win_collapsed);
233 Iterator output(_output, win_collapsed);
234 execute_window_loop(win_collapsed, [&](const Coordinates &)
235 {
236 auto input_ptr = reinterpret_cast<const T *>(input.ptr());
237 auto output_ptr = reinterpret_cast<uint16_t *>(output.ptr());
238
239 int x = window_start_x;
240 for(; x <= (window_end_x - window_step); x += window_step)
241 {
242 uint16x8x2_t tmp = vquantize_qasymm16(load_value(&input_ptr[x]), uqinfo);
243 vst1q_u16(&output_ptr[x], tmp.val[0]);
244 vst1q_u16(&output_ptr[x + 8], tmp.val[1]);
245 }
246 // Compute left-over elements
247 for(; x < window_end_x; ++x)
248 {
249 output_ptr[x] = quantize_qasymm16(input_ptr[x], uqinfo, rounding_policy);
250 }
251 },
252 input, output);
253}
254
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100255void NEQuantizationLayerKernel::run(const Window &window, const ThreadInfo &info)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100256{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100257 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100258 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
259 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100260 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100261
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100262 (this->*_func)(window);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100263}
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100264} // namespace arm_compute