blob: 745b1566c22cf8a198bc8c96e4f7866809222334 [file] [log] [blame]
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001/*
Manuel Bottinicfac51c2021-06-18 15:47:28 +01002 * Copyright (c) 2019-2021 Arm Limited.
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +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/CpuConvertQuantizedSignednessKernel.h"
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
28#include "arm_compute/core/ITensor.h"
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010029#include "arm_compute/core/TensorInfo.h"
30#include "arm_compute/core/Validate.h"
31#include "arm_compute/core/Window.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010035#include "src/core/NEON/wrapper/wrapper.h"
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010036
37namespace arm_compute
38{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010039namespace cpu
40{
41namespace kernels
42{
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010043namespace
44{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010045Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010046{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
48 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010049
50 // Validate output if initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 if (dst->total_size() != 0)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010052 {
Manuel Bottinicfac51c2021-06-18 15:47:28 +010053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
54 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(src->tensor_shape(), dst->tensor_shape());
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010055 }
56
57 return Status{};
58}
59
Manuel Bottinicfac51c2021-06-18 15:47:28 +010060std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010061{
62 // Output auto inizialitation if not yet initialized
63 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010064 const bool is_input_signed = src->data_type() == DataType::QASYMM8_SIGNED;
65 const DataType dt = is_input_signed ? DataType::QASYMM8 : DataType::QASYMM8_SIGNED;
66 const UniformQuantizationInfo qinfo = src->quantization_info().uniform();
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010067 const int offset_correction = is_input_signed ? -128 : 128;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 const QuantizationInfo corrected_qinfo = QuantizationInfo(qinfo.scale, qinfo.offset + offset_correction);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010069
Manuel Bottinicfac51c2021-06-18 15:47:28 +010070 auto_init_if_empty(*dst, src->clone()->set_data_type(dt).set_quantization_info(corrected_qinfo));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010071 }
72
Manuel Bottinicfac51c2021-06-18 15:47:28 +010073 return std::make_pair(Status{}, calculate_max_window(*dst));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010074}
75} // namespace
76
Manuel Bottinicfac51c2021-06-18 15:47:28 +010077void CpuConvertQuantizedSignednessKernel::configure(const ITensorInfo *src, ITensorInfo *dst)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010078{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010079 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
80 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010081
Manuel Bottinicfac51c2021-06-18 15:47:28 +010082 std::pair<Status, Window> win_config = validate_and_configure_window(src, dst);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010083 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
Manuel Bottinicfac51c2021-06-18 15:47:28 +010084 ICpuKernel::configure(win_config.second);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010085}
86
Manuel Bottinicfac51c2021-06-18 15:47:28 +010087Status CpuConvertQuantizedSignednessKernel::validate(const ITensorInfo *src, const ITensorInfo *dst)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010088{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010089 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst));
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010090 return Status{};
91}
92
Manuel Bottinicfac51c2021-06-18 15:47:28 +010093void CpuConvertQuantizedSignednessKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010094{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010095 auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
96 auto dst = tensors.get_tensor(TensorType::ACL_DST);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010097 ARM_COMPUTE_UNUSED(info);
98 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottinicfac51c2021-06-18 15:47:28 +010099 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100100
101 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
102 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
103
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100104 Iterator input(src, win_collapsed);
105 Iterator output(dst, win_collapsed);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100106
107 const int window_step_x = 16;
108 const auto window_start_x = static_cast<int>(window.x().start());
109 const auto window_end_x = static_cast<int>(window.x().end());
110
111 const uint8_t mask = 128;
112 const auto vmask = wrapper::vdup_n(mask, wrapper::traits::vector_128_tag{});
113
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100114 execute_window_loop(
115 win_collapsed,
116 [&](const Coordinates &)
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100117 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr());
119 const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100120
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100121 // Compute S elements per iteration
122 int x = window_start_x;
123 for (; x <= (window_end_x - window_step_x); x += window_step_x)
124 {
125 const auto vin = wrapper::vloadq(input_ptr + x);
126 wrapper::vstore(output_ptr + x, wrapper::veor(vin, vmask));
127 }
128
129 // Compute left-over elements
130 for (; x < window_end_x; ++x)
131 {
132 const uint8_t in = *(reinterpret_cast<const uint8_t *>(input_ptr + x));
133 *(output_ptr + x) = in ^ mask;
134 }
135 },
136 input, output);
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100137}
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100138
139const char *CpuConvertQuantizedSignednessKernel::name() const
140{
141 return "CpuConvertQuantizedSignednessKernel";
142}
143} // namespace kernels
144} // namespace cpu
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +0100145} // namespace arm_compute