blob: b8224d2ccefe7cf36b86c1d39051a2f3df9732bd [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
Pablo Tello6a14adb2019-03-05 17:33:08 +000026#include "arm_compute/core/CL/kernels/CLHeightConcatenateLayerKernel.h"
27#include "arm_compute/core/utils/misc/ShapeCalculator.h"
28#include "arm_compute/runtime/CL/CLScheduler.h"
Georgios Pinitase29acf12018-07-16 14:40:09 +010029#include "arm_compute/runtime/CL/functions/CLDepthConcatenateLayer.h"
30#include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h"
31
32#include "arm_compute/core/CL/ICLTensor.h"
33#include "arm_compute/core/Error.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Types.h"
36#include "support/ToolchainSupport.h"
37
38namespace arm_compute
39{
40CLConcatenateLayer::CLConcatenateLayer()
Michalis Spyrou8c571692019-04-05 11:29:52 +010041 : _concat_kernels(),
Pablo Tello6a14adb2019-03-05 17:33:08 +000042 _num_inputs(0),
43 _axis(Window::DimX)
Georgios Pinitase29acf12018-07-16 14:40:09 +010044{
45}
46
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010047void CLConcatenateLayer::configure(const std::vector<ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis)
Pablo Tello6a14adb2019-03-05 17:33:08 +000048{
Michalis Spyrou8c571692019-04-05 11:29:52 +010049 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010050 _axis = axis;
Pablo Tello6a14adb2019-03-05 17:33:08 +000051 _num_inputs = inputs_vector.size();
52
53 std::vector<ITensorInfo *> inputs_vector_info(inputs_vector.size());
54 std::transform(inputs_vector.begin(), inputs_vector.end(), inputs_vector_info.begin(), [](ICLTensor * t)
55 {
56 ARM_COMPUTE_ERROR_ON_NULLPTR(t);
57 return t->info();
58 });
Michalis Spyroua9c44722019-04-05 17:18:36 +010059 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
Pablo Tello6a14adb2019-03-05 17:33:08 +000060
61 // Output auto inizialitation if not yet initialized
62 auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type());
Michalis Spyrou8c571692019-04-05 11:29:52 +010063 ARM_COMPUTE_ERROR_THROW_ON(CLConcatenateLayer::validate(inputs_vector_info, output->info(), axis));
Pablo Tello6a14adb2019-03-05 17:33:08 +000064
Michalis Spyrou8c571692019-04-05 11:29:52 +010065 unsigned int offset = 0;
Pablo Tello6a14adb2019-03-05 17:33:08 +000066 switch(_axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +010067 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010068 case Window::DimX:
Georgios Pinitase29acf12018-07-16 14:40:09 +010069 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010070 switch(_num_inputs)
71 {
72 case 2:
73 {
74 // Configure WidthConcatenate2Tensors kernel
75 auto kernel = support::cpp14::make_unique<CLWidthConcatenate2TensorsKernel>();
76 kernel->configure(inputs_vector.at(0), inputs_vector.at(1), output);
77 _concat_kernels.emplace_back(std::move(kernel));
78 break;
79 }
80 case 4:
81 {
82 // Configure WidthConcatenate4Tensors kernel
83 auto kernel = support::cpp14::make_unique<CLWidthConcatenate4TensorsKernel>();
84 kernel->configure(inputs_vector.at(0), inputs_vector.at(1), inputs_vector.at(2), inputs_vector.at(3), output);
85 _concat_kernels.emplace_back(std::move(kernel));
86 break;
87 }
88 default:
89 {
90 // Configure generic case WidthConcatenate kernels
91 for(unsigned int i = 0; i < _num_inputs; ++i)
92 {
93 auto kernel = support::cpp14::make_unique<CLWidthConcatenateLayerKernel>();
94 kernel->configure(inputs_vector.at(i), offset, output);
95 offset += inputs_vector.at(i)->info()->dimension(_axis);
96 _concat_kernels.emplace_back(std::move(kernel));
97 }
98 break;
99 }
100 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100101 break;
102 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100103 case Window::DimY:
Pablo Tello6a14adb2019-03-05 17:33:08 +0000104 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100105 for(unsigned int i = 0; i < _num_inputs; ++i)
106 {
107 auto kernel = support::cpp14::make_unique<CLHeightConcatenateLayerKernel>();
108 kernel->configure(inputs_vector.at(i), offset, output);
109 offset += inputs_vector.at(i)->info()->dimension(_axis);
110 _concat_kernels.emplace_back(std::move(kernel));
111 }
Pablo Tello6a14adb2019-03-05 17:33:08 +0000112 break;
113 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100114 case Window::DimZ:
Georgios Pinitase29acf12018-07-16 14:40:09 +0100115 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100116 for(unsigned int i = 0; i < _num_inputs; ++i)
117 {
118 auto kernel = support::cpp14::make_unique<CLDepthConcatenateLayerKernel>();
119 kernel->configure(inputs_vector.at(i), offset, output);
120 offset += inputs_vector.at(i)->info()->dimension(_axis);
121 _concat_kernels.emplace_back(std::move(kernel));
122 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100123 break;
124 }
125 default:
Michalis Spyrou8c571692019-04-05 11:29:52 +0100126 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitase29acf12018-07-16 14:40:09 +0100127 }
128}
129
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100130Status CLConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +0100131{
132 ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100133 const unsigned int num_inputs = inputs_vector.size();
Georgios Pinitase29acf12018-07-16 14:40:09 +0100134
Michalis Spyrou8c571692019-04-05 11:29:52 +0100135 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
136 ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100137
Michalis Spyrou8c571692019-04-05 11:29:52 +0100138 unsigned int offset = 0;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100139 switch(axis)
Michalis Spyrou8c571692019-04-05 11:29:52 +0100140 {
141 case Window::DimX:
142 {
143 switch(num_inputs)
144 {
145 case 2:
146 // Validate WidthConcatenate2Tensors kernels if there are 2 inputs
147 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100148 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(inputs_vector[0], inputs_vector[1], output));
Michalis Spyrou8c571692019-04-05 11:29:52 +0100149 break;
150 case 4:
151 // Validate WidthConcatenate4Tensors kernels if there are 4 inputs
152 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 +0100153 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 +0100154 break;
155 default:
156 // Validate generic case of WidthConcatenate kernel
157 for(const auto &input : inputs_vector)
158 {
159 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Michalis Spyroua9c44722019-04-05 17:18:36 +0100160 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100161 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100162 }
163 break;
164 }
165 break;
166 }
167 case Window::DimY:
168 {
169 for(const auto &input : inputs_vector)
170 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100171 ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100172 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100173 }
174 break;
175 }
176 case Window::DimZ:
177 {
178 for(const auto &input : inputs_vector)
179 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100180 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, output));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100181 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100182 }
183 break;
184 }
185 default:
186 ARM_COMPUTE_ERROR("Axis not supported");
187 }
188
Michalis Spyroua9c44722019-04-05 17:18:36 +0100189 if(output->total_size() != 0)
190 {
191 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
192 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
193 }
194
Georgios Pinitase29acf12018-07-16 14:40:09 +0100195 return Status{};
196}
197
198void CLConcatenateLayer::run()
199{
Michalis Spyrou8c571692019-04-05 11:29:52 +0100200 for(auto &kernel : _concat_kernels)
Pablo Tello6a14adb2019-03-05 17:33:08 +0000201 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100202 CLScheduler::get().enqueue(*kernel, true);
Pablo Tello6a14adb2019-03-05 17:33:08 +0000203 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100204}
205} // namespace arm_compute