giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 1 | /* |
Michele Di Giorgio | d9eaf61 | 2020-07-08 11:12:57 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2020 Arm Limited. |
giuros01 | ba36825 | 2019-02-19 13:53:10 +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/NESpaceToBatchLayerKernel.h" |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Helpers.h" |
| 27 | #include "arm_compute/core/ITensor.h" |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/Types.h" |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/Validate.h" |
| 31 | |
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" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | #include "src/core/NEON/wrapper/wrapper.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 36 | #include <arm_neon.h> |
| 37 | #include <cstdint> |
| 38 | |
| 39 | using namespace arm_compute::misc::shape_calculator; |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, |
| 46 | const ITensorInfo *block_info, |
| 47 | const ITensorInfo *paddings, |
| 48 | const ITensorInfo *output) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 49 | { |
SiCong Li | 18bdfae | 2020-11-08 21:58:01 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, block_info, paddings, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(block_info, 1, DataType::S32); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 54 | ARM_COMPUTE_RETURN_ERROR_ON(block_info->num_dimensions() > 1); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(block_info->tensor_shape(), TensorShape{2}); |
SiCong Li | 18bdfae | 2020-11-08 21:58:01 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON(paddings->num_dimensions() > 2); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(paddings->tensor_shape(), TensorShape{2, 2}); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [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) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 61 | { |
| 62 | const DataLayout data_layout = input->data_layout(); |
| 63 | const int idx_channel = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(input->tensor_shape()[idx_channel] != output->tensor_shape()[idx_channel]); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
SiCong Li | 18bdfae | 2020-11-08 21:58:01 +0000 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | return Status{}; |
| 70 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 71 | Status validate_arguments_static(const ITensorInfo *input, |
| 72 | const int block_shape_x, |
| 73 | const int block_shape_y, |
| 74 | const Size2D &padding_left, |
| 75 | const Size2D &padding_right, |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 76 | const ITensorInfo *output) |
| 77 | { |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 79 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 80 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
SiCong Li | 18bdfae | 2020-11-08 21:58:01 +0000 | [diff] [blame] | 81 | ARM_COMPUTE_RETURN_ERROR_ON(block_shape_x < 1 || block_shape_y < 1); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 82 | |
| 83 | // Validate output if initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 84 | if (output->total_size() != 0) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 85 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 86 | TensorShape expected_output_shape = misc::shape_calculator::compute_space_to_batch_shape( |
| 87 | input, block_shape_x, block_shape_y, padding_left, padding_right); |
SiCong Li | 18bdfae | 2020-11-08 21:58:01 +0000 | [diff] [blame] | 88 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), expected_output_shape); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 89 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michele Di Giorgio | 93c70b8 | 2019-08-08 11:59:14 +0100 | [diff] [blame] | 90 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | return Status{}; |
| 94 | } |
| 95 | } // namespace |
| 96 | |
| 97 | NESpaceToBatchLayerKernel::NESpaceToBatchLayerKernel() |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 98 | : _input(nullptr), |
| 99 | _block_shape(nullptr), |
| 100 | _paddings(nullptr), |
| 101 | _output(nullptr), |
| 102 | _data_layout(DataLayout::UNKNOWN), |
| 103 | _padding_left(), |
| 104 | _block_shape_x(), |
| 105 | _block_shape_y() |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 106 | { |
| 107 | } |
| 108 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 109 | void NESpaceToBatchLayerKernel::configure(const ITensor *input, |
| 110 | const ITensor *block_shape, |
| 111 | const ITensor *paddings, |
| 112 | ITensor *output) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 113 | { |
SiCong Li | 18bdfae | 2020-11-08 21:58:01 +0000 | [diff] [blame] | 114 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, block_shape, paddings, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | ARM_COMPUTE_ERROR_THROW_ON( |
| 116 | validate_arguments(input->info(), block_shape->info(), paddings->info(), output->info())); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 117 | |
| 118 | _input = input; |
| 119 | _block_shape = block_shape; |
| 120 | _paddings = paddings; |
| 121 | _output = output; |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 122 | _data_layout = input->info()->data_layout(); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 123 | |
| 124 | // Configure kernel window |
| 125 | Window win = calculate_max_window(*output->info(), Steps()); |
| 126 | ICPPKernel::configure(win); |
| 127 | } |
| 128 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 129 | void NESpaceToBatchLayerKernel::configure(const ITensor *input, |
| 130 | const int block_shape_x, |
| 131 | const int block_shape_y, |
| 132 | const Size2D &padding_left, |
| 133 | const Size2D &padding_right, |
| 134 | ITensor *output) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 135 | { |
| 136 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 137 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 138 | TensorShape output_shape = misc::shape_calculator::compute_space_to_batch_shape( |
| 139 | input->info(), block_shape_x, block_shape_y, padding_left, padding_right); |
| 140 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), |
| 141 | input->info()->quantization_info()); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 142 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_static(input->info(), block_shape_x, block_shape_y, padding_left, |
| 144 | padding_right, output->info())); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 145 | |
| 146 | _input = input; |
| 147 | _output = output; |
| 148 | _block_shape_x = block_shape_x; |
| 149 | _block_shape_y = block_shape_y; |
| 150 | _padding_left = padding_left; |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 151 | _data_layout = input->info()->data_layout(); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 152 | |
| 153 | // Configure kernel window |
| 154 | Window win = calculate_max_window(*output->info(), Steps()); |
| 155 | INEKernel::configure(win); |
| 156 | } |
| 157 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 158 | Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, |
| 159 | const ITensorInfo *block_shape, |
| 160 | const ITensorInfo *paddings, |
| 161 | const ITensorInfo *output) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 162 | { |
| 163 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, block_shape, paddings, output)); |
| 164 | return Status{}; |
| 165 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 166 | Status NESpaceToBatchLayerKernel::validate(const ITensorInfo *input, |
| 167 | const int block_shape_x, |
| 168 | const int block_shape_y, |
| 169 | const Size2D &padding_left, |
| 170 | const Size2D &padding_right, |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 171 | const ITensorInfo *output) |
| 172 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 173 | ARM_COMPUTE_RETURN_ON_ERROR( |
| 174 | validate_arguments_static(input, block_shape_x, block_shape_y, padding_left, padding_right, output)); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 175 | return Status{}; |
| 176 | } |
| 177 | |
| 178 | void NESpaceToBatchLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 179 | { |
| 180 | ARM_COMPUTE_UNUSED(info); |
| 181 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 182 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window); |
| 183 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 184 | if (_block_shape != nullptr) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 185 | { |
| 186 | // Retrieve the block shapes dynamically |
| 187 | _block_shape_x = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(0))); |
| 188 | _block_shape_y = *(reinterpret_cast<const int *>(_block_shape->ptr_to_element(1))); |
| 189 | } |
| 190 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 191 | if (_paddings != nullptr) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 192 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 193 | const size_t pad_left_x = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({0, 0})); |
| 194 | const size_t pad_left_y = *reinterpret_cast<const size_t *>(_paddings->ptr_to_element({1, 0})); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 195 | _padding_left = Size2D(pad_left_x, pad_left_y); |
| 196 | } |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 197 | const int height_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::HEIGHT); |
| 198 | const int width_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::WIDTH); |
| 199 | const int batch_idx = get_data_layout_dimension_index(_data_layout, DataLayoutDimension::BATCHES); |
| 200 | const int element_size = _input->info()->element_size(); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 201 | |
| 202 | const size_t height = _input->info()->dimension(height_idx); |
| 203 | const size_t width = _input->info()->dimension(width_idx); |
Sadik Armagan | 2965804 | 2020-05-11 10:35:08 +0100 | [diff] [blame] | 204 | const size_t batch_size = _input->info()->dimension(batch_idx); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 205 | |
| 206 | Window slice_out = window.first_slice_window_3D(); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 207 | |
| 208 | int batch_id = 0; |
| 209 | |
| 210 | // Main loop for NCHW and NHWC |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 211 | if (_data_layout == DataLayout::NCHW) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 212 | { |
| 213 | do |
| 214 | { |
| 215 | Iterator out(_output, slice_out); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 216 | execute_window_loop( |
| 217 | slice_out, |
| 218 | [&](const Coordinates &id) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 219 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 220 | const size_t out_x = id.x(); |
| 221 | const size_t out_y = id.y(); |
| 222 | const size_t z = id.z(); |
| 223 | const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x; |
| 224 | const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x; |
| 225 | if (pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && |
| 226 | pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width) |
| 227 | { |
| 228 | const int w = batch_id % batch_size; |
| 229 | const int in_x = pos_x - _padding_left.x(); |
| 230 | const int in_y = pos_y - _padding_left.y(); |
| 231 | Coordinates input_coords{in_x, in_y, z, w}; |
| 232 | memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size); |
| 233 | } |
| 234 | }, |
| 235 | out); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 236 | ++batch_id; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 237 | } while (window.slide_window_slice_3D(slice_out)); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 238 | } |
| 239 | else |
| 240 | { |
| 241 | do |
| 242 | { |
| 243 | Iterator out(_output, slice_out); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 244 | execute_window_loop( |
| 245 | slice_out, |
| 246 | [&](const Coordinates &id) |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 247 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 248 | const size_t out_x = id.y(); |
| 249 | const size_t out_y = id.z(); |
| 250 | const size_t z = id.x(); |
| 251 | const size_t pos_x = out_x * _block_shape_x + (batch_id / batch_size) % _block_shape_x; |
| 252 | const size_t pos_y = out_y * _block_shape_y + (batch_id / batch_size) / _block_shape_x; |
| 253 | if (pos_y >= _padding_left.y() && pos_y < _padding_left.y() + height && |
| 254 | pos_x >= _padding_left.x() && pos_x < _padding_left.x() + width) |
| 255 | { |
| 256 | const int w = batch_id % batch_size; |
| 257 | const int in_x = pos_x - _padding_left.x(); |
| 258 | const int in_y = pos_y - _padding_left.y(); |
| 259 | Coordinates input_coords{z, in_x, in_y, w}; |
| 260 | memcpy(out.ptr(), _input->ptr_to_element(input_coords), element_size); |
| 261 | } |
| 262 | }, |
| 263 | out); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 264 | ++batch_id; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 265 | } while (window.slide_window_slice_3D(slice_out)); |
giuros01 | ba36825 | 2019-02-19 13:53:10 +0000 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | } // namespace arm_compute |