blob: fc22b05823dd1ff9fd6d116c5c71be0101d78a4f [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2017 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 */
24#include "arm_compute/core/NEON/kernels/NETransposeKernel.h"
25
Gian Marco5420b282017-11-29 10:41:38 +000026#include "arm_compute/core/AccessWindowStatic.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010027#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
Gian Marco5420b282017-11-29 10:41:38 +000030#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/Validate.h"
32
33#include <arm_neon.h>
34
35using namespace arm_compute;
36
37namespace arm_compute
38{
39class Coordinates;
40} // namespace arm_compute
41
42namespace
43{
Gian Marco7c435f22017-12-05 16:17:23 +000044TensorShape transposed_tensor_shape(const TensorShape &in)
45{
46 TensorShape output_shape{ in };
47 const size_t w_out = in[1];
48 const size_t h_out = in[0];
49 output_shape.set(0, w_out);
50 output_shape.set(1, h_out);
51
52 return output_shape;
53}
54
55unsigned int num_elems_processed(size_t element_size)
56{
57 switch(element_size)
58 {
59 case 1:
60 return 8;
61 break;
62 case 2:
63 return 4;
64 break;
65 case 4:
66 return 4;
67 break;
68 default:
69 ARM_COMPUTE_ERROR("Element size not supported");
70 break;
71 }
72}
73
Georgios Pinitas631c41a2017-12-06 11:53:03 +000074Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +000075{
76 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QS8, DataType::QASYMM8, DataType::U16, DataType::S16, DataType::QS16, DataType::U32, DataType::S32,
77 DataType::F16,
78 DataType::F32);
79
80 if(output->total_size() != 0)
81 {
82 const TensorInfo tensor_info = input->clone()->set_tensor_shape(transposed_tensor_shape(input->tensor_shape()));
83
84 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info);
85 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
86 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_FIXED_POINT(input, output);
87 }
88
Georgios Pinitas631c41a2017-12-06 11:53:03 +000089 return Status{};
Gian Marco7c435f22017-12-05 16:17:23 +000090}
91
Georgios Pinitas631c41a2017-12-06 11:53:03 +000092std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +000093{
Gian Marcob42d53c2017-12-07 10:09:07 +000094 // Note: This kernel performs 16 elements per iteration.
95 // However, since we use a left-over for loop on both dimensions (X and Y), we cannot have any read or write out of memory
96 // For this reason num_elems_processed_per_iteration_x is set to 1
97 const unsigned int num_elems_processed_per_iteration_x = 1;
98 const unsigned int num_elems_processed_per_iteration_y = num_elems_processed(input->element_size());
Gian Marco7c435f22017-12-05 16:17:23 +000099
100 // Configure kernel window
Gian Marcob42d53c2017-12-07 10:09:07 +0000101 Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y));
Gian Marco7c435f22017-12-05 16:17:23 +0000102
Gian Marcob42d53c2017-12-07 10:09:07 +0000103 AccessWindowStatic input_access(input, 0, 0, input->dimension(0), input->dimension(1));
Gian Marco7c435f22017-12-05 16:17:23 +0000104
105 bool window_changed = update_window_and_padding(win, input_access);
106
107 if(output->total_size() != 0)
108 {
109 // TODO (COMPMID-708): Replace AccessWindowStatic with AccessWindowTranspose
Gian Marcob42d53c2017-12-07 10:09:07 +0000110 AccessWindowStatic output_access(output, 0, 0, output->dimension(0), output->dimension(1));
Gian Marco7c435f22017-12-05 16:17:23 +0000111
112 window_changed = window_changed || update_window_and_padding(win, output_access);
113
114 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
115 }
116
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000117 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marco7c435f22017-12-05 16:17:23 +0000118 return std::make_pair(err, win);
119}
120
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100121void transpose_8bit_elements(const ITensor *in, ITensor *out, const Window &window)
122{
Gian Marcob42d53c2017-12-07 10:09:07 +0000123 const int window_step_x = 8;
124 const int window_step_y = 8;
125 const int window_start_x = window.x().start();
126 const int window_end_x = window.x().end();
127 const int window_start_y = window.y().start();
128 const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1)));
129 const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y;
130 const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1];
131 const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1];
132
133 // Check if we need a left-over loop for the y dimension
134 bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0);
135
136 Window window_in(window);
137 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
138 if(left_over_loop_y)
139 {
140 // Check if window_end_y_multiple_of is greater than window_start_y
141 if(window_end_y_multiple_of > window_start_y)
142 {
143 window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y));
144 }
145 else
146 {
147 window_in.set(Window::DimY, Window::Dimension(0, 0, 1));
148 }
149 }
150
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100151 Window window_out(window);
152 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
153 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
154
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100155 Iterator output(out, window_out);
156
Gian Marcob42d53c2017-12-07 10:09:07 +0000157 // Run the NEON path if and only if the input is not a row-vector
158 if(in->info()->dimension(1) != 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100159 {
Gian Marcob42d53c2017-12-07 10:09:07 +0000160 Iterator input(in, window_in);
161 execute_window_loop(window_in, [&](const Coordinates & id)
162 {
163 // Compute 8x8 elements per iteration
164 int x = window_start_x;
165 for(; x <= (window_end_x - window_step_x); x += window_step_x)
166 {
167 const uint8x8_t row0 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 0 * input_stride_in_bytes));
168 const uint8x8_t row1 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 1 * input_stride_in_bytes));
169 const uint8x8_t row2 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 2 * input_stride_in_bytes));
170 const uint8x8_t row3 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 3 * input_stride_in_bytes));
171 const uint8x8_t row4 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 4 * input_stride_in_bytes));
172 const uint8x8_t row5 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 5 * input_stride_in_bytes));
173 const uint8x8_t row6 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 6 * input_stride_in_bytes));
174 const uint8x8_t row7 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 7 * input_stride_in_bytes));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100175
Gian Marcob42d53c2017-12-07 10:09:07 +0000176 // Transpose 2x2
177 const uint8x8x2_t k0_u8 = vtrn_u8(row0, row1);
178 const uint8x8x2_t k1_u8 = vtrn_u8(row2, row3);
179 const uint8x8x2_t k2_u8 = vtrn_u8(row4, row5);
180 const uint8x8x2_t k3_u8 = vtrn_u8(row6, row7);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100181
Gian Marcob42d53c2017-12-07 10:09:07 +0000182 // Transpose 4x4
183 const uint16x4x2_t k0_u16 = vtrn_u16(vreinterpret_u16_u8(k0_u8.val[0]), vreinterpret_u16_u8(k1_u8.val[0]));
184 const uint16x4x2_t k1_u16 = vtrn_u16(vreinterpret_u16_u8(k0_u8.val[1]), vreinterpret_u16_u8(k1_u8.val[1]));
185 const uint16x4x2_t k2_u16 = vtrn_u16(vreinterpret_u16_u8(k2_u8.val[0]), vreinterpret_u16_u8(k3_u8.val[0]));
186 const uint16x4x2_t k3_u16 = vtrn_u16(vreinterpret_u16_u8(k2_u8.val[1]), vreinterpret_u16_u8(k3_u8.val[1]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187
Gian Marcob42d53c2017-12-07 10:09:07 +0000188 // Transpose 8x8
189 const uint32x2x2_t k0_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[0]), vreinterpret_u32_u16(k2_u16.val[0]));
190 const uint32x2x2_t k1_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[1]), vreinterpret_u32_u16(k2_u16.val[1]));
191 const uint32x2x2_t k2_u32 = vtrn_u32(vreinterpret_u32_u16(k1_u16.val[0]), vreinterpret_u32_u16(k3_u16.val[0]));
192 const uint32x2x2_t k3_u32 = vtrn_u32(vreinterpret_u32_u16(k1_u16.val[1]), vreinterpret_u32_u16(k3_u16.val[1]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100193
Gian Marcob42d53c2017-12-07 10:09:07 +0000194 // Compute destination address
195 const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + x * output_stride_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196
Gian Marcob42d53c2017-12-07 10:09:07 +0000197 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 0 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k0_u32.val[0])));
198 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 1 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k2_u32.val[0])));
199 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 2 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k1_u32.val[0])));
200 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 3 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k3_u32.val[0])));
201 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 4 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k0_u32.val[1])));
202 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 5 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k2_u32.val[1])));
203 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 6 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k1_u32.val[1])));
204 vst1_u8(reinterpret_cast<uint8_t *>(output.ptr() + dst_offset_in_bytes + 7 * output_stride_in_bytes), vreinterpret_u8_u16(vreinterpret_u16_u32(k3_u32.val[1])));
205 }
206
207 // Compute left-over elements along the x dimension (1x8)
208 for(; x < window_end_x; ++x)
209 {
210 const uint8_t val0 = *(input.ptr() + x + 0 * input_stride_in_bytes);
211 const uint8_t val1 = *(input.ptr() + x + 1 * input_stride_in_bytes);
212 const uint8_t val2 = *(input.ptr() + x + 2 * input_stride_in_bytes);
213 const uint8_t val3 = *(input.ptr() + x + 3 * input_stride_in_bytes);
214 const uint8_t val4 = *(input.ptr() + x + 4 * input_stride_in_bytes);
215 const uint8_t val5 = *(input.ptr() + x + 5 * input_stride_in_bytes);
216 const uint8_t val6 = *(input.ptr() + x + 6 * input_stride_in_bytes);
217 const uint8_t val7 = *(input.ptr() + x + 7 * input_stride_in_bytes);
218
219 uint8x8_t result = vdup_n_u8(0);
220 result = vset_lane_u8(val0, result, 0);
221 result = vset_lane_u8(val1, result, 1);
222 result = vset_lane_u8(val2, result, 2);
223 result = vset_lane_u8(val3, result, 3);
224 result = vset_lane_u8(val4, result, 4);
225 result = vset_lane_u8(val5, result, 5);
226 result = vset_lane_u8(val6, result, 6);
227 result = vset_lane_u8(val7, result, 7);
228
229 // Compute destination address
230 const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + x * output_stride_in_bytes;
231
232 vst1_u8(output.ptr() + dst_offset_in_bytes, result);
233 }
234 },
235 input, output);
236 }
237
238 if(left_over_loop_y)
239 {
240 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1));
241 window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1));
242
243 Iterator input(in, window_in);
244 Iterator output(out, window_out);
245
246 // Compute left-over elements along the y dimension (1x1)
247 execute_window_loop(window_in, [&](const Coordinates & id)
248 {
249 const uint8_t val0 = *input.ptr();
250
251 // Compute destination address
252 const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + id.x() * output_stride_in_bytes;
253
254 *(output.ptr() + dst_offset_in_bytes) = val0;
255 },
256 input, output);
257 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100258}
259
260void transpose_16bit_elements(const ITensor *in, ITensor *out, const Window &window)
261{
Gian Marcob42d53c2017-12-07 10:09:07 +0000262 const int window_step_x = 4;
263 const int window_step_y = 4;
264 const int window_start_x = window.x().start();
265 const int window_end_x = window.x().end();
266 const int window_start_y = window.y().start();
267 const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1)));
268 const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y;
269 const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1];
270 const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1];
271
272 // Check if we need a left-over loop for the y dimension
273 bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0);
274
275 Window window_in(window);
276 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
277 if(left_over_loop_y)
278 {
279 // Check if window_end_y_multiple_of is greater than window_start_y
280 if(window_end_y_multiple_of > window_start_y)
281 {
282 window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y));
283 }
284 else
285 {
286 window_in.set(Window::DimY, Window::Dimension(0, 0, 1));
287 }
288 }
289
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100290 Window window_out(window);
291 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
292 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
293
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100294 Iterator output(out, window_out);
295
Gian Marcob42d53c2017-12-07 10:09:07 +0000296 // Run the NEON path if and only if the input is not a row-vector
297 if(in->info()->dimension(1) != 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100298 {
Gian Marcob42d53c2017-12-07 10:09:07 +0000299 Iterator input(in, window_in);
300 execute_window_loop(window_in, [&](const Coordinates & id)
301 {
302 // Compute 4x4 elements per iteration
303 int x = window_start_x;
304 for(; x <= (window_end_x - window_step_x); x += window_step_x)
305 {
306 const uint16x4_t row0 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
307 const uint16x4_t row1 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
308 const uint16x4_t row2 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
309 const uint16x4_t row3 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 3 * input_stride_in_bytes) + x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100310
Gian Marcob42d53c2017-12-07 10:09:07 +0000311 // Transpose 2x2
312 const uint16x4x2_t k0_u16 = vtrn_u16(row0, row1);
313 const uint16x4x2_t k1_u16 = vtrn_u16(row2, row3);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100314
Gian Marcob42d53c2017-12-07 10:09:07 +0000315 // Transpose 4x4
316 const uint32x2x2_t k0_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[0]), vreinterpret_u32_u16(k1_u16.val[0]));
317 const uint32x2x2_t k1_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[1]), vreinterpret_u32_u16(k1_u16.val[1]));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100318
Gian Marcob42d53c2017-12-07 10:09:07 +0000319 // Compute destination address
320 const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + x * output_stride_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100321
Gian Marcob42d53c2017-12-07 10:09:07 +0000322 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 0 * output_stride_in_bytes), vreinterpret_u16_u32(k0_u32.val[0]));
323 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 1 * output_stride_in_bytes), vreinterpret_u16_u32(k1_u32.val[0]));
324 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 2 * output_stride_in_bytes), vreinterpret_u16_u32(k0_u32.val[1]));
325 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 3 * output_stride_in_bytes), vreinterpret_u16_u32(k1_u32.val[1]));
326 }
327
328 // Compute left-over elements (1x4)
329 for(; x < window_end_x; ++x)
330 {
331 const uint16_t val0 = *(reinterpret_cast<uint16_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
332 const uint16_t val1 = *(reinterpret_cast<uint16_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
333 const uint16_t val2 = *(reinterpret_cast<uint16_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
334 const uint16_t val3 = *(reinterpret_cast<uint16_t *>(input.ptr() + 3 * input_stride_in_bytes) + x);
335
336 uint16x4_t result = vdup_n_u16(0);
337 result = vset_lane_u16(val0, result, 0);
338 result = vset_lane_u16(val1, result, 1);
339 result = vset_lane_u16(val2, result, 2);
340 result = vset_lane_u16(val3, result, 3);
341
342 // Compute destination address
343 const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + x * output_stride_in_bytes;
344
345 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes), result);
346 }
347 },
348 input, output);
349 }
350
351 if(left_over_loop_y)
352 {
353 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1));
354 window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1));
355
356 Iterator input(in, window_in);
357 Iterator output(out, window_out);
358
359 // Compute left-over elements along the y dimension (1x1)
360 execute_window_loop(window_in, [&](const Coordinates & id)
361 {
362 const uint16_t val0 = *(reinterpret_cast<uint16_t *>(input.ptr()));
363
364 // Compute destination address
365 const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + id.x() * output_stride_in_bytes;
366
367 *(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes)) = val0;
368 },
369 input, output);
370 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100371}
372
373void transpose_32bit_elements(const ITensor *in, ITensor *out, const Window &window)
374{
Gian Marcob42d53c2017-12-07 10:09:07 +0000375 const int window_step_x = 4;
376 const int window_step_y = 4;
377 const int window_start_x = window.x().start();
378 const int window_end_x = window.x().end();
379 const int window_start_y = window.y().start();
380 const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1)));
381 const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y;
382 const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1];
383 const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1];
384
385 // Check if we need a left-over loop for the y dimension
386 bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0);
387
388 Window window_in(window);
389 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
390 if(left_over_loop_y)
391 {
392 // Check if window_end_y_multiple_of is greater than window_start_y
393 if(window_end_y_multiple_of > window_start_y)
394 {
395 window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y));
396 }
397 else
398 {
399 window_in.set(Window::DimY, Window::Dimension(0, 0, 1));
400 }
401 }
402
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403 Window window_out(window);
404 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
405 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
406
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100407 Iterator output(out, window_out);
408
Gian Marcob42d53c2017-12-07 10:09:07 +0000409 // Run the NEON path if and only if the input is not a row-vector
410 if(in->info()->dimension(1) != 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100411 {
Gian Marcob42d53c2017-12-07 10:09:07 +0000412 Iterator input(in, window_in);
413 execute_window_loop(window_in, [&](const Coordinates & id)
414 {
415 // Compute 4x4 elements per iteration
416 int x = window_start_x;
417 for(; x <= (window_end_x - window_step_x); x += window_step_x)
418 {
419 const uint32x4_t row0 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
420 const uint32x4_t row1 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
421 const uint32x4_t row2 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
422 const uint32x4_t row3 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 3 * input_stride_in_bytes) + x);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100423
Gian Marcob42d53c2017-12-07 10:09:07 +0000424 // Transpose 2x2
425 const uint32x2x2_t k0_u32 = vtrn_u32(vget_low_u32(row0), vget_low_u32(row1));
426 const uint32x2x2_t k1_u32 = vtrn_u32(vget_high_u32(row2), vget_high_u32(row3));
427 const uint32x2x2_t k2_u32 = vtrn_u32(vget_high_u32(row0), vget_high_u32(row1));
428 const uint32x2x2_t k3_u32 = vtrn_u32(vget_low_u32(row2), vget_low_u32(row3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100429
Gian Marcob42d53c2017-12-07 10:09:07 +0000430 // Compute destination address
431 const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + x * output_stride_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100432
Gian Marcob42d53c2017-12-07 10:09:07 +0000433 // Swap block 01 with block 10 and store
434 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes + 0 * output_stride_in_bytes), vcombine_u32(k0_u32.val[0], k3_u32.val[0]));
435 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes + 1 * output_stride_in_bytes), vcombine_u32(k0_u32.val[1], k3_u32.val[1]));
436 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes + 2 * output_stride_in_bytes), vcombine_u32(k2_u32.val[0], k1_u32.val[0]));
437 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes + 3 * output_stride_in_bytes), vcombine_u32(k2_u32.val[1], k1_u32.val[1]));
438 }
439
440 // Compute left-over elements (1x4)
441 for(; x < window_end_x; ++x)
442 {
443 const uint32_t val0 = *(reinterpret_cast<uint32_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
444 const uint32_t val1 = *(reinterpret_cast<uint32_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
445 const uint32_t val2 = *(reinterpret_cast<uint32_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
446 const uint32_t val3 = *(reinterpret_cast<uint32_t *>(input.ptr() + 3 * input_stride_in_bytes) + x);
447
448 uint32x4_t result = vdupq_n_u32(0);
449 result = vsetq_lane_u32(val0, result, 0);
450 result = vsetq_lane_u32(val1, result, 1);
451 result = vsetq_lane_u32(val2, result, 2);
452 result = vsetq_lane_u32(val3, result, 3);
453
454 // Compute destination address
455 const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + x * output_stride_in_bytes;
456
457 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes), result);
458 }
459 },
460 input, output);
461 }
462
463 if(left_over_loop_y)
464 {
465 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1));
466 window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1));
467
468 Iterator input(in, window_in);
469 Iterator output(out, window_out);
470
471 // Compute left-over elements along the y dimension (1x1)
472 execute_window_loop(window_in, [&](const Coordinates & id)
473 {
474 const uint32_t val0 = *(reinterpret_cast<uint32_t *>(input.ptr()));
475
476 // Compute destination address
477 const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + id.x() * output_stride_in_bytes;
478
479 *(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes)) = val0;
480 },
481 input, output);
482 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100483}
484} // namespace
485
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000486Status NETransposeKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +0000487{
488 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
489 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
490 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000491 return Status{};
Gian Marco7c435f22017-12-05 16:17:23 +0000492}
493
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100494NETransposeKernel::NETransposeKernel()
495 : _func(nullptr), _input(nullptr), _output(nullptr)
496{
497}
498
499void NETransposeKernel::configure(const ITensor *input, ITensor *output)
500{
Gian Marco7c435f22017-12-05 16:17:23 +0000501 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100502
503 // Output tensor auto inizialitation if not yet initialized
Gian Marco7c435f22017-12-05 16:17:23 +0000504 auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(transposed_tensor_shape(input->info()->tensor_shape())));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100505
Gian Marco7c435f22017-12-05 16:17:23 +0000506 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100507
508 _input = input;
509 _output = output;
510
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100511 switch(input->info()->element_size())
512 {
513 case 1:
Gian Marco7c435f22017-12-05 16:17:23 +0000514 _func = &transpose_8bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100515 break;
516 case 2:
Gian Marco7c435f22017-12-05 16:17:23 +0000517 _func = &transpose_16bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100518 break;
519 case 4:
Gian Marco7c435f22017-12-05 16:17:23 +0000520 _func = &transpose_32bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100521 break;
522 default:
523 ARM_COMPUTE_ERROR("Element size not supported");
524 break;
525 }
526
527 // Configure kernel window
Gian Marco7c435f22017-12-05 16:17:23 +0000528 auto win_config = validate_and_configure_window(input->info(), output->info());
529 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
530 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100531}
532
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100533void NETransposeKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100534{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100535 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100536 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
537 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
538 ARM_COMPUTE_ERROR_ON(_func == nullptr);
539
540 (*_func)(_input, _output, window);
541}