blob: d338493e5124af693162f5efe3c541de578227a0 [file] [log] [blame]
Georgios Pinitasae54e022018-07-16 15:41:27 +01001/*
Pablo Tello3dd5b682019-03-04 14:14:02 +00002 * Copyright (c) 2018-2019 ARM Limited.
Georgios Pinitasae54e022018-07-16 15:41:27 +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/NEON/functions/NEConcatenateLayer.h"
25
Georgios Pinitas09f24972019-05-17 18:14:40 +010026#include "arm_compute/core/NEON/kernels/NEDepthConcatenateLayerKernel.h"
27#include "arm_compute/core/NEON/kernels/NEHeightConcatenateLayerKernel.h"
28#include "arm_compute/core/NEON/kernels/NEWidthConcatenateLayerKernel.h"
Georgios Pinitasae54e022018-07-16 15:41:27 +010029
Pablo Tello3dd5b682019-03-04 14:14:02 +000030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
31#include "arm_compute/runtime/NEON/NEScheduler.h"
32
Georgios Pinitasae54e022018-07-16 15:41:27 +010033#include "arm_compute/core/Error.h"
34#include "arm_compute/core/ITensor.h"
35#include "arm_compute/core/TensorInfo.h"
36#include "arm_compute/core/Types.h"
37#include "support/ToolchainSupport.h"
38
39namespace arm_compute
40{
41NEConcatenateLayer::NEConcatenateLayer()
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010042 : _concat_kernels(),
Pablo Tello3dd5b682019-03-04 14:14:02 +000043 _num_inputs(0),
44 _axis(Window::DimX)
Georgios Pinitasae54e022018-07-16 15:41:27 +010045{
46}
47
Georgios Pinitas09f24972019-05-17 18:14:40 +010048void NEConcatenateLayer::configure(std::vector<ITensor *> inputs_vector, ITensor *output, size_t axis)
49{
50 configure_internal(std::move(inputs_vector), output, axis);
51}
52
53void NEConcatenateLayer::configure(std::vector<const ITensor *> inputs_vector, ITensor *output, size_t axis)
54{
55 configure_internal(std::move(inputs_vector), output, axis);
56}
57
58Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
59{
60 return validate_internal(inputs_vector, output, axis);
61}
62
63Status NEConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
64{
65 return validate_internal(inputs_vector, output, axis);
66}
67
68template <typename TensorType, typename>
69void NEConcatenateLayer::configure_internal(std::vector<TensorType *> &&inputs_vector, ITensor *output, size_t axis)
Pablo Tello3dd5b682019-03-04 14:14:02 +000070{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010071 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010072 _axis = axis;
Pablo Tello3dd5b682019-03-04 14:14:02 +000073 _num_inputs = inputs_vector.size();
74
75 std::vector<ITensorInfo *> inputs_vector_info;
Michalis Spyroubcfd09a2019-05-01 13:03:59 +010076 inputs_vector_info.reserve(_num_inputs);
Pablo Tello3dd5b682019-03-04 14:14:02 +000077 for(unsigned int i = 0; i < _num_inputs; ++i)
78 {
79 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
80 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
81 }
Michalis Spyroua9c44722019-04-05 17:18:36 +010082 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +000083
84 // Output auto inizialitation if not yet initialized
85 auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type());
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010086 ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector_info, output->info(), axis));
Pablo Tello3dd5b682019-03-04 14:14:02 +000087
88 unsigned int offset = 0;
89
Pablo Tello3dd5b682019-03-04 14:14:02 +000090 for(unsigned int i = 0; i < _num_inputs; ++i)
91 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010092 switch(_axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +010093 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010094 case Window::DimX:
95 {
96 auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>();
97 kernel->configure(inputs_vector.at(i), offset, output);
98 _concat_kernels.emplace_back(std::move(kernel));
99 break;
100 }
101 case Window::DimY:
102 {
103 auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>();
104 kernel->configure(inputs_vector.at(i), offset, output);
105 _concat_kernels.emplace_back(std::move(kernel));
106 break;
107 }
108 case Window::DimZ:
109 {
110 auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>();
111 kernel->configure(inputs_vector.at(i), offset, output);
112 _concat_kernels.emplace_back(std::move(kernel));
113 break;
114 }
115 default:
116 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitasae54e022018-07-16 15:41:27 +0100117 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100118 offset += inputs_vector.at(i)->info()->dimension(_axis);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100119 }
120}
121
Georgios Pinitas09f24972019-05-17 18:14:40 +0100122template <typename TensorInfoType, typename>
123Status NEConcatenateLayer::validate_internal(const std::vector<TensorInfoType *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100124{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100125 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
126 ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100127
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100128 unsigned int offset = 0;
129 for(const auto &input : inputs_vector)
130 {
131 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100132 switch(axis)
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100133 {
134 case Window::DimX:
135 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100136 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100137 break;
138 }
139 case Window::DimY:
140 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100141 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100142 break;
143 }
144 case Window::DimZ:
145 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100146 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100147 break;
148 }
149 default:
150 ARM_COMPUTE_ERROR("Axis not supported");
151 }
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100152 offset += input->dimension(axis);
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100153 }
154
Michalis Spyroua9c44722019-04-05 17:18:36 +0100155 if(output->total_size() != 0)
156 {
157 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
158 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
159 }
160
Georgios Pinitasae54e022018-07-16 15:41:27 +0100161 return Status{};
162}
163
164void NEConcatenateLayer::run()
165{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100166 for(auto &kernel : _concat_kernels)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000167 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100168 NEScheduler::get().schedule(kernel.get(), _axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +0000169 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100170}
171} // namespace arm_compute