blob: 0c473a79c83338aeeef785776ba0854ef46708fd [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"
Georgios Pinitase29acf12018-07-16 14:40:09 +010040
41namespace arm_compute
42{
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010043namespace experimental
44{
Georgios Pinitas09cad722020-07-22 12:11:20 +010045CLConcatenation::CLConcatenation()
Michalis Spyrou8c571692019-04-05 11:29:52 +010046 : _concat_kernels(),
Pablo Tello6a14adb2019-03-05 17:33:08 +000047 _num_inputs(0),
48 _axis(Window::DimX)
Georgios Pinitase29acf12018-07-16 14:40:09 +010049{
50}
51
Georgios Pinitas09cad722020-07-22 12:11:20 +010052void CLConcatenation::configure(const CLCompileContext &compile_context, const std::vector<ITensorInfo *> &inputs_vector, ITensorInfo *output, size_t axis)
Pablo Tello6a14adb2019-03-05 17:33:08 +000053{
Michalis Spyrou8c571692019-04-05 11:29:52 +010054 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010055 _axis = axis;
Pablo Tello6a14adb2019-03-05 17:33:08 +000056 _num_inputs = inputs_vector.size();
57
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010058 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
59 std::vector<const ITensorInfo *> const_inputs_vector(inputs_vector.size());
60 std::transform(inputs_vector.begin(), inputs_vector.end(), const_inputs_vector.begin(), [](ITensorInfo * t)
Pablo Tello6a14adb2019-03-05 17:33:08 +000061 {
62 ARM_COMPUTE_ERROR_ON_NULLPTR(t);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010063 return t;
Pablo Tello6a14adb2019-03-05 17:33:08 +000064 });
Pablo Tello6a14adb2019-03-05 17:33:08 +000065
66 // Output auto inizialitation if not yet initialized
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +010067 auto_init_if_empty(*output, output_shape, 1, inputs_vector[0]->data_type());
68 ARM_COMPUTE_ERROR_THROW_ON(CLConcatenateLayer::validate(const_inputs_vector, output, axis));
Pablo Tello6a14adb2019-03-05 17:33:08 +000069
Michalis Spyrou8c571692019-04-05 11:29:52 +010070 unsigned int offset = 0;
Pablo Tello6a14adb2019-03-05 17:33:08 +000071 switch(_axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +010072 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010073 case Window::DimX:
Georgios Pinitase29acf12018-07-16 14:40:09 +010074 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010075 switch(_num_inputs)
76 {
77 case 2:
78 {
79 // Configure WidthConcatenate2Tensors kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +000080 auto kernel = std::make_unique<CLWidthConcatenate2TensorsKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010081 kernel->configure(compile_context, inputs_vector.at(0), inputs_vector.at(1), output);
Michalis Spyrou8c571692019-04-05 11:29:52 +010082 _concat_kernels.emplace_back(std::move(kernel));
83 break;
84 }
85 case 4:
86 {
87 // Configure WidthConcatenate4Tensors kernel
Georgios Pinitas40f51a62020-11-21 03:04:18 +000088 auto kernel = std::make_unique<CLWidthConcatenate4TensorsKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010089 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 +010090 _concat_kernels.emplace_back(std::move(kernel));
91 break;
92 }
93 default:
94 {
95 // Configure generic case WidthConcatenate kernels
96 for(unsigned int i = 0; i < _num_inputs; ++i)
97 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +000098 auto kernel = std::make_unique<CLWidthConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +010099 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100100 offset += inputs_vector.at(i)->dimension(_axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100101 _concat_kernels.emplace_back(std::move(kernel));
102 }
103 break;
104 }
105 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100106 break;
107 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100108 case Window::DimY:
Pablo Tello6a14adb2019-03-05 17:33:08 +0000109 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100110 for(unsigned int i = 0; i < _num_inputs; ++i)
111 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000112 auto kernel = std::make_unique<CLHeightConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100113 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100114 offset += inputs_vector.at(i)->dimension(_axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100115 _concat_kernels.emplace_back(std::move(kernel));
116 }
Pablo Tello6a14adb2019-03-05 17:33:08 +0000117 break;
118 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100119 case Window::DimZ:
Georgios Pinitase29acf12018-07-16 14:40:09 +0100120 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100121 for(unsigned int i = 0; i < _num_inputs; ++i)
122 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000123 auto kernel = std::make_unique<CLDepthConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100124 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100125 offset += inputs_vector.at(i)->dimension(_axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100126 _concat_kernels.emplace_back(std::move(kernel));
127 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100128 break;
129 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100130 case 3:
131 {
132 for(unsigned int i = 0; i < _num_inputs; ++i)
133 {
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000134 auto kernel = std::make_unique<CLBatchConcatenateLayerKernel>();
Manuel Bottini2b84be52020-04-08 10:15:51 +0100135 kernel->configure(compile_context, inputs_vector.at(i), offset, output);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100136 offset += inputs_vector.at(i)->dimension(_axis);
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100137 _concat_kernels.emplace_back(std::move(kernel));
138 }
139 break;
140 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100141 default:
Michalis Spyrou8c571692019-04-05 11:29:52 +0100142 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitase29acf12018-07-16 14:40:09 +0100143 }
144}
145
Georgios Pinitas09cad722020-07-22 12:11:20 +0100146Status CLConcatenation::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +0100147{
148 ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100149 const unsigned int num_inputs = inputs_vector.size();
Georgios Pinitase29acf12018-07-16 14:40:09 +0100150
Michalis Spyrou8c571692019-04-05 11:29:52 +0100151 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
152 ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100153
Michalis Spyrou8c571692019-04-05 11:29:52 +0100154 unsigned int offset = 0;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100155 switch(axis)
Michalis Spyrou8c571692019-04-05 11:29:52 +0100156 {
157 case Window::DimX:
158 {
159 switch(num_inputs)
160 {
161 case 2:
162 // Validate WidthConcatenate2Tensors kernels if there are 2 inputs
163 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100164 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(inputs_vector[0], inputs_vector[1], output));
Michalis Spyrou8c571692019-04-05 11:29:52 +0100165 break;
166 case 4:
167 // Validate WidthConcatenate4Tensors kernels if there are 4 inputs
168 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 +0100169 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 +0100170 break;
171 default:
172 // Validate generic case of WidthConcatenate kernel
173 for(const auto &input : inputs_vector)
174 {
175 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100176 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100177 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100178 }
179 break;
180 }
181 break;
182 }
183 case Window::DimY:
184 {
185 for(const auto &input : inputs_vector)
186 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100187 ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100188 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100189 }
190 break;
191 }
192 case Window::DimZ:
193 {
194 for(const auto &input : inputs_vector)
195 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100196 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100197 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100198 }
199 break;
200 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100201 case 3:
202 {
203 for(const auto &input : inputs_vector)
204 {
205 ARM_COMPUTE_RETURN_ON_ERROR(CLBatchConcatenateLayerKernel::validate(input, offset, output));
206 offset += input->dimension(axis);
207 }
208 break;
209 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100210 default:
211 ARM_COMPUTE_ERROR("Axis not supported");
212 }
213
Michalis Spyroua9c44722019-04-05 17:18:36 +0100214 if(output->total_size() != 0)
215 {
216 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
217 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
218 }
219
Georgios Pinitase29acf12018-07-16 14:40:09 +0100220 return Status{};
221}
222
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100223void CLConcatenation::run(ITensorPack &tensors)
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100224{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100225 if(tensors.empty())
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100226 {
227 ARM_COMPUTE_ERROR("No inputs provided");
228 }
229
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100230 if(static_cast<int>(tensors.size()) - 1 != static_cast<int>(_num_inputs))
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100231 {
232 ARM_COMPUTE_ERROR("Configured with different number of inputs");
233 }
234
235 if(_axis == Window::DimX && (_num_inputs == 2 || _num_inputs == 4))
236 {
237 ARM_COMPUTE_ERROR_ON(_concat_kernels.empty());
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100238 CLScheduler::get().enqueue_op(*_concat_kernels.at(0), tensors, true);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100239 }
240 else
241 {
242 int i = 0;
243 for(auto &k : _concat_kernels)
244 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100245 ITensorPack pack;
246 pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i));
247 pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST));
248 CLScheduler::get().enqueue_op(*k, pack, true);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100249 ++i;
250 }
251 }
252}
253} // namespace experimental
254
255struct CLConcatenateLayer::Impl
256{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100257 std::vector<const ICLTensor *> srcs{};
258 ICLTensor *dst{ nullptr };
259 unsigned int num_inputs{ 0 };
260 unsigned int axis{ 0 };
261 std::unique_ptr<experimental::CLConcatenation> op{ nullptr };
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100262};
263
264CLConcatenateLayer::CLConcatenateLayer()
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000265 : _impl(std::make_unique<Impl>())
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100266{
267}
268
269CLConcatenateLayer::CLConcatenateLayer(CLConcatenateLayer &&) = default;
270
271CLConcatenateLayer &CLConcatenateLayer::operator=(CLConcatenateLayer &&) = default;
272
273CLConcatenateLayer::~CLConcatenateLayer() = default;
274
275void CLConcatenateLayer::configure(std::vector<const ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
276{
277 configure(CLKernelLibrary::get().get_compile_context(), inputs_vector, output, axis);
278}
279
280void CLConcatenateLayer::configure(const CLCompileContext &compile_context, std::vector<const ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
281{
282 ARM_COMPUTE_ERROR_ON(output == nullptr);
283
284 _impl->srcs = inputs_vector;
285 _impl->dst = output;
286 _impl->axis = axis;
287 _impl->num_inputs = inputs_vector.size();
Georgios Pinitas40f51a62020-11-21 03:04:18 +0000288 _impl->op = std::make_unique<experimental::CLConcatenation>();
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100289
290 std::vector<ITensorInfo *> inputs_vector_info;
291 for(unsigned int i = 0; i < inputs_vector.size(); ++i)
292 {
293 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
294 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
295 }
296 _impl->op->configure(compile_context, inputs_vector_info, _impl->dst->info(), axis);
297}
298
299Status CLConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
300{
Georgios Pinitas09cad722020-07-22 12:11:20 +0100301 return experimental::CLConcatenation::validate(inputs_vector, output, axis);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100302}
303
Georgios Pinitase29acf12018-07-16 14:40:09 +0100304void CLConcatenateLayer::run()
305{
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100306 ITensorPack pack;
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100307 for(unsigned i = 0; i < _impl->num_inputs; ++i)
Pablo Tello6a14adb2019-03-05 17:33:08 +0000308 {
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100309 pack.add_tensor(TensorType::ACL_SRC_VEC + i, _impl->srcs.at(i));
Pablo Tello6a14adb2019-03-05 17:33:08 +0000310 }
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100311 pack.add_tensor(TensorType::ACL_DST, _impl->dst);
Michele Di Giorgiof932d2c2020-07-06 11:27:21 +0100312
Georgios Pinitas0499dff2020-07-31 22:21:38 +0100313 _impl->op->run(pack);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100314}
315} // namespace arm_compute