blob: 4e000c61b1019a5d57c188c76bbc95ae44872b8e [file] [log] [blame]
SiCong Li3e363692017-07-04 15:02:10 +01001/*
2 * Copyright (c) 2017 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/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"
29#include "arm_compute/core/CL/ICLArray.h"
30#include "arm_compute/core/CL/ICLTensor.h"
31#include "arm_compute/core/CL/OpenCL.h"
32#include "arm_compute/core/Helpers.h"
33#include "arm_compute/core/TensorInfo.h"
34#include "arm_compute/core/Utils.h"
35#include "arm_compute/core/Validate.h"
36#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);
52 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F16, DataType::F32);
53 ARM_COMPUTE_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0));
54 ARM_COMPUTE_ERROR_ON(rois->num_values() == 0);
55
56 // Output auto inizialitation if not yet initialized
57 TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->num_values());
58 auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type(), input->info()->fixed_point_position());
59
60 ARM_COMPUTE_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
61 ARM_COMPUTE_ERROR_ON((output->info()->dimension(0) != pool_info.pooled_width()) || (output->info()->dimension(1) != pool_info.pooled_height()));
62 ARM_COMPUTE_ERROR_ON(input->info()->dimension(2) != output->info()->dimension(2));
63 ARM_COMPUTE_ERROR_ON(rois->num_values() != output->info()->dimension(3));
64
65 // Set instance variables
66 _input = input;
67 _rois = rois;
68 _output = output;
69 _pool_info = pool_info;
70
71 // Set build options
72 std::set<std::string> build_opts;
73 build_opts.emplace(("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())));
74 build_opts.emplace(("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())));
75 build_opts.emplace(("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))));
76 build_opts.emplace(("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))));
77 build_opts.emplace(("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))));
78 build_opts.emplace(("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())));
79 build_opts.emplace(("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())));
80 build_opts.emplace(("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale())));
81
82 // Create kernel
83 std::string kernel_name = "roi_pooling_layer";
84 _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts));
85
86 // Set static kernel arguments
87 unsigned int idx = 2 * num_arguments_per_3D_tensor() + num_arguments_per_1D_array();
88 add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]);
89 add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]);
90
91 // Configure kernel window
92 const unsigned int num_elems_processed_per_iteration = 1;
93 Window window = calculate_max_window(*output->info(), Steps(num_elems_processed_per_iteration));
94 AccessWindowStatic input_access(input->info(),
95 input->info()->valid_region().start(0),
96 input->info()->valid_region().start(1),
97 input->info()->valid_region().end(0),
98 input->info()->valid_region().end(1));
99 AccessWindowStatic output_access(output->info(), 0, 0, pool_info.pooled_width(), pool_info.pooled_height());
100
101 update_window_and_padding(window, input_access, output_access);
102 output_access.set_valid_region(window, ValidRegion(Coordinates(), output->info()->tensor_shape()));
103 ICLKernel::configure(window);
104}
105
106void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue)
107{
108 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
109 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window);
110
111 Window slice(window);
112 // Parallelize spatially and across the fourth dimension of the output tensor (also across ROIArray)
113 slice.set(Window::DimZ, window[3]);
114
115 // Set arguments
116 unsigned int idx = 0;
117 add_3D_tensor_argument(idx, _input, slice);
118 add_1D_array_argument<ROI>(idx, _rois, Strides(sizeof(ROI)), 1U, slice);
119 add_3D_tensor_argument(idx, _output, slice);
120 enqueue(queue, *this, slice);
121}