blob: f1d457d399f26b4c1217f5ef56706734d8962a72 [file] [log] [blame]
John Kesapides2dce6cc2019-01-14 09:47:09 +00001/*
Viet-Hoa Do37c989a2023-02-24 15:52:21 +00002 * Copyright (c) 2019-2023 Arm Limited.
John Kesapides2dce6cc2019-01-14 09:47:09 +00003 *
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 Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGatherKernel.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000025
26#include "arm_compute/core/Coordinates.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000029#include "arm_compute/core/TensorInfo.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030#include "arm_compute/core/utils/misc/ShapeCalculator.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000031#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/Window.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010033
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
John Kesapides2dce6cc2019-01-14 09:47:09 +000036
37namespace arm_compute
38{
39namespace
40{
Georgios Pinitas33843562019-12-10 13:33:18 +000041Status 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 Pinitas33843562019-12-10 13:33:18 +000044 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
45
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010046 if (axis < 0)
Georgios Pinitas33843562019-12-10 13:33:18 +000047 {
48 axis += input->num_dimensions();
49 }
50
51 ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() + indices->num_dimensions() - 1 >
53 Coordinates::num_max_dimensions);
Georgios Pinitas33843562019-12-10 13:33:18 +000054 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
55
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010056 if (output->total_size() != 0)
Georgios Pinitas33843562019-12-10 13:33:18 +000057 {
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(
61 input->tensor_shape(), indices->tensor_shape(), axis);
Georgios Pinitas33843562019-12-10 13:33:18 +000062 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 Kesapides2dce6cc2019-01-14 09:47:09 +000069} // namespace
70
71NEGatherKernel::NEGatherKernel()
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000072 : _input{}, _indices{}, _axis{}, _output{}, _func{}, _src_it_strides{}, _idx_it_strides{}
John Kesapides2dce6cc2019-01-14 09:47:09 +000073{
74}
75
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000076template <typename TIndex>
77void NEGatherKernel::gather_common(const Window &window, const ThreadInfo &info)
John Kesapides2dce6cc2019-01-14 09:47:09 +000078{
79 ARM_COMPUTE_UNUSED(info);
80
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000081 auto dst_win = window;
John Kesapides2dce6cc2019-01-14 09:47:09 +000082
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000083 const auto src_info = _input->info();
84 const auto idx_info = _indices->info();
85 const auto dst_info = _output->info();
86
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 const auto num_dims = dst_info->num_dimensions();
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000088 const auto chunk_stride = src_info->strides_in_bytes()[_axis];
89
90 const auto window_start_x = window.x().start();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010091 const auto window_end_x = window.x().end();
92 auto window_size_x = src_info->element_size();
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000093
Viet-Hoa Doa25582c2023-03-15 16:52:05 +000094 const auto idx_limit = static_cast<TIndex>(src_info->tensor_shape()[_axis]);
95
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096 if (_axis != 0)
John Kesapides2dce6cc2019-01-14 09:47:09 +000097 {
Viet-Hoa Do37c989a2023-02-24 15:52:21 +000098 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 Kesapides2dce6cc2019-01-14 09:47:09 +0000101
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000102 // Compute source and index tensors window based on the output window.
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103 auto src_win = dst_win;
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000104 Window idx_win;
John Kesapides2dce6cc2019-01-14 09:47:09 +0000105
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000106 for (size_t i = 0; i < idx_info->num_dimensions(); ++i)
John Kesapides2dce6cc2019-01-14 09:47:09 +0000107 {
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000108 src_win.set(_axis + i, Window::Dimension(0, 1, 1));
109 idx_win.set(_axis + i, window[_axis + i]);
110 }
John Kesapides2dce6cc2019-01-14 09:47:09 +0000111
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000112 // 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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100115 Iterator dst_it(num_dims, dst_info->strides_in_bytes(), _output->buffer(),
116 dst_info->offset_first_element_in_bytes(), dst_win);
John Kesapides2dce6cc2019-01-14 09:47:09 +0000117
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100118 execute_window_loop(
119 dst_win,
120 [&](const Coordinates &)
Viet-Hoa Doa25582c2023-03-15 16:52:05 +0000121 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 const auto idx = *reinterpret_cast<const TIndex *>(idx_it.ptr());
Viet-Hoa Doa25582c2023-03-15 16:52:05 +0000123
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 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 Kesapides2dce6cc2019-01-14 09:47:09 +0000136}
137
138void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
139{
140 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
Georgios Pinitas33843562019-12-10 13:33:18 +0000141 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000142
143 _input = input;
144 _indices = indices;
145 _output = output;
146 _axis = axis;
147
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100148 if (_axis < 0)
John Kesapides2dce6cc2019-01-14 09:47:09 +0000149 {
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100154 switch (_indices->info()->data_type())
John Kesapides2dce6cc2019-01-14 09:47:09 +0000155 {
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000156 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 Tello894659a2022-05-13 12:20:16 +0100163 ARM_COMPUTE_ERROR("Not supported");
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000164 break;
John Kesapides2dce6cc2019-01-14 09:47:09 +0000165 }
Pablo Marquez Tello894659a2022-05-13 12:20:16 +0100166
John Kesapides2dce6cc2019-01-14 09:47:09 +0000167 // Output auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100168 const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(
169 input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis);
Georgios Pinitas33843562019-12-10 13:33:18 +0000170 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000171
172 // Create window
173 Window win = calculate_max_window(*output->info(), Steps());
John Kesapides2dce6cc2019-01-14 09:47:09 +0000174
175 INEKernel::configure(win);
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000176
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 Thomasmathibalanafd38f02023-09-27 17:46:17 +0100181 const auto input_info = input->info();
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000182 const auto &input_strides = input_info->strides_in_bytes();
183
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100184 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 Do37c989a2023-02-24 15:52:21 +0000187
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100188 for (; dim_no < static_cast<size_t>(_axis); ++dim_no)
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000189 {
190 _src_it_strides[dim_no] = input_strides[dim_no];
191 }
192
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100193 for (; dim_no < static_cast<size_t>(_axis) + indices_num_dims; ++dim_no)
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000194 {
195 _idx_it_strides[dim_no] = indices_strides[dim_no - _axis];
196 }
197
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100198 for (; dim_no < Coordinates::num_max_dimensions; ++dim_no)
Viet-Hoa Do37c989a2023-02-24 15:52:21 +0000199 {
200 _src_it_strides[dim_no] = input_strides[dim_no - indices_num_dims + 1];
201 }
John Kesapides2dce6cc2019-01-14 09:47:09 +0000202}
203
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100204Status
205NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
John Kesapides2dce6cc2019-01-14 09:47:09 +0000206{
Georgios Pinitas33843562019-12-10 13:33:18 +0000207 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
John Kesapides2dce6cc2019-01-14 09:47:09 +0000208 return Status{};
209}
210
211void 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 Kesapides2dce6cc2019-01-14 09:47:09 +0000217 (this->*_func)(window, info);
218}
219
220} // namespace arm_compute