blob: a2d24f9243aa3d2144a994248671b441c9b44932 [file] [log] [blame]
Michele Di Giorgio32982d82017-07-07 14:44:43 +01001/*
Michalis Spyrouc5b1bee2021-04-06 11:54:03 +01002 * Copyright (c) 2017-2021 Arm Limited.
Michele Di Giorgio32982d82017-07-07 14:44:43 +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/CpuDequantizeKernel.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010028#include "arm_compute/core/Utils.h"
29#include "arm_compute/core/Validate.h"
30#include "arm_compute/core/Window.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010031#include "src/core/CPP/Validate.h"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010032#include "src/core/NEON/NEAsymm.h"
33#include "src/core/NEON/NESymm.h"
34#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Michele Di Giorgio32982d82017-07-07 14:44:43 +010037
38#include <arm_neon.h>
39
Georgios Pinitas574775c2019-02-18 20:08:02 +000040namespace arm_compute
41{
Manuel Bottini00f4dfc2021-03-10 09:55:14 +000042namespace cpu
43{
44namespace kernels
45{
Alex Gilday60954c62018-03-05 16:22:48 +000046namespace
47{
Manuel Bottini00f4dfc2021-03-10 09:55:14 +000048Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst)
Alex Gilday60954c62018-03-05 16:22:48 +000049{
Manuel Bottini00f4dfc2021-03-10 09:55:14 +000050 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
51 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::QSYMM8_PER_CHANNEL, DataType::QSYMM8, DataType::QSYMM16);
Alex Gilday60954c62018-03-05 16:22:48 +000052
Manuel Bottini00f4dfc2021-03-10 09:55:14 +000053 if(dst->tensor_shape().total_size() > 0)
Alex Gilday60954c62018-03-05 16:22:48 +000054 {
Manuel Bottini00f4dfc2021-03-10 09:55:14 +000055 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(dst);
56 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::F16, DataType::F32);
57 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(src, dst);
Alex Gilday60954c62018-03-05 16:22:48 +000058 }
59
60 return Status{};
61}
62
Georgios Pinitas574775c2019-02-18 20:08:02 +000063template <typename T>
64inline void store_result(T *ptr, const float32x4x4_t &v)
65{
66 ARM_COMPUTE_UNUSED(ptr, v);
67}
Alex Gilday60954c62018-03-05 16:22:48 +000068
Georgios Pinitas574775c2019-02-18 20:08:02 +000069template <>
70inline void store_result<float>(float *ptr, const float32x4x4_t &v)
71{
72 wrapper::vstore(ptr, v.val[0]);
73 wrapper::vstore(ptr + 4, v.val[1]);
74 wrapper::vstore(ptr + 8, v.val[2]);
75 wrapper::vstore(ptr + 12, v.val[3]);
76}
Alex Gilday60954c62018-03-05 16:22:48 +000077
Georgios Pinitas574775c2019-02-18 20:08:02 +000078#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
79template <>
80inline void store_result<float16_t>(float16_t *ptr, const float32x4x4_t &v)
81{
82 wrapper::vstore(ptr, vcombine_f16(vcvt_f16_f32(v.val[0]), vcvt_f16_f32(v.val[1])));
83 wrapper::vstore(ptr + 8, vcombine_f16(vcvt_f16_f32(v.val[2]), vcvt_f16_f32(v.val[3])));
84}
85#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
86
87template <typename T>
Michalis Spyrouba27e442019-05-28 10:04:57 +010088inline void store_result(T *ptr, const float32x4x2_t &v)
89{
90 ARM_COMPUTE_UNUSED(ptr, v);
91}
92
93template <>
94inline void store_result<float>(float *ptr, const float32x4x2_t &v)
95{
96 wrapper::vstore(ptr, v.val[0]);
97 wrapper::vstore(ptr + 4, v.val[1]);
98}
99
100#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
101template <>
102inline void store_result<float16_t>(float16_t *ptr, const float32x4x2_t &v)
103{
104 wrapper::vstore(ptr, vcombine_f16(vcvt_f16_f32(v.val[0]), vcvt_f16_f32(v.val[1])));
105}
106#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
107
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000108template <typename TOut, typename TIn>
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100109void run_dequantization_qasymm8(const ITensor *input, ITensor *output, const Window &window)
Georgios Pinitas574775c2019-02-18 20:08:02 +0000110{
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100111 const UniformQuantizationInfo &qinfo = input->info()->quantization_info().uniform();
112 const float scale = qinfo.scale;
113 const int32_t offset = qinfo.offset;
Georgios Pinitas574775c2019-02-18 20:08:02 +0000114
115 const int window_step_x = 16;
116 const auto window_start_x = static_cast<int>(window.x().start());
117 const auto window_end_x = static_cast<int>(window.x().end());
118
119 // Collapse window and reset first dimension to handle tail calculations manually
120 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
121 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
122
123 // Create iterators
124 Iterator in(input, win_collapsed);
125 Iterator out(output, win_collapsed);
126
Michalis Spyroua4f378d2019-04-26 14:54:54 +0100127 execute_window_loop(win_collapsed, [&](const Coordinates &)
Georgios Pinitas574775c2019-02-18 20:08:02 +0000128 {
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000129 const auto in_ptr = reinterpret_cast<const TIn *>(in.ptr());
130 const auto out_ptr = reinterpret_cast<TOut *>(out.ptr());
Georgios Pinitas574775c2019-02-18 20:08:02 +0000131
132 int x = window_start_x;
133 for(; x <= (window_end_x - window_step_x); x += window_step_x)
134 {
135 const auto vin = wrapper::vloadq(in_ptr + x);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100136 const auto vdeq = vdequantize(vin, scale, offset);
Georgios Pinitas574775c2019-02-18 20:08:02 +0000137
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000138 store_result(reinterpret_cast<TOut *>(out_ptr + x), vdeq);
Georgios Pinitas574775c2019-02-18 20:08:02 +0000139 }
140
141 // Compute left-over elements
142 for(; x < window_end_x; ++x)
143 {
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000144 auto val = *(in_ptr + x);
145 *(out_ptr + x) = static_cast<TOut>(Qasymm8QuantizationHelper<TIn>::dequantize(val, qinfo));
Georgios Pinitas574775c2019-02-18 20:08:02 +0000146 }
147 },
148 in, out);
Alex Gilday60954c62018-03-05 16:22:48 +0000149}
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100150
151template <typename T>
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000152void run_dequantization_qsymm8_per_channel_nchw(const ITensor *input, ITensor *output, const Window &window)
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100153{
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000154 const auto scale = input->info()->quantization_info().scale();
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100155
156 const int window_step_x = 16;
157 const auto window_start_x = static_cast<int>(window.x().start());
158 const auto window_end_x = static_cast<int>(window.x().end());
159
160 // Reset first dimension to handle tail calculations manually
161 Window win(window);
162 win.set(Window::DimX, Window::Dimension(0, 1, 1));
163
164 // Create iterators
165 Iterator in(input, win);
166 Iterator out(output, win);
167
168 execute_window_loop(win, [&](const Coordinates & id)
169 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000170 const auto in_ptr = reinterpret_cast<const int8_t *>(in.ptr());
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100171 const auto out_ptr = reinterpret_cast<T *>(out.ptr());
172
173 int x = window_start_x;
174 for(; x <= (window_end_x - window_step_x); x += window_step_x)
175 {
176 const auto vin = wrapper::vloadq(in_ptr + x);
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000177 const auto vdeq = vdequantize(vin, scale[id.z()]);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100178
179 store_result<T>(reinterpret_cast<T *>(out_ptr + x), vdeq);
180 }
181
182 // Compute left-over elements
183 for(; x < window_end_x; ++x)
184 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000185 int8_t val = *(in_ptr + x);
186 *(out_ptr + x) = static_cast<T>(dequantize(val, scale[id.z()]));
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100187 }
188 },
189 in, out);
190}
191
192template <typename T>
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000193void run_dequantization_qsymm8_per_channel_nhwc(const ITensor *input, ITensor *output, const Window &window)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100194{
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000195 const auto scale = input->info()->quantization_info().scale();
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100196
197 const int window_step_x = 16;
198 const auto window_start_x = static_cast<int>(window.x().start());
199 const auto window_end_x = static_cast<int>(window.x().end());
200
201 // Reset first dimension to handle tail calculations manually
202 Window win(window);
203 win.set(Window::DimX, Window::Dimension(0, 1, 1));
204
205 // Create iterators
206 Iterator in(input, win);
207 Iterator out(output, win);
208
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100209 execute_window_loop(win, [&](const Coordinates &)
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100210 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000211 const auto in_ptr = reinterpret_cast<const int8_t *>(in.ptr());
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100212 const auto out_ptr = reinterpret_cast<T *>(out.ptr());
213
214 int x = window_start_x;
215 for(; x <= (window_end_x - window_step_x); x += window_step_x)
216 {
217 const float32x4x4_t vscale =
218 {
219 {
220 scale[x + 0], scale[x + 1], scale[x + 2], scale[x + 3],
221 scale[x + 4], scale[x + 5], scale[x + 6], scale[x + 7],
222 scale[x + 8], scale[x + 9], scale[x + 10], scale[x + 11],
223 scale[x + 12], scale[x + 13], scale[x + 14], scale[x + 15]
224 }
225 };
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100226 const auto vin = wrapper::vloadq(in_ptr + x);
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000227 const auto vdeq = vdequantize(vin, vscale);
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100228
229 store_result<T>(reinterpret_cast<T *>(out_ptr + x), vdeq);
230 }
231
232 // Compute left-over elements
233 for(; x < window_end_x; ++x)
234 {
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000235 int8_t val = *(in_ptr + x);
236 *(out_ptr + x) = static_cast<T>(dequantize(val, scale[x]));
Michalis Spyrou3f632f32019-08-22 16:52:00 +0100237 }
238 },
239 in, out);
240}
241
242template <typename T>
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100243void run_dequantization_qsymm8(const ITensor *input, ITensor *output, const Window &window)
244{
245 const UniformQuantizationInfo &qinfo = input->info()->quantization_info().uniform();
246 const float scale = qinfo.scale;
247
248 const int window_step_x = 16;
249 const auto window_start_x = static_cast<int>(window.x().start());
250 const auto window_end_x = static_cast<int>(window.x().end());
251
252 // Collapse window and reset first dimension to handle tail calculations manually
253 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
254 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
255
256 // Create iterators
257 Iterator in(input, win_collapsed);
258 Iterator out(output, win_collapsed);
259
260 execute_window_loop(win_collapsed, [&](const Coordinates &)
261 {
262 const auto in_ptr = reinterpret_cast<const int8_t *>(in.ptr());
263 const auto out_ptr = reinterpret_cast<T *>(out.ptr());
264
265 int x = window_start_x;
266 for(; x <= (window_end_x - window_step_x); x += window_step_x)
267 {
268 const auto vin = wrapper::vloadq(in_ptr + x);
269 const auto vdeq = vdequantize(vin, scale);
270
271 store_result<T>(reinterpret_cast<T *>(out_ptr + x), vdeq);
272 }
273
274 // Compute left-over elements
275 for(; x < window_end_x; ++x)
276 {
Manuel Bottini10c53f12019-07-17 16:11:53 +0100277 int8_t val = *(in_ptr + x);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100278 *(out_ptr + x) = static_cast<T>(dequantize(val, scale));
279 }
280 },
281 in, out);
282}
283
284template <typename T>
Michalis Spyrouba27e442019-05-28 10:04:57 +0100285void run_dequantization_qsymm16(const ITensor *input, ITensor *output, const Window &window)
286{
287 const UniformQuantizationInfo &qinfo = input->info()->quantization_info().uniform();
288 const float scale = qinfo.scale;
289
290 const int window_step_x = 8;
291 const auto window_start_x = static_cast<int>(window.x().start());
292 const auto window_end_x = static_cast<int>(window.x().end());
293
294 // Collapse window and reset first dimension to handle tail calculations manually
295 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
296 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
297
298 // Create iterators
299 Iterator in(input, win_collapsed);
300 Iterator out(output, win_collapsed);
301
302 execute_window_loop(win_collapsed, [&](const Coordinates &)
303 {
304 const auto in_ptr = reinterpret_cast<const int16_t *>(in.ptr());
305 const auto out_ptr = reinterpret_cast<T *>(out.ptr());
306
307 int x = window_start_x;
308 for(; x <= (window_end_x - window_step_x); x += window_step_x)
309 {
310 const auto vin = wrapper::vloadq(in_ptr + x);
311 const auto vdeq = vdequantize_int16(vin, scale);
312
313 store_result<T>(reinterpret_cast<T *>(out_ptr + x), vdeq);
314 }
315
316 // Compute left-over elements
317 for(; x < window_end_x; ++x)
318 {
319 int16_t val = *(in_ptr + x);
320 *(out_ptr + x) = static_cast<T>(dequantize_qsymm16(val, scale));
321 }
322 },
323 in, out);
324}
325
326template <typename T>
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100327void run_dequantization_core(const ITensor *input, ITensor *output, const Window &window)
328{
329 switch(input->info()->data_type())
330 {
331 case DataType::QASYMM8:
Sang-Hoon Parkd8176472019-12-04 09:46:28 +0000332 run_dequantization_qasymm8<T, uint8_t>(input, output, window);
333 break;
334 case DataType::QASYMM8_SIGNED:
335 run_dequantization_qasymm8<T, int8_t>(input, output, window);
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100336 break;
Georgios Pinitas8217c8e2019-11-11 18:24:22 +0000337 case DataType::QSYMM8_PER_CHANNEL:
338 input->info()->data_layout() == DataLayout::NHWC ? run_dequantization_qsymm8_per_channel_nhwc<T>(input, output, window) : run_dequantization_qsymm8_per_channel_nchw<T>(input, output, window);
Michalis Spyrou29a01c92019-08-22 11:44:04 +0100339 break;
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100340 case DataType::QSYMM8:
341 run_dequantization_qsymm8<T>(input, output, window);
342 break;
Michalis Spyrouba27e442019-05-28 10:04:57 +0100343 case DataType::QSYMM16:
344 run_dequantization_qsymm16<T>(input, output, window);
345 break;
Georgios Pinitas3d13af82019-06-04 13:04:16 +0100346 default:
347 ARM_COMPUTE_ERROR("Unsupported data type.");
348 }
349}
Alex Gilday60954c62018-03-05 16:22:48 +0000350} // namespace
351
Georgios Pinitasef516e82021-04-30 14:46:05 +0100352void CpuDequantizeKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100353{
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000354 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst));
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100355
Alex Gilday60954c62018-03-05 16:22:48 +0000356 // Configure kernel window
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000357 Window win = calculate_max_window(*src, Steps());
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100358
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000359 // Output tensor auto initialization if not yet initialized
360 auto_init_if_empty(*dst, src->tensor_shape(), 1, DataType::F32);
Gian Marco Iodice06b184a2017-08-29 16:05:25 +0100361
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000362 ICpuKernel::configure(win);
Alex Gilday60954c62018-03-05 16:22:48 +0000363}
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100364
Georgios Pinitasef516e82021-04-30 14:46:05 +0100365Status CpuDequantizeKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
Alex Gilday60954c62018-03-05 16:22:48 +0000366{
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000367 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst));
Alex Gilday60954c62018-03-05 16:22:48 +0000368 return Status{};
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100369}
370
Georgios Pinitasef516e82021-04-30 14:46:05 +0100371void CpuDequantizeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100372{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100373 ARM_COMPUTE_UNUSED(info);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100374 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000375 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100376
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000377 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
378 auto dst = tensors.get_tensor(TensorType::ACL_DST);
379
380 switch(dst->info()->data_type())
Michele Di Giorgio32982d82017-07-07 14:44:43 +0100381 {
Georgios Pinitas574775c2019-02-18 20:08:02 +0000382 case DataType::F32:
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000383 run_dequantization_core<float>(src, dst, window);
Georgios Pinitas574775c2019-02-18 20:08:02 +0000384 break;
385#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
386 case DataType::F16:
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000387 run_dequantization_core<float16_t>(src, dst, window);
Georgios Pinitas574775c2019-02-18 20:08:02 +0000388 break;
389#endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
390 default:
391 ARM_COMPUTE_ERROR("Unsupported data type.");
392 }
393}
Georgios Pinitasef516e82021-04-30 14:46:05 +0100394const char *CpuDequantizeKernel::name() const
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000395{
Georgios Pinitasef516e82021-04-30 14:46:05 +0100396 return "CpuDequantizeKernel";
Manuel Bottini00f4dfc2021-03-10 09:55:14 +0000397}
398} // namespace kernels
399} // namespace cpu
Michalis Spyrouba27e442019-05-28 10:04:57 +0100400} // namespace arm_compute