Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | f1aeab9 | 2023-05-30 13:35:34 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [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/CpuConcatenateWidthKernel.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Steps.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Validate.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 30 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/WindowHelpers.h" |
| 32 | #include "src/core/NEON/NEAsymm.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 33 | |
| 34 | namespace arm_compute |
| 35 | { |
| 36 | namespace cpu |
| 37 | { |
| 38 | namespace kernels |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | Status validate_arguments(const ITensorInfo *src, unsigned int width_offset, const ITensorInfo *dst) |
| 43 | { |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 45 | // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src) is not needed here as this kernel doesn't use CPU FP16 instructions. |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(0) + width_offset > dst->dimension(0)); |
| 49 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | for (size_t i = 1; i < Coordinates::num_max_dimensions; ++i) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(i) != dst->dimension(i)); |
| 53 | } |
| 54 | |
| 55 | return Status{}; |
| 56 | } |
| 57 | } // namespace |
| 58 | |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 59 | void CpuConcatenateWidthKernel::configure(const ITensorInfo *src, unsigned int width_offset, ITensorInfo *dst) |
| 60 | { |
| 61 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 62 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, width_offset, dst)); |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_UNUSED(dst); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 64 | |
| 65 | _width_offset = width_offset; |
| 66 | |
| 67 | // Configure kernel window |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 68 | Window win = calculate_max_window(*src, Steps()); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 69 | |
| 70 | ICpuKernel::configure(win); |
| 71 | } |
| 72 | |
| 73 | Status CpuConcatenateWidthKernel::validate(const ITensorInfo *src, unsigned int width_offset, const ITensorInfo *dst) |
| 74 | { |
| 75 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, width_offset, dst)); |
| 76 | return Status{}; |
| 77 | } |
| 78 | |
| 79 | void CpuConcatenateWidthKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 80 | { |
| 81 | ARM_COMPUTE_UNUSED(info); |
| 82 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 83 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
| 84 | |
| 85 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 86 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 87 | |
| 88 | // Offset output pointer to the correct position |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + |
| 90 | _width_offset * dst->info()->strides_in_bytes()[0]; |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 91 | |
| 92 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 93 | const auto window_end_x = static_cast<int>(window.x().end()) * static_cast<int>(dst->info()->element_size()); |
| 94 | constexpr int window_step_x = 16; |
| 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | Window win{window}; |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 97 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 98 | |
| 99 | // Create iterators |
| 100 | Iterator src_it(src, win); |
| 101 | Iterator dst_it(dst, win); |
| 102 | const DataType dt = src->info()->data_type(); |
| 103 | const UniformQuantizationInfo &src_qinfo = src->info()->quantization_info().uniform(); |
| 104 | const UniformQuantizationInfo &dst_qinfo = dst->info()->quantization_info().uniform(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 105 | if (dt == DataType::QASYMM8 && src_qinfo != dst_qinfo) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 106 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 107 | execute_window_loop( |
| 108 | win, |
| 109 | [&](const Coordinates &) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 110 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 111 | int x = window_start_x; |
| 112 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 113 | { |
| 114 | vst1q_u8(dst_ptr + dst_it.offset() + x, |
| 115 | vquantize(vdequantize(vld1q_u8(src_it.ptr() + x), src_qinfo), dst_qinfo)); |
| 116 | } |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 117 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | // Compute left-over elements |
| 119 | for (; x < window_end_x; ++x) |
| 120 | { |
| 121 | *(dst_ptr + dst_it.offset() + x) = |
| 122 | quantize_qasymm8(dequantize_qasymm8(*(src_it.ptr() + x), src_qinfo), dst_qinfo); |
| 123 | } |
| 124 | }, |
| 125 | src_it, dst_it); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 126 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | else if (dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 128 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | execute_window_loop( |
| 130 | win, |
| 131 | [&](const Coordinates &) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 132 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 133 | int x = window_start_x; |
| 134 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 135 | { |
| 136 | vst1q_s8( |
| 137 | reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset() + x), |
| 138 | vquantize_signed(vdequantize(vld1q_s8(reinterpret_cast<int8_t *>(src_it.ptr() + x)), src_qinfo), |
| 139 | dst_qinfo)); |
| 140 | } |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 141 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 142 | // Compute left-over elements |
| 143 | for (; x < window_end_x; ++x) |
| 144 | { |
| 145 | *(dst_ptr + dst_it.offset() + x) = |
| 146 | quantize_qasymm8_signed(dequantize_qasymm8_signed(*(src_it.ptr() + x), src_qinfo), dst_qinfo); |
| 147 | } |
| 148 | }, |
| 149 | src_it, dst_it); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 150 | } |
| 151 | else |
| 152 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 153 | execute_window_loop( |
| 154 | win, |
| 155 | [&](const Coordinates &) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 156 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 157 | const auto in_ptr = src_it.ptr(); |
| 158 | const auto out_ptr = dst_ptr + dst_it.offset(); |
| 159 | int x = window_start_x; |
| 160 | for (; x <= (window_end_x - window_step_x); x += window_step_x) |
| 161 | { |
| 162 | wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x)); |
| 163 | } |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 164 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 165 | // Compute left-over elements |
| 166 | for (; x < window_end_x; ++x) |
| 167 | { |
| 168 | *(out_ptr + x) = *(in_ptr + x); |
| 169 | } |
| 170 | }, |
| 171 | src_it, dst_it); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 172 | } |
| 173 | } |
| 174 | |
| 175 | const char *CpuConcatenateWidthKernel::name() const |
| 176 | { |
| 177 | return "CpuConcatenateWidthKernel"; |
| 178 | } |
| 179 | } // namespace kernels |
| 180 | } // namespace cpu |
| 181 | } // namespace arm_compute |