blob: a9896772f6250c8d2ac00d1f5228d317f997961f [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
SiCongLib88272e2021-02-24 15:40:57 +00002 * Copyright (c) 2017-2021 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
111 Iterator mm_result_it(mm_result, collapsed_window);
112
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000113 if((a_offset != 0) && (b_offset != 0) && (vector_sum_col != nullptr) && (vector_sum_row != nullptr)) // true, true
114 {
115 // Set window for vector_sum_col
116 Window win_vector_sum_col(collapsed_window);
117 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
118 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
119
120 // Set window for vector_sum_row
121 Window win_vector_sum_row(collapsed_window);
122 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
123 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
124 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
125
126 Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
127 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000128
129 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
130
131 // Offset in case vector_sum_col is batched
132 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
133
134 execute_window_loop(collapsed_window, [&](const Coordinates & id)
135 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100136 const int batch_id = id.z() / depth_input;
137 auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
138 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000139
140 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100141 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);
142 b_offset_term_s32 *= b_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000143
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100144 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
145
146 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100147 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000148 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100149 // Compute the leftover term due to a_offset.
150 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000151 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100152 {
153 vld1q_s32(vector_sum_col_ptr + x + 0),
154 vld1q_s32(vector_sum_col_ptr + x + 4),
155 vld1q_s32(vector_sum_col_ptr + x + 8),
156 vld1q_s32(vector_sum_col_ptr + x + 12)
157 }
158 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000159
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100160 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
161 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
162 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
163 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000164
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100165 // Add a_offset_term_s32 and b_offset_term_s32
166 int32x4x4_t offset_term_s32 =
167 {
168 {
169 vdupq_n_s32(k_offset),
170 vdupq_n_s32(k_offset),
171 vdupq_n_s32(k_offset),
172 vdupq_n_s32(k_offset)
173 }
174 };
175
176 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));
177 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));
178 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));
179 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));
180
181 int32x4x4_t in_s32 =
182 {
183 {
184 vld1q_s32(mm_result_ptr + x + 0),
185 vld1q_s32(mm_result_ptr + x + 4),
186 vld1q_s32(mm_result_ptr + x + 8),
187 vld1q_s32(mm_result_ptr + x + 12)
188 }
189 };
190
191 // Add the offset terms to GEMM's result
192 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
193 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
194 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
195 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
196
197 // Store the result with the offset contribution
198 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
199 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
200 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
201 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
202 }
203
204 // Left-overs loop
205 for(; x < window_end_x; ++x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000206 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100207 // Compute the leftover term due to a_offset.
208 int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000209
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100210 a_offset_term_s32 *= a_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000211
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100212 // Add the offset terms to GEMM's result
213 // Store the result with the offset contribution
214 mm_result_ptr[x] += k_offset + a_offset_term_s32 + b_offset_term_s32;
215 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000216 },
217 vector_sum_col_it, vector_sum_row_it, mm_result_it);
218 }
219 else if((a_offset == 0) && (b_offset != 0) && (vector_sum_row != nullptr)) // false, true
220 {
221 ARM_COMPUTE_ERROR_ON_NULLPTR(vector_sum_row);
222
223 // Set window for vector_sum_row
224 Window win_vector_sum_row(collapsed_window);
225 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
226 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
227 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
228
229 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000230
231 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
232
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100233 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000234 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100235 const int batch_id = id.z() / depth_input;
236 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000237
238 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100239 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);
240 b_offset_term_s32 *= b_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000241
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100242 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
243
244 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100245 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000246 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100247 int32x4x4_t in_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000248 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100249 {
250 vld1q_s32(mm_result_ptr + x + 0),
251 vld1q_s32(mm_result_ptr + x + 4),
252 vld1q_s32(mm_result_ptr + x + 8),
253 vld1q_s32(mm_result_ptr + x + 12)
254 }
255 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000256
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100257 // Add the offset terms to GEMM's result
258 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32_vec);
259 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32_vec);
260 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32_vec);
261 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32_vec);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000262
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100263 // Store the result with the offset contribution
264 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
265 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
266 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
267 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
268 }
269
270 // Left-overs loop
271 for(; x < window_end_x; ++x)
272 {
273 // Add the offset terms to GEMM's result
274 // Store the result with the offset contribution
275 mm_result_ptr[x] += b_offset_term_s32;
276 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000277 },
278 vector_sum_row_it, mm_result_it);
279 }
280 else if((a_offset != 0) && (b_offset == 0) && (vector_sum_col != nullptr)) // true, false
281 {
282 // Set window for vector_sum_col
283 Window win_vector_sum_col(collapsed_window);
284 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
285 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
286
287 Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000288
289 // Offset in case vector_sum_col is batched
290 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
291
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100292 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000293 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100294 const int batch_id = id.z() / depth_input;
295 auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
296 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000297
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100298 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100299 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000300 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100301 // Compute the leftover term due to a_offset.
302 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000303 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100304 {
305 vld1q_s32(vector_sum_col_ptr + x + 0),
306 vld1q_s32(vector_sum_col_ptr + x + 4),
307 vld1q_s32(vector_sum_col_ptr + x + 8),
308 vld1q_s32(vector_sum_col_ptr + x + 12)
309 }
310 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000311
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100312 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
313 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
314 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
315 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000316
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100317 int32x4x4_t in_s32 =
318 {
319 {
320 vld1q_s32(mm_result_ptr + x + 0),
321 vld1q_s32(mm_result_ptr + x + 4),
322 vld1q_s32(mm_result_ptr + x + 8),
323 vld1q_s32(mm_result_ptr + x + 12)
324 }
325 };
326
327 // Add the offset terms to GEMM's result
328 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
329 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
330 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
331 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
332
333 // Store the result with the offset contribution
334 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
335 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
336 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
337 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
338 }
339
340 // Left-overs loop
341 for(; x < window_end_x; ++x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000342 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100343 // Compute the leftover term due to a_offset.
344 const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000345
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100346 // Add the offset terms to GEMM's result
347 // Store the result with the offset contribution
348 mm_result_ptr[x] += a_offset_term_s32 * a_offset;
349 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000350 },
351 vector_sum_col_it, mm_result_it);
352 }
353 else // false, false
354 {
355 // No offset contribution from matrix A and matrix B
356 return;
357 }
358}
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000359} // namespace
360
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100361void 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 +0000362{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000363 // Perform validate step
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100364 ARM_COMPUTE_UNUSED(vector_sum_row);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000365 ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100366 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 +0000367
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100368 _a_offset = a_offset;
369 _b_offset = b_offset;
370 _k_offset = a_offset * b_offset * k;
Gian Marcoe75a02b2017-11-08 12:24:09 +0000371
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000372 // If a_offset == 0, vector_sum_col can be a nullptr
373 if(a_offset != 0)
374 {
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000375 // Check if vector_sum_col_shape should be slidden or not
376 // 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
377 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100378 _slide_vector_sum_col = vector_sum_col->tensor_shape().num_dimensions() > 1;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000379 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000380
381 // Configure kernel window
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100382 Window win = calculate_max_window(*mm_result, Steps());
383 ICpuKernel::configure(win);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000384}
Gian Marcoe75a02b2017-11-08 12:24:09 +0000385
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100386Status CpuGemmLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
387 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000388{
389 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 +0000390 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000391}
392
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100393void CpuGemmLowpOffsetContributionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000394{
395 ARM_COMPUTE_UNUSED(info);
396 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100397 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
398
399 auto vector_sum_col = tensors.get_const_tensor(TensorType::ACL_SRC_0);
400 auto vector_sum_row = tensors.get_const_tensor(TensorType::ACL_SRC_1);
401 auto mm_result = tensors.get_tensor(TensorType::ACL_DST);
Gian Marcoe75a02b2017-11-08 12:24:09 +0000402
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000403 // Check if input is a 3D reinterpretation
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100404 const bool reinterpret_as_3d = vector_sum_row != nullptr
405 && mm_result->info()->num_dimensions() > 1
406 && mm_result->info()->tensor_shape().y() != vector_sum_row->info()->tensor_shape().x();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000407
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100408 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 +0100409}
Manuel Bottinicfac51c2021-06-18 15:47:28 +0100410
411const char *CpuGemmLowpOffsetContributionKernel::name() const
412{
413 return "CpuGemmLowpOffsetContributionKernel";
414}
415} // namespace kernels
416} // namespace cpu
417} // namespace arm_compute