blob: b444a25ff737c948f9d595b09de820f59bb10d1c [file] [log] [blame]
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +00001/*
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 */
Georgios Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuPermuteKernel.h"
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000025
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/Types.h"
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000031#include "arm_compute/core/utils/misc/ShapeCalculator.h"
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010032#include "arm_compute/core/Validate.h"
33
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
36
37namespace
38{
39#include "src/core/NEON/kernels/convolution/common/shims.hpp"
40} // namespace
41
42namespace arm_compute
43{
44namespace cpu
45{
46namespace kernels
47{
48namespace
49{
50inline bool is_permutation_supported(const PermutationVector &v)
51{
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010052 static const std::array<PermutationVector, 2> permutations2 = {{
53 PermutationVector(0U, 1U),
54 PermutationVector(1U, 0U),
55 }};
56 static const std::array<PermutationVector, 6> permutations3 = {{
57 PermutationVector(2U, 0U, 1U),
58 PermutationVector(1U, 2U, 0U),
59 PermutationVector(0U, 1U, 2U),
60 PermutationVector(0U, 2U, 1U),
61 PermutationVector(1U, 0U, 2U),
62 PermutationVector(2U, 1U, 0U),
63 }};
64 static const std::array<PermutationVector, 24> permutations4 = {
65 {PermutationVector(0U, 1U, 2U, 3U), PermutationVector(1U, 0U, 2U, 3U), PermutationVector(2U, 0U, 1U, 3U),
66 PermutationVector(0U, 2U, 1U, 3U), PermutationVector(1U, 2U, 0U, 3U), PermutationVector(2U, 1U, 0U, 3U),
67 PermutationVector(2U, 1U, 3U, 0U), PermutationVector(1U, 2U, 3U, 0U), PermutationVector(3U, 2U, 1U, 0U),
68 PermutationVector(2U, 3U, 1U, 0U), PermutationVector(1U, 3U, 2U, 0U), PermutationVector(3U, 1U, 2U, 0U),
69 PermutationVector(3U, 0U, 2U, 1U), PermutationVector(0U, 3U, 2U, 1U), PermutationVector(2U, 3U, 0U, 1U),
70 PermutationVector(3U, 2U, 0U, 1U), PermutationVector(0U, 2U, 3U, 1U), PermutationVector(2U, 0U, 3U, 1U),
71 PermutationVector(1U, 0U, 3U, 2U), PermutationVector(0U, 1U, 3U, 2U), PermutationVector(3U, 1U, 0U, 2U),
72 PermutationVector(1U, 3U, 0U, 2U), PermutationVector(0U, 3U, 1U, 2U), PermutationVector(3U, 0U, 1U, 2U)}};
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000073
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010074 return (permutations2.end() != std::find(permutations2.begin(), permutations2.end(), v)) ||
75 (permutations3.end() != std::find(permutations3.begin(), permutations3.end(), v)) ||
76 (permutations4.end() != std::find(permutations4.begin(), permutations4.end(), v));
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000077}
78
79Status validate_arguments(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
80{
81 ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN);
82 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_permutation_supported(perm), "PermutationVector not supported.");
83
84 const TensorShape dst_shape = misc::shape_calculator::compute_permutation_output_shape(*src, perm);
85
86 // Validate configured destination
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +010087 if (dst->total_size() != 0)
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +000088 {
89 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), dst_shape);
90 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst);
91 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst);
92 }
93
94 return Status{};
95}
96
97template <typename T>
98void run_permute(const Window &window, const ITensor *src, const ITensor *dst, const PermutationVector &perm)
99{
100 const DataLayout src_layout = src->info()->data_layout();
101
102 // Source window
103 Window window_src = window;
104
105 // we only support these two configs in src/core/NEON/kernels/convolution/common/shims.hpp, for all others
106 // we have to fall back to C++
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100107 if ((src_layout == DataLayout::NCHW && perm == PermutationVector{2U, 0U, 1U}) ||
108 (src_layout == DataLayout::NHWC && perm == PermutationVector{1U, 2U, 0U}))
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000109 {
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100110 window_src.set(Window::DimX,
111 Window::Dimension(window.x().start(), window.x().end(), window.x().end() - window.x().start()));
112 window_src.set(Window::DimY,
113 Window::Dimension(window.y().start(), window.y().end(), window.y().end() - window.y().start()));
114 window_src.set(Window::DimZ,
115 Window::Dimension(window.z().start(), window.z().end(), window.z().end() - window.z().start()));
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000116 window_src.set(3, Window::Dimension(window[3].start(), window[3].end(), window[3].end() - window[3].start()));
117 }
118
119 // Destination window
120 Window window_dst(window);
121 const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100122 for (size_t d = 0; d <= dst->info()->num_dimensions(); ++d)
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000123 {
124 window_dst.set(d, zero_window);
125 }
126
127 // Create iterators
128 Iterator src_it(src, window_src);
129 Iterator dst_it(dst, window_dst);
130
131 int in_row_stride = 0;
132 int in_col_stride = 0;
133 int in_channel_stride = 0;
134 int in_batch_stride = 0;
135 int n_cols = 0;
136 int n_rows = 0;
137 int n_channels = 0;
138 int n_batches = 0;
139
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100140 switch (src_layout)
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000141 {
142 case DataLayout::NCHW:
143 {
144 in_row_stride = src->info()->strides_in_bytes().y() / sizeof(T);
145 in_channel_stride = src->info()->strides_in_bytes().z() / sizeof(T);
146 in_batch_stride = src->info()->strides_in_bytes()[3] / sizeof(T);
147 n_cols = src->info()->tensor_shape().x();
148 n_rows = window_src.y().step();
149 n_channels = src->info()->tensor_shape().z();
150 n_batches = src->info()->tensor_shape()[3];
151 break;
152 }
153 case DataLayout::NHWC:
154 {
155 in_col_stride = src->info()->strides_in_bytes().y() / sizeof(T);
156 in_row_stride = src->info()->strides_in_bytes().z() / sizeof(T);
157 in_batch_stride = src->info()->strides_in_bytes()[3] / sizeof(T);
158 n_channels = src->info()->tensor_shape().x();
159 n_cols = window_src.y().step();
160 n_rows = src->info()->tensor_shape().z();
161 n_batches = src->info()->tensor_shape()[3];
162 break;
163 }
164 default:
165 {
166 ARM_COMPUTE_ERROR("Invalid source data layout.");
167 break;
168 }
169 }
170
171 // CHW -> HWC
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100172 if (src_layout == DataLayout::NCHW && perm == PermutationVector{2U, 0U, 1U})
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000173 {
174 const int out_channel_stride = dst->info()->strides_in_bytes().x() / sizeof(T);
175 const int out_col_stride = dst->info()->strides_in_bytes().y() / sizeof(T);
176 const int out_row_stride = dst->info()->strides_in_bytes().z() / sizeof(T);
177 const int out_batch_stride = dst->info()->strides_in_bytes()[3] / sizeof(T);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100178 execute_window_loop(
179 window_src,
180 [&](const Coordinates &id)
181 {
182 const int idx = id[0] * out_col_stride + id[1] * out_row_stride + id[2] * out_channel_stride;
183 reorder::nchw_to_nhwc(reinterpret_cast<const T *>(src_it.ptr()),
184 reinterpret_cast<T *>(dst_it.ptr()) + idx, n_batches, n_channels, n_rows, n_cols,
185 in_batch_stride, in_channel_stride, in_row_stride, out_batch_stride,
186 out_row_stride, out_col_stride);
187 },
188 src_it, dst_it);
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000189 }
190 // HWC -> CHW
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100191 else if (src_layout == DataLayout::NHWC && perm == PermutationVector{1U, 2U, 0U})
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000192 {
193 const int out_col_stride = dst->info()->strides_in_bytes().x() / sizeof(T);
194 const int out_row_stride = dst->info()->strides_in_bytes().y() / sizeof(T);
195 const int out_channel_stride = dst->info()->strides_in_bytes().z() / sizeof(T);
196 const int out_batch_stride = dst->info()->strides_in_bytes()[3] / sizeof(T);
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100197 execute_window_loop(
198 window_src,
199 [&](const Coordinates &id)
200 {
201 const int idx = id[0] * out_channel_stride + id[1] * out_col_stride + id[2] * out_row_stride;
202 reorder::nhwc_to_nchw(reinterpret_cast<const T *>(src_it.ptr()),
203 reinterpret_cast<T *>(dst_it.ptr()) + idx, n_batches, n_rows, n_cols, n_channels,
204 in_batch_stride, in_row_stride, in_col_stride, out_batch_stride,
205 out_channel_stride, out_row_stride);
206 },
207 src_it, dst_it);
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000208 }
209 else
210 {
211 // All other cases fall back to C++
212 // Permute strides
213 Strides strides = dst->info()->strides_in_bytes();
214 Strides perm_strides = strides;
215 permute_strides(perm_strides, perm);
216 const int perm_stride_3 = src->info()->num_dimensions() >= 4 ? perm_strides[3] : 0;
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100217 execute_window_loop(
218 window,
219 [&](const Coordinates &id)
220 {
221 const int idx =
222 id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_stride_3;
223 *(reinterpret_cast<T *>(dst_it.ptr() + idx)) = *(reinterpret_cast<const T *>(src_it.ptr()));
224 },
225 src_it, dst_it);
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000226 }
227}
228} // namespace
229
230void CpuPermuteKernel::configure(const ITensorInfo *src, ITensorInfo *dst, const PermutationVector &perm)
231{
232 ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst);
233 const TensorShape dst_shape = misc::shape_calculator::compute_permutation_output_shape(*src, perm);
234 // Destination auto inizialitation if not yet initialized
235 auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape));
236
237 // Perform validation step
238 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(src, dst, perm));
239
240 _perm = perm;
241
242 // Configure kernel window
243 Window win = calculate_max_window(*src, Steps());
244
Teresa Charlind1dc09c2021-03-04 15:24:45 +0000245 // This kernel doesn't need padding so update_window_and_padding() can be skipped
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000246
247 ICpuKernel::configure(win);
248}
249
250Status CpuPermuteKernel::validate(const ITensorInfo *src, const ITensorInfo *dst, const PermutationVector &perm)
251{
252 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(src, dst, perm));
253 return Status{};
254}
255
256void CpuPermuteKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
257{
258 ARM_COMPUTE_UNUSED(info);
259 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
260 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
261
262 const auto src = tensors.get_const_tensor(TensorType::ACL_SRC);
263 auto dst = tensors.get_tensor(TensorType::ACL_DST);
264
Felix Thomasmathibalanafd38f02023-09-27 17:46:17 +0100265 switch (src->info()->element_size())
Georgios Pinitas0f7ef8a2021-01-10 04:23:52 +0000266 {
267 case 1:
268 run_permute<uint8_t>(window, src, dst, _perm);
269 break;
270 case 2:
271 run_permute<uint16_t>(window, src, dst, _perm);
272 break;
273 case 4:
274 run_permute<uint32_t>(window, src, dst, _perm);
275 break;
276 default:
277 ARM_COMPUTE_ERROR("Element size not supported");
278 break;
279 }
280}
281
282const char *CpuPermuteKernel::name() const
283{
284 return "CpuPermuteKernel";
285}
286} // namespace kernels
287} // namespace cpu
288} // namespace arm_compute