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