blob: 8c875cdb2d4e00e82e7df4e1f44d425069f205b7 [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);
Anthony Barbiereaefd002018-07-20 17:49:35 +010098 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Georgios Pinitasae54e022018-07-16 15:41:27 +010099 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
100 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
101
102 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
103 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) > output->dimension(0));
104 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) > output->dimension(1));
105 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
106
107 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
108 // Otherwise it is not clear how the padding should be added onto the input tensor
109 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) - input->dimension(0)) % 2);
110 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(1) - input->dimension(1)) % 2);
111
112 return Status{};
113}
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100114} // namespace
115
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000116NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel()
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100117 : _func(nullptr), _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100118{
119}
120
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000121BorderSize NEDepthConcatenateLayerKernel::border_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122{
123 return BorderSize(_top_bottom, _left_right);
124}
125
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000126void NEDepthConcatenateLayerKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100127{
Georgios Pinitasae54e022018-07-16 15:41:27 +0100128 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
129 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), depth_offset, output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100131 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132 _input = input;
133 _output = output;
134 _depth_offset = depth_offset;
135 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
136 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
137
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100138 switch(input->info()->data_type())
139 {
Georgios Pinitasae54e022018-07-16 15:41:27 +0100140 case DataType::QASYMM8:
141 _func = &depth_concat<uint8_t>;
142 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100143 case DataType::F16:
144 _func = &depth_concat<uint16_t>;
145 break;
146 case DataType::F32:
147 _func = &depth_concat<uint32_t>;
148 break;
149 default:
150 ARM_COMPUTE_ERROR("Unsupported data type.");
151 }
152
Georgios Pinitasae54e022018-07-16 15:41:27 +0100153 // Configure kernel window
154 auto win_config = validate_and_configure_window(input->info(), depth_offset, output->info());
155 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156
Georgios Pinitasae54e022018-07-16 15:41:27 +0100157 INEKernel::configure(std::get<1>(win_config));
158}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159
Georgios Pinitasae54e022018-07-16 15:41:27 +0100160Status NEDepthConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
161 unsigned int depth_offset,
162 const arm_compute::ITensorInfo *output)
163{
164 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, depth_offset, output));
165 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), depth_offset, output->clone().get()).first);
166 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167}
168
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000169void NEDepthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100170{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100171 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100172 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
173 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100174 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100176 (*_func)(_input, _output, std::make_pair(_left_right, _top_bottom), _depth_offset, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100177}