blob: b8cfa2b8f2dd23d0112da52a169445c5d6dda6c0 [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
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010047void NEConcatenateLayer::configure(const std::vector<ITensor *> &inputs_vector, ITensor *output, size_t axis)
Pablo Tello3dd5b682019-03-04 14:14:02 +000048{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010049 ARM_COMPUTE_ERROR_ON(output == nullptr);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010050 _axis = 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 Spyroua9c44722019-04-05 17:18:36 +010059 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +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 Spyrou9f15c5e2019-04-03 19:48:54 +010063 ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector_info, output->info(), axis));
Pablo Tello3dd5b682019-03-04 14:14:02 +000064
65 unsigned int offset = 0;
66
Pablo Tello3dd5b682019-03-04 14:14:02 +000067 for(unsigned int i = 0; i < _num_inputs; ++i)
68 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010069 switch(_axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +010070 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010071 case Window::DimX:
72 {
73 auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>();
74 kernel->configure(inputs_vector.at(i), offset, output);
75 _concat_kernels.emplace_back(std::move(kernel));
76 break;
77 }
78 case Window::DimY:
79 {
80 auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>();
81 kernel->configure(inputs_vector.at(i), offset, output);
82 _concat_kernels.emplace_back(std::move(kernel));
83 break;
84 }
85 case Window::DimZ:
86 {
87 auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>();
88 kernel->configure(inputs_vector.at(i), offset, output);
89 _concat_kernels.emplace_back(std::move(kernel));
90 break;
91 }
92 default:
93 ARM_COMPUTE_ERROR("Axis not supported");
Georgios Pinitasae54e022018-07-16 15:41:27 +010094 }
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +010095 offset += inputs_vector.at(i)->info()->dimension(_axis);
Georgios Pinitasae54e022018-07-16 15:41:27 +010096 }
97}
98
Georgios Pinitas9e4824c2019-04-12 13:15:58 +010099Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis)
Georgios Pinitasae54e022018-07-16 15:41:27 +0100100{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100101 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output);
102 ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2);
Georgios Pinitasae54e022018-07-16 15:41:27 +0100103
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100104 unsigned int offset = 0;
105 for(const auto &input : inputs_vector)
106 {
107 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input);
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100108 switch(axis)
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100109 {
110 case Window::DimX:
111 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100112 ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100113 break;
114 }
115 case Window::DimY:
116 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100117 ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100118 break;
119 }
120 case Window::DimZ:
121 {
Michalis Spyroua9c44722019-04-05 17:18:36 +0100122 ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output));
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100123 break;
124 }
125 default:
126 ARM_COMPUTE_ERROR("Axis not supported");
127 }
Georgios Pinitas9e4824c2019-04-12 13:15:58 +0100128 offset += input->dimension(axis);
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100129 }
130
Michalis Spyroua9c44722019-04-05 17:18:36 +0100131 if(output->total_size() != 0)
132 {
133 TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis);
134 ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
135 }
136
Georgios Pinitasae54e022018-07-16 15:41:27 +0100137 return Status{};
138}
139
140void NEConcatenateLayer::run()
141{
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100142 for(auto &kernel : _concat_kernels)
Pablo Tello3dd5b682019-03-04 14:14:02 +0000143 {
Michalis Spyrou9f15c5e2019-04-03 19:48:54 +0100144 NEScheduler::get().schedule(kernel.get(), _axis);
Pablo Tello3dd5b682019-03-04 14:14:02 +0000145 }
Georgios Pinitasae54e022018-07-16 15:41:27 +0100146}
147} // namespace arm_compute