blob: 3d41548a6aeb4487a2c2d6bb8b2d0d95378438b8 [file] [log] [blame]
Gian Marcoe75a02b2017-11-08 12:24:09 +00001/*
2 * Copyright (c) 2017 ARM Limited.
3 *
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 {
146 TensorShape vector_sum_col_shape = vector_sum_col->info()->tensor_shape(); // NOLINT
147 vector_sum_col_shape.collapse(1);
148
149 // Check if vector_sum_col_shape should be slidden or not
150 // 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
151 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
152 _slide_vector_sum_col = vector_sum_col_shape[1] != 1;
153 }
Gian Marcoe75a02b2017-11-08 12:24:09 +0000154
155 // Configure kernel window
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000156 auto win_config = validate_and_configure_window(mm_result->info(),
157 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT
158 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT
159 a_offset, b_offset);
160 ARM_COMPUTE_ERROR_THROW_ON(win_config.first);
161 INEKernel::configure(win_config.second);
162}
Gian Marcoe75a02b2017-11-08 12:24:09 +0000163
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000164Status NEGEMMLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
165 int32_t a_offset, int32_t b_offset)
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000166{
167 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
168 ARM_COMPUTE_RETURN_ON_ERROR(validate_and_configure_window(mm_result->clone().get(),
169 vector_sum_col != nullptr ? vector_sum_col->clone().get() : nullptr,
170 vector_sum_row != nullptr ? vector_sum_row->clone().get() : nullptr,
171 a_offset, b_offset)
172 .first); // NOLINT
Gian Marcoe75a02b2017-11-08 12:24:09 +0000173
Georgios Pinitas631c41a2017-12-06 11:53:03 +0000174 return Status{};
Gian Marcoe75a02b2017-11-08 12:24:09 +0000175}
176
177void NEGEMMLowpOffsetContributionKernel::run(const Window &window, const ThreadInfo &info)
178{
179 ARM_COMPUTE_UNUSED(info);
180 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
181 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
182
183 Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimZ);
184
185 if(_a_offset != 0 && _b_offset != 0) // true, true
186 {
187 // Set window for vector_sum_col
188 Window win_vector_sum_col(collapsed_window);
189 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
190 if(!_slide_vector_sum_col)
191 {
192 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
193 }
194
195 // Set window for vector_sum_row
196 Window win_vector_sum_row(collapsed_window);
197 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
198 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
199
200 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
201 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
202 Iterator mm_result(_mm_result, window);
203
204 execute_window_loop(window, [&](const Coordinates & id)
205 {
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.
223 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr()) + id.y());
224 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));
272
273 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
274 Iterator mm_result(_mm_result, window);
275
276 execute_window_loop(window, [&](const Coordinates & id)
277 {
278 // Compute the leftover term due to b_offset.
279 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr()) + id.y());
280 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
281
282 int32x4x4_t in_s32 =
283 {
284 {
285 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
286 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
287 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
288 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
289 }
290 };
291
292 // Add the offset terms to GEMM's result
293 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32);
294 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32);
295 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32);
296 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32);
297
298 // Store the result with the offset contribution
299 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
300 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
301 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
302 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
303 },
304 vector_sum_row, mm_result);
305 }
306 else if((_a_offset != 0) && (_b_offset == 0)) // true, false
307 {
308 // Set window for vector_sum_col
309 Window win_vector_sum_col(collapsed_window);
310 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
311 if(!_slide_vector_sum_col)
312 {
313 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
314 }
315
316 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
317 Iterator mm_result(_mm_result, window);
318
319 execute_window_loop(window, [&](const Coordinates & id)
320 {
321 // Compute the leftover term due to a_offset.
322 int32x4x4_t a_offset_term_s32 =
323 {
324 {
325 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
326 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
327 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
328 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
329 }
330 };
331
332 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
333 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
334 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
335 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
336
337 int32x4x4_t in_s32 =
338 {
339 {
340 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
341 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
342 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
343 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
344 }
345 };
346
347 // Add the offset terms to GEMM's result
348 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
349 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
350 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
351 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
352
353 // Store the result with the offset contribution
354 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
355 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
356 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
357 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
358 },
359 vector_sum_col, mm_result);
360 }
361 else // false, false
362 {
363 // No offset contribution from matrix A and matrix B
364 return;
365 }
366}