Georgios Pinitas | 284cfe2 | 2018-02-13 12:15:13 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/NEON/kernels/NEPermuteKernel.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 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 33 | |
| 34 | namespace |
| 35 | { |
| 36 | #include "arm_compute/core/NEON/kernels/convolution/common/shims.hpp" |
| 37 | } // namespace |
| 38 | |
| 39 | #include <cstddef> |
| 40 | #include <cstdint> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | namespace |
| 45 | { |
| 46 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
| 47 | { |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, |
| 49 | DataType::U16, DataType::S16, DataType::QS16, |
| 50 | DataType::U32, DataType::S32, |
| 51 | DataType::F16, DataType::F32); |
Isabella Gottardi | aad9f2c | 2018-02-21 11:51:23 +0000 | [diff] [blame] | 52 | 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)), |
| 53 | "Only [2, 0, 1] and [1, 2, 0] permutation is supported"); |
Georgios Pinitas | 284cfe2 | 2018-02-13 12:15:13 +0000 | [diff] [blame] | 54 | |
| 55 | const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm); |
| 56 | |
| 57 | // Validate configured output |
| 58 | if(output->total_size() != 0) |
| 59 | { |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape); |
| 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 Status{}; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | template <typename T> |
| 70 | void NEPermuteKernel::run_permute(const Window &window) |
| 71 | { |
| 72 | // Input window |
| 73 | Window window_in = window; |
| 74 | window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().end() - window.x().start())); |
| 75 | window_in.set(Window::DimY, Window::Dimension(window.y().start(), window.y().end(), window.y().end() - window.y().start())); |
| 76 | window_in.set(Window::DimZ, Window::Dimension(window.z().start(), window.z().end(), window.z().end() - window.z().start())); |
| 77 | window_in.set(3, Window::Dimension(window[3].start(), window[3].end(), window[3].end() - window[3].start())); |
| 78 | |
| 79 | // Output window |
| 80 | Window window_out(window); |
| 81 | const Window::Dimension zero_window = Window::Dimension(0, 0, 0); |
| 82 | for(size_t d = 0; d <= _perm.num_dimensions(); ++d) |
| 83 | { |
| 84 | window_out.set(d, zero_window); |
| 85 | } |
| 86 | |
| 87 | // Create iterators |
| 88 | Iterator in(_input, window_in); |
| 89 | Iterator out(_output, window_out); |
| 90 | |
| 91 | // CHW -> HWC |
| 92 | if((_perm.num_dimensions() == 3) && (_perm[0] == 2) && (_perm[1] == 0) && (_perm[2] == 1)) |
| 93 | { |
| 94 | const int in_row_stride = _input->info()->strides_in_bytes().y() / sizeof(T); |
| 95 | const int in_channel_stride = _input->info()->strides_in_bytes().z() / sizeof(T); |
| 96 | const int in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T); |
| 97 | |
| 98 | const int out_channel_stride = _output->info()->strides_in_bytes().x() / sizeof(T); |
| 99 | const int out_col_stride = _output->info()->strides_in_bytes().y() / sizeof(T); |
| 100 | const int out_row_stride = _output->info()->strides_in_bytes().z() / sizeof(T); |
| 101 | const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T); |
| 102 | |
| 103 | const int n_cols = _input->info()->tensor_shape().x(); |
| 104 | const int n_rows = window_in.y().step(); |
| 105 | const int n_channels = _input->info()->tensor_shape().z(); |
| 106 | const int n_batches = _input->info()->tensor_shape()[3]; |
| 107 | |
| 108 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 109 | { |
| 110 | const int idx = id[0] * out_col_stride + id[1] * out_row_stride + id[2] * out_channel_stride; |
| 111 | reorder::nchw_to_nhwc(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx, |
| 112 | n_batches, n_channels, n_rows, n_cols, |
| 113 | in_batch_stride, in_channel_stride, in_row_stride, |
| 114 | out_batch_stride, out_row_stride, out_col_stride); |
| 115 | }, |
| 116 | in, out); |
| 117 | } |
| 118 | // HWC -> CHW |
| 119 | else if((_perm.num_dimensions() == 3) && (_perm[0] == 1) && (_perm[1] == 2) && (_perm[2] == 0)) |
| 120 | { |
| 121 | const int in_col_stride = _input->info()->strides_in_bytes().y() / sizeof(T); |
| 122 | const int in_row_stride = _input->info()->strides_in_bytes().z() / sizeof(T); |
| 123 | const int in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T); |
| 124 | |
| 125 | const int out_col_stride = _output->info()->strides_in_bytes().x() / sizeof(T); |
| 126 | const int out_row_stride = _output->info()->strides_in_bytes().y() / sizeof(T); |
| 127 | const int out_channel_stride = _output->info()->strides_in_bytes().z() / sizeof(T); |
| 128 | const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T); |
| 129 | |
| 130 | const int n_channels = _input->info()->tensor_shape().x(); |
| 131 | const int n_cols = window_in.y().step(); |
| 132 | const int n_rows = _input->info()->tensor_shape().z(); |
| 133 | const int n_batches = _input->info()->tensor_shape()[3]; |
| 134 | |
| 135 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 136 | { |
| 137 | const int idx = id[0] * out_channel_stride + id[1] * out_col_stride + id[2] * out_row_stride; |
| 138 | reorder::nhwc_to_nchw(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx, |
| 139 | n_batches, n_rows, n_cols, n_channels, |
| 140 | in_batch_stride, in_row_stride, in_col_stride, |
| 141 | out_batch_stride, out_channel_stride, out_row_stride); |
| 142 | }, |
| 143 | in, out); |
| 144 | } |
| 145 | else |
| 146 | { |
| 147 | ARM_COMPUTE_ERROR("Unsupported permutation vector"); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | NEPermuteKernel::NEPermuteKernel() |
| 152 | : _func(), _input(nullptr), _output(nullptr), _perm() |
| 153 | { |
| 154 | } |
| 155 | |
| 156 | void NEPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm) |
| 157 | { |
| 158 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 159 | const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm); |
| 160 | // Output auto inizialitation if not yet initialized |
| 161 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 162 | |
| 163 | // Perform validation step |
| 164 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm)); |
| 165 | |
| 166 | _input = input; |
| 167 | _output = output; |
| 168 | _perm = perm; |
| 169 | |
| 170 | switch(input->info()->element_size()) |
| 171 | { |
| 172 | case 1: |
| 173 | _func = &NEPermuteKernel::run_permute<uint8_t>; |
| 174 | break; |
| 175 | case 2: |
| 176 | _func = &NEPermuteKernel::run_permute<uint16_t>; |
| 177 | break; |
| 178 | case 4: |
| 179 | _func = &NEPermuteKernel::run_permute<uint32_t>; |
| 180 | break; |
| 181 | default: |
| 182 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 183 | break; |
| 184 | } |
| 185 | |
| 186 | // Configure kernel window |
| 187 | Window win = calculate_max_window(*input->info(), Steps()); |
| 188 | |
| 189 | // The NEPermute doesn't need padding so update_window_and_padding() can be skipped |
| 190 | Coordinates coord; |
| 191 | coord.set_num_dimensions(output->info()->num_dimensions()); |
| 192 | output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape())); |
| 193 | |
| 194 | ICPPKernel::configure(win); |
| 195 | } |
| 196 | |
| 197 | Status NEPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm) |
| 198 | { |
| 199 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm)); |
| 200 | return Status{}; |
| 201 | } |
| 202 | |
| 203 | void NEPermuteKernel::run(const Window &window, const ThreadInfo &info) |
| 204 | { |
| 205 | ARM_COMPUTE_UNUSED(info); |
| 206 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 207 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 208 | |
| 209 | if(_func != nullptr) |
| 210 | { |
| 211 | (this->*_func)(window); |
| 212 | } |
| 213 | } |