Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 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 "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" |
| 32 | |
| 33 | #include <cstddef> |
| 34 | #include <cstdint> |
| 35 | |
| 36 | using namespace arm_compute; |
| 37 | |
| 38 | namespace |
| 39 | { |
| 40 | TensorShape get_output_shape(const ITensorInfo *input, const PermutationVector &perm) |
| 41 | { |
| 42 | TensorShape output_shape = input->tensor_shape(); |
| 43 | permute(output_shape, perm); |
| 44 | return output_shape; |
| 45 | } |
| 46 | |
| 47 | Error validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
| 48 | { |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, |
| 50 | DataType::U16, DataType::S16, DataType::QS16, |
| 51 | DataType::U32, DataType::S32, |
| 52 | DataType::F16, DataType::F32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(input->num_dimensions() < 3, "Invalid input size!"); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(perm.num_dimensions() != 3 && ((perm[0] != 2 && perm[1] != 0 && perm[2] != 1) || (perm[0] != 1 && perm[1] != 2 && perm[2] != 0)), |
| 55 | "Only [2, 0, 1] and [1, 2, 0] permutation is supported"); |
| 56 | |
| 57 | // Validate configured output |
| 58 | if(output->total_size() != 0) |
| 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), get_output_shape(input, perm)); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output); |
| 63 | } |
| 64 | |
| 65 | return Error{}; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | template <typename T> |
| 70 | void CPPPermuteKernel::run_permute(const Window &window) |
| 71 | { |
| 72 | const int output_stride_x = _output->info()->strides_in_bytes().x(); |
| 73 | const int output_stride_y = _output->info()->strides_in_bytes().y(); |
| 74 | const int output_stride_z = _output->info()->strides_in_bytes().z(); |
| 75 | |
| 76 | Window window_out(window); |
| 77 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 78 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 79 | window_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 80 | |
| 81 | // Create iterators |
| 82 | Iterator in(_input, window); |
| 83 | Iterator out(_output, window_out); |
| 84 | |
| 85 | // Run [2, 0, 1] permute |
| 86 | if(_perm[0] == 2 && _perm[1] == 0 && _perm[2] == 1) |
| 87 | { |
| 88 | execute_window_loop(window, [&](const Coordinates & id) |
| 89 | { |
| 90 | const int idx = id.y() * output_stride_z + id.x() * output_stride_y + id.z() * output_stride_x; |
| 91 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 92 | }, |
| 93 | in, out); |
| 94 | } |
| 95 | // Run [1, 2, 0] permute |
| 96 | else |
| 97 | { |
| 98 | execute_window_loop(window, [&](const Coordinates & id) |
| 99 | { |
| 100 | const int idx = id.x() * output_stride_z + id.z() * output_stride_y + id.y() * output_stride_x; |
| 101 | *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr())); |
| 102 | }, |
| 103 | in, out); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | CPPPermuteKernel::CPPPermuteKernel() |
| 108 | : _func(), _input(nullptr), _output(nullptr), _perm() |
| 109 | { |
| 110 | } |
| 111 | |
| 112 | void CPPPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm) |
| 113 | { |
| 114 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 115 | |
| 116 | // Output auto inizialitation if not yet initialized |
| 117 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(get_output_shape(input->info(), perm))); |
| 118 | |
| 119 | // Perform validation step |
| 120 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm)); |
| 121 | |
| 122 | _input = input; |
| 123 | _output = output; |
| 124 | _perm = perm; |
| 125 | |
| 126 | switch(input->info()->element_size()) |
| 127 | { |
| 128 | case 1: |
| 129 | _func = &CPPPermuteKernel::run_permute<uint8_t>; |
| 130 | break; |
| 131 | case 2: |
| 132 | _func = &CPPPermuteKernel::run_permute<uint16_t>; |
| 133 | break; |
| 134 | case 4: |
| 135 | _func = &CPPPermuteKernel::run_permute<uint32_t>; |
| 136 | break; |
| 137 | default: |
| 138 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 139 | break; |
| 140 | } |
| 141 | |
| 142 | // Configure kernel window |
| 143 | Window win = calculate_max_window(*input->info(), Steps()); |
| 144 | |
| 145 | // The CPPPermute doesn't need padding so update_window_and_padding() can be skipped |
| 146 | Coordinates coord; |
| 147 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 148 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 149 | |
| 150 | ICPPKernel::configure(win); |
| 151 | } |
| 152 | |
| 153 | Error CPPPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
| 154 | { |
| 155 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm)); |
| 156 | return Error{}; |
| 157 | } |
| 158 | |
| 159 | void CPPPermuteKernel::run(const Window &window, const ThreadInfo &info) |
| 160 | { |
| 161 | ARM_COMPUTE_UNUSED(info); |
| 162 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 163 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 164 | |
| 165 | if(_func != nullptr) |
| 166 | { |
| 167 | (this->*_func)(window); |
| 168 | } |
| 169 | } |