Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, 2023 Arm Limited. |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +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/NEDepthToSpaceLayerKernel.h" |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 25 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CoreTypes.h" |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
| 32 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 35 | #include "src/cpu/kernels/depth_to_space/list.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 36 | |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 37 | #include <cstdint> |
| 38 | |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 48 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 2); |
| 49 | |
| 50 | const DataLayout data_layout = input->data_layout(); |
| 51 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0); |
| 53 | // Validate output if initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | if (output->total_size() != 0) |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 55 | { |
| 56 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 57 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != |
| 59 | (block_shape * input->tensor_shape()[idx_width])); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != |
| 61 | (block_shape * input->tensor_shape()[idx_height])); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 64 | } |
| 65 | |
| 66 | return Status{}; |
| 67 | } |
| 68 | } // namespace |
| 69 | |
| 70 | NEDepthToSpaceLayerKernel::NEDepthToSpaceLayerKernel() |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 71 | : _input(nullptr), |
| 72 | _output(nullptr), |
| 73 | _block_shape(), |
| 74 | _data_layout(DataLayout::UNKNOWN), |
| 75 | _split_dimension(Window::DimY) |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 76 | { |
| 77 | } |
| 78 | |
| 79 | void NEDepthToSpaceLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape) |
| 80 | { |
| 81 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 82 | TensorShape output_shape = misc::shape_calculator::compute_depth_to_space_shape( |
| 83 | input->info()->tensor_shape(), input->info()->data_layout(), block_shape); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 84 | // Output auto inizialitation if not yet initialized |
| 85 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 86 | |
| 87 | // Perform validation step |
| 88 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape)); |
| 89 | |
| 90 | _input = input; |
| 91 | _output = output; |
| 92 | _block_shape = block_shape; |
Georgios Pinitas | 0e4556c | 2019-12-20 10:29:12 +0000 | [diff] [blame] | 93 | _data_layout = input->info()->data_layout(); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 94 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 95 | constexpr size_t dim_b = 3; |
| 96 | const auto dim_h = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 97 | const auto dim_w = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 98 | const auto dim_c = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
| 99 | |
| 100 | ARM_COMPUTE_ERROR_ON(get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES) != dim_b); |
| 101 | |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 102 | // Configure kernel window |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 103 | Steps steps; |
| 104 | steps.set(dim_h, block_shape); |
| 105 | steps.set(dim_w, block_shape); |
| 106 | steps.set(dim_c, output->info()->dimension(dim_c)); |
| 107 | |
| 108 | Window win = calculate_max_window(*output->info(), steps); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 109 | ICPPKernel::configure(win); |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 110 | |
| 111 | const auto num_batches = input->info()->tensor_shape().total_size_upper(dim_b); |
| 112 | if (num_batches > 1) |
| 113 | { |
| 114 | _split_dimension = dim_b; |
| 115 | } |
| 116 | else |
| 117 | { |
| 118 | _split_dimension = dim_h; |
| 119 | } |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | Status NEDepthToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) |
| 123 | { |
| 124 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 125 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape)); |
| 126 | return Status{}; |
| 127 | } |
| 128 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 129 | size_t NEDepthToSpaceLayerKernel::get_split_dimension() const |
| 130 | { |
| 131 | return _split_dimension; |
| 132 | } |
| 133 | |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 134 | void NEDepthToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 135 | { |
| 136 | ARM_COMPUTE_UNUSED(info); |
| 137 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 138 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 139 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 140 | const auto *input_info = _input->info(); |
| 141 | const auto *output_info = _output->info(); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 142 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 143 | const auto element_size = input_info->element_size(); |
| 144 | const auto &input_strides = input_info->strides_in_bytes(); |
| 145 | const auto &output_strides = output_info->strides_in_bytes(); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 146 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 147 | const auto &input_shape = input_info->tensor_shape(); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 148 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 149 | const uintptr_t k_input_strides[] = {input_strides[0], input_strides[1], input_strides[2], input_strides[3]}; |
| 150 | const uintptr_t k_output_strides[] = {output_strides[0], output_strides[1], output_strides[2], output_strides[3]}; |
| 151 | |
| 152 | const uint8_t *k_input_ptr = _input->buffer(); |
| 153 | uint8_t *k_output_ptr = // |
| 154 | _output->buffer() + // |
| 155 | window[3].start() * output_strides[3] + // |
| 156 | window[2].start() * output_strides[2] + // |
| 157 | window[1].start() * output_strides[1] + // |
| 158 | window[0].start() * output_strides[0]; |
| 159 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 160 | if (_data_layout == DataLayout::NCHW) |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 161 | { |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 162 | ARM_COMPUTE_ERROR_ON_MSG(window[2].start() != 0 || window[2].end() != window[2].step(), |
| 163 | "The window cannot be splitted in channel dimension"); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 164 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 165 | const uintptr_t k_input_shape[] = { |
| 166 | window.num_iterations(0), // |
| 167 | window.num_iterations(1), // |
| 168 | input_shape[2], // The window cannot be splitted in channel dimension. |
| 169 | window.num_iterations(3) // |
| 170 | }; |
| 171 | |
| 172 | k_input_ptr += window[3].start() * input_strides[3] + // |
| 173 | window[2].start() * _block_shape * _block_shape * input_strides[2] + // |
| 174 | (window[1].start() / _block_shape) * input_strides[1] + // |
| 175 | (window[0].start() / _block_shape) * input_strides[0]; |
| 176 | |
| 177 | cpu::depth_to_space_nchw_any( // |
| 178 | k_input_ptr, k_output_ptr, // |
| 179 | k_input_shape, k_input_strides, k_output_strides, // |
| 180 | element_size, _block_shape); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 181 | } |
| 182 | else |
| 183 | { |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 184 | ARM_COMPUTE_ERROR_ON_MSG(window[0].start() != 0 || window[0].end() != window[0].step(), |
| 185 | "The window cannot be splitted in channel dimension"); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 186 | |
Viet-Hoa Do | 4737094 | 2023-11-13 17:20:45 +0000 | [diff] [blame] | 187 | const uintptr_t k_input_shape[] = { |
| 188 | input_shape[0], // The window cannot be splitted in channel dimension. |
| 189 | window.num_iterations(1), // |
| 190 | window.num_iterations(2), // |
| 191 | window.num_iterations(3) // |
| 192 | }; |
| 193 | |
| 194 | k_input_ptr += window[3].start() * input_strides[3] + // |
| 195 | (window[2].start() / _block_shape) * input_strides[2] + // |
| 196 | (window[1].start() / _block_shape) * input_strides[1] + // |
| 197 | window[0].start() * _block_shape * _block_shape * input_strides[0]; |
| 198 | |
| 199 | cpu::depth_to_space_nhwc_any( // |
| 200 | k_input_ptr, k_output_ptr, // |
| 201 | k_input_shape, k_input_strides, k_output_strides, // |
| 202 | element_size, _block_shape); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | } // namespace arm_compute |