Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 1 | /* |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 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 | |
| 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Validate.h" |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 33 | |
| 34 | #include <cstddef> |
| 35 | #include <cstdint> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | namespace |
| 40 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 41 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, |
| 44 | DataType::U16, DataType::S16, DataType::QS16, |
| 45 | DataType::U32, DataType::S32, |
| 46 | DataType::F16, DataType::F32); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() < 3, "Invalid input size!"); |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 49 | (perm.num_dimensions() != 3 && ((perm[0] != 2 && perm[1] != 0 && perm[2] != 1) || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))) && (perm.num_dimensions() != 4 && ((perm[0] != 2 && perm[1] != 0 |
| 50 | && perm[2] != 1) |
| 51 | || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0))), |
| 52 | "Only [2, 0, 1],[1, 2, 0] and [3, 2, 0, 1] permutation is supported"); |
| 53 | |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 54 | 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] | 55 | |
| 56 | // Validate configured output |
| 57 | if(output->total_size() != 0) |
| 58 | { |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 62 | } |
| 63 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 64 | return Status{}; |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 65 | } |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 66 | |
| 67 | template <typename T> |
| 68 | inline void permute_strides(Dimensions<T> &dimensions, const PermutationVector &perm) |
| 69 | { |
| 70 | const auto old_dim = utility::make_array<Dimensions<T>::num_max_dimensions>(dimensions.begin(), dimensions.end()); |
| 71 | for(unsigned int i = 0; i < perm.num_dimensions(); ++i) |
| 72 | { |
| 73 | dimensions[perm[i]] = old_dim[i]; |
| 74 | } |
| 75 | } |
| 76 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 77 | } // namespace |
| 78 | |
| 79 | template <typename T> |
| 80 | void CPPPermuteKernel::run_permute(const Window &window) |
| 81 | { |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 82 | Strides strides = _output->info()->strides_in_bytes(); |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 83 | Strides perm_strides = strides; |
Pablo Tello | 6c6e77a | 2018-01-23 10:03:27 +0000 | [diff] [blame] | 84 | permute_strides(perm_strides, _perm); |
| 85 | const int output_stride_w = strides[3]; |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 86 | Window window_out(window); |
| 87 | const Window::Dimension zero_window = Window::Dimension(0, 0, 0); |
| 88 | for(size_t d = 0; d <= _perm.num_dimensions(); ++d) |
| 89 | { |
| 90 | window_out.set(d, zero_window); |
| 91 | } |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 92 | // Create iterators |
| 93 | Iterator in(_input, window); |
| 94 | Iterator out(_output, window_out); |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_ON(_perm.num_dimensions() > _input->info()->num_dimensions()); |
| 96 | if(_input->info()->num_dimensions() <= 3) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 97 | { |
| 98 | execute_window_loop(window, [&](const Coordinates & id) |
| 99 | { |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 100 | const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2]; |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 101 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 102 | }, |
| 103 | in, out); |
| 104 | } |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 105 | else if(_input->info()->num_dimensions() >= 4) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 106 | { |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 107 | if(_perm.num_dimensions() < _input->info()->num_dimensions()) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 108 | { |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 109 | // special case: perm.size = 3 and tensor size > 3, _perm[3] would be invalid so we handle this with id[3] * output_stride_w instead of id[_perm[3]] |
| 110 | ARM_COMPUTE_ERROR_ON(_perm.num_dimensions() < 3); |
| 111 | execute_window_loop(window, [&](const Coordinates & id) |
| 112 | { |
| 113 | const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * output_stride_w; |
| 114 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 115 | }, |
| 116 | in, out); |
| 117 | } |
| 118 | else |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 119 | { |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 120 | execute_window_loop(window, [&](const Coordinates & id) |
| 121 | { |
| 122 | const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_strides[3]; |
| 123 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 124 | }, |
| 125 | in, out); |
| 126 | } |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 127 | } |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | CPPPermuteKernel::CPPPermuteKernel() |
| 131 | : _func(), _input(nullptr), _output(nullptr), _perm() |
| 132 | { |
| 133 | } |
| 134 | |
| 135 | void CPPPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm) |
| 136 | { |
| 137 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Pablo Tello | 00afd11 | 2018-01-04 10:34:24 +0000 | [diff] [blame] | 138 | 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] | 139 | // Output auto inizialitation if not yet initialized |
Pablo Tello | 02541fb | 2017-12-15 09:48:59 +0000 | [diff] [blame] | 140 | 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] | 141 | |
| 142 | // Perform validation step |
| 143 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm)); |
| 144 | |
| 145 | _input = input; |
| 146 | _output = output; |
| 147 | _perm = perm; |
| 148 | |
| 149 | switch(input->info()->element_size()) |
| 150 | { |
| 151 | case 1: |
| 152 | _func = &CPPPermuteKernel::run_permute<uint8_t>; |
| 153 | break; |
| 154 | case 2: |
| 155 | _func = &CPPPermuteKernel::run_permute<uint16_t>; |
| 156 | break; |
| 157 | case 4: |
| 158 | _func = &CPPPermuteKernel::run_permute<uint32_t>; |
| 159 | break; |
| 160 | default: |
| 161 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | // Configure kernel window |
| 166 | Window win = calculate_max_window(*input->info(), Steps()); |
| 167 | |
| 168 | // The CPPPermute doesn't need padding so update_window_and_padding() can be skipped |
| 169 | Coordinates coord; |
| 170 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 171 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 172 | |
| 173 | ICPPKernel::configure(win); |
| 174 | } |
| 175 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 176 | Status CPPPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 177 | { |
| 178 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm)); |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 179 | return Status{}; |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void CPPPermuteKernel::run(const Window &window, const ThreadInfo &info) |
| 183 | { |
| 184 | ARM_COMPUTE_UNUSED(info); |
| 185 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 186 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 187 | |
| 188 | if(_func != nullptr) |
| 189 | { |
| 190 | (this->*_func)(window); |
| 191 | } |
| 192 | } |