blob: f764a126a067b97b8e84f4a1f88f045f2350b495 [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
26#include "arm_compute/runtime/NEON/functions/NEDepthConcatenateLayer.h"
27#include "arm_compute/runtime/NEON/functions/NEWidthConcatenateLayer.h"
28
Pablo Tello3dd5b682019-03-04 14:14:02 +000029#include "arm_compute/core/utils/misc/ShapeCalculator.h"
30#include "arm_compute/runtime/NEON/NEScheduler.h"
31
Georgios Pinitasae54e022018-07-16 15:41:27 +010032#include "arm_compute/core/Error.h"
33#include "arm_compute/core/ITensor.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{
40NEConcatenateLayer::NEConcatenateLayer()
Pablo Tello3dd5b682019-03-04 14:14:02 +000041 : _concat_function(nullptr),
42 _hconcat_kernels(),
43 _num_inputs(0),
44 _axis(Window::DimX)
Georgios Pinitasae54e022018-07-16 15:41:27 +010045{
46}
47
Pablo Tello3dd5b682019-03-04 14:14:02 +000048Status NEConcatenateLayer::validate_h_concatenate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output)
49{
50 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
51 ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
52
53 // Output auto inizialitation if not yet initialized
54 TensorInfo tmp_output_info = *output->clone();
55 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, Window::DimY);
56 auto_init_if_empty(tmp_output_info, output_shape, 1, inputs_vector[0]->data_type());
57
58 unsigned int offset = 0;
59 for(const auto &input : inputs_vector)
60 {
61 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
62 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
63 offset += input->dimension(Window::DimY);
64 }
65
66 return Status{};
67}
68
69void NEConcatenateLayer::configure_h_concatenate(std::vector<ITensor *> inputs_vector, ITensor *output)
70{
71 _num_inputs = inputs_vector.size();
72
73 std::vector<ITensorInfo *> inputs_vector_info;
74 for(unsigned int i = 0; i < _num_inputs; ++i)
75 {
76 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
77 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
78 }
79 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, Window::DimY);
80
81 // Output auto inizialitation if not yet initialized
82 auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type());
83 ARM_COMPUTE_ERROR_THROW_ON(validate_h_concatenate(inputs_vector_info, output->info()));
84
85 unsigned int offset = 0;
86
87 _hconcat_kernels = arm_compute::support::cpp14::make_unique<NEHeightConcatenateLayerKernel[]>(_num_inputs);
88
89 for(unsigned int i = 0; i < _num_inputs; ++i)
90 {
91 _hconcat_kernels[i].configure(inputs_vector.at(i), offset, output);
92 offset += inputs_vector.at(i)->info()->dimension(Window::DimY);
93 }
94}
95
Georgios Pinitasae54e022018-07-16 15:41:27 +010096void NEConcatenateLayer::configure(const std::vector<ITensor *> &inputs_vector, ITensor *output, DataLayoutDimension axis)
97{
98 ARM_COMPUTE_ERROR_ON(output == nullptr);
Pablo Tello3dd5b682019-03-04 14:14:02 +000099 _axis = get_data_layout_dimension_index(output->info()->data_layout(), axis);
100 switch(_axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100101 {
102 case 0:
103 {
104 auto func = support::cpp14::make_unique<NEWidthConcatenateLayer>();
105 func->configure(inputs_vector, output);
106 _concat_function = std::move(func);
107 break;
108 }
Pablo Tello3dd5b682019-03-04 14:14:02 +0000109 case 1:
110 {
111 configure_h_concatenate(inputs_vector, output);
112 break;
113 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100114 case 2:
115 {
116 auto func = support::cpp14::make_unique<NEDepthConcatenateLayer>();
117 func->configure(inputs_vector, output);
118 _concat_function = std::move(func);
119 break;
120 }
121 default:
122 ARM_COMPUTE_ERROR("Concatenation is supported across width and depth only!");
123 }
124}
125
126Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, DataLayoutDimension axis)
127{
128 ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr);
129
130 switch(get_data_layout_dimension_index(output->data_layout(), axis))
131 {
132 case 0:
133 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayer::validate(inputs_vector, output));
134 break;
Pablo Tello3dd5b682019-03-04 14:14:02 +0000135 case 1:
136 ARM_COMPUTE_RETURN_ON_ERROR(NEConcatenateLayer::validate_h_concatenate(inputs_vector, output));
137 break;
Georgios Pinitasae54e022018-07-16 15:41:27 +0100138 case 2:
139 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayer::validate(inputs_vector, output));
140 break;
141 default:
142 ARM_COMPUTE_RETURN_ERROR_MSG("Concatenation is supported across width and depth only!");
143 }
144 return Status{};
145}
146
147void NEConcatenateLayer::run()
148{
Pablo Tello3dd5b682019-03-04 14:14:02 +0000149 switch(_axis)
150 {
151 case 0:
152 case 2:
153 {
154 ARM_COMPUTE_ERROR_ON(_concat_function == nullptr);
155 _concat_function->run();
156 break;
157 }
158 case 1:
159 {
160 for(unsigned i = 0; i < _num_inputs; ++i)
161 {
162 NEScheduler::get().schedule(_hconcat_kernels.get() + i, Window::DimY);
163 }
164 break;
165 }
166 default:
167 {
168 ARM_COMPUTE_ERROR("Axis not supported.");
169 break;
170 }
171 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100172}
173} // namespace arm_compute