blob: 4ac33d1e29dd293ac9c8bc0cd76750b9c954209d [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2017-2020 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 */
24#include "arm_compute/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.h"
25
26#include "arm_compute/core/AccessWindowStatic.h"
27#include "arm_compute/core/Error.h"
28#include "arm_compute/core/Helpers.h"
29#include "arm_compute/core/ITensor.h"
30#include "arm_compute/core/TensorInfo.h"
31#include "arm_compute/core/Types.h"
32#include "arm_compute/core/Utils.h"
33#include "arm_compute/core/Validate.h"
34#include "arm_compute/core/Window.h"
35
36#include <arm_neon.h>
Gian Marcoe75a02b2017-11-08 12:24:09 +000037
38namespace arm_compute
39{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000040namespace
41{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000042Status validate_arguments(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
43 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000044{
45 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mm_result, 1, DataType::S32);
46
47 // If a_offset == 0, vector_sum_col can be a nullptr
48 if(a_offset != 0)
49 {
50 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
51 ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != mm_result->dimension(0));
52 }
53
54 // If b_offset == 0, vector_sum_row can be a nullptr
55 if(b_offset != 0)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000058
59 // Check if input is a 3D reinterpretation
60 const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x();
61
62 // Validate input
63 ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2)));
64 ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != mm_result->dimension(1));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000065
Georgios Pinitas40626802017-12-08 19:02:45 +000066 TensorShape output_shape = mm_result->tensor_shape();
67 if(output_shape.num_dimensions() > 1)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000068 {
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000069 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
70
Georgios Pinitas40626802017-12-08 19:02:45 +000071 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
72 vector_sum_row_shape.collapse_from(1);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000073 output_shape.collapse_from(output_batch_idx);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000074
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000075 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[output_batch_idx],
Georgios Pinitas358ca202017-12-07 16:47:52 +000076 "mm_result tensor must have the same number of batches of output tensor");
Georgios Pinitas40626802017-12-08 19:02:45 +000077
78 if(a_offset != 0)
79 {
80 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
81 vector_sum_col_shape.collapse_from(1);
82
Georgios Pinitas358ca202017-12-07 16:47:52 +000083 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
84 "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 +000085 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000086 }
87 }
88
Georgios Pinitas631c41a2017-12-06 11:53:03 +000089 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000090}
91
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000092void run_offset_contribution(const Window &window,
93 ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row,
Michalis Spyrou0028d7c2020-06-22 13:45:17 +010094 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 +000095{
96 Window collapsed_window = window.collapse_if_possible(window, Window::DimZ);
Michele Di Giorgio7c850d52020-09-04 15:01:15 +010097 collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000098
99 const int height_input = is_gemm3d ? mm_result->info()->dimension(1) : 0;
100 const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1;
101
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100102 const int window_start_x = window.x().start();
103 const int window_end_x = window.x().end();
104 const int window_step_x = 16;
105
106 Iterator mm_result_it(mm_result, collapsed_window);
107
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000108 if((a_offset != 0) && (b_offset != 0) && (vector_sum_col != nullptr) && (vector_sum_row != nullptr)) // true, true
109 {
110 // Set window for vector_sum_col
111 Window win_vector_sum_col(collapsed_window);
112 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
113 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
114
115 // Set window for vector_sum_row
116 Window win_vector_sum_row(collapsed_window);
117 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
118 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
119 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
120
121 Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
122 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000123
124 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
125
126 // Offset in case vector_sum_col is batched
127 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
128
129 execute_window_loop(collapsed_window, [&](const Coordinates & id)
130 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100131 const int batch_id = id.z() / depth_input;
132 auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
133 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000134
135 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100136 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);
137 b_offset_term_s32 *= b_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000138
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100139 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
140
141 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100142 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000143 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100144 // Compute the leftover term due to a_offset.
145 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000146 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100147 {
148 vld1q_s32(vector_sum_col_ptr + x + 0),
149 vld1q_s32(vector_sum_col_ptr + x + 4),
150 vld1q_s32(vector_sum_col_ptr + x + 8),
151 vld1q_s32(vector_sum_col_ptr + x + 12)
152 }
153 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000154
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100155 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
156 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
157 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
158 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000159
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100160 // Add a_offset_term_s32 and b_offset_term_s32
161 int32x4x4_t offset_term_s32 =
162 {
163 {
164 vdupq_n_s32(k_offset),
165 vdupq_n_s32(k_offset),
166 vdupq_n_s32(k_offset),
167 vdupq_n_s32(k_offset)
168 }
169 };
170
171 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));
172 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));
173 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));
174 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));
175
176 int32x4x4_t in_s32 =
177 {
178 {
179 vld1q_s32(mm_result_ptr + x + 0),
180 vld1q_s32(mm_result_ptr + x + 4),
181 vld1q_s32(mm_result_ptr + x + 8),
182 vld1q_s32(mm_result_ptr + x + 12)
183 }
184 };
185
186 // Add the offset terms to GEMM's result
187 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
188 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
189 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
190 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
191
192 // Store the result with the offset contribution
193 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
194 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
195 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
196 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
197 }
198
199 // Left-overs loop
200 for(; x < window_end_x; ++x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000201 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100202 // Compute the leftover term due to a_offset.
203 int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000204
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100205 a_offset_term_s32 *= a_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000206
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100207 // Add the offset terms to GEMM's result
208 // Store the result with the offset contribution
209 mm_result_ptr[x] += k_offset + a_offset_term_s32 + b_offset_term_s32;
210 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000211 },
212 vector_sum_col_it, vector_sum_row_it, mm_result_it);
213 }
214 else if((a_offset == 0) && (b_offset != 0) && (vector_sum_row != nullptr)) // false, true
215 {
216 ARM_COMPUTE_ERROR_ON_NULLPTR(vector_sum_row);
217
218 // Set window for vector_sum_row
219 Window win_vector_sum_row(collapsed_window);
220 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
221 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
222 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
223
224 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000225
226 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
227
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100228 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000229 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100230 const int batch_id = id.z() / depth_input;
231 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000232
233 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100234 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);
235 b_offset_term_s32 *= b_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000236
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100237 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
238
239 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100240 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000241 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100242 int32x4x4_t in_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000243 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100244 {
245 vld1q_s32(mm_result_ptr + x + 0),
246 vld1q_s32(mm_result_ptr + x + 4),
247 vld1q_s32(mm_result_ptr + x + 8),
248 vld1q_s32(mm_result_ptr + x + 12)
249 }
250 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000251
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100252 // Add the offset terms to GEMM's result
253 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32_vec);
254 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32_vec);
255 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32_vec);
256 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32_vec);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000257
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100258 // Store the result with the offset contribution
259 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
260 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
261 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
262 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
263 }
264
265 // Left-overs loop
266 for(; x < window_end_x; ++x)
267 {
268 // Add the offset terms to GEMM's result
269 // Store the result with the offset contribution
270 mm_result_ptr[x] += b_offset_term_s32;
271 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000272 },
273 vector_sum_row_it, mm_result_it);
274 }
275 else if((a_offset != 0) && (b_offset == 0) && (vector_sum_col != nullptr)) // true, false
276 {
277 // Set window for vector_sum_col
278 Window win_vector_sum_col(collapsed_window);
279 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
280 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
281
282 Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000283
284 // Offset in case vector_sum_col is batched
285 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
286
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100287 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000288 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100289 const int batch_id = id.z() / depth_input;
290 auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
291 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000292
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100293 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100294 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000295 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100296 // Compute the leftover term due to a_offset.
297 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000298 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100299 {
300 vld1q_s32(vector_sum_col_ptr + x + 0),
301 vld1q_s32(vector_sum_col_ptr + x + 4),
302 vld1q_s32(vector_sum_col_ptr + x + 8),
303 vld1q_s32(vector_sum_col_ptr + x + 12)
304 }
305 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000306
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100307 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
308 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
309 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
310 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000311
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100312 int32x4x4_t in_s32 =
313 {
314 {
315 vld1q_s32(mm_result_ptr + x + 0),
316 vld1q_s32(mm_result_ptr + x + 4),
317 vld1q_s32(mm_result_ptr + x + 8),
318 vld1q_s32(mm_result_ptr + x + 12)
319 }
320 };
321
322 // Add the offset terms to GEMM's result
323 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
324 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
325 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
326 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
327
328 // Store the result with the offset contribution
329 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
330 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
331 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
332 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
333 }
334
335 // Left-overs loop
336 for(; x < window_end_x; ++x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000337 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100338 // Compute the leftover term due to a_offset.
339 const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000340
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100341 // Add the offset terms to GEMM's result
342 // Store the result with the offset contribution
343 mm_result_ptr[x] += a_offset_term_s32 * a_offset;
344 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000345 },
346 vector_sum_col_it, mm_result_it);
347 }
348 else // false, false
349 {
350 // No offset contribution from matrix A and matrix B
351 return;
352 }
353}
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000354} // namespace
355
Gian Marcoe75a02b2017-11-08 12:24:09 +0000356NEGEMMLowpOffsetContributionKernel::NEGEMMLowpOffsetContributionKernel()
357 : _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)
358{
359}
360
361void 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)
362{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000363 // Perform validate step
364 ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
365 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result->info(),
366 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT
367 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT
368 a_offset, b_offset)); // NOLINT
Gian Marcoe75a02b2017-11-08 12:24:09 +0000369
370 _vector_sum_col = vector_sum_col;
371 _vector_sum_row = vector_sum_row;
372 _mm_result = mm_result;
373 _a_offset = a_offset;
374 _b_offset = b_offset;
375 _k_offset = a_offset * b_offset * k;
376
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000377 // If a_offset == 0, vector_sum_col can be a nullptr
378 if(a_offset != 0)
379 {
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000380 // Check if vector_sum_col_shape should be slidden or not
381 // 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
382 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
Isabella Gottardie6630e42018-01-18 15:50:39 +0000383 _slide_vector_sum_col = vector_sum_col->info()->tensor_shape().num_dimensions() > 1;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000384 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000385
386 // Configure kernel window
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100387 Window win = calculate_max_window(*mm_result->info(), Steps());
388 Coordinates coord;
389 coord.set_num_dimensions(mm_result->info()->num_dimensions());
390 mm_result->info()->set_valid_region(ValidRegion(coord, mm_result->info()->tensor_shape()));
391 INEKernel::configure(win);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000392}
Gian Marcoe75a02b2017-11-08 12:24:09 +0000393
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000394Status NEGEMMLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
395 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000396{
397 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
Gian Marcoe75a02b2017-11-08 12:24:09 +0000398
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000399 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000400}
401
402void NEGEMMLowpOffsetContributionKernel::run(const Window &window, const ThreadInfo &info)
403{
404 ARM_COMPUTE_UNUSED(info);
405 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
406 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
407
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000408 // Check if input is a 3D reinterpretation
409 const bool reinterpret_as_3d = _vector_sum_row != nullptr
410 && _mm_result->info()->num_dimensions() > 1
411 && _mm_result->info()->tensor_shape().y() != _vector_sum_row->info()->tensor_shape().x();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000412
Michalis Spyrou0028d7c2020-06-22 13:45:17 +0100413 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);
414}
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100415} // namespace arm_compute