blob: 737b10b03c8720ffbcd69b31034ceb70f7fd81e2 [file] [log] [blame]
Georgios Pinitas284cfe22018-02-13 12:15:13 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2018-2019 Arm Limited.
Georgios Pinitas284cfe22018-02-13 12:15:13 +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/NEPermuteKernel.h"
25
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"
31#include "arm_compute/core/Validate.h"
32#include "arm_compute/core/utils/misc/ShapeCalculator.h"
33
34namespace
35{
36#include "arm_compute/core/NEON/kernels/convolution/common/shims.hpp"
37} // namespace
38
39#include <cstddef>
40#include <cstdint>
41
42using namespace arm_compute;
43
44namespace
45{
Pablo Tello35767bc2018-12-05 17:36:30 +000046inline bool is_permutation_supported(const PermutationVector &v)
47{
48 static const std::array<PermutationVector, 6> permutations3 =
49 {
Georgios Pinitasd57891a2019-02-19 18:10:03 +000050 {
51 PermutationVector(2U, 0U, 1U),
52 PermutationVector(1U, 2U, 0U),
53 PermutationVector(0U, 1U, 2U),
54 PermutationVector(0U, 2U, 1U),
55 PermutationVector(1U, 0U, 2U),
56 PermutationVector(2U, 1U, 0U),
57 }
Pablo Tello35767bc2018-12-05 17:36:30 +000058 };
59 static const std::array<PermutationVector, 24> permutations4 =
60 {
Georgios Pinitasd57891a2019-02-19 18:10:03 +000061 {
62 PermutationVector(0U, 1U, 2U, 3U),
63 PermutationVector(1U, 0U, 2U, 3U),
64 PermutationVector(2U, 0U, 1U, 3U),
65 PermutationVector(0U, 2U, 1U, 3U),
66 PermutationVector(1U, 2U, 0U, 3U),
67 PermutationVector(2U, 1U, 0U, 3U),
68 PermutationVector(2U, 1U, 3U, 0U),
69 PermutationVector(1U, 2U, 3U, 0U),
70 PermutationVector(3U, 2U, 1U, 0U),
71 PermutationVector(2U, 3U, 1U, 0U),
72 PermutationVector(1U, 3U, 2U, 0U),
73 PermutationVector(3U, 1U, 2U, 0U),
74 PermutationVector(3U, 0U, 2U, 1U),
75 PermutationVector(0U, 3U, 2U, 1U),
76 PermutationVector(2U, 3U, 0U, 1U),
77 PermutationVector(3U, 2U, 0U, 1U),
78 PermutationVector(0U, 2U, 3U, 1U),
79 PermutationVector(2U, 0U, 3U, 1U),
80 PermutationVector(1U, 0U, 3U, 2U),
81 PermutationVector(0U, 1U, 3U, 2U),
82 PermutationVector(3U, 1U, 0U, 2U),
83 PermutationVector(1U, 3U, 0U, 2U),
84 PermutationVector(0U, 3U, 1U, 2U),
85 PermutationVector(3U, 0U, 1U, 2U)
86 }
Pablo Tello35767bc2018-12-05 17:36:30 +000087 };
88
89 return (permutations3.end() != std::find(permutations3.begin(), permutations3.end(), v)) || (permutations4.end() != std::find(permutations4.begin(), permutations4.end(), v));
90}
91
Georgios Pinitas284cfe22018-02-13 12:15:13 +000092Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
93{
Georgios Pinitas33843562019-12-10 13:33:18 +000094 ARM_COMPUTE_RETURN_ERROR_ON(input->data_type() == DataType::UNKNOWN);
Pablo Tello35767bc2018-12-05 17:36:30 +000095 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_permutation_supported(perm), "PermutationVector not supported.");
Georgios Pinitas284cfe22018-02-13 12:15:13 +000096
97 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
98
99 // Validate configured output
100 if(output->total_size() != 0)
101 {
102 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +0000103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000104 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000105 }
106
107 return Status{};
108}
109} // namespace
110
111template <typename T>
112void NEPermuteKernel::run_permute(const Window &window)
113{
Pablo Tello35767bc2018-12-05 17:36:30 +0000114 const DataLayout input_layout = _input->info()->data_layout();
115
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000116 // Input window
117 Window window_in = window;
Pablo Tello35767bc2018-12-05 17:36:30 +0000118
119 // we only support these two configs in arm_compute/core/NEON/kernels/convolution/common/shims.hpp, for all others
120 // we have to fall back to C++
121 if((input_layout == DataLayout::NCHW && _perm == PermutationVector{ 2U, 0U, 1U }) || (input_layout == DataLayout::NHWC && _perm == PermutationVector{ 1U, 2U, 0U }))
122 {
123 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().end() - window.x().start()));
124 window_in.set(Window::DimY, Window::Dimension(window.y().start(), window.y().end(), window.y().end() - window.y().start()));
125 window_in.set(Window::DimZ, Window::Dimension(window.z().start(), window.z().end(), window.z().end() - window.z().start()));
126 window_in.set(3, Window::Dimension(window[3].start(), window[3].end(), window[3].end() - window[3].start()));
127 }
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000128
129 // Output window
130 Window window_out(window);
131 const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
132 for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
133 {
134 window_out.set(d, zero_window);
135 }
136
137 // Create iterators
138 Iterator in(_input, window_in);
139 Iterator out(_output, window_out);
140
Pablo Tello35767bc2018-12-05 17:36:30 +0000141 int in_row_stride = 0;
142 int in_col_stride = 0;
143 int in_channel_stride = 0;
144 int in_batch_stride = 0;
145 int n_cols = 0;
146 int n_rows = 0;
147 int n_channels = 0;
148 int n_batches = 0;
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000149
Pablo Tello35767bc2018-12-05 17:36:30 +0000150 switch(input_layout)
151 {
152 case DataLayout::NCHW:
153 {
154 in_row_stride = _input->info()->strides_in_bytes().y() / sizeof(T);
155 in_channel_stride = _input->info()->strides_in_bytes().z() / sizeof(T);
156 in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T);
157 n_cols = _input->info()->tensor_shape().x();
158 n_rows = window_in.y().step();
159 n_channels = _input->info()->tensor_shape().z();
160 n_batches = _input->info()->tensor_shape()[3];
161 break;
162 }
163 case DataLayout::NHWC:
164 {
165 in_col_stride = _input->info()->strides_in_bytes().y() / sizeof(T);
166 in_row_stride = _input->info()->strides_in_bytes().z() / sizeof(T);
167 in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T);
168 n_channels = _input->info()->tensor_shape().x();
169 n_cols = window_in.y().step();
170 n_rows = _input->info()->tensor_shape().z();
171 n_batches = _input->info()->tensor_shape()[3];
172 break;
173 }
174 default:
175 {
176 ARM_COMPUTE_ERROR("Invalid input data layout.");
177 break;
178 }
179 }
180
181 // CHW -> HWC
182 if(input_layout == DataLayout::NCHW && _perm == PermutationVector{ 2U, 0U, 1U })
183 {
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000184 const int out_channel_stride = _output->info()->strides_in_bytes().x() / sizeof(T);
185 const int out_col_stride = _output->info()->strides_in_bytes().y() / sizeof(T);
186 const int out_row_stride = _output->info()->strides_in_bytes().z() / sizeof(T);
187 const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000188 execute_window_loop(window_in, [&](const Coordinates & id)
189 {
190 const int idx = id[0] * out_col_stride + id[1] * out_row_stride + id[2] * out_channel_stride;
191 reorder::nchw_to_nhwc(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx,
192 n_batches, n_channels, n_rows, n_cols,
193 in_batch_stride, in_channel_stride, in_row_stride,
194 out_batch_stride, out_row_stride, out_col_stride);
195 },
196 in, out);
197 }
198 // HWC -> CHW
Pablo Tello35767bc2018-12-05 17:36:30 +0000199 else if(input_layout == DataLayout::NHWC && _perm == PermutationVector{ 1U, 2U, 0U })
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000200 {
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000201 const int out_col_stride = _output->info()->strides_in_bytes().x() / sizeof(T);
202 const int out_row_stride = _output->info()->strides_in_bytes().y() / sizeof(T);
203 const int out_channel_stride = _output->info()->strides_in_bytes().z() / sizeof(T);
204 const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000205 execute_window_loop(window_in, [&](const Coordinates & id)
206 {
207 const int idx = id[0] * out_channel_stride + id[1] * out_col_stride + id[2] * out_row_stride;
208 reorder::nhwc_to_nchw(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx,
209 n_batches, n_rows, n_cols, n_channels,
210 in_batch_stride, in_row_stride, in_col_stride,
211 out_batch_stride, out_channel_stride, out_row_stride);
212 },
213 in, out);
214 }
215 else
216 {
Pablo Tello35767bc2018-12-05 17:36:30 +0000217 // All other cases fall back to C++
218 // Permute strides
219 Strides strides = _output->info()->strides_in_bytes();
220 Strides perm_strides = strides;
221 permute_strides(perm_strides, _perm);
222 const int perm_stride_3 = _input->info()->num_dimensions() >= 4 ? perm_strides[3] : 0;
223 execute_window_loop(window, [&](const Coordinates & id)
224 {
225 const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_stride_3;
226 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
227 },
228 in, out);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000229 }
230}
231
232NEPermuteKernel::NEPermuteKernel()
233 : _func(), _input(nullptr), _output(nullptr), _perm()
234{
235}
236
237void NEPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
238{
239 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
240 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm);
241 // Output auto inizialitation if not yet initialized
242 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
243
244 // Perform validation step
245 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
246
247 _input = input;
248 _output = output;
249 _perm = perm;
250
251 switch(input->info()->element_size())
252 {
253 case 1:
254 _func = &NEPermuteKernel::run_permute<uint8_t>;
255 break;
256 case 2:
257 _func = &NEPermuteKernel::run_permute<uint16_t>;
258 break;
259 case 4:
260 _func = &NEPermuteKernel::run_permute<uint32_t>;
261 break;
262 default:
263 ARM_COMPUTE_ERROR("Element size not supported");
264 break;
265 }
266
267 // Configure kernel window
268 Window win = calculate_max_window(*input->info(), Steps());
269
270 // The NEPermute doesn't need padding so update_window_and_padding() can be skipped
271 Coordinates coord;
272 coord.set_num_dimensions(output->info()->num_dimensions());
273 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
274
275 ICPPKernel::configure(win);
276}
277
278Status NEPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
279{
280 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
281 return Status{};
282}
283
284void NEPermuteKernel::run(const Window &window, const ThreadInfo &info)
285{
286 ARM_COMPUTE_UNUSED(info);
287 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
288 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
289
290 if(_func != nullptr)
291 {
292 (this->*_func)(window);
293 }
294}