blob: a7e5cd8c60fe5472304bcc2e6ceb52e7ebf239d3 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Georgios Pinitas61ba0692021-01-10 04:07:39 +00002 * Copyright (c) 2017-2021 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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 Pinitas61ba0692021-01-10 04:07:39 +000024#include "src/core/cpu/kernels/CpuConcatenateDepthKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010025
26#include "arm_compute/core/Error.h"
27#include "arm_compute/core/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#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"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010033#include "src/core/NEON/NEAsymm.h"
34#include "src/core/NEON/NEFixedPoint.h"
35#include "src/core/NEON/wrapper/wrapper.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010036#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
Georgios Pinitasac4e8732017-07-05 17:02:25 +010039#include <cstdint>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040
Michalis Spyrouc28d4282020-03-04 15:30:41 +000041namespace arm_compute
42{
Georgios Pinitas61ba0692021-01-10 04:07:39 +000043namespace cpu
44{
45namespace kernels
46{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010047namespace
48{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010049template <typename T>
Georgios Pinitas61ba0692021-01-10 04:07:39 +000050void depth_concat(const ITensor *src, ITensor *dst, unsigned int depth_offset, const Window &window)
Georgios Pinitasac4e8732017-07-05 17:02:25 +010051{
Georgios Pinitas61ba0692021-01-10 04:07:39 +000052 // Offset source
53 uint8_t *src_ptr = src->buffer() + src->info()->offset_first_element_in_bytes();
Georgios Pinitasac4e8732017-07-05 17:02:25 +010054
Georgios Pinitas61ba0692021-01-10 04:07:39 +000055 // Offset destination
56 uint8_t *dst_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + depth_offset * dst->info()->strides_in_bytes()[2];
Georgios Pinitasac4e8732017-07-05 17:02:25 +010057
Michalis Spyrouc28d4282020-03-04 15:30:41 +000058 const auto window_start_x = static_cast<int>(window.x().start());
59 const auto window_end_x = static_cast<int>(window.x().end());
Georgios Pinitas61ba0692021-01-10 04:07:39 +000060 const int window_step_x = 16 / dst->info()->element_size();
Michalis Spyrouc28d4282020-03-04 15:30:41 +000061
62 Window win{ window };
63 win.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitas61ba0692021-01-10 04:07:39 +000064 win.set(Window::DimZ, Window::Dimension(0, src->info()->tensor_shape().z(), 1));
Michalis Spyrouc28d4282020-03-04 15:30:41 +000065
Georgios Pinitas61ba0692021-01-10 04:07:39 +000066 Iterator src_it(src, win);
67 Iterator dst_it(dst, win);
Georgios Pinitasac4e8732017-07-05 17:02:25 +010068
Georgios Pinitas61ba0692021-01-10 04:07:39 +000069 const DataType dt = src->info()->data_type();
70 const UniformQuantizationInfo src_qinfo = src->info()->quantization_info().uniform();
71 const UniformQuantizationInfo dst_qinfo = dst->info()->quantization_info().uniform();
72 if(dt == DataType::QASYMM8 && src_qinfo != dst_qinfo)
Georgios Pinitasac4e8732017-07-05 17:02:25 +010073 {
Michalis Spyrouc28d4282020-03-04 15:30:41 +000074 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +000075 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +000076 const auto in_ptr = reinterpret_cast<const uint8_t *>(src_ptr + src_it.offset());
77 const auto out_ptr = reinterpret_cast<uint8_t *>(dst_ptr + dst_it.offset());
Michalis Spyrouc28d4282020-03-04 15:30:41 +000078 int x = window_start_x;
79 for(; x <= (window_end_x - window_step_x); x += window_step_x)
80 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +000081 wrapper::vstore(out_ptr + x, vquantize(vdequantize(wrapper::vloadq(in_ptr + x), src_qinfo), dst_qinfo));
Michalis Spyrouc28d4282020-03-04 15:30:41 +000082 }
83
84 // Compute left-over elements
85 for(; x < window_end_x; ++x)
86 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +000087 *(out_ptr + x) = quantize_qasymm8(dequantize_qasymm8(*(in_ptr + x), src_qinfo), dst_qinfo);
Michalis Spyrouc28d4282020-03-04 15:30:41 +000088 }
Pablo Tello54e98d92019-02-05 16:16:19 +000089 },
Georgios Pinitas61ba0692021-01-10 04:07:39 +000090 src_it, dst_it);
Pablo Tello54e98d92019-02-05 16:16:19 +000091 }
Georgios Pinitas61ba0692021-01-10 04:07:39 +000092 else if(dt == DataType::QASYMM8_SIGNED && src_qinfo != dst_qinfo)
Georgios Pinitas33843562019-12-10 13:33:18 +000093 {
Michalis Spyrouc28d4282020-03-04 15:30:41 +000094 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +000095 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +000096 const auto in_ptr = reinterpret_cast<const int8_t *>(src_ptr + src_it.offset());
97 const auto out_ptr = reinterpret_cast<int8_t *>(dst_ptr + dst_it.offset());
Michalis Spyrouc28d4282020-03-04 15:30:41 +000098 int x = window_start_x;
99 for(; x <= (window_end_x - window_step_x); x += window_step_x)
100 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000101 wrapper::vstore(out_ptr + x, vquantize_signed(vdequantize(wrapper::vloadq(in_ptr + x), src_qinfo), dst_qinfo));
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000102 }
103
104 // Compute left-over elements
105 for(; x < window_end_x; ++x)
106 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000107 *(out_ptr + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(in_ptr + x), src_qinfo), dst_qinfo);
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000108 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000109 },
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000110 src_it, dst_it);
Georgios Pinitas33843562019-12-10 13:33:18 +0000111 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000112 else
113 {
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000114 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +0000115 {
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000116 const auto in_ptr = reinterpret_cast<const T *>(src_ptr + src_it.offset());
117 const auto out_ptr = reinterpret_cast<T *>(dst_ptr + dst_it.offset());
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000118 int x = window_start_x;
119 for(; x <= (window_end_x - window_step_x); x += window_step_x)
120 {
121 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
122 }
123 // Compute left-over elements
124 for(; x < window_end_x; ++x)
125 {
126 *(out_ptr + x) = *(in_ptr + x);
127 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000128 },
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000129 src_it, dst_it);
Pablo Tello54e98d92019-02-05 16:16:19 +0000130 }
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100131}
Georgios Pinitasae54e022018-07-16 15:41:27 +0100132
Georgios Pinitasae54e022018-07-16 15:41:27 +0100133Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
134{
135 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Michele Di Giorgio33f41fa2021-03-09 14:09:08 +0000136 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use CPU FP16 instructions.
Georgios Pinitas33843562019-12-10 13:33:18 +0000137 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F16, DataType::F32);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100138 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
139
Michalis Spyroua9c44722019-04-05 17:18:36 +0100140 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
141 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) != output->dimension(Window::DimY));
Georgios Pinitasae54e022018-07-16 15:41:27 +0100142 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
Georgios Pinitasae54e022018-07-16 15:41:27 +0100143 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
144
Georgios Pinitasae54e022018-07-16 15:41:27 +0100145 return Status{};
146}
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100147} // namespace
148
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000149CpuConcatenateDepthKernel::CpuConcatenateDepthKernel()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100150 : _func(nullptr), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151{
152}
153
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000154void CpuConcatenateDepthKernel::configure(const ITensorInfo *src, unsigned int depth_offset, ITensorInfo *dst)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155{
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000156 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
157 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, depth_offset, dst));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100159 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 _depth_offset = depth_offset;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100161
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000162 switch(src->data_type())
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100163 {
Georgios Pinitasae54e022018-07-16 15:41:27 +0100164 case DataType::QASYMM8:
165 _func = &depth_concat<uint8_t>;
166 break;
Georgios Pinitas33843562019-12-10 13:33:18 +0000167 case DataType::QASYMM8_SIGNED:
168 _func = &depth_concat<int8_t>;
169 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100170 case DataType::F16:
171 _func = &depth_concat<uint16_t>;
172 break;
173 case DataType::F32:
174 _func = &depth_concat<uint32_t>;
175 break;
176 default:
177 ARM_COMPUTE_ERROR("Unsupported data type.");
178 }
179
Georgios Pinitasae54e022018-07-16 15:41:27 +0100180 // Configure kernel window
SiCongLib88272e2021-02-24 15:40:57 +0000181 Window win = calculate_max_window(*dst, Steps());
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000182 ICpuKernel::configure(win);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100183}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000185Status CpuConcatenateDepthKernel::validate(const arm_compute::ITensorInfo *src,
186 unsigned int depth_offset,
187 const arm_compute::ITensorInfo *dst)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100188{
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000189 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, depth_offset, dst));
Georgios Pinitasae54e022018-07-16 15:41:27 +0100190 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100191}
192
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000193void CpuConcatenateDepthKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100195 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000197 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100198 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100199
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100200 (*_func)(tensors.get_const_tensor(TensorType::ACL_SRC),
201 tensors.get_tensor(TensorType::ACL_DST),
202 _depth_offset,
203 window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100204}
Georgios Pinitas61ba0692021-01-10 04:07:39 +0000205
206const char *CpuConcatenateDepthKernel::name() const
207{
208 return "CpuConcatenateDepthKernel";
209}
210} // namespace kernels
211} // namespace cpu
Michalis Spyrouc28d4282020-03-04 15:30:41 +0000212} // namespace arm_compute