blob: 69bfd56ce0d4bedee5c19de2ff3e23f882f1bc03 [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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010030#include "src/core/CPP/Validate.h"
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020031#include "src/core/common/Registrars.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010032#include "src/core/helpers/AutoConfiguration.h"
33#include "src/core/helpers/WindowHelpers.h"
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020034#include "src/cpu/kernels/boundingboxtransform/list.h"
Pablo Telloed0e35b2019-08-30 14:44:52 +010035
36#include <arm_neon.h>
37
38namespace arm_compute
39{
40namespace
41{
Dana Zlotnik3475ffe2022-01-03 14:37:10 +020042struct BoundingBoxTransformSelectorData
43{
44 DataType dt;
45};
46
47using BoundingBoxTransformSelctorPtr = std::add_pointer<bool(const BoundingBoxTransformSelectorData &data)>::type;
48using BoundingBoxTransformUKernelPtr = std::add_pointer<void(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, BoundingBoxTransformInfo bbinfo, const Window &window)>::type;
49
50struct BoundingBoxTransformKernel
51{
52 const char *name;
53 const BoundingBoxTransformSelctorPtr is_selected;
54 BoundingBoxTransformUKernelPtr ukernel;
55};
56
57static const BoundingBoxTransformKernel available_kernels[] =
58{
59 {
60 "fp32_neon_boundingboxtransform",
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",
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",
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 */
86const 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
Pablo Telloed0e35b2019-08-30 14:44:52 +010098Status 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);
Pablo Telloffd31de2019-09-04 13:38:14 +0100102 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);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100104 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);
Pablo Telloffd31de2019-09-04 13:38:14 +0100109 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 }
Pablo Telloed0e35b2019-08-30 14:44:52 +0100122
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);
Pablo Telloffd31de2019-09-04 13:38:14 +0100128 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 }
Pablo Telloed0e35b2019-08-30 14:44:52 +0100134 }
Pablo Telloffd31de2019-09-04 13:38:14 +0100135
Pablo Telloed0e35b2019-08-30 14:44:52 +0100136 return Status{};
137}
Pablo Telloed0e35b2019-08-30 14:44:52 +0100138} // namespace
139
140NEBoundingBoxTransformKernel::NEBoundingBoxTransformKernel()
141 : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr), _bbinfo(0, 0, 0)
142{
143}
144
145void NEBoundingBoxTransformKernel::configure(const ITensor *boxes, ITensor *pred_boxes, const ITensor *deltas, const BoundingBoxTransformInfo &info)
146{
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000147 ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100148 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
149
150 // Configure kernel window
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000151 auto_init_if_empty(*pred_boxes->info(), deltas->info()->clone()->set_data_type(boxes->info()->data_type()).set_quantization_info(boxes->info()->quantization_info()));
Pablo Telloed0e35b2019-08-30 14:44:52 +0100152
153 // Set instance variables
154 _boxes = boxes;
155 _pred_boxes = pred_boxes;
156 _deltas = deltas;
157 _bbinfo = info;
158
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000159 const unsigned int num_boxes = boxes->info()->dimension(1);
160 Window win = calculate_max_window(*pred_boxes->info(), Steps());
Michalis Spyrou3bb75d62020-03-23 10:53:11 +0000161 win.set(Window::DimX, Window::Dimension(0, 1u));
162 win.set(Window::DimY, Window::Dimension(0, num_boxes));
163
164 INEKernel::configure(win);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100165}
166
167Status 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
Pablo Telloed0e35b2019-08-30 14:44:52 +0100173void NEBoundingBoxTransformKernel::run(const Window &window, const ThreadInfo &info)
174{
Michalis Spyrou6bff1952019-10-02 17:22:11 +0100175 ARM_COMPUTE_UNUSED(info);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100176 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
177 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
Dana Zlotnik3475ffe2022-01-03 14:37:10 +0200178
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);
Pablo Telloed0e35b2019-08-30 14:44:52 +0100183}
184} // namespace arm_compute