blob: 782f8f1ff7be22780a3c0de4c45cb9015f5a3881 [file] [log] [blame]
Georgios Pinitasae54e022018-07-16 15:41:27 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitasae54e022018-07-16 15:41:27 +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 */
24#include "arm_compute/runtime/NEON/functions/NEConcatenateLayer.h"
25
Michalis Spyrouebcebf12020-10-21 00:04:14 +010026#include "src/core/NEON/kernels/NEBatchConcatenateLayerKernel.h"
27#include "src/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
28#include "src/core/NEON/kernels/NEHeightConcatenateLayerKernel.h"
29#include "src/core/NEON/kernels/NEWidthConcatenateLayerKernel.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010030
Pablo Tello3dd5b682019-03-04 14:14:02 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
32#include "arm_compute/runtime/NEON/NEScheduler.h"
33
Georgios Pinitasae54e022018-07-16 15:41:27 +010034#include "arm_compute/core/Error.h"
35#include "arm_compute/core/ITensor.h"
36#include "arm_compute/core/TensorInfo.h"
37#include "arm_compute/core/Types.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010038#include "src/core/helpers/AutoConfiguration.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010039
40namespace arm_compute
41{
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010042namespace experimental
43{
Georgios Pinitas09cad722020-07-22 12:11:20 +010044NEConcatenation::NEConcatenation()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010045 : _concat_kernels(), _num_inputs(0), _axis(0)
Georgios Pinitasae54e022018-07-16 15:41:27 +010046{
47}
48
Georgios Pinitas09cad722020-07-22 12:11:20 +010049void NEConcatenation::configure(const std::vector<const ITensorInfo *> &inputs_vector, ITensorInfo *output, size_t axis)
Pablo Tello3dd5b682019-03-04 14:14:02 +000050{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010051 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010052
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010053 _axis = axis;
Pablo Tello3dd5b682019-03-04 14:14:02 +000054 _num_inputs = inputs_vector.size();
55
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010056 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +000057
58 // Output auto inizialitation if not yet initialized
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010059 auto_init_if_empty(*output, output_shape, 1, inputs_vector[0]->data_type());
60 ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector, output, axis));
Pablo Tello3dd5b682019-03-04 14:14:02 +000061
62 unsigned int offset = 0;
63
Pablo Tello3dd5b682019-03-04 14:14:02 +000064 for(unsigned int i = 0; i < _num_inputs; ++i)
65 {
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010066 switch(axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +010067 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010068 case Window::DimX:
69 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000070 auto kernel = std::make_unique<NEWidthConcatenateLayerKernel>();
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010071 kernel->configure(inputs_vector.at(i), offset, output);
72 _concat_kernels.emplace_back(std::move(kernel));
73 break;
74 }
75 case Window::DimY:
76 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000077 auto kernel = std::make_unique<NEHeightConcatenateLayerKernel>();
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010078 kernel->configure(inputs_vector.at(i), offset, output);
79 _concat_kernels.emplace_back(std::move(kernel));
80 break;
81 }
82 case Window::DimZ:
83 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000084 auto kernel = std::make_unique<NEDepthConcatenateLayerKernel>();
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010085 kernel->configure(inputs_vector.at(i), offset, output);
86 _concat_kernels.emplace_back(std::move(kernel));
87 break;
88 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010089 case 3:
90 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000091 auto kernel = std::make_unique<NEBatchConcatenateLayerKernel>();
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010092 kernel->configure(inputs_vector.at(i), offset, output);
93 _concat_kernels.emplace_back(std::move(kernel));
94 break;
95 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010096 default:
97 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitasae54e022018-07-16 15:41:27 +010098 }
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010099 offset += inputs_vector.at(i)->dimension(axis);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100100 }
101}
102
Georgios Pinitas09cad722020-07-22 12:11:20 +0100103Status NEConcatenation::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100104{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100105 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
106 ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100107
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100108 unsigned int offset = 0;
109 for(const auto &input : inputs_vector)
110 {
111 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100112 switch(axis)
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100113 {
114 case Window::DimX:
115 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100116 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100117 break;
118 }
119 case Window::DimY:
120 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100121 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100122 break;
123 }
124 case Window::DimZ:
125 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100126 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100127 break;
128 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100129 case 3:
130 {
131 ARM_COMPUTE_RETURN_ON_ERROR(NEBatchConcatenateLayerKernel::validate(input, offset, output));
132 break;
133 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100134 default:
135 ARM_COMPUTE_ERROR("Axis not supported");
136 }
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100137 offset += input->dimension(axis);
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100138 }
139
Michalis Spyroua9c44722019-04-05 17:18:36 +0100140 if(output->total_size() != 0)
141 {
142 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
143 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
144 }
145
Georgios Pinitasae54e022018-07-16 15:41:27 +0100146 return Status{};
147}
148
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100149void NEConcatenation::run(ITensorPack &tensors)
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100150{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100151 if(tensors.empty())
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100152 {
153 ARM_COMPUTE_ERROR("No inputs provided");
154 }
155
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100156 if(static_cast<int>(tensors.size() - 1) != static_cast<int>(_num_inputs))
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100157 {
158 ARM_COMPUTE_ERROR("Configured with different number of inputs");
159 }
160
161 int i = 0;
162 for(auto &k : _concat_kernels)
163 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100164 ITensorPack pack;
165 pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i));
166 pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST));
167 NEScheduler::get().schedule_op(k.get(), Window::DimY, pack);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100168 ++i;
169 }
170}
171} // namespace experimental
172
173struct NEConcatenateLayer::Impl
174{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100175 std::vector<const ITensor *> srcs{};
176 ITensor *dst{ nullptr };
177 unsigned int num_inputs{ 0 };
178 unsigned int axis{ 0 };
179 std::unique_ptr<experimental::NEConcatenation> op{ nullptr };
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100180};
181
182NEConcatenateLayer::NEConcatenateLayer()
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000183 : _impl(std::make_unique<Impl>())
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100184{
185}
186
187NEConcatenateLayer::NEConcatenateLayer(NEConcatenateLayer &&) = default;
188
189NEConcatenateLayer &NEConcatenateLayer::operator=(NEConcatenateLayer &&) = default;
190
191NEConcatenateLayer::~NEConcatenateLayer() = default;
192
193void NEConcatenateLayer::configure(std::vector<const ITensor *> inputs_vector, ITensor *output, size_t axis)
194{
195 ARM_COMPUTE_ERROR_ON(output == nullptr);
196
197 _impl->srcs = inputs_vector;
198 _impl->dst = output;
199 _impl->axis = axis;
200 _impl->num_inputs = inputs_vector.size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000201 _impl->op = std::make_unique<experimental::NEConcatenation>();
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100202
203 std::vector<const ITensorInfo *> inputs_vector_info;
204 for(unsigned int i = 0; i < inputs_vector.size(); ++i)
205 {
206 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
207 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
208 }
209 _impl->op->configure(inputs_vector_info, _impl->dst->info(), axis);
210}
211
212Status NEConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
213{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100214 return experimental::NEConcatenation::validate(inputs_vector, output, axis);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100215}
216
Georgios Pinitasae54e022018-07-16 15:41:27 +0100217void NEConcatenateLayer::run()
218{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100219 ITensorPack pack;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100220 for(unsigned i = 0; i < _impl->num_inputs; ++i)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000221 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100222 pack.add_tensor(TensorType::ACL_SRC_VEC + i, _impl->srcs.at(i));
Pablo Tello3dd5b682019-03-04 14:14:02 +0000223 }
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100224 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100225
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100226 _impl->op->run(pack);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100227}
228} // namespace arm_compute