blob: 1b2c414a496e2a2a3872fc73f9ed6defd5544020 [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
Matthew Bentham314d3e22023-06-23 10:53:52 +00002 * Copyright (c) 2017-2021, 2023 Arm Limited.
SiCong Li3e363692017-07-04 15:02:10 +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/CLROIPoolingLayerKernel.h"
SiCong Li3e363692017-07-04 15:02:10 +010025
SiCong Li3e363692017-07-04 15:02:10 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
SiCong Li3e363692017-07-04 15:02:10 +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"
SiCong Li3e363692017-07-04 15:02:10 +010039
Gian Marco Iodice3eb5d292021-04-23 10:30:43 +010040#include <cfloat>
SiCong Li3e363692017-07-04 15:02:10 +010041#include <cmath>
SiCong Li3e363692017-07-04 15:02:10 +010042#include <string>
43
Manuel Bottinicc5171b2019-01-09 17:04:39 +000044namespace arm_compute
45{
SiCong Li3e363692017-07-04 15:02:10 +010046CLROIPoolingLayerKernel::CLROIPoolingLayerKernel()
47 : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
48{
Giorgio Arena4a95bba2021-06-28 11:00:27 +010049 _type = CLKernelType::ELEMENTWISE;
SiCong Li3e363692017-07-04 15:02:10 +010050}
51
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052Status CLROIPoolingLayerKernel::validate(const ITensorInfo *input,
53 const ITensorInfo *rois,
54 const ITensorInfo *output,
55 const ROIPoolingLayerInfo &pool_info)
Suhail Munshi4ed7b392021-03-22 13:13:55 +000056{
57 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
58
59 //Validate arguments
60 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
61 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16);
62 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
63 ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
64 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
65 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8);
66 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
67
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010068 if (output->total_size() != 0)
Suhail Munshi4ed7b392021-03-22 13:13:55 +000069 {
70 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010071 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) ||
72 (output->dimension(1) != pool_info.pooled_height()));
Suhail Munshi4ed7b392021-03-22 13:13:55 +000073 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2));
74 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(1) != output->dimension(3));
75 }
76
77 return Status{};
78}
79
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010080void CLROIPoolingLayerKernel::configure(const ICLTensor *input,
81 const ICLTensor *rois,
82 ICLTensor *output,
83 const ROIPoolingLayerInfo &pool_info)
SiCong Li3e363692017-07-04 15:02:10 +010084{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010085 configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info);
86}
87
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010088void CLROIPoolingLayerKernel::configure(const CLCompileContext &compile_context,
89 const ICLTensor *input,
90 const ICLTensor *rois,
91 const ICLTensor *output,
92 const ROIPoolingLayerInfo &pool_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010093{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010094 ARM_COMPUTE_ERROR_THROW_ON(
95 CLROIPoolingLayerKernel::validate(input->info(), rois->info(), output->info(), pool_info));
SiCong Li3e363692017-07-04 15:02:10 +010096
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010097 auto padding_info = get_padding_info({input, rois, output});
Gian Marco Iodice3eb5d292021-04-23 10:30:43 +010098
99 // Output auto initialization if not yet initialized
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100100 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2),
101 rois->info()->dimension(1));
102 auto_init_if_empty(*(output->info()), output_shape, 1, input->info()->data_type(),
103 output->info()->quantization_info());
SiCong Li3e363692017-07-04 15:02:10 +0100104
105 // Set instance variables
106 _input = input;
107 _rois = rois;
108 _output = output;
109 _pool_info = pool_info;
110
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000111 const DataType data_type = input->info()->data_type();
112 const bool is_qasymm = is_data_type_quantized_asymmetric(data_type);
113
SiCong Li3e363692017-07-04 15:02:10 +0100114 // Set build options
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000115 CLBuildOptions build_opts;
116 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
117 build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(data_type));
118 build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX)));
119 build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY)));
120 build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ)));
121 build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()));
122 build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()));
123 build_opts.add_option("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale()));
124
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100125 if (is_qasymm)
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000126 {
127 // Determine quantization info scale, offset
128 UniformQuantizationInfo uqinfo = UniformQuantizationInfo();
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100129 uqinfo = compute_requantization_scale_offset(_input->info()->quantization_info().uniform(),
130 _output->info()->quantization_info().uniform());
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000131 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(uqinfo.offset));
132 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(uqinfo.scale));
133
134 // Specify minimum possible value of datatype
135 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(0));
136 }
Gian Marco Iodice3eb5d292021-04-23 10:30:43 +0100137 else
138 {
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000139 // Specify min value of F32 datatype
140 build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(-FLT_MAX));
141 }
SiCong Li3e363692017-07-04 15:02:10 +0100142
Gian Marco Iodice3eb5d292021-04-23 10:30:43 +0100143 Window win = calculate_max_window(*(output->info()), Steps());
144 ICLKernel::configure_internal(win);
145
SiCong Li3e363692017-07-04 15:02:10 +0100146 // Create kernel
147 std::string kernel_name = "roi_pooling_layer";
Suhail Munshi4ed7b392021-03-22 13:13:55 +0000148 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
SiCong Li3e363692017-07-04 15:02:10 +0100149
Gian Marco Iodice3eb5d292021-04-23 10:30:43 +0100150 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
SiCong Li3e363692017-07-04 15:02:10 +0100151}
152
153void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
154{
155 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
156 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
157
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000158 Window slice = window.first_slice_window_3D();
159 Window slice_rois = slice;
160 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
161 slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
SiCong Li3e363692017-07-04 15:02:10 +0100162 slice.set(Window::DimZ, window[3]);
163
164 // Set arguments
165 unsigned int idx = 0;
166 add_3D_tensor_argument(idx, _input, slice);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000167 add_2D_tensor_argument(idx, _rois, slice_rois);
SiCong Li3e363692017-07-04 15:02:10 +0100168 add_3D_tensor_argument(idx, _output, slice);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000169 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
170 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
171
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100172 enqueue(queue, *this, slice, lws_hint());
SiCong Li3e363692017-07-04 15:02:10 +0100173}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100174} // namespace arm_compute