giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1 | /* |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 ARM Limited. |
giuros01 | 1887081 | 2018-09-13 09:31:40 +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/CLROIAlignLayerKernel.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/CLValidate.h" |
| 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" |
| 36 | #include "arm_compute/core/Window.h" |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 37 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
| 38 | |
| 39 | using namespace arm_compute::misc::shape_calculator; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 46 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8, DataType::F32, DataType::F16); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NHWC, DataLayout::NCHW); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON((pool_info.pooled_width() == 0) || (pool_info.pooled_height() == 0)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 54 | |
| 55 | if(output->total_size() != 0) |
| 56 | { |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michele Di Giorgio | c8df89f | 2018-11-16 10:02:26 +0000 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(compute_roi_align_shape(*input, *rois, pool_info), output->tensor_shape()); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 60 | } |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 61 | |
| 62 | if(input->data_type() == DataType::QASYMM8) |
| 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(rois, 1, DataType::QASYMM16); |
| 65 | |
| 66 | const UniformQuantizationInfo rois_qinfo = rois->quantization_info().uniform(); |
| 67 | ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.scale != 0.125f); |
| 68 | ARM_COMPUTE_RETURN_ERROR_ON(rois_qinfo.offset != 0); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois); |
| 73 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 74 | return Status{}; |
| 75 | } |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 76 | |
| 77 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) |
| 78 | { |
| 79 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 80 | |
| 81 | // Output auto inizialitation if not yet initialized |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 82 | const TensorShape output_shape = compute_roi_align_shape(*input, *rois, pool_info); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 83 | auto_init_if_empty((*output), output_shape, 1, input->data_type()); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 84 | output->set_data_layout(input->data_layout()); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 85 | |
| 86 | // Configure kernel window |
Michele Di Giorgio | 4646d2e | 2019-06-19 12:28:47 +0100 | [diff] [blame] | 87 | constexpr unsigned int num_elems_processed_per_iteration = 1; |
| 88 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 89 | |
| 90 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 91 | AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration); |
| 92 | |
| 93 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 94 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 95 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 96 | return std::make_pair(err, win); |
| 97 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 98 | } // namespace |
| 99 | |
| 100 | CLROIAlignLayerKernel::CLROIAlignLayerKernel() |
| 101 | : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f) |
| 102 | { |
| 103 | } |
| 104 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 105 | void CLROIAlignLayerKernel::configure(const ICLTensor *input, const ICLTensor *rois, ICLTensor *output, const ROIPoolingLayerInfo &pool_info) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 106 | { |
| 107 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 108 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), rois->info(), output->info(), pool_info)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 109 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 110 | // Configure kernel window |
| 111 | auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info); |
| 112 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 113 | |
| 114 | _input = input; |
| 115 | _output = output; |
| 116 | _rois = rois; |
| 117 | _pool_info = pool_info; |
| 118 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 119 | const DataType data_type = input->info()->data_type(); |
| 120 | const bool is_qasymm = is_data_type_quantized_asymmetric(data_type); |
| 121 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 122 | // Set build options |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 123 | CLBuildOptions build_opts; |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 124 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 125 | build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 126 | build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::WIDTH)))); |
| 127 | build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::HEIGHT)))); |
| 128 | build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(get_data_layout_dimension_index(input->info()->data_layout(), DataLayoutDimension::CHANNEL)))); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 129 | build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())); |
| 130 | build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())); |
| 131 | build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale())); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 132 | build_opts.add_option_if(input->info()->data_layout() == DataLayout::NHWC, "-DNHWC"); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 133 | build_opts.add_option_if(pool_info.sampling_ratio() > 0, "-DSAMPLING_RATIO=" + support::cpp11::to_string(pool_info.sampling_ratio())); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 134 | |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 135 | if(is_qasymm) |
| 136 | { |
| 137 | const UniformQuantizationInfo iq_info = input->info()->quantization_info().uniform(); |
| 138 | const UniformQuantizationInfo roisq_info = rois->info()->quantization_info().uniform(); |
| 139 | const UniformQuantizationInfo oq_info = output->info()->quantization_info().uniform(); |
| 140 | |
| 141 | build_opts.add_option("-DOFFSET_IN=" + float_to_string_with_full_precision(iq_info.offset)); |
| 142 | build_opts.add_option("-DSCALE_IN=" + float_to_string_with_full_precision(iq_info.scale)); |
| 143 | build_opts.add_option("-DOFFSET_ROIS=" + float_to_string_with_full_precision(roisq_info.offset)); |
| 144 | build_opts.add_option("-DSCALE_ROIS=" + float_to_string_with_full_precision(roisq_info.scale)); |
| 145 | build_opts.add_option("-DOFFSET_OUT=" + float_to_string_with_full_precision(oq_info.offset)); |
| 146 | build_opts.add_option("-DSCALE_OUT=" + float_to_string_with_full_precision(oq_info.scale)); |
| 147 | } |
| 148 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 149 | // Create kernel |
Michele Di Giorgio | 578a9fc | 2019-08-23 11:49:04 +0100 | [diff] [blame] | 150 | const std::string kernel_name = (is_qasymm) ? "roi_align_layer_quantized" : "roi_align_layer"; |
| 151 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 152 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 153 | ICLKernel::configure_internal(win_config.second); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 154 | } |
| 155 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 156 | Status CLROIAlignLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 157 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 158 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 159 | return Status{}; |
| 160 | } |
| 161 | |
| 162 | void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 163 | { |
| 164 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 165 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 166 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 167 | Window slice = window.first_slice_window_3D(); |
| 168 | Window slice_rois = slice; |
| 169 | // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor) |
| 170 | slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0)); |
George Wort | 44b4e97 | 2019-01-08 11:41:54 +0000 | [diff] [blame] | 171 | slice.set(get_data_layout_dimension_index(_input->info()->data_layout(), DataLayoutDimension::CHANNEL), window[3]); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 172 | |
| 173 | // Set arguments |
| 174 | unsigned int idx = 0; |
| 175 | add_3D_tensor_argument(idx, _input, slice); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 176 | add_2D_tensor_argument(idx, _rois, slice_rois); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 177 | add_3D_tensor_argument(idx, _output, slice); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 178 | add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]); |
| 179 | add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]); |
| 180 | |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 181 | enqueue(queue, *this, slice, lws_hint()); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 182 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 183 | } // namespace arm_compute |