blob: 1b937b5be8bb085daf973aeb7f493ad3828c1130 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +01002 * Copyright (c) 2017-2018 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 */
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000024#include "arm_compute/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 Pinitasac4e8732017-07-05 17:02:25 +010030#include "arm_compute/core/NEON/NEFixedPoint.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010031#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#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
Georgios Pinitasac4e8732017-07-05 17:02:25 +010037#include <cstdint>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038
39using namespace arm_compute;
40
Georgios Pinitasac4e8732017-07-05 17:02:25 +010041namespace
42{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010043template <typename T>
44void depth_concat(const ITensor *in, ITensor *out, std::pair<int, int> start_xy, int depth_offset, const Window &window)
45{
46 const int start_x = start_xy.first;
47 const int start_y = start_xy.second;
48
49 // Offset input
50 const int input_offset_to_first_elements_in_bytes = in->info()->offset_first_element_in_bytes() - start_x * in->info()->strides_in_bytes()[0] - start_y * in->info()->strides_in_bytes()[1];
51 uint8_t *input_ptr = in->buffer() + input_offset_to_first_elements_in_bytes;
52
53 // Offset output
54 const unsigned int output_offset_to_first_elements_in_bytes = out->info()->offset_first_element_in_bytes() + depth_offset * out->info()->strides_in_bytes()[2];
55 uint8_t *output_ptr = out->buffer() + output_offset_to_first_elements_in_bytes;
56
57 Iterator input(in, window);
58 Iterator output(out, window);
59
60 execute_window_loop(window, [&](const Coordinates & id)
61 {
62 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
63 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
64
Georgios Pinitasae54e022018-07-16 15:41:27 +010065 wrapper::vstore(out_ptr, wrapper::vloadq(in_ptr));
Georgios Pinitasac4e8732017-07-05 17:02:25 +010066 },
67 input, output);
68}
Georgios Pinitasae54e022018-07-16 15:41:27 +010069
70std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int depth_offset, ITensorInfo *output)
71{
72 ARM_COMPUTE_UNUSED(depth_offset);
73
74 // Configure kernel window
75 const int left_right = (output->dimension(0) - input->dimension(0)) / 2;
76 const int top_bottom = (output->dimension(1) - input->dimension(1)) / 2;
77
78 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
79 const unsigned int num_elems_read_per_iteration = 16 / input->element_size();
80 const unsigned int num_rows_read_per_iteration = 1;
81
82 // The window needs to be based on input as we copy all the depths of input
83 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
84 win.set(Window::DimZ, Window::Dimension(0, input->tensor_shape().z(), 1));
85
86 AccessWindowRectangle input_access(input, -left_right, -top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration);
87 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
88 bool window_changed = update_window_and_padding(win, input_access, output_access);
89 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
90
91 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
92 return std::make_pair(err, win);
93}
94
95Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
96{
97 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
98 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
99 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
100
101 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
102 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) > output->dimension(0));
103 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) > output->dimension(1));
104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
105
106 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
107 // Otherwise it is not clear how the padding should be added onto the input tensor
108 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) - input->dimension(0)) % 2);
109 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(1) - input->dimension(1)) % 2);
110
111 return Status{};
112}
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100113} // namespace
114
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000115NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel()
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100116 : _func(nullptr), _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100117{
118}
119
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000120BorderSize NEDepthConcatenateLayerKernel::border_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121{
122 return BorderSize(_top_bottom, _left_right);
123}
124
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000125void NEDepthConcatenateLayerKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126{
Georgios Pinitasae54e022018-07-16 15:41:27 +0100127 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
128 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), depth_offset, output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100129
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100130 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100131 _input = input;
132 _output = output;
133 _depth_offset = depth_offset;
134 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
135 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
136
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100137 switch(input->info()->data_type())
138 {
Georgios Pinitasae54e022018-07-16 15:41:27 +0100139 case DataType::QASYMM8:
140 _func = &depth_concat<uint8_t>;
141 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100142 case DataType::F16:
143 _func = &depth_concat<uint16_t>;
144 break;
145 case DataType::F32:
146 _func = &depth_concat<uint32_t>;
147 break;
148 default:
149 ARM_COMPUTE_ERROR("Unsupported data type.");
150 }
151
Georgios Pinitasae54e022018-07-16 15:41:27 +0100152 // Configure kernel window
153 auto win_config = validate_and_configure_window(input->info(), depth_offset, output->info());
154 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
Georgios Pinitasae54e022018-07-16 15:41:27 +0100156 INEKernel::configure(std::get<1>(win_config));
157}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Georgios Pinitasae54e022018-07-16 15:41:27 +0100159Status NEDepthConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
160 unsigned int depth_offset,
161 const arm_compute::ITensorInfo *output)
162{
163 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, depth_offset, output));
164 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), depth_offset, output->clone().get()).first);
165 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100166}
167
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000168void NEDepthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100170 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100171 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
172 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100173 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100174
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100175 (*_func)(_input, _output, std::make_pair(_left_right, _top_bottom), _depth_offset, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176}