blob: 2e1e85488b1010acced27331ed25b15c7961f1da [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{
42Status validate_arguments(const ITensorInfo *input, size_t num_rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
43{
44 ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output);
45 ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input);
46 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
47 ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
48 ARM_COMPUTE_RETURN_ERROR_ON(num_rois == 0);
49
50 if(output->total_size() != 0)
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
53 ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) || (output->dimension(1) != pool_info.pooled_height()));
54 ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2));
55 ARM_COMPUTE_RETURN_ERROR_ON(num_rois != output->dimension(3));
56 }
57
58 return Status{};
59}
60} // namespace
61
62CLROIAlignLayerKernel::CLROIAlignLayerKernel()
63 : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f)
64{
65}
66
67void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLROIArray *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
68{
69 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois);
70 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->num_values(), output->info(), pool_info));
71
72 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->num_values());
73 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type());
74
75 _input = input;
76 _output = output;
77 _rois = rois;
78 _pool_info = pool_info;
79
80 // Set build options
81 std::set<std::string> build_opts;
82 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
83 build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())));
84 build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))));
85 build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))));
86 build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))));
87 build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())));
88 build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())));
89 build_opts.emplace(("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale())));
90 if(pool_info.sampling_ratio() > 0)
91 {
92 build_opts.emplace(("-DSAMPLING_RATIO=" + support::cpp11::to_string(pool_info.sampling_ratio())));
93 }
94
95 // Create kernel
96 std::string kernel_name = "roi_align_layer";
97 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
98
99 // Set static kernel arguments
100 unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array();
101 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
102 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
103
104 // Configure kernel window
105 const unsigned int num_elems_processed_per_iteration = 1;
106 Window window = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
107 AccessWindowStatic input_access(input->info(),
108 input->info()->valid_region().start(0),
109 input->info()->valid_region().start(1),
110 input->info()->valid_region().end(0),
111 input->info()->valid_region().end(1));
112 AccessWindowStatic output_access(output->info(), 0, 0, pool_info.pooled_width(), pool_info.pooled_height());
113
114 output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape()));
115 ICLKernel::configure_internal(window);
116}
117
118Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, size_t num_rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
119{
120 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, num_rois, output, pool_info));
121 return Status{};
122}
123
124void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue)
125{
126 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
127 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
128
129 Window slice = window.first_slice_window_3D();
130 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROIArray)
131 slice.set(Window::DimZ, window[3]);
132
133 // Set arguments
134 unsigned int idx = 0;
135 add_3D_tensor_argument(idx, _input, slice);
136 add_1D_array_argument<ROI>(idx, _rois, Strides(sizeof(ROI)), 1U, slice);
137 add_3D_tensor_argument(idx, _output, slice);
138 enqueue(queue, *this, slice);
139}
140
141} // namespace arm_compute