blob: 54b972662b7dc6b2fa6f7bbf09fd26b9fa2b2703 [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/CpuConcatenateHeightKernel.h"
25
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"
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#include <cstdint>
39
40namespace arm_compute
41{
42namespace cpu
43{
44namespace kernels
45{
46namespace
47{
48Status validate_arguments(const ITensorInfo *src, unsigned int height_offset, const ITensorInfo *dst)
49{
50 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src, dst);
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +000051 // 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 +000052 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
53 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
54 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimX) != dst->dimension(Window::DimX));
55 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(Window::DimY) + height_offset > dst->dimension(Window::DimY));
56 for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON(src->dimension(i) != dst->dimension(i));
59 }
60
61 return Status{};
62}
63} // namespace
64
65CpuConcatenateHeightKernel::CpuConcatenateHeightKernel()
66 : _height_offset(0)
67{
68}
69
70void CpuConcatenateHeightKernel::configure(const ITensorInfo *src, unsigned int height_offset, ITensorInfo *dst)
71{
72 ARM_COMPUTE_UNUSED(src);
73 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
74 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, height_offset, dst));
75
76 _height_offset = height_offset;
77
78 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +000079 Window win = calculate_max_window(*dst, Steps());
Georgios Pinitas61ba0692021-01-10 04:07:39 +000080 ICpuKernel::configure(win);
81}
82
83Status CpuConcatenateHeightKernel::validate(const ITensorInfo *src, unsigned int height_offset, const ITensorInfo *dst)
84{
85 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, height_offset, dst));
86 return Status{};
87}
88
89void CpuConcatenateHeightKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
90{
91 ARM_COMPUTE_UNUSED(info);
92 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
93 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
94
95 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
96 auto dst = tensors.get_tensor(TensorType::ACL_DST);
97
98 // Offset destination pointer to the correct position
99 uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + _height_offset * dst->info()->strides_in_bytes()[Window::DimY];
100
101 const auto window_start_x = static_cast<int>(window.x().start());
102 const auto window_end_x = static_cast<int>(window.x().end()) * static_cast<int>(dst->info()->element_size());
103 const int window_step_x = 16;
104
105 Window win{ window };
106 win.set(Window::DimX, Window::Dimension(0, 1, 1));
107 win.set(Window::DimY, Window::Dimension(0, src->info()->tensor_shape().y(), 1));
108
109 // Create iterators
110 Iterator src_it(src, win);
111 Iterator dst_it(dst, win);
112
113 const DataType dt = src->info()->data_type();
114 const UniformQuantizationInfo &src_qinfo = src->info()->quantization_info().uniform();
115 const UniformQuantizationInfo &dst_qinfo = dst->info()->quantization_info().uniform();
116 if(dt == DataType::QASYMM8 && src_qinfo != dst_qinfo)
117 {
118 execute_window_loop(win, [&](const Coordinates &)
119 {
120 int x = window_start_x;
121 for(; x <= (window_end_x - window_step_x); x += window_step_x)
122 {
123 vst1q_u8(dst_ptr + dst_it.offset() + x, vquantize(vdequantize(vld1q_u8(src_it.ptr() + x), src_qinfo), dst_qinfo));
124 }
125
126 // Compute left-over elements
127 for(; x < window_end_x; ++x)
128 {
129 *(dst_ptr + dst_it.offset() + x) = quantize_qasymm8(dequantize_qasymm8(*(src_it.ptr() + x), src_qinfo), dst_qinfo);
130 }
131
132 },
133 src_it, dst_it);
134 }
135 else if(dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo)
136 {
137 execute_window_loop(win, [&](const Coordinates &)
138 {
139 int x = window_start_x;
140 for(; x <= (window_end_x - window_step_x); x += window_step_x)
141 {
142 vst1q_s8(reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset() + x),
143 vquantize_signed(vdequantize(vld1q_s8(reinterpret_cast<int8_t *>(src_it.ptr()) + x), src_qinfo), dst_qinfo));
144 }
145
146 // Compute left-over elements
147 for(; x < window_end_x; ++x)
148 {
149 *(dst_ptr + dst_it.offset() + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(src_it.ptr() + x), src_qinfo), dst_qinfo);
150 }
151 },
152 src_it, dst_it);
153 }
154 else
155 {
156 execute_window_loop(win, [&](const Coordinates &)
157 {
158 const auto in_ptr = src_it.ptr();
159 const auto out_ptr = dst_ptr + dst_it.offset();
160
161 int x = window_start_x;
162 for(; x <= (window_end_x - window_step_x); x += window_step_x)
163 {
164 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
165 }
166
167 // Compute left-over elements
168 for(; x < window_end_x; ++x)
169 {
170 *(out_ptr + x) = *(in_ptr + x);
171 }
172 },
173 src_it, dst_it);
174 }
175}
176
177const char *CpuConcatenateHeightKernel::name() const
178{
179 return "CpuConcatenateHeightKernel";
180}
181} // namespace kernels
182} // namespace cpu
183} // namespace arm_compute