SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | 4e1e7dc | 2018-02-05 19:04:25 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 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" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/CLValidate.h" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 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" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 36 | #include "arm_compute/core/Window.h" |
| 37 | |
| 38 | #include <cmath> |
| 39 | #include <set> |
| 40 | #include <string> |
| 41 | |
| 42 | using namespace arm_compute; |
| 43 | |
| 44 | CLROIPoolingLayerKernel::CLROIPoolingLayerKernel() |
| 45 | : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | void 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 Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_ERROR_ON_F16_UNSUPPORTED(input); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 53 | 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()); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 59 | auto_init_if_empty(*output->info(), output_shape, 1, input->info()->data_type()); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 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())); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 104 | ICLKernel::configure_internal(window); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void 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 Pinitas | 4e1e7dc | 2018-02-05 19:04:25 +0000 | [diff] [blame] | 112 | Window slice = window.first_slice_window_3D(); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 113 | // 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 | } |