blob: 43492a3d50ea531d40561be31387f13e532eb078 [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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 */
Sang-Hoon Parkbef7fa22020-10-21 15:58:54 +010024#include "src/core/CL/kernels/CLROIPoolingLayerKernel.h"
SiCong Li3e363692017-07-04 15:02:10 +010025
SiCong Li3e363692017-07-04 15:02:10 +010026#include "arm_compute/core/CL/CLHelpers.h"
27#include "arm_compute/core/CL/CLKernelLibrary.h"
28#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"
Sang-Hoon Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/AccessWindowStatic.h"
35#include "src/core/CL/CLValidate.h"
36#include "src/core/helpers/AutoConfiguration.h"
37#include "src/core/helpers/WindowHelpers.h"
Matthew Bentham758b5ba2020-03-05 23:37:48 +000038#include "support/StringSupport.h"
SiCong Li3e363692017-07-04 15:02:10 +010039
40#include <cmath>
41#include <set>
42#include <string>
43
Manuel Bottinicc5171b2019-01-09 17:04:39 +000044namespace arm_compute
45{
46namespace
47{
48std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info)
49{
50 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
51
52 // Output auto initialization if not yet initialized
53 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->dimension(2), rois->dimension(1));
54 auto_init_if_empty((*output), output_shape, 1, input->data_type());
55
56 // Configure kernel window
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +010057 constexpr unsigned int num_elems_processed_per_iteration = 1;
58 Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration));
Manuel Bottinicc5171b2019-01-09 17:04:39 +000059
60 AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration);
61 AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration);
62
63 bool window_changed = update_window_and_padding(win, input_access, output_access);
64 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
65 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
66 return std::make_pair(err, win);
67}
68} // namespace
SiCong Li3e363692017-07-04 15:02:10 +010069
70CLROIPoolingLayerKernel::CLROIPoolingLayerKernel()
71 : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
72{
73}
74
Manuel Bottinicc5171b2019-01-09 17:04:39 +000075void CLROIPoolingLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
SiCong Li3e363692017-07-04 15:02:10 +010076{
Manuel Bottini4c6bd512020-04-08 10:15:51 +010077 configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info);
78}
79
Manuel Bottini2803f702020-04-21 16:20:03 +010080void CLROIPoolingLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
Manuel Bottini4c6bd512020-04-08 10:15:51 +010081{
SiCong Li3e363692017-07-04 15:02:10 +010082 ARM_COMPUTE_ERROR_ON_NULLPTR(input, rois, output);
Manuel Bottinicc5171b2019-01-09 17:04:39 +000083
84 //Validate arguments
85 ARM_COMPUTE_ERROR_ON_NULLPTR(input->info(), rois->info(), output->info());
86 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16);
87 ARM_COMPUTE_ERROR_ON(rois->info()->dimension(0) != 5);
88 ARM_COMPUTE_ERROR_ON(rois->info()->num_dimensions() > 2);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010089 ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(input);
Manuel Bottinicc5171b2019-01-09 17:04:39 +000090 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16);
SiCong Li3e363692017-07-04 15:02:10 +010091 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
SiCong Li3e363692017-07-04 15:02:10 +010092
Manuel Bottinicc5171b2019-01-09 17:04:39 +000093 if(output->info()->total_size() != 0)
94 {
95 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
96 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
97 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
98 ARM_COMPUTE_ERROR_ON(rois->info()->dimension(1) != output->info()->dimension(3));
99 }
SiCong Li3e363692017-07-04 15:02:10 +0100100
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000101 // Configure kernel window
102 auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info);
103 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
SiCong Li3e363692017-07-04 15:02:10 +0100104
105 // Set instance variables
106 _input = input;
107 _rois = rois;
108 _output = output;
109 _pool_info = pool_info;
110
111 // Set build options
112 std::set<std::string> build_opts;
113 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
114 build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())));
115 build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))));
116 build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))));
117 build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))));
118 build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())));
119 build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())));
120 build_opts.emplace(("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale())));
121
122 // Create kernel
123 std::string kernel_name = "roi_pooling_layer";
Manuel Bottini4c6bd512020-04-08 10:15:51 +0100124 _kernel = create_kernel(compile_context, kernel_name, build_opts);
SiCong Li3e363692017-07-04 15:02:10 +0100125
126 // Set static kernel arguments
127 unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array();
128 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
129 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
130
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000131 ICLKernel::configure_internal(win_config.second);
SiCong Li3e363692017-07-04 15:02:10 +0100132}
133
134void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
135{
136 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
137 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
138
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000139 Window slice = window.first_slice_window_3D();
140 Window slice_rois = slice;
141 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor)
142 slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0));
SiCong Li3e363692017-07-04 15:02:10 +0100143 slice.set(Window::DimZ, window[3]);
144
145 // Set arguments
146 unsigned int idx = 0;
147 add_3D_tensor_argument(idx, _input, slice);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000148 add_2D_tensor_argument(idx, _rois, slice_rois);
SiCong Li3e363692017-07-04 15:02:10 +0100149 add_3D_tensor_argument(idx, _output, slice);
Manuel Bottinicc5171b2019-01-09 17:04:39 +0000150 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
151 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
152
Georgios Pinitas275f99c2019-08-23 12:44:11 +0100153 enqueue(queue, *this, slice, lws_hint());
SiCong Li3e363692017-07-04 15:02:10 +0100154}
Michele Di Giorgio4646d2e2019-06-19 12:28:47 +0100155} // namespace arm_compute