From 932402276935a5fdf3a15b0c10e5310c7a0ae393 Mon Sep 17 00:00:00 2001 From: Giorgio Arena Date: Wed, 23 Dec 2020 11:55:29 +0000 Subject: Remove OpenCL padding: CLPadLayerKernel Resolves: COMPMID-3912 Change-Id: I1f8bd3bfec263ebfd70bc96f9183ccdc3089db13 Signed-off-by: Giorgio Arena Reviewed-on: https://review.mlplatform.org/c/ml/ComputeLibrary/+/4754 Tested-by: Arm Jenkins Comments-Addressed: Arm Jenkins Reviewed-by: Manuel Bottini Reviewed-by: Georgios Pinitas Reviewed-by: Gian Marco Iodice --- src/core/CL/kernels/CLPadLayerKernel.cpp | 114 ++++++++++++------------------- 1 file changed, 42 insertions(+), 72 deletions(-) (limited to 'src/core/CL/kernels/CLPadLayerKernel.cpp') diff --git a/src/core/CL/kernels/CLPadLayerKernel.cpp b/src/core/CL/kernels/CLPadLayerKernel.cpp index 485676667c..2f54b390d5 100644 --- a/src/core/CL/kernels/CLPadLayerKernel.cpp +++ b/src/core/CL/kernels/CLPadLayerKernel.cpp @@ -39,7 +39,7 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, c ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(input, output); ARM_COMPUTE_UNUSED(constant_value); ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); - ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > input->num_dimensions()); + ARM_COMPUTE_RETURN_ERROR_ON((padding.size() < 1) || (padding.size() > input->num_dimensions())); if(mode == PaddingMode::REFLECT || mode == PaddingMode::SYMMETRIC) { ARM_COMPUTE_RETURN_ERROR_ON(padding.size() > 3); @@ -62,40 +62,10 @@ Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, c return Status{}; } - -std::pair validate_and_configure_window(ITensorInfo *input, ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode, - unsigned int &num_elems_processed_per_iteration) -{ - ARM_COMPUTE_UNUSED(constant_value, mode); - - const TensorShape padded_shape = misc::shape_calculator::compute_padded_shape(input->tensor_shape(), padding); - auto_init_if_empty(*output, input->clone()->set_tensor_shape(padded_shape)); - - num_elems_processed_per_iteration = std::min(16U, 32U / static_cast(element_size_from_data_type(input->data_type()))); - if(input->dimension(0) < num_elems_processed_per_iteration) - { - num_elems_processed_per_iteration = 1 << static_cast(std::log2(input->dimension(0))); - } - - // Configure kernel window - Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); - - const int input_start_x = mode == PaddingMode::CONSTANT ? -(padding.at(0).first % num_elems_processed_per_iteration) : 0; - const int input_start_y = (mode == PaddingMode::CONSTANT && padding.size() > 1) ? -padding.at(1).first : 0; - - AccessWindowRectangle input_access(input, input_start_x, input_start_y, num_elems_processed_per_iteration, 1); - AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); - - const bool window_changed = update_window_and_padding(win, input_access, output_access); - output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape())); - - Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; - return std::make_pair(err, win); -} } // namespace CLPadLayerKernel::CLPadLayerKernel() - : _input(nullptr), _output(nullptr), _input_start_x(0), _input_start_y(0), _4d_enabled(false) + : _input(nullptr), _output(nullptr), _4d_enabled(false) { } @@ -106,40 +76,36 @@ void CLPadLayerKernel::configure(const ICLTensor *input, ICLTensor *output, cons void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const ICLTensor *input, ICLTensor *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) { - // Perform validation step ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); + auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(misc::shape_calculator::compute_padded_shape(input->info()->tensor_shape(), padding))); ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), padding, constant_value, mode)); + auto padding_info = get_padding_info({ input, output }); + _input = input; _output = output; _4d_enabled = (mode == PaddingMode::CONSTANT) && (padding.size() > 3); - // Configure window - unsigned int vec_size; - auto win_config = validate_and_configure_window(input->info(), output->info(), padding, constant_value, mode, vec_size); - ARM_COMPUTE_ERROR_THROW_ON(win_config.first); - ICLKernel::configure_internal(win_config.second); - // Set build options - std::string kernel_name = "pad_layer_"; - - const DataType &data_type = input->info()->data_type(); - const unsigned int input_width = input->info()->dimension(0); - const unsigned int input_height = input->info()->dimension(1); - const unsigned int input_depth = input->info()->dimension(2); - const unsigned int pad_x_before = padding.at(0).first; - const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0; - const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0; - const unsigned int pad_right_start = input_width + pad_x_before; - - _input_start_x = mode == PaddingMode::CONSTANT ? -(pad_x_before % vec_size) : 0; - _input_start_y = (mode == PaddingMode::CONSTANT && padding.size() > 1) ? -padding.at(1).first : 0; + const DataType &data_type = input->info()->data_type(); + const unsigned int input_width = input->info()->dimension(0); + const unsigned int input_height = input->info()->dimension(1); + const unsigned int input_depth = input->info()->dimension(2); + const unsigned int pad_x_before = padding.at(0).first; + const unsigned int pad_y_before = padding.size() > 1 ? padding.at(1).first : 0; + const unsigned int pad_z_before = padding.size() > 2 ? padding.at(2).first : 0; + const unsigned int vec_size = adjust_vec_size(std::min(16U, 32U / static_cast(element_size_from_data_type(input->info()->data_type()))), input_width); + const unsigned int pad_right_start = input_width + pad_x_before; + const unsigned int pad_x_before_remainder = pad_x_before % vec_size; + const unsigned int vec_size_leftover_write = vec_size - (ceil_to_multiple(output->info()->dimension(0), vec_size) - output->info()->dimension(0)); CLBuildOptions build_opts; build_opts.add_option("-DDATA_TYPE=" + get_cl_type_from_data_type(data_type)); build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size)); build_opts.add_option("-DPAD_X_BEFORE=" + support::cpp11::to_string(pad_x_before)); build_opts.add_option("-DSRC_WIDTH=" + support::cpp11::to_string(input_width)); + build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder)); + build_opts.add_option("-DVEC_SIZE_LEFTOVER_WRITE=" + support::cpp11::to_string(vec_size_leftover_write)); if(padding.size() > 1) { build_opts.add_option("-DPAD_Y_BEFORE=" + support::cpp11::to_string(pad_y_before)); @@ -152,15 +118,23 @@ void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const } } + std::string kernel_name = "pad_layer_"; switch(mode) { case PaddingMode::CONSTANT: { kernel_name += "constant"; + const unsigned int vec_size_leftover_read = vec_size - (ceil_to_multiple(pad_right_start, vec_size) - pad_right_start); + build_opts.add_option("-DCONST_VAL=" + string_from_pixel_value(constant_value, data_type)); - build_opts.add_option_if(pad_x_before >= vec_size, "-DNUM_THREADS_TO_SKIP_X=" + support::cpp11::to_string(pad_x_before / vec_size)); + build_opts.add_option("-DVEC_SIZE_LEFTOVER_READ=" + support::cpp11::to_string(vec_size_leftover_read)); + if(pad_x_before >= vec_size) + { + build_opts.add_option("-DTHREADS_TO_SKIP_BEFORE=" + support::cpp11::to_string(pad_x_before / vec_size)); + build_opts.add_option("-DTHREADS_TO_SKIP_AFTER=" + support::cpp11::to_string(pad_right_start / vec_size)); + } if(_4d_enabled) { build_opts.add_option("-DPAD_W_BEFORE=" + support::cpp11::to_string(padding.at(3).first)); @@ -176,13 +150,11 @@ void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const const auto is_reflect = static_cast(mode == PaddingMode::REFLECT); - const unsigned int pad_x_before_remainder = pad_x_before % vec_size; - const unsigned int pad_x_after_remainder = pad_right_start % vec_size; - const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect; - const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size); + const unsigned int pad_x_after_remainder = pad_right_start % vec_size; + const unsigned int after_pad_fact_x = (2 * input_width + pad_x_before) - is_reflect; + const unsigned int output_last_x = ceil_to_multiple(pad_right_start + padding.at(0).second, vec_size); build_opts.add_option("-DIS_REFLECT=" + support::cpp11::to_string(is_reflect)); - build_opts.add_option("-DPAD_X_BEFORE_REMAINDER=" + support::cpp11::to_string(pad_x_before_remainder)); build_opts.add_option("-DPAD_X_AFTER_REMAINDER=" + support::cpp11::to_string(pad_x_after_remainder)); build_opts.add_option("-DPAD_X_BEFORE_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_before_remainder + is_reflect) % vec_size)); build_opts.add_option("-DPAD_X_AFTER_REMAINDER_REFL=" + support::cpp11::to_string((pad_x_after_remainder - is_reflect) % vec_size)); @@ -197,14 +169,17 @@ void CLPadLayerKernel::configure(const CLCompileContext &compile_context, const // Create kernel _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); + + // Configure window + Window win = calculate_max_window(*output->info(), Steps(vec_size)); + ICLKernel::configure_internal(win); + + ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); } Status CLPadLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PaddingList &padding, PixelValue constant_value, PaddingMode mode) { - unsigned int vec_size; ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, padding, constant_value, mode)); - ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get(), padding, constant_value, mode, vec_size).first); - return Status{}; } @@ -213,25 +188,20 @@ void CLPadLayerKernel::run(const Window &window, cl::CommandQueue &queue) ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); - Window win_in = window; - win_in.adjust(Window::DimX, _input_start_x, true); - win_in.adjust(Window::DimY, _input_start_y, true); - - Window slice_out = window.first_slice_window_3D(); - Window slice_in = win_in.first_slice_window_3D(); - unsigned int batch = 0; + Window slice = window.first_slice_window_3D(); + unsigned int batch = 0; do { unsigned int idx = 0; - add_3D_tensor_argument(idx, _input, slice_in); - add_3D_tensor_argument(idx, _output, slice_out); + add_3D_tensor_argument(idx, _input, slice); + add_3D_tensor_argument(idx, _output, slice); if(_4d_enabled) { add_argument(idx, batch++); } - enqueue(queue, *this, slice_out, lws_hint()); + enqueue(queue, *this, slice, lws_hint()); } - while(window.slide_window_slice_3D(slice_out) && win_in.slide_window_slice_3D(slice_in)); + while(window.slide_window_slice_3D(slice)); } } // namespace arm_compute -- cgit v1.2.1