Michele Di Giorgio | 7d61ff0 | 2021-01-18 21:15:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2021 Arm Limited. |
| 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 "src/runtime/gpu/cl/operators/ClConcatenate.h" |
| 25 | |
| 26 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 27 | #include "arm_compute/runtime/CL/CLScheduler.h" |
| 28 | |
| 29 | #include "src/core/gpu/cl/kernels/ClBatchConcatenateKernel.h" |
| 30 | #include "src/core/gpu/cl/kernels/ClDepthConcatenateKernel.h" |
| 31 | #include "src/core/gpu/cl/kernels/ClHeightConcatenateKernel.h" |
| 32 | #include "src/core/gpu/cl/kernels/ClWidthConcatenate2TensorsKernel.h" |
| 33 | #include "src/core/gpu/cl/kernels/ClWidthConcatenate4TensorsKernel.h" |
| 34 | #include "src/core/gpu/cl/kernels/ClWidthConcatenateKernel.h" |
| 35 | |
| 36 | #include "arm_compute/core/Error.h" |
| 37 | #include "arm_compute/core/TensorInfo.h" |
| 38 | #include "arm_compute/core/Types.h" |
| 39 | #include "src/core/helpers/AutoConfiguration.h" |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace opencl |
| 44 | { |
| 45 | ClConcatenate::ClConcatenate() |
| 46 | : _concat_kernels(), |
| 47 | _num_inputs(0), |
| 48 | _axis(Window::DimX) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | void ClConcatenate::configure(const CLCompileContext &compile_context, const std::vector<ITensorInfo *> &src_vector, ITensorInfo *dst, size_t axis) |
| 53 | { |
| 54 | ARM_COMPUTE_ERROR_ON(dst == nullptr); |
| 55 | _axis = axis; |
| 56 | _num_inputs = src_vector.size(); |
| 57 | |
| 58 | TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(src_vector, _axis); |
| 59 | std::vector<const ITensorInfo *> const_src_vector(src_vector.size()); |
| 60 | std::transform(src_vector.begin(), src_vector.end(), const_src_vector.begin(), [](ITensorInfo * t) |
| 61 | { |
| 62 | ARM_COMPUTE_ERROR_ON_NULLPTR(t); |
| 63 | return t; |
| 64 | }); |
| 65 | |
| 66 | // dst auto inizialitation if not yet initialized |
| 67 | auto_init_if_empty(*dst, dst_shape, 1, src_vector[0]->data_type()); |
| 68 | ARM_COMPUTE_ERROR_THROW_ON(ClConcatenate::validate(const_src_vector, dst, axis)); |
| 69 | |
| 70 | unsigned int offset = 0; |
| 71 | switch(_axis) |
| 72 | { |
| 73 | case Window::DimX: |
| 74 | { |
| 75 | switch(_num_inputs) |
| 76 | { |
| 77 | case 2: |
| 78 | { |
| 79 | // Configure WidthConcatenate2Tensors kernel |
| 80 | auto kernel = std::make_unique<kernels::ClWidthConcatenate2TensorsKernel>(); |
| 81 | kernel->configure(compile_context, src_vector.at(0), src_vector.at(1), dst); |
| 82 | _concat_kernels.emplace_back(std::move(kernel)); |
| 83 | break; |
| 84 | } |
| 85 | case 4: |
| 86 | { |
| 87 | // Configure WidthConcatenate4Tensors kernel |
| 88 | auto kernel = std::make_unique<kernels::ClWidthConcatenate4TensorsKernel>(); |
| 89 | kernel->configure(compile_context, src_vector.at(0), src_vector.at(1), src_vector.at(2), src_vector.at(3), dst); |
| 90 | _concat_kernels.emplace_back(std::move(kernel)); |
| 91 | break; |
| 92 | } |
| 93 | default: |
| 94 | { |
| 95 | // Configure generic case WidthConcatenate kernels |
| 96 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 97 | { |
| 98 | auto kernel = std::make_unique<kernels::ClWidthConcatenateKernel>(); |
| 99 | kernel->configure(compile_context, src_vector.at(i), offset, dst); |
| 100 | offset += src_vector.at(i)->dimension(_axis); |
| 101 | _concat_kernels.emplace_back(std::move(kernel)); |
| 102 | } |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | case Window::DimY: |
| 109 | { |
| 110 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 111 | { |
| 112 | auto kernel = std::make_unique<kernels::ClHeightConcatenateKernel>(); |
| 113 | kernel->configure(compile_context, src_vector.at(i), offset, dst); |
| 114 | offset += src_vector.at(i)->dimension(_axis); |
| 115 | _concat_kernels.emplace_back(std::move(kernel)); |
| 116 | } |
| 117 | break; |
| 118 | } |
| 119 | case Window::DimZ: |
| 120 | { |
| 121 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 122 | { |
| 123 | auto kernel = std::make_unique<kernels::ClDepthConcatenateKernel>(); |
| 124 | kernel->configure(compile_context, src_vector.at(i), offset, dst); |
| 125 | offset += src_vector.at(i)->dimension(_axis); |
| 126 | _concat_kernels.emplace_back(std::move(kernel)); |
| 127 | } |
| 128 | break; |
| 129 | } |
| 130 | case 3: |
| 131 | { |
| 132 | for(unsigned int i = 0; i < _num_inputs; ++i) |
| 133 | { |
| 134 | auto kernel = std::make_unique<kernels::ClBatchConcatenateKernel>(); |
| 135 | kernel->configure(compile_context, src_vector.at(i), offset, dst); |
| 136 | offset += src_vector.at(i)->dimension(_axis); |
| 137 | _concat_kernels.emplace_back(std::move(kernel)); |
| 138 | } |
| 139 | break; |
| 140 | } |
| 141 | default: |
| 142 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | Status ClConcatenate::validate(const std::vector<const ITensorInfo *> &src_vector, const ITensorInfo *dst, size_t axis) |
| 147 | { |
| 148 | ARM_COMPUTE_RETURN_ERROR_ON(dst == nullptr); |
| 149 | const unsigned int num_inputs = src_vector.size(); |
| 150 | |
| 151 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst); |
| 152 | ARM_COMPUTE_RETURN_ERROR_ON(num_inputs < 2); |
| 153 | |
| 154 | unsigned int offset = 0; |
| 155 | switch(axis) |
| 156 | { |
| 157 | case Window::DimX: |
| 158 | { |
| 159 | switch(num_inputs) |
| 160 | { |
| 161 | case 2: |
| 162 | // Validate WidthConcatenate2Tensors kernels if there are 2 inputs |
| 163 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src_vector[0], src_vector[1]); |
| 164 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWidthConcatenate2TensorsKernel::validate(src_vector[0], src_vector[1], dst)); |
| 165 | break; |
| 166 | case 4: |
| 167 | // Validate WidthConcatenate4Tensors kernels if there are 4 inputs |
| 168 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src_vector[0], src_vector[1], src_vector[2], src_vector[3]); |
| 169 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWidthConcatenate4TensorsKernel::validate(src_vector[0], src_vector[1], src_vector[2], src_vector[3], dst)); |
| 170 | break; |
| 171 | default: |
| 172 | // Validate generic case of WidthConcatenate kernel |
| 173 | for(const auto &src : src_vector) |
| 174 | { |
| 175 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
| 176 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClWidthConcatenateKernel::validate(src, offset, dst)); |
| 177 | offset += src->dimension(axis); |
| 178 | } |
| 179 | break; |
| 180 | } |
| 181 | break; |
| 182 | } |
| 183 | case Window::DimY: |
| 184 | { |
| 185 | for(const auto &src : src_vector) |
| 186 | { |
| 187 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClHeightConcatenateKernel::validate(src, offset, dst)); |
| 188 | offset += src->dimension(axis); |
| 189 | } |
| 190 | break; |
| 191 | } |
| 192 | case Window::DimZ: |
| 193 | { |
| 194 | for(const auto &src : src_vector) |
| 195 | { |
| 196 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClDepthConcatenateKernel::validate(src, offset, dst)); |
| 197 | offset += src->dimension(axis); |
| 198 | } |
| 199 | break; |
| 200 | } |
| 201 | case 3: |
| 202 | { |
| 203 | for(const auto &src : src_vector) |
| 204 | { |
| 205 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::ClBatchConcatenateKernel::validate(src, offset, dst)); |
| 206 | offset += src->dimension(axis); |
| 207 | } |
| 208 | break; |
| 209 | } |
| 210 | default: |
| 211 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 212 | } |
| 213 | |
| 214 | if(dst->total_size() != 0) |
| 215 | { |
| 216 | TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(src_vector, axis); |
| 217 | ARM_COMPUTE_RETURN_ERROR_ON(dst_shape.total_size() != dst->tensor_shape().total_size()); |
| 218 | } |
| 219 | |
| 220 | return Status{}; |
| 221 | } |
| 222 | |
| 223 | void ClConcatenate::run(ITensorPack &tensors) |
| 224 | { |
| 225 | if(tensors.empty()) |
| 226 | { |
| 227 | ARM_COMPUTE_ERROR("No inputs provided"); |
| 228 | } |
| 229 | |
| 230 | if(static_cast<int>(tensors.size()) - 1 != static_cast<int>(_num_inputs)) |
| 231 | { |
| 232 | ARM_COMPUTE_ERROR("Configured with different number of inputs"); |
| 233 | } |
| 234 | |
| 235 | if(_axis == Window::DimX && (_num_inputs == 2 || _num_inputs == 4)) |
| 236 | { |
| 237 | ARM_COMPUTE_ERROR_ON(_concat_kernels.empty()); |
| 238 | CLScheduler::get().enqueue_op(*_concat_kernels.at(0), tensors, true); |
| 239 | } |
| 240 | else |
| 241 | { |
| 242 | int i = 0; |
| 243 | for(auto &k : _concat_kernels) |
| 244 | { |
| 245 | ITensorPack pack; |
| 246 | pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i)); |
| 247 | pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST)); |
| 248 | CLScheduler::get().enqueue_op(*k, pack, true); |
| 249 | ++i; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | } // namespace opencl |
| 254 | } // namespace arm_compute |