blob: 38443ca4a87020f1819799a3883309c534cf9379 [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"
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
Georgios Pinitasac4e8732017-07-05 17:02:25 +010044uint16x8_t loadq(const uint16_t *ptr)
45{
46 return vld1q_u16(ptr);
47}
48uint32x4_t loadq(const uint32_t *ptr)
49{
50 return vld1q_u32(ptr);
51}
52// Overloads of 128-bit vector stores
Georgios Pinitasac4e8732017-07-05 17:02:25 +010053void storeq(uint16_t *ptr, uint16x8_t val)
54{
55 return vst1q_u16(ptr, val);
56}
57void storeq(uint32_t *ptr, uint32x4_t val)
58{
59 return vst1q_u32(ptr, val);
60}
61
62template <typename T>
63void depth_concat(const ITensor *in, ITensor *out, std::pair<int, int> start_xy, int depth_offset, const Window &window)
64{
65 const int start_x = start_xy.first;
66 const int start_y = start_xy.second;
67
68 // Offset input
69 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];
70 uint8_t *input_ptr = in->buffer() + input_offset_to_first_elements_in_bytes;
71
72 // Offset output
73 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];
74 uint8_t *output_ptr = out->buffer() + output_offset_to_first_elements_in_bytes;
75
76 Iterator input(in, window);
77 Iterator output(out, window);
78
79 execute_window_loop(window, [&](const Coordinates & id)
80 {
81 const auto in_ptr = reinterpret_cast<const T *>(input_ptr + input.offset());
82 const auto out_ptr = reinterpret_cast<T *>(output_ptr + output.offset());
83
84 storeq(out_ptr, loadq(in_ptr));
85 },
86 input, output);
87}
88} // namespace
89
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000090NEDepthConcatenateLayerKernel::NEDepthConcatenateLayerKernel()
Georgios Pinitasac4e8732017-07-05 17:02:25 +010091 : _func(nullptr), _input(nullptr), _output(nullptr), _top_bottom(0), _left_right(0), _depth_offset(0)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010092{
93}
94
Giorgio Arena04a8f8c2017-11-23 11:45:24 +000095BorderSize NEDepthConcatenateLayerKernel::border_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +010096{
97 return BorderSize(_top_bottom, _left_right);
98}
99
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000100void NEDepthConcatenateLayerKernel::configure(const ITensor *input, unsigned int depth_offset, ITensor *output)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100101{
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +0100102 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100103 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100104 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) + depth_offset > output->info()->dimension(2));
105 ARM_COMPUTE_ERROR_ON(input->info()->dimension(0) > output->info()->dimension(0));
106 ARM_COMPUTE_ERROR_ON(input->info()->dimension(1) > output->info()->dimension(1));
107 ARM_COMPUTE_ERROR_ON_MISMATCHING_SHAPES(3, input, output);
108
109 // The gaps between the two lowest dimensions of input and output need to be divisible by 2
110 // Otherwise it is not clear how the padding should be added onto the input tensor
111 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) - input->info()->dimension(0)) % 2);
112 ARM_COMPUTE_ERROR_ON((output->info()->dimension(1) - input->info()->dimension(1)) % 2);
113
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100114 _func = nullptr;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115 _input = input;
116 _output = output;
117 _depth_offset = depth_offset;
118 _left_right = (output->info()->dimension(0) - input->info()->dimension(0)) / 2;
119 _top_bottom = (output->info()->dimension(1) - input->info()->dimension(1)) / 2;
120
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100121 switch(input->info()->data_type())
122 {
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100123 case DataType::F16:
124 _func = &depth_concat<uint16_t>;
125 break;
126 case DataType::F32:
127 _func = &depth_concat<uint32_t>;
128 break;
129 default:
130 ARM_COMPUTE_ERROR("Unsupported data type.");
131 }
132
133 const unsigned int num_elems_processed_per_iteration = 16 / input->info()->element_size();
134 const unsigned int num_elems_read_per_iteration = 16 / input->info()->element_size();
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100135 const unsigned int num_rows_read_per_iteration = 1;
136
137 // The window needs to be based on input as we copy all the depths of input
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100138 Window win = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
139 win.set(Window::DimZ, Window::Dimension(0, input->info()->tensor_shape().z(), 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100140
141 AccessWindowRectangle input_access(input->info(), -_left_right, -_top_bottom, num_elems_read_per_iteration, num_rows_read_per_iteration);
142 AccessWindowHorizontal output_access(output->info(), 0, num_elems_processed_per_iteration);
143 update_window_and_padding(win, input_access, output_access);
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000144 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->info()->tensor_shape()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100145
146 INEKernel::configure(win);
147}
148
Giorgio Arena04a8f8c2017-11-23 11:45:24 +0000149void NEDepthConcatenateLayerKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100150{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100151 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
153 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100154 ARM_COMPUTE_ERROR_ON(_func == nullptr);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155
Georgios Pinitasac4e8732017-07-05 17:02:25 +0100156 (*_func)(_input, _output, std::make_pair(_left_right, _top_bottom), _depth_offset, window);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100157}