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