blob: 72bd9e6b19a52f8a42d239ebcfad58fe17aafcad [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
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010026#include "arm_compute/core/NEON/kernels/NEBatchConcatenateLayerKernel.h"
Georgios Pinitas09f24972019-05-17 18:14:40 +010027#include "arm_compute/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
28#include "arm_compute/core/NEON/kernels/NEHeightConcatenateLayerKernel.h"
29#include "arm_compute/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"
Matthew Bentham92046462020-03-07 22:15:55 +000039#include "support/MemorySupport.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010040
41namespace arm_compute
42{
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010043namespace experimental
44{
Georgios Pinitas09cad722020-07-22 12:11:20 +010045NEConcatenation::NEConcatenation()
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010046 : _concat_kernels(), _num_inputs(0), _axis(0)
Georgios Pinitasae54e022018-07-16 15:41:27 +010047{
48}
49
Georgios Pinitas09cad722020-07-22 12:11:20 +010050void NEConcatenation::configure(const std::vector<const ITensorInfo *> &inputs_vector, ITensorInfo *output, size_t axis)
Pablo Tello3dd5b682019-03-04 14:14:02 +000051{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010052 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010053
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010054 _axis = axis;
Pablo Tello3dd5b682019-03-04 14:14:02 +000055 _num_inputs = inputs_vector.size();
56
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010057 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +000058
59 // Output auto inizialitation if not yet initialized
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010060 auto_init_if_empty(*output, output_shape, 1, inputs_vector[0]->data_type());
61 ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector, output, axis));
Pablo Tello3dd5b682019-03-04 14:14:02 +000062
63 unsigned int offset = 0;
64
Pablo Tello3dd5b682019-03-04 14:14:02 +000065 for(unsigned int i = 0; i < _num_inputs; ++i)
66 {
Georgios Pinitas4667ddd2020-07-13 21:21:33 +010067 switch(axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +010068 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010069 case Window::DimX:
70 {
71 auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>();
72 kernel->configure(inputs_vector.at(i), offset, output);
73 _concat_kernels.emplace_back(std::move(kernel));
74 break;
75 }
76 case Window::DimY:
77 {
78 auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>();
79 kernel->configure(inputs_vector.at(i), offset, output);
80 _concat_kernels.emplace_back(std::move(kernel));
81 break;
82 }
83 case Window::DimZ:
84 {
85 auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>();
86 kernel->configure(inputs_vector.at(i), offset, output);
87 _concat_kernels.emplace_back(std::move(kernel));
88 break;
89 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010090 case 3:
91 {
92 auto kernel = support::cpp14::make_unique<NEBatchConcatenateLayerKernel>();
93 kernel->configure(inputs_vector.at(i), offset, output);
94 _concat_kernels.emplace_back(std::move(kernel));
95 break;
96 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010097 default:
98 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitasae54e022018-07-16 15:41:27 +010099 }
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100100 offset += inputs_vector.at(i)->dimension(axis);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100101 }
102}
103
Georgios Pinitas09cad722020-07-22 12:11:20 +0100104Status NEConcatenation::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100105{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100106 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
107 ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100108
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100109 unsigned int offset = 0;
110 for(const auto &input : inputs_vector)
111 {
112 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100113 switch(axis)
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100114 {
115 case Window::DimX:
116 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100117 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100118 break;
119 }
120 case Window::DimY:
121 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100122 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100123 break;
124 }
125 case Window::DimZ:
126 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100127 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100128 break;
129 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100130 case 3:
131 {
132 ARM_COMPUTE_RETURN_ON_ERROR(NEBatchConcatenateLayerKernel::validate(input, offset, output));
133 break;
134 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100135 default:
136 ARM_COMPUTE_ERROR("Axis not supported");
137 }
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100138 offset += input->dimension(axis);
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100139 }
140
Michalis Spyroua9c44722019-04-05 17:18:36 +0100141 if(output->total_size() != 0)
142 {
143 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
144 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
145 }
146
Georgios Pinitasae54e022018-07-16 15:41:27 +0100147 return Status{};
148}
149
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100150void NEConcatenation::run(ITensorPack &tensors)
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100151{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100152 if(tensors.empty())
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100153 {
154 ARM_COMPUTE_ERROR("No inputs provided");
155 }
156
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100157 if(static_cast<int>(tensors.size() - 1) != static_cast<int>(_num_inputs))
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100158 {
159 ARM_COMPUTE_ERROR("Configured with different number of inputs");
160 }
161
162 int i = 0;
163 for(auto &k : _concat_kernels)
164 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100165 ITensorPack pack;
166 pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i));
167 pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST));
168 NEScheduler::get().schedule_op(k.get(), Window::DimY, pack);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100169 ++i;
170 }
171}
172} // namespace experimental
173
174struct NEConcatenateLayer::Impl
175{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100176 std::vector<const ITensor *> srcs{};
177 ITensor *dst{ nullptr };
178 unsigned int num_inputs{ 0 };
179 unsigned int axis{ 0 };
180 std::unique_ptr<experimental::NEConcatenation> op{ nullptr };
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100181};
182
183NEConcatenateLayer::NEConcatenateLayer()
184 : _impl(support::cpp14::make_unique<Impl>())
185{
186}
187
188NEConcatenateLayer::NEConcatenateLayer(NEConcatenateLayer &&) = default;
189
190NEConcatenateLayer &NEConcatenateLayer::operator=(NEConcatenateLayer &&) = default;
191
192NEConcatenateLayer::~NEConcatenateLayer() = default;
193
194void NEConcatenateLayer::configure(std::vector<const ITensor *> inputs_vector, ITensor *output, size_t axis)
195{
196 ARM_COMPUTE_ERROR_ON(output == nullptr);
197
198 _impl->srcs = inputs_vector;
199 _impl->dst = output;
200 _impl->axis = axis;
201 _impl->num_inputs = inputs_vector.size();
Georgios Pinitas09cad722020-07-22 12:11:20 +0100202 _impl->op = arm_compute::support::cpp14::make_unique<experimental::NEConcatenation>();
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100203
204 std::vector<const ITensorInfo *> inputs_vector_info;
205 for(unsigned int i = 0; i < inputs_vector.size(); ++i)
206 {
207 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
208 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
209 }
210 _impl->op->configure(inputs_vector_info, _impl->dst->info(), axis);
211}
212
213Status NEConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
214{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100215 return experimental::NEConcatenation::validate(inputs_vector, output, axis);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100216}
217
Georgios Pinitasae54e022018-07-16 15:41:27 +0100218void NEConcatenateLayer::run()
219{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100220 ITensorPack pack;
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100221 for(unsigned i = 0; i < _impl->num_inputs; ++i)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000222 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100223 pack.add_tensor(TensorType::ACL_SRC_VEC + i, _impl->srcs.at(i));
Pablo Tello3dd5b682019-03-04 14:14:02 +0000224 }
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100225 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
Georgios Pinitas4667ddd2020-07-13 21:21:33 +0100226
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100227 _impl->op->run(pack);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100228}
229} // namespace arm_compute