Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEReorgLayerKernel.h" |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 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" |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/Validate.h" |
| 33 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 36 | |
| 37 | #include <cstddef> |
| 38 | #include <cstdint> |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t stride) |
| 45 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 46 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use CPU FP16 instructions. |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_layout() == DataLayout::UNKNOWN); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 49 | |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 50 | const size_t idx_width = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::WIDTH); |
| 51 | const size_t idx_height = get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::HEIGHT); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 52 | |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON(stride <= 0); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_width] % stride) != 0, |
| 55 | "The width of the input tensor must be a multiple of stride"); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((input->tensor_shape()[idx_height] % stride) != 0, |
| 57 | "The height of the input tensor must be a multiple of stride"); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 58 | |
| 59 | // Validate output if initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | if (output->total_size() != 0) |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 61 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | const TensorInfo tensor_info_output = |
| 63 | output->clone()->set_tensor_shape(misc::shape_calculator::compute_reorg_output_shape(*input, stride)); |
Gian Marco Iodice | 477531c | 2018-08-21 17:53:38 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info_output); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 66 | } |
| 67 | |
| 68 | return Status{}; |
| 69 | } |
| 70 | } // namespace |
| 71 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 72 | NEReorgLayerKernel::NEReorgLayerKernel() : _input(nullptr), _output(nullptr), _stride(1) |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 73 | { |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | void NEReorgLayerKernel::configure(const ITensor *input, ITensor *output, int32_t stride) |
| 77 | { |
| 78 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 79 | |
| 80 | // Output auto inizialitation if not yet initialized |
| 81 | const TensorShape output_shape = misc::shape_calculator::compute_reorg_output_shape(*input->info(), stride); |
| 82 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 83 | |
| 84 | // Perform validation step |
| 85 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), stride)); |
| 86 | |
| 87 | _input = input; |
| 88 | _output = output; |
| 89 | _stride = stride; |
| 90 | |
| 91 | // The NEReorgLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
Michalis Spyrou | a50e702 | 2019-04-09 14:03:17 +0100 | [diff] [blame] | 92 | |
| 93 | // Configure kernel window |
| 94 | Window win = calculate_max_window(*output->info(), Steps()); |
| 95 | |
| 96 | ICPPKernel::configure(win); |
| 97 | } |
| 98 | |
| 99 | Status NEReorgLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t stride) |
| 100 | { |
| 101 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, stride)); |
| 102 | return Status{}; |
| 103 | } |
| 104 | |
| 105 | void NEReorgLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 106 | { |
| 107 | ARM_COMPUTE_UNUSED(info); |
| 108 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 109 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 110 | |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 111 | const DataLayout data_layout = _input->info()->data_layout(); |
| 112 | const size_t idx_w = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 113 | const size_t idx_h = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 114 | const size_t idx_c = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 115 | |
| 116 | const unsigned int stride = _stride; |
| 117 | const unsigned int out_c = _output->info()->tensor_shape()[idx_c] / (stride * stride); |
| 118 | const uint8_t *in_ptr = _input->buffer(); |
| 119 | |
| 120 | // Collapse |
| 121 | Window collapsed_window = window.collapse_if_possible(window, 4); |
| 122 | |
| 123 | // Create Iterator |
| 124 | Iterator out(_output, collapsed_window); |
| 125 | |
| 126 | // Perform reorg |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | execute_window_loop( |
| 128 | collapsed_window, |
| 129 | [&](const Coordinates &id) |
| 130 | { |
| 131 | // Get spatial coords and channels |
| 132 | const unsigned int w = id[idx_w]; |
| 133 | const unsigned int h = id[idx_h]; |
| 134 | const unsigned int c = id[idx_c]; |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 135 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 136 | // Calculate mapping |
| 137 | const unsigned int offset = c / out_c; |
| 138 | Coordinates map_coords = id; |
| 139 | map_coords.set(idx_w, w * stride + offset % stride); |
| 140 | map_coords.set(idx_h, h * stride + offset / stride); |
| 141 | map_coords.set(idx_c, c % out_c); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 142 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | // Perform mapping |
| 144 | std::memcpy(out.ptr(), in_ptr + _input->info()->offset_element_in_bytes(map_coords), |
| 145 | _input->info()->element_size()); |
| 146 | }, |
| 147 | out); |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 148 | } |
Georgios Pinitas | aa6a04a | 2018-08-29 12:53:41 +0100 | [diff] [blame] | 149 | } // namespace arm_compute |