Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 8a14b2c | 2020-09-04 20:20:56 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 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 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
| 29 | #include "arm_compute/core/Validate.h" |
| 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Georgios Pinitas | ddb93bb | 2020-10-02 16:38:59 +0100 | [diff] [blame] | 31 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 32 | #include "src/core/helpers/AutoConfiguration.h" |
| 33 | #include "src/core/helpers/WindowHelpers.h" |
| 34 | |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 35 | #include <arm_neon.h> |
| 36 | #include <cstdint> |
| 37 | |
| 38 | using namespace arm_compute::misc::shape_calculator; |
| 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) |
| 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape < 2); |
| 50 | |
| 51 | const DataLayout data_layout = input->data_layout(); |
| 52 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] % (block_shape * block_shape) != 0); |
| 54 | // Validate output if initialized |
| 55 | if(output->total_size() != 0) |
| 56 | { |
| 57 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 58 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_width] != (block_shape * input->tensor_shape()[idx_width])); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(output->tensor_shape()[idx_height] != (block_shape * input->tensor_shape()[idx_height])); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 63 | } |
| 64 | |
| 65 | return Status{}; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | NEDepthToSpaceLayerKernel::NEDepthToSpaceLayerKernel() |
Georgios Pinitas | 0e4556c | 2019-12-20 10:29:12 +0000 | [diff] [blame] | 70 | : _input(nullptr), _output(nullptr), _block_shape(), _data_layout(DataLayout::UNKNOWN) |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 71 | { |
| 72 | } |
| 73 | |
| 74 | void NEDepthToSpaceLayerKernel::configure(const ITensor *input, ITensor *output, int32_t block_shape) |
| 75 | { |
| 76 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 8a14b2c | 2020-09-04 20:20:56 +0100 | [diff] [blame] | 77 | TensorShape output_shape = compute_depth_to_space_shape(input->info()->tensor_shape(), input->info()->data_layout(), block_shape); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 78 | // Output auto inizialitation if not yet initialized |
| 79 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 80 | |
| 81 | // Perform validation step |
| 82 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), block_shape)); |
| 83 | |
| 84 | _input = input; |
| 85 | _output = output; |
| 86 | _block_shape = block_shape; |
Georgios Pinitas | 0e4556c | 2019-12-20 10:29:12 +0000 | [diff] [blame] | 87 | _data_layout = input->info()->data_layout(); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 88 | |
| 89 | // Configure kernel window |
| 90 | Window win = calculate_max_window(*input->info(), Steps()); |
| 91 | ICPPKernel::configure(win); |
| 92 | } |
| 93 | |
| 94 | Status NEDepthToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, int32_t block_shape) |
| 95 | { |
| 96 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 97 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, block_shape)); |
| 98 | return Status{}; |
| 99 | } |
| 100 | |
| 101 | void NEDepthToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 102 | { |
| 103 | ARM_COMPUTE_UNUSED(info); |
| 104 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 105 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 106 | |
Georgios Pinitas | 0e4556c | 2019-12-20 10:29:12 +0000 | [diff] [blame] | 107 | const int idx_channel = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::CHANNEL); |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 108 | const int depth_size = _input->info()->dimension(idx_channel); |
| 109 | const int r = (depth_size / (_block_shape * _block_shape)); |
| 110 | const int element_size = _input->info()->element_size(); |
| 111 | |
| 112 | Window slice_out = window.first_slice_window_3D(); |
| 113 | |
| 114 | // The slice_out slice does not move |
| 115 | slice_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 116 | slice_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 117 | slice_out.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 118 | |
| 119 | // Main loop for NCHW and NHWC |
Georgios Pinitas | 0e4556c | 2019-12-20 10:29:12 +0000 | [diff] [blame] | 120 | if(_data_layout == DataLayout::NCHW) |
Michalis Spyrou | 22f917c | 2019-05-21 13:30:10 +0100 | [diff] [blame] | 121 | { |
| 122 | Window slice_in = window.first_slice_window_2D(); |
| 123 | do |
| 124 | { |
| 125 | Iterator in(_input, slice_in); |
| 126 | execute_window_loop(slice_in, [&](const Coordinates & id) |
| 127 | { |
| 128 | const int x = id.x(); |
| 129 | const int y = id.y(); |
| 130 | |
| 131 | const int z = id.z() % r; |
| 132 | const int out_x = x * _block_shape + (id.z() / r) % _block_shape; |
| 133 | const int out_y = y * _block_shape + (id.z() / r) / _block_shape; |
| 134 | Coordinates output_coords{ out_x, out_y, z, id[3] }; |
| 135 | memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size); |
| 136 | }, |
| 137 | in); |
| 138 | } |
| 139 | while(window.slide_window_slice_2D(slice_in)); |
| 140 | } |
| 141 | else |
| 142 | { |
| 143 | Window slice_in = window.first_slice_window_3D(); |
| 144 | do |
| 145 | { |
| 146 | Iterator in(_input, slice_in); |
| 147 | execute_window_loop(slice_in, [&](const Coordinates & id) |
| 148 | { |
| 149 | const int x = id.y(); |
| 150 | const int y = id.z(); |
| 151 | |
| 152 | const int z = id.x() % r; |
| 153 | const int out_x = x * _block_shape + (id.x() / r) % _block_shape; |
| 154 | const int out_y = y * _block_shape + (id.x() / r) / _block_shape; |
| 155 | Coordinates output_coords{ z, out_x, out_y, id[3] }; |
| 156 | memcpy(_output->ptr_to_element(output_coords), in.ptr(), element_size); |
| 157 | }, |
| 158 | in); |
| 159 | } |
| 160 | while(window.slide_window_slice_3D(slice_in)); |
| 161 | } |
| 162 | } |
| 163 | } // namespace arm_compute |