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