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