Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019-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/core/cpu/kernels/CpuConcatenateBatchKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
| 33 | #include "src/core/NEON/NEAsymm.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 | namespace arm_compute |
| 39 | { |
| 40 | namespace cpu |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
| 44 | namespace |
| 45 | { |
| 46 | template <typename T> |
| 47 | void batch_concat(const ITensor *src, ITensor *dst, unsigned int batch_offset, const Window &window) |
| 48 | { |
| 49 | // Offset src |
| 50 | uint8_t *src_ptr = src->buffer() + src->info()->offset_first_element_in_bytes(); |
| 51 | |
| 52 | // Offset dst |
| 53 | uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + batch_offset * dst->info()->strides_in_bytes()[3]; |
| 54 | |
| 55 | const auto window_start_x = static_cast<int>(window.x().start()); |
| 56 | const auto window_end_x = static_cast<int>(window.x().end()); |
| 57 | const int window_step_x = 16 / dst->info()->element_size(); |
| 58 | |
| 59 | Window win{ window }; |
| 60 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 61 | win.set(3, Window::Dimension(0, src->info()->tensor_shape()[3], 1)); |
| 62 | |
| 63 | Iterator src_it(src, win); |
| 64 | Iterator dst_it(dst, win); |
| 65 | |
| 66 | const DataType dt = src->info()->data_type(); |
| 67 | const UniformQuantizationInfo src_qinfo = src->info()->quantization_info().uniform(); |
| 68 | const UniformQuantizationInfo dst_qinfo = dst->info()->quantization_info().uniform(); |
| 69 | if(dt == DataType::QASYMM8 && src_qinfo != dst_qinfo) |
| 70 | { |
| 71 | execute_window_loop(win, [&](const Coordinates &) |
| 72 | { |
| 73 | const auto in_ptr = reinterpret_cast<const uint8_t *>(src_ptr + src_it.offset()); |
| 74 | const auto out_ptr = reinterpret_cast<uint8_t *>(dst_ptr + dst_it.offset()); |
| 75 | |
| 76 | int x = window_start_x; |
| 77 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 78 | { |
| 79 | wrapper::vstore(out_ptr, vquantize(vdequantize(wrapper::vloadq(in_ptr), src_qinfo), dst_qinfo)); |
| 80 | } |
| 81 | |
| 82 | // Compute left-over elements |
| 83 | for(; x < window_end_x; ++x) |
| 84 | { |
| 85 | *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), src_qinfo), dst_qinfo); |
| 86 | } |
| 87 | }, |
| 88 | src_it, dst_it); |
| 89 | } |
| 90 | else if(dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo) |
| 91 | { |
| 92 | execute_window_loop(win, [&](const Coordinates &) |
| 93 | { |
| 94 | const auto in_ptr = reinterpret_cast<const int8_t *>(src_ptr + src_it.offset()); |
| 95 | const auto out_ptr = reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset()); |
| 96 | int x = window_start_x; |
| 97 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 98 | { |
| 99 | wrapper::vstore(out_ptr, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr), src_qinfo), dst_qinfo)); |
| 100 | } |
| 101 | // Compute left-over elements |
| 102 | for(; x < window_end_x; ++x) |
| 103 | { |
| 104 | *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), src_qinfo), dst_qinfo); |
| 105 | } |
| 106 | }, |
| 107 | src_it, dst_it); |
| 108 | } |
| 109 | else |
| 110 | { |
| 111 | execute_window_loop(win, [&](const Coordinates &) |
| 112 | { |
| 113 | const auto in_ptr = reinterpret_cast<const T *>(src_ptr + src_it.offset()); |
| 114 | const auto out_ptr = reinterpret_cast<T *>(dst_ptr + dst_it.offset()); |
| 115 | |
| 116 | int x = window_start_x; |
| 117 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 118 | { |
| 119 | wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x)); |
| 120 | } |
| 121 | |
| 122 | // Compute left-over elements |
| 123 | for(; x < window_end_x; ++x) |
| 124 | { |
| 125 | *(out_ptr + x) = *(in_ptr + x); |
| 126 | } |
| 127 | }, |
| 128 | src_it, dst_it); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | Status validate_arguments(const ITensorInfo *src, unsigned int batch_offset, const ITensorInfo *dst) |
| 133 | { |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst); |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 135 | //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] | 136 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 137 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 138 | |
| 139 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimX) != dst->dimension(Window::DimX)); |
| 140 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimY) != dst->dimension(Window::DimY)); |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimZ) != dst->dimension(Window::DimZ)); |
| 142 | ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(3) + batch_offset > dst->dimension(3)); |
| 143 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(4, src, dst); |
| 144 | |
| 145 | return Status{}; |
| 146 | } |
| 147 | } // namespace |
| 148 | |
| 149 | CpuConcatenateBatchKernel::CpuConcatenateBatchKernel() |
| 150 | : _func(nullptr), _batch_offset(0) |
| 151 | { |
| 152 | } |
| 153 | |
| 154 | void CpuConcatenateBatchKernel::configure(const ITensorInfo *src, unsigned int batch_offset, ITensorInfo *dst) |
| 155 | { |
| 156 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
| 157 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, batch_offset, dst)); |
| 158 | |
| 159 | _func = nullptr; |
| 160 | _batch_offset = batch_offset; |
| 161 | |
| 162 | switch(src->data_type()) |
| 163 | { |
| 164 | case DataType::S8: |
| 165 | case DataType::U8: |
| 166 | case DataType::QASYMM8: |
| 167 | case DataType::QASYMM8_SIGNED: |
| 168 | _func = &batch_concat<uint8_t>; |
| 169 | break; |
| 170 | case DataType::S16: |
| 171 | case DataType::U16: |
| 172 | case DataType::F16: |
| 173 | _func = &batch_concat<uint16_t>; |
| 174 | break; |
| 175 | case DataType::S32: |
| 176 | case DataType::U32: |
| 177 | case DataType::F32: |
| 178 | _func = &batch_concat<uint32_t>; |
| 179 | break; |
| 180 | default: |
| 181 | ARM_COMPUTE_ERROR("Unsupported data type."); |
| 182 | } |
| 183 | |
| 184 | // Configure kernel window |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 185 | Window win = calculate_max_window(*dst, Steps()); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 186 | ICpuKernel::configure(win); |
| 187 | } |
| 188 | |
| 189 | Status CpuConcatenateBatchKernel::validate(const arm_compute::ITensorInfo *src, |
| 190 | unsigned int batch_offset, |
| 191 | const arm_compute::ITensorInfo *dst) |
| 192 | { |
| 193 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, batch_offset, dst)); |
| 194 | return Status{}; |
| 195 | } |
| 196 | |
| 197 | void CpuConcatenateBatchKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
| 198 | { |
| 199 | ARM_COMPUTE_UNUSED(info); |
| 200 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 201 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
| 202 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 203 | |
| 204 | (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC), |
| 205 | tensors.get_tensor(TensorType::ACL_DST), |
| 206 | _batch_offset, |
| 207 | window); |
| 208 | } |
| 209 | |
| 210 | const char *CpuConcatenateBatchKernel::name() const |
| 211 | { |
| 212 | return "CpuConcatenateBatchKernel"; |
| 213 | } |
| 214 | } // namespace kernels |
| 215 | } // namespace cpu |
| 216 | } // namespace arm_compute |