blob: 8352c945865a9b0765ca1bdd1166ffedab841e49 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Pablo Tello54e98d92019-02-05 16:16:19 +00002 * Copyright (c) 2017-2019 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"
Pablo Tello54e98d92019-02-05 16:16:19 +000030#include "arm_compute/core/NEON/NEAsymm.h"
Georgios Pinitasac4e8732017-07-05 17:02:25 +010031#include "arm_compute/core/NEON/NEFixedPoint.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010032#include "arm_compute/core/NEON/wrapper/wrapper.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#include "arm_compute/core/Window.h"
37
Georgios Pinitasac4e8732017-07-05 17:02:25 +010038#include <cstdint>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010039
40using namespace arm_compute;
41
Georgios Pinitasac4e8732017-07-05 17:02:25 +010042namespace
43{
Georgios Pinitasac4e8732017-07-05 17:02:25 +010044template <typename T>
45void depth_concat(const ITensor *in, ITensor *out, std::pair<int, int> start_xy, int depth_offset, const Window &window)
46{
47 const int start_x = start_xy.first;
48 const int start_y = start_xy.second;
49
50 // Offset input
51 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];
52 uint8_t *input_ptr = in->buffer() + input_offset_to_first_elements_in_bytes;
53
54 // Offset output
55 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];
56 uint8_t *output_ptr = out->buffer() + output_offset_to_first_elements_in_bytes;
57
58 Iterator input(in, window);
59 Iterator output(out, window);
60
Pablo Tello54e98d92019-02-05 16:16:19 +000061 const DataType dt = in->info()->data_type();
62 const QuantizationInfo &input_qinfo = in->info()->quantization_info();
63 const QuantizationInfo &output_qinfo = out->info()->quantization_info();
64 if(dt == DataType::QASYMM8 && input_qinfo != output_qinfo)
Georgios Pinitasac4e8732017-07-05 17:02:25 +010065 {
Pablo Tello54e98d92019-02-05 16:16:19 +000066 execute_window_loop(window, [&](const Coordinates &)
67 {
68 const auto in_ptr = reinterpret_cast<const uint8_t *>(input_ptr + input.offset());
69 const auto out_ptr = reinterpret_cast<uint8_t *>(output_ptr + output.offset());
70 vst1q_u8(out_ptr, vquantize(vdequantize(vld1q_u8(in_ptr), input_qinfo), output_qinfo));
71 },
72 input, output);
73 }
74 else
75 {
76 execute_window_loop(window, [&](const Coordinates &)
77 {
78 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
79 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
Georgios Pinitasac4e8732017-07-05 17:02:25 +010080
Pablo Tello54e98d92019-02-05 16:16:19 +000081 wrapper::vstore(out_ptr, wrapper::vloadq(in_ptr));
82 },
83 input, output);
84 }
Georgios Pinitasac4e8732017-07-05 17:02:25 +010085}
Georgios Pinitasae54e022018-07-16 15:41:27 +010086
87std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, unsigned int depth_offset, ITensorInfo *output)
88{
89 ARM_COMPUTE_UNUSED(depth_offset);
90
91 // Configure kernel window
92 const int left_right = (output->dimension(0) - input->dimension(0)) / 2;
93 const int top_bottom = (output->dimension(1) - input->dimension(1)) / 2;
94
95 const unsigned int num_elems_processed_per_iteration = 16 / input->element_size();
96 const unsigned int num_elems_read_per_iteration = 16 / input->element_size();
97 const unsigned int num_rows_read_per_iteration = 1;
98
99 // The window needs to be based on input as we copy all the depths of input
100 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
101 win.set(Window::DimZ, Window::Dimension(0, input->tensor_shape().z(), 1));
102
103 AccessWindowRectangle input_access(input, -left_right, -top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration);
104 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
105 bool window_changed = update_window_and_padding(win, input_access, output_access);
106 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
107
108 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
109 return std::make_pair(err, win);
110}
111
112Status validate_arguments(const ITensorInfo *input, unsigned int depth_offset, const ITensorInfo *output)
113{
114 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
Anthony Barbiereaefd002018-07-20 17:49:35 +0100115 //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 +0100116 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F16, DataType::F32);
117 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
118
119 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) + depth_offset > output->dimension(2));
120 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(0) > output->dimension(0));
121 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(1) > output->dimension(1));
122 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
123
124 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
125 // Otherwise it is not clear how the padding should be added onto the input tensor
126 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) - input->dimension(0)) % 2);
127 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(1) - input->dimension(1)) % 2);
128
129 return Status{};
130}
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100131} // namespace
132
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000133NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel()
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100134 : _func(nullptr), _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135{
136}
137
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000138BorderSize NEDepthConcatenateLayerKernel::border_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100139{
140 return BorderSize(_top_bottom, _left_right);
141}
142
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000143void NEDepthConcatenateLayerKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100144{
Georgios Pinitasae54e022018-07-16 15:41:27 +0100145 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
146 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), depth_offset, output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100147
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100148 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100149 _input = input;
150 _output = output;
151 _depth_offset = depth_offset;
152 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
153 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
154
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100155 switch(input->info()->data_type())
156 {
Georgios Pinitasae54e022018-07-16 15:41:27 +0100157 case DataType::QASYMM8:
158 _func = &depth_concat<uint8_t>;
159 break;
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100160 case DataType::F16:
161 _func = &depth_concat<uint16_t>;
162 break;
163 case DataType::F32:
164 _func = &depth_concat<uint32_t>;
165 break;
166 default:
167 ARM_COMPUTE_ERROR("Unsupported data type.");
168 }
169
Georgios Pinitasae54e022018-07-16 15:41:27 +0100170 // Configure kernel window
171 auto win_config = validate_and_configure_window(input->info(), depth_offset, output->info());
172 ARM_COMPUTE_ERROR_THROW_ON(std::get<0>(win_config));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100173
Georgios Pinitasae54e022018-07-16 15:41:27 +0100174 INEKernel::configure(std::get<1>(win_config));
175}
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176
Georgios Pinitasae54e022018-07-16 15:41:27 +0100177Status NEDepthConcatenateLayerKernel::validate(const arm_compute::ITensorInfo *input,
178 unsigned int depth_offset,
179 const arm_compute::ITensorInfo *output)
180{
181 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, depth_offset, output));
182 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), depth_offset, output->clone().get()).first);
183 return Status{};
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100184}
185
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000186void NEDepthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100188 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100189 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
190 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100191 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100192
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100193 (*_func)(_input, _output, std::make_pair(_left_right, _top_bottom), _depth_offset, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100194}