blob: a65f1a33de740a40a63b00916004b079ca57e99f [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
Mohammed Suhail Munshi67824522022-09-29 13:07:21 +01002 * Copyright (c) 2017-2022 Arm Limited.
Gian Marcoe75a02b2017-11-08 12:24:09 +00003 *
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 Pinitas7891a732021-08-20 21:39:25 +010024#include "src/cpu/kernels/CpuGemmLowpOffsetContributionKernel.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000025
Gian Marcoe75a02b2017-11-08 12:24:09 +000026#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 Park68dd25f2020-10-19 16:00:11 +010034#include "src/core/helpers/AutoConfiguration.h"
35#include "src/core/helpers/WindowHelpers.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000036
37#include <arm_neon.h>
Gian Marcoe75a02b2017-11-08 12:24:09 +000038
39namespace arm_compute
40{
Manuel Bottinicfac51c2021-06-18 15:47:28 +010041namespace cpu
42{
43namespace kernels
44{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000045namespace
46{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000047Status 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 Pinitasa3b1b462017-11-16 19:24:39 +000049{
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 Pinitasbb081ca2018-11-08 10:22:01 +000063
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 Pinitasa3b1b462017-11-16 19:24:39 +000070
Georgios Pinitas40626802017-12-08 19:02:45 +000071 TensorShape output_shape = mm_result->tensor_shape();
72 if(output_shape.num_dimensions() > 1)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000073 {
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000074 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
75
Georgios Pinitas40626802017-12-08 19:02:45 +000076 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
77 vector_sum_row_shape.collapse_from(1);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000078 output_shape.collapse_from(output_batch_idx);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000079
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000080 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[output_batch_idx],
Georgios Pinitas358ca202017-12-07 16:47:52 +000081 "mm_result tensor must have the same number of batches of output tensor");
Georgios Pinitas40626802017-12-08 19:02:45 +000082
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 Pinitas358ca202017-12-07 16:47:52 +000088 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 Pinitas40626802017-12-08 19:02:45 +000090 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000091 }
92 }
93
Georgios Pinitas631c41a2017-12-06 11:53:03 +000094 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000095}
96
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000097void run_offset_contribution(const Window &window,
98 ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row,
Michalis Spyrou0028d7c2020-06-22 13:45:17 +010099 int32_t a_offset, int32_t b_offset, int32_t k_offset, bool slide_vector_sum_col, bool is_gemm3d)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000100{
101 Window collapsed_window = window.collapse_if_possible(window, Window::DimZ);
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100102 collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000103
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 Giorgio7c850d52020-09-04 15:01:15 +0100107 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 Munshi67824522022-09-29 13:07:21 +0100111 // 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 Giorgio7c850d52020-09-04 15:01:15 +0100114
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000115 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 Pinitasbb081ca2018-11-08 10:22:01 +0000130
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 Munshi67824522022-09-29 13:07:21 +0100138 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 Pinitasbb081ca2018-11-08 10:22:01 +0000142
143 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100144 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 Pinitasbb081ca2018-11-08 10:22:01 +0000146
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100147 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
148
149 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100150 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000151 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100152 // Compute the leftover term due to a_offset.
153 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000154 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100155 {
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 Pinitasbb081ca2018-11-08 10:22:01 +0000162
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100163 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 Pinitasbb081ca2018-11-08 10:22:01 +0000167
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100168 // 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 Pinitasbb081ca2018-11-08 10:22:01 +0000209 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100210 // Compute the leftover term due to a_offset.
211 int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000212
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100213 a_offset_term_s32 *= a_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000214
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100215 // 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 Pinitasbb081ca2018-11-08 10:22:01 +0000219 },
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 Pinitasbb081ca2018-11-08 10:22:01 +0000233
234 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
235
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100236 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000237 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100238 const int batch_id = id.z() / depth_input;
239 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000240
241 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100242 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 Pinitasbb081ca2018-11-08 10:22:01 +0000244
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100245 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
246
247 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100248 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000249 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100250 int32x4x4_t in_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000251 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100252 {
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 Pinitasbb081ca2018-11-08 10:22:01 +0000259
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100260 // 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 Pinitasbb081ca2018-11-08 10:22:01 +0000265
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100266 // 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 Pinitasbb081ca2018-11-08 10:22:01 +0000280 },
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 Pinitasbb081ca2018-11-08 10:22:01 +0000291
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 Giorgio7c850d52020-09-04 15:01:15 +0100295 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000296 {
Mohammed Suhail Munshi67824522022-09-29 13:07:21 +0100297 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 Pinitasbb081ca2018-11-08 10:22:01 +0000301
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100302 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100303 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000304 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100305 // Compute the leftover term due to a_offset.
306 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000307 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100308 {
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 Pinitasbb081ca2018-11-08 10:22:01 +0000315
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100316 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 Pinitasbb081ca2018-11-08 10:22:01 +0000320
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100321 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 Pinitasbb081ca2018-11-08 10:22:01 +0000346 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100347 // Compute the leftover term due to a_offset.
348 const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000349
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100350 // 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 Pinitasbb081ca2018-11-08 10:22:01 +0000354 },
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 Pinitasa3b1b462017-11-16 19:24:39 +0000363} // namespace
364
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100365void 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 Marcoe75a02b2017-11-08 12:24:09 +0000366{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000367 // Perform validate step
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100368 ARM_COMPUTE_UNUSED(vector_sum_row);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000369 ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100370 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
Gian Marcoe75a02b2017-11-08 12:24:09 +0000371
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100372 _a_offset = a_offset;
373 _b_offset = b_offset;
374 _k_offset = a_offset * b_offset * k;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000375
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000376 // If a_offset == 0, vector_sum_col can be a nullptr
377 if(a_offset != 0)
378 {
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000379 // 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 Bottinicfac51c2021-06-18 15:47:28 +0100382 _slide_vector_sum_col = vector_sum_col->tensor_shape().num_dimensions() > 1;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000383 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000384
385 // Configure kernel window
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100386 Window win = calculate_max_window(*mm_result, Steps());
387 ICpuKernel::configure(win);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000388}
Gian Marcoe75a02b2017-11-08 12:24:09 +0000389
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100390Status 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 Pinitasa3b1b462017-11-16 19:24:39 +0000392{
393 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000394 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000395}
396
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100397void CpuGemmLowpOffsetContributionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000398{
399 ARM_COMPUTE_UNUSED(info);
400 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100401 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 Marcoe75a02b2017-11-08 12:24:09 +0000406
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000407 // Check if input is a 3D reinterpretation
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100408 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 Marcoe75a02b2017-11-08 12:24:09 +0000411
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100412 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 Spyrou0028d7c2020-06-22 13:45:17 +0100413}
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100414
415const char *CpuGemmLowpOffsetContributionKernel::name() const
416{
417 return "CpuGemmLowpOffsetContributionKernel";
418}
419} // namespace kernels
420} // namespace cpu
421} // namespace arm_compute