Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 1 | /* |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, 2023 Arm Limited. |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +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/CLChannelShuffleLayerKernel.h" |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
| 28 | #include "arm_compute/core/CL/ICLTensor.h" |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
Matthew Bentham | 314d3e2 | 2023-06-23 10:53:52 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/utils/helpers/AdjustVecSize.h" |
| 33 | #include "arm_compute/core/utils/StringUtils.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 34 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 35 | #include "src/core/CL/CLValidate.h" |
| 36 | #include "src/core/helpers/AutoConfiguration.h" |
| 37 | #include "src/core/helpers/WindowHelpers.h" |
Matthew Bentham | 758b5ba | 2020-03-05 23:37:48 +0000 | [diff] [blame] | 38 | #include "support/StringSupport.h" |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 39 | |
| 40 | namespace arm_compute |
| 41 | { |
| 42 | namespace |
| 43 | { |
| 44 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) |
| 45 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Manuel Bottini | 8481d83 | 2019-12-10 15:28:40 +0000 | [diff] [blame] | 47 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 48 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient"); |
| 49 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 50 | const unsigned int channels = |
| 51 | input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 52 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 54 | num_groups == channels, |
| 55 | "Channel shuffling with same number of groups as number of channels would be inefficient"); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 56 | // There cannot be more groups than channels |
| 57 | ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 58 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, |
| 59 | "The number of channels must be a multiple of the number of groups"); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 60 | |
| 61 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 62 | if (output->total_size() != 0) |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 63 | { |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Isabella Gottardi | 0a1090a | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 65 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 66 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 67 | } |
| 68 | |
| 69 | return Status{}; |
| 70 | } |
| 71 | |
| 72 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 73 | { |
| 74 | // Output tensor auto initialization if not yet initialized |
| 75 | auto_init_if_empty(*output, *input->clone()); |
| 76 | |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 77 | const bool is_nhwc = input->data_layout() == DataLayout::NHWC; |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 78 | if (is_nhwc) |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 79 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | unsigned int num_elems_processed_per_iteration_x = |
| 81 | adjust_vec_size(max_cl_vector_width / input->element_size(), input->dimension(0)); |
| 82 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x)); |
| 83 | Window win_collapsed = win.collapse(win, Window::DimZ); |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 84 | return std::make_pair(Status{}, win_collapsed); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | const unsigned int num_elems_processed_per_iteration_x = max_cl_vector_width / input->element_size(); |
| 89 | constexpr unsigned int num_elems_processed_per_iteration_y = 2; |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 90 | |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 91 | // Configure kernel window |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 92 | Window win = calculate_max_window( |
| 93 | *input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 94 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, |
| 95 | num_elems_processed_per_iteration_y); |
| 96 | AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, |
| 97 | num_elems_processed_per_iteration_y); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 98 | |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 99 | const bool window_changed = update_window_and_padding(win, input_access, output_access); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 100 | |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 101 | Window win_collapsed = win.collapse(win, Window::DimZ); |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 102 | |
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{}; |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 105 | return std::make_pair(err, win_collapsed); |
| 106 | } |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 107 | } |
| 108 | } // namespace |
| 109 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 110 | CLChannelShuffleLayerKernel::CLChannelShuffleLayerKernel() : _input(nullptr), _output(nullptr) |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 111 | { |
Giorgio Arena | 4a95bba | 2021-06-28 11:00:27 +0100 | [diff] [blame] | 112 | _type = CLKernelType::ELEMENTWISE; |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void CLChannelShuffleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int num_groups) |
| 116 | { |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 117 | configure(CLKernelLibrary::get().get_compile_context(), input, output, num_groups); |
| 118 | } |
| 119 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 120 | void CLChannelShuffleLayerKernel::configure(const CLCompileContext &compile_context, |
| 121 | const ICLTensor *input, |
| 122 | ICLTensor *output, |
| 123 | unsigned int num_groups) |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 124 | { |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 125 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 126 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), num_groups)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 127 | auto padding_info = get_padding_info({input, output}); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 128 | |
| 129 | _input = input; |
| 130 | _output = output; |
| 131 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 132 | const DataLayout data_layout = input->info()->data_layout(); |
| 133 | const bool is_nhwc = data_layout == DataLayout::NHWC; |
| 134 | const unsigned int channels = |
| 135 | input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)); |
| 136 | unsigned int vec_size_x = 0; |
| 137 | unsigned int vec_size_x_leftovers = 0; |
| 138 | if (is_nhwc) |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 139 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 140 | vec_size_x = adjust_vec_size(max_cl_vector_width / input->info()->element_size(), input->info()->dimension(0)); |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 141 | vec_size_x_leftovers = input->info()->dimension(0) % vec_size_x; |
| 142 | } |
| 143 | else |
| 144 | { |
| 145 | vec_size_x = max_cl_vector_width / input->info()->element_size(); |
| 146 | } |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 147 | |
| 148 | // Set kernel build options |
| 149 | CLBuildOptions build_opts; |
| 150 | build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups)); |
| 151 | build_opts.add_option("-DK=" + support::cpp11::to_string(channels / num_groups)); |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 152 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size_x)); |
| 153 | build_opts.add_option_if(is_nhwc, "-DVEC_SIZE_LEFTOVER=" + support::cpp11::to_string(vec_size_x_leftovers)); |
| 154 | build_opts.add_option_if(is_nhwc, "-DSRC_DIM_X=" + support::cpp11::to_string(input->info()->dimension(0))); |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 155 | build_opts.add_option("-DSRC_DIM_Z=" + support::cpp11::to_string(input->info()->dimension(2))); |
Michele Di Giorgio | df4cf57 | 2019-10-09 15:32:39 +0100 | [diff] [blame] | 156 | build_opts.add_option("-DDATA_TYPE=" + get_cl_unsigned_type_from_element_size(input->info()->element_size())); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 157 | |
| 158 | // Create kernel |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 159 | std::string kernel_name = "channel_shuffle_" + lower_string(string_from_data_layout(data_layout)); |
Giorgio Arena | 6e9d0e0 | 2020-01-03 15:02:04 +0000 | [diff] [blame] | 160 | |
Manuel Bottini | 4c6bd51 | 2020-04-08 10:15:51 +0100 | [diff] [blame] | 161 | _kernel = create_kernel(compile_context, kernel_name, build_opts.options()); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 162 | |
| 163 | // Configure kernel window |
| 164 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 165 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 166 | ICLKernel::configure_internal(win_config.second); |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 167 | |
| 168 | // Set config_id for enabling LWS tuning |
| 169 | _config_id = kernel_name; |
| 170 | _config_id += "_"; |
| 171 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 172 | _config_id += "_"; |
| 173 | _config_id += support::cpp11::to_string(num_groups); |
| 174 | _config_id += "_"; |
| 175 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 176 | _config_id += "_"; |
| 177 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 178 | _config_id += "_"; |
| 179 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 180 | _config_id += "_"; |
| 181 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 182 | _config_id += "_"; |
| 183 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 184 | _config_id += "_"; |
| 185 | _config_id += support::cpp11::to_string(output->info()->dimension(2)); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 186 | if (data_layout == DataLayout::NHWC) |
Manuel Bottini | e57e11d | 2021-05-11 17:36:14 +0100 | [diff] [blame] | 187 | { |
| 188 | ARM_COMPUTE_ERROR_ON(has_padding_changed(padding_info)); |
| 189 | } |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 190 | } |
| 191 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 192 | Status |
| 193 | CLChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 194 | { |
| 195 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups)); |
| 196 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
| 197 | |
| 198 | return Status{}; |
| 199 | } |
| 200 | |
| 201 | void CLChannelShuffleLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 202 | { |
| 203 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 204 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 205 | |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 206 | unsigned int idx = 0; |
| 207 | add_4D_tensor_argument(idx, _input, window); |
| 208 | add_4D_tensor_argument(idx, _output, window); |
| 209 | enqueue(queue, *this, window, lws_hint()); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 210 | } |
| 211 | } // namespace arm_compute |