giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 1 | /* |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2020, 2023 Arm Limited. |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [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/NEBatchToSpaceLayerKernel.h" |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 28 | #include "arm_compute/core/TensorInfo.h" |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Types.h" |
| 30 | #include "arm_compute/core/Validate.h" |
| 31 | #include "arm_compute/core/utils/misc/ShapeCalculator.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" |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace arm_compute::misc::shape_calculator; |
| 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
| 41 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *block_info, const ITensorInfo *output) |
| 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, output); |
| 44 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(block_info, 1, DataType::S32); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 47 | |
| 48 | // Validate output if initialized |
| 49 | if(output->total_size() != 0) |
| 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 53 | } |
| 54 | |
| 55 | return Status{}; |
| 56 | } |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 57 | Status validate_arguments_static(const ITensorInfo *input, int block_shape_x, int block_shape_y, const ITensorInfo *output, const CropInfo &crop_info) |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x <= 0); |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape_y <= 0); |
| 63 | |
| 64 | const DataLayout data_layout = input->data_layout(); |
| 65 | const int idx_batch = get_data_layout_dimension_index(data_layout, DataLayoutDimension::BATCHES); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_batch] % (block_shape_x * block_shape_y) != 0); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 67 | // Validate output if initialized |
| 68 | if(output->total_size() != 0) |
| 69 | { |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON(output->num_dimensions() > 4); |
| 71 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 72 | |
| 73 | const TensorShape expected_output_shape = compute_batch_to_space_shape(input->data_layout(), input->tensor_shape(), block_shape_x, block_shape_y, crop_info); |
| 74 | const TensorInfo expected_output = output->clone()->set_tensor_shape(expected_output_shape); |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &expected_output); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | return Status{}; |
| 79 | } |
| 80 | } // namespace |
| 81 | |
| 82 | NEBatchToSpaceLayerKernel::NEBatchToSpaceLayerKernel() |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 83 | : _input(nullptr), _block_shape(nullptr), _output(nullptr), _data_layout(DataLayout::UNKNOWN), _block_shape_x(), _block_shape_y(), _crop_info() |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 84 | { |
| 85 | } |
| 86 | |
| 87 | void NEBatchToSpaceLayerKernel::configure(const ITensor *input, const ITensor *block_shape, ITensor *output) |
| 88 | { |
| 89 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 90 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), block_shape->info(), output->info())); |
| 91 | |
| 92 | _input = input; |
| 93 | _block_shape = block_shape; |
| 94 | _output = output; |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 95 | _data_layout = input->info()->data_layout(); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 96 | |
| 97 | // Configure kernel window |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 98 | Window win = calculate_max_window(*output->info(), Steps()); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 99 | ICPPKernel::configure(win); |
| 100 | } |
| 101 | |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 102 | void NEBatchToSpaceLayerKernel::configure(const ITensor *input, int32_t block_shape_x, int32_t block_shape_y, ITensor *output, const CropInfo &crop_info) |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 103 | { |
| 104 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
SiCong Li | 5a7d157 | 2023-03-21 12:00:15 +0000 | [diff] [blame] | 105 | const TensorShape output_shape = compute_batch_to_space_shape(input->info()->data_layout(), input->info()->tensor_shape(), block_shape_x, block_shape_y); |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 106 | // Output auto initialization if not yet initialized |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 107 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
| 108 | |
| 109 | // Perform validation step |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 110 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, output->info(), crop_info)); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 111 | |
| 112 | _input = input; |
| 113 | _output = output; |
| 114 | _block_shape_x = block_shape_x; |
| 115 | _block_shape_y = block_shape_y; |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 116 | _data_layout = input->info()->data_layout(); |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 117 | _crop_info = crop_info; |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 118 | |
| 119 | // Configure kernel window |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 120 | Window win = calculate_max_window(*output->info(), Steps()); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 121 | ICPPKernel::configure(win); |
| 122 | } |
| 123 | |
| 124 | Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *block_shape, const ITensorInfo *output) |
| 125 | { |
| 126 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_shape, output); |
| 127 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, output)); |
| 128 | return Status{}; |
| 129 | } |
| 130 | |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 131 | Status NEBatchToSpaceLayerKernel::validate(const ITensorInfo *input, int32_t block_shape_x, int32_t block_shape_y, const ITensorInfo *output, const CropInfo &crop_info) |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 132 | { |
| 133 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 134 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_static(input, block_shape_x, block_shape_y, output, crop_info)); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 135 | return Status{}; |
| 136 | } |
| 137 | |
| 138 | void NEBatchToSpaceLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 139 | { |
| 140 | ARM_COMPUTE_UNUSED(info); |
| 141 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 142 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 143 | |
| 144 | if(_block_shape != nullptr) |
| 145 | { |
| 146 | // Retrieve the block shapes dynamically |
| 147 | _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0))); |
| 148 | _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1))); |
| 149 | } |
| 150 | |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 151 | const int batch_size = _output->info()->dimension(3); |
| 152 | const int element_size = _output->info()->element_size(); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 153 | |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 154 | Window slice_out = window.first_slice_window_3D(); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 155 | |
| 156 | int batch_id = 0; |
| 157 | // Main loop for NCHW and NHWC |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 158 | if(_data_layout == DataLayout::NCHW) |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 159 | { |
| 160 | do |
| 161 | { |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 162 | Iterator out(_output, slice_out); |
| 163 | execute_window_loop(slice_out, [&](const Coordinates & id) |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 164 | { |
| 165 | |
| 166 | const int x = id.x(); |
| 167 | const int y = id.y(); |
| 168 | const int z = id.z(); |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 169 | // Translate x, y to uncropped version |
| 170 | const int x_c = x + _crop_info.left; |
| 171 | const int y_c = y + _crop_info.top; |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 172 | |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 173 | const int in_batch = batch_id + ((x_c % _block_shape_x) + (y_c % _block_shape_y) * _block_shape_x) * batch_size; |
| 174 | const int in_x = x_c / _block_shape_x; |
| 175 | const int in_y = y_c / _block_shape_y; |
| 176 | Coordinates input_coords{ in_x, in_y, z, in_batch }; |
| 177 | memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 178 | }, |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 179 | out); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 180 | ++batch_id; |
| 181 | } |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 182 | while(window.slide_window_slice_3D(slice_out)); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 183 | } |
| 184 | else |
| 185 | { |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 186 | // For NHWC we can perform a block copy on the Channel (first) dimension. Thus we do not need to iterate over this dimension |
| 187 | slice_out.set(0U, Window::Dimension(0U, 1U, 1U)); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 188 | do |
| 189 | { |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 190 | Iterator out(_output, slice_out); |
| 191 | execute_window_loop(slice_out, [&](const Coordinates & id) |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 192 | { |
| 193 | |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 194 | const int x = id.y(); |
| 195 | const int y = id.z(); |
| 196 | |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 197 | // Translate x, y to uncropped version |
| 198 | const int x_c = x + _crop_info.left; |
| 199 | const int y_c = y + _crop_info.top; |
| 200 | |
| 201 | const int in_batch = batch_id + ((x_c % _block_shape_x) + (y_c % _block_shape_y) * _block_shape_x) * batch_size; |
| 202 | const int in_x = x_c / _block_shape_x; |
| 203 | const int in_y = y_c / _block_shape_y; |
| 204 | Coordinates input_coords{ 0, in_x, in_y, in_batch }; |
| 205 | memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size * _input->info()->dimension(0)); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 206 | }, |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 207 | out); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 208 | ++batch_id; |
| 209 | } |
SiCong Li | 8893e45 | 2023-03-23 12:06:45 +0000 | [diff] [blame^] | 210 | while(window.slide_window_slice_3D(slice_out)); |
giuros01 | fc1da13 | 2019-02-18 16:48:35 +0000 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | } // namespace arm_compute |