blob: 1b32e3614e1ecbf6c35db7e1cc978a7f30c4919a [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 */
24#include "arm_compute/core/NEON/kernels/NEWidthConcatenateLayerKernel.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"
Pablo Tello54e98d92019-02-05 16:16:19 +000030#include "arm_compute/core/NEON/NEAsymm.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010031#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{
Georgios Pinitasae54e022018-07-16 15:41:27 +010041namespace
42{
Georgios Pinitasae54e022018-07-16 15:41:27 +010043Status validate_arguments(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
44{
45 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000046 // 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);
Georgios Pinitasae54e022018-07-16 15:41:27 +010048 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
49 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) + width_offset > output->dimension(0));
50
51 for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i)
52 {
53 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
54 }
Georgios Pinitasae54e022018-07-16 15:41:27 +010055
56 return Status{};
57}
58} // namespace
59
60NEWidthConcatenateLayerKernel::NEWidthConcatenateLayerKernel()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010061 : _width_offset(0)
Georgios Pinitasae54e022018-07-16 15:41:27 +010062{
63}
64
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010065void NEWidthConcatenateLayerKernel::configure(const ITensorInfo *input, unsigned int width_offset, ITensorInfo *output)
Georgios Pinitasae54e022018-07-16 15:41:27 +010066{
67 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010068 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input, width_offset, output));
Georgios Pinitasae54e022018-07-16 15:41:27 +010069
Georgios Pinitasae54e022018-07-16 15:41:27 +010070 _width_offset = width_offset;
71
72 // Configure kernel window
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010073 Window win = calculate_max_window(*input, Steps());
Michalis Spyrou16a70e02020-03-04 15:57:04 +000074 Coordinates coord;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010075 coord.set_num_dimensions(output->num_dimensions());
76 output->set_valid_region(ValidRegion(coord, output->tensor_shape()));
Georgios Pinitasae54e022018-07-16 15:41:27 +010077
Michalis Spyrou16a70e02020-03-04 15:57:04 +000078 INEKernel::configure(win);
Georgios Pinitasae54e022018-07-16 15:41:27 +010079}
80
81Status NEWidthConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
82{
83 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, width_offset, output));
Georgios Pinitasae54e022018-07-16 15:41:27 +010084 return Status{};
85}
86
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010087void NEWidthConcatenateLayerKernel::run_op(const InputTensorMap &inputs, const OutputTensorMap &outputs,
88 const Window &window, const ThreadInfo &info)
Georgios Pinitasae54e022018-07-16 15:41:27 +010089{
90 ARM_COMPUTE_UNUSED(info);
91 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
92 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
93
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010094 const auto src = inputs.at(TensorType::ACL_SRC);
95 auto dst = outputs.at(TensorType::ACL_DST);
96
Georgios Pinitasae54e022018-07-16 15:41:27 +010097 // Offset output pointer to the correct position
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010098 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 +010099
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000100 const auto window_start_x = static_cast<int>(window.x().start());
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100101 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 +0000102 constexpr int window_step_x = 16;
103
104 Window win{ window };
105 win.set(Window::DimX, Window::Dimension(0, 1, 1));
106
Georgios Pinitasae54e022018-07-16 15:41:27 +0100107 // Create iterators
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100108 Iterator input(src, win);
109 Iterator output(dst, win);
110 const DataType dt = src->info()->data_type();
111 const UniformQuantizationInfo &input_qinfo = src->info()->quantization_info().uniform();
112 const UniformQuantizationInfo &output_qinfo = dst->info()->quantization_info().uniform();
Pablo Tello54e98d92019-02-05 16:16:19 +0000113 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100114 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000115 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +0000116 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000117 int x = window_start_x;
118 for(; x <= (window_end_x - window_step_x); x += window_step_x)
119 {
120 vst1q_u8(output_ptr + output.offset() + x, vquantize(vdequantize(vld1q_u8(input.ptr() + x), input_qinfo), output_qinfo));
121 }
122
123 // Compute left-over elements
124 for(; x < window_end_x; ++x)
125 {
126 *(output_ptr + output.offset() + x) = quantize_qasymm8(dequantize_qasymm8(*(input.ptr() + x), input_qinfo), output_qinfo);
127 }
Pablo Tello54e98d92019-02-05 16:16:19 +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 Spyrou16a70e02020-03-04 15:57:04 +0000133 execute_window_loop(win, [&](const Coordinates &)
Georgios Pinitas33843562019-12-10 13:33:18 +0000134 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +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 Tello54e98d92019-02-05 16:16:19 +0000150 else
151 {
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000152 execute_window_loop(win, [&](const Coordinates &)
Pablo Tello54e98d92019-02-05 16:16:19 +0000153 {
154 const auto in_ptr = input.ptr();
155 const auto out_ptr = output_ptr + output.offset();
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000156 int x = window_start_x;
157 for(; x <= (window_end_x - window_step_x); x += window_step_x)
158 {
159 wrapper::vstore(out_ptr + x, wrapper::vloadq(in_ptr + x));
160 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100161
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000162 // Compute left-over elements
163 for(; x < window_end_x; ++x)
164 {
165 *(out_ptr + x) = *(in_ptr + x);
166 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000167 },
168 input, output);
169 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100170}
Michalis Spyrou16a70e02020-03-04 15:57:04 +0000171} // namespace arm_compute