Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 1 | /* |
Sheri Zhang | ac6499a | 2021-02-10 15:32:38 +0000 | [diff] [blame] | 2 | * Copyright (c) 2018-2021 Arm Limited. |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEChannelShuffleLayerKernel.h" |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 25 | |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Utils.h" |
| 31 | #include "arm_compute/core/Validate.h" |
| 32 | #include "arm_compute/core/Window.h" |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 33 | |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/CPP/Validate.h" |
| 35 | #include "src/core/helpers/AutoConfiguration.h" |
| 36 | #include "src/core/helpers/WindowHelpers.h" |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace |
| 41 | { |
| 42 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) |
| 43 | { |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 44 | // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use CPU FP16 instructions. |
Georgios Pinitas | 3384356 | 2019-12-10 13:33:18 +0000 | [diff] [blame] | 45 | ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(input, DataLayout::NCHW, DataLayout::NHWC); |
| 47 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 48 | const unsigned int channels = |
| 49 | input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL)); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 50 | |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient"); |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 52 | ARM_COMPUTE_RETURN_ERROR_ON_MSG( |
| 53 | num_groups == channels, |
| 54 | "Channel shuffling with same number of groups as number of channels would be inefficient"); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 55 | ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels); // There cannot be more groups than channels |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 56 | ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, |
| 57 | "The number of channels must be a multiple of the number of groups"); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 58 | |
| 59 | // Checks performed when output is configured |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 60 | if (output->total_size() != 0) |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 61 | { |
| 62 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(input, output); |
| 63 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output); |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(input, output); |
| 65 | } |
| 66 | |
| 67 | return Status{}; |
| 68 | } |
| 69 | void channel_shuffle_nhwc(const ITensor *input, ITensor *output, unsigned int num_groups, const Window &window) |
| 70 | { |
| 71 | const DataLayout data_layout = input->info()->data_layout(); |
| 72 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 73 | |
| 74 | const size_t element_size = input->info()->element_size(); |
| 75 | const unsigned int K = input->info()->dimension(channel_idx) / num_groups; |
Pablo Marquez Tello | 3611852 | 2021-10-18 10:50:31 +0100 | [diff] [blame] | 76 | const double rK = 1.0 / K; |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 77 | |
| 78 | Iterator in(input, window); |
| 79 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 80 | execute_window_loop( |
| 81 | window, |
| 82 | [&](const Coordinates &id) |
| 83 | { |
| 84 | // Shuffle channel |
| 85 | const unsigned int curr_channel = id.x(); |
| 86 | const unsigned int group_id = curr_channel * rK; |
| 87 | const unsigned int r = group_id * K; |
| 88 | const unsigned int channel_id = curr_channel - r; |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 89 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 90 | // Calculate output coordinates |
| 91 | Coordinates out_coords = id; |
| 92 | out_coords.set(Window::DimX, channel_id * num_groups + group_id); |
| 93 | std::copy_n(in.ptr(), element_size, output->ptr_to_element(out_coords)); |
| 94 | }, |
| 95 | in); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 96 | } |
| 97 | void channel_shuffle_nchw(const ITensor *input, ITensor *output, unsigned int num_groups, const Window &window) |
| 98 | { |
| 99 | Window win = window; |
| 100 | win.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 101 | win.set(Window::DimY, Window::Dimension(0, 1, 1)); |
| 102 | |
| 103 | const DataLayout data_layout = input->info()->data_layout(); |
| 104 | const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 105 | const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL); |
| 106 | |
| 107 | const unsigned int height = input->info()->tensor_shape().y(); |
| 108 | const size_t input_stride_y = input->info()->strides_in_bytes().y(); |
| 109 | const size_t output_stride_y = output->info()->strides_in_bytes().y(); |
| 110 | const size_t row_size = input->info()->dimension(width_idx) * input->info()->element_size(); |
| 111 | |
| 112 | const unsigned int K = input->info()->dimension(channel_idx) / num_groups; |
Pablo Tello | 0d11b70 | 2021-10-08 11:22:24 +0100 | [diff] [blame] | 113 | const double rK = 1.0 / K; |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 114 | |
| 115 | Iterator in(input, win); |
| 116 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 117 | execute_window_loop( |
| 118 | win, |
| 119 | [&](const Coordinates &id) |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 120 | { |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 121 | // Shuffle channel |
| 122 | const unsigned int curr_channel = id.z(); |
| 123 | const unsigned int group_id = curr_channel * rK; |
| 124 | const unsigned int r = group_id * K; |
| 125 | const unsigned int channel_id = curr_channel - r; |
| 126 | |
| 127 | // Calculate output coordinates |
| 128 | Coordinates out_coords = id; |
| 129 | out_coords.set(Window::DimZ, channel_id * num_groups + group_id); |
| 130 | const uint8_t *input_ptr = in.ptr(); |
| 131 | uint8_t *output_ptr = output->ptr_to_element(out_coords); |
| 132 | |
| 133 | // Copy plane |
| 134 | for (unsigned int y = 0; y < height; ++y) |
| 135 | { |
| 136 | std::copy_n(input_ptr, row_size, output_ptr); |
| 137 | input_ptr += input_stride_y; |
| 138 | output_ptr += output_stride_y; |
| 139 | } |
| 140 | }, |
| 141 | in); |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 142 | } |
| 143 | } // namespace |
| 144 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 145 | NEChannelShuffleLayerKernel::NEChannelShuffleLayerKernel() : _input(nullptr), _output(nullptr), _num_groups() |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 146 | { |
| 147 | } |
| 148 | |
| 149 | void NEChannelShuffleLayerKernel::configure(const ITensor *input, ITensor *output, unsigned int num_groups) |
| 150 | { |
| 151 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
| 152 | |
| 153 | // Output tensor auto initialization if not yet initialized |
| 154 | auto_init_if_empty(*output->info(), *input->info()->clone()); |
| 155 | |
| 156 | _input = input; |
| 157 | _output = output; |
| 158 | _num_groups = num_groups; |
| 159 | |
| 160 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), num_groups)); |
| 161 | |
| 162 | // Configure kernel window |
| 163 | Window win = calculate_max_window(*input->info(), Steps()); |
| 164 | |
| 165 | // The NEChannelShuffleLayerKernel doesn't need padding so update_window_and_padding() can be skipped |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 166 | INEKernel::configure(win); |
| 167 | } |
| 168 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 169 | Status |
| 170 | NEChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups) |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 171 | { |
| 172 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups)); |
| 173 | return Status{}; |
| 174 | } |
| 175 | |
| 176 | void NEChannelShuffleLayerKernel::run(const Window &window, const ThreadInfo &info) |
| 177 | { |
| 178 | ARM_COMPUTE_UNUSED(info); |
| 179 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 180 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(IKernel::window(), window); |
| 181 | |
Felix Thomasmathibalan | afd38f0 | 2023-09-27 17:46:17 +0100 | [diff] [blame] | 182 | switch (_input->info()->data_layout()) |
Georgios Pinitas | f1adf11 | 2018-11-02 12:54:18 +0000 | [diff] [blame] | 183 | { |
| 184 | case DataLayout::NHWC: |
| 185 | channel_shuffle_nhwc(_input, _output, _num_groups, window); |
| 186 | break; |
| 187 | case DataLayout::NCHW: |
| 188 | channel_shuffle_nchw(_input, _output, _num_groups, window); |
| 189 | break; |
| 190 | default: |
| 191 | ARM_COMPUTE_ERROR("Unsupported data layout!"); |
| 192 | break; |
| 193 | } |
| 194 | } |
| 195 | } // namespace arm_compute |