SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 1 | /* |
Michalis Spyrou | 702dc0c | 2021-03-19 15:06:07 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 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 | */ |
Sang-Hoon Park | bef7fa2 | 2020-10-21 15:58:54 +0100 | [diff] [blame] | 24 | #include "src/core/CL/kernels/CLROIPoolingLayerKernel.h" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 25 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/CL/OpenCL.h" |
| 30 | #include "arm_compute/core/Helpers.h" |
| 31 | #include "arm_compute/core/TensorInfo.h" |
| 32 | #include "arm_compute/core/Utils.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/CL/CLValidate.h" |
| 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 36 | #include "support/StringSupport.h" |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 37 | |
Gian Marco Iodice | 3eb5d29 | 2021-04-23 10:30:43 +0100 | [diff] [blame] | 38 | #include <cfloat> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 39 | #include <cmath> |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 40 | #include <string> |
| 41 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 42 | namespace arm_compute |
| 43 | { |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 44 | CLROIPoolingLayerKernel::CLROIPoolingLayerKernel() |
| 45 | : _input(nullptr), _rois(nullptr), _output(nullptr), _pool_info(0, 0, 0.f) |
| 46 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 47 | _type = CLKernelType::ELEMENTWISE; |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 48 | } |
| 49 | |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 50 | Status CLROIPoolingLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, const ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) |
| 51 | { |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output); |
| 53 | |
| 54 | //Validate arguments |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output); |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::U16); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2); |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 60 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16, DataType::QASYMM8); |
| 61 | ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); |
| 62 | |
| 63 | if(output->total_size() != 0) |
| 64 | { |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) || (output->dimension(1) != pool_info.pooled_height())); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2)); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(1) != output->dimension(3)); |
| 69 | } |
| 70 | |
| 71 | return Status{}; |
| 72 | } |
| 73 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 74 | void CLROIPoolingLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info) |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 75 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 76 | configure(CLKernelLibrary::get().get_compile_context(), input, rois, output, pool_info); |
| 77 | } |
| 78 | |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 79 | void CLROIPoolingLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, const ICLTensor *rois, const ICLTensor *output, const ROIPoolingLayerInfo &pool_info) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 80 | { |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 81 | ARM_COMPUTE_ERROR_THROW_ON(CLROIPoolingLayerKernel::validate(input->info(), rois->info(), output->info(), pool_info)); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 82 | |
Gian Marco Iodice | 3eb5d29 | 2021-04-23 10:30:43 +0100 | [diff] [blame] | 83 | auto padding_info = get_padding_info({ input, rois, output }); |
| 84 | |
| 85 | // Output auto initialization if not yet initialized |
| 86 | TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->info()->dimension(2), rois->info()->dimension(1)); |
| 87 | auto_init_if_empty(*(output->info()), output_shape, 1, input->info()->data_type(), output->info()->quantization_info()); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 88 | |
| 89 | // Set instance variables |
| 90 | _input = input; |
| 91 | _rois = rois; |
| 92 | _output = output; |
| 93 | _pool_info = pool_info; |
| 94 | |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 95 | const DataType data_type = input->info()->data_type(); |
| 96 | const bool is_qasymm = is_data_type_quantized_asymmetric(data_type); |
| 97 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 98 | // Set build options |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 99 | CLBuildOptions build_opts; |
| 100 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
| 101 | build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(data_type)); |
| 102 | build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))); |
| 103 | build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))); |
| 104 | build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))); |
| 105 | build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())); |
| 106 | build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())); |
| 107 | build_opts.add_option("-DSPATIAL_SCALE=" + support::cpp11::to_string(pool_info.spatial_scale())); |
| 108 | |
| 109 | if(is_qasymm) |
| 110 | { |
| 111 | // Determine quantization info scale, offset |
| 112 | UniformQuantizationInfo uqinfo = UniformQuantizationInfo(); |
| 113 | uqinfo = compute_requantization_scale_offset(_input->info()->quantization_info().uniform(), _output->info()->quantization_info().uniform()); |
| 114 | build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(uqinfo.offset)); |
| 115 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(uqinfo.scale)); |
| 116 | |
| 117 | // Specify minimum possible value of datatype |
| 118 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(0)); |
| 119 | } |
Gian Marco Iodice | 3eb5d29 | 2021-04-23 10:30:43 +0100 | [diff] [blame] | 120 | else |
| 121 | { |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 122 | // Specify min value of F32 datatype |
| 123 | build_opts.add_option("-DMIN_VALUE=" + support::cpp11::to_string(-FLT_MAX)); |
| 124 | } |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 125 | |
Gian Marco Iodice | 3eb5d29 | 2021-04-23 10:30:43 +0100 | [diff] [blame] | 126 | Window win = calculate_max_window(*(output->info()), Steps()); |
| 127 | ICLKernel::configure_internal(win); |
| 128 | |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 129 | // Create kernel |
| 130 | std::string kernel_name = "roi_pooling_layer"; |
Suhail Munshi | 4ed7b39 | 2021-03-22 13:13:55 +0000 | [diff] [blame] | 131 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 132 | |
Gian Marco Iodice | 3eb5d29 | 2021-04-23 10:30:43 +0100 | [diff] [blame] | 133 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void CLROIPoolingLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 137 | { |
| 138 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 139 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 140 | |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 141 | Window slice = window.first_slice_window_3D(); |
| 142 | Window slice_rois = slice; |
| 143 | // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor) |
| 144 | slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0)); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 145 | slice.set(Window::DimZ, window[3]); |
| 146 | |
| 147 | // Set arguments |
| 148 | unsigned int idx = 0; |
| 149 | add_3D_tensor_argument(idx, _input, slice); |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 150 | add_2D_tensor_argument(idx, _rois, slice_rois); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 151 | add_3D_tensor_argument(idx, _output, slice); |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 152 | add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]); |
| 153 | add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]); |
| 154 | |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 155 | enqueue(queue, *this, slice, lws_hint()); |
SiCong Li | 3e36369 | 2017-07-04 15:02:10 +0100 | [diff] [blame] | 156 | } |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 157 | } // namespace arm_compute |