Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 Marco | 5420b28 | 2017-11-29 10:41:38 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/AccessWindowStatic.h" |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/AccessWindowTranspose.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/Error.h" |
| 29 | #include "arm_compute/core/Helpers.h" |
| 30 | #include "arm_compute/core/ITensor.h" |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/TensorInfo.h" |
Gian Marco | 5420b28 | 2017-11-29 10:41:38 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/Utils.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | #include "arm_compute/core/Validate.h" |
| 34 | |
| 35 | #include <arm_neon.h> |
| 36 | |
| 37 | using namespace arm_compute; |
| 38 | |
| 39 | namespace arm_compute |
| 40 | { |
| 41 | class Coordinates; |
| 42 | } // namespace arm_compute |
| 43 | |
| 44 | namespace |
| 45 | { |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 46 | TensorShape 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 | |
| 57 | unsigned 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 Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 76 | Status validate_arguments(const ITensorInfo *input, const ITensorInfo *output) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 77 | { |
Anthony Barbier | eaefd00 | 2018-07-20 17:49:35 +0100 | [diff] [blame] | 78 | //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 Loganathan | 7485d5a | 2018-07-04 09:34:00 +0100 | [diff] [blame] | 79 | 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 Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 80 | 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); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 91 | return Status{}; |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 94 | std::pair<Status, Window> validate_and_configure_window(ITensorInfo *input, ITensorInfo *output) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 95 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 96 | // Note: This kernel performs 16 elements per iteration. |
| 97 | // 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 |
| 98 | // For this reason num_elems_processed_per_iteration_x is set to 1 |
| 99 | const unsigned int num_elems_processed_per_iteration_x = 1; |
| 100 | const unsigned int num_elems_processed_per_iteration_y = num_elems_processed(input->element_size()); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 101 | |
| 102 | // Configure kernel window |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 103 | Window win = calculate_max_window(*input, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 104 | |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 105 | AccessWindowRectangle input_access(input, 0, 0, num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y); |
| 106 | bool window_changed = update_window_and_padding(win, input_access); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 107 | |
| 108 | if(output->total_size() != 0) |
| 109 | { |
Giorgio Arena | e3d24ce | 2018-08-24 14:44:08 +0100 | [diff] [blame] | 110 | AccessWindowTranspose output_access(output, 0, 0, num_elems_processed_per_iteration_y, num_elems_processed_per_iteration_x); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 111 | |
| 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 Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 117 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 118 | return std::make_pair(err, win); |
| 119 | } |
| 120 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 121 | void transpose_8bit_elements(const ITensor *in, ITensor *out, const Window &window) |
| 122 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 123 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 155 | Iterator output(out, window_out); |
| 156 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 157 | // Run the NEON path if and only if the input is not a row-vector |
| 158 | if(in->info()->dimension(1) != 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 159 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 160 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 175 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 176 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 181 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 182 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 187 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 188 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 193 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 194 | // Compute destination address |
| 195 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + x * output_stride_in_bytes; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 196 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 197 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | void transpose_16bit_elements(const ITensor *in, ITensor *out, const Window &window) |
| 261 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 262 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 290 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 294 | Iterator output(out, window_out); |
| 295 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 296 | // Run the NEON path if and only if the input is not a row-vector |
| 297 | if(in->info()->dimension(1) != 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 298 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 299 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 310 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 311 | // Transpose 2x2 |
| 312 | const uint16x4x2_t k0_u16 = vtrn_u16(row0, row1); |
| 313 | const uint16x4x2_t k1_u16 = vtrn_u16(row2, row3); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 314 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 315 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 318 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 319 | // Compute destination address |
| 320 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + x * output_stride_in_bytes; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 321 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 322 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | void transpose_32bit_elements(const ITensor *in, ITensor *out, const Window &window) |
| 374 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 375 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 403 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 407 | Iterator output(out, window_out); |
| 408 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 409 | // Run the NEON path if and only if the input is not a row-vector |
| 410 | if(in->info()->dimension(1) != 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 411 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 412 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 423 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 424 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 429 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 430 | // Compute destination address |
| 431 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + x * output_stride_in_bytes; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 432 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 433 | // 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 483 | } |
| 484 | } // namespace |
| 485 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 486 | Status NETransposeKernel::validate(const ITensorInfo *input, const ITensorInfo *output) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 487 | { |
| 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 Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 491 | return Status{}; |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 492 | } |
| 493 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 494 | NETransposeKernel::NETransposeKernel() |
| 495 | : _func(nullptr), _input(nullptr), _output(nullptr) |
| 496 | { |
| 497 | } |
| 498 | |
| 499 | void NETransposeKernel::configure(const ITensor *input, ITensor *output) |
| 500 | { |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 501 | ARM_COMPUTE_ERROR_ON_NULLPTR(input, output); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 502 | |
| 503 | // Output tensor auto inizialitation if not yet initialized |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 504 | auto_init_if_empty(*output->info(), input->info()->clone()->set_tensor_shape(transposed_tensor_shape(input->info()->tensor_shape()))); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 505 | |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 506 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(input->info(), output->info())); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 507 | |
| 508 | _input = input; |
| 509 | _output = output; |
| 510 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 511 | switch(input->info()->element_size()) |
| 512 | { |
| 513 | case 1: |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 514 | _func = &transpose_8bit_elements; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 515 | break; |
| 516 | case 2: |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 517 | _func = &transpose_16bit_elements; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 518 | break; |
| 519 | case 4: |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 520 | _func = &transpose_32bit_elements; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 521 | break; |
| 522 | default: |
| 523 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 524 | break; |
| 525 | } |
| 526 | |
| 527 | // Configure kernel window |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 528 | 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 Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 531 | } |
| 532 | |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 533 | void NETransposeKernel::run(const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 534 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 535 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 536 | 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 | } |