Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #include "arm_compute/core/NEON/kernels/NEGEMMLowpReductionKernel.h" |
| 25 | |
| 26 | #include "arm_compute/core/AccessWindowStatic.h" |
| 27 | #include "arm_compute/core/Error.h" |
| 28 | #include "arm_compute/core/Helpers.h" |
| 29 | #include "arm_compute/core/ITensor.h" |
| 30 | #include "arm_compute/core/TensorInfo.h" |
| 31 | #include "arm_compute/core/Types.h" |
| 32 | #include "arm_compute/core/Utils.h" |
| 33 | #include "arm_compute/core/Validate.h" |
| 34 | #include "arm_compute/core/Window.h" |
| 35 | |
| 36 | #include <arm_neon.h> |
| 37 | #include <cstddef> |
| 38 | #include <cstdint> |
| 39 | |
| 40 | using namespace arm_compute; |
| 41 | |
| 42 | namespace arm_compute |
| 43 | { |
| 44 | class Coordinates; |
| 45 | } // namespace arm_compute |
| 46 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 47 | namespace |
| 48 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 49 | Status validate_arguments_matrix_a_reduction(const ITensorInfo *input, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 53 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 54 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 55 | } |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 56 | std::pair<Status, Window> validate_and_configure_window_matrix_a_reduction(ITensorInfo *input, ITensorInfo *output, bool is_reshaped) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 57 | { |
| 58 | const unsigned int num_elems_processed_per_iteration = is_reshaped ? 4 : 1; |
| 59 | |
| 60 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 61 | |
| 62 | AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), 16), input->dimension(1)); |
| 63 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 64 | |
| 65 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 66 | |
| 67 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 68 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 69 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 70 | return std::make_pair(err, win); |
| 71 | } |
| 72 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 73 | Status validate_arguments_matrix_b_reduction(const ITensorInfo *input, const ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 74 | { |
| 75 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(input, 1, DataType::QASYMM8); |
| 76 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(output, 1, DataType::S32); |
| 77 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 78 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 81 | std::pair<Status, Window> validate_and_configure_window_matrix_b_reduction(ITensorInfo *input, ITensorInfo *output) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 82 | { |
| 83 | constexpr unsigned int num_elems_processed_per_iteration = 16; |
| 84 | |
| 85 | // Configure kernel window |
| 86 | Window win = calculate_max_window(*output, Steps(num_elems_processed_per_iteration)); |
| 87 | |
| 88 | AccessWindowStatic input_access(input, 0, 0, ceil_to_multiple(input->dimension(0), 16), input->dimension(1)); |
| 89 | AccessWindowHorizontal output_access(output, 0, num_elems_processed_per_iteration); |
| 90 | |
| 91 | bool window_changed = update_window_and_padding(win, input_access, output_access); |
| 92 | |
| 93 | output_access.set_valid_region(win, ValidRegion(Coordinates(0, 0), output->tensor_shape())); |
| 94 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 95 | Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 96 | return std::make_pair(err, win); |
| 97 | } |
| 98 | } // namespace |
| 99 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 100 | INEGEMMLowpReductionKernel::INEGEMMLowpReductionKernel() |
| 101 | : _input(), _output(), _k(0), _is_reshaped(false) |
| 102 | { |
| 103 | } |
| 104 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 105 | void NEGEMMLowpMatrixAReductionKernel::configure(const ITensor *mtx_a, ITensor *vector_sum_row, int32_t num_mtx_a_cols, bool is_interleaved4x4) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 106 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 107 | // Perform validate step |
| 108 | ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_a, vector_sum_row); |
| 109 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_a_reduction(mtx_a->info(), vector_sum_row->info())); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 110 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 111 | _input = mtx_a; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 112 | _output = vector_sum_row; |
| 113 | _k = num_mtx_a_cols; |
| 114 | _is_reshaped = is_interleaved4x4; |
| 115 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 116 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 117 | auto win_config = validate_and_configure_window_matrix_a_reduction(_input->info(), _output->info(), _is_reshaped); |
| 118 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 119 | INEKernel::configure(win_config.second); |
| 120 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 121 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 122 | Status NEGEMMLowpMatrixAReductionKernel::validate(const ITensorInfo *mtx_a, const ITensorInfo *vector_sum_row, int32_t num_mtx_a_cols, bool is_interleaved4x4) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 123 | { |
| 124 | ARM_COMPUTE_UNUSED(num_mtx_a_cols); |
| 125 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_a_reduction(mtx_a, vector_sum_row)); |
| 126 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_a_reduction(mtx_a->clone().get(), vector_sum_row->clone().get(), is_interleaved4x4).first); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 127 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 128 | return Status{}; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void NEGEMMLowpMatrixAReductionKernel::run(const Window &window, const ThreadInfo &info) |
| 132 | { |
| 133 | ARM_COMPUTE_UNUSED(info); |
| 134 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 135 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 136 | |
| 137 | Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimY); |
| 138 | |
| 139 | Window win_input(collapsed_window); |
| 140 | win_input.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 141 | win_input.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 142 | win_input.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 143 | |
| 144 | Iterator in(_input, win_input); |
| 145 | Iterator out(_output, collapsed_window); |
| 146 | |
| 147 | if(_is_reshaped) |
| 148 | { |
| 149 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
| 150 | { |
| 151 | // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 152 | uint32x4_t sum_row = vdupq_n_u32(0); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 153 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 154 | const uint8_t *matrix_a = (in.ptr() + (id.x() / 4) * _input->info()->strides_in_bytes()[1] + id.y() * _input->info()->strides_in_bytes()[2]); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 155 | |
| 156 | #if __arm__ |
| 157 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_a)); |
| 158 | #endif /* __arm__ */ |
| 159 | |
| 160 | int i = 0; |
| 161 | // This for loop performs 4 accumulations |
| 162 | for(; i <= (_k - 4); i += 4) |
| 163 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 164 | const uint8x16_t a0_u8 = vld1q_u8(matrix_a + i * 4); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 165 | |
| 166 | // Convert U8 to U16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 167 | uint16x4x4_t a0_u16 = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 168 | { |
| 169 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 170 | vget_low_u16(vmovl_u8(vget_low_u8(a0_u8))), |
| 171 | vget_high_u16(vmovl_u8(vget_low_u8(a0_u8))), |
| 172 | vget_low_u16(vmovl_u8(vget_high_u8(a0_u8))), |
| 173 | vget_high_u16(vmovl_u8(vget_high_u8(a0_u8))) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 174 | } |
| 175 | }; |
| 176 | |
| 177 | // Accumulate to U16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 178 | a0_u16.val[0] = vadd_u16(a0_u16.val[0], a0_u16.val[1]); |
| 179 | a0_u16.val[0] = vadd_u16(a0_u16.val[0], a0_u16.val[2]); |
| 180 | a0_u16.val[0] = vadd_u16(a0_u16.val[0], a0_u16.val[3]); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 181 | |
| 182 | // Accumulate to U32 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 183 | sum_row = vaddw_u16(sum_row, a0_u16.val[0]); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // This for loop performs the leftover accumulations |
| 187 | for(; i < _k; ++i) |
| 188 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 189 | const uint8x8_t a0_u8 = vld1_u8(matrix_a + i * 4); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 190 | |
| 191 | // Convert U8 to U16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 192 | const uint16x4_t a0_u16 = vget_low_u16(vmovl_u8(a0_u8)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 193 | |
| 194 | // Accumulate to U32 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 195 | sum_row = vaddw_u16(sum_row, a0_u16); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | auto vector_sum_row = reinterpret_cast<int32_t *>(out.ptr()); |
| 199 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 200 | vst1q_s32(vector_sum_row, vreinterpretq_s32_u32(sum_row)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 201 | }, |
| 202 | in, out); |
| 203 | } |
| 204 | else // it is not reshaped |
| 205 | { |
| 206 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
| 207 | { |
| 208 | // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 209 | uint32x4_t sum_row_u32 = vdupq_n_u32(0); |
| 210 | uint32_t sum_row = 0; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 211 | |
Gian Marco | 05288a2 | 2017-11-21 10:57:50 +0000 | [diff] [blame] | 212 | const uint8_t *matrix_a = (in.ptr() + id.x() * _input->info()->strides_in_bytes()[1] + id.y() * _input->info()->strides_in_bytes()[2]); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 213 | |
| 214 | #if __arm__ |
| 215 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_a)); |
| 216 | #endif /* __arm__ */ |
| 217 | |
| 218 | int i = 0; |
| 219 | // This for loop performs 16 accumulations |
| 220 | for(; i <= (_k - 16); i += 16) |
| 221 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 222 | const uint8x16_t a0_u8 = vld1q_u8(matrix_a + i); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 223 | |
| 224 | // Partial accumulations in U16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 225 | const uint16x8_t tmp_sum0 = vaddl_u8(vget_low_u8(a0_u8), vget_high_u8(a0_u8)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 226 | |
| 227 | // Accumulate to U32 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 228 | sum_row_u32 = vaddq_u32(sum_row_u32, vpaddlq_u16(tmp_sum0)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | // This for loop performs the leftover accumulations |
| 232 | for(; i < _k; ++i) |
| 233 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 234 | sum_row += static_cast<uint32_t>(matrix_a[i]); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | #if defined(__aarch64__) |
| 238 | // Reduction operation available on 64 bit architectures only |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 239 | sum_row += vaddvq_u32(sum_row_u32); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 240 | #else // __aarch64__ |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 241 | uint32x2_t tmp = vpadd_u32(vget_high_u32(sum_row_u32), vget_low_u32(sum_row_u32)); |
| 242 | tmp = vpadd_u32(tmp, tmp); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 243 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 244 | sum_row += vget_lane_u32(tmp, 0); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 245 | #endif // __aarch64__ |
| 246 | |
| 247 | *(reinterpret_cast<int *>(out.ptr())) = static_cast<int>(sum_row); |
| 248 | }, |
| 249 | in, out); |
| 250 | } |
| 251 | } |
| 252 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 253 | void NEGEMMLowpMatrixBReductionKernel::configure(const ITensor *mtx_b, ITensor *vector_sum_col, int32_t num_mtx_b_rows, bool is_transposed1xW) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 254 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 255 | ARM_COMPUTE_ERROR_ON_NULLPTR(mtx_b, vector_sum_col); |
| 256 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments_matrix_b_reduction(mtx_b->info(), vector_sum_col->info())); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 257 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 258 | _input = mtx_b; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 259 | _output = vector_sum_col; |
| 260 | _k = num_mtx_b_rows; |
| 261 | _is_reshaped = is_transposed1xW; |
| 262 | |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 263 | // Configure kernel window |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 264 | auto win_config = validate_and_configure_window_matrix_b_reduction(_input->info(), _output->info()); |
| 265 | ARM_COMPUTE_ERROR_THROW_ON(win_config.first); |
| 266 | INEKernel::configure(win_config.second); |
| 267 | } |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 268 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 269 | Status NEGEMMLowpMatrixBReductionKernel::validate(const ITensorInfo *mtx_b, const ITensorInfo *vector_sum_col, int32_t num_mtx_b_rows, bool is_transposed1xW) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 270 | { |
| 271 | ARM_COMPUTE_UNUSED(num_mtx_b_rows); |
| 272 | ARM_COMPUTE_UNUSED(is_transposed1xW); |
| 273 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments_matrix_b_reduction(mtx_b, vector_sum_col)); |
| 274 | ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window_matrix_b_reduction(mtx_b->clone().get(), vector_sum_col->clone().get()).first); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 275 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 276 | return Status{}; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void NEGEMMLowpMatrixBReductionKernel::run(const Window &window, const ThreadInfo &info) |
| 280 | { |
| 281 | ARM_COMPUTE_UNUSED(info); |
| 282 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 283 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 284 | |
| 285 | Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimY); |
| 286 | |
| 287 | if(_is_reshaped) |
| 288 | { |
| 289 | Window win_input(collapsed_window); |
| 290 | win_input.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 291 | win_input.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 292 | win_input.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 293 | |
| 294 | Iterator in(_input, win_input); |
| 295 | Iterator out(_output, collapsed_window); |
| 296 | |
| 297 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
| 298 | { |
| 299 | // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 300 | uint32x4x4_t sum_col = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 301 | { |
| 302 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 303 | vdupq_n_u32(0), |
| 304 | vdupq_n_u32(0), |
| 305 | vdupq_n_u32(0), |
| 306 | vdupq_n_u32(0) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 307 | } |
| 308 | }; |
| 309 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 310 | const uint8_t *matrix_b = in.ptr() + (id.x() / 16) * _input->info()->strides_in_bytes()[1] + id.y() * _input->info()->strides_in_bytes()[2]; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 311 | |
| 312 | #if __arm__ |
| 313 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b)); |
| 314 | #endif /* __arm__ */ |
| 315 | |
| 316 | int i = 0; |
| 317 | for(; i < _k; ++i) |
| 318 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 319 | const uint8x16_t b0_u8 = vld1q_u8(matrix_b + i * 16); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 320 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 321 | // Convert S8 to U16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 322 | const uint16x8x2_t b0_u16 = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 323 | { |
| 324 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 325 | vmovl_u8(vget_low_u8(b0_u8)), |
| 326 | vmovl_u8(vget_high_u8(b0_u8)) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 327 | } |
| 328 | }; |
| 329 | |
| 330 | // Accumulate to U32 |
| 331 | sum_col = |
| 332 | { |
| 333 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 334 | vaddw_u16(sum_col.val[0], vget_low_u16(b0_u16.val[0])), |
| 335 | vaddw_u16(sum_col.val[1], vget_high_u16(b0_u16.val[0])), |
| 336 | vaddw_u16(sum_col.val[2], vget_low_u16(b0_u16.val[1])), |
| 337 | vaddw_u16(sum_col.val[3], vget_high_u16(b0_u16.val[1])) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 338 | } |
| 339 | }; |
| 340 | } |
| 341 | |
| 342 | auto vector_sum_col = reinterpret_cast<int32_t *>(out.ptr()); |
| 343 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 344 | vst1q_s32(vector_sum_col + 0, vreinterpretq_s32_u32(sum_col.val[0])); |
| 345 | vst1q_s32(vector_sum_col + 4, vreinterpretq_s32_u32(sum_col.val[1])); |
| 346 | vst1q_s32(vector_sum_col + 8, vreinterpretq_s32_u32(sum_col.val[2])); |
| 347 | vst1q_s32(vector_sum_col + 12, vreinterpretq_s32_u32(sum_col.val[3])); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 348 | }, |
| 349 | in, out); |
| 350 | } |
| 351 | else // it is not reshaped |
| 352 | { |
| 353 | const auto width_matrix_b = static_cast<int>(_input->info()->dimension(0)); |
| 354 | const auto in_b_stride = static_cast<int>(_input->info()->strides_in_bytes()[1]); |
| 355 | |
| 356 | // The implementation computes 16 elements per iteration |
| 357 | const int window_start_x = 16 * info.thread_id; |
| 358 | const int window_step_x = 16 * info.num_threads; |
| 359 | // Make sure (window_end_x - window_start_x) is a multiple of window_step_x |
| 360 | const int window_end_x = ceil_to_multiple(width_matrix_b - window_start_x, window_step_x) + window_start_x; |
| 361 | |
| 362 | Window win_out(collapsed_window); |
| 363 | win_out.set(Window::DimX, Window::Dimension(window_start_x, window_end_x, window_step_x)); |
| 364 | |
| 365 | Window win_in(win_out); |
| 366 | win_in.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 367 | win_in.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 368 | |
| 369 | Iterator inb(_input, win_in); |
| 370 | Iterator out(_output, win_out); |
| 371 | |
| 372 | execute_window_loop(win_out, [&](const Coordinates & id) |
| 373 | { |
| 374 | if(id.x() > width_matrix_b) |
| 375 | { |
| 376 | return; |
| 377 | } |
| 378 | |
| 379 | // Note: Since the input is unsigned char, we can safely use unsigned int for the accumulation |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 380 | uint32x4x4_t sum_col = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 381 | { |
| 382 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 383 | vdupq_n_u32(0), |
| 384 | vdupq_n_u32(0), |
| 385 | vdupq_n_u32(0), |
| 386 | vdupq_n_u32(0) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 387 | } |
| 388 | }; |
| 389 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 390 | const uint8_t *matrix_b = inb.ptr() + id.y() * _input->info()->strides_in_bytes()[2]; |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 391 | |
| 392 | #if __arm__ |
| 393 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b)); |
| 394 | asm volatile("PLD [%0, #128*4]" ::"r"(matrix_b + in_b_stride)); |
| 395 | #endif /* __arm__ */ |
| 396 | |
| 397 | int i = 0; |
| 398 | // This for loop performs 4 accumulations |
| 399 | for(; i <= (_k - 4); i += 4) |
| 400 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 401 | const uint8x16_t b0_u8 = vld1q_u8(matrix_b + 0 * in_b_stride); |
| 402 | const uint8x16_t b1_u8 = vld1q_u8(matrix_b + 1 * in_b_stride); |
| 403 | const uint8x16_t b2_u8 = vld1q_u8(matrix_b + 2 * in_b_stride); |
| 404 | const uint8x16_t b3_u8 = vld1q_u8(matrix_b + 3 * in_b_stride); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 405 | |
| 406 | #if __arm__ |
| 407 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 1 * in_b_stride)); |
| 408 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 2 * in_b_stride)); |
| 409 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 3 * in_b_stride)); |
| 410 | asm volatile("PLD [%0, #128*1]" ::"r"(matrix_b + 4 * in_b_stride)); |
| 411 | #endif /* __arm__ */ |
| 412 | |
| 413 | // Partial accumulation in u16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 414 | uint16x8x2_t tmp_sum = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 415 | { |
| 416 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 417 | vdupq_n_u16(0), |
| 418 | vdupq_n_u16(0) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 419 | } |
| 420 | }; |
| 421 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 422 | tmp_sum.val[0] = vaddw_u8(tmp_sum.val[0], vget_low_u8(b0_u8)); |
| 423 | tmp_sum.val[0] = vaddw_u8(tmp_sum.val[0], vget_low_u8(b1_u8)); |
| 424 | tmp_sum.val[0] = vaddw_u8(tmp_sum.val[0], vget_low_u8(b2_u8)); |
| 425 | tmp_sum.val[0] = vaddw_u8(tmp_sum.val[0], vget_low_u8(b3_u8)); |
| 426 | tmp_sum.val[1] = vaddw_u8(tmp_sum.val[1], vget_high_u8(b0_u8)); |
| 427 | tmp_sum.val[1] = vaddw_u8(tmp_sum.val[1], vget_high_u8(b1_u8)); |
| 428 | tmp_sum.val[1] = vaddw_u8(tmp_sum.val[1], vget_high_u8(b2_u8)); |
| 429 | tmp_sum.val[1] = vaddw_u8(tmp_sum.val[1], vget_high_u8(b3_u8)); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 430 | |
| 431 | // Accumulate to U32 |
| 432 | sum_col = |
| 433 | { |
| 434 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 435 | vaddw_u16(sum_col.val[0], vget_low_u16(tmp_sum.val[0])), |
| 436 | vaddw_u16(sum_col.val[1], vget_high_u16(tmp_sum.val[0])), |
| 437 | vaddw_u16(sum_col.val[2], vget_low_u16(tmp_sum.val[1])), |
| 438 | vaddw_u16(sum_col.val[3], vget_high_u16(tmp_sum.val[1])) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 439 | } |
| 440 | }; |
| 441 | |
| 442 | matrix_b += 4 * in_b_stride; |
| 443 | } |
| 444 | |
| 445 | // This for loop perfoms the leftover accumulations |
| 446 | for(; i < _k; ++i) |
| 447 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 448 | const uint8x16_t b0_u8 = vld1q_u8(matrix_b + 0 * in_b_stride); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 449 | |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 450 | // Convert S8 to S16 |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 451 | const uint16x8x2_t b0_u16 = |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 452 | { |
| 453 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 454 | vmovl_u8(vget_low_u8(b0_u8)), |
| 455 | vmovl_u8(vget_high_u8(b0_u8)) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 456 | } |
| 457 | }; |
| 458 | |
| 459 | // Accumulate to U32 |
| 460 | sum_col = |
| 461 | { |
| 462 | { |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 463 | vaddw_u16(sum_col.val[0], vget_low_u16(b0_u16.val[0])), |
| 464 | vaddw_u16(sum_col.val[1], vget_high_u16(b0_u16.val[0])), |
| 465 | vaddw_u16(sum_col.val[2], vget_low_u16(b0_u16.val[1])), |
| 466 | vaddw_u16(sum_col.val[3], vget_high_u16(b0_u16.val[1])) |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 467 | } |
| 468 | }; |
| 469 | |
| 470 | matrix_b += in_b_stride; |
| 471 | } |
| 472 | |
| 473 | auto vector_sum_col = reinterpret_cast<int32_t *>(out.ptr()); |
| 474 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 475 | vst1q_s32(vector_sum_col + 0, vreinterpretq_s32_u32(sum_col.val[0])); |
| 476 | vst1q_s32(vector_sum_col + 4, vreinterpretq_s32_u32(sum_col.val[1])); |
| 477 | vst1q_s32(vector_sum_col + 8, vreinterpretq_s32_u32(sum_col.val[2])); |
| 478 | vst1q_s32(vector_sum_col + 12, vreinterpretq_s32_u32(sum_col.val[3])); |
Gian Marco Iodice | ab18212 | 2017-10-09 15:05:40 +0100 | [diff] [blame] | 479 | }, |
| 480 | inb, out); |
| 481 | } |
Pablo Tello | 6ff12a0 | 2017-11-02 16:09:35 +0000 | [diff] [blame] | 482 | } |