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