blob: cb869838e275e1d1e8b5104bb1f7b8b92ae9b457 [file] [log] [blame]
Pablo Telloed0e35b2019-08-30 14:44:52 +01001/*
Dana Zlotnik3475ffe2022-01-03 14:37:10 +02002 * Copyright (c) 2019-2022 Arm Limited.
Pablo Telloed0e35b2019-08-30 14:44:52 +01003 *
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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEBoundingBoxTransformKernel.h"
Pablo Telloed0e35b2019-08-30 14:44:52 +010025
Pablo Telloed0e35b2019-08-30 14:44:52 +010026#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"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010030
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020031#include "src/core/common/Registrars.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "src/core/CPP/Validate.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010033#include "src/core/helpers/AutoConfiguration.h"
34#include "src/core/helpers/WindowHelpers.h"
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020035#include "src/cpu/kernels/boundingboxtransform/list.h"
Pablo Telloed0e35b2019-08-30 14:44:52 +010036
37#include <arm_neon.h>
38
39namespace arm_compute
40{
41namespace
42{
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020043struct BoundingBoxTransformSelectorData
44{
45 DataType dt;
46};
47
48using BoundingBoxTransformSelctorPtr = std::add_pointer<bool(const BoundingBoxTransformSelectorData &data)>::type;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010049using BoundingBoxTransformUKernelPtr = std::add_pointer<void(const ITensor *boxes,
50 ITensor *pred_boxes,
51 const ITensor *deltas,
52 BoundingBoxTransformInfo bbinfo,
53 const Window &window)>::type;
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020054
55struct BoundingBoxTransformKernel
56{
57 const char *name;
58 const BoundingBoxTransformSelctorPtr is_selected;
59 BoundingBoxTransformUKernelPtr ukernel;
60};
61
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010062static const BoundingBoxTransformKernel available_kernels[] = {
63 {"fp32_neon_boundingboxtransform",
64 [](const BoundingBoxTransformSelectorData &data) { return data.dt == DataType::F32; },
65 REGISTER_FP32_NEON(arm_compute::cpu::neon_fp32_boundingboxtransform)},
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020066#ifdef __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010067 {"fp16_neon_boundingboxtransform",
68 [](const BoundingBoxTransformSelectorData &data) { return data.dt == DataType::F16; },
69 REGISTER_FP16_NEON(arm_compute::cpu::neon_fp16_boundingboxtransform)},
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020070#endif // __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
71#if defined(ARM_COMPUTE_ENABLE_NEON)
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 {"qu16_neon_boundingboxtransform",
73 [](const BoundingBoxTransformSelectorData &data) { return data.dt == DataType::QASYMM16; },
74 REGISTER_QSYMM16_NEON(arm_compute::cpu::neon_qu16_boundingboxtransform)},
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020075#endif //defined(ARM_COMPUTE_ENABLE_NEON)
76};
77
78/** Micro-kernel selector
79 *
80 * @param[in] data Selection data passed to help pick the appropriate micro-kernel
81 *
82 * @return A matching micro-kernel else nullptr
83 */
84const BoundingBoxTransformKernel *get_implementation(const BoundingBoxTransformSelectorData &data)
85{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010086 for (const auto &uk : available_kernels)
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020087 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 if (uk.is_selected(data))
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020089 {
90 return &uk;
91 }
92 }
93 return nullptr;
94}
95
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010096Status validate_arguments(const ITensorInfo *boxes,
97 const ITensorInfo *pred_boxes,
98 const ITensorInfo *deltas,
99 const BoundingBoxTransformInfo &info)
Pablo Telloed0e35b2019-08-30 14:44:52 +0100100{
101 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
102 ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(boxes);
Pablo Telloffd31de2019-09-04 13:38:14 +0100103 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::QASYMM16, DataType::F32, DataType::F16);
104 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8, DataType::F32, DataType::F16);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100105 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
106 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
107 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
108 ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
109 ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
Pablo Telloffd31de2019-09-04 13:38:14 +0100110 ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
111
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100112 if (boxes->data_type() == DataType::QASYMM16)
Pablo Telloffd31de2019-09-04 13:38:14 +0100113 {
114 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(deltas, 1, DataType::QASYMM8);
115 const UniformQuantizationInfo deltas_qinfo = deltas->quantization_info().uniform();
116 ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.scale != 0.125f);
117 ARM_COMPUTE_RETURN_ERROR_ON(deltas_qinfo.offset != 0);
118 }
119 else
120 {
121 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(boxes, deltas);
122 }
Pablo Telloed0e35b2019-08-30 14:44:52 +0100123
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100124 if (pred_boxes->total_size() > 0)
Pablo Telloed0e35b2019-08-30 14:44:52 +0100125 {
126 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
127 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, deltas);
128 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 if (pred_boxes->data_type() == DataType::QASYMM16)
Pablo Telloffd31de2019-09-04 13:38:14 +0100130 {
131 const UniformQuantizationInfo pred_qinfo = pred_boxes->quantization_info().uniform();
132 ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.scale != 0.125f);
133 ARM_COMPUTE_RETURN_ERROR_ON(pred_qinfo.offset != 0);
134 }
Pablo Telloed0e35b2019-08-30 14:44:52 +0100135 }
Pablo Telloffd31de2019-09-04 13:38:14 +0100136
Pablo Telloed0e35b2019-08-30 14:44:52 +0100137 return Status{};
138}
Pablo Telloed0e35b2019-08-30 14:44:52 +0100139} // namespace
140
141NEBoundingBoxTransformKernel::NEBoundingBoxTransformKernel()
142 : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr), _bbinfo(0, 0, 0)
143{
144}
145
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100146void NEBoundingBoxTransformKernel::configure(const ITensor *boxes,
147 ITensor *pred_boxes,
148 const ITensor *deltas,
149 const BoundingBoxTransformInfo &info)
Pablo Telloed0e35b2019-08-30 14:44:52 +0100150{
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000151 ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100152 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
153
154 // Configure kernel window
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100155 auto_init_if_empty(*pred_boxes->info(), deltas->info()
156 ->clone()
157 ->set_data_type(boxes->info()->data_type())
158 .set_quantization_info(boxes->info()->quantization_info()));
Pablo Telloed0e35b2019-08-30 14:44:52 +0100159
160 // Set instance variables
161 _boxes = boxes;
162 _pred_boxes = pred_boxes;
163 _deltas = deltas;
164 _bbinfo = info;
165
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000166 const unsigned int num_boxes = boxes->info()->dimension(1);
167 Window win = calculate_max_window(*pred_boxes->info(), Steps());
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000168 win.set(Window::DimX, Window::Dimension(0, 1u));
169 win.set(Window::DimY, Window::Dimension(0, num_boxes));
170
171 INEKernel::configure(win);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100172}
173
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100174Status NEBoundingBoxTransformKernel::validate(const ITensorInfo *boxes,
175 const ITensorInfo *pred_boxes,
176 const ITensorInfo *deltas,
177 const BoundingBoxTransformInfo &info)
Pablo Telloed0e35b2019-08-30 14:44:52 +0100178{
179 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
180 return Status{};
181}
182
Pablo Telloed0e35b2019-08-30 14:44:52 +0100183void NEBoundingBoxTransformKernel::run(const Window &window, const ThreadInfo &info)
184{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100185 ARM_COMPUTE_UNUSED(info);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100186 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
187 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Dana Zlotnik3475ffe2022-01-03 14:37:10 +0200188
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100189 const auto *uk = get_implementation(BoundingBoxTransformSelectorData{_boxes->info()->data_type()});
Dana Zlotnik3475ffe2022-01-03 14:37:10 +0200190 ARM_COMPUTE_ERROR_ON(uk == nullptr || uk->ukernel == nullptr);
191
192 uk->ukernel(_boxes, _pred_boxes, _deltas, _bbinfo, window);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100193}
194} // namespace arm_compute