blob: b9b3c5bb8050c1888f56143385a23d5327408c75 [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 Spyrou8c571692019-04-05 11:29:52 +010059 TensorShape output_shape{};
60 if(_axis == Window::DimZ)
61 {
62 output_shape = arm_compute::misc::shape_calculator::calculate_depth_concatenate_shape(inputs_vector);
63 }
64 else
65 {
66 output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
67 }
Pablo Tello6a14adb2019-03-05 17:33:08 +000068
69 // Output auto inizialitation if not yet initialized
70 auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type());
Michalis Spyrou8c571692019-04-05 11:29:52 +010071 ARM_COMPUTE_ERROR_THROW_ON(CLConcatenateLayer::validate(inputs_vector_info, output->info(), axis));
Pablo Tello6a14adb2019-03-05 17:33:08 +000072
Michalis Spyrou8c571692019-04-05 11:29:52 +010073 unsigned int offset = 0;
Pablo Tello6a14adb2019-03-05 17:33:08 +000074 switch(_axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +010075 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010076 case Window::DimX:
Georgios Pinitase29acf12018-07-16 14:40:09 +010077 {
Michalis Spyrou8c571692019-04-05 11:29:52 +010078 switch(_num_inputs)
79 {
80 case 2:
81 {
82 // Configure WidthConcatenate2Tensors kernel
83 auto kernel = support::cpp14::make_unique<CLWidthConcatenate2TensorsKernel>();
84 kernel->configure(inputs_vector.at(0), inputs_vector.at(1), output);
85 _concat_kernels.emplace_back(std::move(kernel));
86 break;
87 }
88 case 4:
89 {
90 // Configure WidthConcatenate4Tensors kernel
91 auto kernel = support::cpp14::make_unique<CLWidthConcatenate4TensorsKernel>();
92 kernel->configure(inputs_vector.at(0), inputs_vector.at(1), inputs_vector.at(2), inputs_vector.at(3), output);
93 _concat_kernels.emplace_back(std::move(kernel));
94 break;
95 }
96 default:
97 {
98 // Configure generic case WidthConcatenate kernels
99 for(unsigned int i = 0; i < _num_inputs; ++i)
100 {
101 auto kernel = support::cpp14::make_unique<CLWidthConcatenateLayerKernel>();
102 kernel->configure(inputs_vector.at(i), offset, output);
103 offset += inputs_vector.at(i)->info()->dimension(_axis);
104 _concat_kernels.emplace_back(std::move(kernel));
105 }
106 break;
107 }
108 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100109 break;
110 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100111 case Window::DimY:
Pablo Tello6a14adb2019-03-05 17:33:08 +0000112 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100113 for(unsigned int i = 0; i < _num_inputs; ++i)
114 {
115 auto kernel = support::cpp14::make_unique<CLHeightConcatenateLayerKernel>();
116 kernel->configure(inputs_vector.at(i), offset, output);
117 offset += inputs_vector.at(i)->info()->dimension(_axis);
118 _concat_kernels.emplace_back(std::move(kernel));
119 }
Pablo Tello6a14adb2019-03-05 17:33:08 +0000120 break;
121 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100122 case Window::DimZ:
Georgios Pinitase29acf12018-07-16 14:40:09 +0100123 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100124 for(unsigned int i = 0; i < _num_inputs; ++i)
125 {
126 auto kernel = support::cpp14::make_unique<CLDepthConcatenateLayerKernel>();
127 kernel->configure(inputs_vector.at(i), offset, output);
128 offset += inputs_vector.at(i)->info()->dimension(_axis);
129 _concat_kernels.emplace_back(std::move(kernel));
130 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100131 break;
132 }
133 default:
Michalis Spyrou8c571692019-04-05 11:29:52 +0100134 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitase29acf12018-07-16 14:40:09 +0100135 }
136}
137
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100138Status CLConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitase29acf12018-07-16 14:40:09 +0100139{
140 ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100141 const unsigned int num_inputs = inputs_vector.size();
Georgios Pinitase29acf12018-07-16 14:40:09 +0100142
Michalis Spyrou8c571692019-04-05 11:29:52 +0100143 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
144 ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100145
146 // Output auto inizialitation if not yet initialized
147 TensorInfo tmp_output_info = *output->clone();
148 TensorShape output_shape{};
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100149 if(axis == Window::DimZ)
Georgios Pinitase29acf12018-07-16 14:40:09 +0100150 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100151 output_shape = arm_compute::misc::shape_calculator::calculate_depth_concatenate_shape(inputs_vector);
Georgios Pinitase29acf12018-07-16 14:40:09 +0100152 }
Michalis Spyrou8c571692019-04-05 11:29:52 +0100153 else
154 {
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100155 output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100156 }
157 auto_init_if_empty(tmp_output_info, output_shape, 1, inputs_vector[0]->data_type());
158
159 unsigned int offset = 0;
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100160 switch(axis)
Michalis Spyrou8c571692019-04-05 11:29:52 +0100161 {
162 case Window::DimX:
163 {
164 switch(num_inputs)
165 {
166 case 2:
167 // Validate WidthConcatenate2Tensors kernels if there are 2 inputs
168 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]);
169 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(inputs_vector[0], inputs_vector[1], &tmp_output_info));
170 break;
171 case 4:
172 // Validate WidthConcatenate4Tensors kernels if there are 4 inputs
173 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1], inputs_vector[2], inputs_vector[3]);
174 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate4TensorsKernel::validate(inputs_vector[0], inputs_vector[1], inputs_vector[2], inputs_vector[3], &tmp_output_info));
175 break;
176 default:
177 // Validate generic case of WidthConcatenate kernel
178 for(const auto &input : inputs_vector)
179 {
180 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
181 ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100182 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100183 }
184 break;
185 }
186 break;
187 }
188 case Window::DimY:
189 {
190 for(const auto &input : inputs_vector)
191 {
192 ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100193 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100194 }
195 break;
196 }
197 case Window::DimZ:
198 {
199 for(const auto &input : inputs_vector)
200 {
201 ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100202 offset += input->dimension(axis);
Michalis Spyrou8c571692019-04-05 11:29:52 +0100203 }
204 break;
205 }
206 default:
207 ARM_COMPUTE_ERROR("Axis not supported");
208 }
209
Georgios Pinitase29acf12018-07-16 14:40:09 +0100210 return Status{};
211}
212
213void CLConcatenateLayer::run()
214{
Michalis Spyrou8c571692019-04-05 11:29:52 +0100215 for(auto &kernel : _concat_kernels)
Pablo Tello6a14adb2019-03-05 17:33:08 +0000216 {
Michalis Spyrou8c571692019-04-05 11:29:52 +0100217 CLScheduler::get().enqueue(*kernel, true);
Pablo Tello6a14adb2019-03-05 17:33:08 +0000218 }
Georgios Pinitase29acf12018-07-16 14:40:09 +0100219}
220} // namespace arm_compute