blob: 51873ff66a5bf2ee76f6c2c2a7dee1c58bf22185 [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
Georgios Pinitas4e1e7dc2018-02-05 19:04:25 +00002 * Copyright (c) 2017-2018 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
42using namespace arm_compute;
43
44CLROIPoolingLayerKernel::CLROIPoolingLayerKernel()
45 : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f)
46{
47}
48
49void CLROIPoolingLayerKernel::configure(const ICLTensor *input, const ICLROIArray *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info)
50{
51 ARM_COMPUTE_ERROR_ON_NULLPTR(input, rois, output);
Vidhya Sudhan Loganathanf1f49062018-05-25 13:21:26 +010052 ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(input);
SiCong Li3e363692017-07-04 15:02:10 +010053 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
54 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
55 ARM_COMPUTE_ERROR_ON(rois->num_values() == 0);
56
57 // Output auto inizialitation if not yet initialized
58 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->num_values());
59 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
60
61 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
62 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
63 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
64 ARM_COMPUTE_ERROR_ON(rois->num_values() != output->info()->dimension(3));
65
66 // Set instance variables
67 _input = input;
68 _rois = rois;
69 _output = output;
70 _pool_info = pool_info;
71
72 // Set build options
73 std::set<std::string> build_opts;
74 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
75 build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())));
76 build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))));
77 build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))));
78 build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))));
79 build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())));
80 build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())));
81 build_opts.emplace(("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale())));
82
83 // Create kernel
84 std::string kernel_name = "roi_pooling_layer";
85 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
86
87 // Set static kernel arguments
88 unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array();
89 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
90 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
91
92 // Configure kernel window
93 const unsigned int num_elems_processed_per_iteration = 1;
94 Window window = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
95 AccessWindowStatic input_access(input->info(),
96 input->info()->valid_region().start(0),
97 input->info()->valid_region().start(1),
98 input->info()->valid_region().end(0),
99 input->info()->valid_region().end(1));
100 AccessWindowStatic output_access(output->info(), 0, 0, pool_info.pooled_width(), pool_info.pooled_height());
101
102 update_window_and_padding(window, input_access, output_access);
103 output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape()));
104 ICLKernel::configure(window);
105}
106
107void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
108{
109 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
110 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
111
Georgios Pinitas4e1e7dc2018-02-05 19:04:25 +0000112 Window slice = window.first_slice_window_3D();
SiCong Li3e363692017-07-04 15:02:10 +0100113 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROIArray)
114 slice.set(Window::DimZ, window[3]);
115
116 // Set arguments
117 unsigned int idx = 0;
118 add_3D_tensor_argument(idx, _input, slice);
119 add_1D_array_argument<ROI>(idx, _rois, Strides(sizeof(ROI)), 1U, slice);
120 add_3D_tensor_argument(idx, _output, slice);
121 enqueue(queue, *this, slice);
122}