blob: 8c290173e8f0752efd198e2e0024e32c05b2181a [file] [log] [blame]
Georgios Pinitas61ba0692021-01-10 04:07:39 +00001/*
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuConcatenateBatchKernel.h"
Georgios Pinitas61ba0692021-01-10 04:07:39 +000025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
Georgios Pinitas61ba0692021-01-10 04:07:39 +000028#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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Georgios Pinitas61ba0692021-01-10 04:07:39 +000034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010036#include "src/core/NEON/NEAsymm.h"
37#include "src/core/NEON/wrapper/wrapper.h"
Georgios Pinitas61ba0692021-01-10 04:07:39 +000038
39namespace arm_compute
40{
41namespace cpu
42{
43namespace kernels
44{
45namespace
46{
47template <typename T>
48void batch_concat(const ITensor *src, ITensor *dst, unsigned int batch_offset, const Window &window)
49{
50 // Offset src
51 uint8_t *src_ptr = src->buffer() + src->info()->offset_first_element_in_bytes();
52
53 // Offset dst
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010054 uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() +
55 batch_offset * dst->info()->strides_in_bytes()[3];
Georgios Pinitas61ba0692021-01-10 04:07:39 +000056
57 const auto window_start_x = static_cast<int>(window.x().start());
58 const auto window_end_x = static_cast<int>(window.x().end());
59 const int window_step_x = 16 / dst->info()->element_size();
60
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010061 Window win{window};
Georgios Pinitas61ba0692021-01-10 04:07:39 +000062 win.set(Window::DimX, Window::Dimension(0, 1, 1));
63 win.set(3, Window::Dimension(0, src->info()->tensor_shape()[3], 1));
64
65 Iterator src_it(src, win);
66 Iterator dst_it(dst, win);
67
68 const DataType dt = src->info()->data_type();
69 const UniformQuantizationInfo src_qinfo = src->info()->quantization_info().uniform();
70 const UniformQuantizationInfo dst_qinfo = dst->info()->quantization_info().uniform();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 if (dt == DataType::QASYMM8 && src_qinfo != dst_qinfo)
Georgios Pinitas61ba0692021-01-10 04:07:39 +000072 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010073 execute_window_loop(
74 win,
75 [&](const Coordinates &)
Georgios Pinitas61ba0692021-01-10 04:07:39 +000076 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_ptr + src_it.offset());
78 const auto out_ptr = reinterpret_cast<uint8_t *>(dst_ptr + dst_it.offset());
Georgios Pinitas61ba0692021-01-10 04:07:39 +000079
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080 int x = window_start_x;
81 for (; x <= (window_end_x - window_step_x); x += window_step_x)
82 {
83 wrapper::vstore(out_ptr, vquantize(vdequantize(wrapper::vloadq(in_ptr), src_qinfo), dst_qinfo));
84 }
85
86 // Compute left-over elements
87 for (; x < window_end_x; ++x)
88 {
89 *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), src_qinfo), dst_qinfo);
90 }
91 },
92 src_it, dst_it);
Georgios Pinitas61ba0692021-01-10 04:07:39 +000093 }
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 else if (dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo)
Georgios Pinitas61ba0692021-01-10 04:07:39 +000095 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 execute_window_loop(
97 win,
98 [&](const Coordinates &)
Georgios Pinitas61ba0692021-01-10 04:07:39 +000099 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 const auto in_ptr = reinterpret_cast<const int8_t *>(src_ptr + src_it.offset());
101 const auto out_ptr = reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset());
102 int x = window_start_x;
103 for (; x <= (window_end_x - window_step_x); x += window_step_x)
104 {
105 wrapper::vstore(out_ptr,
106 vquantize_signed(vdequantize(wrapper::vloadq(in_ptr), src_qinfo), dst_qinfo));
107 }
108 // Compute left-over elements
109 for (; x < window_end_x; ++x)
110 {
111 *(out_ptr + x) =
112 quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), src_qinfo), dst_qinfo);
113 }
114 },
115 src_it, dst_it);
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000116 }
117 else
118 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100119 execute_window_loop(
120 win,
121 [&](const Coordinates &)
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000122 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100123 const auto in_ptr = reinterpret_cast<const T *>(src_ptr + src_it.offset());
124 const auto out_ptr = reinterpret_cast<T *>(dst_ptr + dst_it.offset());
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000125
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100126 int x = window_start_x;
127 for (; x <= (window_end_x - window_step_x); x += window_step_x)
128 {
129 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
130 }
131
132 // Compute left-over elements
133 for (; x < window_end_x; ++x)
134 {
135 *(out_ptr + x) = *(in_ptr + x);
136 }
137 },
138 src_it, dst_it);
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000139 }
140}
141
142Status validate_arguments(const ITensorInfo *src, unsigned int batch_offset, const ITensorInfo *dst)
143{
144 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000145 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(src) is not needed here as this kernel doesn't use CPU FP16 instructions.
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000146 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
147 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
148
149 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimX) != dst->dimension(Window::DimX));
150 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimY) != dst->dimension(Window::DimY));
151 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimZ) != dst->dimension(Window::DimZ));
152 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(3) + batch_offset > dst->dimension(3));
153 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(4, src, dst);
154
155 return Status{};
156}
157} // namespace
158
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000159void CpuConcatenateBatchKernel::configure(const ITensorInfo *src, unsigned int batch_offset, ITensorInfo *dst)
160{
161 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
162 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, batch_offset, dst));
163
164 _func = nullptr;
165 _batch_offset = batch_offset;
166
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100167 switch (src->data_type())
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000168 {
169 case DataType::S8:
170 case DataType::U8:
171 case DataType::QASYMM8:
172 case DataType::QASYMM8_SIGNED:
173 _func = &batch_concat<uint8_t>;
174 break;
175 case DataType::S16:
176 case DataType::U16:
177 case DataType::F16:
178 _func = &batch_concat<uint16_t>;
179 break;
180 case DataType::S32:
181 case DataType::U32:
182 case DataType::F32:
183 _func = &batch_concat<uint32_t>;
184 break;
185 default:
186 ARM_COMPUTE_ERROR("Unsupported data type.");
187 }
188
189 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000190 Window win = calculate_max_window(*dst, Steps());
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000191 ICpuKernel::configure(win);
192}
193
194Status CpuConcatenateBatchKernel::validate(const arm_compute::ITensorInfo *src,
195 unsigned int batch_offset,
196 const arm_compute::ITensorInfo *dst)
197{
198 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, batch_offset, dst));
199 return Status{};
200}
201
202void CpuConcatenateBatchKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
203{
204 ARM_COMPUTE_UNUSED(info);
205 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
206 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
207 ARM_COMPUTE_ERROR_ON(_func == nullptr);
208
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100209 (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC), tensors.get_tensor(TensorType::ACL_DST), _batch_offset,
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000210 window);
211}
212
213const char *CpuConcatenateBatchKernel::name() const
214{
215 return "CpuConcatenateBatchKernel";
216}
217} // namespace kernels
218} // namespace cpu
219} // namespace arm_compute