blob: 62f4014acbdde007b83933636319767faff54f95 [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{
49Error 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)
51{
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
67 TensorShape output_shape = mm_result->tensor_shape();
68 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
69 vector_sum_row_shape.collapse(1);
70 output_shape.collapse(2);
71
72 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[2],
73 "mm_result tensor must have the same number of batches of output tensor");
74
75 if(a_offset != 0)
76 {
77 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
78 vector_sum_col_shape.collapse(1);
79
80 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
81 "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");
82 }
83 }
84
85 return Error{};
86}
87
88std::pair<Error, Window> validate_and_configure_window(ITensorInfo *mm_result, ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row,
89 int32_t a_offset, int32_t b_offset)
90{
91 constexpr unsigned int num_elems_processed_per_iteration = 16;
92 bool window_changed = false;
93
94 // Configure kernel window
95 Window win = calculate_max_window(*mm_result, Steps(num_elems_processed_per_iteration));
96
97 AccessWindowHorizontal mm_result_access(mm_result, 0, num_elems_processed_per_iteration);
98 window_changed = window_changed || update_window_and_padding(win,
99 mm_result_access);
100
101 if(a_offset != 0)
102 {
103 AccessWindowHorizontal vector_sum_col_access(vector_sum_col, 0, num_elems_processed_per_iteration);
104 window_changed = window_changed || update_window_and_padding(win,
105 vector_sum_col_access);
106 }
107 if(b_offset != 0)
108 {
109 AccessWindowStatic vector_sum_row_access(vector_sum_row, 0, 0, vector_sum_row->dimension(0), 0); // NOLINT
110 window_changed = window_changed || update_window_and_padding(win,
111 vector_sum_row_access);
112 }
113
114 Error err = (window_changed) ? ARM_COMPUTE_CREATE_ERROR(ErrorCode::RUNTIME_ERROR, "Insufficient Padding!") : Error{};
115 return std::make_pair(err, win);
116}
117} // namespace
118
Gian Marcoe75a02b2017-11-08 12:24:09 +0000119NEGEMMLowpOffsetContributionKernel::NEGEMMLowpOffsetContributionKernel()
120 : _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)
121{
122}
123
124void 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)
125{
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000126 // Perform validate step
127 ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
128 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result->info(),
129 vector_sum_col != nullptr ? vector_sum_col->info() : nullptr, // NOLINT
130 vector_sum_row != nullptr ? vector_sum_row->info() : nullptr, // NOLINT
131 a_offset, b_offset)); // NOLINT
Gian Marcoe75a02b2017-11-08 12:24:09 +0000132
133 _vector_sum_col = vector_sum_col;
134 _vector_sum_row = vector_sum_row;
135 _mm_result = mm_result;
136 _a_offset = a_offset;
137 _b_offset = b_offset;
138 _k_offset = a_offset * b_offset * k;
139
Georgios Pinitasa3b1b462017-11-16 19:24:39 +0000140 // If a_offset == 0, vector_sum_col can be a nullptr
141 if(a_offset != 0)
142 {
143 TensorShape vector_sum_col_shape = vector_sum_col->info()->tensor_shape(); // NOLINT
144 vector_sum_col_shape.collapse(1);
145
146 // 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
149 _slide_vector_sum_col = vector_sum_col_shape[1] != 1;
150 }
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 Pinitasa3b1b462017-11-16 19:24:39 +0000161Error 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)
163{
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 Pinitasa3b1b462017-11-16 19:24:39 +0000171 return Error{};
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));
196
197 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
198 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
199 Iterator mm_result(_mm_result, window);
200
201 execute_window_loop(window, [&](const Coordinates & id)
202 {
203 // Compute the leftover term due to a_offset.
204 int32x4x4_t a_offset_term_s32 =
205 {
206 {
207 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
208 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
209 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
210 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
211 }
212 };
213
214 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
215 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
216 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
217 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
218
219 // Compute the leftover term due to b_offset.
220 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr()) + id.y());
221 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
222
223 // Add a_offset_term_s32 and b_offset_term_s32
224 int32x4x4_t offset_term_s32 =
225 {
226 {
227 vdupq_n_s32(_k_offset),
228 vdupq_n_s32(_k_offset),
229 vdupq_n_s32(_k_offset),
230 vdupq_n_s32(_k_offset)
231 }
232 };
233
234 offset_term_s32.val[0] = vaddq_s32(offset_term_s32.val[0], vaddq_s32(a_offset_term_s32.val[0], b_offset_term_s32));
235 offset_term_s32.val[1] = vaddq_s32(offset_term_s32.val[1], vaddq_s32(a_offset_term_s32.val[1], b_offset_term_s32));
236 offset_term_s32.val[2] = vaddq_s32(offset_term_s32.val[2], vaddq_s32(a_offset_term_s32.val[2], b_offset_term_s32));
237 offset_term_s32.val[3] = vaddq_s32(offset_term_s32.val[3], vaddq_s32(a_offset_term_s32.val[3], b_offset_term_s32));
238
239 int32x4x4_t in_s32 =
240 {
241 {
242 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
243 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
244 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
245 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
246 }
247 };
248
249 // Add the offset terms to GEMM's result
250 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
251 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
252 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
253 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
254
255 // Store the result with the offset contribution
256 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
257 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
258 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
259 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
260 },
261 vector_sum_col, vector_sum_row, mm_result);
262 }
263 else if((_a_offset == 0) && (_b_offset != 0)) // false, true
264 {
265 // Set window for vector_sum_row
266 Window win_vector_sum_row(collapsed_window);
267 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
268 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
269
270 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
271 Iterator mm_result(_mm_result, window);
272
273 execute_window_loop(window, [&](const Coordinates & id)
274 {
275 // Compute the leftover term due to b_offset.
276 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr()) + id.y());
277 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
278
279 int32x4x4_t in_s32 =
280 {
281 {
282 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
283 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
284 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
285 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
286 }
287 };
288
289 // Add the offset terms to GEMM's result
290 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32);
291 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32);
292 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32);
293 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32);
294
295 // Store the result with the offset contribution
296 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
297 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
298 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
299 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
300 },
301 vector_sum_row, mm_result);
302 }
303 else if((_a_offset != 0) && (_b_offset == 0)) // true, false
304 {
305 // Set window for vector_sum_col
306 Window win_vector_sum_col(collapsed_window);
307 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
308 if(!_slide_vector_sum_col)
309 {
310 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
311 }
312
313 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
314 Iterator mm_result(_mm_result, window);
315
316 execute_window_loop(window, [&](const Coordinates & id)
317 {
318 // Compute the leftover term due to a_offset.
319 int32x4x4_t a_offset_term_s32 =
320 {
321 {
322 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
323 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
324 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
325 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
326 }
327 };
328
329 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
330 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
331 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
332 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
333
334 int32x4x4_t in_s32 =
335 {
336 {
337 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
338 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
339 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
340 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
341 }
342 };
343
344 // Add the offset terms to GEMM's result
345 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
346 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
347 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
348 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
349
350 // Store the result with the offset contribution
351 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
352 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
353 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
354 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
355 },
356 vector_sum_col, mm_result);
357 }
358 else // false, false
359 {
360 // No offset contribution from matrix A and matrix B
361 return;
362 }
363}