Compute Library
 22.11
NECropKernel.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 
28 #include "arm_compute/core/Types.h"
32 #include "src/core/CPP/Validate.h"
39 
40 namespace arm_compute
41 {
42 namespace
43 {
44 struct CropSelectorData
45 {
47 };
48 
51 
52 struct CropUKernel
53 {
54  const char *name;
55  const CropSelectorPtr is_selected;
56  CropUKernelPtr ukernel;
57 };
58 
59 static const CropUKernel available_kernels[] =
60 {
61  {
62  "fp16_neon_crop",
63  [](const CropSelectorData & data) { return data.dt == DataType::F16; },
65  },
66  {
67  "f32_neon_crop",
68  [](const CropSelectorData & data) { return data.dt == DataType::F32; },
70  },
71  {
72  "u8_neon_crop",
73  [](const CropSelectorData & data) { return data.dt == DataType::U8; },
75  },
76  {
77  "u16_neon_crop",
78  [](const CropSelectorData & data) { return data.dt == DataType::U16; },
80  },
81  {
82  "u32_neon_crop",
83  [](const CropSelectorData & data) { return data.dt == DataType::U32; },
85  },
86  {
87  "s8_neon_crop",
88  [](const CropSelectorData & data) { return data.dt == DataType::S8; },
90  },
91  {
92  "s16_neon_crop",
93  [](const CropSelectorData & data) { return data.dt == DataType::S16; },
95  },
96  {
97  "s32_neon_crop",
98  [](const CropSelectorData & data) { return data.dt == DataType::S32; },
100  },
101 };
102 
103 /** Micro-kernel selector
104  *
105  * @param[in] data Selection data passed to help pick the appropriate micro-kernel
106  *
107  * @return A matching micro-kernel else nullptr
108  */
109 const CropUKernel *get_implementation(const CropSelectorData &data)
110 {
111  for(const auto &uk : available_kernels)
112  {
113  if(uk.is_selected(data))
114  {
115  return &uk;
116  }
117  }
118 
119  return nullptr;
120 }
121 
122 inline void out_of_bounds_crop_window(const ITensor *output, float *output_ptr, float extrapolation_value,
123  int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit)
124 {
125  auto in = wrapper::vdup_n(extrapolation_value, wrapper::traits::vector_128_tag());
126  int32_t x = 0;
127  int32_t limit = (output_width_limit - output_width_start) * static_cast<int32_t>(output->info()->dimension(0));
128  float *output_start_ptr = output_ptr + output_width_start * output->info()->dimension(0);
129  for(; x <= limit - window_step_x; x += window_step_x)
130  {
131  wrapper::vstore(output_start_ptr + x, in);
132  }
133  for(; x < limit; ++x)
134  {
135  *(output_start_ptr + x) = extrapolation_value;
136  }
137 }
138 
139 inline void execute_window(const ITensor *input, const ITensor *output, Coordinates input_offset, float extrapolation_value,
140  const std::array<uint32_t, 2> &rows_out_of_bounds, const std::array<uint32_t, 2> &cols_out_of_bounds, NECropKernel::InBoundsCropFunction *in_bounds_crop_function,
141  bool is_height_flipped, bool has_cols_in_bounds, bool has_cols_out_of_bounds_before, bool has_cols_out_of_bounds_after, bool input_has_single_channel, bool is_width_flipped)
142 {
143  // Output is always float.
144  const int window_step_x = 16 / sizeof(float);
145  auto *output_ptr = reinterpret_cast<float *>(output->buffer());
146  // Output window:
147  // --------------------------------
148  // | Out of bounds |
149  // | rows before |
150  // |------------------------------|
151  // | Out of | In | Out of |
152  // | bounds | bounds | bounds |
153  // | cols | elements | cols |
154  // | before | copied | after |
155  // | | from input | |
156  // --------------------------------
157  // | Out of bounds |
158  // | rows after |
159  // |------------------------------|
160  // Fill all output rows that have no elements that are within the input bounds with the extrapolation value.
161  // First for the rows before the in bounds rows.
162  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, 0, rows_out_of_bounds[0] * output->info()->dimension(1));
163  output_ptr += rows_out_of_bounds[0] * output->info()->dimension(1) * output->info()->dimension(0);
164  // Iterate through each row that has any elements within the input bounds.
165  for(uint32_t row = rows_out_of_bounds[0]; static_cast<int32_t>(row) < static_cast<int32_t>(output->info()->dimension(2) - rows_out_of_bounds[1]);
166  ++row, is_height_flipped ? --input_offset[2] : ++input_offset[2])
167  {
168  // Fill all elements in the row that are out of bounds with the extrapolation value.
169  // First for the elements before the in bounds elements.
170  if(has_cols_out_of_bounds_before)
171  {
172  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, 0, cols_out_of_bounds[0]);
173  }
174  // Copy all elements within the input bounds from the input tensor.
175  if(has_cols_in_bounds)
176  {
177  (*in_bounds_crop_function)(input, output, output_ptr, input_offset, window_step_x, cols_out_of_bounds[0],
178  output->info()->dimension(1) - cols_out_of_bounds[1], input_has_single_channel, is_width_flipped);
179  }
180  // Fill all elements after the in bounds elements with the extrapolation value.
181  if(has_cols_out_of_bounds_after)
182  {
183  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, output->info()->dimension(1) - cols_out_of_bounds[1], output->info()->dimension(1));
184  }
185  output_ptr += output->info()->dimension(1) * output->info()->dimension(0);
186  }
187  // Fill all rows after the in bounds elements with the extrapolation value.
188  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, 0, rows_out_of_bounds[1] * output->info()->dimension(1));
189 }
190 } // namespace
191 
193  : _input(nullptr), _crop_boxes(nullptr), _box_ind(nullptr), _output(nullptr), _start(), _end(), _crop_box_ind(0), _extrapolation_value(0), _rows_out_of_bounds(), _cols_out_of_bounds()
194 {
195 }
196 
197 void NECropKernel::configure(const ITensor *input, const ITensor *crop_boxes, const ITensor *box_ind, ITensor *output, uint32_t crop_box_ind, float extrapolation_value)
198 {
199  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
200  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), crop_boxes->info(), box_ind->info(), output->info(), crop_box_ind, extrapolation_value));
201 
202  _input = input;
203  _crop_boxes = crop_boxes;
204  _box_ind = box_ind;
205  _output = output;
206  _crop_box_ind = crop_box_ind;
207  _extrapolation_value = extrapolation_value;
208 }
209 
210 Status NECropKernel::validate(const ITensorInfo *input, const ITensorInfo *crop_boxes, const ITensorInfo *box_ind, const ITensorInfo *output, uint32_t crop_box_ind, float extrapolation_value)
211 {
212  ARM_COMPUTE_UNUSED(extrapolation_value);
213  const auto *uk = get_implementation(CropSelectorData{ input->data_type() });
214  ARM_COMPUTE_RETURN_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
215 
220  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[0] != 4);
221  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
222  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] <= crop_box_ind);
223  ARM_COMPUTE_RETURN_ERROR_ON(box_ind->tensor_shape()[0] <= crop_box_ind);
224  if(output->total_size() > 0)
225  {
230  }
231  return Status{};
232 }
233 
235 {
236  // _crop_box_ind is used to index _crop_boxes and retrieve the appropriate crop box.
237  // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
238  const float x0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(1, _crop_box_ind)));
239  const float y0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(0, _crop_box_ind)));
240  const float x1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(3, _crop_box_ind)));
241  const float y1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(2, _crop_box_ind)));
242  // The normalized coordiantes are scaled to retrieve the floating point image coordinates which are rounded to integers.
243  _start = Coordinates(std::floor(x0 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
244  std::floor(y0 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
245  _end = Coordinates(std::floor(x1 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
246  std::floor(y1 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
247  const TensorShape out_shape(_input->info()->tensor_shape()[0], abs(_end[0] - _start[0]) + 1, abs(_end[1] - _start[1]) + 1);
248  _output->info()->set_tensor_shape(out_shape);
249 
250  bool is_width_flipped = _end[0] < _start[0];
251  bool is_height_flipped = _end[1] < _start[1];
252  if(is_height_flipped)
253  {
254  _rows_out_of_bounds[0] = _start[1] >= static_cast<int32_t>(_input->info()->dimension(2)) ? std::min(static_cast<uint32_t>(_start[1] - _input->info()->dimension(2) + 1),
255  static_cast<uint32_t>(_output->info()->dimension(2))) :
256  0;
257  _rows_out_of_bounds[1] = _end[1] < 0 ? std::min(static_cast<uint32_t>(-_end[1]),
258  static_cast<uint32_t>(_output->info()->dimension(2))) :
259  0;
260  }
261  else
262  {
263  _rows_out_of_bounds[0] = _start[1] < 0 ? std::min(static_cast<uint32_t>(-_start[1]),
264  static_cast<uint32_t>(_output->info()->dimension(2))) :
265  0;
266  _rows_out_of_bounds[1] = _end[1] >= static_cast<int32_t>(_input->info()->dimension(2)) ? std::min(static_cast<uint32_t>(_end[1] - _input->info()->dimension(2) + 1),
267  static_cast<uint32_t>(_output->info()->dimension(2))) :
268  0;
269  }
270  if(is_width_flipped)
271  {
272  _cols_out_of_bounds[0] = _start[0] >= static_cast<int32_t>(_input->info()->dimension(1)) ? std::min(static_cast<uint32_t>(_start[0] - _input->info()->dimension(1) + 1),
273  static_cast<uint32_t>(_output->info()->dimension(1))) :
274  0;
275  _cols_out_of_bounds[1] = _end[0] < 0 ? std::min(static_cast<uint32_t>(-_end[0]),
276  static_cast<uint32_t>(_output->info()->dimension(1))) :
277  0;
278  }
279  else
280  {
281  _cols_out_of_bounds[0] = _start[0] < 0 ? std::min(static_cast<uint32_t>(-_start[0]),
282  static_cast<uint32_t>(_output->info()->dimension(1))) :
283  0;
284  _cols_out_of_bounds[1] = _end[0] >= static_cast<int32_t>(_input->info()->dimension(1)) ? std::min(static_cast<uint32_t>(_end[0] - _input->info()->dimension(1) + 1),
285  static_cast<uint32_t>(_output->info()->dimension(1))) :
286  0;
287  }
288 
289  INEKernel::configure(calculate_max_window(*_output->info()));
290 }
291 
293 {
294  ARM_COMPUTE_UNUSED(window, info);
297 
298  ARM_COMPUTE_ERROR_ON(_input->info()->has_padding());
299  ARM_COMPUTE_ERROR_ON(_output->info()->has_padding());
300 
301  const auto *uk = get_implementation(CropSelectorData{ _input->info()->data_type() });
302 
303  uint32_t batch_index = *(reinterpret_cast<int32_t *>(_box_ind->ptr_to_element(Coordinates(_crop_box_ind))));
304  Coordinates input_offset(0, _end[0] < _start[0] ? _start[0] - _cols_out_of_bounds[0] : _start[0] + _cols_out_of_bounds[0],
305  _end[1] < _start[1] ? _start[1] - _rows_out_of_bounds[0] : _start[1] + _rows_out_of_bounds[0], batch_index);
306  execute_window(_input, _output, input_offset, _extrapolation_value, _rows_out_of_bounds, _cols_out_of_bounds, uk->ukernel, _end[1] < _start[1],
307  _cols_out_of_bounds[0] + _cols_out_of_bounds[1] < _output->info()->dimension(1), _cols_out_of_bounds[0] > 0, _cols_out_of_bounds[1] > 0,
308  _start[0] <= _end[0], _end[0] < _start[0]);
309 }
310 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
DataType dt
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:742
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:490
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:115
#define REGISTER_FP16_NEON(func_name)
Definition: Registrars.h:48
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
const char * name
void(const ITensor *, const ITensor *, float *, Coordinates, int32_t, int32_t, int32_t, bool, bool) InBoundsCropFunction
Function to use for in bounds crop for the particular tensor types passed to configure() ...
Definition: NECropKernel.h:94
virtual ITensorInfo & set_tensor_shape(const TensorShape &shape)=0
Set the shape of an already initialized tensor.
1 channel, 1 U8 per channel
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
virtual DataType data_type() const =0
Data type used for each element of the tensor.
1 channel, 1 F32 per channel
#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
1 channel, 1 U16 per channel
void fp16_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Status class.
Definition: Error.h:52
void configure(const ITensor *input, const ITensor *crop_boxes, const ITensor *box_ind, ITensor *output, uint32_t crop_box_ind=0, float extrapolation_value=0)
Configure kernel.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
#define REGISTER_INTEGER_NEON(func_name)
Definition: Registrars.h:171
const CropSelectorPtr is_selected
1 channel, 1 S32 per channel
void s8_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: integer.cpp:53
#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.
void s16_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: integer.cpp:60
Coordinates of an item.
Definition: Coordinates.h:37
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
void u16_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: integer.cpp:39
void configure_output_shape()
Configure output tensor&#39;s shape as this can only be determined at runtime.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
static Status validate(const ITensorInfo *input, const ITensorInfo *crop_boxes, const ITensorInfo *box_ind, const ITensorInfo *output, uint32_t crop_box_ind=0, float extrapolation_value=0)
Static function to check if given info will lead to a valid configuration of CLStridedSliceKernel.
CropUKernelPtr ukernel
void u8_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: integer.cpp:32
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
1 channel, 1 S16 per channel
void s32_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: integer.cpp:67
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:179
virtual size_t total_size() const =0
Returns the total size of the tensor in bytes.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
Num samples, height, width, channels.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
Includes all wrapper headers at once.
void u32_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: integer.cpp:46
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
DataType
Available data types.
Definition: Types.h:79
void fp32_in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset, int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
Definition: fp32.cpp:31
signed 8-bit number
Describe a multidimensional execution window.
Definition: Window.h:39
virtual bool has_padding() const =0
Checks if the tensor has been allocated with padding or not.
NECropKernel()
Default constructor.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201