xref: /aosp_15_r20/external/ComputeLibrary/src/cpu/kernels/CpuGemmLowpOffsetContributionKernel.cpp (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2022 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 "src/cpu/kernels/CpuGemmLowpOffsetContributionKernel.h"
25 
26 #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"
34 #include "src/core/helpers/AutoConfiguration.h"
35 #include "src/core/helpers/WindowHelpers.h"
36 
37 #include <arm_neon.h>
38 
39 namespace arm_compute
40 {
41 namespace cpu
42 {
43 namespace kernels
44 {
45 namespace
46 {
validate_arguments(const ITensorInfo * mm_result,const ITensorInfo * vector_sum_col,const ITensorInfo * vector_sum_row,int32_t a_offset,int32_t b_offset)47 Status validate_arguments(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
48                           int32_t a_offset, int32_t b_offset)
49 {
50     ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(mm_result, 1, DataType::S32);
51 
52     // If a_offset == 0, vector_sum_col can be a nullptr
53     if(a_offset != 0)
54     {
55         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_col, 1, DataType::S32);
56         ARM_COMPUTE_RETURN_ERROR_ON(vector_sum_col->dimension(0) != mm_result->dimension(0));
57     }
58 
59     // If b_offset == 0, vector_sum_row can be a nullptr
60     if(b_offset != 0)
61     {
62         ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(vector_sum_row, 1, DataType::S32);
63 
64         // Check if input is a 3D reinterpretation
65         const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x();
66 
67         // Validate input
68         ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2)));
69         ARM_COMPUTE_RETURN_ERROR_ON(!reinterpret_as_3d && vector_sum_row->dimension(0) != mm_result->dimension(1));
70 
71         TensorShape output_shape = mm_result->tensor_shape();
72         if(output_shape.num_dimensions() > 1)
73         {
74             const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
75 
76             TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
77             vector_sum_row_shape.collapse_from(1);
78             output_shape.collapse_from(output_batch_idx);
79 
80             ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_row_shape[1] != output_shape[output_batch_idx],
81                                             "mm_result tensor must have the same number of batches of output tensor");
82 
83             if(a_offset != 0)
84             {
85                 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
86                 vector_sum_col_shape.collapse_from(1);
87 
88                 ARM_COMPUTE_RETURN_ERROR_ON_MSG(vector_sum_col_shape[1] != 1 && vector_sum_col_shape[1] != vector_sum_row_shape[1],
89                                                 "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");
90             }
91         }
92     }
93 
94     return Status{};
95 }
96 
run_offset_contribution(const Window & window,ITensor * mm_result,const ITensor * vector_sum_col,const ITensor * vector_sum_row,int32_t a_offset,int32_t b_offset,int32_t k_offset,bool slide_vector_sum_col,bool is_gemm3d)97 void run_offset_contribution(const Window &window,
98                              ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row,
99                              int32_t a_offset, int32_t b_offset, int32_t k_offset, bool slide_vector_sum_col, bool is_gemm3d)
100 {
101     Window collapsed_window = window.collapse_if_possible(window, Window::DimZ);
102     collapsed_window.set(Window::DimX, Window::Dimension(0, 1, 1));
103 
104     const int height_input = is_gemm3d ? mm_result->info()->dimension(1) : 0;
105     const int depth_input  = is_gemm3d ? mm_result->info()->dimension(2) : 1;
106 
107     const int window_start_x = window.x().start();
108     const int window_end_x   = window.x().end();
109     const int window_step_x  = 16;
110 
111     // if vector_sum_col is nullptr then stride_y is 0, else get stride_y
112     const size_t sum_col_stride_y = (vector_sum_col != nullptr) ? (vector_sum_col->info()->strides_in_bytes().y()) : 0;
113     Iterator     mm_result_it(mm_result, collapsed_window);
114 
115     if((a_offset != 0) && (b_offset != 0) && (vector_sum_col != nullptr) && (vector_sum_row != nullptr)) // true, true
116     {
117         // Set window for vector_sum_col
118         Window win_vector_sum_col(collapsed_window);
119         win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
120         win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
121 
122         // Set window for vector_sum_row
123         Window win_vector_sum_row(collapsed_window);
124         win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
125         win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
126         win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
127 
128         Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
129         Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
130 
131         const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
132 
133         // Offset in case vector_sum_col is batched
134         const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
135 
136         execute_window_loop(collapsed_window, [&](const Coordinates & id)
137         {
138             const int    batch_id           = id.z() / depth_input;
139             const size_t batch_offset_col   = batch_id * (sum_col_stride_y );
140             auto         vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_offset_col + batch_id * vector_sum_col_batch_offset);
141             auto         mm_result_ptr      = reinterpret_cast<int32_t *>(mm_result_it.ptr());
142 
143             // Compute the leftover term due to b_offset.
144             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);
145             b_offset_term_s32 *= b_offset;
146 
147             const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
148 
149             int x = window_start_x;
150             for(; x <= (window_end_x - window_step_x); x += window_step_x)
151             {
152                 // Compute the leftover term due to a_offset.
153                 int32x4x4_t a_offset_term_s32 =
154                 {
155                     {
156                         vld1q_s32(vector_sum_col_ptr + x + 0),
157                         vld1q_s32(vector_sum_col_ptr + x + 4),
158                         vld1q_s32(vector_sum_col_ptr + x + 8),
159                         vld1q_s32(vector_sum_col_ptr + x + 12)
160                     }
161                 };
162 
163                 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
164                 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
165                 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
166                 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
167 
168                 // Add a_offset_term_s32 and b_offset_term_s32
169                 int32x4x4_t offset_term_s32 =
170                 {
171                     {
172                         vdupq_n_s32(k_offset),
173                         vdupq_n_s32(k_offset),
174                         vdupq_n_s32(k_offset),
175                         vdupq_n_s32(k_offset)
176                     }
177                 };
178 
179                 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));
180                 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));
181                 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));
182                 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));
183 
184                 int32x4x4_t in_s32 =
185                 {
186                     {
187                         vld1q_s32(mm_result_ptr + x + 0),
188                         vld1q_s32(mm_result_ptr + x + 4),
189                         vld1q_s32(mm_result_ptr + x + 8),
190                         vld1q_s32(mm_result_ptr + x + 12)
191                     }
192                 };
193 
194                 // Add the offset terms to GEMM's result
195                 in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
196                 in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
197                 in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
198                 in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
199 
200                 // Store the result with the offset contribution
201                 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
202                 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
203                 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
204                 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
205             }
206 
207             // Left-overs loop
208             for(; x < window_end_x; ++x)
209             {
210                 // Compute the leftover term due to a_offset.
211                 int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
212 
213                 a_offset_term_s32 *= a_offset;
214 
215                 // Add the offset terms to GEMM's result
216                 // Store the result with the offset contribution
217                 mm_result_ptr[x] += k_offset + a_offset_term_s32 + b_offset_term_s32;
218             }
219         },
220         vector_sum_col_it, vector_sum_row_it, mm_result_it);
221     }
222     else if((a_offset == 0) && (b_offset != 0) && (vector_sum_row != nullptr)) // false, true
223     {
224         ARM_COMPUTE_ERROR_ON_NULLPTR(vector_sum_row);
225 
226         // Set window for vector_sum_row
227         Window win_vector_sum_row(collapsed_window);
228         win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
229         win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
230         win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
231 
232         Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
233 
234         const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
235 
236         execute_window_loop(collapsed_window, [&](const Coordinates & id)
237         {
238             const int batch_id      = id.z() / depth_input;
239             auto      mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
240 
241             // Compute the leftover term due to b_offset.
242             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);
243             b_offset_term_s32 *= b_offset;
244 
245             const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
246 
247             int x = window_start_x;
248             for(; x <= (window_end_x - window_step_x); x += window_step_x)
249             {
250                 int32x4x4_t in_s32 =
251                 {
252                     {
253                         vld1q_s32(mm_result_ptr + x + 0),
254                         vld1q_s32(mm_result_ptr + x + 4),
255                         vld1q_s32(mm_result_ptr + x + 8),
256                         vld1q_s32(mm_result_ptr + x + 12)
257                     }
258                 };
259 
260                 // Add the offset terms to GEMM's result
261                 in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32_vec);
262                 in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32_vec);
263                 in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32_vec);
264                 in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32_vec);
265 
266                 // Store the result with the offset contribution
267                 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
268                 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
269                 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
270                 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
271             }
272 
273             // Left-overs loop
274             for(; x < window_end_x; ++x)
275             {
276                 // Add the offset terms to GEMM's result
277                 // Store the result with the offset contribution
278                 mm_result_ptr[x] += b_offset_term_s32;
279             }
280         },
281         vector_sum_row_it, mm_result_it);
282     }
283     else if((a_offset != 0) && (b_offset == 0) && (vector_sum_col != nullptr)) // true, false
284     {
285         // Set window for vector_sum_col
286         Window win_vector_sum_col(collapsed_window);
287         win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
288         win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
289 
290         Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
291 
292         // Offset in case vector_sum_col is batched
293         const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
294 
295         execute_window_loop(collapsed_window, [&](const Coordinates & id)
296         {
297             const int    batch_id           = id.z() / depth_input;
298             const size_t batch_offset_col   = batch_id * (sum_col_stride_y ); // Value to offset vector_sum_col_ptr to allow for iteration of y values in tensor
299             auto         vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_offset_col + batch_id * vector_sum_col_batch_offset);
300             auto         mm_result_ptr      = reinterpret_cast<int32_t *>(mm_result_it.ptr());
301 
302             int x = window_start_x;
303             for(; x <= (window_end_x - window_step_x); x += window_step_x)
304             {
305                 // Compute the leftover term due to a_offset.
306                 int32x4x4_t a_offset_term_s32 =
307                 {
308                     {
309                         vld1q_s32(vector_sum_col_ptr + x + 0),
310                         vld1q_s32(vector_sum_col_ptr + x + 4),
311                         vld1q_s32(vector_sum_col_ptr + x + 8),
312                         vld1q_s32(vector_sum_col_ptr + x + 12)
313                     }
314                 };
315 
316                 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
317                 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
318                 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
319                 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
320 
321                 int32x4x4_t in_s32 =
322                 {
323                     {
324                         vld1q_s32(mm_result_ptr + x + 0),
325                         vld1q_s32(mm_result_ptr + x + 4),
326                         vld1q_s32(mm_result_ptr + x + 8),
327                         vld1q_s32(mm_result_ptr + x + 12)
328                     }
329                 };
330 
331                 // Add the offset terms to GEMM's result
332                 in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
333                 in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
334                 in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
335                 in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
336 
337                 // Store the result with the offset contribution
338                 vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
339                 vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
340                 vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
341                 vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
342             }
343 
344             // Left-overs loop
345             for(; x < window_end_x; ++x)
346             {
347                 // Compute the leftover term due to a_offset.
348                 const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
349 
350                 // Add the offset terms to GEMM's result
351                 // Store the result with the offset contribution
352                 mm_result_ptr[x] += a_offset_term_s32 * a_offset;
353             }
354         },
355         vector_sum_col_it, mm_result_it);
356     }
357     else // false, false
358     {
359         // No offset contribution from matrix A and matrix B
360         return;
361     }
362 }
363 } // namespace
364 
configure(ITensorInfo * mm_result,ITensorInfo * vector_sum_col,ITensorInfo * vector_sum_row,int32_t k,int32_t a_offset,int32_t b_offset)365 void CpuGemmLowpOffsetContributionKernel::configure(ITensorInfo *mm_result, ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, int32_t k, int32_t a_offset, int32_t b_offset)
366 {
367     // Perform validate step
368     ARM_COMPUTE_UNUSED(vector_sum_row);
369     ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
370     ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
371 
372     _a_offset = a_offset;
373     _b_offset = b_offset;
374     _k_offset = a_offset * b_offset * k;
375 
376     // If a_offset == 0, vector_sum_col can be a nullptr
377     if(a_offset != 0)
378     {
379         // Check if vector_sum_col_shape should be slidden or not
380         // 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
381         // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
382         _slide_vector_sum_col = vector_sum_col->tensor_shape().num_dimensions() > 1;
383     }
384 
385     // Configure kernel window
386     Window win = calculate_max_window(*mm_result, Steps());
387     ICpuKernel::configure(win);
388 }
389 
validate(const ITensorInfo * mm_result,const ITensorInfo * vector_sum_col,const ITensorInfo * vector_sum_row,int32_t a_offset,int32_t b_offset)390 Status CpuGemmLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
391                                                      int32_t a_offset, int32_t b_offset)
392 {
393     ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
394     return Status{};
395 }
396 
run_op(ITensorPack & tensors,const Window & window,const ThreadInfo & info)397 void CpuGemmLowpOffsetContributionKernel::run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info)
398 {
399     ARM_COMPUTE_UNUSED(info);
400     ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(this);
401     ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(ICpuKernel::window(), window);
402 
403     auto vector_sum_col = tensors.get_const_tensor(TensorType::ACL_SRC_0);
404     auto vector_sum_row = tensors.get_const_tensor(TensorType::ACL_SRC_1);
405     auto mm_result      = tensors.get_tensor(TensorType::ACL_DST);
406 
407     // Check if input is a 3D reinterpretation
408     const bool reinterpret_as_3d = vector_sum_row != nullptr
409                                    && mm_result->info()->num_dimensions() > 1
410                                    && mm_result->info()->tensor_shape().y() != vector_sum_row->info()->tensor_shape().x();
411 
412     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);
413 }
414 
name() const415 const char *CpuGemmLowpOffsetContributionKernel::name() const
416 {
417     return "CpuGemmLowpOffsetContributionKernel";
418 }
419 } // namespace kernels
420 } // namespace cpu
421 } // namespace arm_compute