Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | e82cb04 | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2019 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 | */ |
| 24 | #include "arm_compute/core/CL/kernels/CLChannelShuffleLayerKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/CL/CLHelpers.h" |
| 27 | #include "arm_compute/core/CL/CLKernelLibrary.h" |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/CL/CLValidate.h" |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 29 | #include "arm_compute/core/CL/ICLTensor.h" |
| 30 | #include "arm_compute/core/Error.h" |
| 31 | #include "arm_compute/core/Helpers.h" |
| 32 | #include "arm_compute/core/TensorInfo.h" |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Window.h" |
| 34 | |
| 35 | namespace arm_compute |
| 36 | { |
| 37 | namespace |
| 38 | { |
| 39 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) |
| 40 | { |
Vidhya Sudhan Loganathan | f1f4906 | 2018-05-25 13:21:26 +0100 | [diff] [blame] | 41 | ARM_COMPUTE_RETURN_ERROR_ON_F16_UNSUPPORTED(input); |
Vidhya Sudhan Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 42 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, |
| 43 | DataType::U16, DataType::S16, |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 44 | DataType::U32, DataType::S32, |
| 45 | DataType::F16, DataType::F32); |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient"); |
| 47 | |
| 48 | const unsigned int channels = input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)); |
| 49 | |
| 50 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups == channels, "Channel shuffling with same number of groups as number of channels would be inefficient"); |
| 51 | // There cannot be more groups than channels |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels); |
| 53 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, "The number of channels must be a multiple of the number of groups"); |
| 54 | |
| 55 | // Checks performed when output is configured |
| 56 | if(output->total_size() != 0) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
Isabella Gottardi | e82cb04 | 2019-02-14 18:07:36 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 60 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 61 | } |
| 62 | |
| 63 | return Status{}; |
| 64 | } |
| 65 | |
| 66 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
| 67 | { |
| 68 | // Output tensor auto initialization if not yet initialized |
| 69 | auto_init_if_empty(*output, *input->clone()); |
| 70 | |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 71 | const bool is_nhwc = input->data_layout() == DataLayout::NHWC; |
| 72 | const unsigned int num_elems_processed_per_iteration_x = is_nhwc ? 4 : max_cl_vector_width / input->element_size(); |
| 73 | constexpr unsigned int num_elems_processed_per_iteration_y = 2; |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 74 | |
| 75 | // Configure kernel window |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 76 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
| 77 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 78 | AccessWindowRectangle output_access(output, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 79 | |
| 80 | const bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 81 | output_access.set_valid_region(win, input->valid_region()); |
| 82 | |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 83 | Window win_collapsed = win.collapse(win, Window::DimZ); |
| 84 | |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 85 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 86 | return std::make_pair(err, win_collapsed); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 87 | } |
| 88 | } // namespace |
| 89 | |
| 90 | CLChannelShuffleLayerKernel::CLChannelShuffleLayerKernel() |
| 91 | : _input(nullptr), _output(nullptr) |
| 92 | { |
| 93 | } |
| 94 | |
| 95 | void CLChannelShuffleLayerKernel::configure(const ICLTensor *input, ICLTensor *output, unsigned int num_groups) |
| 96 | { |
| 97 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 98 | |
| 99 | _input = input; |
| 100 | _output = output; |
| 101 | |
| 102 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), num_groups)); |
| 103 | |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 104 | const DataLayout data_layout = input->info()->data_layout(); |
| 105 | const bool is_nhwc = data_layout == DataLayout::NHWC; |
| 106 | const unsigned int channels = input->info()->dimension(get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL)); |
| 107 | const unsigned int vec_size = is_nhwc ? 4 : max_cl_vector_width / input->info()->element_size(); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 108 | |
| 109 | // Set kernel build options |
| 110 | CLBuildOptions build_opts; |
| 111 | build_opts.add_option("-DNUM_GROUPS=" + support::cpp11::to_string(num_groups)); |
| 112 | build_opts.add_option("-DK=" + support::cpp11::to_string(channels / num_groups)); |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 113 | build_opts.add_option("-DVEC_SIZE=" + support::cpp11::to_string(vec_size)); |
| 114 | build_opts.add_option("-DSRC_DIM_Z=" + support::cpp11::to_string(input->info()->dimension(2))); |
| 115 | build_opts.add_option("-DLAST_ACCESSED=" + support::cpp11::to_string(std::max(static_cast<int>(channels - vec_size), 0))); |
| 116 | |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 117 | switch(input->info()->element_size()) |
| 118 | { |
| 119 | case 1: |
| 120 | build_opts.add_option("-DDATA_TYPE=uchar"); |
| 121 | break; |
| 122 | case 2: |
| 123 | build_opts.add_option("-DDATA_TYPE=ushort"); |
| 124 | break; |
| 125 | case 4: |
| 126 | build_opts.add_option("-DDATA_TYPE=uint"); |
| 127 | break; |
| 128 | default: |
| 129 | ARM_COMPUTE_ERROR("Data type not supported"); |
| 130 | } |
| 131 | |
| 132 | // Create kernel |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 133 | std::string kernel_name = "channel_shuffle_" + lower_string(string_from_data_layout(data_layout)); |
| 134 | ; |
| 135 | _kernel = static_cast<cl::Kernel>(CLKernelLibrary::get().create_kernel(kernel_name, build_opts.options())); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 136 | |
| 137 | // Configure kernel window |
| 138 | auto win_config = validate_and_configure_window(input->info(), output->info()); |
| 139 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
Anthony Barbier | b6eb353 | 2018-08-08 13:20:04 +0100 | [diff] [blame] | 140 | ICLKernel::configure_internal(win_config.second); |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 141 | |
| 142 | // Set config_id for enabling LWS tuning |
| 143 | _config_id = kernel_name; |
| 144 | _config_id += "_"; |
| 145 | _config_id += lower_string(string_from_data_type(input->info()->data_type())); |
| 146 | _config_id += "_"; |
| 147 | _config_id += support::cpp11::to_string(num_groups); |
| 148 | _config_id += "_"; |
| 149 | _config_id += support::cpp11::to_string(input->info()->dimension(0)); |
| 150 | _config_id += "_"; |
| 151 | _config_id += support::cpp11::to_string(input->info()->dimension(1)); |
| 152 | _config_id += "_"; |
| 153 | _config_id += support::cpp11::to_string(input->info()->dimension(2)); |
| 154 | _config_id += "_"; |
| 155 | _config_id += support::cpp11::to_string(output->info()->dimension(0)); |
| 156 | _config_id += "_"; |
| 157 | _config_id += support::cpp11::to_string(output->info()->dimension(1)); |
| 158 | _config_id += "_"; |
| 159 | _config_id += support::cpp11::to_string(output->info()->dimension(2)); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | Status CLChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) |
| 163 | { |
| 164 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups)); |
| 165 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first); |
| 166 | |
| 167 | return Status{}; |
| 168 | } |
| 169 | |
| 170 | void CLChannelShuffleLayerKernel::run(const Window &window, cl::CommandQueue &queue) |
| 171 | { |
| 172 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 173 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICLKernel::window(), window); |
| 174 | |
Gian Marco Iodice | 8bab0ee | 2018-09-13 11:51:56 +0100 | [diff] [blame] | 175 | unsigned int idx = 0; |
| 176 | add_4D_tensor_argument(idx, _input, window); |
| 177 | add_4D_tensor_argument(idx, _output, window); |
| 178 | enqueue(queue, *this, window, lws_hint()); |
Michele Di Giorgio | 7217563 | 2018-05-01 16:52:00 +0100 | [diff] [blame] | 179 | } |
| 180 | } // namespace arm_compute |