Compute Library
 22.11
NEBoundingBoxTransformKernel.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/Utils.h"
30 #include "src/core/CPP/Validate.h"
35 
36 #include <arm_neon.h>
37 
38 namespace arm_compute
39 {
40 namespace
41 {
42 struct BoundingBoxTransformSelectorData
43 {
45 };
46 
49 
50 struct BoundingBoxTransformKernel
51 {
52  const char *name;
53  const BoundingBoxTransformSelctorPtr is_selected;
54  BoundingBoxTransformUKernelPtr ukernel;
55 };
56 
57 static const BoundingBoxTransformKernel available_kernels[] =
58 {
59  {
60  "fp32_neon_boundingboxtransform",
61  [](const BoundingBoxTransformSelectorData & data) { return data.dt == DataType::F32; },
63  },
64 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
65  {
66  "fp16_neon_boundingboxtransform",
67  [](const BoundingBoxTransformSelectorData & data) { return data.dt == DataType::F16; },
69  },
70 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
71 #if defined(ARM_COMPUTE_ENABLE_NEON)
72  {
73  "qu16_neon_boundingboxtransform",
74  [](const BoundingBoxTransformSelectorData & data) { return data.dt == DataType::QASYMM16; },
76  },
77 #endif //defined(ARM_COMPUTE_ENABLE_NEON)
78 };
79 
80 /** Micro-kernel selector
81  *
82  * @param[in] data Selection data passed to help pick the appropriate micro-kernel
83  *
84  * @return A matching micro-kernel else nullptr
85  */
86 const BoundingBoxTransformKernel *get_implementation(const BoundingBoxTransformSelectorData &data)
87 {
88  for(const auto &uk : available_kernels)
89  {
90  if(uk.is_selected(data))
91  {
92  return &uk;
93  }
94  }
95  return nullptr;
96 }
97 
98 Status validate_arguments(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
99 {
100  ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
104  ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
105  ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
106  ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
107  ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
108  ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
109  ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
110 
111  if(boxes->data_type() == DataType::QASYMM16)
112  {
114  const UniformQuantizationInfo deltas_qinfo = deltas->quantization_info().uniform();
115  ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.scale != 0.125f);
116  ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.offset != 0);
117  }
118  else
119  {
121  }
122 
123  if(pred_boxes->total_size() > 0)
124  {
125  ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
127  ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
128  if(pred_boxes->data_type() == DataType::QASYMM16)
129  {
130  const UniformQuantizationInfo pred_qinfo = pred_boxes->quantization_info().uniform();
131  ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.scale != 0.125f);
132  ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.offset != 0);
133  }
134  }
135 
136  return Status{};
137 }
138 } // namespace
139 
141  : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr), _bbinfo(0, 0, 0)
142 {
143 }
144 
145 void NEBoundingBoxTransformKernel::configure(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, const BoundingBoxTransformInfo &info)
146 {
147  ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
148  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
149 
150  // Configure kernel window
151  auto_init_if_empty(*pred_boxes->info(), deltas->info()->clone()->set_data_type(boxes->info()->data_type()).set_quantization_info(boxes->info()->quantization_info()));
152 
153  // Set instance variables
154  _boxes = boxes;
155  _pred_boxes = pred_boxes;
156  _deltas = deltas;
157  _bbinfo = info;
158 
159  const unsigned int num_boxes = boxes->info()->dimension(1);
160  Window win = calculate_max_window(*pred_boxes->info(), Steps());
161  win.set(Window::DimX, Window::Dimension(0, 1u));
162  win.set(Window::DimY, Window::Dimension(0, num_boxes));
163 
164  INEKernel::configure(win);
165 }
166 
168 {
169  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
170  return Status{};
171 }
172 
174 {
175  ARM_COMPUTE_UNUSED(info);
178 
179  const auto *uk = get_implementation(BoundingBoxTransformSelectorData{ _boxes->info()->data_type() });
180  ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
181 
182  uk->ukernel(_boxes, _pred_boxes, _deltas, _bbinfo, window);
183 }
184 } // namespace arm_compute
void neon_qu16_boundingboxtransform(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)
Definition: qsymm16.cpp:29
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
const char * name
#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.
#define REGISTER_FP32_NEON(func_name)
Definition: Registrars.h:74
#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.
1 channel, 1 F32 per channel
void configure(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, const BoundingBoxTransformInfo &info)
Set the input and output tensors.
#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
quantized, asymmetric fixed-point 16-bit number
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
decltype(strategy::transforms) typedef type
Interface for CPU tensor.
Definition: ITensor.h:36
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:284
Copyright (c) 2017-2022 Arm Limited.
1 channel, 1 F16 per channel
#define ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(...)
Definition: Validate.h:159
void neon_fp16_boundingboxtransform(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)
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
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
#define REGISTER_QSYMM16_NEON(func_name)
Definition: Registrars.h:139
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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.
void set(size_t dimension, const Dimension &dim)
Set the values of a given dimension.
Definition: Window.inl:49
Bounding Box Transform information class.
Definition: Types.h:1572
virtual QuantizationInfo quantization_info() const =0
Get the quantization settings (scale and offset) of the tensor.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
static Status validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
Static function to check if given info will lead to a valid configuration of CLBoundingBoxTransform.
BoundingBoxTransformUKernelPtr ukernel
const BoundingBoxTransformSelctorPtr is_selected
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
void neon_fp32_boundingboxtransform(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)
Definition: fp32.cpp:29
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
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(t,...)
Definition: Validate.h:690
DataType
Available data types.
Definition: Types.h:79
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201