giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2018 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/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" |
| 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 42 | 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] | 43 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 44 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, rois, output); |
| 45 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, rois); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(0) != 5); |
| 47 | ARM_COMPUTE_RETURN_ERROR_ON(rois->num_dimensions() > 2); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
| 49 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::F32, DataType::F16); |
Michele Di Giorgio | c8df89f | 2018-11-16 10:02:26 +0000 | [diff] [blame] | 50 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NCHW); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 51 | 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] | 52 | |
| 53 | if(output->total_size() != 0) |
| 54 | { |
| 55 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
Michele Di Giorgio | c8df89f | 2018-11-16 10:02:26 +0000 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 57 | ARM_COMPUTE_RETURN_ERROR_ON((output->dimension(0) != pool_info.pooled_width()) || (output->dimension(1) != pool_info.pooled_height())); |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON(input->dimension(2) != output->dimension(2)); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON(rois->dimension(1) != output->dimension(3)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | return Status{}; |
| 63 | } |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 64 | |
| 65 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *rois, ITensorInfo *output, const ROIPoolingLayerInfo &pool_info) |
| 66 | { |
| 67 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 68 | |
| 69 | // Output auto inizialitation if not yet initialized |
| 70 | TensorShape output_shape(pool_info.pooled_width(), pool_info.pooled_height(), input->dimension(2), rois->dimension(1)); |
| 71 | auto_init_if_empty((*output), output_shape, 1, input->data_type()); |
| 72 | |
| 73 | // Configure kernel window |
| 74 | const unsigned int num_elems_processed_per_iteration = 1; |
| 75 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 76 | |
| 77 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 78 | AccessWindowHorizontal input_access(input, input->valid_region().start(0), num_elems_processed_per_iteration); |
| 79 | |
| 80 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 81 | output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); |
| 82 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
| 83 | return std::make_pair(err, win); |
| 84 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 85 | } // namespace |
| 86 | |
| 87 | CLROIAlignLayerKernel::CLROIAlignLayerKernel() |
| 88 | : _input(nullptr), _output(nullptr), _rois(nullptr), _pool_info(0, 0, 0.f) |
| 89 | { |
| 90 | } |
| 91 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 92 | 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] | 93 | { |
| 94 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output, rois); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 95 | 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] | 96 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 97 | // Configure kernel window |
| 98 | auto win_config = validate_and_configure_window(input->info(), rois->info(), output->info(), pool_info); |
| 99 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 100 | |
| 101 | _input = input; |
| 102 | _output = output; |
| 103 | _rois = rois; |
| 104 | _pool_info = pool_info; |
| 105 | |
| 106 | // Set build options |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 107 | CLBuildOptions build_opts; |
| 108 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input->info()->data_type())); |
| 109 | build_opts.add_option("-DDATA_SIZE=" + get_data_size_from_data_type(input->info()->data_type())); |
| 110 | build_opts.add_option("-DMAX_DIM_X=" + support::cpp11::to_string(_input->info()->dimension(Window::DimX))); |
| 111 | build_opts.add_option("-DMAX_DIM_Y=" + support::cpp11::to_string(_input->info()->dimension(Window::DimY))); |
| 112 | build_opts.add_option("-DMAX_DIM_Z=" + support::cpp11::to_string(_input->info()->dimension(Window::DimZ))); |
| 113 | build_opts.add_option("-DPOOLED_DIM_X=" + support::cpp11::to_string(pool_info.pooled_width())); |
| 114 | build_opts.add_option("-DPOOLED_DIM_Y=" + support::cpp11::to_string(pool_info.pooled_height())); |
| 115 | build_opts.add_option("-DSPATIAL_SCALE=" + float_to_string_with_full_precision(pool_info.spatial_scale())); |
| 116 | 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] | 117 | |
| 118 | // Create kernel |
| 119 | std::string kernel_name = "roi_align_layer"; |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 120 | _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] | 121 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 122 | ICLKernel::configure_internal(win_config.second); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 123 | } |
| 124 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 125 | 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] | 126 | { |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 127 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, rois, output, pool_info)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 128 | return Status{}; |
| 129 | } |
| 130 | |
| 131 | void CLROIAlignLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 132 | { |
| 133 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 134 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 135 | |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 136 | Window slice = window.first_slice_window_3D(); |
| 137 | Window slice_rois = slice; |
| 138 | // Parallelize spatially and across the fourth dimension of the output tensor (also across ROITensor) |
| 139 | slice_rois.set_dimension_step(Window::DimX, _rois->info()->dimension(0)); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 140 | slice.set(Window::DimZ, window[3]); |
| 141 | |
| 142 | // Set arguments |
| 143 | unsigned int idx = 0; |
| 144 | add_3D_tensor_argument(idx, _input, slice); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 145 | add_2D_tensor_argument(idx, _rois, slice_rois); |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 146 | add_3D_tensor_argument(idx, _output, slice); |
Manuel Bottini | 60f0a41 | 2018-10-24 17:27:02 +0100 | [diff] [blame] | 147 | add_argument<cl_uint>(idx, _input->info()->strides_in_bytes()[3]); |
| 148 | add_argument<cl_uint>(idx, _output->info()->strides_in_bytes()[3]); |
| 149 | |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 150 | enqueue(queue, *this, slice); |
| 151 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 152 | } // namespace arm_compute |