Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020 Arm Limited. |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [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/core/CPP/kernels/CPPPermuteKernel.h" |
| 25 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Helpers.h" |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 27 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 28 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 29 | #include "src/core/helpers/AutoConfiguration.h" |
| 30 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 31 | |
| 32 | #include <cstddef> |
| 33 | #include <cstdint> |
| 34 | |
| 35 | using namespace arm_compute; |
| 36 | |
| 37 | namespace |
| 38 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 40 | { |
Sheri Zhang | a0352d3 | 2020-03-16 14:20:56 +0000 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() > 4, "Only up to 4D permutation vectors are supported"); |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 43 | |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 44 | const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 45 | |
| 46 | // Validate configured output |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 47 | if (output->total_size() != 0) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 48 | { |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 51 | } |
| 52 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 53 | return Status{}; |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 54 | } |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 55 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 56 | } // namespace |
| 57 | |
| 58 | template <typename T> |
| 59 | void CPPPermuteKernel::run_permute(const Window &window) |
| 60 | { |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 61 | // Permute strides |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 62 | Strides strides = _output->info()->strides_in_bytes(); |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 63 | Strides perm_strides = strides; |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 64 | permute_strides(perm_strides, _perm); |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 65 | |
| 66 | // Create output window |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 67 | Window window_out(window); |
| 68 | const Window::Dimension zero_window = Window::Dimension(0, 0, 0); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 69 | for (size_t d = 0; d <= _perm.num_dimensions(); ++d) |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 70 | { |
| 71 | window_out.set(d, zero_window); |
| 72 | } |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 73 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 74 | // Create iterators |
| 75 | Iterator in(_input, window); |
| 76 | Iterator out(_output, window_out); |
Georgios Pinitas | 69af6cf | 2018-02-14 19:23:44 +0000 | [diff] [blame] | 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | if (_input->info()->num_dimensions() <= 3) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 79 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | execute_window_loop( |
| 81 | window, |
| 82 | [&](const Coordinates &id) |
| 83 | { |
| 84 | const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2]; |
| 85 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 86 | }, |
| 87 | in, out); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 88 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 89 | else if (_input->info()->num_dimensions() >= 4) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 90 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | execute_window_loop( |
| 92 | window, |
| 93 | [&](const Coordinates &id) |
| 94 | { |
| 95 | const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + |
| 96 | id[3] * perm_strides[3]; |
| 97 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 98 | }, |
| 99 | in, out); |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 100 | } |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 101 | } |
| 102 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | CPPPermuteKernel::CPPPermuteKernel() : _func(), _input(nullptr), _output(nullptr), _perm() |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | |
| 107 | void CPPPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm) |
| 108 | { |
| 109 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 110 | const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 111 | // Output auto inizialitation if not yet initialized |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 112 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 113 | |
| 114 | // Perform validation step |
| 115 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm)); |
| 116 | |
| 117 | _input = input; |
| 118 | _output = output; |
| 119 | _perm = perm; |
| 120 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | switch (input->info()->element_size()) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 122 | { |
| 123 | case 1: |
| 124 | _func = &CPPPermuteKernel::run_permute<uint8_t>; |
| 125 | break; |
| 126 | case 2: |
| 127 | _func = &CPPPermuteKernel::run_permute<uint16_t>; |
| 128 | break; |
| 129 | case 4: |
| 130 | _func = &CPPPermuteKernel::run_permute<uint32_t>; |
| 131 | break; |
| 132 | default: |
| 133 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 134 | break; |
| 135 | } |
| 136 | |
| 137 | // Configure kernel window |
| 138 | Window win = calculate_max_window(*input->info(), Steps()); |
| 139 | |
| 140 | // The CPPPermute doesn't need padding so update_window_and_padding() can be skipped |
| 141 | Coordinates coord; |
| 142 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 143 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 144 | |
| 145 | ICPPKernel::configure(win); |
| 146 | } |
| 147 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 148 | Status CPPPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 149 | { |
| 150 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 151 | return Status{}; |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void CPPPermuteKernel::run(const Window &window, const ThreadInfo &info) |
| 155 | { |
| 156 | ARM_COMPUTE_UNUSED(info); |
| 157 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 158 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 159 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 160 | if (_func != nullptr) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 161 | { |
| 162 | (this->*_func)(window); |
| 163 | } |
| 164 | } |