Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018-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 | */ |
Georgios Pinitas | 7891a73 | 2021-08-20 21:39:25 +0100 | [diff] [blame] | 24 | #include "src/cpu/operators/CpuConcatenate.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
| 28 | #include "arm_compute/core/TensorInfo.h" |
| 29 | #include "arm_compute/core/Types.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 32 | #include "arm_compute/runtime/NEON/NEScheduler.h" |
| 33 | |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 34 | #include "src/common/utils/Log.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 35 | #include "src/core/helpers/AutoConfiguration.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 36 | #include "src/cpu/kernels/CpuConcatenateBatchKernel.h" |
| 37 | #include "src/cpu/kernels/CpuConcatenateDepthKernel.h" |
| 38 | #include "src/cpu/kernels/CpuConcatenateHeightKernel.h" |
| 39 | #include "src/cpu/kernels/CpuConcatenateWidthKernel.h" |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace cpu |
| 44 | { |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 45 | void CpuConcatenate::configure(const std::vector<const ITensorInfo *> &srcs_vector, ITensorInfo *dst, size_t axis) |
| 46 | { |
| 47 | ARM_COMPUTE_ERROR_ON(dst == nullptr); |
ramelg01 | 3ae3d88 | 2021-09-12 23:07:47 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_LOG_PARAMS(srcs_vector, dst, axis); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 49 | |
| 50 | _axis = axis; |
| 51 | _num_srcs = srcs_vector.size(); |
| 52 | |
| 53 | TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(srcs_vector, axis); |
| 54 | |
| 55 | // Output auto inizialitation if not yet initialized |
| 56 | auto_init_if_empty(*dst, dst_shape, 1, srcs_vector[0]->data_type()); |
| 57 | ARM_COMPUTE_ERROR_THROW_ON(CpuConcatenate::validate(srcs_vector, dst, axis)); |
| 58 | |
| 59 | unsigned int offset = 0; |
| 60 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 61 | for (unsigned int i = 0; i < _num_srcs; ++i) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 62 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 63 | switch (axis) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 64 | { |
| 65 | case Window::DimX: |
| 66 | { |
| 67 | auto kernel = std::make_unique<kernels::CpuConcatenateWidthKernel>(); |
| 68 | kernel->configure(srcs_vector.at(i), offset, dst); |
| 69 | _concat_kernels.emplace_back(std::move(kernel)); |
| 70 | break; |
| 71 | } |
| 72 | case Window::DimY: |
| 73 | { |
| 74 | auto kernel = std::make_unique<kernels::CpuConcatenateHeightKernel>(); |
| 75 | kernel->configure(srcs_vector.at(i), offset, dst); |
| 76 | _concat_kernels.emplace_back(std::move(kernel)); |
| 77 | break; |
| 78 | } |
| 79 | case Window::DimZ: |
| 80 | { |
| 81 | auto kernel = std::make_unique<kernels::CpuConcatenateDepthKernel>(); |
| 82 | kernel->configure(srcs_vector.at(i), offset, dst); |
| 83 | _concat_kernels.emplace_back(std::move(kernel)); |
| 84 | break; |
| 85 | } |
| 86 | case 3: |
| 87 | { |
| 88 | auto kernel = std::make_unique<kernels::CpuConcatenateBatchKernel>(); |
| 89 | kernel->configure(srcs_vector.at(i), offset, dst); |
| 90 | _concat_kernels.emplace_back(std::move(kernel)); |
| 91 | break; |
| 92 | } |
| 93 | default: |
| 94 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 95 | } |
| 96 | offset += srcs_vector.at(i)->dimension(axis); |
| 97 | } |
| 98 | } |
| 99 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 100 | Status |
| 101 | CpuConcatenate::validate(const std::vector<const ITensorInfo *> &srcs_vector, const ITensorInfo *dst, size_t axis) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 102 | { |
| 103 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(dst); |
| 104 | ARM_COMPUTE_RETURN_ERROR_ON(srcs_vector.size() < 2); |
| 105 | |
| 106 | unsigned int offset = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 107 | for (const auto &src : srcs_vector) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 108 | { |
| 109 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 110 | switch (axis) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 111 | { |
| 112 | case Window::DimX: |
| 113 | { |
| 114 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuConcatenateWidthKernel::validate(src, offset, dst)); |
| 115 | break; |
| 116 | } |
| 117 | case Window::DimY: |
| 118 | { |
| 119 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuConcatenateHeightKernel::validate(src, offset, dst)); |
| 120 | break; |
| 121 | } |
| 122 | case Window::DimZ: |
| 123 | { |
| 124 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuConcatenateDepthKernel::validate(src, offset, dst)); |
| 125 | break; |
| 126 | } |
| 127 | case 3: |
| 128 | { |
| 129 | ARM_COMPUTE_RETURN_ON_ERROR(kernels::CpuConcatenateBatchKernel::validate(src, offset, dst)); |
| 130 | break; |
| 131 | } |
| 132 | default: |
| 133 | ARM_COMPUTE_ERROR("Axis not supported"); |
| 134 | } |
| 135 | offset += src->dimension(axis); |
| 136 | } |
| 137 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 138 | if (dst->total_size() != 0) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 139 | { |
| 140 | TensorShape dst_shape = arm_compute::misc::shape_calculator::calculate_concatenate_shape(srcs_vector, axis); |
| 141 | ARM_COMPUTE_RETURN_ERROR_ON(dst_shape.total_size() != dst->tensor_shape().total_size()); |
| 142 | } |
| 143 | |
| 144 | return Status{}; |
| 145 | } |
| 146 | |
| 147 | void CpuConcatenate::run(ITensorPack &tensors) |
| 148 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 149 | if (tensors.empty()) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 150 | { |
| 151 | ARM_COMPUTE_ERROR("No inputs provided"); |
| 152 | } |
| 153 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 154 | if (static_cast<int>(tensors.size() - 1) != static_cast<int>(_num_srcs)) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 155 | { |
| 156 | ARM_COMPUTE_ERROR("Configured with different number of inputs"); |
| 157 | } |
| 158 | |
| 159 | int i = 0; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame^] | 160 | for (auto &k : _concat_kernels) |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 161 | { |
| 162 | ITensorPack pack; |
| 163 | pack.add_tensor(TensorType::ACL_SRC, tensors.get_const_tensor(ACL_SRC_VEC + i)); |
| 164 | pack.add_tensor(TensorType::ACL_DST, tensors.get_tensor(ACL_DST)); |
Sang-Hoon Park | 0094c02 | 2021-01-20 18:16:47 +0000 | [diff] [blame] | 165 | NEScheduler::get().schedule_op(k.get(), Window::DimY, k->window(), pack); |
Georgios Pinitas | 61ba069 | 2021-01-10 04:07:39 +0000 | [diff] [blame] | 166 | ++i; |
| 167 | } |
| 168 | } |
| 169 | } // namespace cpu |
| 170 | } // namespace arm_compute |