xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuDequantizeKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2021 Arm Limited.
3  *
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 "src/cpu/kernels/CpuDequantizeKernel.h"
25 
26 #include "arm_compute/core/Error.h"
27 #include "arm_compute/core/Helpers.h"
28 #include "arm_compute/core/Utils.h"
29 #include "arm_compute/core/Validate.h"
30 #include "arm_compute/core/Window.h"
31 #include "src/core/CPP/Validate.h"
32 #include "src/core/NEON/NEAsymm.h"
33 #include "src/core/NEON/NESymm.h"
34 #include "src/core/NEON/wrapper/wrapper.h"
35 #include "src/core/helpers/AutoConfiguration.h"
36 #include "src/core/helpers/WindowHelpers.h"
37 
38 #include <arm_neon.h>
39 
40 namespace arm_compute
41 {
42 namespace cpu
43 {
44 namespace kernels
45 {
46 namespace
47 {
validate_arguments(const ITensorInfo * src,const ITensorInfo * dst)48 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst)
49 {
50     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);
52 
53     if(dst->tensor_shape().total_size() > 0)
54     {
55         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);
58     }
59 
60     return Status{};
61 }
62 
63 template <typename T>
store_result(T * ptr,const float32x4x4_t & v)64 inline void store_result(T *ptr, const float32x4x4_t &v)
65 {
66     ARM_COMPUTE_UNUSED(ptr, v);
67 }
68 
69 template <>
store_result(float * ptr,const float32x4x4_t & v)70 inline 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 }
77 
78 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
79 template <>
store_result(float16_t * ptr,const float32x4x4_t & v)80 inline 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 
87 template <typename T>
store_result(T * ptr,const float32x4x2_t & v)88 inline void store_result(T *ptr, const float32x4x2_t &v)
89 {
90     ARM_COMPUTE_UNUSED(ptr, v);
91 }
92 
93 template <>
store_result(float * ptr,const float32x4x2_t & v)94 inline 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
101 template <>
store_result(float16_t * ptr,const float32x4x2_t & v)102 inline 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 
108 template <typename TOut, typename TIn>
run_dequantization_qasymm8(const ITensor * input,ITensor * output,const Window & window)109 void run_dequantization_qasymm8(const ITensor *input, ITensor *output, const Window &window)
110 {
111     const UniformQuantizationInfo &qinfo  = input->info()->quantization_info().uniform();
112     const float                    scale  = qinfo.scale;
113     const int32_t                  offset = qinfo.offset;
114 
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 
127     execute_window_loop(win_collapsed, [&](const Coordinates &)
128     {
129         const auto in_ptr  = reinterpret_cast<const TIn *>(in.ptr());
130         const auto out_ptr = reinterpret_cast<TOut *>(out.ptr());
131 
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);
136             const auto vdeq = vdequantize(vin, scale, offset);
137 
138             store_result(reinterpret_cast<TOut *>(out_ptr + x), vdeq);
139         }
140 
141         // Compute left-over elements
142         for(; x < window_end_x; ++x)
143         {
144             auto val       = *(in_ptr + x);
145             *(out_ptr + x) = static_cast<TOut>(Qasymm8QuantizationHelper<TIn>::dequantize(val, qinfo));
146         }
147     },
148     in, out);
149 }
150 
151 template <typename T>
run_dequantization_qsymm8_per_channel_nchw(const ITensor * input,ITensor * output,const Window & window)152 void run_dequantization_qsymm8_per_channel_nchw(const ITensor *input, ITensor *output, const Window &window)
153 {
154     const auto scale = input->info()->quantization_info().scale();
155 
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     {
170         const auto in_ptr  = reinterpret_cast<const int8_t *>(in.ptr());
171         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);
177             const auto vdeq = vdequantize(vin, scale[id.z()]);
178 
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         {
185             int8_t val     = *(in_ptr + x);
186             *(out_ptr + x) = static_cast<T>(dequantize(val, scale[id.z()]));
187         }
188     },
189     in, out);
190 }
191 
192 template <typename T>
run_dequantization_qsymm8_per_channel_nhwc(const ITensor * input,ITensor * output,const Window & window)193 void run_dequantization_qsymm8_per_channel_nhwc(const ITensor *input, ITensor *output, const Window &window)
194 {
195     const auto scale = input->info()->quantization_info().scale();
196 
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 
209     execute_window_loop(win, [&](const Coordinates &)
210     {
211         const auto in_ptr  = reinterpret_cast<const int8_t *>(in.ptr());
212         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             };
226             const auto vin  = wrapper::vloadq(in_ptr + x);
227             const auto vdeq = vdequantize(vin, vscale);
228 
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         {
235             int8_t val     = *(in_ptr + x);
236             *(out_ptr + x) = static_cast<T>(dequantize(val, scale[x]));
237         }
238     },
239     in, out);
240 }
241 
242 template <typename T>
run_dequantization_qsymm8(const ITensor * input,ITensor * output,const Window & window)243 void 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         {
277             int8_t val     = *(in_ptr + x);
278             *(out_ptr + x) = static_cast<T>(dequantize(val, scale));
279         }
280     },
281     in, out);
282 }
283 
284 template <typename T>
run_dequantization_qsymm16(const ITensor * input,ITensor * output,const Window & window)285 void 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 
326 template <typename T>
run_dequantization_core(const ITensor * input,ITensor * output,const Window & window)327 void run_dequantization_core(const ITensor *input, ITensor *output, const Window &window)
328 {
329     switch(input->info()->data_type())
330     {
331         case DataType::QASYMM8:
332             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);
336             break;
337         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);
339             break;
340         case DataType::QSYMM8:
341             run_dequantization_qsymm8<T>(input, output, window);
342             break;
343         case DataType::QSYMM16:
344             run_dequantization_qsymm16<T>(input, output, window);
345             break;
346         default:
347             ARM_COMPUTE_ERROR("Unsupported data type.");
348     }
349 }
350 } // namespace
351 
configure(const ITensorInfo * src,ITensorInfo * dst)352 void CpuDequantizeKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
353 {
354     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst));
355 
356     // Configure kernel window
357     Window win = calculate_max_window(*src, Steps());
358 
359     // Output tensor auto initialization if not yet initialized
360     auto_init_if_empty(*dst, src->tensor_shape(), 1, DataType::F32);
361 
362     ICpuKernel::configure(win);
363 }
364 
validate(const ITensorInfo * src,const ITensorInfo * dst)365 Status CpuDequantizeKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
366 {
367     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst));
368     return Status{};
369 }
370 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)371 void CpuDequantizeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
372 {
373     ARM_COMPUTE_UNUSED(info);
374     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
375     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
376 
377     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())
381     {
382         case DataType::F32:
383             run_dequantization_core<float>(src, dst, window);
384             break;
385 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
386         case DataType::F16:
387             run_dequantization_core<float16_t>(src, dst, window);
388             break;
389 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
390         default:
391             ARM_COMPUTE_ERROR("Unsupported data type.");
392     }
393 }
name() const394 const char *CpuDequantizeKernel::name() const
395 {
396     return "CpuDequantizeKernel";
397 }
398 } // namespace kernels
399 } // namespace cpu
400 } // namespace arm_compute
401