blob: df7687edea2c22c55702d87cfbcbf106ec35abf8 [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
Manuel Bottinicc5171b2019-01-09 17:04:39 +00002 * Copyright (c) 2017-2019 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 */
24#include "arm_compute/core/CL/kernels/CLROIPoolingLayerKernel.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"
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010029#include "arm_compute/core/CL/CLValidate.h"
SiCong Li3e363692017-07-04 15:02:10 +010030#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"
SiCong Li3e363692017-07-04 15:02:10 +010036#include "arm_compute/core/Window.h"
37
38#include <cmath>
39#include <set>
40#include <string>
41
Manuel Bottinicc5171b2019-01-09 17:04:39 +000042namespace arm_compute
43{
44namespace
45{
46std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
47{
48 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
49
50 // Output auto initialization if not yet initialized
51 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->dimension(2), rois->dimension(1));
52 auto_init_if_empty((*output), output_shape, 1, input->data_type());
53
54 // Configure kernel window
55 const unsigned int num_elems_processed_per_iteration = 1;
56 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
57
58 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
59 AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
60
61 bool window_changed = update_window_and_padding(win, input_access, output_access);
62 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
63 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
64 return std::make_pair(err, win);
65}
66} // namespace
SiCong Li3e363692017-07-04 15:02:10 +010067
68CLROIPoolingLayerKernel::CLROIPoolingLayerKernel()
69 : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
70{
71}
72
Manuel Bottinicc5171b2019-01-09 17:04:39 +000073void CLROIPoolingLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
SiCong Li3e363692017-07-04 15:02:10 +010074{
75 ARM_COMPUTE_ERROR_ON_NULLPTR(input, rois, output);
Manuel Bottinicc5171b2019-01-09 17:04:39 +000076
77 //Validate arguments
78 ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info());
79 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16);
80 ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5);
81 ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010082 ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottinicc5171b2019-01-09 17:04:39 +000083 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
SiCong Li3e363692017-07-04 15:02:10 +010084 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
SiCong Li3e363692017-07-04 15:02:10 +010085
Manuel Bottinicc5171b2019-01-09 17:04:39 +000086 if(output->info()->total_size() != 0)
87 {
88 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
89 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
90 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
91 ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3));
92 }
SiCong Li3e363692017-07-04 15:02:10 +010093
Manuel Bottinicc5171b2019-01-09 17:04:39 +000094 // Configure kernel window
95 auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
96 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
SiCong Li3e363692017-07-04 15:02:10 +010097
98 // Set instance variables
99 _input = input;
100 _rois = rois;
101 _output = output;
102 _pool_info = pool_info;
103
104 // Set build options
105 std::set<std::string> build_opts;
106 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
107 build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())));
108 build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))));
109 build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))));
110 build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))));
111 build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())));
112 build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())));
113 build_opts.emplace(("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale())));
114
115 // Create kernel
116 std::string kernel_name = "roi_pooling_layer";
117 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
118
119 // Set static kernel arguments
120 unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array();
121 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
122 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
123
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000124 ICLKernel::configure_internal(win_config.second);
SiCong Li3e363692017-07-04 15:02:10 +0100125}
126
127void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
128{
129 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
130 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
131
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000132 Window slice = window.first_slice_window_3D();
133 Window slice_rois = slice;
134 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
135 slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
SiCong Li3e363692017-07-04 15:02:10 +0100136 slice.set(Window::DimZ, window[3]);
137
138 // Set arguments
139 unsigned int idx = 0;
140 add_3D_tensor_argument(idx, _input, slice);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000141 add_2D_tensor_argument(idx, _rois, slice_rois);
SiCong Li3e363692017-07-04 15:02:10 +0100142 add_3D_tensor_argument(idx, _output, slice);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000143 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
144 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
145
SiCong Li3e363692017-07-04 15:02:10 +0100146 enqueue(queue, *this, slice);
147}
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000148} // namespace arm_compute