blob: f40f1215d3923e6bcad2d79450327552eb016b26 [file] [log] [blame]
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01001/*
Georgios Pinitasddb93bb2020-10-02 16:38:59 +01002 * Copyright (c) 2019-2020 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 */
24#include "arm_compute/core/NEON/kernels/NEConvertQuantizedSignednessKernel.h"
25
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"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010032#include "src/core/NEON/wrapper/wrapper.h"
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +010033
34namespace arm_compute
35{
36namespace
37{
38Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
39{
40 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
41 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
42
43 // Validate output if initialized
44 if(output->total_size() != 0)
45 {
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED);
47 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(input->tensor_shape(), output->tensor_shape());
48 }
49
50 return Status{};
51}
52
53std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
54{
55 // Output auto inizialitation if not yet initialized
56 {
57 const bool is_input_signed = input->data_type() == DataType::QASYMM8_SIGNED;
58 const DataType dt = is_input_signed ? DataType::QASYMM8 : DataType::QASYMM8_SIGNED;
59 const UniformQuantizationInfo qinfo = input->quantization_info().uniform();
60 const int offset_correction = is_input_signed ? -128 : 128;
61 const QuantizationInfo corrected_qinfo = QuantizationInfo(qinfo.scale, qinfo.offset + offset_correction);
62
63 auto_init_if_empty(*output, input->clone()->set_data_type(dt).set_quantization_info(corrected_qinfo));
64 }
65
66 return std::make_pair(Status{}, calculate_max_window(*output));
67}
68} // namespace
69
70NEConvertQuantizedSignednessKernel::NEConvertQuantizedSignednessKernel()
71 : _input(nullptr), _output(nullptr)
72{
73}
74
75void NEConvertQuantizedSignednessKernel::configure(const ITensor *input, ITensor *output)
76{
77 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
78 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
79
80 _input = input;
81 _output = output;
82
83 std::pair<Status, Window> win_config = validate_and_configure_window(input->info(), output->info());
84 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
85 INEKernel::configure(win_config.second);
86}
87
88Status NEConvertQuantizedSignednessKernel::validate(const arm_compute::ITensorInfo *input, const arm_compute::ITensorInfo *output)
89{
90 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
91 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
92 return Status{};
93}
94
95void NEConvertQuantizedSignednessKernel::run(const Window &window, const ThreadInfo &info)
96{
97 ARM_COMPUTE_UNUSED(info);
98 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
99 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
100
101 Window win_collapsed = window.collapse_if_possible(window, Window::DimZ);
102 win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1));
103
104 Iterator input(_input, win_collapsed);
105 Iterator output(_output, win_collapsed);
106
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
114 execute_window_loop(win_collapsed, [&](const Coordinates &)
115 {
116 const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr());
117 const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr());
118
119 // Compute S elements per iteration
120 int x = window_start_x;
121 for(; x <= (window_end_x - window_step_x); x += window_step_x)
122 {
123 const auto vin = wrapper::vloadq(input_ptr + x);
124 wrapper::vstore(output_ptr + x, wrapper::veor(vin, vmask));
125 }
126
127 // Compute left-over elements
128 for(; x < window_end_x; ++x)
129 {
130 const uint8_t in = *(reinterpret_cast<const uint8_t *>(input_ptr + x));
131 *(output_ptr + x) = in ^ mask;
132 }
133 },
134 input, output);
135}
136} // namespace arm_compute