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 | |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/kernels/CLHeightConcatenateLayerKernel.h" |
| 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 28 | #include "arm_compute/runtime/CL/CLScheduler.h" |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 29 | #include "arm_compute/runtime/CL/functions/CLDepthConcatenateLayer.h" |
| 30 | #include "arm_compute/runtime/CL/functions/CLWidthConcatenateLayer.h" |
| 31 | |
| 32 | #include "arm_compute/core/CL/ICLTensor.h" |
| 33 | #include "arm_compute/core/Error.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 | CLConcatenateLayer::CLConcatenateLayer() |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 41 | : _concat_kernels(), |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 42 | _num_inputs(0), |
| 43 | _axis(Window::DimX) |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 47 | void CLConcatenateLayer::configure(const std::vector<ICLTensor *> &inputs_vector, ICLTensor *output, size_t axis) |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 48 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +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 | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 51 | _num_inputs = inputs_vector.size(); |
| 52 | |
| 53 | std::vector<ITensorInfo *> inputs_vector_info(inputs_vector.size()); |
| 54 | std::transform(inputs_vector.begin(), inputs_vector.end(), inputs_vector_info.begin(), [](ICLTensor * t) |
| 55 | { |
| 56 | ARM_COMPUTE_ERROR_ON_NULLPTR(t); |
| 57 | return t->info(); |
| 58 | }); |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 59 | 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] | 60 | |
| 61 | // Output auto inizialitation if not yet initialized |
| 62 | 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] | 63 | 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] | 64 | |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 65 | unsigned int offset = 0; |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 66 | switch(_axis) |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 67 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 68 | case Window::DimX: |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 69 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 70 | switch(_num_inputs) |
| 71 | { |
| 72 | case 2: |
| 73 | { |
| 74 | // Configure WidthConcatenate2Tensors kernel |
| 75 | auto kernel = support::cpp14::make_unique<CLWidthConcatenate2TensorsKernel>(); |
| 76 | kernel->configure(inputs_vector.at(0), inputs_vector.at(1), output); |
| 77 | _concat_kernels.emplace_back(std::move(kernel)); |
| 78 | break; |
| 79 | } |
| 80 | case 4: |
| 81 | { |
| 82 | // Configure WidthConcatenate4Tensors kernel |
| 83 | auto kernel = support::cpp14::make_unique<CLWidthConcatenate4TensorsKernel>(); |
| 84 | kernel->configure(inputs_vector.at(0), inputs_vector.at(1), inputs_vector.at(2), inputs_vector.at(3), output); |
| 85 | _concat_kernels.emplace_back(std::move(kernel)); |
| 86 | break; |
| 87 | } |
| 88 | default: |
| 89 | { |
| 90 | // Configure generic case WidthConcatenate kernels |
| 91 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 92 | { |
| 93 | auto kernel = support::cpp14::make_unique<CLWidthConcatenateLayerKernel>(); |
| 94 | kernel->configure(inputs_vector.at(i), offset, output); |
| 95 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 96 | _concat_kernels.emplace_back(std::move(kernel)); |
| 97 | } |
| 98 | break; |
| 99 | } |
| 100 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 101 | break; |
| 102 | } |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 103 | case Window::DimY: |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 104 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 105 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 106 | { |
| 107 | auto kernel = support::cpp14::make_unique<CLHeightConcatenateLayerKernel>(); |
| 108 | kernel->configure(inputs_vector.at(i), offset, output); |
| 109 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 110 | _concat_kernels.emplace_back(std::move(kernel)); |
| 111 | } |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 112 | break; |
| 113 | } |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 114 | case Window::DimZ: |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 115 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 116 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 117 | { |
| 118 | auto kernel = support::cpp14::make_unique<CLDepthConcatenateLayerKernel>(); |
| 119 | kernel->configure(inputs_vector.at(i), offset, output); |
| 120 | offset += inputs_vector.at(i)->info()->dimension(_axis); |
| 121 | _concat_kernels.emplace_back(std::move(kernel)); |
| 122 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 123 | break; |
| 124 | } |
| 125 | default: |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_ERROR("Axis not supported"); |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 130 | Status CLConcatenateLayer::validate(const std::vector<ITensorInfo *> &inputs_vector, const ITensorInfo *output, size_t axis) |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 131 | { |
| 132 | ARM_COMPUTE_RETURN_ERROR_ON(output == nullptr); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 133 | const unsigned int num_inputs = inputs_vector.size(); |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 134 | |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(output); |
| 136 | ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 137 | |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 138 | unsigned int offset = 0; |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 139 | switch(axis) |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 140 | { |
| 141 | case Window::DimX: |
| 142 | { |
| 143 | switch(num_inputs) |
| 144 | { |
| 145 | case 2: |
| 146 | // Validate WidthConcatenate2Tensors kernels if there are 2 inputs |
| 147 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(inputs_vector[0], inputs_vector[1]); |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 148 | 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] | 149 | break; |
| 150 | case 4: |
| 151 | // Validate WidthConcatenate4Tensors kernels if there are 4 inputs |
| 152 | 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] | 153 | 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] | 154 | break; |
| 155 | default: |
| 156 | // Validate generic case of WidthConcatenate kernel |
| 157 | for(const auto &input : inputs_vector) |
| 158 | { |
| 159 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input); |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 160 | ARM_COMPUTE_RETURN_ON_ERROR(CLWidthConcatenateLayerKernel::validate(input, offset, output)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 161 | offset += input->dimension(axis); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 162 | } |
| 163 | break; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | case Window::DimY: |
| 168 | { |
| 169 | for(const auto &input : inputs_vector) |
| 170 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 171 | ARM_COMPUTE_RETURN_ON_ERROR(CLHeightConcatenateLayerKernel::validate(input, offset, output)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 172 | offset += input->dimension(axis); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 173 | } |
| 174 | break; |
| 175 | } |
| 176 | case Window::DimZ: |
| 177 | { |
| 178 | for(const auto &input : inputs_vector) |
| 179 | { |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 180 | ARM_COMPUTE_RETURN_ON_ERROR(CLDepthConcatenateLayerKernel::validate(input, offset, output)); |
Georgios Pinitas | 9e4824c | 2019-04-12 13:15:58 +0100 | [diff] [blame] | 181 | offset += input->dimension(axis); |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 182 | } |
| 183 | break; |
| 184 | } |
| 185 | default: |
| 186 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 187 | } |
| 188 | |
Michalis Spyrou | a9c4472 | 2019-04-05 17:18:36 +0100 | [diff] [blame] | 189 | if(output->total_size() != 0) |
| 190 | { |
| 191 | TensorShape output_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(inputs_vector, axis); |
| 192 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 193 | } |
| 194 | |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 195 | return Status{}; |
| 196 | } |
| 197 | |
| 198 | void CLConcatenateLayer::run() |
| 199 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 200 | for(auto &kernel : _concat_kernels) |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 201 | { |
Michalis Spyrou | 8c57169 | 2019-04-05 11:29:52 +0100 | [diff] [blame] | 202 | CLScheduler::get().enqueue(*kernel, true); |
Pablo Tello | 6a14adb | 2019-03-05 17:33:08 +0000 | [diff] [blame] | 203 | } |
Georgios Pinitas | e29acf1 | 2018-07-16 14:40:09 +0100 | [diff] [blame] | 204 | } |
| 205 | } // namespace arm_compute |