blob: 54f71f9765ee8cc46d4ebe238786cd4540faba03 [file] [log] [blame]
Georgios Pinitase29acf12018-07-16 14:40:09 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 Arm Limited.
Georgios Pinitase29acf12018-07-16 14:40:09 +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/CL/functions/CLConcatenateLayer.h"
25
Pablo Tello6a14adb2019-03-05 17:33:08 +000026#include "arm_compute/core/utils/misc/ShapeCalculator.h"
27#include "arm_compute/runtime/CL/CLScheduler.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010028#include "src/core/CL/kernels/CLDepthConcatenateLayerKernel.h"
29#include "src/core/CL/kernels/CLHeightConcatenateLayerKernel.h"
30#include "src/core/CL/kernels/CLWidthConcatenate2TensorsKernel.h"
31#include "src/core/CL/kernels/CLWidthConcatenate4TensorsKernel.h"
32#include "src/core/CL/kernels/CLWidthConcatenateLayerKernel.h"
Georgios Pinitase29acf12018-07-16 14:40:09 +010033
34#include "arm_compute/core/CL/ICLTensor.h"
35#include "arm_compute/core/Error.h"
36#include "arm_compute/core/TensorInfo.h"
37#include "arm_compute/core/Types.h"
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010038#include "src/core/CL/kernels/CLBatchConcatenateLayerKernel.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010039#include "src/core/helpers/AutoConfiguration.h"
Matthew Bentham92046462020-03-07 22:15:55 +000040#include "support/MemorySupport.h"
Georgios Pinitase29acf12018-07-16 14:40:09 +010041
42namespace arm_compute
43{
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010044namespace experimental
45{
Georgios Pinitas09cad722020-07-22 12:11:20 +010046CLConcatenation::CLConcatenation()
Michalis Spyrou8c571692019-04-05 11:29:52 +010047 : _concat_kernels(),
Pablo Tello6a14adb2019-03-05 17:33:08 +000048 _num_inputs(0),
49 _axis(Window::DimX)
Georgios Pinitase29acf12018-07-16 14:40:09 +010050{
51}
52
Georgios Pinitas09cad722020-07-22 12:11:20 +010053void CLConcatenation::configure(const CLCompileContext &compile_context, const std::vector<ITensorInfo *> &inputs_vector, ITensorInfo *output, size_t axis)
Pablo Tello6a14adb2019-03-05 17:33:08 +000054{
Michalis Spyrou8c571692019-04-05 11:29:52 +010055 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010056 _axis = axis;
Pablo Tello6a14adb2019-03-05 17:33:08 +000057 _num_inputs = inputs_vector.size();
58
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010059 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
60 std::vector<const ITensorInfo *> const_inputs_vector(inputs_vector.size());
61 std::transform(inputs_vector.begin(), inputs_vector.end(), const_inputs_vector.begin(), [](ITensorInfo * t)
Pablo Tello6a14adb2019-03-05 17:33:08 +000062 {
63 ARM_COMPUTE_ERROR_ON_NULLPTR(t);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010064 return t;
Pablo Tello6a14adb2019-03-05 17:33:08 +000065 });
Pablo Tello6a14adb2019-03-05 17:33:08 +000066
67 // Output auto inizialitation if not yet initialized
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010068 auto_init_if_empty(*output, output_shape, 1, inputs_vector[0]->data_type());
69 ARM_COMPUTE_ERROR_THROW_ON(CLConcatenateLayer::validate(const_inputs_vector, output, axis));
Pablo Tello6a14adb2019-03-05 17:33:08 +000070
Michalis Spyrou8c571692019-04-05 11:29:52 +010071 unsigned int offset = 0;
Pablo Tello6a14adb2019-03-05 17:33:08 +000072 switch(_axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +010073 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010074 case Window::DimX:
Georgios Pinitase29acf12018-07-16 14:40:09 +010075 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010076 switch(_num_inputs)
77 {
78 case 2:
79 {
80 // Configure WidthConcatenate2Tensors kernel
81 auto kernel = support::cpp14::make_unique<CLWidthConcatenate2TensorsKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010082 kernel->configure(compile_context, inputs_vector.at(0), inputs_vector.at(1), output);
Michalis Spyrou8c571692019-04-05 11:29:52 +010083 _concat_kernels.emplace_back(std::move(kernel));
84 break;
85 }
86 case 4:
87 {
88 // Configure WidthConcatenate4Tensors kernel
89 auto kernel = support::cpp14::make_unique<CLWidthConcatenate4TensorsKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010090 kernel->configure(compile_context, inputs_vector.at(0), inputs_vector.at(1), inputs_vector.at(2), inputs_vector.at(3), output);
Michalis Spyrou8c571692019-04-05 11:29:52 +010091 _concat_kernels.emplace_back(std::move(kernel));
92 break;
93 }
94 default:
95 {
96 // Configure generic case WidthConcatenate kernels
97 for(unsigned int i = 0; i < _num_inputs; ++i)
98 {
99 auto kernel = support::cpp14::make_unique<CLWidthConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100100 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100101 offset += inputs_vector.at(i)->dimension(_axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100102 _concat_kernels.emplace_back(std::move(kernel));
103 }
104 break;
105 }
106 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100107 break;
108 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100109 case Window::DimY:
Pablo Tello6a14adb2019-03-05 17:33:08 +0000110 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100111 for(unsigned int i = 0; i < _num_inputs; ++i)
112 {
113 auto kernel = support::cpp14::make_unique<CLHeightConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100114 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100115 offset += inputs_vector.at(i)->dimension(_axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100116 _concat_kernels.emplace_back(std::move(kernel));
117 }
Pablo Tello6a14adb2019-03-05 17:33:08 +0000118 break;
119 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100120 case Window::DimZ:
Georgios Pinitase29acf12018-07-16 14:40:09 +0100121 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100122 for(unsigned int i = 0; i < _num_inputs; ++i)
123 {
124 auto kernel = support::cpp14::make_unique<CLDepthConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100125 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100126 offset += inputs_vector.at(i)->dimension(_axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100127 _concat_kernels.emplace_back(std::move(kernel));
128 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100129 break;
130 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100131 case 3:
132 {
133 for(unsigned int i = 0; i < _num_inputs; ++i)
134 {
135 auto kernel = support::cpp14::make_unique<CLBatchConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100136 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100137 offset += inputs_vector.at(i)->dimension(_axis);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100138 _concat_kernels.emplace_back(std::move(kernel));
139 }
140 break;
141 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100142 default:
Michalis Spyrou8c571692019-04-05 11:29:52 +0100143 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitase29acf12018-07-16 14:40:09 +0100144 }
145}
146
Georgios Pinitas09cad722020-07-22 12:11:20 +0100147Status CLConcatenation::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +0100148{
149 ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100150 const unsigned int num_inputs = inputs_vector.size();
Georgios Pinitase29acf12018-07-16 14:40:09 +0100151
Michalis Spyrou8c571692019-04-05 11:29:52 +0100152 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
153 ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100154
Michalis Spyrou8c571692019-04-05 11:29:52 +0100155 unsigned int offset = 0;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100156 switch(axis)
Michalis Spyrou8c571692019-04-05 11:29:52 +0100157 {
158 case Window::DimX:
159 {
160 switch(num_inputs)
161 {
162 case 2:
163 // Validate WidthConcatenate2Tensors kernels if there are 2 inputs
164 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100165 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(inputs_vector[0], inputs_vector[1], output));
Michalis Spyrou8c571692019-04-05 11:29:52 +0100166 break;
167 case 4:
168 // Validate WidthConcatenate4Tensors kernels if there are 4 inputs
169 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1], inputs_vector[2], inputs_vector[3]);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100170 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate4TensorsKernel::validate(inputs_vector[0], inputs_vector[1], inputs_vector[2], inputs_vector[3], output));
Michalis Spyrou8c571692019-04-05 11:29:52 +0100171 break;
172 default:
173 // Validate generic case of WidthConcatenate kernel
174 for(const auto &input : inputs_vector)
175 {
176 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100177 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100178 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100179 }
180 break;
181 }
182 break;
183 }
184 case Window::DimY:
185 {
186 for(const auto &input : inputs_vector)
187 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100188 ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100189 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100190 }
191 break;
192 }
193 case Window::DimZ:
194 {
195 for(const auto &input : inputs_vector)
196 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100197 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100198 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100199 }
200 break;
201 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100202 case 3:
203 {
204 for(const auto &input : inputs_vector)
205 {
206 ARM_COMPUTE_RETURN_ON_ERROR(CLBatchConcatenateLayerKernel::validate(input, offset, output));
207 offset += input->dimension(axis);
208 }
209 break;
210 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100211 default:
212 ARM_COMPUTE_ERROR("Axis not supported");
213 }
214
Michalis Spyroua9c44722019-04-05 17:18:36 +0100215 if(output->total_size() != 0)
216 {
217 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
218 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
219 }
220
Georgios Pinitase29acf12018-07-16 14:40:09 +0100221 return Status{};
222}
223
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100224void CLConcatenation::run(ITensorPack &tensors)
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100225{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100226 if(tensors.empty())
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100227 {
228 ARM_COMPUTE_ERROR("No inputs provided");
229 }
230
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100231 if(static_cast<int>(tensors.size()) - 1 != static_cast<int>(_num_inputs))
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100232 {
233 ARM_COMPUTE_ERROR("Configured with different number of inputs");
234 }
235
236 if(_axis == Window::DimX && (_num_inputs == 2 || _num_inputs == 4))
237 {
238 ARM_COMPUTE_ERROR_ON(_concat_kernels.empty());
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100239 CLScheduler::get().enqueue_op(*_concat_kernels.at(0), tensors, true);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100240 }
241 else
242 {
243 int i = 0;
244 for(auto &k : _concat_kernels)
245 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100246 ITensorPack pack;
247 pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i));
248 pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST));
249 CLScheduler::get().enqueue_op(*k, pack, true);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100250 ++i;
251 }
252 }
253}
254} // namespace experimental
255
256struct CLConcatenateLayer::Impl
257{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100258 std::vector<const ICLTensor *> srcs{};
259 ICLTensor *dst{ nullptr };
260 unsigned int num_inputs{ 0 };
261 unsigned int axis{ 0 };
262 std::unique_ptr<experimental::CLConcatenation> op{ nullptr };
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100263};
264
265CLConcatenateLayer::CLConcatenateLayer()
266 : _impl(support::cpp14::make_unique<Impl>())
267{
268}
269
270CLConcatenateLayer::CLConcatenateLayer(CLConcatenateLayer &&) = default;
271
272CLConcatenateLayer &CLConcatenateLayer::operator=(CLConcatenateLayer &&) = default;
273
274CLConcatenateLayer::~CLConcatenateLayer() = default;
275
276void CLConcatenateLayer::configure(std::vector<const ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
277{
278 configure(CLKernelLibrary::get().get_compile_context(), inputs_vector, output, axis);
279}
280
281void CLConcatenateLayer::configure(const CLCompileContext &compile_context, std::vector<const ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
282{
283 ARM_COMPUTE_ERROR_ON(output == nullptr);
284
285 _impl->srcs = inputs_vector;
286 _impl->dst = output;
287 _impl->axis = axis;
288 _impl->num_inputs = inputs_vector.size();
Georgios Pinitas09cad722020-07-22 12:11:20 +0100289 _impl->op = arm_compute::support::cpp14::make_unique<experimental::CLConcatenation>();
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100290
291 std::vector<ITensorInfo *> inputs_vector_info;
292 for(unsigned int i = 0; i < inputs_vector.size(); ++i)
293 {
294 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
295 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
296 }
297 _impl->op->configure(compile_context, inputs_vector_info, _impl->dst->info(), axis);
298}
299
300Status CLConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
301{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100302 return experimental::CLConcatenation::validate(inputs_vector, output, axis);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100303}
304
Georgios Pinitase29acf12018-07-16 14:40:09 +0100305void CLConcatenateLayer::run()
306{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100307 ITensorPack pack;
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100308 for(unsigned i = 0; i < _impl->num_inputs; ++i)
Pablo Tello6a14adb2019-03-05 17:33:08 +0000309 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100310 pack.add_tensor(TensorType::ACL_SRC_VEC + i, _impl->srcs.at(i));
Pablo Tello6a14adb2019-03-05 17:33:08 +0000311 }
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100312 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100313
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100314 _impl->op->run(pack);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100315}
316} // namespace arm_compute