Compute Library
 21.02
NEGatherKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2020 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"
34 #include "src/core/CPP/Validate.h"
37 
38 namespace arm_compute
39 {
40 namespace
41 {
42 /** Validate the indices
43  *
44  * Validate that indices are not negative
45  *
46  * @param[in] indices Indices tensor info.
47  */
48 template <typename U>
49 void validate_indices(const ITensor *indices)
50 {
51  for(size_t i = 0; i < indices->info()->tensor_shape()[0]; ++i)
52  {
53  ARM_COMPUTE_ERROR_ON(*(reinterpret_cast<U *>(indices->ptr_to_element(Coordinates(i)))) < 0);
54  }
55 }
56 
57 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
58 {
59  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, indices, output);
60  ARM_COMPUTE_RETURN_ERROR_ON(indices->num_dimensions() > 1);
61  ARM_COMPUTE_RETURN_ERROR_ON(input->num_dimensions() > 4);
62 
63  if(axis < 0)
64  {
65  axis += input->num_dimensions();
66  }
67 
68  ARM_COMPUTE_RETURN_ERROR_ON(0 > axis || axis >= static_cast<int32_t>(input->num_dimensions()));
69  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
70 
71  if(output->total_size() != 0)
72  {
75  TensorShape output_shape = arm_compute::misc::shape_calculator::compute_gather_shape(input->tensor_shape(), indices->tensor_shape(), axis);
76  ARM_COMPUTE_RETURN_ERROR_ON(output_shape.total_size() != output->tensor_shape().total_size());
77  }
78 
80 
81  return Status{};
82 }
83 } // namespace
84 
86  : _input{}, _indices{}, _axis{}, _output{}, _func{}
87 {
88 }
89 
90 template <typename U>
91 inline void NEGatherKernel::gather_0_axis(const Window &window, const ThreadInfo &info)
92 {
93  ARM_COMPUTE_UNUSED(info);
94 
95  // Validate that the indices are not negative
96  validate_indices<U>(_indices);
97 
98  Iterator output_it(_output, window);
99  execute_window_loop(window, [&](const Coordinates & id)
100  {
101  Coordinates gather_id(id);
102 
103  auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[0]))));
104  gather_id.set(0, new_index);
105 
106  std::copy_n(_input->ptr_to_element(gather_id), _output->info()->element_size(), output_it.ptr());
107  },
108  output_it);
109 }
110 
111 template <typename U>
112 void NEGatherKernel::gather_n_axis(const Window &window, const ThreadInfo &info)
113 {
114  ARM_COMPUTE_UNUSED(info);
115 
116  // Validate that the indices are not negative
117  validate_indices<U>(_indices);
118 
119  Window output_window{ window };
120  output_window.set(Window::DimX, Window::Dimension(0, 1, 1));
121 
122  Iterator output_it(_output, output_window);
123  execute_window_loop(output_window, [&](const Coordinates & id)
124  {
125  Coordinates gather_id(id);
126 
127  auto new_index = *(reinterpret_cast<U *>(_indices->ptr_to_element(Coordinates(id[_axis]))));
128  gather_id.set(_axis, new_index);
129 
130  std::copy_n(_input->ptr_to_element(gather_id), _input->info()->dimension(0) * _output->info()->element_size(), output_it.ptr());
131  },
132  output_it);
133 }
134 
135 void NEGatherKernel::configure(const ITensor *input, const ITensor *indices, ITensor *output, int axis)
136 {
137  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, indices);
138  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), indices->info(), output->info(), axis));
139 
140  _input = input;
141  _indices = indices;
142  _output = output;
143  _axis = axis;
144 
145  if(_axis < 0)
146  {
147  _axis += input->info()->num_dimensions();
148  }
149  ARM_COMPUTE_ERROR_ON(0 > _axis || _axis >= static_cast<int32_t>(input->info()->num_dimensions()));
150 
151  if(0 == _axis)
152  {
153  switch(_indices->info()->data_type())
154  {
155  case DataType::U32:
156  _func = &NEGatherKernel::gather_0_axis<uint32_t>;
157  break;
158  case DataType::S32:
159  _func = &NEGatherKernel::gather_0_axis<int32_t>;
160  break;
161  default:
162  ARM_COMPUTE_ERROR("Not supported");
163  break;
164  }
165  }
166  else
167  {
168  switch(_indices->info()->data_type())
169  {
170  case DataType::U32:
171  _func = &NEGatherKernel::gather_n_axis<uint32_t>;
172  break;
173  case DataType::S32:
174  _func = &NEGatherKernel::gather_n_axis<int32_t>;
175  break;
176  default:
177  ARM_COMPUTE_ERROR("Not supported");
178  break;
179  }
180  }
181  // Output auto initialization if not yet initialized
183  auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
184 
185  // Create window
186  Window win = calculate_max_window(*output->info(), Steps());
187  output->info()->set_valid_region(ValidRegion(Coordinates(), output->info()->tensor_shape()));
188 
189  INEKernel::configure(win);
190 }
191 
192 Status NEGatherKernel::validate(const ITensorInfo *input, const ITensorInfo *indices, const ITensorInfo *output, int axis)
193 {
194  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, indices, output, axis));
195  return Status{};
196 }
197 
198 void NEGatherKernel::run(const Window &window, const ThreadInfo &info)
199 {
200  ARM_COMPUTE_UNUSED(info);
202  ARM_COMPUTE_ERROR_ON(_func == nullptr);
203 
204  (this->*_func)(window, info);
205 }
206 
207 } // 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:610
#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 Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
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:163
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:941
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:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:792
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
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
Container for valid region of a window.
Definition: Types.h:188
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)