John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 1 | /* |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2023 Arm Limited. |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +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/NEGatherKernel.h" |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/Coordinates.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 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" |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 36 | |
| 37 | namespace arm_compute |
| 38 | { |
| 39 | namespace |
| 40 | { |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 41 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis) |
| 42 | { |
| 43 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4); |
| 45 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 46 | if (axis < 0) |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 47 | { |
| 48 | axis += input->num_dimensions(); |
| 49 | } |
| 50 | |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions())); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() + indices->num_dimensions() - 1 > |
| 53 | Coordinates::num_max_dimensions); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
| 55 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | if (output->total_size() != 0) |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape( |
| 61 | input->tensor_shape(), indices->tensor_shape(), axis); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 62 | ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size()); |
| 63 | } |
| 64 | |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32); |
| 66 | |
| 67 | return Status{}; |
| 68 | } |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 69 | } // namespace |
| 70 | |
| 71 | NEGatherKernel::NEGatherKernel() |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 72 | : _input{}, _indices{}, _axis{}, _output{}, _func{}, _src_it_strides{}, _idx_it_strides{} |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 73 | { |
| 74 | } |
| 75 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 76 | template <typename TIndex> |
| 77 | void NEGatherKernel::gather_common(const Window &window, const ThreadInfo &info) |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 78 | { |
| 79 | ARM_COMPUTE_UNUSED(info); |
| 80 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 81 | auto dst_win = window; |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 82 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 83 | const auto src_info = _input->info(); |
| 84 | const auto idx_info = _indices->info(); |
| 85 | const auto dst_info = _output->info(); |
| 86 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 87 | const auto num_dims = dst_info->num_dimensions(); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 88 | const auto chunk_stride = src_info->strides_in_bytes()[_axis]; |
| 89 | |
| 90 | const auto window_start_x = window.x().start(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | const auto window_end_x = window.x().end(); |
| 92 | auto window_size_x = src_info->element_size(); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 93 | |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 94 | const auto idx_limit = static_cast<TIndex>(src_info->tensor_shape()[_axis]); |
| 95 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 96 | if (_axis != 0) |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 97 | { |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 98 | dst_win.set(0, Window::Dimension(window_start_x, window_start_x + 1, 1)); |
| 99 | window_size_x *= window_end_x - window_start_x; |
| 100 | } |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 101 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 102 | // Compute source and index tensors window based on the output window. |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | auto src_win = dst_win; |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 104 | Window idx_win; |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 105 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 106 | for (size_t i = 0; i < idx_info->num_dimensions(); ++i) |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 107 | { |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 108 | src_win.set(_axis + i, Window::Dimension(0, 1, 1)); |
| 109 | idx_win.set(_axis + i, window[_axis + i]); |
| 110 | } |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 111 | |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 112 | // Use the custom strides to access all three tensors using the same loop. |
| 113 | Iterator src_it(num_dims, _src_it_strides, _input->buffer(), src_info->offset_first_element_in_bytes(), src_win); |
| 114 | Iterator idx_it(num_dims, _idx_it_strides, _indices->buffer(), idx_info->offset_first_element_in_bytes(), idx_win); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | Iterator dst_it(num_dims, dst_info->strides_in_bytes(), _output->buffer(), |
| 116 | dst_info->offset_first_element_in_bytes(), dst_win); |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 117 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 118 | execute_window_loop( |
| 119 | dst_win, |
| 120 | [&](const Coordinates &) |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 121 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 122 | const auto idx = *reinterpret_cast<const TIndex *>(idx_it.ptr()); |
Viet-Hoa Do | a25582c | 2023-03-15 16:52:05 +0000 | [diff] [blame] | 123 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 124 | if (idx >= 0 && idx < idx_limit) |
| 125 | { |
| 126 | const auto src_ptr = src_it.ptr() + idx * chunk_stride; |
| 127 | |
| 128 | std::copy_n(src_ptr, window_size_x, dst_it.ptr()); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | std::fill_n(dst_it.ptr(), window_size_x, 0); |
| 133 | } |
| 134 | }, |
| 135 | src_it, idx_it, dst_it); |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis) |
| 139 | { |
| 140 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 141 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis)); |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 142 | |
| 143 | _input = input; |
| 144 | _indices = indices; |
| 145 | _output = output; |
| 146 | _axis = axis; |
| 147 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 148 | if (_axis < 0) |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 149 | { |
| 150 | _axis += input->info()->num_dimensions(); |
| 151 | } |
| 152 | ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions())); |
| 153 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | switch (_indices->info()->data_type()) |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 155 | { |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 156 | case DataType::U32: |
| 157 | _func = &NEGatherKernel::gather_common<uint32_t>; |
| 158 | break; |
| 159 | case DataType::S32: |
| 160 | _func = &NEGatherKernel::gather_common<int32_t>; |
| 161 | break; |
| 162 | default: |
Pablo Marquez Tello | 894659a | 2022-05-13 12:20:16 +0100 | [diff] [blame] | 163 | ARM_COMPUTE_ERROR("Not supported"); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 164 | break; |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 165 | } |
Pablo Marquez Tello | 894659a | 2022-05-13 12:20:16 +0100 | [diff] [blame] | 166 | |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 167 | // Output auto initialization if not yet initialized |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 168 | const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape( |
| 169 | input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis); |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 170 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape)); |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 171 | |
| 172 | // Create window |
| 173 | Window win = calculate_max_window(*output->info(), Steps()); |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 174 | |
| 175 | INEKernel::configure(win); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 176 | |
| 177 | // Create input and indices strides that have the same number of dimensions as the output tensor. |
| 178 | // These will be used to iterate lock-step through all tensors (input, indices and output). |
| 179 | size_t dim_no = 0; |
| 180 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 181 | const auto input_info = input->info(); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 182 | const auto &input_strides = input_info->strides_in_bytes(); |
| 183 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 184 | const auto indices_info = indices->info(); |
| 185 | const auto &indices_strides = indices_info->strides_in_bytes(); |
| 186 | const auto indices_num_dims = indices_info->num_dimensions(); |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 187 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 188 | for (; dim_no < static_cast<size_t>(_axis); ++dim_no) |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 189 | { |
| 190 | _src_it_strides[dim_no] = input_strides[dim_no]; |
| 191 | } |
| 192 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 193 | for (; dim_no < static_cast<size_t>(_axis) + indices_num_dims; ++dim_no) |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 194 | { |
| 195 | _idx_it_strides[dim_no] = indices_strides[dim_no - _axis]; |
| 196 | } |
| 197 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 198 | for (; dim_no < Coordinates::num_max_dimensions; ++dim_no) |
Viet-Hoa Do | 37c989a | 2023-02-24 15:52:21 +0000 | [diff] [blame] | 199 | { |
| 200 | _src_it_strides[dim_no] = input_strides[dim_no - indices_num_dims + 1]; |
| 201 | } |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 204 | Status |
| 205 | NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis) |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 206 | { |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 207 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis)); |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 208 | return Status{}; |
| 209 | } |
| 210 | |
| 211 | void NEGatherKernel::run(const Window &window, const ThreadInfo &info) |
| 212 | { |
| 213 | ARM_COMPUTE_UNUSED(info); |
| 214 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 215 | ARM_COMPUTE_ERROR_ON(_func == nullptr); |
| 216 | |
John Kesapides | 2dce6cc | 2019-01-14 09:47:09 +0000 | [diff] [blame] | 217 | (this->*_func)(window, info); |
| 218 | } |
| 219 | |
| 220 | } // namespace arm_compute |