Compute Library
 21.11
NEGatherKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021 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 template <typename U>
48 void validate_indices(const ITensor *indices)
49 {
50  for(size_t i = 0; i < indices->info()->tensor_shape()[0]; ++i)
51  {
52  ARM_COMPUTE_ERROR_ON(*(reinterpret_cast<U *>(indices->ptr_to_element(Coordinates(i)))) < 0);
53  }
54 }
55 
56 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
57 {
58  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
59  ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1);
60  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
61 
62  if(axis < 0)
63  {
64  axis += input->num_dimensions();
65  }
66 
67  ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
68  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
69 
70  if(output->total_size() != 0)
71  {
74  TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), axis);
75  ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
76  }
77 
79 
80  return Status{};
81 }
82 } // namespace
83 
85  : _input{}, _indices{}, _axis{}, _output{}, _func{}
86 {
87 }
88 
89 template <typename U>
90 inline void NEGatherKernel::gather_0_axis(const Window &window, const ThreadInfo &info)
91 {
92  ARM_COMPUTE_UNUSED(info);
93 
94  // Validate that the indices are not negative
95  validate_indices<U>(_indices);
96 
97  Iterator output_it(_output, window);
98  execute_window_loop(window, [&](const Coordinates & id)
99  {
100  Coordinates gather_id(id);
101 
102  auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
103  gather_id.set(0, new_index);
104 
105  std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(), output_it.ptr());
106  },
107  output_it);
108 }
109 
110 template <typename U>
111 void NEGatherKernel::gather_n_axis(const Window &window, const ThreadInfo &info)
112 {
113  ARM_COMPUTE_UNUSED(info);
114 
115  // Validate that the indices are not negative
116  validate_indices<U>(_indices);
117 
118  Window output_window{ window };
119  output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
120 
121  Iterator output_it(_output, output_window);
122  execute_window_loop(output_window, [&](const Coordinates & id)
123  {
124  Coordinates gather_id(id);
125 
126  auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
127  gather_id.set(_axis, new_index);
128 
129  std::copy_n(_input->ptr_to_element(gather_id), _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
130  },
131  output_it);
132 }
133 
134 void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
135 {
136  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
137  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
138 
139  _input = input;
140  _indices = indices;
141  _output = output;
142  _axis = axis;
143 
144  if(_axis < 0)
145  {
146  _axis += input->info()->num_dimensions();
147  }
148  ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
149 
150  if(0 == _axis)
151  {
152  switch(_indices->info()->data_type())
153  {
154  case DataType::U32:
155  _func = &NEGatherKernel::gather_0_axis<uint32_t>;
156  break;
157  case DataType::S32:
158  _func = &NEGatherKernel::gather_0_axis<int32_t>;
159  break;
160  default:
161  ARM_COMPUTE_ERROR("Not supported");
162  break;
163  }
164  }
165  else
166  {
167  switch(_indices->info()->data_type())
168  {
169  case DataType::U32:
170  _func = &NEGatherKernel::gather_n_axis<uint32_t>;
171  break;
172  case DataType::S32:
173  _func = &NEGatherKernel::gather_n_axis<int32_t>;
174  break;
175  default:
176  ARM_COMPUTE_ERROR("Not supported");
177  break;
178  }
179  }
180  // Output auto initialization if not yet initialized
182  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
183 
184  // Create window
185  Window win = calculate_max_window(*output->info(), Steps());
186 
187  INEKernel::configure(win);
188 }
189 
190 Status NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
191 {
192  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
193  return Status{};
194 }
195 
196 void NEGatherKernel::run(const Window &window, const ThreadInfo &info)
197 {
198  ARM_COMPUTE_UNUSED(info);
200  ARM_COMPUTE_ERROR_ON(_func == nullptr);
201 
202  (this->*_func)(window, info);
203 }
204 
205 } // 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:77
Status class.
Definition: Error.h:52
#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-2021 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 of NEGatherKernel.
NEGatherKernel()
Default constructor.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:158
#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)