blob: 325eeb240f253b8775e0a68a8dab2d9a1bba3fc7 [file] [log] [blame]
giuros0118870812018-09-13 09:31:40 +01001/*
2 * Copyright (c) 2018 ARM Limited.
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 */
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"
37
38namespace arm_compute
39{
40namespace
41{
Manuel Bottini60f0a412018-10-24 17:27:02 +010042Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +010043{
Manuel Bottini60f0a412018-10-24 17:27:02 +010044 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois);
46 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5);
47 ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2);
giuros0118870812018-09-13 09:31:40 +010048 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
49 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
Michele Di Giorgioc8df89f2018-11-16 10:02:26 +000050 ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NCHW);
giuros0118870812018-09-13 09:31:40 +010051 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
giuros0118870812018-09-13 09:31:40 +010052
53 if(output->total_size() != 0)
54 {
55 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Michele Di Giorgioc8df89f2018-11-16 10:02:26 +000056 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output);
giuros0118870812018-09-13 09:31:40 +010057 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) || (output->dimension(1) != pool_info.pooled_height()));
58 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2));
Manuel Bottini60f0a412018-10-24 17:27:02 +010059 ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(1) != output->dimension(3));
giuros0118870812018-09-13 09:31:40 +010060 }
61
62 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
70 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->dimension(2), rois->dimension(1));
71 auto_init_if_empty((*output), output_shape, 1, input->data_type());
72
73 // Configure kernel window
74 const unsigned int num_elems_processed_per_iteration = 1;
75 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
76
77 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
78 AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
79
80 bool window_changed = update_window_and_padding(win, input_access, output_access);
81 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
82 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
83 return std::make_pair(err, win);
84}
giuros0118870812018-09-13 09:31:40 +010085} // namespace
86
87CLROIAlignLayerKernel::CLROIAlignLayerKernel()
88 : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
89{
90}
91
Manuel Bottini60f0a412018-10-24 17:27:02 +010092void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +010093{
94 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
Manuel Bottini60f0a412018-10-24 17:27:02 +010095 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info));
giuros0118870812018-09-13 09:31:40 +010096
Manuel Bottini60f0a412018-10-24 17:27:02 +010097 // Configure kernel window
98 auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
99 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
giuros0118870812018-09-13 09:31:40 +0100100
101 _input = input;
102 _output = output;
103 _rois = rois;
104 _pool_info = pool_info;
105
106 // Set build options
Manuel Bottini60f0a412018-10-24 17:27:02 +0100107 CLBuildOptions build_opts;
108 build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type()));
109 build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type()));
110 build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX)));
111 build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY)));
112 build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ)));
113 build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width()));
114 build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height()));
115 build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale()));
116 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 +0100117
118 // Create kernel
119 std::string kernel_name = "roi_align_layer";
Manuel Bottini60f0a412018-10-24 17:27:02 +0100120 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options()));
giuros0118870812018-09-13 09:31:40 +0100121
Manuel Bottini60f0a412018-10-24 17:27:02 +0100122 ICLKernel::configure_internal(win_config.second);
giuros0118870812018-09-13 09:31:40 +0100123}
124
Manuel Bottini60f0a412018-10-24 17:27:02 +0100125Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
giuros0118870812018-09-13 09:31:40 +0100126{
Manuel Bottini60f0a412018-10-24 17:27:02 +0100127 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info));
giuros0118870812018-09-13 09:31:40 +0100128 return Status{};
129}
130
131void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue)
132{
133 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
134 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
135
Manuel Bottini60f0a412018-10-24 17:27:02 +0100136 Window slice = window.first_slice_window_3D();
137 Window slice_rois = slice;
138 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
139 slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
giuros0118870812018-09-13 09:31:40 +0100140 slice.set(Window::DimZ, window[3]);
141
142 // Set arguments
143 unsigned int idx = 0;
144 add_3D_tensor_argument(idx, _input, slice);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100145 add_2D_tensor_argument(idx, _rois, slice_rois);
giuros0118870812018-09-13 09:31:40 +0100146 add_3D_tensor_argument(idx, _output, slice);
Manuel Bottini60f0a412018-10-24 17:27:02 +0100147 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
148 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
149
giuros0118870812018-09-13 09:31:40 +0100150 enqueue(queue, *this, slice);
151}
giuros0118870812018-09-13 09:31:40 +0100152} // namespace arm_compute