blob: d58e4e0aa5acad3be06c8799d562bf2fc7bf574f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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/NEDepthConcatenateKernel.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"
Georgios Pinitasac4e8732017-07-05 17:02:25 +010030#include "arm_compute/core/NEON/NEFixedPoint.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <arm_neon.h>
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{
43// Overloads of 128-bit vector loads
44uint8x16_t loadq(const uint8_t *ptr)
45{
46 return vld1q_u8(ptr);
47}
48uint16x8_t loadq(const uint16_t *ptr)
49{
50 return vld1q_u16(ptr);
51}
52uint32x4_t loadq(const uint32_t *ptr)
53{
54 return vld1q_u32(ptr);
55}
56// Overloads of 128-bit vector stores
57void storeq(uint8_t *ptr, uint8x16_t val)
58{
59 return vst1q_u8(ptr, val);
60}
61void storeq(uint16_t *ptr, uint16x8_t val)
62{
63 return vst1q_u16(ptr, val);
64}
65void storeq(uint32_t *ptr, uint32x4_t val)
66{
67 return vst1q_u32(ptr, val);
68}
69
70template <typename T>
71void depth_concat(const ITensor *in, ITensor *out, std::pair<int, int> start_xy, int depth_offset, const Window &window)
72{
73 const int start_x = start_xy.first;
74 const int start_y = start_xy.second;
75
76 // Offset input
77 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];
78 uint8_t *input_ptr = in->buffer() + input_offset_to_first_elements_in_bytes;
79
80 // Offset output
81 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];
82 uint8_t *output_ptr = out->buffer() + output_offset_to_first_elements_in_bytes;
83
84 Iterator input(in, window);
85 Iterator output(out, window);
86
87 execute_window_loop(window, [&](const Coordinates & id)
88 {
89 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
90 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
91
92 storeq(out_ptr, loadq(in_ptr));
93 },
94 input, output);
95}
96} // namespace
97
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098NEDepthConcatenateKernel::NEDepthConcatenateKernel()
Georgios Pinitasac4e8732017-07-05 17:02:25 +010099 : _func(nullptr), _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100{
101}
102
103BorderSize NEDepthConcatenateKernel::border_size() const
104{
105 return BorderSize(_top_bottom, _left_right);
106}
107
108void NEDepthConcatenateKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output)
109{
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100110 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QS8, DataType::QS16, DataType::F16, DataType::F32);
111 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
112 ARM_COMPUTE_ERROR_ON_MISMATCHING_FIXED_POINT_POSITION(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100113 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2));
114 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0));
115 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1));
116 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
117
118 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
119 // Otherwise it is not clear how the padding should be added onto the input tensor
120 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2);
121 ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2);
122
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100123 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124 _input = input;
125 _output = output;
126 _depth_offset = depth_offset;
127 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
128 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
129
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100130 switch(input->info()->data_type())
131 {
132 case DataType::QS8:
133 _func = &depth_concat<uint8_t>;
134 break;
135 case DataType::QS16:
136 case DataType::F16:
137 _func = &depth_concat<uint16_t>;
138 break;
139 case DataType::F32:
140 _func = &depth_concat<uint32_t>;
141 break;
142 default:
143 ARM_COMPUTE_ERROR("Unsupported data type.");
144 }
145
146 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
147 const unsigned int num_elems_read_per_iteration = 16 / input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100148 const unsigned int num_rows_read_per_iteration = 1;
149
150 // The window needs to be based on input as we copy all the depths of input
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100151 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
152 win.set(Window::DimZ, Window::Dimension(0, input->info()->tensor_shape().z(), 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100153
154 AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration);
155 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
156 update_window_and_padding(win, input_access, output_access);
157 output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->info()->tensor_shape()));
158
159 INEKernel::configure(win);
160}
161
162void NEDepthConcatenateKernel::run(const Window &window)
163{
164 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
165 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100166 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100167
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100168 (*_func)(_input, _output, std::make_pair(_left_right, _top_bottom), _depth_offset, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100169}