xref: /aosp_15_r20/external/ComputeLibrary/src/core/NEON/kernels/NEGatherKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2022 Arm Limited.
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  */
24 #include "src/core/NEON/kernels/NEGatherKernel.h"
25 
26 #include "arm_compute/core/Coordinates.h"
27 #include "arm_compute/core/Error.h"
28 #include "arm_compute/core/Helpers.h"
29 #include "arm_compute/core/TensorInfo.h"
30 #include "arm_compute/core/Validate.h"
31 #include "arm_compute/core/Window.h"
32 #include "arm_compute/core/utils/misc/ShapeCalculator.h"
33 #include "src/core/CPP/Validate.h"
34 #include "src/core/helpers/AutoConfiguration.h"
35 #include "src/core/helpers/WindowHelpers.h"
36 
37 namespace arm_compute
38 {
39 namespace
40 {
41 /** Validate the indices
42  *
43  * Validate that indices are not negative
44  *
45  * @param[in] indices Indices tensor info.
46  */
47 
48 template <typename U>
validate_indices(const ITensor * indices)49 void validate_indices(const ITensor *indices)
50 {
51     Window window;
52     window.use_tensor_dimensions(indices->info()->tensor_shape());
53     execute_window_loop(window, [&](const Coordinates & id)
54     {
55         const auto i = *(reinterpret_cast<int32_t *>(indices->ptr_to_element(id)));
56         ARM_COMPUTE_UNUSED(i);
57         ARM_COMPUTE_ERROR_ON(i < 0);
58     });
59 }
60 
validate_arguments(const ITensorInfo * input,const ITensorInfo * indices,const ITensorInfo * output,int axis)61 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
62 {
63     ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
64     ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
65 
66     if(axis < 0)
67     {
68         axis += input->num_dimensions();
69     }
70 
71     ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
72     ARM_COMPUTE_RETURN_ERROR_ON(axis != 1 && indices->num_dimensions() > 1);
73     ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
74 
75     if(output->total_size() != 0)
76     {
77         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
78         ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
79         TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), axis);
80         ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
81     }
82 
83     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(indices, 1, DataType::U32, DataType::S32);
84 
85     return Status{};
86 }
87 } // namespace
88 
NEGatherKernel()89 NEGatherKernel::NEGatherKernel()
90     : _input{}, _indices{}, _axis{}, _output{}, _func{}
91 {
92 }
93 
94 template <typename U>
gather_multiindices_1_axis(const Window & window,const ThreadInfo & info)95 inline void NEGatherKernel::gather_multiindices_1_axis(const Window &window, const ThreadInfo &info)
96 {
97     ARM_COMPUTE_UNUSED(info);
98     ARM_COMPUTE_ERROR_ON(_indices->info()->num_dimensions() < 2 || _indices->info()->num_dimensions() > 3);
99     validate_indices<U>(_indices);
100     Window win = window;
101     win.set(Window::DimX, Window::Dimension(0, 1, 1));
102     execute_window_loop(win, [&](const Coordinates & id)
103     {
104         auto       *dst_ptr = _output->ptr_to_element(id);
105         Coordinates index_offset;
106         for(uint32_t k = 0; k < _indices->info()->num_dimensions(); ++k)
107         {
108             index_offset.set(k, id[k + 1]);
109         }
110         const uint32_t row = *(reinterpret_cast<uint32_t *>(_indices->ptr_to_element(index_offset)));
111         Coordinates    src_offset;
112         // Set up input coords to read the row specified by the current index
113         src_offset.set(0, 0);
114         src_offset.set(1, row);
115         for(uint32_t j = 2; j < _input->info()->num_dimensions(); ++j)
116         {
117             src_offset.set(j, id[1 + _indices->info()->num_dimensions() + (j - 2)]);
118         }
119         const auto in_ptr_row = _input->ptr_to_element(src_offset);
120         // Copy a row from input to output
121         memcpy(dst_ptr, in_ptr_row, _input->info()->tensor_shape()[0] * _input->info()->element_size());
122     });
123 }
124 
125 template <typename U>
gather_0_axis(const Window & window,const ThreadInfo & info)126 inline void NEGatherKernel::gather_0_axis(const Window &window, const ThreadInfo &info)
127 {
128     ARM_COMPUTE_UNUSED(info);
129 
130     // Validate that the indices are not negative
131     validate_indices<U>(_indices);
132 
133     Iterator output_it(_output, window);
134     execute_window_loop(window, [&](const Coordinates & id)
135     {
136         Coordinates gather_id(id);
137 
138         auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
139         gather_id.set(0, new_index);
140 
141         std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(), output_it.ptr());
142     },
143     output_it);
144 }
145 
146 template <typename U>
gather_n_axis(const Window & window,const ThreadInfo & info)147 void NEGatherKernel::gather_n_axis(const Window &window, const ThreadInfo &info)
148 {
149     ARM_COMPUTE_UNUSED(info);
150 
151     // Validate that the indices are not negative
152     validate_indices<U>(_indices);
153 
154     Window output_window{ window };
155     output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
156 
157     Iterator output_it(_output, output_window);
158     execute_window_loop(output_window, [&](const Coordinates & id)
159     {
160         Coordinates gather_id(id);
161 
162         auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
163         gather_id.set(_axis, new_index);
164 
165         std::copy_n(_input->ptr_to_element(gather_id), _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
166     },
167     output_it);
168 }
169 
configure(const ITensor * input,const ITensor * indices,ITensor * output,int axis)170 void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
171 {
172     ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
173     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
174 
175     _input   = input;
176     _indices = indices;
177     _output  = output;
178     _axis    = axis;
179 
180     if(_axis < 0)
181     {
182         _axis += input->info()->num_dimensions();
183     }
184     ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
185 
186     if(indices->info()->num_dimensions() == 1u)
187     {
188         if(_axis == 0)
189         {
190             switch(_indices->info()->data_type())
191             {
192                 case DataType::U32:
193                     _func = &NEGatherKernel::gather_0_axis<uint32_t>;
194                     break;
195                 case DataType::S32:
196                     _func = &NEGatherKernel::gather_0_axis<int32_t>;
197                     break;
198                 default:
199                     ARM_COMPUTE_ERROR("Not supported");
200                     break;
201             }
202         }
203         else
204         {
205             switch(_indices->info()->data_type())
206             {
207                 case DataType::U32:
208                     _func = &NEGatherKernel::gather_n_axis<uint32_t>;
209                     break;
210                 case DataType::S32:
211                     _func = &NEGatherKernel::gather_n_axis<int32_t>;
212                     break;
213                 default:
214                     ARM_COMPUTE_ERROR("Not supported");
215                     break;
216             }
217         }
218     }
219     else
220     {
221         if(_axis == 1)
222         {
223             switch(_indices->info()->data_type())
224             {
225                 case DataType::U32:
226                     _func = &NEGatherKernel::gather_multiindices_1_axis<uint32_t>;
227                     break;
228                 case DataType::S32:
229                     _func = &NEGatherKernel::gather_multiindices_1_axis<int32_t>;
230                     break;
231                 default:
232                     ARM_COMPUTE_ERROR("Not supported");
233                     break;
234             }
235         }
236         else
237         {
238             ARM_COMPUTE_ERROR("Not supported");
239         }
240     }
241 
242     // Output auto initialization if not yet initialized
243     const TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->info()->tensor_shape(), indices->info()->tensor_shape(), _axis);
244     auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
245 
246     // Create window
247     Window win = calculate_max_window(*output->info(), Steps());
248 
249     INEKernel::configure(win);
250 }
251 
validate(const ITensorInfo * input,const ITensorInfo * indices,const ITensorInfo * output,int axis)252 Status NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
253 {
254     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
255     return Status{};
256 }
257 
run(const Window & window,const ThreadInfo & info)258 void NEGatherKernel::run(const Window &window, const ThreadInfo &info)
259 {
260     ARM_COMPUTE_UNUSED(info);
261     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
262     ARM_COMPUTE_ERROR_ON(_func == nullptr);
263 
264     (this->*_func)(window, info);
265 }
266 
267 } // namespace arm_compute
268