blob: 2f130d9aae6a19dc0bf5997c3e8996070adba712 [file] [log] [blame]
Pablo Tello3dd5b682019-03-04 14:14:02 +00001/*
Michalis Spyrou61b17082020-03-04 17:34:20 +00002 * Copyright (c) 2019-2020 ARM Limited.
Pablo Tello3dd5b682019-03-04 14:14:02 +00003 *
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 "arm_compute/core/NEON/kernels/NEHeightConcatenateLayerKernel.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/NEON/NEAsymm.h"
31#include "arm_compute/core/NEON/wrapper/wrapper.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
34#include "arm_compute/core/Validate.h"
35#include "arm_compute/core/Window.h"
36
37#include <cstdint>
38
Georgios Pinitas33843562019-12-10 13:33:18 +000039namespace arm_compute
40{
Pablo Tello3dd5b682019-03-04 14:14:02 +000041namespace
42{
Pablo Tello3dd5b682019-03-04 14:14:02 +000043Status validate_arguments(const ITensorInfo *input, unsigned int height_offset, const ITensorInfo *output)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
46 // 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 +000047 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Pablo Tello3dd5b682019-03-04 14:14:02 +000048 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimX) != output->dimension(Window::DimX));
50 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(Window::DimY) + height_offset > output->dimension(Window::DimY));
51 for(size_t i = 2; i < Coordinates::num_max_dimensions; ++i)
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
54 }
55
56 return Status{};
57}
58} // namespace
59
60NEHeightConcatenateLayerKernel::NEHeightConcatenateLayerKernel()
61 : _input(nullptr), _output(nullptr), _height_offset(0)
62{
63}
64
65void NEHeightConcatenateLayerKernel::configure(const ITensor *input, unsigned int height_offset, ITensor *output)
66{
67 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
68 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), height_offset, output->info()));
69
70 _input = input;
71 _output = output;
72 _height_offset = height_offset;
73
74 // Configure kernel window
Michalis Spyrou61b17082020-03-04 17:34:20 +000075 Window win = calculate_max_window(*output->info(), Steps());
76 Coordinates coord;
77 coord.set_num_dimensions(output->info()->num_dimensions());
78 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
79 INEKernel::configure(win);
Pablo Tello3dd5b682019-03-04 14:14:02 +000080}
81
82Status NEHeightConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int height_offset, const ITensorInfo *output)
83{
84 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, height_offset, output));
Pablo Tello3dd5b682019-03-04 14:14:02 +000085 return Status{};
86}
87
88void NEHeightConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
89{
90 ARM_COMPUTE_UNUSED(info);
91 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
92 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
93
94 // Offset output pointer to the correct position
95 uint8_t *output_ptr = _output->buffer() + _output->info()->offset_first_element_in_bytes() + _height_offset * _output->info()->strides_in_bytes()[Window::DimY];
96
Michalis Spyrou61b17082020-03-04 17:34:20 +000097 const auto window_start_x = static_cast<int>(window.x().start());
98 const auto window_end_x = static_cast<int>(window.x().end()) * static_cast<int>(_output->info()->element_size());
99 const int window_step_x = 16;
100
101 Window win{ window };
102 win.set(Window::DimX, Window::Dimension(0, 1, 1));
103 win.set(Window::DimY, Window::Dimension(0, _input->info()->tensor_shape().y(), 1));
104
Pablo Tello3dd5b682019-03-04 14:14:02 +0000105 // Create iterators
Michalis Spyrou61b17082020-03-04 17:34:20 +0000106 Iterator input(_input, win);
107 Iterator output(_output, win);
108
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100109 const DataType dt = _input->info()->data_type();
110 const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform();
111 const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform();
Pablo Tello3dd5b682019-03-04 14:14:02 +0000112 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
113 {
Michalis Spyrou61b17082020-03-04 17:34:20 +0000114 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000115 {
Michalis Spyrou61b17082020-03-04 17:34:20 +0000116 int x = window_start_x;
117 for(; x <= (window_end_x - window_step_x); x += window_step_x)
118 {
119 vst1q_u8(output_ptr + output.offset() + x, vquantize(vdequantize(vld1q_u8(input.ptr() + x), input_qinfo), output_qinfo));
120 }
121
122 // Compute left-over elements
123 for(; x < window_end_x; ++x)
124 {
125 *(output_ptr + output.offset() + x) = quantize_qasymm8(dequantize_qasymm8(*(input.ptr() + x), input_qinfo), output_qinfo);
126 }
127
Pablo Tello3dd5b682019-03-04 14:14:02 +0000128 },
129 input, output);
130 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000131 else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
132 {
Michalis Spyrou61b17082020-03-04 17:34:20 +0000133 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +0000134 {
Michalis Spyrou61b17082020-03-04 17:34:20 +0000135 int x = window_start_x;
136 for(; x <= (window_end_x - window_step_x); x += window_step_x)
137 {
138 vst1q_s8(reinterpret_cast<int8_t *>(output_ptr + output.offset() + x),
139 vquantize_signed(vdequantize(vld1q_s8(reinterpret_cast<int8_t *>(input.ptr()) + x), input_qinfo), output_qinfo));
140 }
141
142 // Compute left-over elements
143 for(; x < window_end_x; ++x)
144 {
145 *(output_ptr + output.offset() + x) = quantize_qasymm8_signed(dequantize_qasymm8_signed(*(input.ptr() + x), input_qinfo), output_qinfo);
146 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000147 },
148 input, output);
149 }
Pablo Tello3dd5b682019-03-04 14:14:02 +0000150 else
151 {
Michalis Spyrou61b17082020-03-04 17:34:20 +0000152 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000153 {
154 const auto in_ptr = input.ptr();
155 const auto out_ptr = output_ptr + output.offset();
156
Michalis Spyrou61b17082020-03-04 17:34:20 +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 }
162
163 // Compute left-over elements
164 for(; x < window_end_x; ++x)
165 {
166 *(out_ptr + x) = *(in_ptr + x);
167 }
Pablo Tello3dd5b682019-03-04 14:14:02 +0000168 },
169 input, output);
170 }
171}
Georgios Pinitas33843562019-12-10 13:33:18 +0000172} // namespace arm_compute