blob: b8c0d2f2b8f4fad9c63a6eda45f6d13182310fbc [file] [log] [blame]
giuros01c04a0e82018-10-03 12:44:35 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2020 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 */
24#include "arm_compute/core/CL/kernels/CLBoundingBoxTransformKernel.h"
25
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/ICLArray.h"
29#include "arm_compute/core/CL/ICLTensor.h"
30#include "arm_compute/core/CL/OpenCL.h"
31#include "arm_compute/core/Helpers.h"
32#include "arm_compute/core/TensorInfo.h"
33#include "arm_compute/core/Utils.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#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{
giuros01d696cb62018-11-16 10:39:59 +000044Status validate_arguments(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
giuros01c04a0e82018-10-03 12:44:35 +010045{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000047 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(boxes);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010048 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(boxes, DataType::QASYMM16, DataType::F32, DataType::F16);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8, DataType::F32, DataType::F16);
giuros01c04a0e82018-10-03 12:44:35 +010050 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[1] != boxes->tensor_shape()[1]);
51 ARM_COMPUTE_RETURN_ERROR_ON(deltas->tensor_shape()[0] % 4 != 0);
52 ARM_COMPUTE_RETURN_ERROR_ON(boxes->tensor_shape()[0] != 4);
53 ARM_COMPUTE_RETURN_ERROR_ON(deltas->num_dimensions() > 2);
54 ARM_COMPUTE_RETURN_ERROR_ON(boxes->num_dimensions() > 2);
55
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010056 const bool is_qasymm16 = boxes->data_type() == DataType::QASYMM16;
57 if(is_qasymm16)
58 {
59 const UniformQuantizationInfo boxes_qinfo = boxes->quantization_info().uniform();
60 ARM_COMPUTE_RETURN_ERROR_ON(boxes_qinfo.scale != 0.125f);
61 ARM_COMPUTE_RETURN_ERROR_ON(boxes_qinfo.offset != 0);
62 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(deltas, DataType::QASYMM8);
63 }
Pablo Telloffd31de2019-09-04 13:38:14 +010064 else
65 {
66 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(boxes, deltas);
67 }
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010068
giuros01c04a0e82018-10-03 12:44:35 +010069 if(pred_boxes->total_size() > 0)
70 {
71 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(pred_boxes->tensor_shape(), deltas->tensor_shape());
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010072 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(pred_boxes, boxes);
giuros01c04a0e82018-10-03 12:44:35 +010073 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes->num_dimensions() > 2);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010074 if(is_qasymm16)
75 {
76 const UniformQuantizationInfo pred_boxes_qinfo = pred_boxes->quantization_info().uniform();
77 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes_qinfo.scale != 0.125f);
78 ARM_COMPUTE_RETURN_ERROR_ON(pred_boxes_qinfo.offset != 0);
79 }
giuros01c04a0e82018-10-03 12:44:35 +010080 }
giuros01d696cb62018-11-16 10:39:59 +000081 ARM_COMPUTE_RETURN_ERROR_ON(info.scale() <= 0);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +010082
giuros01c04a0e82018-10-03 12:44:35 +010083 return Status{};
84}
85} // namespace
86
87CLBoundingBoxTransformKernel::CLBoundingBoxTransformKernel()
88 : _boxes(nullptr), _pred_boxes(nullptr), _deltas(nullptr)
89{
90}
91
92void CLBoundingBoxTransformKernel::configure(const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info)
93{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010094 configure(CLKernelLibrary::get().get_compile_context(), boxes, pred_boxes, deltas, info);
95}
96
Manuel Bottini256c0b92020-04-21 13:29:30 +010097void CLBoundingBoxTransformKernel::configure(const CLCompileContext &compile_context, const ICLTensor *boxes, ICLTensor *pred_boxes, const ICLTensor *deltas, const BoundingBoxTransformInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010098{
giuros01c04a0e82018-10-03 12:44:35 +010099 ARM_COMPUTE_ERROR_ON_NULLPTR(boxes, pred_boxes, deltas);
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100100 auto_init_if_empty(*pred_boxes->info(), deltas->info()->clone()->set_data_type(boxes->info()->data_type()).set_quantization_info(boxes->info()->quantization_info()));
giuros01c04a0e82018-10-03 12:44:35 +0100101
giuros01d696cb62018-11-16 10:39:59 +0000102 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(boxes->info(), pred_boxes->info(), deltas->info(), info));
103
giuros01c04a0e82018-10-03 12:44:35 +0100104 // Set instance variables
105 _boxes = boxes;
106 _pred_boxes = pred_boxes;
107 _deltas = deltas;
108
109 // Get image height and widht (rescaled)
110 const int img_h = floor(info.img_height() / info.scale() + 0.5f);
111 const int img_w = floor(info.img_width() / info.scale() + 0.5f);
112
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100113 const bool is_quantized = is_data_type_quantized(boxes->info()->data_type());
114
giuros01c04a0e82018-10-03 12:44:35 +0100115 // Set build options
116 CLBuildOptions build_opts;
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100117 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(boxes->info()->data_type()));
giuros01c04a0e82018-10-03 12:44:35 +0100118 build_opts.add_option("-DWEIGHT_X=" + float_to_string_with_full_precision(info.weights()[0]));
119 build_opts.add_option("-DWEIGHT_Y=" + float_to_string_with_full_precision(info.weights()[1]));
120 build_opts.add_option("-DWEIGHT_W=" + float_to_string_with_full_precision(info.weights()[2]));
121 build_opts.add_option("-DWEIGHT_H=" + float_to_string_with_full_precision(info.weights()[3]));
122 build_opts.add_option("-DBBOX_XFORM_CLIP=" + float_to_string_with_full_precision(info.bbox_xform_clip()));
123 build_opts.add_option("-DIMG_WIDTH=" + support::cpp11::to_string(img_w));
124 build_opts.add_option("-DIMG_HEIGHT=" + support::cpp11::to_string(img_h));
125 build_opts.add_option("-DBOX_FIELDS=" + support::cpp11::to_string(4));
giuros01d696cb62018-11-16 10:39:59 +0000126 build_opts.add_option("-DSCALE_BEFORE=" + float_to_string_with_full_precision(info.scale()));
127 build_opts.add_option_if(info.apply_scale(), "-DSCALE_AFTER=" + float_to_string_with_full_precision(info.scale()));
128 build_opts.add_option_if(info.correct_transform_coords(), "-DOFFSET=1");
giuros01c04a0e82018-10-03 12:44:35 +0100129
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100130 if(is_quantized)
131 {
132 build_opts.add_option("-DDATA_TYPE_DELTAS=" + get_cl_type_from_data_type(deltas->info()->data_type()));
133 const UniformQuantizationInfo boxes_qinfo = boxes->info()->quantization_info().uniform();
134 const UniformQuantizationInfo deltas_qinfo = deltas->info()->quantization_info().uniform();
135 const UniformQuantizationInfo pred_boxes_qinfo = pred_boxes->info()->quantization_info().uniform();
136 build_opts.add_option("-DOFFSET_BOXES=" + float_to_string_with_full_precision(boxes_qinfo.offset));
137 build_opts.add_option("-DSCALE_BOXES=" + float_to_string_with_full_precision(boxes_qinfo.scale));
138 build_opts.add_option("-DOFFSET_DELTAS=" + float_to_string_with_full_precision(deltas_qinfo.offset));
139 build_opts.add_option("-DSCALE_DELTAS=" + float_to_string_with_full_precision(deltas_qinfo.scale));
140 build_opts.add_option("-DOFFSET_PRED_BOXES=" + float_to_string_with_full_precision(pred_boxes_qinfo.offset));
141 build_opts.add_option("-DSCALE_PRED_BOXES=" + float_to_string_with_full_precision(pred_boxes_qinfo.scale));
142 }
143
giuros01c04a0e82018-10-03 12:44:35 +0100144 // Create kernel
Michele Di Giorgio4aff98f2019-08-28 16:27:26 +0100145 const std::string kernel_name = (is_quantized) ? "bounding_box_transform_quantized" : "bounding_box_transform";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100146 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
giuros01c04a0e82018-10-03 12:44:35 +0100147
148 // Since the number of columns is a multiple of 4 by definition, we don't need to pad the tensor
149 const unsigned int num_elems_processed_per_iteration = 4;
150 Window win = calculate_max_window(*deltas->info(), Steps(num_elems_processed_per_iteration));
151 ICLKernel::configure_internal(win);
152}
153
154Status CLBoundingBoxTransformKernel::validate(const ITensorInfo *boxes, const ITensorInfo *pred_boxes, const ITensorInfo *deltas, const BoundingBoxTransformInfo &info)
155{
giuros01d696cb62018-11-16 10:39:59 +0000156 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(boxes, pred_boxes, deltas, info));
giuros01c04a0e82018-10-03 12:44:35 +0100157 return Status{};
158}
159
160void CLBoundingBoxTransformKernel::run(const Window &window, cl::CommandQueue &queue)
161{
162 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
163 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
164
165 Window slice = window.first_slice_window_2D();
166
167 // Set arguments
168 unsigned int idx = 0;
169 add_1D_tensor_argument(idx, _boxes, slice);
170 add_2D_tensor_argument(idx, _pred_boxes, slice);
171 add_2D_tensor_argument(idx, _deltas, slice);
172
173 // 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 +0100174 enqueue(queue, *this, slice, lws_hint());
giuros01c04a0e82018-10-03 12:44:35 +0100175}
176} // namespace arm_compute