Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 1 | /* |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +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/CLPriorBoxLayerKernel.h" |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/ICLTensor.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Utils.h" |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/CL/CLValidate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 37 | #include "support/StringSupport.h" |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 38 | |
| 39 | using namespace arm_compute::misc::shape_calculator; |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
| 43 | namespace |
| 44 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 45 | Status validate_arguments(const ITensorInfo *input1, |
| 46 | const ITensorInfo *input2, |
| 47 | const ITensorInfo *output, |
| 48 | const PriorBoxLayerInfo &info) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 49 | { |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input1, input2, output); |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input1, 1, DataType::F32); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input1, input2); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input1, input2); |
| 54 | |
| 55 | // Check variances |
| 56 | const int var_size = info.variances().size(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 57 | if (var_size > 1) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 58 | { |
| 59 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size != 4, "Must provide 4 variance values"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | for (int i = 0; i < var_size; ++i) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(var_size <= 0, "Must be greater than 0"); |
| 63 | } |
| 64 | } |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[0] < 0.f, "Step x should be greater or equal to 0"); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.steps()[1] < 0.f, "Step y should be greater or equal to 0"); |
| 67 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 68 | if (!info.max_sizes().empty()) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 69 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 70 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes().size() != info.min_sizes().size(), |
| 71 | "Max and min sizes dimensions should match"); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 74 | for (unsigned int i = 0; i < info.max_sizes().size(); ++i) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 75 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(info.max_sizes()[i] < info.min_sizes()[i], |
| 77 | "Max size should be greater than min size"); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | if (output != nullptr && output->total_size() != 0) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 81 | { |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 82 | ARM_COMPUTE_RETURN_ERROR_ON(output->dimension(1) != 2); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | return Status{}; |
| 86 | } |
| 87 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 88 | std::pair<Status, Window> validate_and_configure_window(const ITensorInfo *input1, |
| 89 | const ITensorInfo *input2, |
| 90 | ITensorInfo *output, |
| 91 | const PriorBoxLayerInfo &info, |
| 92 | int num_priors) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 93 | { |
| 94 | ARM_COMPUTE_UNUSED(input2); |
| 95 | // Output tensor auto initialization if not yet initialized |
| 96 | TensorShape output_shape = compute_prior_box_shape(*input1, info); |
| 97 | auto_init_if_empty(*output, output_shape, 1, input1->data_type()); |
| 98 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 99 | const unsigned int num_elems_processed_per_iteration = 4 * num_priors; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 100 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 101 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 102 | bool window_changed = update_window_and_padding(win, output_access); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 103 | Status err = |
| 104 | (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 105 | return std::make_pair(err, win); |
| 106 | } |
| 107 | } // namespace |
| 108 | |
| 109 | CLPriorBoxLayerKernel::CLPriorBoxLayerKernel() |
| 110 | : _input1(nullptr), _input2(nullptr), _output(nullptr), _info(), _num_priors(), _min(), _max(), _aspect_ratios() |
| 111 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 112 | _type = CLKernelType::ELEMENTWISE; |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 113 | } |
| 114 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 115 | void CLPriorBoxLayerKernel::configure(const ICLTensor *input1, |
| 116 | const ICLTensor *input2, |
| 117 | ICLTensor *output, |
| 118 | const PriorBoxLayerInfo &info, |
| 119 | cl::Buffer *min, |
| 120 | cl::Buffer *max, |
| 121 | cl::Buffer *aspect_ratios) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 122 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 123 | configure(CLKernelLibrary::get().get_compile_context(), input1, input2, output, info, min, max, aspect_ratios); |
| 124 | } |
| 125 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | void CLPriorBoxLayerKernel::configure(const CLCompileContext &compile_context, |
| 127 | const ICLTensor *input1, |
| 128 | const ICLTensor *input2, |
| 129 | ICLTensor *output, |
| 130 | const PriorBoxLayerInfo &info, |
| 131 | cl::Buffer *min, |
| 132 | cl::Buffer *max, |
| 133 | cl::Buffer *aspect_ratios) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 134 | { |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 135 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 136 | |
| 137 | _input1 = input1; |
| 138 | _input2 = input2; |
| 139 | _output = output; |
| 140 | _info = info; |
| 141 | _min = min; |
| 142 | _max = max; |
| 143 | _aspect_ratios = aspect_ratios; |
| 144 | |
| 145 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input1->info(), input2->info(), output->info(), info)); |
| 146 | |
| 147 | // Calculate number of aspect ratios |
| 148 | _num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
| 149 | |
| 150 | const DataLayout data_layout = input1->info()->data_layout(); |
| 151 | |
| 152 | const int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 153 | const int height_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 154 | |
| 155 | const int layer_width = input1->info()->dimension(width_idx); |
| 156 | const int layer_height = input1->info()->dimension(height_idx); |
| 157 | |
| 158 | int img_width = info.img_size().x; |
| 159 | int img_height = info.img_size().y; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 160 | if (img_width == 0 || img_height == 0) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 161 | { |
| 162 | img_width = input2->info()->dimension(width_idx); |
| 163 | img_height = input2->info()->dimension(height_idx); |
| 164 | } |
| 165 | |
| 166 | float step_x = info.steps()[0]; |
| 167 | float step_y = info.steps()[0]; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 168 | if (step_x == 0.f || step_y == 0.f) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 169 | { |
| 170 | step_x = static_cast<float>(img_width) / layer_width; |
| 171 | step_y = static_cast<float>(img_height) / layer_height; |
| 172 | } |
| 173 | |
| 174 | // Set build options |
| 175 | CLBuildOptions build_opts; |
| 176 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(input1->info()->data_type())); |
| 177 | build_opts.add_option("-DWIDTH=" + support::cpp11::to_string(img_width)); |
| 178 | build_opts.add_option("-DHEIGHT=" + support::cpp11::to_string(img_height)); |
| 179 | build_opts.add_option("-DLAYER_WIDTH=" + support::cpp11::to_string(layer_width)); |
| 180 | build_opts.add_option("-DLAYER_HEIGHT=" + support::cpp11::to_string(layer_height)); |
| 181 | build_opts.add_option("-DSTEP_X=" + support::cpp11::to_string(step_x)); |
| 182 | build_opts.add_option("-DSTEP_Y=" + support::cpp11::to_string(step_y)); |
| 183 | build_opts.add_option("-DNUM_PRIORS=" + support::cpp11::to_string(_num_priors)); |
| 184 | build_opts.add_option("-DOFFSET=" + support::cpp11::to_string(info.offset())); |
| 185 | build_opts.add_option_if(info.clip(), "-DIN_PLACE"); |
| 186 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 187 | if (info.variances().size() > 1) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 188 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 189 | for (unsigned int i = 0; i < info.variances().size(); ++i) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 190 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 191 | build_opts.add_option("-DVARIANCE_" + support::cpp11::to_string(i) + "=" + |
| 192 | support::cpp11::to_string(info.variances().at(i))); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | else |
| 196 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 197 | for (unsigned int i = 0; i < 4; ++i) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 198 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 199 | build_opts.add_option("-DVARIANCE_" + support::cpp11::to_string(i) + "=" + |
| 200 | support::cpp11::to_string(info.variances().at(0))); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 204 | unsigned int idx = num_arguments_per_2D_tensor(); |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 205 | _kernel = create_kernel(compile_context, "prior_box_layer_nchw", build_opts.options()); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 206 | |
| 207 | _kernel.setArg(idx++, *_min); |
| 208 | _kernel.setArg(idx++, *_max); |
| 209 | _kernel.setArg(idx++, *_aspect_ratios); |
| 210 | _kernel.setArg<unsigned int>(idx++, info.min_sizes().size()); |
| 211 | _kernel.setArg<unsigned int>(idx++, info.max_sizes().size()); |
| 212 | _kernel.setArg<unsigned int>(idx++, info.aspect_ratios().size()); |
| 213 | |
| 214 | // Configure kernel window |
| 215 | auto win_config = validate_and_configure_window(input1->info(), input2->info(), output->info(), info, _num_priors); |
| 216 | |
| 217 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 218 | ICLKernel::configure_internal(win_config.second); |
| 219 | } |
| 220 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 221 | Status CLPriorBoxLayerKernel::validate(const ITensorInfo *input1, |
| 222 | const ITensorInfo *input2, |
| 223 | const ITensorInfo *output, |
| 224 | const PriorBoxLayerInfo &info) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 225 | { |
| 226 | ARM_COMPUTE_ERROR_ON_NULLPTR(input1, input2, output); |
| 227 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input1, input2, output, info)); |
| 228 | const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 229 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input1->clone().get(), input2->clone().get(), |
| 230 | output->clone().get(), info, num_priors) |
| 231 | .first); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 232 | |
| 233 | return Status{}; |
| 234 | } |
| 235 | |
| 236 | void CLPriorBoxLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 237 | { |
| 238 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 239 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 240 | |
| 241 | queue.enqueueWriteBuffer(*_min, CL_TRUE, 0, _info.min_sizes().size() * sizeof(float), _info.min_sizes().data()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 242 | queue.enqueueWriteBuffer(*_aspect_ratios, CL_TRUE, 0, _info.aspect_ratios().size() * sizeof(float), |
| 243 | _info.aspect_ratios().data()); |
| 244 | if (!_info.max_sizes().empty()) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 245 | { |
| 246 | queue.enqueueWriteBuffer(*_max, CL_TRUE, 0, _info.max_sizes().size() * sizeof(float), _info.max_sizes().data()); |
| 247 | } |
| 248 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 249 | Window slice = window.first_slice_window_2D(); |
| 250 | slice.set(Window::DimY, Window::Dimension(0, _output->info()->dimension(1), 2)); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 251 | |
Michalis Spyrou | 3112e33 | 2018-11-21 15:44:55 +0000 | [diff] [blame] | 252 | unsigned int idx = 0; |
| 253 | add_2D_tensor_argument(idx, _output, slice); |
Georgios Pinitas | 275f99c | 2019-08-23 12:44:11 +0100 | [diff] [blame] | 254 | enqueue(queue, *this, slice, lws_hint()); |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 255 | } |
| 256 | } // namespace arm_compute |