Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2021 Arm Limited. |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/kernels/CpuConvertQuantizedSignednessKernel.h" |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/Window.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 32 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 35 | |
| 36 | namespace arm_compute |
| 37 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 38 | namespace cpu |
| 39 | { |
| 40 | namespace kernels |
| 41 | { |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 42 | namespace |
| 43 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 44 | Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 45 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(src, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 48 | |
| 49 | // Validate output if initialized |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 50 | if(dst->total_size() != 0) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 51 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(dst, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(src->tensor_shape(), dst->tensor_shape()); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | return Status{}; |
| 57 | } |
| 58 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 59 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *src, ITensorInfo *dst) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 60 | { |
| 61 | // Output auto inizialitation if not yet initialized |
| 62 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 63 | const bool is_input_signed = src->data_type() == DataType::QASYMM8_SIGNED; |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 64 | const DataType dt = is_input_signed ? DataType::QASYMM8 : DataType::QASYMM8_SIGNED; |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 65 | const UniformQuantizationInfo qinfo = src->quantization_info().uniform(); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 66 | const int offset_correction = is_input_signed ? -128 : 128; |
| 67 | const QuantizationInfo corrected_qinfo = QuantizationInfo(qinfo.scale, qinfo.offset + offset_correction); |
| 68 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 69 | auto_init_if_empty(*dst, src->clone()->set_data_type(dt).set_quantization_info(corrected_qinfo)); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 72 | return std::make_pair(Status{}, calculate_max_window(*dst)); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 73 | } |
| 74 | } // namespace |
| 75 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 76 | void CpuConvertQuantizedSignednessKernel::configure(const ITensorInfo *src, ITensorInfo *dst) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 77 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 79 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst)); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 80 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 81 | std::pair<Status, Window> win_config = validate_and_configure_window(src, dst); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 82 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 83 | ICpuKernel::configure(win_config.second); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 84 | } |
| 85 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 86 | Status CpuConvertQuantizedSignednessKernel::validate(const ITensorInfo *src, const ITensorInfo *dst) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 87 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 88 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst)); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 89 | return Status{}; |
| 90 | } |
| 91 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 92 | void CpuConvertQuantizedSignednessKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 93 | { |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 94 | auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 95 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 96 | ARM_COMPUTE_UNUSED(info); |
| 97 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 98 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 99 | |
| 100 | Window win_collapsed = window.collapse_if_possible(window, Window::DimZ); |
| 101 | win_collapsed.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 102 | |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 103 | Iterator input(src, win_collapsed); |
| 104 | Iterator output(dst, win_collapsed); |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 105 | |
| 106 | const int window_step_x = 16; |
| 107 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 108 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 109 | |
| 110 | const uint8_t mask = 128; |
| 111 | const auto vmask = wrapper::vdup_n(mask, wrapper::traits::vector_128_tag{}); |
| 112 | |
| 113 | execute_window_loop(win_collapsed, [&](const Coordinates &) |
| 114 | { |
| 115 | const auto input_ptr = reinterpret_cast<const uint8_t *>(input.ptr()); |
| 116 | const auto output_ptr = reinterpret_cast<uint8_t *>(output.ptr()); |
| 117 | |
| 118 | // Compute S elements per iteration |
| 119 | int x = window_start_x; |
| 120 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 121 | { |
| 122 | const auto vin = wrapper::vloadq(input_ptr + x); |
| 123 | wrapper::vstore(output_ptr + x, wrapper::veor(vin, vmask)); |
| 124 | } |
| 125 | |
| 126 | // Compute left-over elements |
| 127 | for(; x < window_end_x; ++x) |
| 128 | { |
| 129 | const uint8_t in = *(reinterpret_cast<const uint8_t *>(input_ptr + x)); |
| 130 | *(output_ptr + x) = in ^ mask; |
| 131 | } |
| 132 | }, |
| 133 | input, output); |
| 134 | } |
Manuel Bottini | cfac51c | 2021-06-18 15:47:28 +0100 | [diff] [blame] | 135 | |
| 136 | const char *CpuConvertQuantizedSignednessKernel::name() const |
| 137 | { |
| 138 | return "CpuConvertQuantizedSignednessKernel"; |
| 139 | } |
| 140 | } // namespace kernels |
| 141 | } // namespace cpu |
Georgios Pinitas | dbdea0d | 2019-10-16 19:21:40 +0100 | [diff] [blame] | 142 | } // namespace arm_compute |