giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [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/CLROIAlignLayerKernel.h" |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 25 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [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" |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 33 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/CL/CLValidate.h" |
| 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 38 | #include "support/StringSupport.h" |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 39 | |
| 40 | using namespace arm_compute::misc::shape_calculator; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | namespace |
| 45 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 46 | Status validate_arguments(const ITensorInfo *input, |
| 47 | const ITensorInfo *rois, |
| 48 | ITensorInfo *output, |
| 49 | const ROIPoolingLayerInfo &pool_info) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 50 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 54 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, |
| 56 | DataType::F32, DataType::F16); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 59 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | if (output->total_size() != 0) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michele Di Giorgio | c8df89f | 2018-11-16 10:02:26 +0000 | [diff] [blame] | 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), |
| 65 | output->tensor_shape()); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 66 | } |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | if (is_data_type_quantized_asymmetric(input->data_type())) |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 69 | { |
| 70 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::QASYMM16); |
| 71 | |
| 72 | const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform(); |
| 73 | ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f); |
| 74 | ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois); |
| 79 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 80 | return Status{}; |
| 81 | } |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 82 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 83 | } // namespace |
| 84 | |
| 85 | CLROIAlignLayerKernel::CLROIAlignLayerKernel() |
| 86 | : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f) |
| 87 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 88 | _type = CLKernelType::ELEMENTWISE; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 91 | void CLROIAlignLayerKernel::configure(const ICLTensor *input, |
| 92 | const ICLTensor *rois, |
| 93 | ICLTensor *output, |
| 94 | const ROIPoolingLayerInfo &pool_info) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 95 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 96 | configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info); |
| 97 | } |
| 98 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 99 | void CLROIAlignLayerKernel::configure(const CLCompileContext &compile_context, |
| 100 | const ICLTensor *input, |
| 101 | const ICLTensor *rois, |
| 102 | ICLTensor *output, |
| 103 | const ROIPoolingLayerInfo &pool_info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 104 | { |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 105 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 106 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 107 | |
Manuel Bottini | 4f77ba9 | 2021-01-08 17:14:39 +0000 | [diff] [blame] | 108 | // Output auto inizialitation if not yet initialized |
| 109 | const TensorShape output_shape = compute_roi_align_shape(*input->info(), *rois->info(), pool_info); |
| 110 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
| 111 | output->info()->set_data_layout(input->info()->data_layout()); |
| 112 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 113 | auto padding_info = get_padding_info({input, rois, output}); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 114 | |
| 115 | _input = input; |
| 116 | _output = output; |
| 117 | _rois = rois; |
| 118 | _pool_info = pool_info; |
| 119 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 120 | const DataType data_type = input->info()->data_type(); |
| 121 | const bool is_qasymm = is_data_type_quantized_asymmetric(data_type); |
| 122 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 123 | // Set build options |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 124 | CLBuildOptions build_opts; |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 125 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 126 | build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | build_opts.add_option("-DMAX_DIM_X=" + |
| 128 | support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index( |
| 129 | input->info()->data_layout(), DataLayoutDimension::WIDTH)))); |
| 130 | build_opts.add_option("-DMAX_DIM_Y=" + |
| 131 | support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index( |
| 132 | input->info()->data_layout(), DataLayoutDimension::HEIGHT)))); |
| 133 | build_opts.add_option("-DMAX_DIM_Z=" + |
| 134 | support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index( |
| 135 | input->info()->data_layout(), DataLayoutDimension::CHANNEL)))); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 136 | build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())); |
| 137 | build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())); |
| 138 | build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale())); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 139 | build_opts.add_option_if(input->info()->data_layout() == DataLayout::NHWC, "-DNHWC"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 140 | build_opts.add_option_if(pool_info.sampling_ratio() > 0, |
| 141 | "-DSAMPLING_RATIO=" + support::cpp11::to_string(pool_info.sampling_ratio())); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 142 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 143 | if (is_qasymm) |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 144 | { |
| 145 | const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); |
| 146 | const UniformQuantizationInfo roisq_info = rois->info()->quantization_info().uniform(); |
| 147 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 148 | |
| 149 | build_opts.add_option("-DOFFSET_IN=" + float_to_string_with_full_precision(iq_info.offset)); |
| 150 | build_opts.add_option("-DSCALE_IN=" + float_to_string_with_full_precision(iq_info.scale)); |
| 151 | build_opts.add_option("-DOFFSET_ROIS=" + float_to_string_with_full_precision(roisq_info.offset)); |
| 152 | build_opts.add_option("-DSCALE_ROIS=" + float_to_string_with_full_precision(roisq_info.scale)); |
| 153 | build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset)); |
| 154 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale)); |
| 155 | } |
| 156 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 157 | // Create kernel |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 158 | const std::string kernel_name = (is_qasymm) ? "roi_align_layer_quantized" : "roi_align_layer"; |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 159 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 160 | |
Manuel Bottini | 4f77ba9 | 2021-01-08 17:14:39 +0000 | [diff] [blame] | 161 | // Configure kernel window |
| 162 | Window win = calculate_max_window(*output->info(), Steps()); |
| 163 | ICLKernel::configure_internal(win); |
| 164 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 165 | } |
| 166 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 167 | Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, |
| 168 | const ITensorInfo *rois, |
| 169 | ITensorInfo *output, |
| 170 | const ROIPoolingLayerInfo &pool_info) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 171 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 172 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 173 | return Status{}; |
| 174 | } |
| 175 | |
| 176 | void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 177 | { |
| 178 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 179 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 180 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 181 | Window slice = window.first_slice_window_3D(); |
| 182 | Window slice_rois = slice; |
| 183 | // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor) |
| 184 | slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0)); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 185 | slice.set(get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL), window[3]); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 186 | |
| 187 | // Set arguments |
| 188 | unsigned int idx = 0; |
| 189 | add_3D_tensor_argument(idx, _input, slice); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 190 | add_2D_tensor_argument(idx, _rois, slice_rois); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 191 | add_3D_tensor_argument(idx, _output, slice); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 192 | add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]); |
| 193 | add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]); |
| 194 | |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 195 | enqueue(queue, *this, slice, lws_hint()); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 196 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 197 | } // namespace arm_compute |