blob: b5afeed1f6e0b75c5cc1a9365d1cb538ebf05245 [file] [log] [blame]
Georgios Pinitasae54e022018-07-16 15:41:27 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasae54e022018-07-16 15:41:27 +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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEWidthConcatenateLayerKernel.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010025
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"
Georgios Pinitasae54e022018-07-16 15:41:27 +010030#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"
Georgios Pinitasddb93bb2020-10-02 16:38:59 +010034#include "src/core/NEON/NEAsymm.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"
Georgios Pinitasae54e022018-07-16 15:41:27 +010038
39#include <cstdint>
40
Georgios Pinitas33843562019-12-10 13:33:18 +000041namespace arm_compute
42{
Georgios Pinitasae54e022018-07-16 15:41:27 +010043namespace
44{
Georgios Pinitasae54e022018-07-16 15:41:27 +010045Status validate_arguments(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
46{
47 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000048 // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitas33843562019-12-10 13:33:18 +000049 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitasae54e022018-07-16 15:41:27 +010050 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
51 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) + width_offset > output->dimension(0));
52
53 for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
56 }
Georgios Pinitasae54e022018-07-16 15:41:27 +010057
58 return Status{};
59}
60} // namespace
61
62NEWidthConcatenateLayerKernel::NEWidthConcatenateLayerKernel()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010063 : _width_offset(0)
Georgios Pinitasae54e022018-07-16 15:41:27 +010064{
65}
66
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010067void NEWidthConcatenateLayerKernel::configure(const ITensorInfo *input, unsigned int width_offset, ITensorInfo *output)
Georgios Pinitasae54e022018-07-16 15:41:27 +010068{
69 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010070 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, width_offset, output));
Georgios Pinitasae54e022018-07-16 15:41:27 +010071
Georgios Pinitasae54e022018-07-16 15:41:27 +010072 _width_offset = width_offset;
73
74 // Configure kernel window
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010075 Window win = calculate_max_window(*input, Steps());
Michalis Spyrou16a70e02020-03-04 15:57:04 +000076 Coordinates coord;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010077 coord.set_num_dimensions(output->num_dimensions());
78 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Georgios Pinitasae54e022018-07-16 15:41:27 +010079
Michalis Spyrou16a70e02020-03-04 15:57:04 +000080 INEKernel::configure(win);
Georgios Pinitasae54e022018-07-16 15:41:27 +010081}
82
83Status NEWidthConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
84{
85 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, width_offset, output));
Georgios Pinitasae54e022018-07-16 15:41:27 +010086 return Status{};
87}
88
Georgios Pinitas0499dff2020-07-31 22:21:38 +010089void NEWidthConcatenateLayerKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Georgios Pinitasae54e022018-07-16 15:41:27 +010090{
91 ARM_COMPUTE_UNUSED(info);
92 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
93 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
94
Georgios Pinitas0499dff2020-07-31 22:21:38 +010095 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
96 auto dst = tensors.get_tensor(TensorType::ACL_DST);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010097
Georgios Pinitasae54e022018-07-16 15:41:27 +010098 // Offset output pointer to the correct position
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010099 uint8_t *output_ptr = dst->buffer() + dst->info()->offset_first_element_in_bytes() + _width_offset * dst->info()->strides_in_bytes()[0];
Georgios Pinitasae54e022018-07-16 15:41:27 +0100100
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000101 const auto window_start_x = static_cast<int>(window.x().start());
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100102 const auto window_end_x = static_cast<int>(window.x().end()) * static_cast<int>(dst->info()->element_size());
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000103 constexpr int window_step_x = 16;
104
105 Window win{ window };
106 win.set(Window::DimX, Window::Dimension(0, 1, 1));
107
Georgios Pinitasae54e022018-07-16 15:41:27 +0100108 // Create iterators
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100109 Iterator input(src, win);
110 Iterator output(dst, win);
111 const DataType dt = src->info()->data_type();
112 const UniformQuantizationInfo &input_qinfo = src->info()->quantization_info().uniform();
113 const UniformQuantizationInfo &output_qinfo = dst->info()->quantization_info().uniform();
Pablo Tello54e98d92019-02-05 16:16:19 +0000114 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100115 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000116 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +0000117 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000118 int x = window_start_x;
119 for(; x <= (window_end_x - window_step_x); x += window_step_x)
120 {
121 vst1q_u8(output_ptr + output.offset() + x, vquantize(vdequantize(vld1q_u8(input.ptr() + x), input_qinfo), output_qinfo));
122 }
123
124 // Compute left-over elements
125 for(; x < window_end_x; ++x)
126 {
127 *(output_ptr + output.offset() + x) = quantize_qasymm8(dequantize_qasymm8(*(input.ptr() + x), input_qinfo), output_qinfo);
128 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000129 },
130 input, output);
131 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000132 else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
133 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000134 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +0000135 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000136 int x = window_start_x;
137 for(; x <= (window_end_x - window_step_x); x += window_step_x)
138 {
139 vst1q_s8(reinterpret_cast<int8_t *>(output_ptr + output.offset() + x),
140 vquantize_signed(vdequantize(vld1q_s8(reinterpret_cast<int8_t *>(input.ptr() + x)), input_qinfo), output_qinfo));
141 }
142
143 // Compute left-over elements
144 for(; x < window_end_x; ++x)
145 {
146 *(output_ptr + output.offset() + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(input.ptr() + x), input_qinfo), output_qinfo);
147 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000148 },
149 input, output);
150 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000151 else
152 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000153 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +0000154 {
155 const auto in_ptr = input.ptr();
156 const auto out_ptr = output_ptr + output.offset();
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000157 int x = window_start_x;
158 for(; x <= (window_end_x - window_step_x); x += window_step_x)
159 {
160 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
161 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100162
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000163 // Compute left-over elements
164 for(; x < window_end_x; ++x)
165 {
166 *(out_ptr + x) = *(in_ptr + x);
167 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000168 },
169 input, output);
170 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100171}
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000172} // namespace arm_compute