blob: 8ca81e8b11e2a9f65afc912d5c83b0cf3013f07f [file] [log] [blame]
Michele Di Giorgio4e09b382017-07-05 18:20:02 +01001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2017-2021 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 Pinitasef516e82021-04-30 14:46:05 +010024#include "src/core/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"
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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgio4e09b382017-07-05 18:20:02 +010036
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010037#include "src/core/CPP/Validate.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);
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
57 ARM_COMPUTE_RETURN_ERROR_ON(dst->tensor_shape().total_size() == 0);
58 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QASYMM16);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
Alex Gilday60954c62018-03-05 16:22:48 +000060
61 return Status{};
62}
63
Manuel Bottini4370cff2020-02-07 16:31:59 +000064template <typename T>
65inline float32x4x4_t load_value(const T *input_ptr)
66{
67 using Tx16_t = typename wrapper::traits::neon_vector<T, 16>::type;
68 return arm_compute::convert_to_float32x4x4<Tx16_t>(wrapper::vloadq(input_ptr));
69}
70
71template <>
Michalis Spyroua4f378d2019-04-26 14:54:54 +010072inline float32x4x4_t load_value(const float *input_ptr)
Alex Gilday60954c62018-03-05 16:22:48 +000073{
John Kesapidesadfb2732019-03-04 16:29:22 +000074 return { wrapper::vloadq(input_ptr),
75 wrapper::vloadq(input_ptr + 4),
76 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{
83 return { vcvt_f32_f16(wrapper::vload(input_ptr)),
84 vcvt_f32_f16(wrapper::vload(input_ptr + 4)),
85 vcvt_f32_f16(wrapper::vload(input_ptr + 8)),
86 vcvt_f32_f16(wrapper::vload(input_ptr + 12)) };
87}
88
89#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +000090
91template <typename element_type>
92using vector_type = wrapper::traits::neon_vector_t<element_type, window_step>;
93
94template <typename quantized_type>
95vector_type<quantized_type> vquantize_qasymm8(const float32x4x4_t &qv, const UniformQuantizationInfo &qi);
96
97template <>
98vector_type<uint8_t> vquantize_qasymm8<uint8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
99{
100 return vquantize(qv, qi);
101}
102
103template <>
104vector_type<int8_t> vquantize_qasymm8<int8_t>(const float32x4x4_t &qv, const UniformQuantizationInfo &qi)
105{
106 return vquantize_signed(qv, qi);
107}
108
Alex Gilday60954c62018-03-05 16:22:48 +0000109} // namespace
110
Georgios Pinitasef516e82021-04-30 14:46:05 +0100111void CpuQuantizeKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100112{
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000113 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
114 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst));
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100115
Georgios Pinitasef516e82021-04-30 14:46:05 +0100116 static const std::map<std::string, QuantizeFunctionExecutorPtr> quant_map =
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100117 {
Georgios Pinitasef516e82021-04-30 14:46:05 +0100118 { "op_QASYMM8_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<uint8_t, uint8_t> },
119 { "op_QASYMM8_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<uint8_t, int8_t> },
120 { "op_QASYMM8_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<uint8_t> },
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100121
Georgios Pinitasef516e82021-04-30 14:46:05 +0100122 { "op_QASYMM8_SIGNED_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<int8_t, uint8_t> },
123 { "op_QASYMM8_SIGNED_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<int8_t, int8_t> },
124 { "op_QASYMM8_SIGNED_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<int8_t> },
Manuel Bottini4370cff2020-02-07 16:31:59 +0000125
Georgios Pinitasef516e82021-04-30 14:46:05 +0100126 { "op_F32_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<float, uint8_t> },
127 { "op_F32_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<float, int8_t> },
128 { "op_F32_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<float> },
Manuel Bottini4370cff2020-02-07 16:31:59 +0000129
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100130#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Georgios Pinitasef516e82021-04-30 14:46:05 +0100131 { "op_F16_QASYMM8", &CpuQuantizeKernel::run_quantize_qasymm8<float16_t, uint8_t> },
132 { "op_F16_QASYMM8_SIGNED", &CpuQuantizeKernel::run_quantize_qasymm8<float16_t, int8_t> },
133 { "op_F16_QASYMM16", &CpuQuantizeKernel::run_quantize_qasymm16<float16_t> },
Manuel Bottini4370cff2020-02-07 16:31:59 +0000134#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC*/
135 };
136
137 std::string function_to_call("op_");
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000138 function_to_call += string_from_data_type(src->data_type()) + "_";
139 function_to_call += string_from_data_type(dst->data_type());
Manuel Bottini4370cff2020-02-07 16:31:59 +0000140
141 auto it = quant_map.find(function_to_call);
142
143 if(it == quant_map.end())
144 {
145 ARM_COMPUTE_ERROR("Unsupported combination of input and output data types");
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100146 }
Manuel Bottini4370cff2020-02-07 16:31:59 +0000147 _func = it->second;
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100148
Alex Gilday60954c62018-03-05 16:22:48 +0000149 // Configure kernel window
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000150 Window win_config = calculate_max_window(*src, Steps());
151 ICpuKernel::configure(win_config);
Alex Gilday60954c62018-03-05 16:22:48 +0000152}
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100153
Georgios Pinitasef516e82021-04-30 14:46:05 +0100154Status CpuQuantizeKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
Alex Gilday60954c62018-03-05 16:22:48 +0000155{
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000156 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst));
Alex Gilday60954c62018-03-05 16:22:48 +0000157 return Status{};
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100158}
159
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000160template <typename TIn, typename TOut>
Georgios Pinitasef516e82021-04-30 14:46:05 +0100161void CpuQuantizeKernel::run_quantize_qasymm8(const ITensor *src, ITensor *dst, const Window &window)
John Kesapidesadfb2732019-03-04 16:29:22 +0000162{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000163 const auto window_start_x = static_cast<int>(window.x().start());
164 const auto window_end_x = static_cast<int>(window.x().end());
John Kesapidesadfb2732019-03-04 16:29:22 +0000165
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000166 const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
167 UniformQuantizationInfo uqinfo = dst->info()->quantization_info().uniform();
168 if(is_data_type_quantized_asymmetric(src->info()->data_type()))
Manuel Bottini4370cff2020-02-07 16:31:59 +0000169 {
170 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
171 }
John Kesapidesadfb2732019-03-04 16:29:22 +0000172#ifdef __aarch64__
173 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
174#else //__aarch64__
175 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
176#endif //__aarch64__
177
178 // Collapse window and reset first dimension to handle tail calculations manually
179 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
180 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
181
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000182 Iterator input(src, win_collapsed);
183 Iterator output(dst, win_collapsed);
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100184 execute_window_loop(win_collapsed, [&](const Coordinates &)
John Kesapidesadfb2732019-03-04 16:29:22 +0000185 {
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000186 auto input_ptr = reinterpret_cast<const TIn *>(input.ptr());
187 auto output_ptr = reinterpret_cast<TOut *>(output.ptr());
John Kesapidesadfb2732019-03-04 16:29:22 +0000188
189 int x = window_start_x;
190 for(; x <= (window_end_x - window_step); x += window_step)
191 {
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000192 wrapper::vstore(&output_ptr[x], vquantize_qasymm8<TOut>(load_value(&input_ptr[x]), uqinfo));
John Kesapidesadfb2732019-03-04 16:29:22 +0000193 }
194 // Compute left-over elements
195 for(; x < window_end_x; ++x)
196 {
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000197 output_ptr[x] = Qasymm8QuantizationHelper<TOut>::quantize(input_ptr[x], uqinfo, rounding_policy);
John Kesapidesadfb2732019-03-04 16:29:22 +0000198 }
199 },
200 input, output);
201}
202
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100203template <typename T>
Georgios Pinitasef516e82021-04-30 14:46:05 +0100204void CpuQuantizeKernel::run_quantize_qasymm16(const ITensor *src, ITensor *dst, const Window &window)
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100205{
Sang-Hoon Parkfb6aaeb2019-11-27 15:26:44 +0000206 const auto window_start_x = static_cast<int>(window.x().start());
207 const auto window_end_x = static_cast<int>(window.x().end());
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100208
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000209 const UniformQuantizationInfo uqinfo_in = src->info()->quantization_info().uniform();
210 UniformQuantizationInfo uqinfo = dst->info()->quantization_info().uniform();
211 if(is_data_type_quantized_asymmetric(src->info()->data_type()))
Manuel Bottini4370cff2020-02-07 16:31:59 +0000212 {
213 uqinfo = compute_requantization_scale_offset(uqinfo_in, uqinfo);
214 }
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100215#ifdef __aarch64__
216 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_NEAREST_EVEN;
217#else //__aarch64__
218 constexpr RoundingPolicy rounding_policy = RoundingPolicy::TO_ZERO;
219#endif //__aarch64__
220
221 // Collapse window and reset first dimension to handle tail calculations manually
222 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
223 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
224
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000225 Iterator input(src, win_collapsed);
226 Iterator output(dst, win_collapsed);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100227 execute_window_loop(win_collapsed, [&](const Coordinates &)
228 {
229 auto input_ptr = reinterpret_cast<const T *>(input.ptr());
230 auto output_ptr = reinterpret_cast<uint16_t *>(output.ptr());
231
232 int x = window_start_x;
233 for(; x <= (window_end_x - window_step); x += window_step)
234 {
235 uint16x8x2_t tmp = vquantize_qasymm16(load_value(&input_ptr[x]), uqinfo);
236 vst1q_u16(&output_ptr[x], tmp.val[0]);
237 vst1q_u16(&output_ptr[x + 8], tmp.val[1]);
238 }
239 // Compute left-over elements
240 for(; x < window_end_x; ++x)
241 {
242 output_ptr[x] = quantize_qasymm16(input_ptr[x], uqinfo, rounding_policy);
243 }
244 },
245 input, output);
246}
247
Georgios Pinitasef516e82021-04-30 14:46:05 +0100248void CpuQuantizeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100249{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100250 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100251 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000252 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgiod64a46c2019-10-01 12:25:49 +0100253 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100254
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000255 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
256 auto dst = tensors.get_tensor(TensorType::ACL_DST);
257 (this->*_func)(src, dst, window);
Michele Di Giorgio4e09b382017-07-05 18:20:02 +0100258}
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000259
Georgios Pinitasef516e82021-04-30 14:46:05 +0100260const char *CpuQuantizeKernel::name() const
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000261{
Georgios Pinitasef516e82021-04-30 14:46:05 +0100262 return "CpuQuantizeKernel";
Manuel Bottini0ded4c42021-03-09 14:15:27 +0000263}
264} // namespace kernels
265} // namespace cpu
266} // namespace arm_compute