blob: b9ff72b9289df09102f2b28767c54c3d807d2c07 [file] [log] [blame]
Manuel Bottini5209be52019-02-13 16:34:56 +00001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2019-2021, 2023 Arm Limited.
Manuel Bottini5209be52019-02-13 16:34:56 +00003 *
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/CLGenerateProposalsLayerKernel.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000025
Manuel Bottini5209be52019-02-13 16:34:56 +000026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
Manuel Bottini5209be52019-02-13 16:34:56 +000028#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"
Manuel Bottini5209be52019-02-13 16:34:56 +000039
40namespace arm_compute
41{
42namespace
43{
44Status validate_arguments(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info)
45{
46 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(anchors, all_anchors);
Georgios Pinitas8f5802f2019-02-22 11:08:32 +000047 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(anchors);
Manuel Bottini5209be52019-02-13 16:34:56 +000048 ARM_COMPUTE_RETURN_ERROR_ON(anchors->dimension(0) != info.values_per_roi());
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010049 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(anchors, DataType::QSYMM16, DataType::F16, DataType::F32);
Manuel Bottini5209be52019-02-13 16:34:56 +000050 ARM_COMPUTE_RETURN_ERROR_ON(anchors->num_dimensions() > 2);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010051 if (all_anchors->total_size() > 0)
Manuel Bottini5209be52019-02-13 16:34:56 +000052 {
53 size_t feature_height = info.feat_height();
54 size_t feature_width = info.feat_width();
55 size_t num_anchors = anchors->dimension(1);
56 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(all_anchors, anchors);
57 ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->num_dimensions() > 2);
58 ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(0) != info.values_per_roi());
59 ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(1) != feature_height * feature_width * num_anchors);
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010060
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010061 if (is_data_type_quantized(anchors->data_type()))
Michele Di Giorgio6b612f52019-09-05 12:30:22 +010062 {
63 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(anchors, all_anchors);
64 }
Manuel Bottini5209be52019-02-13 16:34:56 +000065 }
66 return Status{};
67}
68} // namespace
69
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010070CLComputeAllAnchorsKernel::CLComputeAllAnchorsKernel() : _anchors(nullptr), _all_anchors(nullptr)
Manuel Bottini5209be52019-02-13 16:34:56 +000071{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010072 _type = CLKernelType::ELEMENTWISE;
Manuel Bottini5209be52019-02-13 16:34:56 +000073}
74
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010075void CLComputeAllAnchorsKernel::configure(const ICLTensor *anchors,
76 ICLTensor *all_anchors,
77 const ComputeAnchorsInfo &info)
Manuel Bottini5209be52019-02-13 16:34:56 +000078{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010079 configure(CLKernelLibrary::get().get_compile_context(), anchors, all_anchors, info);
80}
81
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010082void CLComputeAllAnchorsKernel::configure(const CLCompileContext &compile_context,
83 const ICLTensor *anchors,
84 ICLTensor *all_anchors,
85 const ComputeAnchorsInfo &info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010086{
Manuel Bottini5209be52019-02-13 16:34:56 +000087 ARM_COMPUTE_ERROR_ON_NULLPTR(anchors, all_anchors);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088 auto padding_info = get_padding_info({anchors, all_anchors});
Manuel Bottini5209be52019-02-13 16:34:56 +000089 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(anchors->info(), all_anchors->info(), info));
90
91 // Metadata
92 const size_t num_anchors = anchors->info()->dimension(1);
93 const DataType data_type = anchors->info()->data_type();
94 const float width = info.feat_width();
95 const float height = info.feat_height();
96
97 // Initialize the output if empty
98 const TensorShape output_shape(info.values_per_roi(), width * height * num_anchors);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010099 auto_init_if_empty(*all_anchors->info(),
100 TensorInfo(output_shape, 1, data_type, anchors->info()->quantization_info()));
Manuel Bottini5209be52019-02-13 16:34:56 +0000101
102 // Set instance variables
103 _anchors = anchors;
104 _all_anchors = all_anchors;
105
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100106 const bool is_quantized = is_data_type_quantized(anchors->info()->data_type());
107
Manuel Bottini5209be52019-02-13 16:34:56 +0000108 // Set build options
109 CLBuildOptions build_opts;
110 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
111 build_opts.add_option("-DWIDTH=" + float_to_string_with_full_precision(width));
112 build_opts.add_option("-DHEIGHT=" + float_to_string_with_full_precision(height));
113 build_opts.add_option("-DSTRIDE=" + float_to_string_with_full_precision(1.f / info.spatial_scale()));
114 build_opts.add_option("-DNUM_ANCHORS=" + support::cpp11::to_string(num_anchors));
115 build_opts.add_option("-DNUM_ROI_FIELDS=" + support::cpp11::to_string(info.values_per_roi()));
116
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100117 if (is_quantized)
Michele Di Giorgio6b612f52019-09-05 12:30:22 +0100118 {
119 const UniformQuantizationInfo qinfo = anchors->info()->quantization_info().uniform();
120 build_opts.add_option("-DSCALE=" + float_to_string_with_full_precision(qinfo.scale));
121 build_opts.add_option("-DOFFSET=" + float_to_string_with_full_precision(qinfo.offset));
122 }
123
Manuel Bottini5209be52019-02-13 16:34:56 +0000124 // Create kernel
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 const std::string kernel_name =
126 (is_quantized) ? "generate_proposals_compute_all_anchors_quantized" : "generate_proposals_compute_all_anchors";
127 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
Manuel Bottini5209be52019-02-13 16:34:56 +0000128
129 // The tensor all_anchors can be interpreted as an array of structs (each structs has values_per_roi fields).
130 // This means we don't need to pad on the X dimension, as we know in advance how many fields
131 // compose the struct.
132 Window win = calculate_max_window(*all_anchors->info(), Steps(info.values_per_roi()));
133 ICLKernel::configure_internal(win);
Manuel Bottinib6869dd2020-12-16 15:34:25 +0000134 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
Manuel Bottini5209be52019-02-13 16:34:56 +0000135}
136
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100137Status CLComputeAllAnchorsKernel::validate(const ITensorInfo *anchors,
138 const ITensorInfo *all_anchors,
139 const ComputeAnchorsInfo &info)
Manuel Bottini5209be52019-02-13 16:34:56 +0000140{
141 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(anchors, all_anchors, info));
142 return Status{};
143}
144
145void CLComputeAllAnchorsKernel::run(const Window &window, cl::CommandQueue &queue)
146{
147 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
148 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
149
150 // Collapse everything on the first dimension
151 Window collapsed = window.collapse(ICLKernel::window(), Window::DimX);
152
153 // Set arguments
154 unsigned int idx = 0;
155 add_1D_tensor_argument(idx, _anchors, collapsed);
156 add_1D_tensor_argument(idx, _all_anchors, collapsed);
157
158 // Note that we don't need to loop over the slices, as we are launching exactly
159 // as many threads as all the anchors generated
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100160 enqueue(queue, *this, collapsed, lws_hint());
Manuel Bottini5209be52019-02-13 16:34:56 +0000161}
162} // namespace arm_compute