Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2019-2021, 2023 Arm Limited. |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +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/CLPadLayerKernel.h" |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
Michele Di Giorgio | f6f7876 | 2020-07-06 11:27:21 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/CL/ICLTensor.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
Michele Di Giorgio | 4c268b9 | 2019-09-20 14:01:48 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 30 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 31 | #include "src/core/helpers/AutoConfiguration.h" |
| 32 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 33 | #include "support/StringSupport.h" |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace |
| 38 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 39 | Status validate_arguments(const ITensorInfo *input, |
| 40 | const ITensorInfo *output, |
| 41 | const PaddingList &padding, |
| 42 | PixelValue constant_value, |
| 43 | PaddingMode mode) |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 44 | { |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_UNUSED(constant_value); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON((padding.size() < 1) || (padding.size() > input->num_dimensions())); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 49 | if (mode == PaddingMode::REFLECT || mode == PaddingMode::SYMMETRIC) |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 3); |
| 52 | |
| 53 | const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 54 | for (size_t i = 0; i < padding.size(); ++i) |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 55 | { |
| 56 | ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).first > (input->dimension(i) - is_reflect)); |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(padding.at(i).second > (input->dimension(i) - is_reflect)); |
| 58 | } |
| 59 | } |
| 60 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 61 | if (output->total_size() > 0) |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 62 | { |
| 63 | TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding); |
| 64 | |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(output, input); |
| 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), padded_shape); |
| 67 | } |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 68 | |
| 69 | return Status{}; |
| 70 | } |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 71 | } // namespace |
| 72 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 73 | CLPadLayerKernel::CLPadLayerKernel() : _input(nullptr), _output(nullptr), _4d_enabled(false) |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 74 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 75 | _type = CLKernelType::ELEMENTWISE; |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | void CLPadLayerKernel::configure( |
| 79 | const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 80 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 81 | configure(CLKernelLibrary::get().get_compile_context(), input, output, padding, constant_value, mode); |
| 82 | } |
| 83 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 84 | void CLPadLayerKernel::configure(const CLCompileContext &compile_context, |
| 85 | const ICLTensor *input, |
| 86 | ICLTensor *output, |
| 87 | const PaddingList &padding, |
| 88 | PixelValue constant_value, |
| 89 | PaddingMode mode) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 90 | { |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 91 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 92 | auto_init_if_empty(*output->info(), |
| 93 | input->info()->clone()->set_tensor_shape( |
| 94 | misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding))); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 95 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode)); |
| 96 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 97 | auto padding_info = get_padding_info({input, output}); |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 98 | |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 99 | _input = input; |
| 100 | _output = output; |
| 101 | _4d_enabled = (mode == PaddingMode::CONSTANT) && (padding.size() > 3); |
| 102 | |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 103 | // Set build options |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 104 | const DataType &data_type = input->info()->data_type(); |
| 105 | const unsigned int input_width = input->info()->dimension(0); |
| 106 | const unsigned int input_height = input->info()->dimension(1); |
| 107 | const unsigned int input_depth = input->info()->dimension(2); |
| 108 | const unsigned int pad_x_before = padding.at(0).first; |
| 109 | const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0; |
| 110 | const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0; |
| 111 | const unsigned int vec_size = adjust_vec_size( |
| 112 | std::min(16U, 32U / static_cast<unsigned int>(element_size_from_data_type(input->info()->data_type()))), |
| 113 | input_width); |
| 114 | const unsigned int pad_right_start = input_width + pad_x_before; |
| 115 | const unsigned int pad_x_before_remainder = pad_x_before % vec_size; |
| 116 | const unsigned int vec_size_leftover_write = |
| 117 | vec_size - (ceil_to_multiple(output->info()->dimension(0), vec_size) - output->info()->dimension(0)); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 118 | |
| 119 | CLBuildOptions build_opts; |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 120 | build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 121 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size)); |
| 122 | build_opts.add_option("-DPAD_X_BEFORE=" + support::cpp11::to_string(pad_x_before)); |
| 123 | build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width)); |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 124 | build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder)); |
| 125 | build_opts.add_option("-DVEC_SIZE_LEFTOVER_WRITE=" + support::cpp11::to_string(vec_size_leftover_write)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 126 | if (padding.size() > 1) |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 127 | { |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 128 | build_opts.add_option("-DPAD_Y_BEFORE=" + support::cpp11::to_string(pad_y_before)); |
| 129 | build_opts.add_option("-DSRC_HEIGHT=" + support::cpp11::to_string(input_height)); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 130 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 131 | if (padding.size() > 2) |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 132 | { |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 133 | build_opts.add_option("-DPAD_Z_BEFORE=" + support::cpp11::to_string(pad_z_before)); |
| 134 | build_opts.add_option("-DSRC_DEPTH=" + support::cpp11::to_string(input_depth)); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 135 | } |
| 136 | } |
| 137 | |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 138 | std::string kernel_name = "pad_layer_"; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 139 | switch (mode) |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 140 | { |
| 141 | case PaddingMode::CONSTANT: |
| 142 | { |
| 143 | kernel_name += "constant"; |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 144 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | const unsigned int vec_size_leftover_read = |
| 146 | vec_size - (ceil_to_multiple(pad_right_start, vec_size) - pad_right_start); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 147 | |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 148 | build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, data_type)); |
| 149 | build_opts.add_option("-DVEC_SIZE_LEFTOVER_READ=" + support::cpp11::to_string(vec_size_leftover_read)); |
| 150 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 151 | if (pad_x_before >= vec_size) |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 152 | { |
| 153 | build_opts.add_option("-DTHREADS_TO_SKIP_BEFORE=" + support::cpp11::to_string(pad_x_before / vec_size)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 154 | build_opts.add_option("-DTHREADS_TO_SKIP_AFTER=" + |
| 155 | support::cpp11::to_string(pad_right_start / vec_size)); |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 156 | } |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 157 | if (_4d_enabled) |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 158 | { |
| 159 | build_opts.add_option("-DPAD_W_BEFORE=" + support::cpp11::to_string(padding.at(3).first)); |
| 160 | build_opts.add_option("-DSRC_BATCH=" + support::cpp11::to_string(input->info()->dimension(3))); |
| 161 | } |
| 162 | |
| 163 | break; |
| 164 | } |
| 165 | case PaddingMode::SYMMETRIC: |
| 166 | case PaddingMode::REFLECT: |
| 167 | { |
| 168 | kernel_name += "symmetric_reflect"; |
| 169 | |
| 170 | const auto is_reflect = static_cast<unsigned int>(mode == PaddingMode::REFLECT); |
| 171 | |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 172 | const unsigned int pad_x_after_remainder = pad_right_start % vec_size; |
| 173 | const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 174 | const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 175 | |
| 176 | build_opts.add_option("-DIS_REFLECT=" + support::cpp11::to_string(is_reflect)); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 177 | build_opts.add_option("-DPAD_X_AFTER_REMAINDER=" + support::cpp11::to_string(pad_x_after_remainder)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 178 | build_opts.add_option("-DPAD_X_BEFORE_REMAINDER_REFL=" + |
| 179 | support::cpp11::to_string((pad_x_before_remainder + is_reflect) % vec_size)); |
| 180 | build_opts.add_option("-DPAD_X_AFTER_REMAINDER_REFL=" + |
| 181 | support::cpp11::to_string((pad_x_after_remainder - is_reflect) % vec_size)); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 182 | build_opts.add_option("-DAFTER_PAD_FACT_X=" + support::cpp11::to_string(after_pad_fact_x)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 183 | build_opts.add_option_if(after_pad_fact_x < output_last_x, |
| 184 | "-DAFTER_PAD_REM=" + support::cpp11::to_string(after_pad_fact_x % vec_size)); |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 185 | |
| 186 | break; |
| 187 | } |
| 188 | default: |
| 189 | ARM_COMPUTE_ERROR("Padding mode not supported."); |
| 190 | } |
| 191 | |
| 192 | // Create kernel |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 193 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 194 | |
| 195 | // Configure window |
| 196 | Window win = calculate_max_window(*output->info(), Steps(vec_size)); |
| 197 | ICLKernel::configure_internal(win); |
| 198 | |
| 199 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 202 | Status CLPadLayerKernel::validate(const ITensorInfo *input, |
| 203 | const ITensorInfo *output, |
| 204 | const PaddingList &padding, |
| 205 | PixelValue constant_value, |
| 206 | PaddingMode mode) |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 207 | { |
| 208 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode)); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 209 | return Status{}; |
| 210 | } |
| 211 | |
| 212 | void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 213 | { |
| 214 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 215 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 216 | |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 217 | Window slice = window.first_slice_window_3D(); |
| 218 | unsigned int batch = 0; |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 219 | do |
| 220 | { |
| 221 | unsigned int idx = 0; |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 222 | add_3D_tensor_argument(idx, _input, slice); |
| 223 | add_3D_tensor_argument(idx, _output, slice); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 224 | if (_4d_enabled) |
Giorgio Arena | 5c4a8e9 | 2019-08-28 17:55:07 +0100 | [diff] [blame] | 225 | { |
| 226 | add_argument<unsigned int>(idx, batch++); |
| 227 | } |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 228 | |
Giorgio Arena | 9324022 | 2020-12-23 11:55:29 +0000 | [diff] [blame] | 229 | enqueue(queue, *this, slice, lws_hint()); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 230 | } while (window.slide_window_slice_3D(slice)); |
Giorgio Arena | 205eed8 | 2019-08-14 10:13:50 +0100 | [diff] [blame] | 231 | } |
| 232 | } // namespace arm_compute |