Compute Library
 22.11
NEGatherKernel.cpp
Go to the documentation of this file.
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  */
25 
27 #include "arm_compute/core/Error.h"
33 #include "src/core/CPP/Validate.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>
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)));
57  ARM_COMPUTE_ERROR_ON(i < 0);
58  });
59 }
60 
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  {
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 
84 
85  return Status{};
86 }
87 } // namespace
88 
90  : _input{}, _indices{}, _axis{}, _output{}, _func{}
91 {
92 }
93 
94 template <typename U>
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>
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>
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 
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 
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 
258 void NEGatherKernel::run(const Window &window, const ThreadInfo &info)
259 {
260  ARM_COMPUTE_UNUSED(info);
262  ARM_COMPUTE_ERROR_ON(_func == nullptr);
263 
264  (this->*_func)(window, info);
265 }
266 
267 } // namespace arm_compute
void set(size_t dimension, T value, bool increase_dim_unit=true)
Accessor to set the value of one of the dimensions.
Definition: Dimensions.h:76
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
uint8_t * ptr_to_element(const Coordinates &id) const
Return a pointer to the element at the passed coordinates.
Definition: ITensor.h:63
Shape of a tensor.
Definition: TensorShape.h:39
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:606
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
virtual DataType data_type() const =0
Data type used for each element of the tensor.
#define ARM_COMPUTE_ERROR_ON(cond)
If the condition is true then an error message is printed and an exception thrown.
Definition: Error.h:466
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Describe one of the image&#39;s dimensions with a start, end and step.
Definition: Window.h:79
Status class.
Definition: Error.h:52
Status validate_arguments(const ITensorInfo *src, const ITensorInfo *weights, const ITensorInfo *dst, const PadStrideInfo &conv_info)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
void configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis=0)
Initialise the kernel&#39;s inputs and outputs.
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
1 channel, 1 S32 per channel
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
1 channel, 1 U32 per channel
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
Coordinates of an item.
Definition: Coordinates.h:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
constexpr uint8_t * ptr() const
Return a pointer to the current pixel.
Definition: Helpers.inl:139
virtual size_t element_size() const =0
Element size in bytes calculated as data_size() * num_channels()
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
static Status validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
Static function to check if given info will lead to a valid configuration.
NEGatherKernel()
Default constructor.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:179
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:541
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Iterator updated by execute_window_loop for each window element.
Definition: Helpers.h:46
Describe a multidimensional execution window.
Definition: Window.h:39
TensorShape compute_gather_shape(const TensorShape &input_shape, const TensorShape &indices_shape, uint32_t actual_axis)
Calculate the gather output shape of a tensor.