blob: e164a387081e969edbd4f4efe1ae9bb38f203b2e [file] [log] [blame]
Georgios Pinitasae54e022018-07-16 15:41:27 +01001/*
Pablo Tello54e98d92019-02-05 16:16:19 +00002 * Copyright (c) 2018-2019 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{
43std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int width_offset, ITensorInfo *output)
44{
45 const unsigned int num_elems_processed_per_iteration = 16 / output->element_size();
46
47 // The window needs to be based on input as we copy all the widths of input
48 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration));
49 AccessWindowHorizontal input_access(input, 0, num_elems_processed_per_iteration);
50 AccessWindowHorizontal output_access(output, width_offset, num_elems_processed_per_iteration);
51 bool window_changed = update_window_and_padding(win, input_access, output_access);
52
53 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
54 return std::make_pair(err, win);
55}
56
57Status validate_arguments(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
58{
59 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000060 // 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 +000061 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Georgios Pinitasae54e022018-07-16 15:41:27 +010062 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
63 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) + width_offset > output->dimension(0));
64
65 for(size_t i = 1; i < Coordinates::num_max_dimensions; ++i)
66 {
67 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(i) != output->dimension(i));
68 }
Georgios Pinitasae54e022018-07-16 15:41:27 +010069
70 return Status{};
71}
72} // namespace
73
74NEWidthConcatenateLayerKernel::NEWidthConcatenateLayerKernel()
75 : _input(nullptr), _output(nullptr), _width_offset(0)
76{
77}
78
79void NEWidthConcatenateLayerKernel::configure(const ITensor *input, unsigned int width_offset, ITensor *output)
80{
81 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
82 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), width_offset, output->info()));
83
84 _input = input;
85 _output = output;
86 _width_offset = width_offset;
87
88 // Configure kernel window
89 auto win_config = validate_and_configure_window(input->info(), width_offset, output->info());
90 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
91
92 INEKernel::configure(std::get<1>(win_config));
Isabella Gottardif59b16f2019-07-25 12:03:39 +010093
94 // Set output valid region
95 output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
Georgios Pinitasae54e022018-07-16 15:41:27 +010096}
97
98Status NEWidthConcatenateLayerKernel::validate(const ITensorInfo *input, unsigned int width_offset, const ITensorInfo *output)
99{
100 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, width_offset, output));
101 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), width_offset, output->clone().get()).first);
102 return Status{};
103}
104
105void NEWidthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
106{
107 ARM_COMPUTE_UNUSED(info);
108 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
109 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
110
111 // Offset output pointer to the correct position
112 uint8_t *output_ptr = _output->buffer() + _output->info()->offset_first_element_in_bytes() + _width_offset * _output->info()->strides_in_bytes()[0];
113
114 // Create iterators
Georgios Pinitas4c5469b2019-05-21 13:32:43 +0100115 Iterator input(_input, window);
116 Iterator output(_output, window);
117 const DataType dt = _input->info()->data_type();
118 const UniformQuantizationInfo &input_qinfo = _input->info()->quantization_info().uniform();
119 const UniformQuantizationInfo &output_qinfo = _output->info()->quantization_info().uniform();
Pablo Tello54e98d92019-02-05 16:16:19 +0000120 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100121 {
Pablo Tello54e98d92019-02-05 16:16:19 +0000122 execute_window_loop(window, [&](const Coordinates &)
123 {
124 vst1q_u8(output_ptr + output.offset(), vquantize(vdequantize(vld1q_u8(input.ptr()), input_qinfo), output_qinfo));
125 },
126 input, output);
127 }
Georgios Pinitas33843562019-12-10 13:33:18 +0000128 else if(dt == DataType::QASYMM8_SIGNED && input_qinfo != output_qinfo)
129 {
130 execute_window_loop(window, [&](const Coordinates &)
131 {
132 vst1q_s8(reinterpret_cast<int8_t *>(output_ptr + output.offset()),
133 vquantize_signed(vdequantize(vld1q_s8(reinterpret_cast<int8_t *>(input.ptr())), input_qinfo), output_qinfo));
134 },
135 input, output);
136 }
Pablo Tello54e98d92019-02-05 16:16:19 +0000137 else
138 {
139 execute_window_loop(window, [&](const Coordinates &)
140 {
141 const auto in_ptr = input.ptr();
142 const auto out_ptr = output_ptr + output.offset();
Georgios Pinitasae54e022018-07-16 15:41:27 +0100143
Pablo Tello54e98d92019-02-05 16:16:19 +0000144 wrapper::vstore(out_ptr, wrapper::vloadq(in_ptr));
145 },
146 input, output);
147 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100148}
Georgios Pinitas33843562019-12-10 13:33:18 +0000149} // namespace arm_compute