blob: 5dde6808373b0912fc944317f66a57063c634eca [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01001/*
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +00002 * Copyright (c) 2017-2022 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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuQuantizeKernel.h"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010025
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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010031
32#include "src/core/CPP/Validate.h"
33#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010035#include "src/core/NEON/NEAsymm.h"
36#include "src/core/NEON/NEMath.h"
37#include "src/core/NEON/wrapper/wrapper.h"
John Kesapidesadfb2732019-03-04 16:29:22 +000038
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010039#include <arm_neon.h>
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +010040#include <map>
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010041
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +010042namespace arm_compute
43{
Manuel Bottini0ded4c42021-03-09 14:15:27 +000044namespace cpu
45{
46namespace kernels
47{
Alex Gilday60954c62018-03-05 16:22:48 +000048namespace
49{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +000050constexpr auto window_step = 16;
51
Manuel Bottini0ded4c42021-03-09 14:15:27 +000052Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst)
Alex Gilday60954c62018-03-05 16:22:48 +000053{
Manuel Bottini0ded4c42021-03-09 14:15:27 +000054 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
55 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED,
57 DataType::F16, DataType::F32);
Manuel Bottini0ded4c42021-03-09 14:15:27 +000058 ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010059 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QSYMM8, DataType::QASYMM8,
60 DataType::QASYMM8_SIGNED, DataType::QASYMM16);
Manuel Bottini0ded4c42021-03-09 14:15:27 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
Alex Gilday60954c62018-03-05 16:22:48 +000062
63 return Status{};
64}
65
Manuel Bottini4370cff2020-02-07 16:31:59 +000066template <typename T>
67inline float32x4x4_t load_value(const T *input_ptr)
68{
69 using Tx16_t = typename wrapper::traits::neon_vector<T, 16>::type;
70 return arm_compute::convert_to_float32x4x4<Tx16_t>(wrapper::vloadq(input_ptr));
71}
72
73template <>
Michalis Spyroua4f378d2019-04-26 14:54:54 +010074inline float32x4x4_t load_value(const float *input_ptr)
Alex Gilday60954c62018-03-05 16:22:48 +000075{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010076 return {wrapper::vloadq(input_ptr), wrapper::vloadq(input_ptr + 4), wrapper::vloadq(input_ptr + 8),
77 wrapper::vloadq(input_ptr + 12)};
Alex Gilday60954c62018-03-05 16:22:48 +000078}
John Kesapidesadfb2732019-03-04 16:29:22 +000079#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Manuel Bottini4370cff2020-02-07 16:31:59 +000080template <>
81inline float32x4x4_t load_value(const float16_t *input_ptr)
John Kesapidesadfb2732019-03-04 16:29:22 +000082{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010083 return {vcvt_f32_f16(wrapper::vload(input_ptr)), vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
84 vcvt_f32_f16(wrapper::vload(input_ptr + 8)), vcvt_f32_f16(wrapper::vload(input_ptr + 12))};
John Kesapidesadfb2732019-03-04 16:29:22 +000085}
86
87#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +000088
89template <typename element_type>
90using vector_type = wrapper::traits::neon_vector_t<element_type, window_step>;
91
92template <typename quantized_type>
93vector_type<quantized_type> vquantize_qasymm8(const float32x4x4_t &qv, const UniformQuantizationInfo &qi);
94
95template <>
96vector_type<uint8_t> vquantize_qasymm8<uint8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
97{
98 return vquantize(qv, qi);
99}
100
101template <>
102vector_type<int8_t> vquantize_qasymm8<int8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
103{
104 return vquantize_signed(qv, qi);
105}
106
Alex Gilday60954c62018-03-05 16:22:48 +0000107} // namespace
108
Georgios Pinitasef516e82021-04-30 14:46:05 +0100109void CpuQuantizeKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100110{
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000111 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
112 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst));
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100113
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 static const std::map<std::string, QuantizeFunctionExecutorPtr> quant_map = {
115 {"op_QASYMM8_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<uint8_t, uint8_t>},
116 {"op_QASYMM8_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<uint8_t, int8_t>},
117 {"op_QASYMM8_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<uint8_t>},
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100118
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 {"op_QASYMM8_SIGNED_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<int8_t, uint8_t>},
120 {"op_QASYMM8_SIGNED_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<int8_t, int8_t>},
121 {"op_QASYMM8_SIGNED_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<int8_t>},
Manuel Bottini4370cff2020-02-07 16:31:59 +0000122
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 {"op_F32_QSYMM8", &CpuQuantizeKernel::run_quantize_qsymm8<float, int8_t>},
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +0000124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 {"op_F32_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<float, uint8_t>},
126 {"op_F32_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<float, int8_t>},
127 {"op_F32_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<float>},
Manuel Bottini4370cff2020-02-07 16:31:59 +0000128
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100129#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100130 {"op_F16_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<float16_t, uint8_t>},
131 {"op_F16_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<float16_t, int8_t>},
132 {"op_F16_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<float16_t>},
Manuel Bottini4370cff2020-02-07 16:31:59 +0000133#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
134 };
135
136 std::string function_to_call("op_");
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000137 function_to_call += string_from_data_type(src->data_type()) + "_";
138 function_to_call += string_from_data_type(dst->data_type());
Manuel Bottini4370cff2020-02-07 16:31:59 +0000139
140 auto it = quant_map.find(function_to_call);
141
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100142 if (it == quant_map.end())
Manuel Bottini4370cff2020-02-07 16:31:59 +0000143 {
144 ARM_COMPUTE_ERROR("Unsupported combination of input and output data types");
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100145 }
Manuel Bottini4370cff2020-02-07 16:31:59 +0000146 _func = it->second;
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100147
Alex Gilday60954c62018-03-05 16:22:48 +0000148 // Configure kernel window
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000149 Window win_config = calculate_max_window(*src, Steps());
150 ICpuKernel::configure(win_config);
Alex Gilday60954c62018-03-05 16:22:48 +0000151}
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100152
Georgios Pinitasef516e82021-04-30 14:46:05 +0100153Status CpuQuantizeKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
Alex Gilday60954c62018-03-05 16:22:48 +0000154{
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000155 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst));
Alex Gilday60954c62018-03-05 16:22:48 +0000156 return Status{};
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100157}
158
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000159template <typename TIn, typename TOut>
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +0000160void CpuQuantizeKernel::run_quantize_qsymm8(const ITensor *src, ITensor *dst, const Window &window)
161{
162 const auto window_start_x = static_cast<int>(window.x().start());
163 const auto window_end_x = static_cast<int>(window.x().end());
164
165 const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
166 UniformQuantizationInfo uqinfo = dst->info()->quantization_info().uniform();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 if (is_data_type_quantized_asymmetric(src->info()->data_type()))
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +0000168 {
169 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
170 }
171 // Collapse window and reset first dimension to handle tail calculations manually
172 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
173 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
174
175 Iterator input(src, win_collapsed);
176 Iterator output(dst, win_collapsed);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100177 execute_window_loop(
178 win_collapsed,
179 [&](const Coordinates &)
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +0000180 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181 auto input_ptr = reinterpret_cast<const TIn *>(input.ptr());
182 auto output_ptr = reinterpret_cast<TOut *>(output.ptr());
183 int x = window_start_x;
184 for (; x <= (window_end_x - window_step); x += window_step)
185 {
186 wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo));
187 }
188 // Compute left-over elements
189 for (; x < window_end_x; ++x)
190 {
191 output_ptr[x] = quantize_qsymm8(input_ptr[x], dst->info()->quantization_info());
192 }
193 },
194 input, output);
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +0000195}
196
197template <typename TIn, typename TOut>
Georgios Pinitasef516e82021-04-30 14:46:05 +0100198void CpuQuantizeKernel::run_quantize_qasymm8(const ITensor *src, ITensor *dst, const Window &window)
John Kesapidesadfb2732019-03-04 16:29:22 +0000199{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000200 const auto window_start_x = static_cast<int>(window.x().start());
201 const auto window_end_x = static_cast<int>(window.x().end());
John Kesapidesadfb2732019-03-04 16:29:22 +0000202
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000203 const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
204 UniformQuantizationInfo uqinfo = dst->info()->quantization_info().uniform();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100205 if (is_data_type_quantized_asymmetric(src->info()->data_type()))
Manuel Bottini4370cff2020-02-07 16:31:59 +0000206 {
207 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
208 }
John Kesapidesadfb2732019-03-04 16:29:22 +0000209#ifdef __aarch64__
210 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
211#else //__aarch64__
212 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
213#endif //__aarch64__
214
215 // Collapse window and reset first dimension to handle tail calculations manually
216 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
217 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
218
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000219 Iterator input(src, win_collapsed);
220 Iterator output(dst, win_collapsed);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100221 execute_window_loop(
222 win_collapsed,
223 [&](const Coordinates &)
224 {
225 auto input_ptr = reinterpret_cast<const TIn *>(input.ptr());
226 auto output_ptr = reinterpret_cast<TOut *>(output.ptr());
John Kesapidesadfb2732019-03-04 16:29:22 +0000227
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100228 int x = window_start_x;
229 for (; x <= (window_end_x - window_step); x += window_step)
230 {
231 wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo));
232 }
233 // Compute left-over elements
234 for (; x < window_end_x; ++x)
235 {
236 output_ptr[x] = Qasymm8QuantizationHelper<TOut>::quantize(input_ptr[x], uqinfo, rounding_policy);
237 }
238 },
239 input, output);
John Kesapidesadfb2732019-03-04 16:29:22 +0000240}
241
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100242template <typename T>
Georgios Pinitasef516e82021-04-30 14:46:05 +0100243void CpuQuantizeKernel::run_quantize_qasymm16(const ITensor *src, ITensor *dst, const Window &window)
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100244{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000245 const auto window_start_x = static_cast<int>(window.x().start());
246 const auto window_end_x = static_cast<int>(window.x().end());
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100247
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000248 const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
249 UniformQuantizationInfo uqinfo = dst->info()->quantization_info().uniform();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100250 if (is_data_type_quantized_asymmetric(src->info()->data_type()))
Manuel Bottini4370cff2020-02-07 16:31:59 +0000251 {
252 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
253 }
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100254#ifdef __aarch64__
255 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
256#else //__aarch64__
257 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
258#endif //__aarch64__
259
260 // Collapse window and reset first dimension to handle tail calculations manually
261 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
262 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
263
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000264 Iterator input(src, win_collapsed);
265 Iterator output(dst, win_collapsed);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100266 execute_window_loop(
267 win_collapsed,
268 [&](const Coordinates &)
269 {
270 auto input_ptr = reinterpret_cast<const T *>(input.ptr());
271 auto output_ptr = reinterpret_cast<uint16_t *>(output.ptr());
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100272
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100273 int x = window_start_x;
274 for (; x <= (window_end_x - window_step); x += window_step)
275 {
276 uint16x8x2_t tmp = vquantize_qasymm16(load_value(&input_ptr[x]), uqinfo);
277 vst1q_u16(&output_ptr[x], tmp.val[0]);
278 vst1q_u16(&output_ptr[x + 8], tmp.val[1]);
279 }
280 // Compute left-over elements
281 for (; x < window_end_x; ++x)
282 {
283 output_ptr[x] = quantize_qasymm16(input_ptr[x], uqinfo, rounding_policy);
284 }
285 },
286 input, output);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100287}
288
Georgios Pinitasef516e82021-04-30 14:46:05 +0100289void CpuQuantizeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100290{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100291 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100292 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000293 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100294 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100295
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000296 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
297 auto dst = tensors.get_tensor(TensorType::ACL_DST);
298 (this->*_func)(src, dst, window);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100299}
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000300
Georgios Pinitasef516e82021-04-30 14:46:05 +0100301const char *CpuQuantizeKernel::name() const
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000302{
Georgios Pinitasef516e82021-04-30 14:46:05 +0100303 return "CpuQuantizeKernel";
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000304}
305} // namespace kernels
306} // namespace cpu
Pablo Marquez Telloa5d61bf2022-03-17 12:52:02 +0000307} // namespace arm_compute