Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 1 | /* |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +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/CL/functions/CLConcatenateLayer.h" |
| 25 | |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/kernels/CLBatchConcatenateLayerKernel.h" |
Georgios Pinitas | 09f2497 | 2019-05-17 18:14:40 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/kernels/CLDepthConcatenateLayerKernel.h" |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/CL/kernels/CLHeightConcatenateLayerKernel.h" |
Georgios Pinitas | 09f2497 | 2019-05-17 18:14:40 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/kernels/CLWidthConcatenate2TensorsKernel.h" |
| 30 | #include "arm_compute/core/CL/kernels/CLWidthConcatenate4TensorsKernel.h" |
| 31 | #include "arm_compute/core/CL/kernels/CLWidthConcatenateLayerKernel.h" |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 33 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 34 | |
| 35 | #include "arm_compute/core/CL/ICLTensor.h" |
| 36 | #include "arm_compute/core/Error.h" |
| 37 | #include "arm_compute/core/TensorInfo.h" |
| 38 | #include "arm_compute/core/Types.h" |
| 39 | #include "support/ToolchainSupport.h" |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | CLConcatenateLayer::CLConcatenateLayer() |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 44 | : _concat_kernels(), |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 45 | _num_inputs(0), |
| 46 | _axis(Window::DimX) |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 47 | { |
| 48 | } |
| 49 | |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 50 | void CLConcatenateLayer::configure(std::vector<ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis) |
| 51 | { |
| 52 | configure_internal(std::move(inputs_vector), output, axis); |
| 53 | } |
| 54 | |
| 55 | void CLConcatenateLayer::configure(std::vector<const ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis) |
| 56 | { |
| 57 | configure_internal(std::move(inputs_vector), output, axis); |
| 58 | } |
| 59 | |
| 60 | Status CLConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
| 61 | { |
| 62 | return validate_internal(inputs_vector, output, axis); |
| 63 | } |
| 64 | |
| 65 | Status CLConcatenateLayer::validate(const std::vector<const ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
| 66 | { |
| 67 | return validate_internal(inputs_vector, output, axis); |
| 68 | } |
| 69 | |
| 70 | template <typename TensorType> |
| 71 | void CLConcatenateLayer::configure_internal(std::vector<TensorType *> &&inputs_vector, ICLTensor *output, size_t axis) |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 72 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 73 | ARM_COMPUTE_ERROR_ON(output == nullptr); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 74 | _axis = axis; |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 75 | _num_inputs = inputs_vector.size(); |
| 76 | |
| 77 | std::vector<ITensorInfo *> inputs_vector_info(inputs_vector.size()); |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 78 | std::transform(inputs_vector.begin(), inputs_vector.end(), inputs_vector_info.begin(), [](TensorType * t) |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 79 | { |
| 80 | ARM_COMPUTE_ERROR_ON_NULLPTR(t); |
| 81 | return t->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 | 6a14adb | 2019-03-05 17:33:08 +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 | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 87 | ARM_COMPUTE_ERROR_THROW_ON(CLConcatenateLayer::validate(inputs_vector_info, output->info(), axis)); |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 88 | |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 89 | unsigned int offset = 0; |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 90 | switch(_axis) |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 91 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 92 | case Window::DimX: |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 93 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 94 | switch(_num_inputs) |
| 95 | { |
| 96 | case 2: |
| 97 | { |
| 98 | // Configure WidthConcatenate2Tensors kernel |
| 99 | auto kernel = support::cpp14::make_unique<CLWidthConcatenate2TensorsKernel>(); |
| 100 | kernel->configure(inputs_vector.at(0), inputs_vector.at(1), output); |
| 101 | _concat_kernels.emplace_back(std::move(kernel)); |
| 102 | break; |
| 103 | } |
| 104 | case 4: |
| 105 | { |
| 106 | // Configure WidthConcatenate4Tensors kernel |
| 107 | auto kernel = support::cpp14::make_unique<CLWidthConcatenate4TensorsKernel>(); |
| 108 | kernel->configure(inputs_vector.at(0), inputs_vector.at(1), inputs_vector.at(2), inputs_vector.at(3), output); |
| 109 | _concat_kernels.emplace_back(std::move(kernel)); |
| 110 | break; |
| 111 | } |
| 112 | default: |
| 113 | { |
| 114 | // Configure generic case WidthConcatenate kernels |
| 115 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 116 | { |
| 117 | auto kernel = support::cpp14::make_unique<CLWidthConcatenateLayerKernel>(); |
| 118 | kernel->configure(inputs_vector.at(i), offset, output); |
| 119 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 120 | _concat_kernels.emplace_back(std::move(kernel)); |
| 121 | } |
| 122 | break; |
| 123 | } |
| 124 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 125 | break; |
| 126 | } |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 127 | case Window::DimY: |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 128 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 129 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 130 | { |
| 131 | auto kernel = support::cpp14::make_unique<CLHeightConcatenateLayerKernel>(); |
| 132 | kernel->configure(inputs_vector.at(i), offset, output); |
| 133 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 134 | _concat_kernels.emplace_back(std::move(kernel)); |
| 135 | } |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 136 | break; |
| 137 | } |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 138 | case Window::DimZ: |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 139 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 140 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 141 | { |
| 142 | auto kernel = support::cpp14::make_unique<CLDepthConcatenateLayerKernel>(); |
| 143 | kernel->configure(inputs_vector.at(i), offset, output); |
| 144 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 145 | _concat_kernels.emplace_back(std::move(kernel)); |
| 146 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 147 | break; |
| 148 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 149 | case 3: |
| 150 | { |
| 151 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 152 | { |
| 153 | auto kernel = support::cpp14::make_unique<CLBatchConcatenateLayerKernel>(); |
| 154 | kernel->configure(inputs_vector.at(i), offset, output); |
| 155 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 156 | _concat_kernels.emplace_back(std::move(kernel)); |
| 157 | } |
| 158 | break; |
| 159 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 160 | default: |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 161 | ARM_COMPUTE_ERROR("Axis not supported"); |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Manuel Bottini | 10c53f1 | 2019-07-17 16:11:53 +0100 | [diff] [blame] | 165 | template <typename TensorInfoType> |
| 166 | Status CLConcatenateLayer::validate_internal(const std::vector<TensorInfoType *> &inputs_vector, const ITensorInfo *output, size_t axis) |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 167 | { |
| 168 | ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 169 | const unsigned int num_inputs = inputs_vector.size(); |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 170 | |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 171 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 172 | ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 173 | |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 174 | unsigned int offset = 0; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 175 | switch(axis) |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 176 | { |
| 177 | case Window::DimX: |
| 178 | { |
| 179 | switch(num_inputs) |
| 180 | { |
| 181 | case 2: |
| 182 | // Validate WidthConcatenate2Tensors kernels if there are 2 inputs |
| 183 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]); |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 184 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate2TensorsKernel::validate(inputs_vector[0], inputs_vector[1], output)); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 185 | break; |
| 186 | case 4: |
| 187 | // Validate WidthConcatenate4Tensors kernels if there are 4 inputs |
| 188 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1], inputs_vector[2], inputs_vector[3]); |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 189 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenate4TensorsKernel::validate(inputs_vector[0], inputs_vector[1], inputs_vector[2], inputs_vector[3], output)); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 190 | break; |
| 191 | default: |
| 192 | // Validate generic case of WidthConcatenate kernel |
| 193 | for(const auto &input : inputs_vector) |
| 194 | { |
| 195 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 196 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::validate(input, offset, output)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 197 | offset += input->dimension(axis); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 198 | } |
| 199 | break; |
| 200 | } |
| 201 | break; |
| 202 | } |
| 203 | case Window::DimY: |
| 204 | { |
| 205 | for(const auto &input : inputs_vector) |
| 206 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 207 | ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, output)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 208 | offset += input->dimension(axis); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 209 | } |
| 210 | break; |
| 211 | } |
| 212 | case Window::DimZ: |
| 213 | { |
| 214 | for(const auto &input : inputs_vector) |
| 215 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 216 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, output)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 217 | offset += input->dimension(axis); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 218 | } |
| 219 | break; |
| 220 | } |
Vidhya Sudhan Loganathan | 338595b | 2019-06-28 14:09:53 +0100 | [diff] [blame] | 221 | case 3: |
| 222 | { |
| 223 | for(const auto &input : inputs_vector) |
| 224 | { |
| 225 | ARM_COMPUTE_RETURN_ON_ERROR(CLBatchConcatenateLayerKernel::validate(input, offset, output)); |
| 226 | offset += input->dimension(axis); |
| 227 | } |
| 228 | break; |
| 229 | } |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 230 | default: |
| 231 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 232 | } |
| 233 | |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 234 | if(output->total_size() != 0) |
| 235 | { |
| 236 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis); |
| 237 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 238 | } |
| 239 | |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 240 | return Status{}; |
| 241 | } |
| 242 | |
| 243 | void CLConcatenateLayer::run() |
| 244 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 245 | for(auto &kernel : _concat_kernels) |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 246 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 247 | CLScheduler::get().enqueue(*kernel, true); |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 248 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 249 | } |
| 250 | } // namespace arm_compute |