Compute Library
 21.02
CpuPermuteKernel.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/Types.h"
35 
36 namespace
37 {
38 #include "src/core/NEON/kernels/convolution/common/shims.hpp"
39 } // namespace
40 
41 namespace arm_compute
42 {
43 namespace cpu
44 {
45 namespace kernels
46 {
47 namespace
48 {
49 inline bool is_permutation_supported(const PermutationVector &v)
50 {
51  static const std::array<PermutationVector, 2> permutations2 =
52  {
53  {
54  PermutationVector(0U, 1U),
55  PermutationVector(1U, 0U),
56  }
57  };
58  static const std::array<PermutationVector, 6> permutations3 =
59  {
60  {
61  PermutationVector(2U, 0U, 1U),
62  PermutationVector(1U, 2U, 0U),
63  PermutationVector(0U, 1U, 2U),
64  PermutationVector(0U, 2U, 1U),
65  PermutationVector(1U, 0U, 2U),
66  PermutationVector(2U, 1U, 0U),
67  }
68  };
69  static const std::array<PermutationVector, 24> permutations4 =
70  {
71  {
72  PermutationVector(0U, 1U, 2U, 3U),
73  PermutationVector(1U, 0U, 2U, 3U),
74  PermutationVector(2U, 0U, 1U, 3U),
75  PermutationVector(0U, 2U, 1U, 3U),
76  PermutationVector(1U, 2U, 0U, 3U),
77  PermutationVector(2U, 1U, 0U, 3U),
78  PermutationVector(2U, 1U, 3U, 0U),
79  PermutationVector(1U, 2U, 3U, 0U),
80  PermutationVector(3U, 2U, 1U, 0U),
81  PermutationVector(2U, 3U, 1U, 0U),
82  PermutationVector(1U, 3U, 2U, 0U),
83  PermutationVector(3U, 1U, 2U, 0U),
84  PermutationVector(3U, 0U, 2U, 1U),
85  PermutationVector(0U, 3U, 2U, 1U),
86  PermutationVector(2U, 3U, 0U, 1U),
87  PermutationVector(3U, 2U, 0U, 1U),
88  PermutationVector(0U, 2U, 3U, 1U),
89  PermutationVector(2U, 0U, 3U, 1U),
90  PermutationVector(1U, 0U, 3U, 2U),
91  PermutationVector(0U, 1U, 3U, 2U),
92  PermutationVector(3U, 1U, 0U, 2U),
93  PermutationVector(1U, 3U, 0U, 2U),
94  PermutationVector(0U, 3U, 1U, 2U),
95  PermutationVector(3U, 0U, 1U, 2U)
96  }
97  };
98 
99  return (permutations2.end() != std::find(permutations2.begin(), permutations2.end(), v)) || (permutations3.end() != std::find(permutations3.begin(), permutations3.end(), v))
100  || (permutations4.end() != std::find(permutations4.begin(), permutations4.end(), v));
101 }
102 
103 Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
104 {
105  ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
106  ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_permutation_supported(perm), "PermutationVector not supported.");
107 
109 
110  // Validate configured destination
111  if(dst->total_size() != 0)
112  {
116  }
117 
118  return Status{};
119 }
120 
121 template <typename T>
122 void run_permute(const Window &window, const ITensor *src, const ITensor *dst, const PermutationVector &perm)
123 {
124  const DataLayout src_layout = src->info()->data_layout();
125 
126  // Source window
127  Window window_src = window;
128 
129  // we only support these two configs in src/core/NEON/kernels/convolution/common/shims.hpp, for all others
130  // we have to fall back to C++
131  if((src_layout == DataLayout::NCHW && perm == PermutationVector{ 2U, 0U, 1U }) || (src_layout == DataLayout::NHWC && perm == PermutationVector{ 1U, 2U, 0U }))
132  {
133  window_src.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().end() - window.x().start()));
134  window_src.set(Window::DimY, Window::Dimension(window.y().start(), window.y().end(), window.y().end() - window.y().start()));
135  window_src.set(Window::DimZ, Window::Dimension(window.z().start(), window.z().end(), window.z().end() - window.z().start()));
136  window_src.set(3, Window::Dimension(window[3].start(), window[3].end(), window[3].end() - window[3].start()));
137  }
138 
139  // Destination window
140  Window window_dst(window);
141  const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
142  for(size_t d = 0; d <= dst->info()->num_dimensions(); ++d)
143  {
144  window_dst.set(d, zero_window);
145  }
146 
147  // Create iterators
148  Iterator src_it(src, window_src);
149  Iterator dst_it(dst, window_dst);
150 
151  int in_row_stride = 0;
152  int in_col_stride = 0;
153  int in_channel_stride = 0;
154  int in_batch_stride = 0;
155  int n_cols = 0;
156  int n_rows = 0;
157  int n_channels = 0;
158  int n_batches = 0;
159 
160  switch(src_layout)
161  {
162  case DataLayout::NCHW:
163  {
164  in_row_stride = src->info()->strides_in_bytes().y() / sizeof(T);
165  in_channel_stride = src->info()->strides_in_bytes().z() / sizeof(T);
166  in_batch_stride = src->info()->strides_in_bytes()[3] / sizeof(T);
167  n_cols = src->info()->tensor_shape().x();
168  n_rows = window_src.y().step();
169  n_channels = src->info()->tensor_shape().z();
170  n_batches = src->info()->tensor_shape()[3];
171  break;
172  }
173  case DataLayout::NHWC:
174  {
175  in_col_stride = src->info()->strides_in_bytes().y() / sizeof(T);
176  in_row_stride = src->info()->strides_in_bytes().z() / sizeof(T);
177  in_batch_stride = src->info()->strides_in_bytes()[3] / sizeof(T);
178  n_channels = src->info()->tensor_shape().x();
179  n_cols = window_src.y().step();
180  n_rows = src->info()->tensor_shape().z();
181  n_batches = src->info()->tensor_shape()[3];
182  break;
183  }
184  default:
185  {
186  ARM_COMPUTE_ERROR("Invalid source data layout.");
187  break;
188  }
189  }
190 
191  // CHW -> HWC
192  if(src_layout == DataLayout::NCHW && perm == PermutationVector{ 2U, 0U, 1U })
193  {
194  const int out_channel_stride = dst->info()->strides_in_bytes().x() / sizeof(T);
195  const int out_col_stride = dst->info()->strides_in_bytes().y() / sizeof(T);
196  const int out_row_stride = dst->info()->strides_in_bytes().z() / sizeof(T);
197  const int out_batch_stride = dst->info()->strides_in_bytes()[3] / sizeof(T);
198  execute_window_loop(window_src, [&](const Coordinates & id)
199  {
200  const int idx = id[0] * out_col_stride + id[1] * out_row_stride + id[2] * out_channel_stride;
201  reorder::nchw_to_nhwc(reinterpret_cast<const T *>(src_it.ptr()), reinterpret_cast<T *>(dst_it.ptr()) + idx,
202  n_batches, n_channels, n_rows, n_cols,
203  in_batch_stride, in_channel_stride, in_row_stride,
204  out_batch_stride, out_row_stride, out_col_stride);
205  },
206  src_it, dst_it);
207  }
208  // HWC -> CHW
209  else if(src_layout == DataLayout::NHWC && perm == PermutationVector{ 1U, 2U, 0U })
210  {
211  const int out_col_stride = dst->info()->strides_in_bytes().x() / sizeof(T);
212  const int out_row_stride = dst->info()->strides_in_bytes().y() / sizeof(T);
213  const int out_channel_stride = dst->info()->strides_in_bytes().z() / sizeof(T);
214  const int out_batch_stride = dst->info()->strides_in_bytes()[3] / sizeof(T);
215  execute_window_loop(window_src, [&](const Coordinates & id)
216  {
217  const int idx = id[0] * out_channel_stride + id[1] * out_col_stride + id[2] * out_row_stride;
218  reorder::nhwc_to_nchw(reinterpret_cast<const T *>(src_it.ptr()), reinterpret_cast<T *>(dst_it.ptr()) + idx,
219  n_batches, n_rows, n_cols, n_channels,
220  in_batch_stride, in_row_stride, in_col_stride,
221  out_batch_stride, out_channel_stride, out_row_stride);
222  },
223  src_it, dst_it);
224  }
225  else
226  {
227  // All other cases fall back to C++
228  // Permute strides
229  Strides strides = dst->info()->strides_in_bytes();
230  Strides perm_strides = strides;
231  permute_strides(perm_strides, perm);
232  const int perm_stride_3 = src->info()->num_dimensions() >= 4 ? perm_strides[3] : 0;
233  execute_window_loop(window, [&](const Coordinates & id)
234  {
235  const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_stride_3;
236  *(reinterpret_cast<T *>(dst_it.ptr() + idx)) = *(reinterpret_cast<const T *>(src_it.ptr()));
237  },
238  src_it, dst_it);
239  }
240 }
241 } // namespace
242 
244 {
247  // Destination auto inizialitation if not yet initialized
248  auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape));
249 
250  // Perform validation step
252 
253  _perm = perm;
254 
255  // Configure kernel window
256  Window win = calculate_max_window(*src, Steps());
257 
258  // The NEPermute doesn't need padding so update_window_and_padding() can be skipped
259  Coordinates coord;
260  coord.set_num_dimensions(dst->num_dimensions());
261  dst->set_valid_region(ValidRegion(coord, dst->tensor_shape()));
262 
263  ICpuKernel::configure(win);
264 }
265 
267 {
269  return Status{};
270 }
271 
273 {
274  ARM_COMPUTE_UNUSED(info);
277 
278  const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
279  auto dst = tensors.get_tensor(TensorType::ACL_DST);
280 
281  switch(src->info()->element_size())
282  {
283  case 1:
284  run_permute<uint8_t>(window, src, dst, _perm);
285  break;
286  case 2:
287  run_permute<uint16_t>(window, src, dst, _perm);
288  break;
289  case 4:
290  run_permute<uint32_t>(window, src, dst, _perm);
291  break;
292  default:
293  ARM_COMPUTE_ERROR("Element size not supported");
294  break;
295  }
296 }
297 
298 const char *CpuPermuteKernel::name() const
299 {
300  return "CpuPermuteKernel";
301 }
302 } // namespace kernels
303 } // namespace cpu
304 } // 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
Shape of a tensor.
Definition: TensorShape.h:39
TensorShape compute_permutation_output_shape(const ITensorInfo &input, const PermutationVector &perm)
Calculate the permuted shape of an input given a permutation vector.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(...)
Definition: Validate.h:610
void permute_strides(Dimensions< T > &dimensions, const PermutationVector &perm)
Permutes the given dimensions according the permutation vector.
Definition: Utils.h:915
#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
Strides PermutationVector
Permutation vector.
Definition: Types.h:49
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
void configure(const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm)
Configure kernel for a given list of arguments.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(...)
Definition: Validate.h:288
SimpleTensor< float > src
Definition: DFT.cpp:155
Copyright (c) 2017-2021 Arm Limited.
virtual void set_valid_region(const ValidRegion &valid_region)=0
Set the valid region of the tensor.
const char * name() const override
Name of the kernel.
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:40
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
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.
void end(TokenStream &in, bool &valid)
Definition: MLGOParser.cpp:290
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:941
Num samples, channels, height, width.
Strides of an item in bytes.
Definition: Strides.h:37
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)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:50
Information about executing thread and CPU.
Definition: CPPTypes.h:235
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
Tensor packing service.
Definition: ITensorPack.h:37
#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
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
void set_num_dimensions(size_t num_dimensions)
Set number of dimensions.
Definition: Dimensions.h:149
static Status validate(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
Static function to check if given info will lead to a valid configuration of CpuPermuteKernel.
Container for valid region of a window.
Definition: Types.h:188
def find(path, pattern)
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