blob: fa7b91c3caf34d39ae3a75bbabd4aee08e66bd9b [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()
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010041 : _concat_kernels(),
Pablo Tello3dd5b682019-03-04 14:14:02 +000042 _num_inputs(0),
43 _axis(Window::DimX)
Georgios Pinitasae54e022018-07-16 15:41:27 +010044{
45}
46
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010047void NEConcatenateLayer::configure(const std::vector<ITensor *> &inputs_vector, ITensor *output, DataLayoutDimension axis)
Pablo Tello3dd5b682019-03-04 14:14:02 +000048{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010049 ARM_COMPUTE_ERROR_ON(output == nullptr);
50 _axis = get_data_layout_dimension_index(output->info()->data_layout(), axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +000051 _num_inputs = inputs_vector.size();
52
53 std::vector<ITensorInfo *> inputs_vector_info;
54 for(unsigned int i = 0; i < _num_inputs; ++i)
55 {
56 ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i));
57 inputs_vector_info.emplace_back(inputs_vector.at(i)->info());
58 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +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 Tello3dd5b682019-03-04 14:14:02 +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 Spyrou9f15c5e2019-04-03 19:48:54 +010071 ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector_info, output->info(), axis));
Pablo Tello3dd5b682019-03-04 14:14:02 +000072
73 unsigned int offset = 0;
74
Pablo Tello3dd5b682019-03-04 14:14:02 +000075 for(unsigned int i = 0; i < _num_inputs; ++i)
76 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010077 switch(_axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +010078 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010079 case Window::DimX:
80 {
81 auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>();
82 kernel->configure(inputs_vector.at(i), offset, output);
83 _concat_kernels.emplace_back(std::move(kernel));
84 break;
85 }
86 case Window::DimY:
87 {
88 auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>();
89 kernel->configure(inputs_vector.at(i), offset, output);
90 _concat_kernels.emplace_back(std::move(kernel));
91 break;
92 }
93 case Window::DimZ:
94 {
95 auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>();
96 kernel->configure(inputs_vector.at(i), offset, output);
97 _concat_kernels.emplace_back(std::move(kernel));
98 break;
99 }
100 default:
101 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitasae54e022018-07-16 15:41:27 +0100102 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100103 offset += inputs_vector.at(i)->info()->dimension(_axis);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100104 }
105}
106
107Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, DataLayoutDimension axis)
108{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100109 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
110 ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
111 const unsigned int _axis = get_data_layout_dimension_index(inputs_vector[0]->data_layout(), axis);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100112
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100113 // Output auto inizialitation if not yet initialized
114 TensorInfo tmp_output_info = *output->clone();
115 TensorShape output_shape{};
116 if(_axis == Window::DimZ)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100117 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100118 output_shape = arm_compute::misc::shape_calculator::calculate_depth_concatenate_shape(inputs_vector);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100119 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100120 else
121 {
122 output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
123 }
124 auto_init_if_empty(tmp_output_info, output_shape, 1, inputs_vector[0]->data_type());
125
126 unsigned int offset = 0;
127 for(const auto &input : inputs_vector)
128 {
129 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
130 switch(_axis)
131 {
132 case Window::DimX:
133 {
134 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
135 break;
136 }
137 case Window::DimY:
138 {
139 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
140 break;
141 }
142 case Window::DimZ:
143 {
144 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, &tmp_output_info));
145 break;
146 }
147 default:
148 ARM_COMPUTE_ERROR("Axis not supported");
149 }
150 offset += input->dimension(_axis);
151 }
152
Georgios Pinitasae54e022018-07-16 15:41:27 +0100153 return Status{};
154}
155
156void NEConcatenateLayer::run()
157{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100158 for(auto &kernel : _concat_kernels)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000159 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100160 NEScheduler::get().schedule(kernel.get(), _axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +0000161 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100162}
163} // namespace arm_compute