blob: f32c518e29c1a95919eb798a948ca9f8ef5147eb [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2018-2021, 2023 Arm Limited.
giuros01c04a0e82018-10-03 12:44:35 +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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLBoundingBoxTransformKernel.h"
giuros01c04a0e82018-10-03 12:44:35 +010025
giuros01c04a0e82018-10-03 12:44:35 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
giuros01c04a0e82018-10-03 12:44:35 +010028#include "arm_compute/core/CL/ICLTensor.h"
29#include "arm_compute/core/CL/OpenCL.h"
30#include "arm_compute/core/Helpers.h"
31#include "arm_compute/core/TensorInfo.h"
32#include "arm_compute/core/Utils.h"
Matthew Bentham314d3e22023-06-23 10:53:52 +000033#include "arm_compute/core/utils/StringUtils.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010034
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
giuros01c04a0e82018-10-03 12:44:35 +010039
40namespace arm_compute
41{
42namespace
43{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010044Status validate_arguments(const ITensorInfo *boxes,
45 const ITensorInfo *pred_boxes,
46 const ITensorInfo *deltas,
47 const BoundingBoxTransformInfo &info)
giuros01c04a0e82018-10-03 12:44:35 +010048{
49 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000050 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(boxes);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010051 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::QASYMM16, DataType::F32, DataType::F16);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8, DataType::F32, DataType::F16);
giuros01c04a0e82018-10-03 12:44:35 +010053 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
54 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
55 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
56 ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
57 ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
58
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010059 const bool is_qasymm16 = boxes->data_type() == DataType::QASYMM16;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010060 if (is_qasymm16)
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010061 {
62 const UniformQuantizationInfo boxes_qinfo = boxes->quantization_info().uniform();
63 ARM_COMPUTE_RETURN_ERROR_ON(boxes_qinfo.scale != 0.125f);
64 ARM_COMPUTE_RETURN_ERROR_ON(boxes_qinfo.offset != 0);
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8);
66 }
Pablo Telloffd31de2019-09-04 13:38:14 +010067 else
68 {
69 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(boxes, deltas);
70 }
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010071
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010072 if (pred_boxes->total_size() > 0)
giuros01c04a0e82018-10-03 12:44:35 +010073 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010075 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, boxes);
giuros01c04a0e82018-10-03 12:44:35 +010076 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010077 if (is_qasymm16)
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010078 {
79 const UniformQuantizationInfo pred_boxes_qinfo = pred_boxes->quantization_info().uniform();
80 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes_qinfo.scale != 0.125f);
81 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes_qinfo.offset != 0);
82 }
giuros01c04a0e82018-10-03 12:44:35 +010083 }
giuros01d696cb62018-11-16 10:39:59 +000084 ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010085
giuros01c04a0e82018-10-03 12:44:35 +010086 return Status{};
87}
88} // namespace
89
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010090CLBoundingBoxTransformKernel::CLBoundingBoxTransformKernel() : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr)
giuros01c04a0e82018-10-03 12:44:35 +010091{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010092 _type = CLKernelType::ELEMENTWISE;
giuros01c04a0e82018-10-03 12:44:35 +010093}
94
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010095void CLBoundingBoxTransformKernel::configure(const ICLTensor *boxes,
96 ICLTensor *pred_boxes,
97 const ICLTensor *deltas,
98 const BoundingBoxTransformInfo &info)
giuros01c04a0e82018-10-03 12:44:35 +010099{
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100100 configure(CLKernelLibrary::get().get_compile_context(), boxes, pred_boxes, deltas, info);
101}
102
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100103void CLBoundingBoxTransformKernel::configure(const CLCompileContext &compile_context,
104 const ICLTensor *boxes,
105 ICLTensor *pred_boxes,
106 const ICLTensor *deltas,
107 const BoundingBoxTransformInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100108{
giuros01c04a0e82018-10-03 12:44:35 +0100109 ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 auto padding_info = get_padding_info({boxes, pred_boxes, deltas});
111 auto_init_if_empty(*pred_boxes->info(), deltas->info()
112 ->clone()
113 ->set_data_type(boxes->info()->data_type())
114 .set_quantization_info(boxes->info()->quantization_info()));
giuros01c04a0e82018-10-03 12:44:35 +0100115
giuros01d696cb62018-11-16 10:39:59 +0000116 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
117
giuros01c04a0e82018-10-03 12:44:35 +0100118 // Set instance variables
119 _boxes = boxes;
120 _pred_boxes = pred_boxes;
121 _deltas = deltas;
122
123 // Get image height and widht (rescaled)
124 const int img_h = floor(info.img_height() / info.scale() + 0.5f);
125 const int img_w = floor(info.img_width() / info.scale() + 0.5f);
126
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100127 const bool is_quantized = is_data_type_quantized(boxes->info()->data_type());
128
giuros01c04a0e82018-10-03 12:44:35 +0100129 // Set build options
130 CLBuildOptions build_opts;
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100131 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(boxes->info()->data_type()));
giuros01c04a0e82018-10-03 12:44:35 +0100132 build_opts.add_option("-DWEIGHT_X=" + float_to_string_with_full_precision(info.weights()[0]));
133 build_opts.add_option("-DWEIGHT_Y=" + float_to_string_with_full_precision(info.weights()[1]));
134 build_opts.add_option("-DWEIGHT_W=" + float_to_string_with_full_precision(info.weights()[2]));
135 build_opts.add_option("-DWEIGHT_H=" + float_to_string_with_full_precision(info.weights()[3]));
136 build_opts.add_option("-DBBOX_XFORM_CLIP=" + float_to_string_with_full_precision(info.bbox_xform_clip()));
137 build_opts.add_option("-DIMG_WIDTH=" + support::cpp11::to_string(img_w));
138 build_opts.add_option("-DIMG_HEIGHT=" + support::cpp11::to_string(img_h));
139 build_opts.add_option("-DBOX_FIELDS=" + support::cpp11::to_string(4));
giuros01d696cb62018-11-16 10:39:59 +0000140 build_opts.add_option("-DSCALE_BEFORE=" + float_to_string_with_full_precision(info.scale()));
141 build_opts.add_option_if(info.apply_scale(), "-DSCALE_AFTER=" + float_to_string_with_full_precision(info.scale()));
142 build_opts.add_option_if(info.correct_transform_coords(), "-DOFFSET=1");
giuros01c04a0e82018-10-03 12:44:35 +0100143
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100144 if (is_quantized)
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100145 {
146 build_opts.add_option("-DDATA_TYPE_DELTAS=" + get_cl_type_from_data_type(deltas->info()->data_type()));
147 const UniformQuantizationInfo boxes_qinfo = boxes->info()->quantization_info().uniform();
148 const UniformQuantizationInfo deltas_qinfo = deltas->info()->quantization_info().uniform();
149 const UniformQuantizationInfo pred_boxes_qinfo = pred_boxes->info()->quantization_info().uniform();
150 build_opts.add_option("-DOFFSET_BOXES=" + float_to_string_with_full_precision(boxes_qinfo.offset));
151 build_opts.add_option("-DSCALE_BOXES=" + float_to_string_with_full_precision(boxes_qinfo.scale));
152 build_opts.add_option("-DOFFSET_DELTAS=" + float_to_string_with_full_precision(deltas_qinfo.offset));
153 build_opts.add_option("-DSCALE_DELTAS=" + float_to_string_with_full_precision(deltas_qinfo.scale));
154 build_opts.add_option("-DOFFSET_PRED_BOXES=" + float_to_string_with_full_precision(pred_boxes_qinfo.offset));
155 build_opts.add_option("-DSCALE_PRED_BOXES=" + float_to_string_with_full_precision(pred_boxes_qinfo.scale));
156 }
157
giuros01c04a0e82018-10-03 12:44:35 +0100158 // Create kernel
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100159 const std::string kernel_name = (is_quantized) ? "bounding_box_transform_quantized" : "bounding_box_transform";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100160 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
giuros01c04a0e82018-10-03 12:44:35 +0100161
162 // Since the number of columns is a multiple of 4 by definition, we don't need to pad the tensor
163 const unsigned int num_elems_processed_per_iteration = 4;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100164 Window win = calculate_max_window(*deltas->info(), Steps(num_elems_processed_per_iteration));
giuros01c04a0e82018-10-03 12:44:35 +0100165 ICLKernel::configure_internal(win);
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000166 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
giuros01c04a0e82018-10-03 12:44:35 +0100167}
168
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100169Status CLBoundingBoxTransformKernel::validate(const ITensorInfo *boxes,
170 const ITensorInfo *pred_boxes,
171 const ITensorInfo *deltas,
172 const BoundingBoxTransformInfo &info)
giuros01c04a0e82018-10-03 12:44:35 +0100173{
giuros01d696cb62018-11-16 10:39:59 +0000174 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
giuros01c04a0e82018-10-03 12:44:35 +0100175 return Status{};
176}
177
178void CLBoundingBoxTransformKernel::run(const Window &window, cl::CommandQueue &queue)
179{
180 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
181 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
182
183 Window slice = window.first_slice_window_2D();
184
185 // Set arguments
186 unsigned int idx = 0;
187 add_1D_tensor_argument(idx, _boxes, slice);
188 add_2D_tensor_argument(idx, _pred_boxes, slice);
189 add_2D_tensor_argument(idx, _deltas, slice);
190
191 // Note that we don't need to loop over the slices, as we are sure that we are dealing with all 2D tensors
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100192 enqueue(queue, *this, slice, lws_hint());
giuros01c04a0e82018-10-03 12:44:35 +0100193}
194} // namespace arm_compute