blob: af84d024d582a7a16dabf74b91a15a5127558d6d [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
Isabella Gottardie6630e42018-01-18 15:50:39 +00002 * Copyright (c) 2017-2018 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>
37#include <cstddef>
38#include <cstdint>
39
40using namespace arm_compute;
41
42namespace arm_compute
43{
44class Coordinates;
45} // namespace arm_compute
46
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000047namespace
48{
Georgios Pinitas631c41a2017-12-06 11:53:03 +000049Status validate_arguments(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
50 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000051{
52 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mm_result, 1, DataType::S32);
53
54 // If a_offset == 0, vector_sum_col can be a nullptr
55 if(a_offset != 0)
56 {
57 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
58 ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != mm_result->dimension(0));
59 }
60
61 // If b_offset == 0, vector_sum_row can be a nullptr
62 if(b_offset != 0)
63 {
64 ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
65 ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_row->dimension(0) != mm_result->dimension(1));
66
Georgios Pinitas40626802017-12-08 19:02:45 +000067 TensorShape output_shape = mm_result->tensor_shape();
68 if(output_shape.num_dimensions() > 1)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000069 {
Georgios Pinitas40626802017-12-08 19:02:45 +000070 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
71 vector_sum_row_shape.collapse_from(1);
72 output_shape.collapse_from(2);
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000073
Georgios Pinitas358ca202017-12-07 16:47:52 +000074 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[2],
75 "mm_result tensor must have the same number of batches of output tensor");
Georgios Pinitas40626802017-12-08 19:02:45 +000076
77 if(a_offset != 0)
78 {
79 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
80 vector_sum_col_shape.collapse_from(1);
81
Georgios Pinitas358ca202017-12-07 16:47:52 +000082 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
83 "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 +000084 }
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000085 }
86 }
87
Georgios Pinitas631c41a2017-12-06 11:53:03 +000088 return Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000089}
90
Georgios Pinitas631c41a2017-12-06 11:53:03 +000091std::pair<Status, Window> validate_and_configure_window(ITensorInfo *mm_result, ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row,
92 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +000093{
94 constexpr unsigned int num_elems_processed_per_iteration = 16;
95 bool window_changed = false;
96
97 // Configure kernel window
98 Window win = calculate_max_window(*mm_result, Steps(num_elems_processed_per_iteration));
99
100 AccessWindowHorizontal mm_result_access(mm_result, 0, num_elems_processed_per_iteration);
101 window_changed = window_changed || update_window_and_padding(win,
102 mm_result_access);
103
104 if(a_offset != 0)
105 {
106 AccessWindowHorizontal vector_sum_col_access(vector_sum_col, 0, num_elems_processed_per_iteration);
107 window_changed = window_changed || update_window_and_padding(win,
108 vector_sum_col_access);
109 }
110 if(b_offset != 0)
111 {
112 AccessWindowStatic vector_sum_row_access(vector_sum_row, 0, 0, vector_sum_row->dimension(0), 0); // NOLINT
113 window_changed = window_changed || update_window_and_padding(win,
114 vector_sum_row_access);
115 }
116
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000117 Status err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Status{};
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000118 return std::make_pair(err, win);
119}
120} // namespace
121
Gian Marcoe75a02b2017-11-08 12:24:09 +0000122NEGEMMLowpOffsetContributionKernel::NEGEMMLowpOffsetContributionKernel()
123 : _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)
124{
125}
126
127void 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)
128{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000129 // Perform validate step
130 ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
131 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result->info(),
132 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT
133 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT
134 a_offset, b_offset)); // NOLINT
Gian Marcoe75a02b2017-11-08 12:24:09 +0000135
136 _vector_sum_col = vector_sum_col;
137 _vector_sum_row = vector_sum_row;
138 _mm_result = mm_result;
139 _a_offset = a_offset;
140 _b_offset = b_offset;
141 _k_offset = a_offset * b_offset * k;
142
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000143 // If a_offset == 0, vector_sum_col can be a nullptr
144 if(a_offset != 0)
145 {
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000146 // Check if vector_sum_col_shape should be slidden or not
147 // 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
148 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
Isabella Gottardie6630e42018-01-18 15:50:39 +0000149 _slide_vector_sum_col = vector_sum_col->info()->tensor_shape().num_dimensions() > 1;
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000150 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000151
152 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000153 auto win_config = validate_and_configure_window(mm_result->info(),
154 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT
155 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT
156 a_offset, b_offset);
157 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
158 INEKernel::configure(win_config.second);
159}
Gian Marcoe75a02b2017-11-08 12:24:09 +0000160
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000161Status NEGEMMLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
162 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000163{
164 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
165 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(mm_result->clone().get(),
166 vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
167 vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
168 a_offset, b_offset)
169 .first); // NOLINT
Gian Marcoe75a02b2017-11-08 12:24:09 +0000170
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000171 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000172}
173
174void NEGEMMLowpOffsetContributionKernel::run(const Window &window, const ThreadInfo &info)
175{
176 ARM_COMPUTE_UNUSED(info);
177 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
178 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
179
180 Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimZ);
181
182 if(_a_offset != 0 && _b_offset != 0) // true, true
183 {
184 // Set window for vector_sum_col
185 Window win_vector_sum_col(collapsed_window);
186 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
187 if(!_slide_vector_sum_col)
188 {
189 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
190 }
191
192 // Set window for vector_sum_row
193 Window win_vector_sum_row(collapsed_window);
194 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
195 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100196 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
Gian Marcoe75a02b2017-11-08 12:24:09 +0000197
198 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
199 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
200 Iterator mm_result(_mm_result, window);
201
Giorgio Arenaa855af12018-07-16 17:20:38 +0100202 const size_t sum_row_stride_y = _vector_sum_row->info()->strides_in_bytes().y();
203
Isabella Gottardie6630e42018-01-18 15:50:39 +0000204 execute_window_loop(collapsed_window, [&](const Coordinates & id)
Gian Marcoe75a02b2017-11-08 12:24:09 +0000205 {
206 // Compute the leftover term due to a_offset.
207 int32x4x4_t a_offset_term_s32 =
208 {
209 {
210 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
211 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
212 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
213 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
214 }
215 };
216
217 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
218 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
219 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
220 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
221
222 // Compute the leftover term due to b_offset.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100223 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr() + id.z() * sum_row_stride_y) + id.y());
Gian Marcoe75a02b2017-11-08 12:24:09 +0000224 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
225
226 // Add a_offset_term_s32 and b_offset_term_s32
227 int32x4x4_t offset_term_s32 =
228 {
229 {
230 vdupq_n_s32(_k_offset),
231 vdupq_n_s32(_k_offset),
232 vdupq_n_s32(_k_offset),
233 vdupq_n_s32(_k_offset)
234 }
235 };
236
237 offset_term_s32.val[0] = vaddq_s32(offset_term_s32.val[0], vaddq_s32(a_offset_term_s32.val[0], b_offset_term_s32));
238 offset_term_s32.val[1] = vaddq_s32(offset_term_s32.val[1], vaddq_s32(a_offset_term_s32.val[1], b_offset_term_s32));
239 offset_term_s32.val[2] = vaddq_s32(offset_term_s32.val[2], vaddq_s32(a_offset_term_s32.val[2], b_offset_term_s32));
240 offset_term_s32.val[3] = vaddq_s32(offset_term_s32.val[3], vaddq_s32(a_offset_term_s32.val[3], b_offset_term_s32));
241
242 int32x4x4_t in_s32 =
243 {
244 {
245 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
246 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
247 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
248 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
249 }
250 };
251
252 // Add the offset terms to GEMM's result
253 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
254 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
255 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
256 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
257
258 // Store the result with the offset contribution
259 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
260 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
261 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
262 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
263 },
264 vector_sum_col, vector_sum_row, mm_result);
265 }
266 else if((_a_offset == 0) && (_b_offset != 0)) // false, true
267 {
268 // Set window for vector_sum_row
269 Window win_vector_sum_row(collapsed_window);
270 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
271 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
Giorgio Arenaa855af12018-07-16 17:20:38 +0100272 win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
Gian Marcoe75a02b2017-11-08 12:24:09 +0000273
274 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
275 Iterator mm_result(_mm_result, window);
276
Giorgio Arenaa855af12018-07-16 17:20:38 +0100277 const size_t sum_row_stride_y = _vector_sum_row->info()->strides_in_bytes().y();
278
Gian Marcoe75a02b2017-11-08 12:24:09 +0000279 execute_window_loop(window, [&](const Coordinates & id)
280 {
281 // Compute the leftover term due to b_offset.
Giorgio Arenaa855af12018-07-16 17:20:38 +0100282 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr() + id.z() * sum_row_stride_y) + id.y());
Gian Marcoe75a02b2017-11-08 12:24:09 +0000283 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
284
285 int32x4x4_t in_s32 =
286 {
287 {
288 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
289 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
290 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
291 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
292 }
293 };
294
295 // Add the offset terms to GEMM's result
296 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32);
297 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32);
298 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32);
299 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32);
300
301 // Store the result with the offset contribution
302 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
303 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
304 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
305 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
306 },
307 vector_sum_row, mm_result);
308 }
309 else if((_a_offset != 0) && (_b_offset == 0)) // true, false
310 {
311 // Set window for vector_sum_col
312 Window win_vector_sum_col(collapsed_window);
313 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
314 if(!_slide_vector_sum_col)
315 {
316 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
317 }
318
319 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
320 Iterator mm_result(_mm_result, window);
321
322 execute_window_loop(window, [&](const Coordinates & id)
323 {
324 // Compute the leftover term due to a_offset.
325 int32x4x4_t a_offset_term_s32 =
326 {
327 {
328 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
329 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
330 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
331 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
332 }
333 };
334
335 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
336 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
337 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
338 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
339
340 int32x4x4_t in_s32 =
341 {
342 {
343 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
344 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
345 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
346 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
347 }
348 };
349
350 // Add the offset terms to GEMM's result
351 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
352 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
353 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
354 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
355
356 // Store the result with the offset contribution
357 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
358 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
359 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
360 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
361 },
362 vector_sum_col, mm_result);
363 }
364 else // false, false
365 {
366 // No offset contribution from matrix A and matrix B
367 return;
368 }
369}