Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2021 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 | */ |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 24 | #include "src/core/cpu/kernels/CpuTransposeKernel.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
Isabella Gottardi | d56e770 | 2018-02-28 14:29:36 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/TensorInfo.h" |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/Types.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/Validate.h" |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 32 | #include "arm_compute/core/utils/misc/ShapeCalculator.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 33 | #include "src/core/helpers/AutoConfiguration.h" |
| 34 | #include "src/core/helpers/WindowHelpers.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | |
| 36 | #include <arm_neon.h> |
| 37 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 38 | namespace arm_compute |
| 39 | { |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 40 | namespace cpu |
| 41 | { |
| 42 | namespace kernels |
| 43 | { |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 44 | namespace |
| 45 | { |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 46 | unsigned int num_elems_processed(size_t element_size) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 47 | { |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 48 | switch(element_size) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 49 | { |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 50 | case 1: |
| 51 | return 8; |
| 52 | case 2: |
| 53 | case 4: |
| 54 | return 4; |
| 55 | default: |
| 56 | break; |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 59 | ARM_COMPUTE_ERROR("Element size not supported"); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 62 | void transpose_8bit_elements(const ITensor *in, ITensor *out, const Window &window) |
| 63 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 64 | const int window_step_x = 8; |
| 65 | const int window_step_y = 8; |
| 66 | const int window_start_x = window.x().start(); |
| 67 | const int window_end_x = window.x().end(); |
| 68 | const int window_start_y = window.y().start(); |
| 69 | const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1))); |
| 70 | const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y; |
| 71 | const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1]; |
| 72 | const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1]; |
| 73 | |
| 74 | // Check if we need a left-over loop for the y dimension |
| 75 | bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0); |
| 76 | |
| 77 | Window window_in(window); |
| 78 | window_in.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 79 | if(left_over_loop_y) |
| 80 | { |
| 81 | // Check if window_end_y_multiple_of is greater than window_start_y |
| 82 | if(window_end_y_multiple_of > window_start_y) |
| 83 | { |
| 84 | window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y)); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | window_in.set(Window::DimY, Window::Dimension(0, 0, 1)); |
| 89 | } |
| 90 | } |
| 91 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 92 | Window window_out(window); |
| 93 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 94 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 95 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 96 | Iterator output(out, window_out); |
| 97 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 98 | // Run the SIMD path if and only if the input is not a row-vector |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 99 | if(in->info()->dimension(1) != 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 100 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 101 | Iterator input(in, window_in); |
| 102 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 103 | { |
| 104 | // Compute 8x8 elements per iteration |
| 105 | int x = window_start_x; |
| 106 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 107 | { |
| 108 | const uint8x8_t row0 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 0 * input_stride_in_bytes)); |
| 109 | const uint8x8_t row1 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 1 * input_stride_in_bytes)); |
| 110 | const uint8x8_t row2 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 2 * input_stride_in_bytes)); |
| 111 | const uint8x8_t row3 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 3 * input_stride_in_bytes)); |
| 112 | const uint8x8_t row4 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 4 * input_stride_in_bytes)); |
| 113 | const uint8x8_t row5 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 5 * input_stride_in_bytes)); |
| 114 | const uint8x8_t row6 = vld1_u8(reinterpret_cast<const uint8_t *>(input.ptr() + x + 6 * input_stride_in_bytes)); |
| 115 | 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] | 116 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 117 | // Transpose 2x2 |
| 118 | const uint8x8x2_t k0_u8 = vtrn_u8(row0, row1); |
| 119 | const uint8x8x2_t k1_u8 = vtrn_u8(row2, row3); |
| 120 | const uint8x8x2_t k2_u8 = vtrn_u8(row4, row5); |
| 121 | const uint8x8x2_t k3_u8 = vtrn_u8(row6, row7); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 122 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 123 | // Transpose 4x4 |
| 124 | const uint16x4x2_t k0_u16 = vtrn_u16(vreinterpret_u16_u8(k0_u8.val[0]), vreinterpret_u16_u8(k1_u8.val[0])); |
| 125 | const uint16x4x2_t k1_u16 = vtrn_u16(vreinterpret_u16_u8(k0_u8.val[1]), vreinterpret_u16_u8(k1_u8.val[1])); |
| 126 | const uint16x4x2_t k2_u16 = vtrn_u16(vreinterpret_u16_u8(k2_u8.val[0]), vreinterpret_u16_u8(k3_u8.val[0])); |
| 127 | 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] | 128 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 129 | // Transpose 8x8 |
| 130 | const uint32x2x2_t k0_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[0]), vreinterpret_u32_u16(k2_u16.val[0])); |
| 131 | const uint32x2x2_t k1_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[1]), vreinterpret_u32_u16(k2_u16.val[1])); |
| 132 | const uint32x2x2_t k2_u32 = vtrn_u32(vreinterpret_u32_u16(k1_u16.val[0]), vreinterpret_u32_u16(k3_u16.val[0])); |
| 133 | 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] | 134 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 135 | // Compute destination address |
| 136 | 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] | 137 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 138 | 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]))); |
| 139 | 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]))); |
| 140 | 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]))); |
| 141 | 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]))); |
| 142 | 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]))); |
| 143 | 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]))); |
| 144 | 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]))); |
| 145 | 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]))); |
| 146 | } |
| 147 | |
| 148 | // Compute left-over elements along the x dimension (1x8) |
| 149 | for(; x < window_end_x; ++x) |
| 150 | { |
| 151 | const uint8_t val0 = *(input.ptr() + x + 0 * input_stride_in_bytes); |
| 152 | const uint8_t val1 = *(input.ptr() + x + 1 * input_stride_in_bytes); |
| 153 | const uint8_t val2 = *(input.ptr() + x + 2 * input_stride_in_bytes); |
| 154 | const uint8_t val3 = *(input.ptr() + x + 3 * input_stride_in_bytes); |
| 155 | const uint8_t val4 = *(input.ptr() + x + 4 * input_stride_in_bytes); |
| 156 | const uint8_t val5 = *(input.ptr() + x + 5 * input_stride_in_bytes); |
| 157 | const uint8_t val6 = *(input.ptr() + x + 6 * input_stride_in_bytes); |
| 158 | const uint8_t val7 = *(input.ptr() + x + 7 * input_stride_in_bytes); |
| 159 | |
| 160 | uint8x8_t result = vdup_n_u8(0); |
| 161 | result = vset_lane_u8(val0, result, 0); |
| 162 | result = vset_lane_u8(val1, result, 1); |
| 163 | result = vset_lane_u8(val2, result, 2); |
| 164 | result = vset_lane_u8(val3, result, 3); |
| 165 | result = vset_lane_u8(val4, result, 4); |
| 166 | result = vset_lane_u8(val5, result, 5); |
| 167 | result = vset_lane_u8(val6, result, 6); |
| 168 | result = vset_lane_u8(val7, result, 7); |
| 169 | |
| 170 | // Compute destination address |
| 171 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + x * output_stride_in_bytes; |
| 172 | |
| 173 | vst1_u8(output.ptr() + dst_offset_in_bytes, result); |
| 174 | } |
| 175 | }, |
| 176 | input, output); |
| 177 | } |
| 178 | |
| 179 | if(left_over_loop_y) |
| 180 | { |
| 181 | window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1)); |
| 182 | window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1)); |
| 183 | |
| 184 | Iterator input(in, window_in); |
| 185 | Iterator output(out, window_out); |
| 186 | |
| 187 | // Compute left-over elements along the y dimension (1x1) |
| 188 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 189 | { |
| 190 | const uint8_t val0 = *input.ptr(); |
| 191 | |
| 192 | // Compute destination address |
| 193 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint8_t) + id.x() * output_stride_in_bytes; |
| 194 | |
| 195 | *(output.ptr() + dst_offset_in_bytes) = val0; |
| 196 | }, |
| 197 | input, output); |
| 198 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void transpose_16bit_elements(const ITensor *in, ITensor *out, const Window &window) |
| 202 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 203 | const int window_step_x = 4; |
| 204 | const int window_step_y = 4; |
| 205 | const int window_start_x = window.x().start(); |
| 206 | const int window_end_x = window.x().end(); |
| 207 | const int window_start_y = window.y().start(); |
| 208 | const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1))); |
| 209 | const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y; |
| 210 | const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1]; |
| 211 | const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1]; |
| 212 | |
| 213 | // Check if we need a left-over loop for the y dimension |
| 214 | bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0); |
| 215 | |
| 216 | Window window_in(window); |
| 217 | window_in.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 218 | if(left_over_loop_y) |
| 219 | { |
| 220 | // Check if window_end_y_multiple_of is greater than window_start_y |
| 221 | if(window_end_y_multiple_of > window_start_y) |
| 222 | { |
| 223 | window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y)); |
| 224 | } |
| 225 | else |
| 226 | { |
| 227 | window_in.set(Window::DimY, Window::Dimension(0, 0, 1)); |
| 228 | } |
| 229 | } |
| 230 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | Window window_out(window); |
| 232 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 233 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 234 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 235 | Iterator output(out, window_out); |
| 236 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 237 | // Run the SIMD path if and only if the input is not a row-vector |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 238 | if(in->info()->dimension(1) != 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 239 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 240 | Iterator input(in, window_in); |
| 241 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 242 | { |
| 243 | // Compute 4x4 elements per iteration |
| 244 | int x = window_start_x; |
| 245 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 246 | { |
| 247 | const uint16x4_t row0 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 0 * input_stride_in_bytes) + x); |
| 248 | const uint16x4_t row1 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 1 * input_stride_in_bytes) + x); |
| 249 | const uint16x4_t row2 = vld1_u16(reinterpret_cast<const uint16_t *>(input.ptr() + 2 * input_stride_in_bytes) + x); |
| 250 | 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] | 251 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 252 | // Transpose 2x2 |
| 253 | const uint16x4x2_t k0_u16 = vtrn_u16(row0, row1); |
| 254 | const uint16x4x2_t k1_u16 = vtrn_u16(row2, row3); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 256 | // Transpose 4x4 |
| 257 | const uint32x2x2_t k0_u32 = vtrn_u32(vreinterpret_u32_u16(k0_u16.val[0]), vreinterpret_u32_u16(k1_u16.val[0])); |
| 258 | 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] | 259 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 260 | // Compute destination address |
| 261 | 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] | 262 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 263 | vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 0 * output_stride_in_bytes), vreinterpret_u16_u32(k0_u32.val[0])); |
| 264 | vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 1 * output_stride_in_bytes), vreinterpret_u16_u32(k1_u32.val[0])); |
| 265 | vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 2 * output_stride_in_bytes), vreinterpret_u16_u32(k0_u32.val[1])); |
| 266 | vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes + 3 * output_stride_in_bytes), vreinterpret_u16_u32(k1_u32.val[1])); |
| 267 | } |
| 268 | |
| 269 | // Compute left-over elements (1x4) |
| 270 | for(; x < window_end_x; ++x) |
| 271 | { |
| 272 | const uint16_t val0 = *(reinterpret_cast<uint16_t *>(input.ptr() + 0 * input_stride_in_bytes) + x); |
| 273 | const uint16_t val1 = *(reinterpret_cast<uint16_t *>(input.ptr() + 1 * input_stride_in_bytes) + x); |
| 274 | const uint16_t val2 = *(reinterpret_cast<uint16_t *>(input.ptr() + 2 * input_stride_in_bytes) + x); |
| 275 | const uint16_t val3 = *(reinterpret_cast<uint16_t *>(input.ptr() + 3 * input_stride_in_bytes) + x); |
| 276 | |
| 277 | uint16x4_t result = vdup_n_u16(0); |
| 278 | result = vset_lane_u16(val0, result, 0); |
| 279 | result = vset_lane_u16(val1, result, 1); |
| 280 | result = vset_lane_u16(val2, result, 2); |
| 281 | result = vset_lane_u16(val3, result, 3); |
| 282 | |
| 283 | // Compute destination address |
| 284 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + x * output_stride_in_bytes; |
| 285 | |
| 286 | vst1_u16(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes), result); |
| 287 | } |
| 288 | }, |
| 289 | input, output); |
| 290 | } |
| 291 | |
| 292 | if(left_over_loop_y) |
| 293 | { |
| 294 | window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1)); |
| 295 | window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1)); |
| 296 | |
| 297 | Iterator input(in, window_in); |
| 298 | Iterator output(out, window_out); |
| 299 | |
| 300 | // Compute left-over elements along the y dimension (1x1) |
| 301 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 302 | { |
| 303 | const uint16_t val0 = *(reinterpret_cast<uint16_t *>(input.ptr())); |
| 304 | |
| 305 | // Compute destination address |
| 306 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint16_t) + id.x() * output_stride_in_bytes; |
| 307 | |
| 308 | *(reinterpret_cast<uint16_t *>(output.ptr() + dst_offset_in_bytes)) = val0; |
| 309 | }, |
| 310 | input, output); |
| 311 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | void transpose_32bit_elements(const ITensor *in, ITensor *out, const Window &window) |
| 315 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 316 | const int window_step_x = 4; |
| 317 | const int window_step_y = 4; |
| 318 | const int window_start_x = window.x().start(); |
| 319 | const int window_end_x = window.x().end(); |
| 320 | const int window_start_y = window.y().start(); |
| 321 | const int window_end_y = std::min(window.y().end(), static_cast<int>(in->info()->dimension(1))); |
| 322 | const int window_end_y_multiple_of = ((window_end_y - window_start_y) / window_step_y) * window_step_y; |
| 323 | const size_t input_stride_in_bytes = in->info()->strides_in_bytes()[1]; |
| 324 | const size_t output_stride_in_bytes = out->info()->strides_in_bytes()[1]; |
| 325 | |
| 326 | // Check if we need a left-over loop for the y dimension |
| 327 | bool left_over_loop_y = (((window_end_y - window_start_y) % window_step_y) != 0); |
| 328 | |
| 329 | Window window_in(window); |
| 330 | window_in.set(Window::DimX, Window::Dimension(0, 1, 1)); |
| 331 | if(left_over_loop_y) |
| 332 | { |
| 333 | // Check if window_end_y_multiple_of is greater than window_start_y |
| 334 | if(window_end_y_multiple_of > window_start_y) |
| 335 | { |
| 336 | window_in.set(Window::DimY, Window::Dimension(window_start_y, window_end_y_multiple_of, window_step_y)); |
| 337 | } |
| 338 | else |
| 339 | { |
| 340 | window_in.set(Window::DimY, Window::Dimension(0, 0, 1)); |
| 341 | } |
| 342 | } |
| 343 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 344 | Window window_out(window); |
| 345 | window_out.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 346 | window_out.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 347 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | Iterator output(out, window_out); |
| 349 | |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 350 | // Run the SIMD path if and only if the input is not a row-vector |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 351 | if(in->info()->dimension(1) != 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 352 | { |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 353 | Iterator input(in, window_in); |
| 354 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 355 | { |
| 356 | // Compute 4x4 elements per iteration |
| 357 | int x = window_start_x; |
| 358 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
| 359 | { |
| 360 | const uint32x4_t row0 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 0 * input_stride_in_bytes) + x); |
| 361 | const uint32x4_t row1 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 1 * input_stride_in_bytes) + x); |
| 362 | const uint32x4_t row2 = vld1q_u32(reinterpret_cast<const uint32_t *>(input.ptr() + 2 * input_stride_in_bytes) + x); |
| 363 | 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] | 364 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 365 | // Transpose 2x2 |
| 366 | const uint32x2x2_t k0_u32 = vtrn_u32(vget_low_u32(row0), vget_low_u32(row1)); |
| 367 | const uint32x2x2_t k1_u32 = vtrn_u32(vget_high_u32(row2), vget_high_u32(row3)); |
| 368 | const uint32x2x2_t k2_u32 = vtrn_u32(vget_high_u32(row0), vget_high_u32(row1)); |
| 369 | 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] | 370 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 371 | // Compute destination address |
| 372 | 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] | 373 | |
Gian Marco | b42d53c | 2017-12-07 10:09:07 +0000 | [diff] [blame] | 374 | // Swap block 01 with block 10 and store |
| 375 | 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])); |
| 376 | 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])); |
| 377 | 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])); |
| 378 | 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])); |
| 379 | } |
| 380 | |
| 381 | // Compute left-over elements (1x4) |
| 382 | for(; x < window_end_x; ++x) |
| 383 | { |
| 384 | const uint32_t val0 = *(reinterpret_cast<uint32_t *>(input.ptr() + 0 * input_stride_in_bytes) + x); |
| 385 | const uint32_t val1 = *(reinterpret_cast<uint32_t *>(input.ptr() + 1 * input_stride_in_bytes) + x); |
| 386 | const uint32_t val2 = *(reinterpret_cast<uint32_t *>(input.ptr() + 2 * input_stride_in_bytes) + x); |
| 387 | const uint32_t val3 = *(reinterpret_cast<uint32_t *>(input.ptr() + 3 * input_stride_in_bytes) + x); |
| 388 | |
| 389 | uint32x4_t result = vdupq_n_u32(0); |
| 390 | result = vsetq_lane_u32(val0, result, 0); |
| 391 | result = vsetq_lane_u32(val1, result, 1); |
| 392 | result = vsetq_lane_u32(val2, result, 2); |
| 393 | result = vsetq_lane_u32(val3, result, 3); |
| 394 | |
| 395 | // Compute destination address |
| 396 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + x * output_stride_in_bytes; |
| 397 | |
| 398 | vst1q_u32(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes), result); |
| 399 | } |
| 400 | }, |
| 401 | input, output); |
| 402 | } |
| 403 | |
| 404 | if(left_over_loop_y) |
| 405 | { |
| 406 | window_in.set(Window::DimX, Window::Dimension(window.x().start(), window.x().end(), 1)); |
| 407 | window_in.set(Window::DimY, Window::Dimension(window_end_y_multiple_of, window_end_y, 1)); |
| 408 | |
| 409 | Iterator input(in, window_in); |
| 410 | Iterator output(out, window_out); |
| 411 | |
| 412 | // Compute left-over elements along the y dimension (1x1) |
| 413 | execute_window_loop(window_in, [&](const Coordinates & id) |
| 414 | { |
| 415 | const uint32_t val0 = *(reinterpret_cast<uint32_t *>(input.ptr())); |
| 416 | |
| 417 | // Compute destination address |
| 418 | const size_t dst_offset_in_bytes = id.y() * sizeof(uint32_t) + id.x() * output_stride_in_bytes; |
| 419 | |
| 420 | *(reinterpret_cast<uint32_t *>(output.ptr() + dst_offset_in_bytes)) = val0; |
| 421 | }, |
| 422 | input, output); |
| 423 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 424 | } |
| 425 | } // namespace |
| 426 | |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 427 | void CpuTransposeKernel::configure(const ITensorInfo *src, ITensorInfo *dst) |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 428 | { |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 429 | ARM_COMPUTE_ERROR_ON_NULLPTR(src, dst); |
Gian Marco | 7c435f2 | 2017-12-05 16:17:23 +0000 | [diff] [blame] | 430 | |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 431 | // Destination auto inizialitation if not yet initialized |
| 432 | const TensorShape dst_shape = misc::shape_calculator::compute_transposed_shape(*src); |
| 433 | auto_init_if_empty(*dst, src->clone()->set_tensor_shape(dst_shape)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 434 | |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 435 | // Perform validation step |
| 436 | ARM_COMPUTE_ERROR_THROW_ON(validate(src, dst)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 437 | |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 438 | // Note: This kernel performs 16 elements per iteration. |
| 439 | // 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 |
| 440 | // For this reason num_elems_processed_per_iteration_x is set to 1 |
| 441 | const unsigned int num_elems_processed_per_iteration_x = 1; |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 442 | const unsigned int num_elems_processed_per_iteration_y = num_elems_processed(src->element_size()); |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 443 | |
| 444 | // Configure kernel window |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 445 | Window win = calculate_max_window(*src, Steps(num_elems_processed_per_iteration_x, num_elems_processed_per_iteration_y)); |
Michalis Spyrou | 0b1452d | 2020-02-27 16:20:19 +0000 | [diff] [blame] | 446 | |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 447 | // The CpuTranspose doesn't need padding so update_window_and_padding() can be skipped |
| 448 | Coordinates coord; |
| 449 | coord.set_num_dimensions(dst->num_dimensions()); |
| 450 | dst->set_valid_region(ValidRegion(coord, dst->tensor_shape())); |
| 451 | |
| 452 | ICpuKernel::configure(win); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 453 | } |
| 454 | |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 455 | Status CpuTransposeKernel::validate(const ITensorInfo *src, const ITensorInfo *dst) |
| 456 | { |
| 457 | ARM_COMPUTE_RETURN_ERROR_ON_NULLPTR(src); |
Michele Di Giorgio | 33f41fa | 2021-03-09 14:09:08 +0000 | [diff] [blame] | 458 | //Note: ARM_COMPUTE_RETURN_ERROR_ON_CPU_F16_UNSUPPORTED(input) is not needed here as this kernel doesn't use CPU FP16 instructions. |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 459 | ARM_COMPUTE_RETURN_ERROR_ON(src->data_type() == DataType::UNKNOWN); |
| 460 | |
| 461 | // Error if input is not 8 bit, 16bit or 32bit |
| 462 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(src->element_size() != 1 && src->element_size() != 2 && src->element_size() != 4, |
| 463 | "Element size not supported"); |
| 464 | |
| 465 | // Validate configured destination |
| 466 | if(dst->total_size() != 0) |
| 467 | { |
| 468 | const TensorShape dst_shape = misc::shape_calculator::compute_transposed_shape(*src); |
| 469 | |
| 470 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DIMENSIONS(dst->tensor_shape(), dst_shape); |
| 471 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_QUANTIZATION_INFO(src, dst); |
| 472 | ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_DATA_TYPES(src, dst); |
| 473 | } |
| 474 | |
| 475 | return Status{}; |
| 476 | } |
| 477 | |
| 478 | void CpuTransposeKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 479 | { |
Moritz Pflanzer | c186b57 | 2017-09-07 09:48:04 +0100 | [diff] [blame] | 480 | ARM_COMPUTE_UNUSED(info); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 481 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 482 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 483 | |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 484 | const auto src = tensors.get_const_tensor(TensorType::ACL_SRC); |
| 485 | auto dst = tensors.get_tensor(TensorType::ACL_DST); |
| 486 | |
| 487 | switch(src->info()->element_size()) |
| 488 | { |
| 489 | case 1: |
| 490 | transpose_8bit_elements(src, dst, window); |
| 491 | break; |
| 492 | case 2: |
| 493 | transpose_16bit_elements(src, dst, window); |
| 494 | break; |
| 495 | case 4: |
| 496 | transpose_32bit_elements(src, dst, window); |
| 497 | break; |
| 498 | default: |
| 499 | ARM_COMPUTE_ERROR("Element size not supported"); |
| 500 | break; |
| 501 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 502 | } |
Teresa Charlin | d1dc09c | 2021-03-04 15:24:45 +0000 | [diff] [blame] | 503 | |
| 504 | const char *CpuTransposeKernel::name() const |
| 505 | { |
| 506 | return "CpuTransposeKernel"; |
| 507 | } |
| 508 | } // namespace kernels |
| 509 | } // namespace cpu |
| 510 | } // namespace arm_compute |