blob: 5d224db8e914b12e4431ab5ca82dffe99a339179 [file] [log] [blame]
Georgios Pinitase29acf12018-07-16 14:40:09 +01001/*
Pablo Tello6a14adb2019-03-05 17:33:08 +00002 * Copyright (c) 2018-2019 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
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +010026#include "arm_compute/core/CL/kernels/CLBatchConcatenateLayerKernel.h"
Georgios Pinitas09f24972019-05-17 18:14:40 +010027#include "arm_compute/core/CL/kernels/CLDepthConcatenateLayerKernel.h"
Pablo Tello6a14adb2019-03-05 17:33:08 +000028#include "arm_compute/core/CL/kernels/CLHeightConcatenateLayerKernel.h"
Georgios Pinitas09f24972019-05-17 18:14:40 +010029#include "arm_compute/core/CL/kernels/CLWidthConcatenate2TensorsKernel.h"
30#include "arm_compute/core/CL/kernels/CLWidthConcatenate4TensorsKernel.h"
31#include "arm_compute/core/CL/kernels/CLWidthConcatenateLayerKernel.h"
Pablo Tello6a14adb2019-03-05 17:33:08 +000032#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitase29acf12018-07-16 14:40:09 +010034
35#include "arm_compute/core/CL/ICLTensor.h"
36#include "arm_compute/core/Error.h"
37#include "arm_compute/core/TensorInfo.h"
38#include "arm_compute/core/Types.h"
39#include "support/ToolchainSupport.h"
40
41namespace arm_compute
42{
43CLConcatenateLayer::CLConcatenateLayer()
Michalis Spyrou8c571692019-04-05 11:29:52 +010044 : _concat_kernels(),
Pablo Tello6a14adb2019-03-05 17:33:08 +000045 _num_inputs(0),
46 _axis(Window::DimX)
Georgios Pinitase29acf12018-07-16 14:40:09 +010047{
48}
49
Manuel Bottini10c53f12019-07-17 16:11:53 +010050void CLConcatenateLayer::configure(std::vector<ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
51{
52 configure_internal(std::move(inputs_vector), output, axis);
53}
54
55void CLConcatenateLayer::configure(std::vector<const ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
56{
57 configure_internal(std::move(inputs_vector), output, axis);
58}
59
60Status CLConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
61{
62 return validate_internal(inputs_vector, output, axis);
63}
64
65Status CLConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
66{
67 return validate_internal(inputs_vector, output, axis);
68}
69
70template <typename TensorType>
71void CLConcatenateLayer::configure_internal(std::vector<TensorType *> &&inputs_vector, ICLTensor *output, size_t axis)
Pablo Tello6a14adb2019-03-05 17:33:08 +000072{
Michalis Spyrou8c571692019-04-05 11:29:52 +010073 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010074 _axis = axis;
Pablo Tello6a14adb2019-03-05 17:33:08 +000075 _num_inputs = inputs_vector.size();
76
77 std::vector<ITensorInfo *> inputs_vector_info(inputs_vector.size());
Manuel Bottini10c53f12019-07-17 16:11:53 +010078 std::transform(inputs_vector.begin(), inputs_vector.end(), inputs_vector_info.begin(), [](TensorType * t)
Pablo Tello6a14adb2019-03-05 17:33:08 +000079 {
80 ARM_COMPUTE_ERROR_ON_NULLPTR(t);
81 return t->info();
82 });
Michalis Spyroua9c44722019-04-05 17:18:36 +010083 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
Pablo Tello6a14adb2019-03-05 17:33:08 +000084
85 // Output auto inizialitation if not yet initialized
86 auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type());
Michalis Spyrou8c571692019-04-05 11:29:52 +010087 ARM_COMPUTE_ERROR_THROW_ON(CLConcatenateLayer::validate(inputs_vector_info, output->info(), axis));
Pablo Tello6a14adb2019-03-05 17:33:08 +000088
Michalis Spyrou8c571692019-04-05 11:29:52 +010089 unsigned int offset = 0;
Pablo Tello6a14adb2019-03-05 17:33:08 +000090 switch(_axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +010091 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010092 case Window::DimX:
Georgios Pinitase29acf12018-07-16 14:40:09 +010093 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010094 switch(_num_inputs)
95 {
96 case 2:
97 {
98 // Configure WidthConcatenate2Tensors kernel
99 auto kernel = support::cpp14::make_unique<CLWidthConcatenate2TensorsKernel>();
100 kernel->configure(inputs_vector.at(0), inputs_vector.at(1), output);
101 _concat_kernels.emplace_back(std::move(kernel));
102 break;
103 }
104 case 4:
105 {
106 // Configure WidthConcatenate4Tensors kernel
107 auto kernel = support::cpp14::make_unique<CLWidthConcatenate4TensorsKernel>();
108 kernel->configure(inputs_vector.at(0), inputs_vector.at(1), inputs_vector.at(2), inputs_vector.at(3), output);
109 _concat_kernels.emplace_back(std::move(kernel));
110 break;
111 }
112 default:
113 {
114 // Configure generic case WidthConcatenate kernels
115 for(unsigned int i = 0; i < _num_inputs; ++i)
116 {
117 auto kernel = support::cpp14::make_unique<CLWidthConcatenateLayerKernel>();
118 kernel->configure(inputs_vector.at(i), offset, output);
119 offset += inputs_vector.at(i)->info()->dimension(_axis);
120 _concat_kernels.emplace_back(std::move(kernel));
121 }
122 break;
123 }
124 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100125 break;
126 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100127 case Window::DimY:
Pablo Tello6a14adb2019-03-05 17:33:08 +0000128 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100129 for(unsigned int i = 0; i < _num_inputs; ++i)
130 {
131 auto kernel = support::cpp14::make_unique<CLHeightConcatenateLayerKernel>();
132 kernel->configure(inputs_vector.at(i), offset, output);
133 offset += inputs_vector.at(i)->info()->dimension(_axis);
134 _concat_kernels.emplace_back(std::move(kernel));
135 }
Pablo Tello6a14adb2019-03-05 17:33:08 +0000136 break;
137 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100138 case Window::DimZ:
Georgios Pinitase29acf12018-07-16 14:40:09 +0100139 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100140 for(unsigned int i = 0; i < _num_inputs; ++i)
141 {
142 auto kernel = support::cpp14::make_unique<CLDepthConcatenateLayerKernel>();
143 kernel->configure(inputs_vector.at(i), offset, output);
144 offset += inputs_vector.at(i)->info()->dimension(_axis);
145 _concat_kernels.emplace_back(std::move(kernel));
146 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100147 break;
148 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100149 case 3:
150 {
151 for(unsigned int i = 0; i < _num_inputs; ++i)
152 {
153 auto kernel = support::cpp14::make_unique<CLBatchConcatenateLayerKernel>();
154 kernel->configure(inputs_vector.at(i), offset, output);
155 offset += inputs_vector.at(i)->info()->dimension(_axis);
156 _concat_kernels.emplace_back(std::move(kernel));
157 }
158 break;
159 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100160 default:
Michalis Spyrou8c571692019-04-05 11:29:52 +0100161 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitase29acf12018-07-16 14:40:09 +0100162 }
163}
164
Manuel Bottini10c53f12019-07-17 16:11:53 +0100165template <typename TensorInfoType>
166Status CLConcatenateLayer::validate_internal(const std::vector<TensorInfoType *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +0100167{
168 ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100169 const unsigned int num_inputs = inputs_vector.size();
Georgios Pinitase29acf12018-07-16 14:40:09 +0100170
Michalis Spyrou8c571692019-04-05 11:29:52 +0100171 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
172 ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100173
Michalis Spyrou8c571692019-04-05 11:29:52 +0100174 unsigned int offset = 0;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100175 switch(axis)
Michalis Spyrou8c571692019-04-05 11:29:52 +0100176 {
177 case Window::DimX:
178 {
179 switch(num_inputs)
180 {
181 case 2:
182 // Validate WidthConcatenate2Tensors kernels if there are 2 inputs
183 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100184 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(inputs_vector[0], inputs_vector[1], output));
Michalis Spyrou8c571692019-04-05 11:29:52 +0100185 break;
186 case 4:
187 // Validate WidthConcatenate4Tensors kernels if there are 4 inputs
188 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 +0100189 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 +0100190 break;
191 default:
192 // Validate generic case of WidthConcatenate kernel
193 for(const auto &input : inputs_vector)
194 {
195 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100196 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::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 }
201 break;
202 }
203 case Window::DimY:
204 {
205 for(const auto &input : inputs_vector)
206 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100207 ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100208 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100209 }
210 break;
211 }
212 case Window::DimZ:
213 {
214 for(const auto &input : inputs_vector)
215 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100216 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100217 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100218 }
219 break;
220 }
Vidhya Sudhan Loganathan338595b2019-06-28 14:09:53 +0100221 case 3:
222 {
223 for(const auto &input : inputs_vector)
224 {
225 ARM_COMPUTE_RETURN_ON_ERROR(CLBatchConcatenateLayerKernel::validate(input, offset, output));
226 offset += input->dimension(axis);
227 }
228 break;
229 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100230 default:
231 ARM_COMPUTE_ERROR("Axis not supported");
232 }
233
Michalis Spyroua9c44722019-04-05 17:18:36 +0100234 if(output->total_size() != 0)
235 {
236 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
237 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
238 }
239
Georgios Pinitase29acf12018-07-16 14:40:09 +0100240 return Status{};
241}
242
243void CLConcatenateLayer::run()
244{
Michalis Spyrou8c571692019-04-05 11:29:52 +0100245 for(auto &kernel : _concat_kernels)
Pablo Tello6a14adb2019-03-05 17:33:08 +0000246 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100247 CLScheduler::get().enqueue(*kernel, true);
Pablo Tello6a14adb2019-03-05 17:33:08 +0000248 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100249}
250} // namespace arm_compute