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 | |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/NEON/kernels/NEBatchConcatenateLayerKernel.h" |
Georgios Pinitas | 09f2497 | 2019-05-17 18:14:40 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/NEON/kernels/NEDepthConcatenateLayerKernel.h" |
| 28 | #include "arm_compute/core/NEON/kernels/NEHeightConcatenateLayerKernel.h" |
| 29 | #include "arm_compute/core/NEON/kernels/NEWidthConcatenateLayerKernel.h" |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 30 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 32 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 33 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 34 | #include "arm_compute/core/Error.h" |
| 35 | #include "arm_compute/core/ITensor.h" |
| 36 | #include "arm_compute/core/TensorInfo.h" |
| 37 | #include "arm_compute/core/Types.h" |
| 38 | #include "support/ToolchainSupport.h" |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | NEConcatenateLayer::NEConcatenateLayer() |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 43 | : _concat_kernels(), |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 44 | _num_inputs(0), |
| 45 | _axis(Window::DimX) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Georgios Pinitas | 09f2497 | 2019-05-17 18:14:40 +0100 | [diff] [blame] | 49 | void NEConcatenateLayer::configure(std::vector<ITensor *> inputs_vector, ITensor *output, size_t axis) |
| 50 | { |
| 51 | configure_internal(std::move(inputs_vector), output, axis); |
| 52 | } |
| 53 | |
| 54 | void NEConcatenateLayer::configure(std::vector<const ITensor *> inputs_vector, ITensor *output, size_t axis) |
| 55 | { |
| 56 | configure_internal(std::move(inputs_vector), output, axis); |
| 57 | } |
| 58 | |
| 59 | Status NEConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
| 60 | { |
| 61 | return validate_internal(inputs_vector, output, axis); |
| 62 | } |
| 63 | |
| 64 | Status NEConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
| 65 | { |
| 66 | return validate_internal(inputs_vector, output, axis); |
| 67 | } |
| 68 | |
| 69 | template <typename TensorType, typename> |
| 70 | void NEConcatenateLayer::configure_internal(std::vector<TensorType *> &&inputs_vector, ITensor *output, size_t axis) |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 71 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 72 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 73 | _axis = axis; |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 74 | _num_inputs = inputs_vector.size(); |
| 75 | |
| 76 | std::vector<ITensorInfo *> inputs_vector_info; |
Michalis Spyrou | bcfd09a | 2019-05-01 13:03:59 +0100 | [diff] [blame] | 77 | inputs_vector_info.reserve(_num_inputs); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 78 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 79 | { |
| 80 | ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i)); |
| 81 | inputs_vector_info.emplace_back(inputs_vector.at(i)->info()); |
| 82 | } |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 83 | 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] | 84 | |
| 85 | // Output auto inizialitation if not yet initialized |
| 86 | 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] | 87 | 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] | 88 | |
| 89 | unsigned int offset = 0; |
| 90 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 91 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 92 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 93 | switch(_axis) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 94 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 95 | case Window::DimX: |
| 96 | { |
| 97 | auto kernel = support::cpp14::make_unique<NEWidthConcatenateLayerKernel>(); |
| 98 | kernel->configure(inputs_vector.at(i), offset, output); |
| 99 | _concat_kernels.emplace_back(std::move(kernel)); |
| 100 | break; |
| 101 | } |
| 102 | case Window::DimY: |
| 103 | { |
| 104 | auto kernel = support::cpp14::make_unique<NEHeightConcatenateLayerKernel>(); |
| 105 | kernel->configure(inputs_vector.at(i), offset, output); |
| 106 | _concat_kernels.emplace_back(std::move(kernel)); |
| 107 | break; |
| 108 | } |
| 109 | case Window::DimZ: |
| 110 | { |
| 111 | auto kernel = support::cpp14::make_unique<NEDepthConcatenateLayerKernel>(); |
| 112 | kernel->configure(inputs_vector.at(i), offset, output); |
| 113 | _concat_kernels.emplace_back(std::move(kernel)); |
| 114 | break; |
| 115 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 116 | case 3: |
| 117 | { |
| 118 | auto kernel = support::cpp14::make_unique<NEBatchConcatenateLayerKernel>(); |
| 119 | kernel->configure(inputs_vector.at(i), offset, output); |
| 120 | _concat_kernels.emplace_back(std::move(kernel)); |
| 121 | break; |
| 122 | } |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 123 | default: |
| 124 | ARM_COMPUTE_ERROR("Axis not supported"); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 125 | } |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 126 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Georgios Pinitas | 09f2497 | 2019-05-17 18:14:40 +0100 | [diff] [blame] | 130 | template <typename TensorInfoType, typename> |
| 131 | Status NEConcatenateLayer::validate_internal(const std::vector<TensorInfoType *> &inputs_vector, const ITensorInfo *output, size_t axis) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 132 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 133 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 134 | ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 135 | |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 136 | unsigned int offset = 0; |
| 137 | for(const auto &input : inputs_vector) |
| 138 | { |
| 139 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 140 | switch(axis) |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 141 | { |
| 142 | case Window::DimX: |
| 143 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 144 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | case Window::DimY: |
| 148 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 149 | ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 150 | break; |
| 151 | } |
| 152 | case Window::DimZ: |
| 153 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 154 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 155 | break; |
| 156 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 157 | case 3: |
| 158 | { |
| 159 | ARM_COMPUTE_RETURN_ON_ERROR(NEBatchConcatenateLayerKernel::validate(input, offset, output)); |
| 160 | break; |
| 161 | } |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 162 | default: |
| 163 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 164 | } |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 165 | offset += input->dimension(axis); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 168 | if(output->total_size() != 0) |
| 169 | { |
| 170 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis); |
| 171 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 172 | } |
| 173 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 174 | return Status{}; |
| 175 | } |
| 176 | |
| 177 | void NEConcatenateLayer::run() |
| 178 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 179 | for(auto &kernel : _concat_kernels) |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 180 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 181 | NEScheduler::get().schedule(kernel.get(), _axis); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 182 | } |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 183 | } |
| 184 | } // namespace arm_compute |