blob: bd550db54c3d16cb09e745c0a5244f55562c64cd [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
47NEGEMMLowpOffsetContributionKernel::NEGEMMLowpOffsetContributionKernel()
48 : _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)
49{
50}
51
52void 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)
53{
54 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mm_result, 1, DataType::S32);
55
56 // If a_offset == 0, vector_sum_col can be a nullptr
57 if(a_offset != 0)
58 {
59 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
60 ARM_COMPUTE_ERROR_ON(vector_sum_col->info()->dimension(0) != mm_result->info()->dimension(0));
61
62 TensorShape vector_sum_col_shape = vector_sum_col->info()->tensor_shape();
63 vector_sum_col_shape.collapse(1);
64
65 // Check if vector_sum_col_shape should be slidden or not
66 // 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
67 // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
68 _slide_vector_sum_col = vector_sum_col_shape[1] != 1;
69 }
70
71 // If b_offset == 0, vector_sum_row can be a nullptr
72 if(b_offset != 0)
73 {
74 ARM_COMPUTE_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
75 ARM_COMPUTE_ERROR_ON(vector_sum_row->info()->dimension(0) != mm_result->info()->dimension(1));
76
77 TensorShape output_shape = mm_result->info()->tensor_shape();
78 TensorShape vector_sum_row_shape = vector_sum_row->info()->tensor_shape();
79 vector_sum_row_shape.collapse(1);
80 output_shape.collapse(2);
81
82 ARM_COMPUTE_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[2], "mm_result tensor must have the same number of batches of output tensor");
83
84 if(a_offset != 0)
85 {
86 TensorShape vector_sum_col_shape = vector_sum_col->info()->tensor_shape();
87 vector_sum_col_shape.collapse(1);
88
89 ARM_COMPUTE_ERROR_ON_MSG(vector_sum_col_shape[1] != 1
90 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
91 "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");
92 }
93 }
94
95 _vector_sum_col = vector_sum_col;
96 _vector_sum_row = vector_sum_row;
97 _mm_result = mm_result;
98 _a_offset = a_offset;
99 _b_offset = b_offset;
100 _k_offset = a_offset * b_offset * k;
101
102 constexpr unsigned int num_elems_processed_per_iteration = 16;
103
104 // Configure kernel window
105 Window win = calculate_max_window(*mm_result->info(), Steps(num_elems_processed_per_iteration));
106
107 AccessWindowHorizontal mm_result_access(mm_result->info(), 0, num_elems_processed_per_iteration);
108
109 // Accordingly with a_offset and b_offset, we can have 4 cases:
110 // a_offset != 0 && b_offset != 0
111 // a_offset = 0 && b_offset != 0
112 // a_offset != 0 && b_offset = 0
113 // a_offset = 0 && b_offset = 0
114 if(a_offset != 0 && b_offset != 0)
115 {
116 AccessWindowStatic vector_sum_row_access(vector_sum_row->info(), 0, 0, vector_sum_row->info()->dimension(0), 0);
117 AccessWindowHorizontal vector_sum_col_access(vector_sum_col->info(), 0, num_elems_processed_per_iteration);
118
119 update_window_and_padding(win,
120 vector_sum_col_access,
121 vector_sum_row_access,
122 mm_result_access);
123 }
124 else if(a_offset == 0 && b_offset != 0)
125 {
126 AccessWindowStatic vector_sum_row_access(vector_sum_row->info(), 0, 0, vector_sum_row->info()->dimension(0), 0);
127
128 update_window_and_padding(win,
129 vector_sum_row_access,
130 mm_result_access);
131 }
132 else if(a_offset != 0 && b_offset == 0)
133 {
134 AccessWindowHorizontal vector_sum_col_access(vector_sum_col->info(), 0, num_elems_processed_per_iteration);
135
136 update_window_and_padding(win,
137 vector_sum_col_access,
138 mm_result_access);
139 }
140 else
141 {
142 update_window_and_padding(win,
143 mm_result_access);
144 }
145
146 INEKernel::configure(win);
147}
148
149void NEGEMMLowpOffsetContributionKernel::run(const Window &window, const ThreadInfo &info)
150{
151 ARM_COMPUTE_UNUSED(info);
152 ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
153 ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(INEKernel::window(), window);
154
155 Window collapsed_window = window.collapse_if_possible(IKernel::window(), Window::DimZ);
156
157 if(_a_offset != 0 && _b_offset != 0) // true, true
158 {
159 // Set window for vector_sum_col
160 Window win_vector_sum_col(collapsed_window);
161 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
162 if(!_slide_vector_sum_col)
163 {
164 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
165 }
166
167 // Set window for vector_sum_row
168 Window win_vector_sum_row(collapsed_window);
169 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
170 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
171
172 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
173 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
174 Iterator mm_result(_mm_result, window);
175
176 execute_window_loop(window, [&](const Coordinates & id)
177 {
178 // Compute the leftover term due to a_offset.
179 int32x4x4_t a_offset_term_s32 =
180 {
181 {
182 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
183 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
184 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
185 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
186 }
187 };
188
189 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
190 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
191 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
192 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
193
194 // Compute the leftover term due to b_offset.
195 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr()) + id.y());
196 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
197
198 // Add a_offset_term_s32 and b_offset_term_s32
199 int32x4x4_t offset_term_s32 =
200 {
201 {
202 vdupq_n_s32(_k_offset),
203 vdupq_n_s32(_k_offset),
204 vdupq_n_s32(_k_offset),
205 vdupq_n_s32(_k_offset)
206 }
207 };
208
209 offset_term_s32.val[0] = vaddq_s32(offset_term_s32.val[0], vaddq_s32(a_offset_term_s32.val[0], b_offset_term_s32));
210 offset_term_s32.val[1] = vaddq_s32(offset_term_s32.val[1], vaddq_s32(a_offset_term_s32.val[1], b_offset_term_s32));
211 offset_term_s32.val[2] = vaddq_s32(offset_term_s32.val[2], vaddq_s32(a_offset_term_s32.val[2], b_offset_term_s32));
212 offset_term_s32.val[3] = vaddq_s32(offset_term_s32.val[3], vaddq_s32(a_offset_term_s32.val[3], b_offset_term_s32));
213
214 int32x4x4_t in_s32 =
215 {
216 {
217 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
218 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
219 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
220 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
221 }
222 };
223
224 // Add the offset terms to GEMM's result
225 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
226 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
227 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
228 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
229
230 // Store the result with the offset contribution
231 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
232 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
233 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
234 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
235 },
236 vector_sum_col, vector_sum_row, mm_result);
237 }
238 else if((_a_offset == 0) && (_b_offset != 0)) // false, true
239 {
240 // Set window for vector_sum_row
241 Window win_vector_sum_row(collapsed_window);
242 win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
243 win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
244
245 Iterator vector_sum_row(_vector_sum_row, win_vector_sum_row);
246 Iterator mm_result(_mm_result, window);
247
248 execute_window_loop(window, [&](const Coordinates & id)
249 {
250 // Compute the leftover term due to b_offset.
251 int32x4_t b_offset_term_s32 = vld1q_dup_s32(reinterpret_cast<const int32_t *>(vector_sum_row.ptr()) + id.y());
252 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, _b_offset);
253
254 int32x4x4_t in_s32 =
255 {
256 {
257 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
258 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
259 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
260 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
261 }
262 };
263
264 // Add the offset terms to GEMM's result
265 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32);
266 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32);
267 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32);
268 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32);
269
270 // Store the result with the offset contribution
271 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
272 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
273 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
274 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
275 },
276 vector_sum_row, mm_result);
277 }
278 else if((_a_offset != 0) && (_b_offset == 0)) // true, false
279 {
280 // Set window for vector_sum_col
281 Window win_vector_sum_col(collapsed_window);
282 win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
283 if(!_slide_vector_sum_col)
284 {
285 win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
286 }
287
288 Iterator vector_sum_col(_vector_sum_col, win_vector_sum_col);
289 Iterator mm_result(_mm_result, window);
290
291 execute_window_loop(window, [&](const Coordinates & id)
292 {
293 // Compute the leftover term due to a_offset.
294 int32x4x4_t a_offset_term_s32 =
295 {
296 {
297 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 0),
298 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 4),
299 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 8),
300 vld1q_s32(reinterpret_cast<const int32_t *>(vector_sum_col.ptr()) + 12)
301 }
302 };
303
304 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], _a_offset);
305 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], _a_offset);
306 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], _a_offset);
307 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], _a_offset);
308
309 int32x4x4_t in_s32 =
310 {
311 {
312 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 0),
313 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 4),
314 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 8),
315 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result.ptr()) + 12)
316 }
317 };
318
319 // Add the offset terms to GEMM's result
320 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
321 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
322 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
323 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
324
325 // Store the result with the offset contribution
326 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 0, in_s32.val[0]);
327 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 4, in_s32.val[1]);
328 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 8, in_s32.val[2]);
329 vst1q_s32(reinterpret_cast<int32_t *>(mm_result.ptr()) + 12, in_s32.val[3]);
330 },
331 vector_sum_col, mm_result);
332 }
333 else // false, false
334 {
335 // No offset contribution from matrix A and matrix B
336 return;
337 }
338}