blob: 87f4a5d7f340db41d4b32d9a1d60cce3922aa8a8 [file] [log] [blame]
giuros0118870812018-09-13 09:31:40 +01001/*
Manuel Bottini4f77ba92021-01-08 17:14:39 +00002 * Copyright (c) 2018-2021 Arm Limited.
giuros0118870812018-09-13 09:31:40 +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/CLROIAlignLayerKernel.h"
giuros0118870812018-09-13 09:31:40 +010025
giuros0118870812018-09-13 09:31:40 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
giuros0118870812018-09-13 09:31:40 +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"
George Wort44b4e972019-01-08 11:41:54 +000034#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010035#include "src/core/AccessWindowStatic.h"
36#include "src/core/CL/CLValidate.h"
37#include "src/core/helpers/AutoConfiguration.h"
38#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000039#include "support/StringSupport.h"
George Wort44b4e972019-01-08 11:41:54 +000040
41using namespace arm_compute::misc::shape_calculator;
giuros0118870812018-09-13 09:31:40 +010042
43namespace arm_compute
44{
45namespace
46{
Manuel Bottini60f0a412018-10-24 17:27:02 +010047Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +010048{
Manuel Bottini60f0a412018-10-24 17:27:02 +010049 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
Manuel Bottini60f0a412018-10-24 17:27:02 +010050 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
51 ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
giuros0118870812018-09-13 09:31:40 +010052 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottini8481d832019-12-10 15:28:40 +000053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::QASYMM8_SIGNED, DataType::F32, DataType::F16);
George Wort44b4e972019-01-08 11:41:54 +000054 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW);
giuros0118870812018-09-13 09:31:40 +010055 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
giuros0118870812018-09-13 09:31:40 +010056
57 if(output->total_size() != 0)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgioc8df89f2018-11-16 10:02:26 +000060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
George Wort44b4e972019-01-08 11:41:54 +000061 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
giuros0118870812018-09-13 09:31:40 +010062 }
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010063
Manuel Bottini8481d832019-12-10 15:28:40 +000064 if(is_data_type_quantized_asymmetric(input->data_type()))
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +010065 {
66 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::QASYMM16);
67
68 const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform();
69 ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f);
70 ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0);
71 }
72 else
73 {
74 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois);
75 }
giuros0118870812018-09-13 09:31:40 +010076 return Status{};
77}
Manuel Bottini60f0a412018-10-24 17:27:02 +010078
giuros0118870812018-09-13 09:31:40 +010079} // namespace
80
81CLROIAlignLayerKernel::CLROIAlignLayerKernel()
82 : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
83{
84}
85
Manuel Bottini60f0a412018-10-24 17:27:02 +010086void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +010087{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010088 configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info);
89}
90
Manuel Bottini679fc962020-04-21 16:08:53 +010091void CLROIAlignLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010092{
giuros0118870812018-09-13 09:31:40 +010093 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
Manuel Bottini60f0a412018-10-24 17:27:02 +010094 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
giuros0118870812018-09-13 09:31:40 +010095
Manuel Bottini4f77ba92021-01-08 17:14:39 +000096 // Output auto inizialitation if not yet initialized
97 const TensorShape output_shape = compute_roi_align_shape(*input->info(), *rois->info(), pool_info);
98 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
99 output->info()->set_data_layout(input->info()->data_layout());
100
101 auto padding_info = get_padding_info({ input, rois, output });
giuros0118870812018-09-13 09:31:40 +0100102
103 _input = input;
104 _output = output;
105 _rois = rois;
106 _pool_info = pool_info;
107
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100108 const DataType data_type = input->info()->data_type();
109 const bool is_qasymm = is_data_type_quantized_asymmetric(data_type);
110
giuros0118870812018-09-13 09:31:40 +0100111 // Set build options
Manuel Bottini60f0a412018-10-24 17:27:02 +0100112 CLBuildOptions build_opts;
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100113 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type));
Manuel Bottini60f0a412018-10-24 17:27:02 +0100114 build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()));
George Wort44b4e972019-01-08 11:41:54 +0000115 build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH))));
116 build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT))));
117 build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL))));
Manuel Bottini60f0a412018-10-24 17:27:02 +0100118 build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()));
119 build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()));
120 build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale()));
George Wort44b4e972019-01-08 11:41:54 +0000121 build_opts.add_option_if(input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
Manuel Bottini60f0a412018-10-24 17:27:02 +0100122 build_opts.add_option_if(pool_info.sampling_ratio() > 0, "-DSAMPLING_RATIO=" + support::cpp11::to_string(pool_info.sampling_ratio()));
giuros0118870812018-09-13 09:31:40 +0100123
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100124 if(is_qasymm)
125 {
126 const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform();
127 const UniformQuantizationInfo roisq_info = rois->info()->quantization_info().uniform();
128 const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform();
129
130 build_opts.add_option("-DOFFSET_IN=" + float_to_string_with_full_precision(iq_info.offset));
131 build_opts.add_option("-DSCALE_IN=" + float_to_string_with_full_precision(iq_info.scale));
132 build_opts.add_option("-DOFFSET_ROIS=" + float_to_string_with_full_precision(roisq_info.offset));
133 build_opts.add_option("-DSCALE_ROIS=" + float_to_string_with_full_precision(roisq_info.scale));
134 build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset));
135 build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale));
136 }
137
giuros0118870812018-09-13 09:31:40 +0100138 // Create kernel
Michele Di Giorgio578a9fc2019-08-23 11:49:04 +0100139 const std::string kernel_name = (is_qasymm) ? "roi_align_layer_quantized" : "roi_align_layer";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100140 _kernel = create_kernel(compile_context, kernel_name, build_opts.options());
giuros0118870812018-09-13 09:31:40 +0100141
Manuel Bottini4f77ba92021-01-08 17:14:39 +0000142 // Configure kernel window
143 Window win = calculate_max_window(*output->info(), Steps());
144 ICLKernel::configure_internal(win);
145 ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info));
giuros0118870812018-09-13 09:31:40 +0100146}
147
Manuel Bottini60f0a412018-10-24 17:27:02 +0100148Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +0100149{
Manuel Bottini60f0a412018-10-24 17:27:02 +0100150 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
giuros0118870812018-09-13 09:31:40 +0100151 return Status{};
152}
153
154void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue)
155{
156 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
157 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
158
Manuel Bottini60f0a412018-10-24 17:27:02 +0100159 Window slice = window.first_slice_window_3D();
160 Window slice_rois = slice;
161 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
162 slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
George Wort44b4e972019-01-08 11:41:54 +0000163 slice.set(get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL), window[3]);
giuros0118870812018-09-13 09:31:40 +0100164
165 // Set arguments
166 unsigned int idx = 0;
167 add_3D_tensor_argument(idx, _input, slice);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100168 add_2D_tensor_argument(idx, _rois, slice_rois);
giuros0118870812018-09-13 09:31:40 +0100169 add_3D_tensor_argument(idx, _output, slice);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100170 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
171 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
172
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100173 enqueue(queue, *this, slice, lws_hint());
giuros0118870812018-09-13 09:31:40 +0100174}
giuros0118870812018-09-13 09:31:40 +0100175} // namespace arm_compute