blob: a0a8b82494fa1ec24367a4cb37b61af95370a92d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Isabella Gottardi0a1090a2019-02-14 18:07:36 +00002 * Copyright (c) 2017-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
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"
Giorgio Arenae3d24ce2018-08-24 14:44:08 +010027#include "arm_compute/core/AccessWindowTranspose.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010028#include "arm_compute/core/Error.h"
29#include "arm_compute/core/Helpers.h"
30#include "arm_compute/core/ITensor.h"
Isabella Gottardid56e7702018-02-28 14:29:36 +000031#include "arm_compute/core/TensorInfo.h"
Gian Marco5420b282017-11-29 10:41:38 +000032#include "arm_compute/core/Utils.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010033#include "arm_compute/core/Validate.h"
34
35#include <arm_neon.h>
36
37using namespace arm_compute;
38
39namespace arm_compute
40{
41class Coordinates;
42} // namespace arm_compute
43
44namespace
45{
Gian Marco7c435f22017-12-05 16:17:23 +000046TensorShape transposed_tensor_shape(const TensorShape &in)
47{
48 TensorShape output_shape{ in };
49 const size_t w_out = in[1];
50 const size_t h_out = in[0];
51 output_shape.set(0, w_out);
52 output_shape.set(1, h_out);
53
54 return output_shape;
55}
56
57unsigned int num_elems_processed(size_t element_size)
58{
59 switch(element_size)
60 {
61 case 1:
62 return 8;
63 break;
64 case 2:
65 return 4;
66 break;
67 case 4:
68 return 4;
69 break;
70 default:
71 ARM_COMPUTE_ERROR("Element size not supported");
72 break;
73 }
74}
75
Georgios Pinitas631c41a2017-12-06 11:53:03 +000076Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +000077{
Anthony Barbiereaefd002018-07-20 17:49:35 +010078 //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use NEON FP16 instructions.
Vidhya Sudhan Loganathan7485d5a2018-07-04 09:34:00 +010079 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::U8, DataType::S8, DataType::QASYMM8, DataType::U16, DataType::S16, DataType::U32, DataType::S32,
Gian Marco7c435f22017-12-05 16:17:23 +000080 DataType::F16,
81 DataType::F32);
82
83 if(output->total_size() != 0)
84 {
85 const TensorInfo tensor_info = input->clone()->set_tensor_shape(transposed_tensor_shape(input->tensor_shape()));
86
87 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(output, &tensor_info);
88 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(input, output);
Isabella Gottardi0a1090a2019-02-14 18:07:36 +000089 ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(input, output);
Gian Marco7c435f22017-12-05 16:17:23 +000090 }
91
Georgios Pinitas631c41a2017-12-06 11:53:03 +000092 return Status{};
Gian Marco7c435f22017-12-05 16:17:23 +000093}
94
Georgios Pinitas631c41a2017-12-06 11:53:03 +000095std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +000096{
Gian Marcob42d53c2017-12-07 10:09:07 +000097 // Note: This kernel performs 16 elements per iteration.
98 // 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
99 // For this reason num_elems_processed_per_iteration_x is set to 1
100 const unsigned int num_elems_processed_per_iteration_x = 1;
101 const unsigned int num_elems_processed_per_iteration_y = num_elems_processed(input->element_size());
Gian Marco7c435f22017-12-05 16:17:23 +0000102
103 // Configure kernel window
Gian Marcob42d53c2017-12-07 10:09:07 +0000104 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 +0000105
Giorgio Arenae3d24ce2018-08-24 14:44:08 +0100106 AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y);
107 bool window_changed = update_window_and_padding(win, input_access);
Gian Marco7c435f22017-12-05 16:17:23 +0000108
109 if(output->total_size() != 0)
110 {
Giorgio Arenae3d24ce2018-08-24 14:44:08 +0100111 AccessWindowTranspose output_access(output, 0, 0, num_elems_processed_per_iteration_y, num_elems_processed_per_iteration_x);
Gian Marco7c435f22017-12-05 16:17:23 +0000112
113 window_changed = window_changed || update_window_and_padding(win, output_access);
114
115 output_access.set_valid_region(win, ValidRegion(Coordinates(), output->tensor_shape()));
116 }
117
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000118 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Gian Marco7c435f22017-12-05 16:17:23 +0000119 return std::make_pair(err, win);
120}
121
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100122void transpose_8bit_elements(const ITensor *in, ITensor *out, const Window &window)
123{
Gian Marcob42d53c2017-12-07 10:09:07 +0000124 const int window_step_x = 8;
125 const int window_step_y = 8;
126 const int window_start_x = window.x().start();
127 const int window_end_x = window.x().end();
128 const int window_start_y = window.y().start();
129 const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1)));
130 const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y;
131 const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1];
132 const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1];
133
134 // Check if we need a left-over loop for the y dimension
135 bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0);
136
137 Window window_in(window);
138 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
139 if(left_over_loop_y)
140 {
141 // Check if window_end_y_multiple_of is greater than window_start_y
142 if(window_end_y_multiple_of > window_start_y)
143 {
144 window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y));
145 }
146 else
147 {
148 window_in.set(Window::DimY, Window::Dimension(0, 0, 1));
149 }
150 }
151
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100152 Window window_out(window);
153 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
154 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
155
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100156 Iterator output(out, window_out);
157
Gian Marcob42d53c2017-12-07 10:09:07 +0000158 // Run the NEON path if and only if the input is not a row-vector
159 if(in->info()->dimension(1) != 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 {
Gian Marcob42d53c2017-12-07 10:09:07 +0000161 Iterator input(in, window_in);
162 execute_window_loop(window_in, [&](const Coordinates & id)
163 {
164 // Compute 8x8 elements per iteration
165 int x = window_start_x;
166 for(; x <= (window_end_x - window_step_x); x += window_step_x)
167 {
168 const uint8x8_t row0 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 0 * input_stride_in_bytes));
169 const uint8x8_t row1 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 1 * input_stride_in_bytes));
170 const uint8x8_t row2 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 2 * input_stride_in_bytes));
171 const uint8x8_t row3 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 3 * input_stride_in_bytes));
172 const uint8x8_t row4 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 4 * input_stride_in_bytes));
173 const uint8x8_t row5 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 5 * input_stride_in_bytes));
174 const uint8x8_t row6 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 6 * input_stride_in_bytes));
175 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 +0100176
Gian Marcob42d53c2017-12-07 10:09:07 +0000177 // Transpose 2x2
178 const uint8x8x2_t k0_u8 = vtrn_u8(row0, row1);
179 const uint8x8x2_t k1_u8 = vtrn_u8(row2, row3);
180 const uint8x8x2_t k2_u8 = vtrn_u8(row4, row5);
181 const uint8x8x2_t k3_u8 = vtrn_u8(row6, row7);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100182
Gian Marcob42d53c2017-12-07 10:09:07 +0000183 // Transpose 4x4
184 const uint16x4x2_t k0_u16 = vtrn_u16(vreinterpret_u16_u8(k0_u8.val[0]), vreinterpret_u16_u8(k1_u8.val[0]));
185 const uint16x4x2_t k1_u16 = vtrn_u16(vreinterpret_u16_u8(k0_u8.val[1]), vreinterpret_u16_u8(k1_u8.val[1]));
186 const uint16x4x2_t k2_u16 = vtrn_u16(vreinterpret_u16_u8(k2_u8.val[0]), vreinterpret_u16_u8(k3_u8.val[0]));
187 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 +0100188
Gian Marcob42d53c2017-12-07 10:09:07 +0000189 // Transpose 8x8
190 const uint32x2x2_t k0_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[0]), vreinterpret_u32_u16(k2_u16.val[0]));
191 const uint32x2x2_t k1_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[1]), vreinterpret_u32_u16(k2_u16.val[1]));
192 const uint32x2x2_t k2_u32 = vtrn_u32(vreinterpret_u32_u16(k1_u16.val[0]), vreinterpret_u32_u16(k3_u16.val[0]));
193 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 +0100194
Gian Marcob42d53c2017-12-07 10:09:07 +0000195 // Compute destination address
196 const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + x * output_stride_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100197
Gian Marcob42d53c2017-12-07 10:09:07 +0000198 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])));
199 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])));
200 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])));
201 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])));
202 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])));
203 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])));
204 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])));
205 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])));
206 }
207
208 // Compute left-over elements along the x dimension (1x8)
209 for(; x < window_end_x; ++x)
210 {
211 const uint8_t val0 = *(input.ptr() + x + 0 * input_stride_in_bytes);
212 const uint8_t val1 = *(input.ptr() + x + 1 * input_stride_in_bytes);
213 const uint8_t val2 = *(input.ptr() + x + 2 * input_stride_in_bytes);
214 const uint8_t val3 = *(input.ptr() + x + 3 * input_stride_in_bytes);
215 const uint8_t val4 = *(input.ptr() + x + 4 * input_stride_in_bytes);
216 const uint8_t val5 = *(input.ptr() + x + 5 * input_stride_in_bytes);
217 const uint8_t val6 = *(input.ptr() + x + 6 * input_stride_in_bytes);
218 const uint8_t val7 = *(input.ptr() + x + 7 * input_stride_in_bytes);
219
220 uint8x8_t result = vdup_n_u8(0);
221 result = vset_lane_u8(val0, result, 0);
222 result = vset_lane_u8(val1, result, 1);
223 result = vset_lane_u8(val2, result, 2);
224 result = vset_lane_u8(val3, result, 3);
225 result = vset_lane_u8(val4, result, 4);
226 result = vset_lane_u8(val5, result, 5);
227 result = vset_lane_u8(val6, result, 6);
228 result = vset_lane_u8(val7, result, 7);
229
230 // Compute destination address
231 const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + x * output_stride_in_bytes;
232
233 vst1_u8(output.ptr() + dst_offset_in_bytes, result);
234 }
235 },
236 input, output);
237 }
238
239 if(left_over_loop_y)
240 {
241 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1));
242 window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1));
243
244 Iterator input(in, window_in);
245 Iterator output(out, window_out);
246
247 // Compute left-over elements along the y dimension (1x1)
248 execute_window_loop(window_in, [&](const Coordinates & id)
249 {
250 const uint8_t val0 = *input.ptr();
251
252 // Compute destination address
253 const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + id.x() * output_stride_in_bytes;
254
255 *(output.ptr() + dst_offset_in_bytes) = val0;
256 },
257 input, output);
258 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100259}
260
261void transpose_16bit_elements(const ITensor *in, ITensor *out, const Window &window)
262{
Gian Marcob42d53c2017-12-07 10:09:07 +0000263 const int window_step_x = 4;
264 const int window_step_y = 4;
265 const int window_start_x = window.x().start();
266 const int window_end_x = window.x().end();
267 const int window_start_y = window.y().start();
268 const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1)));
269 const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y;
270 const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1];
271 const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1];
272
273 // Check if we need a left-over loop for the y dimension
274 bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0);
275
276 Window window_in(window);
277 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
278 if(left_over_loop_y)
279 {
280 // Check if window_end_y_multiple_of is greater than window_start_y
281 if(window_end_y_multiple_of > window_start_y)
282 {
283 window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y));
284 }
285 else
286 {
287 window_in.set(Window::DimY, Window::Dimension(0, 0, 1));
288 }
289 }
290
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100291 Window window_out(window);
292 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
293 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
294
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100295 Iterator output(out, window_out);
296
Gian Marcob42d53c2017-12-07 10:09:07 +0000297 // Run the NEON path if and only if the input is not a row-vector
298 if(in->info()->dimension(1) != 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100299 {
Gian Marcob42d53c2017-12-07 10:09:07 +0000300 Iterator input(in, window_in);
301 execute_window_loop(window_in, [&](const Coordinates & id)
302 {
303 // Compute 4x4 elements per iteration
304 int x = window_start_x;
305 for(; x <= (window_end_x - window_step_x); x += window_step_x)
306 {
307 const uint16x4_t row0 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
308 const uint16x4_t row1 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
309 const uint16x4_t row2 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
310 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 +0100311
Gian Marcob42d53c2017-12-07 10:09:07 +0000312 // Transpose 2x2
313 const uint16x4x2_t k0_u16 = vtrn_u16(row0, row1);
314 const uint16x4x2_t k1_u16 = vtrn_u16(row2, row3);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100315
Gian Marcob42d53c2017-12-07 10:09:07 +0000316 // Transpose 4x4
317 const uint32x2x2_t k0_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[0]), vreinterpret_u32_u16(k1_u16.val[0]));
318 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 +0100319
Gian Marcob42d53c2017-12-07 10:09:07 +0000320 // Compute destination address
321 const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + x * output_stride_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100322
Gian Marcob42d53c2017-12-07 10:09:07 +0000323 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 0 * output_stride_in_bytes), vreinterpret_u16_u32(k0_u32.val[0]));
324 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 1 * output_stride_in_bytes), vreinterpret_u16_u32(k1_u32.val[0]));
325 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 2 * output_stride_in_bytes), vreinterpret_u16_u32(k0_u32.val[1]));
326 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 3 * output_stride_in_bytes), vreinterpret_u16_u32(k1_u32.val[1]));
327 }
328
329 // Compute left-over elements (1x4)
330 for(; x < window_end_x; ++x)
331 {
332 const uint16_t val0 = *(reinterpret_cast<uint16_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
333 const uint16_t val1 = *(reinterpret_cast<uint16_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
334 const uint16_t val2 = *(reinterpret_cast<uint16_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
335 const uint16_t val3 = *(reinterpret_cast<uint16_t *>(input.ptr() + 3 * input_stride_in_bytes) + x);
336
337 uint16x4_t result = vdup_n_u16(0);
338 result = vset_lane_u16(val0, result, 0);
339 result = vset_lane_u16(val1, result, 1);
340 result = vset_lane_u16(val2, result, 2);
341 result = vset_lane_u16(val3, result, 3);
342
343 // Compute destination address
344 const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + x * output_stride_in_bytes;
345
346 vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes), result);
347 }
348 },
349 input, output);
350 }
351
352 if(left_over_loop_y)
353 {
354 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1));
355 window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1));
356
357 Iterator input(in, window_in);
358 Iterator output(out, window_out);
359
360 // Compute left-over elements along the y dimension (1x1)
361 execute_window_loop(window_in, [&](const Coordinates & id)
362 {
363 const uint16_t val0 = *(reinterpret_cast<uint16_t *>(input.ptr()));
364
365 // Compute destination address
366 const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + id.x() * output_stride_in_bytes;
367
368 *(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes)) = val0;
369 },
370 input, output);
371 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100372}
373
374void transpose_32bit_elements(const ITensor *in, ITensor *out, const Window &window)
375{
Gian Marcob42d53c2017-12-07 10:09:07 +0000376 const int window_step_x = 4;
377 const int window_step_y = 4;
378 const int window_start_x = window.x().start();
379 const int window_end_x = window.x().end();
380 const int window_start_y = window.y().start();
381 const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1)));
382 const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y;
383 const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1];
384 const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1];
385
386 // Check if we need a left-over loop for the y dimension
387 bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0);
388
389 Window window_in(window);
390 window_in.set(Window::DimX, Window::Dimension(0, 1, 1));
391 if(left_over_loop_y)
392 {
393 // Check if window_end_y_multiple_of is greater than window_start_y
394 if(window_end_y_multiple_of > window_start_y)
395 {
396 window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y));
397 }
398 else
399 {
400 window_in.set(Window::DimY, Window::Dimension(0, 0, 1));
401 }
402 }
403
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100404 Window window_out(window);
405 window_out.set(Window::DimX, Window::Dimension(0, 0, 0));
406 window_out.set(Window::DimY, Window::Dimension(0, 0, 0));
407
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100408 Iterator output(out, window_out);
409
Gian Marcob42d53c2017-12-07 10:09:07 +0000410 // Run the NEON path if and only if the input is not a row-vector
411 if(in->info()->dimension(1) != 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100412 {
Gian Marcob42d53c2017-12-07 10:09:07 +0000413 Iterator input(in, window_in);
414 execute_window_loop(window_in, [&](const Coordinates & id)
415 {
416 // Compute 4x4 elements per iteration
417 int x = window_start_x;
418 for(; x <= (window_end_x - window_step_x); x += window_step_x)
419 {
420 const uint32x4_t row0 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
421 const uint32x4_t row1 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
422 const uint32x4_t row2 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
423 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 +0100424
Gian Marcob42d53c2017-12-07 10:09:07 +0000425 // Transpose 2x2
426 const uint32x2x2_t k0_u32 = vtrn_u32(vget_low_u32(row0), vget_low_u32(row1));
427 const uint32x2x2_t k1_u32 = vtrn_u32(vget_high_u32(row2), vget_high_u32(row3));
428 const uint32x2x2_t k2_u32 = vtrn_u32(vget_high_u32(row0), vget_high_u32(row1));
429 const uint32x2x2_t k3_u32 = vtrn_u32(vget_low_u32(row2), vget_low_u32(row3));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100430
Gian Marcob42d53c2017-12-07 10:09:07 +0000431 // Compute destination address
432 const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + x * output_stride_in_bytes;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100433
Gian Marcob42d53c2017-12-07 10:09:07 +0000434 // Swap block 01 with block 10 and store
435 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]));
436 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]));
437 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]));
438 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]));
439 }
440
441 // Compute left-over elements (1x4)
442 for(; x < window_end_x; ++x)
443 {
444 const uint32_t val0 = *(reinterpret_cast<uint32_t *>(input.ptr() + 0 * input_stride_in_bytes) + x);
445 const uint32_t val1 = *(reinterpret_cast<uint32_t *>(input.ptr() + 1 * input_stride_in_bytes) + x);
446 const uint32_t val2 = *(reinterpret_cast<uint32_t *>(input.ptr() + 2 * input_stride_in_bytes) + x);
447 const uint32_t val3 = *(reinterpret_cast<uint32_t *>(input.ptr() + 3 * input_stride_in_bytes) + x);
448
449 uint32x4_t result = vdupq_n_u32(0);
450 result = vsetq_lane_u32(val0, result, 0);
451 result = vsetq_lane_u32(val1, result, 1);
452 result = vsetq_lane_u32(val2, result, 2);
453 result = vsetq_lane_u32(val3, result, 3);
454
455 // Compute destination address
456 const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + x * output_stride_in_bytes;
457
458 vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes), result);
459 }
460 },
461 input, output);
462 }
463
464 if(left_over_loop_y)
465 {
466 window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1));
467 window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1));
468
469 Iterator input(in, window_in);
470 Iterator output(out, window_out);
471
472 // Compute left-over elements along the y dimension (1x1)
473 execute_window_loop(window_in, [&](const Coordinates & id)
474 {
475 const uint32_t val0 = *(reinterpret_cast<uint32_t *>(input.ptr()));
476
477 // Compute destination address
478 const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + id.x() * output_stride_in_bytes;
479
480 *(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes)) = val0;
481 },
482 input, output);
483 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100484}
485} // namespace
486
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000487Status NETransposeKernel::validate(const ITensorInfo *input, const ITensorInfo *output)
Gian Marco7c435f22017-12-05 16:17:23 +0000488{
489 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
490 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(input, output));
491 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(input->clone().get(), output->clone().get()).first);
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000492 return Status{};
Gian Marco7c435f22017-12-05 16:17:23 +0000493}
494
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100495NETransposeKernel::NETransposeKernel()
496 : _func(nullptr), _input(nullptr), _output(nullptr)
497{
498}
499
500void NETransposeKernel::configure(const ITensor *input, ITensor *output)
501{
Gian Marco7c435f22017-12-05 16:17:23 +0000502 ARM_COMPUTE_ERROR_ON_NULLPTR(input, output);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100503
504 // Output tensor auto inizialitation if not yet initialized
Gian Marco7c435f22017-12-05 16:17:23 +0000505 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 +0100506
Gian Marco7c435f22017-12-05 16:17:23 +0000507 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info()));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100508
509 _input = input;
510 _output = output;
511
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100512 switch(input->info()->element_size())
513 {
514 case 1:
Gian Marco7c435f22017-12-05 16:17:23 +0000515 _func = &transpose_8bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100516 break;
517 case 2:
Gian Marco7c435f22017-12-05 16:17:23 +0000518 _func = &transpose_16bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100519 break;
520 case 4:
Gian Marco7c435f22017-12-05 16:17:23 +0000521 _func = &transpose_32bit_elements;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100522 break;
523 default:
524 ARM_COMPUTE_ERROR("Element size not supported");
525 break;
526 }
527
528 // Configure kernel window
Gian Marco7c435f22017-12-05 16:17:23 +0000529 auto win_config = validate_and_configure_window(input->info(), output->info());
530 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
531 INEKernel::configure(win_config.second);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100532}
533
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100534void NETransposeKernel::run(const Window &window, const ThreadInfo &info)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100535{
Moritz Pflanzerc186b572017-09-07 09:48:04 +0100536 ARM_COMPUTE_UNUSED(info);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100537 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
538 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
539 ARM_COMPUTE_ERROR_ON(_func == nullptr);
540
541 (*_func)(_input, _output, window);
542}