Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 1 | /* |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2021 Arm Limited. |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [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 | */ |
Michalis Spyrou | ebcebf1 | 2020-10-21 00:04:14 +0100 | [diff] [blame] | 24 | #include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h" |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 25 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 26 | #include "arm_compute/core/Error.h" |
| 27 | #include "arm_compute/core/Helpers.h" |
| 28 | #include "arm_compute/core/ITensor.h" |
| 29 | #include "arm_compute/core/TensorInfo.h" |
| 30 | #include "arm_compute/core/Types.h" |
| 31 | #include "arm_compute/core/Utils.h" |
| 32 | #include "arm_compute/core/Validate.h" |
| 33 | #include "arm_compute/core/Window.h" |
Sang-Hoon Park | 68dd25f | 2020-10-19 16:00:11 +0100 | [diff] [blame] | 34 | #include "src/core/helpers/AutoConfiguration.h" |
| 35 | #include "src/core/helpers/WindowHelpers.h" |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 36 | |
| 37 | #include <arm_neon.h> |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 38 | |
| 39 | namespace arm_compute |
| 40 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 41 | namespace |
| 42 | { |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 43 | Status validate_arguments(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, |
| 44 | int32_t a_offset, int32_t b_offset) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 45 | { |
| 46 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mm_result, 1, DataType::S32); |
| 47 | |
| 48 | // If a_offset == 0, vector_sum_col can be a nullptr |
| 49 | if(a_offset != 0) |
| 50 | { |
| 51 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32); |
| 52 | ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != mm_result->dimension(0)); |
| 53 | } |
| 54 | |
| 55 | // If b_offset == 0, vector_sum_row can be a nullptr |
| 56 | if(b_offset != 0) |
| 57 | { |
| 58 | ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 59 | |
| 60 | // Check if input is a 3D reinterpretation |
| 61 | const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x(); |
| 62 | |
| 63 | // Validate input |
| 64 | ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2))); |
| 65 | ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != mm_result->dimension(1)); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 66 | |
Georgios Pinitas | 4062680 | 2017-12-08 19:02:45 +0000 | [diff] [blame] | 67 | TensorShape output_shape = mm_result->tensor_shape(); |
| 68 | if(output_shape.num_dimensions() > 1) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 69 | { |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 70 | const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2; |
| 71 | |
Georgios Pinitas | 4062680 | 2017-12-08 19:02:45 +0000 | [diff] [blame] | 72 | TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape(); |
| 73 | vector_sum_row_shape.collapse_from(1); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 74 | output_shape.collapse_from(output_batch_idx); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 75 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 76 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[output_batch_idx], |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 77 | "mm_result tensor must have the same number of batches of output tensor"); |
Georgios Pinitas | 4062680 | 2017-12-08 19:02:45 +0000 | [diff] [blame] | 78 | |
| 79 | if(a_offset != 0) |
| 80 | { |
| 81 | TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape(); |
| 82 | vector_sum_col_shape.collapse_from(1); |
| 83 | |
Georgios Pinitas | 358ca20 | 2017-12-07 16:47:52 +0000 | [diff] [blame] | 84 | ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1], |
| 85 | "vector_sum_col tensor must have the same number of batches of vector_sum_row_shape or the number of batches must be set to 1"); |
Georgios Pinitas | 4062680 | 2017-12-08 19:02:45 +0000 | [diff] [blame] | 86 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 90 | return Status{}; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 93 | void run_offset_contribution(const Window &window, |
| 94 | ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row, |
Michalis Spyrou | 0028d7c | 2020-06-22 13:45:17 +0100 | [diff] [blame] | 95 | int32_t a_offset, int32_t b_offset, int32_t k_offset, bool slide_vector_sum_col, bool is_gemm3d) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 96 | { |
| 97 | Window collapsed_window = window.collapse_if_possible(window, Window::DimZ); |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 98 | collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1)); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 99 | |
| 100 | const int height_input = is_gemm3d ? mm_result->info()->dimension(1) : 0; |
| 101 | const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1; |
| 102 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 103 | const int window_start_x = window.x().start(); |
| 104 | const int window_end_x = window.x().end(); |
| 105 | const int window_step_x = 16; |
| 106 | |
| 107 | Iterator mm_result_it(mm_result, collapsed_window); |
| 108 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 109 | if((a_offset != 0) && (b_offset != 0) && (vector_sum_col != nullptr) && (vector_sum_row != nullptr)) // true, true |
| 110 | { |
| 111 | // Set window for vector_sum_col |
| 112 | Window win_vector_sum_col(collapsed_window); |
| 113 | win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 114 | win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 115 | |
| 116 | // Set window for vector_sum_row |
| 117 | Window win_vector_sum_row(collapsed_window); |
| 118 | win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 119 | win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 120 | win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 121 | |
| 122 | Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col); |
| 123 | Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 124 | |
| 125 | const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y(); |
| 126 | |
| 127 | // Offset in case vector_sum_col is batched |
| 128 | const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0; |
| 129 | |
| 130 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
| 131 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 132 | const int batch_id = id.z() / depth_input; |
| 133 | auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset); |
| 134 | auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr()); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 135 | |
| 136 | // Compute the leftover term due to b_offset. |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 137 | int32_t b_offset_term_s32 = *(reinterpret_cast<const int32_t *>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y) + id.y() + (id.z() % depth_input) * height_input); |
| 138 | b_offset_term_s32 *= b_offset; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 139 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 140 | const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32); |
| 141 | |
| 142 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 143 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 144 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 145 | // Compute the leftover term due to a_offset. |
| 146 | int32x4x4_t a_offset_term_s32 = |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 147 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 148 | { |
| 149 | vld1q_s32(vector_sum_col_ptr + x + 0), |
| 150 | vld1q_s32(vector_sum_col_ptr + x + 4), |
| 151 | vld1q_s32(vector_sum_col_ptr + x + 8), |
| 152 | vld1q_s32(vector_sum_col_ptr + x + 12) |
| 153 | } |
| 154 | }; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 155 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 156 | a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset); |
| 157 | a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset); |
| 158 | a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset); |
| 159 | a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 160 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 161 | // Add a_offset_term_s32 and b_offset_term_s32 |
| 162 | int32x4x4_t offset_term_s32 = |
| 163 | { |
| 164 | { |
| 165 | vdupq_n_s32(k_offset), |
| 166 | vdupq_n_s32(k_offset), |
| 167 | vdupq_n_s32(k_offset), |
| 168 | vdupq_n_s32(k_offset) |
| 169 | } |
| 170 | }; |
| 171 | |
| 172 | offset_term_s32.val[0] = vaddq_s32(offset_term_s32.val[0], vaddq_s32(a_offset_term_s32.val[0], b_offset_term_s32_vec)); |
| 173 | offset_term_s32.val[1] = vaddq_s32(offset_term_s32.val[1], vaddq_s32(a_offset_term_s32.val[1], b_offset_term_s32_vec)); |
| 174 | offset_term_s32.val[2] = vaddq_s32(offset_term_s32.val[2], vaddq_s32(a_offset_term_s32.val[2], b_offset_term_s32_vec)); |
| 175 | offset_term_s32.val[3] = vaddq_s32(offset_term_s32.val[3], vaddq_s32(a_offset_term_s32.val[3], b_offset_term_s32_vec)); |
| 176 | |
| 177 | int32x4x4_t in_s32 = |
| 178 | { |
| 179 | { |
| 180 | vld1q_s32(mm_result_ptr + x + 0), |
| 181 | vld1q_s32(mm_result_ptr + x + 4), |
| 182 | vld1q_s32(mm_result_ptr + x + 8), |
| 183 | vld1q_s32(mm_result_ptr + x + 12) |
| 184 | } |
| 185 | }; |
| 186 | |
| 187 | // Add the offset terms to GEMM's result |
| 188 | in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]); |
| 189 | in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]); |
| 190 | in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]); |
| 191 | in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]); |
| 192 | |
| 193 | // Store the result with the offset contribution |
| 194 | vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]); |
| 195 | vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]); |
| 196 | vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]); |
| 197 | vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]); |
| 198 | } |
| 199 | |
| 200 | // Left-overs loop |
| 201 | for(; x < window_end_x; ++x) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 202 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 203 | // Compute the leftover term due to a_offset. |
| 204 | int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 205 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 206 | a_offset_term_s32 *= a_offset; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 207 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 208 | // Add the offset terms to GEMM's result |
| 209 | // Store the result with the offset contribution |
| 210 | mm_result_ptr[x] += k_offset + a_offset_term_s32 + b_offset_term_s32; |
| 211 | } |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 212 | }, |
| 213 | vector_sum_col_it, vector_sum_row_it, mm_result_it); |
| 214 | } |
| 215 | else if((a_offset == 0) && (b_offset != 0) && (vector_sum_row != nullptr)) // false, true |
| 216 | { |
| 217 | ARM_COMPUTE_ERROR_ON_NULLPTR(vector_sum_row); |
| 218 | |
| 219 | // Set window for vector_sum_row |
| 220 | Window win_vector_sum_row(collapsed_window); |
| 221 | win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0)); |
| 222 | win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 223 | win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 224 | |
| 225 | Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 226 | |
| 227 | const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y(); |
| 228 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 229 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 230 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 231 | const int batch_id = id.z() / depth_input; |
| 232 | auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr()); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 233 | |
| 234 | // Compute the leftover term due to b_offset. |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 235 | int32_t b_offset_term_s32 = *(reinterpret_cast<const int32_t *>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y) + id.y() + (id.z() % depth_input) * height_input); |
| 236 | b_offset_term_s32 *= b_offset; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 237 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 238 | const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32); |
| 239 | |
| 240 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 241 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 242 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 243 | int32x4x4_t in_s32 = |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 244 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 245 | { |
| 246 | vld1q_s32(mm_result_ptr + x + 0), |
| 247 | vld1q_s32(mm_result_ptr + x + 4), |
| 248 | vld1q_s32(mm_result_ptr + x + 8), |
| 249 | vld1q_s32(mm_result_ptr + x + 12) |
| 250 | } |
| 251 | }; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 252 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 253 | // Add the offset terms to GEMM's result |
| 254 | in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32_vec); |
| 255 | in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32_vec); |
| 256 | in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32_vec); |
| 257 | in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32_vec); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 258 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 259 | // Store the result with the offset contribution |
| 260 | vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]); |
| 261 | vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]); |
| 262 | vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]); |
| 263 | vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]); |
| 264 | } |
| 265 | |
| 266 | // Left-overs loop |
| 267 | for(; x < window_end_x; ++x) |
| 268 | { |
| 269 | // Add the offset terms to GEMM's result |
| 270 | // Store the result with the offset contribution |
| 271 | mm_result_ptr[x] += b_offset_term_s32; |
| 272 | } |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 273 | }, |
| 274 | vector_sum_row_it, mm_result_it); |
| 275 | } |
| 276 | else if((a_offset != 0) && (b_offset == 0) && (vector_sum_col != nullptr)) // true, false |
| 277 | { |
| 278 | // Set window for vector_sum_col |
| 279 | Window win_vector_sum_col(collapsed_window); |
| 280 | win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0)); |
| 281 | win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0)); |
| 282 | |
| 283 | Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 284 | |
| 285 | // Offset in case vector_sum_col is batched |
| 286 | const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0; |
| 287 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 288 | execute_window_loop(collapsed_window, [&](const Coordinates & id) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 289 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 290 | const int batch_id = id.z() / depth_input; |
| 291 | auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset); |
| 292 | auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr()); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 293 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 294 | int x = window_start_x; |
Michalis Spyrou | c226853 | 2020-10-09 11:52:10 +0100 | [diff] [blame] | 295 | for(; x <= (window_end_x - window_step_x); x += window_step_x) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 296 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 297 | // Compute the leftover term due to a_offset. |
| 298 | int32x4x4_t a_offset_term_s32 = |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 299 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 300 | { |
| 301 | vld1q_s32(vector_sum_col_ptr + x + 0), |
| 302 | vld1q_s32(vector_sum_col_ptr + x + 4), |
| 303 | vld1q_s32(vector_sum_col_ptr + x + 8), |
| 304 | vld1q_s32(vector_sum_col_ptr + x + 12) |
| 305 | } |
| 306 | }; |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 307 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 308 | a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset); |
| 309 | a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset); |
| 310 | a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset); |
| 311 | a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 312 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 313 | int32x4x4_t in_s32 = |
| 314 | { |
| 315 | { |
| 316 | vld1q_s32(mm_result_ptr + x + 0), |
| 317 | vld1q_s32(mm_result_ptr + x + 4), |
| 318 | vld1q_s32(mm_result_ptr + x + 8), |
| 319 | vld1q_s32(mm_result_ptr + x + 12) |
| 320 | } |
| 321 | }; |
| 322 | |
| 323 | // Add the offset terms to GEMM's result |
| 324 | in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]); |
| 325 | in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]); |
| 326 | in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]); |
| 327 | in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]); |
| 328 | |
| 329 | // Store the result with the offset contribution |
| 330 | vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]); |
| 331 | vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]); |
| 332 | vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]); |
| 333 | vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]); |
| 334 | } |
| 335 | |
| 336 | // Left-overs loop |
| 337 | for(; x < window_end_x; ++x) |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 338 | { |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 339 | // Compute the leftover term due to a_offset. |
| 340 | const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x); |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 341 | |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 342 | // Add the offset terms to GEMM's result |
| 343 | // Store the result with the offset contribution |
| 344 | mm_result_ptr[x] += a_offset_term_s32 * a_offset; |
| 345 | } |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 346 | }, |
| 347 | vector_sum_col_it, mm_result_it); |
| 348 | } |
| 349 | else // false, false |
| 350 | { |
| 351 | // No offset contribution from matrix A and matrix B |
| 352 | return; |
| 353 | } |
| 354 | } |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 355 | } // namespace |
| 356 | |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 357 | NEGEMMLowpOffsetContributionKernel::NEGEMMLowpOffsetContributionKernel() |
| 358 | : _vector_sum_col(nullptr), _vector_sum_row(nullptr), _mm_result(nullptr), _a_offset(0), _b_offset(0), _k_offset(0), _slide_vector_sum_col(true) |
| 359 | { |
| 360 | } |
| 361 | |
| 362 | void NEGEMMLowpOffsetContributionKernel::configure(ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row, int32_t k, int32_t a_offset, int32_t b_offset) |
| 363 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 364 | // Perform validate step |
| 365 | ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result); |
| 366 | ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result->info(), |
| 367 | vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT |
| 368 | vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT |
| 369 | a_offset, b_offset)); // NOLINT |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 370 | |
| 371 | _vector_sum_col = vector_sum_col; |
| 372 | _vector_sum_row = vector_sum_row; |
| 373 | _mm_result = mm_result; |
| 374 | _a_offset = a_offset; |
| 375 | _b_offset = b_offset; |
| 376 | _k_offset = a_offset * b_offset * k; |
| 377 | |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 378 | // If a_offset == 0, vector_sum_col can be a nullptr |
| 379 | if(a_offset != 0) |
| 380 | { |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 381 | // Check if vector_sum_col_shape should be slidden or not |
| 382 | // Don't slide vector_sum_col_shape along the y dimension if vector_sum_col_shape has just 1 dimension and vector_sum_row_shape more than 1 |
| 383 | // This scenario can happen when the the matrix multiplication is used to perform a convolution operation |
Isabella Gottardi | e6630e4 | 2018-01-18 15:50:39 +0000 | [diff] [blame] | 384 | _slide_vector_sum_col = vector_sum_col->info()->tensor_shape().num_dimensions() > 1; |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 385 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 386 | |
| 387 | // Configure kernel window |
SiCongLi | b88272e | 2021-02-24 15:40:57 +0000 | [diff] [blame] | 388 | Window win = calculate_max_window(*mm_result->info(), Steps()); |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 389 | INEKernel::configure(win); |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 390 | } |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 391 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 392 | Status NEGEMMLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, |
| 393 | int32_t a_offset, int32_t b_offset) |
Georgios Pinitas | a3b1b46 | 2017-11-16 19:24:39 +0000 | [diff] [blame] | 394 | { |
| 395 | ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset)); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 396 | |
Georgios Pinitas | 631c41a | 2017-12-06 11:53:03 +0000 | [diff] [blame] | 397 | return Status{}; |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | void NEGEMMLowpOffsetContributionKernel::run(const Window &window, const ThreadInfo &info) |
| 401 | { |
| 402 | ARM_COMPUTE_UNUSED(info); |
| 403 | ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this); |
| 404 | ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window); |
| 405 | |
Georgios Pinitas | bb081ca | 2018-11-08 10:22:01 +0000 | [diff] [blame] | 406 | // Check if input is a 3D reinterpretation |
| 407 | const bool reinterpret_as_3d = _vector_sum_row != nullptr |
| 408 | && _mm_result->info()->num_dimensions() > 1 |
| 409 | && _mm_result->info()->tensor_shape().y() != _vector_sum_row->info()->tensor_shape().x(); |
Gian Marco | e75a02b | 2017-11-08 12:24:09 +0000 | [diff] [blame] | 410 | |
Michalis Spyrou | 0028d7c | 2020-06-22 13:45:17 +0100 | [diff] [blame] | 411 | run_offset_contribution(window, _mm_result, _vector_sum_col, _vector_sum_row, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, reinterpret_as_3d); |
| 412 | } |
Michele Di Giorgio | 7c850d5 | 2020-09-04 15:01:15 +0100 | [diff] [blame] | 413 | } // namespace arm_compute |