Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 3 | * |
| 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 Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLGenerateProposalsLayerKernel.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 25 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 28 | #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 Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/utils/StringUtils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | namespace |
| 42 | { |
| 43 | Status validate_arguments(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info) |
| 44 | { |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(anchors, all_anchors); |
Georgios Pinitas | 8f5802f | 2019-02-22 11:08:32 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(anchors); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(anchors->dimension(0) != info.values_per_roi()); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_NOT_IN(anchors, DataType::QSYMM16, DataType::F16, DataType::F32); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 49 | ARM_COMPUTE_RETURN_ERROR_ON(anchors->num_dimensions() > 2); |
| 50 | if(all_anchors->total_size() > 0) |
| 51 | { |
| 52 | size_t feature_height = info.feat_height(); |
| 53 | size_t feature_width = info.feat_width(); |
| 54 | size_t num_anchors = anchors->dimension(1); |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(all_anchors, anchors); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->num_dimensions() > 2); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(0) != info.values_per_roi()); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(all_anchors->dimension(1) != feature_height * feature_width * num_anchors); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 59 | |
| 60 | if(is_data_type_quantized(anchors->data_type())) |
| 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(anchors, all_anchors); |
| 63 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 64 | } |
| 65 | return Status{}; |
| 66 | } |
| 67 | } // namespace |
| 68 | |
| 69 | CLComputeAllAnchorsKernel::CLComputeAllAnchorsKernel() |
| 70 | : _anchors(nullptr), _all_anchors(nullptr) |
| 71 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 72 | _type = CLKernelType::ELEMENTWISE; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void CLComputeAllAnchorsKernel::configure(const ICLTensor *anchors, ICLTensor *all_anchors, const ComputeAnchorsInfo &info) |
| 76 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 77 | configure(CLKernelLibrary::get().get_compile_context(), anchors, all_anchors, info); |
| 78 | } |
| 79 | |
Manuel Bottini | 679fc96 | 2020-04-21 16:08:53 +0100 | [diff] [blame] | 80 | void CLComputeAllAnchorsKernel::configure(const CLCompileContext &compile_context, const ICLTensor *anchors, ICLTensor *all_anchors, const ComputeAnchorsInfo &info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 81 | { |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_ERROR_ON_NULLPTR(anchors, all_anchors); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 83 | auto padding_info = get_padding_info({ anchors, all_anchors }); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 84 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(anchors->info(), all_anchors->info(), info)); |
| 85 | |
| 86 | // Metadata |
| 87 | const size_t num_anchors = anchors->info()->dimension(1); |
| 88 | const DataType data_type = anchors->info()->data_type(); |
| 89 | const float width = info.feat_width(); |
| 90 | const float height = info.feat_height(); |
| 91 | |
| 92 | // Initialize the output if empty |
| 93 | const TensorShape output_shape(info.values_per_roi(), width * height * num_anchors); |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 94 | auto_init_if_empty(*all_anchors->info(), TensorInfo(output_shape, 1, data_type, anchors->info()->quantization_info())); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 95 | |
| 96 | // Set instance variables |
| 97 | _anchors = anchors; |
| 98 | _all_anchors = all_anchors; |
| 99 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 100 | const bool is_quantized = is_data_type_quantized(anchors->info()->data_type()); |
| 101 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 102 | // Set build options |
| 103 | CLBuildOptions build_opts; |
| 104 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 105 | build_opts.add_option("-DWIDTH=" + float_to_string_with_full_precision(width)); |
| 106 | build_opts.add_option("-DHEIGHT=" + float_to_string_with_full_precision(height)); |
| 107 | build_opts.add_option("-DSTRIDE=" + float_to_string_with_full_precision(1.f / info.spatial_scale())); |
| 108 | build_opts.add_option("-DNUM_ANCHORS=" + support::cpp11::to_string(num_anchors)); |
| 109 | build_opts.add_option("-DNUM_ROI_FIELDS=" + support::cpp11::to_string(info.values_per_roi())); |
| 110 | |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 111 | if(is_quantized) |
| 112 | { |
| 113 | const UniformQuantizationInfo qinfo = anchors->info()->quantization_info().uniform(); |
| 114 | build_opts.add_option("-DSCALE=" + float_to_string_with_full_precision(qinfo.scale)); |
| 115 | build_opts.add_option("-DOFFSET=" + float_to_string_with_full_precision(qinfo.offset)); |
| 116 | } |
| 117 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 118 | // Create kernel |
Michele Di Giorgio | 6b612f5 | 2019-09-05 12:30:22 +0100 | [diff] [blame] | 119 | const std::string kernel_name = (is_quantized) ? "generate_proposals_compute_all_anchors_quantized" : "generate_proposals_compute_all_anchors"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 120 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 121 | |
| 122 | // The tensor all_anchors can be interpreted as an array of structs (each structs has values_per_roi fields). |
| 123 | // This means we don't need to pad on the X dimension, as we know in advance how many fields |
| 124 | // compose the struct. |
| 125 | Window win = calculate_max_window(*all_anchors->info(), Steps(info.values_per_roi())); |
| 126 | ICLKernel::configure_internal(win); |
Manuel Bottini | b6869dd | 2020-12-16 15:34:25 +0000 | [diff] [blame] | 127 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | Status CLComputeAllAnchorsKernel::validate(const ITensorInfo *anchors, const ITensorInfo *all_anchors, const ComputeAnchorsInfo &info) |
| 131 | { |
| 132 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(anchors, all_anchors, info)); |
| 133 | return Status{}; |
| 134 | } |
| 135 | |
| 136 | void CLComputeAllAnchorsKernel::run(const Window &window, cl::CommandQueue &queue) |
| 137 | { |
| 138 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 139 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 140 | |
| 141 | // Collapse everything on the first dimension |
| 142 | Window collapsed = window.collapse(ICLKernel::window(), Window::DimX); |
| 143 | |
| 144 | // Set arguments |
| 145 | unsigned int idx = 0; |
| 146 | add_1D_tensor_argument(idx, _anchors, collapsed); |
| 147 | add_1D_tensor_argument(idx, _all_anchors, collapsed); |
| 148 | |
| 149 | // Note that we don't need to loop over the slices, as we are launching exactly |
| 150 | // as many threads as all the anchors generated |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 151 | enqueue(queue, *this, collapsed, lws_hint()); |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 152 | } |
| 153 | } // namespace arm_compute |