Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 3 | * |
| 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 Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 30 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 31 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 32 | #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 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | NEConcatenateLayer::NEConcatenateLayer() |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 41 | : _concat_kernels(), |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 42 | _num_inputs(0), |
| 43 | _axis(Window::DimX) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 47 | void NEConcatenateLayer::configure(const std::vector<ITensor *> &inputs_vector, ITensor *output, size_t axis) |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 48 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 49 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 50 | _axis = axis; |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 51 | _num_inputs = inputs_vector.size(); |
| 52 | |
| 53 | std::vector<ITensorInfo *> inputs_vector_info; |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 54 | inputs_vector_info.reserve(_num_inputs); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 55 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 56 | { |
| 57 | ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i)); |
| 58 | inputs_vector_info.emplace_back(inputs_vector.at(i)->info()); |
| 59 | } |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 60 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, _axis); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 61 | |
| 62 | // Output auto inizialitation if not yet initialized |
| 63 | auto_init_if_empty(*output->info(), output_shape, 1, inputs_vector[0]->info()->data_type()); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector_info, output->info(), axis)); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 65 | |
| 66 | unsigned int offset = 0; |
| 67 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 68 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 69 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 70 | switch(_axis) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 71 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 72 | case Window::DimX: |
| 73 | { |
| 74 | auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>(); |
| 75 | kernel->configure(inputs_vector.at(i), offset, output); |
| 76 | _concat_kernels.emplace_back(std::move(kernel)); |
| 77 | break; |
| 78 | } |
| 79 | case Window::DimY: |
| 80 | { |
| 81 | auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>(); |
| 82 | kernel->configure(inputs_vector.at(i), offset, output); |
| 83 | _concat_kernels.emplace_back(std::move(kernel)); |
| 84 | break; |
| 85 | } |
| 86 | case Window::DimZ: |
| 87 | { |
| 88 | auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>(); |
| 89 | kernel->configure(inputs_vector.at(i), offset, output); |
| 90 | _concat_kernels.emplace_back(std::move(kernel)); |
| 91 | break; |
| 92 | } |
| 93 | default: |
| 94 | ARM_COMPUTE_ERROR("Axis not supported"); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 95 | } |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 96 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 100 | Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 101 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 102 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 104 | |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 105 | unsigned int offset = 0; |
| 106 | for(const auto &input : inputs_vector) |
| 107 | { |
| 108 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 109 | switch(axis) |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 110 | { |
| 111 | case Window::DimX: |
| 112 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 113 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | case Window::DimY: |
| 117 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 118 | ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | case Window::DimZ: |
| 122 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 123 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 124 | break; |
| 125 | } |
| 126 | default: |
| 127 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 128 | } |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 129 | offset += input->dimension(axis); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 130 | } |
| 131 | |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 132 | if(output->total_size() != 0) |
| 133 | { |
| 134 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis); |
| 135 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 136 | } |
| 137 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 138 | return Status{}; |
| 139 | } |
| 140 | |
| 141 | void NEConcatenateLayer::run() |
| 142 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 143 | for(auto &kernel : _concat_kernels) |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 144 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 145 | NEScheduler::get().schedule(kernel.get(), _axis); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 146 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 147 | } |
| 148 | } // namespace arm_compute |