blob: 897b764b45753f9eb7eefaf970b82d1be67444b8 [file] [log] [blame]
Georgios Pinitas284cfe22018-02-13 12:15:13 +00001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * 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{
Pablo Tello35767bc2018-12-05 17:36:30 +000094 ARM_COMPUTE_RETURN_ERROR_ON_MSG(!is_permutation_supported(perm), "PermutationVector not supported.");
Georgios Pinitas284cfe22018-02-13 12:15:13 +000095
96 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input, perm);
97
98 // Validate configured output
99 if(output->total_size() != 0)
100 {
101 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(output->tensor_shape(), output_shape);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +0000102 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000103 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000104 }
105
106 return Status{};
107}
108} // namespace
109
110template <typename T>
111void NEPermuteKernel::run_permute(const Window &window)
112{
Pablo Tello35767bc2018-12-05 17:36:30 +0000113 const DataLayout input_layout = _input->info()->data_layout();
114
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000115 // Input window
116 Window window_in = window;
Pablo Tello35767bc2018-12-05 17:36:30 +0000117
118 // we only support these two configs in arm_compute/core/NEON/kernels/convolution/common/shims.hpp, for all others
119 // we have to fall back to C++
120 if((input_layout == DataLayout::NCHW && _perm == PermutationVector{ 2U, 0U, 1U }) || (input_layout == DataLayout::NHWC && _perm == PermutationVector{ 1U, 2U, 0U }))
121 {
122 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), window.x().end() - window.x().start()));
123 window_in.set(Window::DimY, Window::Dimension(window.y().start(), window.y().end(), window.y().end() - window.y().start()));
124 window_in.set(Window::DimZ, Window::Dimension(window.z().start(), window.z().end(), window.z().end() - window.z().start()));
125 window_in.set(3, Window::Dimension(window[3].start(), window[3].end(), window[3].end() - window[3].start()));
126 }
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000127
128 // Output window
129 Window window_out(window);
130 const Window::Dimension zero_window = Window::Dimension(0, 0, 0);
131 for(size_t d = 0; d <= _perm.num_dimensions(); ++d)
132 {
133 window_out.set(d, zero_window);
134 }
135
136 // Create iterators
137 Iterator in(_input, window_in);
138 Iterator out(_output, window_out);
139
Pablo Tello35767bc2018-12-05 17:36:30 +0000140 int in_row_stride = 0;
141 int in_col_stride = 0;
142 int in_channel_stride = 0;
143 int in_batch_stride = 0;
144 int n_cols = 0;
145 int n_rows = 0;
146 int n_channels = 0;
147 int n_batches = 0;
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000148
Pablo Tello35767bc2018-12-05 17:36:30 +0000149 switch(input_layout)
150 {
151 case DataLayout::NCHW:
152 {
153 in_row_stride = _input->info()->strides_in_bytes().y() / sizeof(T);
154 in_channel_stride = _input->info()->strides_in_bytes().z() / sizeof(T);
155 in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T);
156 n_cols = _input->info()->tensor_shape().x();
157 n_rows = window_in.y().step();
158 n_channels = _input->info()->tensor_shape().z();
159 n_batches = _input->info()->tensor_shape()[3];
160 break;
161 }
162 case DataLayout::NHWC:
163 {
164 in_col_stride = _input->info()->strides_in_bytes().y() / sizeof(T);
165 in_row_stride = _input->info()->strides_in_bytes().z() / sizeof(T);
166 in_batch_stride = _input->info()->strides_in_bytes()[3] / sizeof(T);
167 n_channels = _input->info()->tensor_shape().x();
168 n_cols = window_in.y().step();
169 n_rows = _input->info()->tensor_shape().z();
170 n_batches = _input->info()->tensor_shape()[3];
171 break;
172 }
173 default:
174 {
175 ARM_COMPUTE_ERROR("Invalid input data layout.");
176 break;
177 }
178 }
179
180 // CHW -> HWC
181 if(input_layout == DataLayout::NCHW && _perm == PermutationVector{ 2U, 0U, 1U })
182 {
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000183 const int out_channel_stride = _output->info()->strides_in_bytes().x() / sizeof(T);
184 const int out_col_stride = _output->info()->strides_in_bytes().y() / sizeof(T);
185 const int out_row_stride = _output->info()->strides_in_bytes().z() / sizeof(T);
186 const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000187 execute_window_loop(window_in, [&](const Coordinates & id)
188 {
189 const int idx = id[0] * out_col_stride + id[1] * out_row_stride + id[2] * out_channel_stride;
190 reorder::nchw_to_nhwc(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx,
191 n_batches, n_channels, n_rows, n_cols,
192 in_batch_stride, in_channel_stride, in_row_stride,
193 out_batch_stride, out_row_stride, out_col_stride);
194 },
195 in, out);
196 }
197 // HWC -> CHW
Pablo Tello35767bc2018-12-05 17:36:30 +0000198 else if(input_layout == DataLayout::NHWC && _perm == PermutationVector{ 1U, 2U, 0U })
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000199 {
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000200 const int out_col_stride = _output->info()->strides_in_bytes().x() / sizeof(T);
201 const int out_row_stride = _output->info()->strides_in_bytes().y() / sizeof(T);
202 const int out_channel_stride = _output->info()->strides_in_bytes().z() / sizeof(T);
203 const int out_batch_stride = _output->info()->strides_in_bytes()[3] / sizeof(T);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000204 execute_window_loop(window_in, [&](const Coordinates & id)
205 {
206 const int idx = id[0] * out_channel_stride + id[1] * out_col_stride + id[2] * out_row_stride;
207 reorder::nhwc_to_nchw(reinterpret_cast<const T *>(in.ptr()), reinterpret_cast<T *>(out.ptr()) + idx,
208 n_batches, n_rows, n_cols, n_channels,
209 in_batch_stride, in_row_stride, in_col_stride,
210 out_batch_stride, out_channel_stride, out_row_stride);
211 },
212 in, out);
213 }
214 else
215 {
Pablo Tello35767bc2018-12-05 17:36:30 +0000216 // All other cases fall back to C++
217 // Permute strides
218 Strides strides = _output->info()->strides_in_bytes();
219 Strides perm_strides = strides;
220 permute_strides(perm_strides, _perm);
221 const int perm_stride_3 = _input->info()->num_dimensions() >= 4 ? perm_strides[3] : 0;
222 execute_window_loop(window, [&](const Coordinates & id)
223 {
224 const int idx = id[0] * perm_strides[0] + id[1] * perm_strides[1] + id[2] * perm_strides[2] + id[3] * perm_stride_3;
225 *(reinterpret_cast<T *>(out.ptr() + idx)) = *(reinterpret_cast<const T *>(in.ptr()));
226 },
227 in, out);
Georgios Pinitas284cfe22018-02-13 12:15:13 +0000228 }
229}
230
231NEPermuteKernel::NEPermuteKernel()
232 : _func(), _input(nullptr), _output(nullptr), _perm()
233{
234}
235
236void NEPermuteKernel::configure(const ITensor *input, ITensor *output, const PermutationVector &perm)
237{
238 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
239 const TensorShape output_shape = misc::shape_calculator::compute_permutation_output_shape(*input->info(), perm);
240 // Output auto inizialitation if not yet initialized
241 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(output_shape));
242
243 // Perform validation step
244 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info(), perm));
245
246 _input = input;
247 _output = output;
248 _perm = perm;
249
250 switch(input->info()->element_size())
251 {
252 case 1:
253 _func = &NEPermuteKernel::run_permute<uint8_t>;
254 break;
255 case 2:
256 _func = &NEPermuteKernel::run_permute<uint16_t>;
257 break;
258 case 4:
259 _func = &NEPermuteKernel::run_permute<uint32_t>;
260 break;
261 default:
262 ARM_COMPUTE_ERROR("Element size not supported");
263 break;
264 }
265
266 // Configure kernel window
267 Window win = calculate_max_window(*input->info(), Steps());
268
269 // The NEPermute doesn't need padding so update_window_and_padding() can be skipped
270 Coordinates coord;
271 coord.set_num_dimensions(output->info()->num_dimensions());
272 output->info()->set_valid_region(ValidRegion(coord, output->info()->tensor_shape()));
273
274 ICPPKernel::configure(win);
275}
276
277Status NEPermuteKernel::validate(const ITensorInfo *input, const ITensorInfo *output, const PermutationVector &perm)
278{
279 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output, perm));
280 return Status{};
281}
282
283void NEPermuteKernel::run(const Window &window, const ThreadInfo &info)
284{
285 ARM_COMPUTE_UNUSED(info);
286 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
287 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICPPKernel::window(), window);
288
289 if(_func != nullptr)
290 {
291 (this->*_func)(window);
292 }
293}