Compute Library
 21.02
NEChannelShuffleLayerKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018-2021 Arm Limited.
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  */
25 
26 #include "arm_compute/core/Error.h"
30 #include "arm_compute/core/Utils.h"
33 #include "src/core/CPP/Validate.h"
36 
37 namespace arm_compute
38 {
39 namespace
40 {
41 Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
42 {
43  // Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use Neon FP16 instructions.
44  ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
46 
47  const unsigned int channels = input->dimension(get_data_layout_dimension_index(input->data_layout(), DataLayoutDimension::CHANNEL));
48 
49  ARM_COMPUTE_RETURN_ERROR_ON_MSG(num_groups < 2, "Channel shuffling with less than 2 groups would be inefficient");
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  ARM_COMPUTE_RETURN_ERROR_ON(num_groups > channels); // There cannot be more groups than channels
52  ARM_COMPUTE_RETURN_ERROR_ON_MSG((channels % num_groups) != 0, "The number of channels must be a multiple of the number of groups");
53 
54  // Checks performed when output is configured
55  if(output->total_size() != 0)
56  {
60  }
61 
62  return Status{};
63 }
64 void channel_shuffle_nhwc(const ITensor *input, ITensor *output, unsigned int num_groups, const Window &window)
65 {
66  const DataLayout data_layout = input->info()->data_layout();
67  const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
68 
69  const size_t element_size = input->info()->element_size();
70  const unsigned int K = input->info()->dimension(channel_idx) / num_groups;
71  const float rK = 1.f / K;
72 
73  Iterator in(input, window);
74 
75  execute_window_loop(window, [&](const Coordinates & id)
76  {
77  // Shuffle channel
78  const unsigned int curr_channel = id.x();
79  const unsigned int group_id = curr_channel * rK;
80  const unsigned int r = group_id * K;
81  const unsigned int channel_id = curr_channel - r;
82 
83  // Calculate output coordinates
84  Coordinates out_coords = id;
85  out_coords.set(Window::DimX, channel_id * num_groups + group_id);
86  std::copy_n(in.ptr(), element_size, output->ptr_to_element(out_coords));
87  },
88  in);
89 }
90 void channel_shuffle_nchw(const ITensor *input, ITensor *output, unsigned int num_groups, const Window &window)
91 {
92  Window win = window;
93  win.set(Window::DimX, Window::Dimension(0, 1, 1));
94  win.set(Window::DimY, Window::Dimension(0, 1, 1));
95 
96  const DataLayout data_layout = input->info()->data_layout();
97  const unsigned int width_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
98  const unsigned int channel_idx = get_data_layout_dimension_index(data_layout, DataLayoutDimension::CHANNEL);
99 
100  const unsigned int height = input->info()->tensor_shape().y();
101  const size_t input_stride_y = input->info()->strides_in_bytes().y();
102  const size_t output_stride_y = output->info()->strides_in_bytes().y();
103  const size_t row_size = input->info()->dimension(width_idx) * input->info()->element_size();
104 
105  const unsigned int K = input->info()->dimension(channel_idx) / num_groups;
106  const float rK = 1.f / K;
107 
108  Iterator in(input, win);
109 
110  execute_window_loop(win, [&](const Coordinates & id)
111  {
112  // Shuffle channel
113  const unsigned int curr_channel = id.z();
114  const unsigned int group_id = curr_channel * rK;
115  const unsigned int r = group_id * K;
116  const unsigned int channel_id = curr_channel - r;
117 
118  // Calculate output coordinates
119  Coordinates out_coords = id;
120  out_coords.set(Window::DimZ, channel_id * num_groups + group_id);
121  const uint8_t *input_ptr = in.ptr();
122  uint8_t *output_ptr = output->ptr_to_element(out_coords);
123 
124  // Copy plane
125  for(unsigned int y = 0; y < height; ++y)
126  {
127  std::copy_n(input_ptr, row_size, output_ptr);
128  input_ptr += input_stride_y;
129  output_ptr += output_stride_y;
130  }
131  },
132  in);
133 }
134 } // namespace
135 
137  : _input(nullptr), _output(nullptr), _num_groups()
138 {
139 }
140 
141 void NEChannelShuffleLayerKernel::configure(const ITensor *input, ITensor *output, unsigned int num_groups)
142 {
143  ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
144 
145  // Output tensor auto initialization if not yet initialized
146  auto_init_if_empty(*output->info(), *input->info()->clone());
147 
148  _input = input;
149  _output = output;
150  _num_groups = num_groups;
151 
153 
154  // Configure kernel window
155  Window win = calculate_max_window(*input->info(), Steps());
156 
157  // The NEChannelShuffleLayerKernel doesn't need padding so update_window_and_padding() can be skipped
158  Coordinates coord;
159  coord.set_num_dimensions(output->info()->num_dimensions());
160  output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
161 
162  INEKernel::configure(win);
163 }
164 
165 Status NEChannelShuffleLayerKernel::validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
166 {
167  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, num_groups));
168  return Status{};
169 }
170 
172 {
173  ARM_COMPUTE_UNUSED(info);
176 
177  switch(_input->info()->data_layout())
178  {
179  case DataLayout::NHWC:
180  channel_shuffle_nhwc(_input, _output, _num_groups, window);
181  break;
182  case DataLayout::NCHW:
183  channel_shuffle_nchw(_input, _output, _num_groups, window);
184  break;
185  default:
186  ARM_COMPUTE_ERROR("Unsupported data layout!");
187  break;
188  }
189 }
190 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
void configure(const ITensor *input, ITensor *output, unsigned int num_groups)
Configure function&#39;s inputs and outputs.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_LAYOUT_NOT_IN(t,...)
Definition: Validate.h:746
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_LAYOUT(...)
Definition: Validate.h:494
#define ARM_COMPUTE_ERROR(msg)
Print the given message then throw an std::runtime_error.
Definition: Error.h:352
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
const DataLayout data_layout
Definition: Im2Col.cpp:151
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
static Status validate(const ITensorInfo *input, const ITensorInfo *output, unsigned int num_groups)
Static function to check if given info will lead to a valid configuration of NEChannelShuffleLayerKer...
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Interface for Neon tensor.
Definition: ITensor.h:36
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
const unsigned int num_groups
Definition: Im2Col.cpp:153
Coordinates of an item.
Definition: Coordinates.h:37
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
Num samples, channels, height, width.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
Information about executing thread and CPU.
Definition: CPPTypes.h:235
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
Definition: Validate.h:443
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(...)
Definition: Validate.h:545
Num samples, height, width, channels.
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:161
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
unsigned int K
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
Container for valid region of a window.
Definition: Types.h:188
size_t get_data_layout_dimension_index(const DataLayout data_layout, const DataLayoutDimension data_layout_dimension)
Get the index of the given dimension.
Definition: Helpers.inl:193
DataLayout
[DataLayout enum definition]
Definition: Types.h:120
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:205
virtual DataLayout data_layout() const =0
Get the data layout of the tensor.