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