blob: 66d26231d7228672a7f6f2f968ef832db6524d7e [file] [log] [blame]
giuros0118870812018-09-13 09:31:40 +01001/*
George Wort44b4e972019-01-08 11:41:54 +00002 * Copyright (c) 2018-2019 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 */
24#include "arm_compute/core/CL/kernels/CLROIAlignLayerKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/CL/CLHelpers.h"
28#include "arm_compute/core/CL/CLKernelLibrary.h"
29#include "arm_compute/core/CL/CLValidate.h"
30#include "arm_compute/core/CL/ICLArray.h"
31#include "arm_compute/core/CL/ICLTensor.h"
32#include "arm_compute/core/CL/OpenCL.h"
33#include "arm_compute/core/Helpers.h"
34#include "arm_compute/core/TensorInfo.h"
35#include "arm_compute/core/Utils.h"
36#include "arm_compute/core/Window.h"
George Wort44b4e972019-01-08 11:41:54 +000037#include "arm_compute/core/utils/misc/ShapeCalculator.h"
38
39using namespace arm_compute::misc::shape_calculator;
giuros0118870812018-09-13 09:31:40 +010040
41namespace arm_compute
42{
43namespace
44{
Manuel Bottini60f0a412018-10-24 17:27:02 +010045Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +010046{
Manuel Bottini60f0a412018-10-24 17:27:02 +010047 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
48 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois);
49 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
50 ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
giuros0118870812018-09-13 09:31:40 +010051 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
George Wort44b4e972019-01-08 11:41:54 +000053 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW);
giuros0118870812018-09-13 09:31:40 +010054 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
giuros0118870812018-09-13 09:31:40 +010055
56 if(output->total_size() != 0)
57 {
58 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgioc8df89f2018-11-16 10:02:26 +000059 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
George Wort44b4e972019-01-08 11:41:54 +000060 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape());
giuros0118870812018-09-13 09:31:40 +010061 }
giuros0118870812018-09-13 09:31:40 +010062 return Status{};
63}
Manuel Bottini60f0a412018-10-24 17:27:02 +010064
65std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
66{
67 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
68
69 // Output auto inizialitation if not yet initialized
George Wort44b4e972019-01-08 11:41:54 +000070 const TensorShape output_shape = compute_roi_align_shape(*input, *rois, pool_info);
Manuel Bottini60f0a412018-10-24 17:27:02 +010071 auto_init_if_empty((*output), output_shape, 1, input->data_type());
George Wort44b4e972019-01-08 11:41:54 +000072 output->set_data_layout(input->data_layout());
Manuel Bottini60f0a412018-10-24 17:27:02 +010073
74 // Configure kernel window
75 const unsigned int num_elems_processed_per_iteration = 1;
76 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
77
78 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
79 AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
80
81 bool window_changed = update_window_and_padding(win, input_access, output_access);
82 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
83 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
84 return std::make_pair(err, win);
85}
giuros0118870812018-09-13 09:31:40 +010086} // namespace
87
88CLROIAlignLayerKernel::CLROIAlignLayerKernel()
89 : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
90{
91}
92
Manuel Bottini60f0a412018-10-24 17:27:02 +010093void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +010094{
95 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
Manuel Bottini60f0a412018-10-24 17:27:02 +010096 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
giuros0118870812018-09-13 09:31:40 +010097
Manuel Bottini60f0a412018-10-24 17:27:02 +010098 // Configure kernel window
99 auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
100 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
giuros0118870812018-09-13 09:31:40 +0100101
102 _input = input;
103 _output = output;
104 _rois = rois;
105 _pool_info = pool_info;
106
107 // Set build options
Manuel Bottini60f0a412018-10-24 17:27:02 +0100108 CLBuildOptions build_opts;
109 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
110 build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()));
George Wort44b4e972019-01-08 11:41:54 +0000111 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))));
112 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))));
113 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 +0100114 build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()));
115 build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()));
116 build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale()));
George Wort44b4e972019-01-08 11:41:54 +0000117 build_opts.add_option_if(input->info()->data_layout() == DataLayout::NHWC, "-DNHWC");
Manuel Bottini60f0a412018-10-24 17:27:02 +0100118 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 +0100119
120 // Create kernel
121 std::string kernel_name = "roi_align_layer";
Manuel Bottini60f0a412018-10-24 17:27:02 +0100122 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
giuros0118870812018-09-13 09:31:40 +0100123
Manuel Bottini60f0a412018-10-24 17:27:02 +0100124 ICLKernel::configure_internal(win_config.second);
giuros0118870812018-09-13 09:31:40 +0100125}
126
Manuel Bottini60f0a412018-10-24 17:27:02 +0100127Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +0100128{
Manuel Bottini60f0a412018-10-24 17:27:02 +0100129 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
giuros0118870812018-09-13 09:31:40 +0100130 return Status{};
131}
132
133void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue)
134{
135 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
136 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
137
Manuel Bottini60f0a412018-10-24 17:27:02 +0100138 Window slice = window.first_slice_window_3D();
139 Window slice_rois = slice;
140 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
141 slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
George Wort44b4e972019-01-08 11:41:54 +0000142 slice.set(get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL), window[3]);
giuros0118870812018-09-13 09:31:40 +0100143
144 // Set arguments
145 unsigned int idx = 0;
146 add_3D_tensor_argument(idx, _input, slice);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100147 add_2D_tensor_argument(idx, _rois, slice_rois);
giuros0118870812018-09-13 09:31:40 +0100148 add_3D_tensor_argument(idx, _output, slice);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100149 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
150 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
151
giuros0118870812018-09-13 09:31:40 +0100152 enqueue(queue, *this, slice);
153}
giuros0118870812018-09-13 09:31:40 +0100154} // namespace arm_compute