blob: 174a06955f619967869fdd60a05a31986a90d308 [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 */
Michalis Spyrouebcebf12020-10-21 00:04:14 +010024#include "src/core/NEON/kernels/NEGEMMLowpOffsetContributionKernel.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/AccessWindowStatic.h"
35#include "src/core/helpers/AutoConfiguration.h"
36#include "src/core/helpers/WindowHelpers.h"
Gian Marcoe75a02b2017-11-08 12:24:09 +000037
38#include <arm_neon.h>
Gian Marcoe75a02b2017-11-08 12:24:09 +000039
40namespace arm_compute
41{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000042namespace
43{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000044Status validate_arguments(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
45 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000046{
47 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mm_result, 1, DataType::S32);
48
49 // If a_offset == 0, vector_sum_col can be a nullptr
50 if(a_offset != 0)
51 {
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
53 ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != mm_result->dimension(0));
54 }
55
56 // If b_offset == 0, vector_sum_row can be a nullptr
57 if(b_offset != 0)
58 {
59 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000060
61 // Check if input is a 3D reinterpretation
62 const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x();
63
64 // Validate input
65 ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2)));
66 ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != mm_result->dimension(1));
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000067
Georgios Pinitas40626802017-12-08 19:02:45 +000068 TensorShape output_shape = mm_result->tensor_shape();
69 if(output_shape.num_dimensions() > 1)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000070 {
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000071 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
72
Georgios Pinitas40626802017-12-08 19:02:45 +000073 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
74 vector_sum_row_shape.collapse_from(1);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000075 output_shape.collapse_from(output_batch_idx);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000076
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000077 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[output_batch_idx],
Georgios Pinitas358ca202017-12-07 16:47:52 +000078 "mm_result tensor must have the same number of batches of output tensor");
Georgios Pinitas40626802017-12-08 19:02:45 +000079
80 if(a_offset != 0)
81 {
82 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
83 vector_sum_col_shape.collapse_from(1);
84
Georgios Pinitas358ca202017-12-07 16:47:52 +000085 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
86 "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 +000087 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000088 }
89 }
90
Georgios Pinitas631c41a2017-12-06 11:53:03 +000091 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000092}
93
Georgios Pinitasbb081ca2018-11-08 10:22:01 +000094void run_offset_contribution(const Window &window,
95 ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row,
Michalis Spyrou0028d7c2020-06-22 13:45:17 +010096 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 +000097{
98 Window collapsed_window = window.collapse_if_possible(window, Window::DimZ);
Michele Di Giorgio7c850d52020-09-04 15:01:15 +010099 collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000100
101 const int height_input = is_gemm3d ? mm_result->info()->dimension(1) : 0;
102 const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1;
103
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100104 const int window_start_x = window.x().start();
105 const int window_end_x = window.x().end();
106 const int window_step_x = 16;
107
108 Iterator mm_result_it(mm_result, collapsed_window);
109
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000110 if((a_offset != 0) && (b_offset != 0) && (vector_sum_col != nullptr) && (vector_sum_row != nullptr)) // true, true
111 {
112 // Set window for vector_sum_col
113 Window win_vector_sum_col(collapsed_window);
114 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
115 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
116
117 // Set window for vector_sum_row
118 Window win_vector_sum_row(collapsed_window);
119 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
120 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
121 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
122
123 Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
124 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000125
126 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
127
128 // Offset in case vector_sum_col is batched
129 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
130
131 execute_window_loop(collapsed_window, [&](const Coordinates & id)
132 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100133 const int batch_id = id.z() / depth_input;
134 auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
135 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000136
137 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100138 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);
139 b_offset_term_s32 *= b_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000140
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100141 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
142
143 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100144 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000145 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100146 // Compute the leftover term due to a_offset.
147 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000148 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100149 {
150 vld1q_s32(vector_sum_col_ptr + x + 0),
151 vld1q_s32(vector_sum_col_ptr + x + 4),
152 vld1q_s32(vector_sum_col_ptr + x + 8),
153 vld1q_s32(vector_sum_col_ptr + x + 12)
154 }
155 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000156
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100157 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
158 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
159 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
160 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000161
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100162 // Add a_offset_term_s32 and b_offset_term_s32
163 int32x4x4_t offset_term_s32 =
164 {
165 {
166 vdupq_n_s32(k_offset),
167 vdupq_n_s32(k_offset),
168 vdupq_n_s32(k_offset),
169 vdupq_n_s32(k_offset)
170 }
171 };
172
173 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));
174 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));
175 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));
176 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));
177
178 int32x4x4_t in_s32 =
179 {
180 {
181 vld1q_s32(mm_result_ptr + x + 0),
182 vld1q_s32(mm_result_ptr + x + 4),
183 vld1q_s32(mm_result_ptr + x + 8),
184 vld1q_s32(mm_result_ptr + x + 12)
185 }
186 };
187
188 // Add the offset terms to GEMM's result
189 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
190 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
191 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
192 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
193
194 // Store the result with the offset contribution
195 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
196 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
197 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
198 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
199 }
200
201 // Left-overs loop
202 for(; x < window_end_x; ++x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000203 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100204 // Compute the leftover term due to a_offset.
205 int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000206
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100207 a_offset_term_s32 *= a_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000208
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100209 // Add the offset terms to GEMM's result
210 // Store the result with the offset contribution
211 mm_result_ptr[x] += k_offset + a_offset_term_s32 + b_offset_term_s32;
212 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000213 },
214 vector_sum_col_it, vector_sum_row_it, mm_result_it);
215 }
216 else if((a_offset == 0) && (b_offset != 0) && (vector_sum_row != nullptr)) // false, true
217 {
218 ARM_COMPUTE_ERROR_ON_NULLPTR(vector_sum_row);
219
220 // Set window for vector_sum_row
221 Window win_vector_sum_row(collapsed_window);
222 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
223 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
224 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
225
226 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000227
228 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
229
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100230 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000231 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100232 const int batch_id = id.z() / depth_input;
233 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000234
235 // Compute the leftover term due to b_offset.
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100236 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);
237 b_offset_term_s32 *= b_offset;
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000238
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100239 const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
240
241 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100242 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000243 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100244 int32x4x4_t in_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000245 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100246 {
247 vld1q_s32(mm_result_ptr + x + 0),
248 vld1q_s32(mm_result_ptr + x + 4),
249 vld1q_s32(mm_result_ptr + x + 8),
250 vld1q_s32(mm_result_ptr + x + 12)
251 }
252 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000253
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100254 // Add the offset terms to GEMM's result
255 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32_vec);
256 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32_vec);
257 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32_vec);
258 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32_vec);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000259
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100260 // Store the result with the offset contribution
261 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
262 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
263 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
264 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
265 }
266
267 // Left-overs loop
268 for(; x < window_end_x; ++x)
269 {
270 // Add the offset terms to GEMM's result
271 // Store the result with the offset contribution
272 mm_result_ptr[x] += b_offset_term_s32;
273 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000274 },
275 vector_sum_row_it, mm_result_it);
276 }
277 else if((a_offset != 0) && (b_offset == 0) && (vector_sum_col != nullptr)) // true, false
278 {
279 // Set window for vector_sum_col
280 Window win_vector_sum_col(collapsed_window);
281 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
282 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
283
284 Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000285
286 // Offset in case vector_sum_col is batched
287 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
288
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100289 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000290 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100291 const int batch_id = id.z() / depth_input;
292 auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
293 auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000294
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100295 int x = window_start_x;
Michalis Spyrouc2268532020-10-09 11:52:10 +0100296 for(; x <= (window_end_x - window_step_x); x += window_step_x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000297 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100298 // Compute the leftover term due to a_offset.
299 int32x4x4_t a_offset_term_s32 =
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000300 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100301 {
302 vld1q_s32(vector_sum_col_ptr + x + 0),
303 vld1q_s32(vector_sum_col_ptr + x + 4),
304 vld1q_s32(vector_sum_col_ptr + x + 8),
305 vld1q_s32(vector_sum_col_ptr + x + 12)
306 }
307 };
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000308
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100309 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
310 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
311 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
312 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000313
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100314 int32x4x4_t in_s32 =
315 {
316 {
317 vld1q_s32(mm_result_ptr + x + 0),
318 vld1q_s32(mm_result_ptr + x + 4),
319 vld1q_s32(mm_result_ptr + x + 8),
320 vld1q_s32(mm_result_ptr + x + 12)
321 }
322 };
323
324 // Add the offset terms to GEMM's result
325 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
326 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
327 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
328 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
329
330 // Store the result with the offset contribution
331 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
332 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
333 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
334 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
335 }
336
337 // Left-overs loop
338 for(; x < window_end_x; ++x)
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000339 {
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100340 // Compute the leftover term due to a_offset.
341 const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000342
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100343 // Add the offset terms to GEMM's result
344 // Store the result with the offset contribution
345 mm_result_ptr[x] += a_offset_term_s32 * a_offset;
346 }
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000347 },
348 vector_sum_col_it, mm_result_it);
349 }
350 else // false, false
351 {
352 // No offset contribution from matrix A and matrix B
353 return;
354 }
355}
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000356} // namespace
357
Gian Marcoe75a02b2017-11-08 12:24:09 +0000358NEGEMMLowpOffsetContributionKernel::NEGEMMLowpOffsetContributionKernel()
359 : _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)
360{
361}
362
363void 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)
364{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000365 // Perform validate step
366 ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
367 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result->info(),
368 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT
369 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT
370 a_offset, b_offset)); // NOLINT
Gian Marcoe75a02b2017-11-08 12:24:09 +0000371
372 _vector_sum_col = vector_sum_col;
373 _vector_sum_row = vector_sum_row;
374 _mm_result = mm_result;
375 _a_offset = a_offset;
376 _b_offset = b_offset;
377 _k_offset = a_offset * b_offset * k;
378
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000379 // If a_offset == 0, vector_sum_col can be a nullptr
380 if(a_offset != 0)
381 {
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000382 // Check if vector_sum_col_shape should be slidden or not
383 // 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
384 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
Isabella Gottardie6630e42018-01-18 15:50:39 +0000385 _slide_vector_sum_col = vector_sum_col->info()->tensor_shape().num_dimensions() > 1;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000386 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000387
388 // Configure kernel window
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100389 Window win = calculate_max_window(*mm_result->info(), Steps());
390 Coordinates coord;
391 coord.set_num_dimensions(mm_result->info()->num_dimensions());
392 mm_result->info()->set_valid_region(ValidRegion(coord, mm_result->info()->tensor_shape()));
393 INEKernel::configure(win);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000394}
Gian Marcoe75a02b2017-11-08 12:24:09 +0000395
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000396Status NEGEMMLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
397 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000398{
399 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 +0000400
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000401 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000402}
403
404void NEGEMMLowpOffsetContributionKernel::run(const Window &window, const ThreadInfo &info)
405{
406 ARM_COMPUTE_UNUSED(info);
407 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
408 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
409
Georgios Pinitasbb081ca2018-11-08 10:22:01 +0000410 // Check if input is a 3D reinterpretation
411 const bool reinterpret_as_3d = _vector_sum_row != nullptr
412 && _mm_result->info()->num_dimensions() > 1
413 && _mm_result->info()->tensor_shape().y() != _vector_sum_row->info()->tensor_shape().x();
Gian Marcoe75a02b2017-11-08 12:24:09 +0000414
Michalis Spyrou0028d7c2020-06-22 13:45:17 +0100415 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);
416}
Michele Di Giorgio7c850d52020-09-04 15:01:15 +0100417} // namespace arm_compute