Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2020 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 | |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 26 | #include "src/core/NEON/kernels/NEBatchConcatenateLayerKernel.h" |
| 27 | #include "src/core/NEON/kernels/NEDepthConcatenateLayerKernel.h" |
| 28 | #include "src/core/NEON/kernels/NEHeightConcatenateLayerKernel.h" |
| 29 | #include "src/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" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 38 | #include "src/core/helpers/AutoConfiguration.h" |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 42 | namespace experimental |
| 43 | { |
Georgios Pinitas | 09cad72 | 2020-07-22 12:11:20 +0100 | [diff] [blame] | 44 | NEConcatenation::NEConcatenation() |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 45 | : _concat_kernels(), _num_inputs(0), _axis(0) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 46 | { |
| 47 | } |
| 48 | |
Georgios Pinitas | 09cad72 | 2020-07-22 12:11:20 +0100 | [diff] [blame] | 49 | void NEConcatenation::configure(const std::vector<const ITensorInfo *> &inputs_vector, ITensorInfo *output, size_t axis) |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 50 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 52 | |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 53 | _axis = axis; |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 54 | _num_inputs = inputs_vector.size(); |
| 55 | |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 56 | 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] | 57 | |
| 58 | // Output auto inizialitation if not yet initialized |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 59 | auto_init_if_empty(*output, output_shape, 1, inputs_vector[0]->data_type()); |
| 60 | ARM_COMPUTE_ERROR_THROW_ON(NEConcatenateLayer::validate(inputs_vector, output, axis)); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 61 | |
| 62 | unsigned int offset = 0; |
| 63 | |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 64 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 65 | { |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 66 | switch(axis) |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 67 | { |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 68 | case Window::DimX: |
| 69 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame^] | 70 | auto kernel = std::make_unique<NEWidthConcatenateLayerKernel>(); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 71 | kernel->configure(inputs_vector.at(i), offset, output); |
| 72 | _concat_kernels.emplace_back(std::move(kernel)); |
| 73 | break; |
| 74 | } |
| 75 | case Window::DimY: |
| 76 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame^] | 77 | auto kernel = std::make_unique<NEHeightConcatenateLayerKernel>(); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 78 | kernel->configure(inputs_vector.at(i), offset, output); |
| 79 | _concat_kernels.emplace_back(std::move(kernel)); |
| 80 | break; |
| 81 | } |
| 82 | case Window::DimZ: |
| 83 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame^] | 84 | auto kernel = std::make_unique<NEDepthConcatenateLayerKernel>(); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 85 | kernel->configure(inputs_vector.at(i), offset, output); |
| 86 | _concat_kernels.emplace_back(std::move(kernel)); |
| 87 | break; |
| 88 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 89 | case 3: |
| 90 | { |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame^] | 91 | auto kernel = std::make_unique<NEBatchConcatenateLayerKernel>(); |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 92 | kernel->configure(inputs_vector.at(i), offset, output); |
| 93 | _concat_kernels.emplace_back(std::move(kernel)); |
| 94 | break; |
| 95 | } |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 96 | default: |
| 97 | ARM_COMPUTE_ERROR("Axis not supported"); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 98 | } |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 99 | offset += inputs_vector.at(i)->dimension(axis); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
Georgios Pinitas | 09cad72 | 2020-07-22 12:11:20 +0100 | [diff] [blame] | 103 | Status NEConcatenation::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
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 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 106 | ARM_COMPUTE_RETURN_ERROR_ON(inputs_vector.size() < 2); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 107 | |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 108 | unsigned int offset = 0; |
| 109 | for(const auto &input : inputs_vector) |
| 110 | { |
| 111 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 112 | switch(axis) |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 113 | { |
| 114 | case Window::DimX: |
| 115 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 116 | ARM_COMPUTE_RETURN_ON_ERROR(NEWidthConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | case Window::DimY: |
| 120 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 121 | ARM_COMPUTE_RETURN_ON_ERROR(NEHeightConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | case Window::DimZ: |
| 125 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_RETURN_ON_ERROR(NEDepthConcatenateLayerKernel::validate(input, offset, output)); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 127 | break; |
| 128 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 129 | case 3: |
| 130 | { |
| 131 | ARM_COMPUTE_RETURN_ON_ERROR(NEBatchConcatenateLayerKernel::validate(input, offset, output)); |
| 132 | break; |
| 133 | } |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 134 | default: |
| 135 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 136 | } |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 137 | offset += input->dimension(axis); |
Michalis Spyrou | 9f15c5e | 2019-04-03 19:48:54 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 140 | if(output->total_size() != 0) |
| 141 | { |
| 142 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis); |
| 143 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 144 | } |
| 145 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 146 | return Status{}; |
| 147 | } |
| 148 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 149 | void NEConcatenation::run(ITensorPack &tensors) |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 150 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 151 | if(tensors.empty()) |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 152 | { |
| 153 | ARM_COMPUTE_ERROR("No inputs provided"); |
| 154 | } |
| 155 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 156 | if(static_cast<int>(tensors.size() - 1) != static_cast<int>(_num_inputs)) |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 157 | { |
| 158 | ARM_COMPUTE_ERROR("Configured with different number of inputs"); |
| 159 | } |
| 160 | |
| 161 | int i = 0; |
| 162 | for(auto &k : _concat_kernels) |
| 163 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 164 | ITensorPack pack; |
| 165 | pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i)); |
| 166 | pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST)); |
| 167 | NEScheduler::get().schedule_op(k.get(), Window::DimY, pack); |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 168 | ++i; |
| 169 | } |
| 170 | } |
| 171 | } // namespace experimental |
| 172 | |
| 173 | struct NEConcatenateLayer::Impl |
| 174 | { |
Georgios Pinitas | 09cad72 | 2020-07-22 12:11:20 +0100 | [diff] [blame] | 175 | std::vector<const ITensor *> srcs{}; |
| 176 | ITensor *dst{ nullptr }; |
| 177 | unsigned int num_inputs{ 0 }; |
| 178 | unsigned int axis{ 0 }; |
| 179 | std::unique_ptr<experimental::NEConcatenation> op{ nullptr }; |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 180 | }; |
| 181 | |
| 182 | NEConcatenateLayer::NEConcatenateLayer() |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame^] | 183 | : _impl(std::make_unique<Impl>()) |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 184 | { |
| 185 | } |
| 186 | |
| 187 | NEConcatenateLayer::NEConcatenateLayer(NEConcatenateLayer &&) = default; |
| 188 | |
| 189 | NEConcatenateLayer &NEConcatenateLayer::operator=(NEConcatenateLayer &&) = default; |
| 190 | |
| 191 | NEConcatenateLayer::~NEConcatenateLayer() = default; |
| 192 | |
| 193 | void NEConcatenateLayer::configure(std::vector<const ITensor *> inputs_vector, ITensor *output, size_t axis) |
| 194 | { |
| 195 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
| 196 | |
| 197 | _impl->srcs = inputs_vector; |
| 198 | _impl->dst = output; |
| 199 | _impl->axis = axis; |
| 200 | _impl->num_inputs = inputs_vector.size(); |
Georgios Pinitas | 40f51a6 | 2020-11-21 03:04:18 +0000 | [diff] [blame^] | 201 | _impl->op = std::make_unique<experimental::NEConcatenation>(); |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 202 | |
| 203 | std::vector<const ITensorInfo *> inputs_vector_info; |
| 204 | for(unsigned int i = 0; i < inputs_vector.size(); ++i) |
| 205 | { |
| 206 | ARM_COMPUTE_ERROR_ON_NULLPTR(inputs_vector.at(i)); |
| 207 | inputs_vector_info.emplace_back(inputs_vector.at(i)->info()); |
| 208 | } |
| 209 | _impl->op->configure(inputs_vector_info, _impl->dst->info(), axis); |
| 210 | } |
| 211 | |
| 212 | Status NEConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
| 213 | { |
Georgios Pinitas | 09cad72 | 2020-07-22 12:11:20 +0100 | [diff] [blame] | 214 | return experimental::NEConcatenation::validate(inputs_vector, output, axis); |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 215 | } |
| 216 | |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 217 | void NEConcatenateLayer::run() |
| 218 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 219 | ITensorPack pack; |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 220 | for(unsigned i = 0; i < _impl->num_inputs; ++i) |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 221 | { |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 222 | pack.add_tensor(TensorType::ACL_SRC_VEC + i, _impl->srcs.at(i)); |
Pablo Tello | 3dd5b68 | 2019-03-04 14:14:02 +0000 | [diff] [blame] | 223 | } |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 224 | pack.add_tensor(TensorType::ACL_DST, _impl->dst); |
Georgios Pinitas | 4667ddd | 2020-07-13 21:21:33 +0100 | [diff] [blame] | 225 | |
Georgios Pinitas | 0499dff | 2020-07-31 22:21:38 +0100 | [diff] [blame] | 226 | _impl->op->run(pack); |
Georgios Pinitas | ae54e02 | 2018-07-16 15:41:27 +0100 | [diff] [blame] | 227 | } |
| 228 | } // namespace arm_compute |