Compute Library
 21.02
NECropKernel.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 
29 #include "arm_compute/core/Types.h"
33 #include "src/core/CPP/Validate.h"
38 
39 namespace arm_compute
40 {
41 namespace
42 {
43 template <typename T>
44 inline float32x4_t load_as_f32(T *ptr)
45 {
46  ARM_COMPUTE_UNUSED(ptr);
47  ARM_COMPUTE_ERROR("Type not supported.");
48 }
49 
50 template <>
51 inline float32x4_t load_as_f32(float *ptr)
52 {
53  return wrapper::vloadq(ptr);
54 }
55 
56 template <>
57 inline float32x4_t load_as_f32(int32_t *ptr)
58 {
59  return vcvtq_f32_s32(wrapper::vloadq(ptr));
60 }
61 
62 template <>
63 inline float32x4_t load_as_f32(uint32_t *ptr)
64 {
65  return vcvtq_f32_u32(wrapper::vloadq(ptr));
66 }
67 
68 template <>
69 inline float32x4_t load_as_f32(int16_t *ptr)
70 {
71  return vcvtq_f32_s32(vmovl_s16(wrapper::vload(ptr)));
72 }
73 
74 template <>
75 inline float32x4_t load_as_f32(uint16_t *ptr)
76 {
77  return vcvtq_f32_u32(vmovl_u16(wrapper::vload(ptr)));
78 }
79 
80 template <>
81 inline float32x4_t load_as_f32(uint8_t *ptr)
82 {
83  return vcvtq_f32_u32(vmovl_u16(vget_low_u16(vmovl_u8(wrapper::vload(ptr)))));
84 }
85 
86 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
87 template <>
88 inline float32x4_t load_as_f32(float16_t *ptr)
89 {
90  return vcvt_f32_f16(wrapper::vload(ptr));
91 }
92 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
93 
94 template <typename T>
95 inline void in_bounds_crop_window(const ITensor *input, const ITensor *output, float *output_ptr, Coordinates input_offset,
96  int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit, bool input_has_single_channel, bool is_width_flipped)
97 {
98  // Reverse elements if width flipped.
99  if(is_width_flipped)
100  {
101  // Collapse first dimension if possible.
102  if(input_has_single_channel)
103  {
104  int32_t x = output_width_start;
105  Coordinates negative_offset(input_offset);
106  negative_offset.set(1, negative_offset[1] - window_step_x + 1);
107  for(; x <= output_width_limit - window_step_x; x += window_step_x, negative_offset[1] -= window_step_x)
108  {
109  auto in = load_as_f32(reinterpret_cast<T *>(input->ptr_to_element(negative_offset)));
110 
111  in = wrapper::vrev64(in);
113 
114  wrapper::vstore(output_ptr + x, in);
115  }
116  input_offset[1] = negative_offset[1] + window_step_x - 1;
117  for(; x < output_width_limit; ++x, --input_offset[1])
118  {
119  *(output_ptr + x) = static_cast<float>(*reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
120  }
121  }
122  else
123  {
124  for(int32_t x = output_width_start; x < output_width_limit; ++x, --input_offset[1])
125  {
126  input_offset.set(0, 0);
127  int32_t c = 0;
128  for(; c <= static_cast<int32_t>(input->info()->dimension(0)) - window_step_x; c += window_step_x, input_offset[0] += window_step_x)
129  {
130  auto in = load_as_f32(reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
131  wrapper::vstore(output_ptr + x * output->info()->dimension(0) + c, in);
132  }
133  for(; c < static_cast<int32_t>(input->info()->dimension(0)); ++c, ++input_offset[0])
134  {
135  *(output_ptr + x * output->info()->dimension(0) + c) = static_cast<float>(*reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
136  }
137  }
138  }
139  }
140  else
141  {
142  // Use memcpy if the elements don't need converting to float.
143  if(std::is_same<T, float>::value)
144  {
145  memcpy(static_cast<void *>(output_ptr + output_width_start * output->info()->dimension(0)),
146  reinterpret_cast<const void *>(input->ptr_to_element(input_offset)),
147  (output_width_limit - output_width_start) * output->info()->dimension(0) * output->info()->element_size());
148  }
149  else
150  {
151  int32_t x = 0;
152  int32_t limit = (output_width_limit - output_width_start) * static_cast<int32_t>(output->info()->dimension(0));
153  float *output_start_ptr = output_ptr + output_width_start * output->info()->dimension(0);
154  for(; x <= limit - window_step_x; x += window_step_x, input_offset[0] += window_step_x)
155  {
156  auto in = load_as_f32(reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
157  wrapper::vstore(output_start_ptr + x, in);
158  }
159  for(; x < limit; ++x, ++input_offset[0])
160  {
161  *(output_start_ptr + x) = static_cast<float>(*reinterpret_cast<T *>(input->ptr_to_element(input_offset)));
162  }
163  }
164  }
165 }
166 
167 inline void out_of_bounds_crop_window(const ITensor *output, float *output_ptr, float extrapolation_value,
168  int32_t window_step_x, int32_t output_width_start, int32_t output_width_limit)
169 {
170  auto in = wrapper::vdup_n(extrapolation_value, wrapper::traits::vector_128_tag());
171  int32_t x = 0;
172  int32_t limit = (output_width_limit - output_width_start) * static_cast<int32_t>(output->info()->dimension(0));
173  float *output_start_ptr = output_ptr + output_width_start * output->info()->dimension(0);
174  for(; x <= limit - window_step_x; x += window_step_x)
175  {
176  wrapper::vstore(output_start_ptr + x, in);
177  }
178  for(; x < limit; ++x)
179  {
180  *(output_start_ptr + x) = extrapolation_value;
181  }
182 }
183 
184 inline void execute_window(const ITensor *input, const ITensor *output, Coordinates input_offset, float extrapolation_value,
185  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,
186  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)
187 {
188  // Output is always float.
189  const int window_step_x = 16 / sizeof(float);
190  auto *output_ptr = reinterpret_cast<float *>(output->buffer());
191  // Output window:
192  // --------------------------------
193  // | Out of bounds |
194  // | rows before |
195  // |------------------------------|
196  // | Out of | In | Out of |
197  // | bounds | bounds | bounds |
198  // | cols | elements | cols |
199  // | before | copied | after |
200  // | | from input | |
201  // --------------------------------
202  // | Out of bounds |
203  // | rows after |
204  // |------------------------------|
205  // Fill all output rows that have no elements that are within the input bounds with the extrapolation value.
206  // First for the rows before the in bounds rows.
207  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, 0, rows_out_of_bounds[0] * output->info()->dimension(1));
208  output_ptr += rows_out_of_bounds[0] * output->info()->dimension(1) * output->info()->dimension(0);
209  // Iterate through each row that has any elements within the input bounds.
210  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]);
211  ++row, is_height_flipped ? --input_offset[2] : ++input_offset[2])
212  {
213  // Fill all elements in the row that are out of bounds with the extrapolation value.
214  // First for the elements before the in bounds elements.
215  if(has_cols_out_of_bounds_before)
216  {
217  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, 0, cols_out_of_bounds[0]);
218  }
219  // Copy all elements within the input bounds from the input tensor.
220  if(has_cols_in_bounds)
221  {
222  (*in_bounds_crop_function)(input, output, output_ptr, input_offset, window_step_x, cols_out_of_bounds[0],
223  output->info()->dimension(1) - cols_out_of_bounds[1], input_has_single_channel, is_width_flipped);
224  }
225  // Fill all elements after the in bounds elements with the extrapolation value.
226  if(has_cols_out_of_bounds_after)
227  {
228  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));
229  }
230  output_ptr += output->info()->dimension(1) * output->info()->dimension(0);
231  }
232  // Fill all rows after the in bounds elements with the extrapolation value.
233  out_of_bounds_crop_window(output, output_ptr, extrapolation_value, window_step_x, 0, rows_out_of_bounds[1] * output->info()->dimension(1));
234 }
235 } // namespace
236 
238  : _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(),
239  _in_bounds_crop_function(nullptr)
240 {
241 }
242 
243 void NECropKernel::configure(const ITensor *input, const ITensor *crop_boxes, const ITensor *box_ind, ITensor *output, uint32_t crop_box_ind, float extrapolation_value)
244 {
245  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
246  ARM_COMPUTE_ERROR_THROW_ON(validate(input->info(), crop_boxes->info(), box_ind->info(), output->info(), crop_box_ind, extrapolation_value));
247 
248  _input = input;
249  _crop_boxes = crop_boxes;
250  _box_ind = box_ind;
251  _output = output;
252  _crop_box_ind = crop_box_ind;
253  _extrapolation_value = extrapolation_value;
254 
255  switch(input->info()->data_type())
256  {
257  case DataType::F32:
258  _in_bounds_crop_function = &in_bounds_crop_window<float>;
259  break;
260 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
261  case DataType::F16:
262  _in_bounds_crop_function = &in_bounds_crop_window<float16_t>;
263  break;
264 #endif /* __ARM_FEATURE_FP16_VECTOR_ARITHMETIC */
265  case DataType::U32:
266  _in_bounds_crop_function = &in_bounds_crop_window<uint32_t>;
267  break;
268  case DataType::S32:
269  _in_bounds_crop_function = &in_bounds_crop_window<int32_t>;
270  break;
271  case DataType::U16:
272  _in_bounds_crop_function = &in_bounds_crop_window<uint16_t>;
273  break;
274  case DataType::S16:
275  _in_bounds_crop_function = &in_bounds_crop_window<int16_t>;
276  break;
277  case DataType::U8:
278  _in_bounds_crop_function = &in_bounds_crop_window<uint8_t>;
279  break;
280  default:
281  ARM_COMPUTE_ERROR("Datatype not supported");
282  }
283 }
284 
285 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)
286 {
287  ARM_COMPUTE_UNUSED(extrapolation_value);
292  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[0] != 4);
293  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] != box_ind->tensor_shape()[0]);
294  ARM_COMPUTE_RETURN_ERROR_ON(crop_boxes->tensor_shape()[1] <= crop_box_ind);
295  ARM_COMPUTE_RETURN_ERROR_ON(box_ind->tensor_shape()[0] <= crop_box_ind);
296  if(output->total_size() > 0)
297  {
302  }
303  return Status{};
304 }
305 
307 {
308  // _crop_box_ind is used to index _crop_boxes and retrieve the appropriate crop box.
309  // The crop box is specified by normalized coordinates [y0, x0, y1, x1].
310  const float x0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(1, _crop_box_ind)));
311  const float y0 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(0, _crop_box_ind)));
312  const float x1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(3, _crop_box_ind)));
313  const float y1 = *reinterpret_cast<const float *>(_crop_boxes->ptr_to_element(Coordinates(2, _crop_box_ind)));
314  // The normalized coordiantes are scaled to retrieve the floating point image coordinates which are rounded to integers.
315  _start = Coordinates(std::floor(x0 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
316  std::floor(y0 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
317  _end = Coordinates(std::floor(x1 * (_input->info()->tensor_shape()[1] - 1) + 0.5f),
318  std::floor(y1 * (_input->info()->tensor_shape()[2] - 1) + 0.5f));
319  const TensorShape out_shape(_input->info()->tensor_shape()[0], abs(_end[0] - _start[0]) + 1, abs(_end[1] - _start[1]) + 1);
320  _output->info()->set_tensor_shape(out_shape);
321 
322  bool is_width_flipped = _end[0] < _start[0];
323  bool is_height_flipped = _end[1] < _start[1];
324  if(is_height_flipped)
325  {
326  _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),
327  static_cast<uint32_t>(_output->info()->dimension(2))) :
328  0;
329  _rows_out_of_bounds[1] = _end[1] < 0 ? std::min(static_cast<uint32_t>(-_end[1]),
330  static_cast<uint32_t>(_output->info()->dimension(2))) :
331  0;
332  }
333  else
334  {
335  _rows_out_of_bounds[0] = _start[1] < 0 ? std::min(static_cast<uint32_t>(-_start[1]),
336  static_cast<uint32_t>(_output->info()->dimension(2))) :
337  0;
338  _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),
339  static_cast<uint32_t>(_output->info()->dimension(2))) :
340  0;
341  }
342  if(is_width_flipped)
343  {
344  _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),
345  static_cast<uint32_t>(_output->info()->dimension(1))) :
346  0;
347  _cols_out_of_bounds[1] = _end[0] < 0 ? std::min(static_cast<uint32_t>(-_end[0]),
348  static_cast<uint32_t>(_output->info()->dimension(1))) :
349  0;
350  }
351  else
352  {
353  _cols_out_of_bounds[0] = _start[0] < 0 ? std::min(static_cast<uint32_t>(-_start[0]),
354  static_cast<uint32_t>(_output->info()->dimension(1))) :
355  0;
356  _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),
357  static_cast<uint32_t>(_output->info()->dimension(1))) :
358  0;
359  }
360 
361  INEKernel::configure(calculate_max_window(*_output->info()));
362 }
363 
365 {
366  ARM_COMPUTE_UNUSED(window, info);
369 
370  ARM_COMPUTE_ERROR_ON(_input->info()->has_padding());
371  ARM_COMPUTE_ERROR_ON(_output->info()->has_padding());
372 
373  uint32_t batch_index = *(reinterpret_cast<int32_t *>(_box_ind->ptr_to_element(Coordinates(_crop_box_ind))));
374  Coordinates input_offset(0, _end[0] < _start[0] ? _start[0] - _cols_out_of_bounds[0] : _start[0] + _cols_out_of_bounds[0],
375  _end[1] < _start[1] ? _start[1] - _rows_out_of_bounds[0] : _start[1] + _rows_out_of_bounds[0], batch_index);
376  execute_window(_input, _output, input_offset, _extrapolation_value, _rows_out_of_bounds, _cols_out_of_bounds, _in_bounds_crop_function, _end[1] < _start[1],
377  _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,
378  _start[0] <= _end[0], _end[0] < _start[0]);
379 }
380 } // namespace arm_compute
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:746
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(tensor)
Definition: Validate.h:108
virtual size_t dimension(size_t index) const =0
Return the size of the requested dimension.
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
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
uint8x16_t vloadq(const uint8_t *ptr)
Definition: load.h:58
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
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
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 F16 per channel
1 channel, 1 S32 per channel
#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.
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.
uint8x8_t vgetlow(const uint8x16_t val)
Definition: getlow.h:39
void configure_output_shape()
Configure output tensor&#39;s shape as this can only be determined at runtime.
uint8x16_t vcombine(const uint8x8_t &a, const uint8x8_t &b)
Definition: combine.h:39
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.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
1 channel, 1 S16 per channel
uint8x8_t vgethigh(const uint8x16_t val)
Definition: gethigh.h:39
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
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:792
uint8x8_t vrev64(const uint8x8_t &a)
Definition: rev64.h:39
uint8x8_t vload(const uint8_t *ptr)
Definition: load.h:39
void vstore(uint8_t *ptr, uint8x8_t val)
Definition: store.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
Definition: dup_n.h:41
Includes all wrapper headers at once.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:694
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:205