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 */
24 #include "src/core/NEON/kernels/NEBoundingBoxTransformKernel.h"
25
26 #include "arm_compute/core/Helpers.h"
27 #include "arm_compute/core/TensorInfo.h"
28 #include "arm_compute/core/Utils.h"
29 #include "arm_compute/core/Window.h"
30 #include "src/core/CPP/Validate.h"
31 #include "src/core/common/Registrars.h"
32 #include "src/core/helpers/AutoConfiguration.h"
33 #include "src/core/helpers/WindowHelpers.h"
34 #include "src/cpu/kernels/boundingboxtransform/list.h"
35
36 #include <arm_neon.h>
37
38 namespace arm_compute
39 {
40 namespace
41 {
42 struct BoundingBoxTransformSelectorData
43 {
44 DataType dt;
45 };
46
47 using BoundingBoxTransformSelctorPtr = std::add_pointer<bool(const BoundingBoxTransformSelectorData &data)>::type;
48 using BoundingBoxTransformUKernelPtr = std::add_pointer<void(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)>::type;
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",
__anon26878c590202() 61 [](const BoundingBoxTransformSelectorData & data) { return data.dt == DataType::F32; },
62 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_boundingboxtransform)
63 },
64 #ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
65 {
66 "fp16_neon_boundingboxtransform",
__anon26878c590302() 67 [](const BoundingBoxTransformSelectorData & data) { return data.dt == DataType::F16; },
68 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_boundingboxtransform)
69 },
70 #endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
71 #if defined(ARM_COMPUTE_ENABLE_NEON)
72 {
73 "qu16_neon_boundingboxtransform",
__anon26878c590402() 74 [](const BoundingBoxTransformSelectorData & data) { return data.dt == DataType::QASYMM16; },
75 REGISTER_QSYMM16_NEON(arm_compute::cpu::neon_qu16_boundingboxtransform)
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 */
get_implementation(const BoundingBoxTransformSelectorData & data)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
validate_arguments(const ITensorInfo * boxes,const ITensorInfo * pred_boxes,const ITensorInfo * deltas,const BoundingBoxTransformInfo & info)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);
101 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(boxes);
102 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::QASYMM16, DataType::F32, DataType::F16);
103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8, DataType::F32, DataType::F16);
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 {
113 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(deltas, 1, DataType::QASYMM8);
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 {
120 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(boxes, deltas);
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());
126 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, deltas);
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
NEBoundingBoxTransformKernel()140 NEBoundingBoxTransformKernel::NEBoundingBoxTransformKernel()
141 : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr), _bbinfo(0, 0, 0)
142 {
143 }
144
configure(const ITensor * boxes,ITensor * pred_boxes,const ITensor * deltas,const BoundingBoxTransformInfo & info)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
validate(const ITensorInfo * boxes,const ITensorInfo * pred_boxes,const ITensorInfo * deltas,const BoundingBoxTransformInfo & info)167 Status NEBoundingBoxTransformKernel::validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
168 {
169 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
170 return Status{};
171 }
172
run(const Window & window,const ThreadInfo & info)173 void NEBoundingBoxTransformKernel::run(const Window &window, const ThreadInfo &info)
174 {
175 ARM_COMPUTE_UNUSED(info);
176 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
177 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
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
185