Compute Library
 21.11
CpuGemmLowpOffsetContributionKernel.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2021 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  */
25 
26 #include "arm_compute/core/Error.h"
30 #include "arm_compute/core/Types.h"
31 #include "arm_compute/core/Utils.h"
36 
37 #include <arm_neon.h>
38 
39 namespace arm_compute
40 {
41 namespace cpu
42 {
43 namespace kernels
44 {
45 namespace
46 {
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 {
51 
52  // If a_offset == 0, vector_sum_col can be a nullptr
53  if(a_offset != 0)
54  {
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  {
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 
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  Iterator mm_result_it(mm_result, collapsed_window);
112 
113  if((a_offset != 0) && (b_offset != 0) && (vector_sum_col != nullptr) && (vector_sum_row != nullptr)) // true, true
114  {
115  // Set window for vector_sum_col
116  Window win_vector_sum_col(collapsed_window);
117  win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
118  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
119 
120  // Set window for vector_sum_row
121  Window win_vector_sum_row(collapsed_window);
122  win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
123  win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
124  win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
125 
126  Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
127  Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
128 
129  const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
130 
131  // Offset in case vector_sum_col is batched
132  const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
133 
134  execute_window_loop(collapsed_window, [&](const Coordinates & id)
135  {
136  const int batch_id = id.z() / depth_input;
137  auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
138  auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
139 
140  // Compute the leftover term due to b_offset.
141  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);
142  b_offset_term_s32 *= b_offset;
143 
144  const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
145 
146  int x = window_start_x;
147  for(; x <= (window_end_x - window_step_x); x += window_step_x)
148  {
149  // Compute the leftover term due to a_offset.
150  int32x4x4_t a_offset_term_s32 =
151  {
152  {
153  vld1q_s32(vector_sum_col_ptr + x + 0),
154  vld1q_s32(vector_sum_col_ptr + x + 4),
155  vld1q_s32(vector_sum_col_ptr + x + 8),
156  vld1q_s32(vector_sum_col_ptr + x + 12)
157  }
158  };
159 
160  a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
161  a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
162  a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
163  a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
164 
165  // Add a_offset_term_s32 and b_offset_term_s32
166  int32x4x4_t offset_term_s32 =
167  {
168  {
169  vdupq_n_s32(k_offset),
170  vdupq_n_s32(k_offset),
171  vdupq_n_s32(k_offset),
172  vdupq_n_s32(k_offset)
173  }
174  };
175 
176  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));
177  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));
178  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));
179  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));
180 
181  int32x4x4_t in_s32 =
182  {
183  {
184  vld1q_s32(mm_result_ptr + x + 0),
185  vld1q_s32(mm_result_ptr + x + 4),
186  vld1q_s32(mm_result_ptr + x + 8),
187  vld1q_s32(mm_result_ptr + x + 12)
188  }
189  };
190 
191  // Add the offset terms to GEMM's result
192  in_s32.val[0] = vaddq_s32(in_s32.val[0], offset_term_s32.val[0]);
193  in_s32.val[1] = vaddq_s32(in_s32.val[1], offset_term_s32.val[1]);
194  in_s32.val[2] = vaddq_s32(in_s32.val[2], offset_term_s32.val[2]);
195  in_s32.val[3] = vaddq_s32(in_s32.val[3], offset_term_s32.val[3]);
196 
197  // Store the result with the offset contribution
198  vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
199  vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
200  vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
201  vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
202  }
203 
204  // Left-overs loop
205  for(; x < window_end_x; ++x)
206  {
207  // Compute the leftover term due to a_offset.
208  int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
209 
210  a_offset_term_s32 *= a_offset;
211 
212  // Add the offset terms to GEMM's result
213  // Store the result with the offset contribution
214  mm_result_ptr[x] += k_offset + a_offset_term_s32 + b_offset_term_s32;
215  }
216  },
217  vector_sum_col_it, vector_sum_row_it, mm_result_it);
218  }
219  else if((a_offset == 0) && (b_offset != 0) && (vector_sum_row != nullptr)) // false, true
220  {
221  ARM_COMPUTE_ERROR_ON_NULLPTR(vector_sum_row);
222 
223  // Set window for vector_sum_row
224  Window win_vector_sum_row(collapsed_window);
225  win_vector_sum_row.set(Window::DimX, Window::Dimension(0, 0, 0));
226  win_vector_sum_row.set(Window::DimY, Window::Dimension(0, 0, 0));
227  win_vector_sum_row.set(Window::DimZ, Window::Dimension(0, 0, 0));
228 
229  Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
230 
231  const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
232 
233  execute_window_loop(collapsed_window, [&](const Coordinates & id)
234  {
235  const int batch_id = id.z() / depth_input;
236  auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
237 
238  // Compute the leftover term due to b_offset.
239  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);
240  b_offset_term_s32 *= b_offset;
241 
242  const int32x4_t b_offset_term_s32_vec = vdupq_n_s32(b_offset_term_s32);
243 
244  int x = window_start_x;
245  for(; x <= (window_end_x - window_step_x); x += window_step_x)
246  {
247  int32x4x4_t in_s32 =
248  {
249  {
250  vld1q_s32(mm_result_ptr + x + 0),
251  vld1q_s32(mm_result_ptr + x + 4),
252  vld1q_s32(mm_result_ptr + x + 8),
253  vld1q_s32(mm_result_ptr + x + 12)
254  }
255  };
256 
257  // Add the offset terms to GEMM's result
258  in_s32.val[0] = vaddq_s32(in_s32.val[0], b_offset_term_s32_vec);
259  in_s32.val[1] = vaddq_s32(in_s32.val[1], b_offset_term_s32_vec);
260  in_s32.val[2] = vaddq_s32(in_s32.val[2], b_offset_term_s32_vec);
261  in_s32.val[3] = vaddq_s32(in_s32.val[3], b_offset_term_s32_vec);
262 
263  // Store the result with the offset contribution
264  vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
265  vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
266  vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
267  vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
268  }
269 
270  // Left-overs loop
271  for(; x < window_end_x; ++x)
272  {
273  // Add the offset terms to GEMM's result
274  // Store the result with the offset contribution
275  mm_result_ptr[x] += b_offset_term_s32;
276  }
277  },
278  vector_sum_row_it, mm_result_it);
279  }
280  else if((a_offset != 0) && (b_offset == 0) && (vector_sum_col != nullptr)) // true, false
281  {
282  // Set window for vector_sum_col
283  Window win_vector_sum_col(collapsed_window);
284  win_vector_sum_col.set(Window::DimY, Window::Dimension(0, 0, 0));
285  win_vector_sum_col.set(Window::DimZ, Window::Dimension(0, 0, 0));
286 
287  Iterator vector_sum_col_it(vector_sum_col, win_vector_sum_col);
288 
289  // Offset in case vector_sum_col is batched
290  const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
291 
292  execute_window_loop(collapsed_window, [&](const Coordinates & id)
293  {
294  const int batch_id = id.z() / depth_input;
295  auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
296  auto mm_result_ptr = reinterpret_cast<int32_t *>(mm_result_it.ptr());
297 
298  int x = window_start_x;
299  for(; x <= (window_end_x - window_step_x); x += window_step_x)
300  {
301  // Compute the leftover term due to a_offset.
302  int32x4x4_t a_offset_term_s32 =
303  {
304  {
305  vld1q_s32(vector_sum_col_ptr + x + 0),
306  vld1q_s32(vector_sum_col_ptr + x + 4),
307  vld1q_s32(vector_sum_col_ptr + x + 8),
308  vld1q_s32(vector_sum_col_ptr + x + 12)
309  }
310  };
311 
312  a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
313  a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
314  a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
315  a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
316 
317  int32x4x4_t in_s32 =
318  {
319  {
320  vld1q_s32(mm_result_ptr + x + 0),
321  vld1q_s32(mm_result_ptr + x + 4),
322  vld1q_s32(mm_result_ptr + x + 8),
323  vld1q_s32(mm_result_ptr + x + 12)
324  }
325  };
326 
327  // Add the offset terms to GEMM's result
328  in_s32.val[0] = vaddq_s32(in_s32.val[0], a_offset_term_s32.val[0]);
329  in_s32.val[1] = vaddq_s32(in_s32.val[1], a_offset_term_s32.val[1]);
330  in_s32.val[2] = vaddq_s32(in_s32.val[2], a_offset_term_s32.val[2]);
331  in_s32.val[3] = vaddq_s32(in_s32.val[3], a_offset_term_s32.val[3]);
332 
333  // Store the result with the offset contribution
334  vst1q_s32(mm_result_ptr + x + 0, in_s32.val[0]);
335  vst1q_s32(mm_result_ptr + x + 4, in_s32.val[1]);
336  vst1q_s32(mm_result_ptr + x + 8, in_s32.val[2]);
337  vst1q_s32(mm_result_ptr + x + 12, in_s32.val[3]);
338  }
339 
340  // Left-overs loop
341  for(; x < window_end_x; ++x)
342  {
343  // Compute the leftover term due to a_offset.
344  const int32_t a_offset_term_s32 = *(vector_sum_col_ptr + x);
345 
346  // Add the offset terms to GEMM's result
347  // Store the result with the offset contribution
348  mm_result_ptr[x] += a_offset_term_s32 * a_offset;
349  }
350  },
351  vector_sum_col_it, mm_result_it);
352  }
353  else // false, false
354  {
355  // No offset contribution from matrix A and matrix B
356  return;
357  }
358 }
359 } // namespace
360 
361 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)
362 {
363  // Perform validate step
364  ARM_COMPUTE_UNUSED(vector_sum_row);
365  ARM_COMPUTE_ERROR_ON_NULLPTR(mm_result);
366  ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
367 
368  _a_offset = a_offset;
369  _b_offset = b_offset;
370  _k_offset = a_offset * b_offset * k;
371 
372  // If a_offset == 0, vector_sum_col can be a nullptr
373  if(a_offset != 0)
374  {
375  // Check if vector_sum_col_shape should be slidden or not
376  // 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
377  // This scenario can happen when the the matrix multiplication is used to perform a convolution operation
378  _slide_vector_sum_col = vector_sum_col->tensor_shape().num_dimensions() > 1;
379  }
380 
381  // Configure kernel window
382  Window win = calculate_max_window(*mm_result, Steps());
383  ICpuKernel::configure(win);
384 }
385 
386 Status CpuGemmLowpOffsetContributionKernel::validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row,
387  int32_t a_offset, int32_t b_offset)
388 {
389  ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, a_offset, b_offset));
390  return Status{};
391 }
392 
394 {
395  ARM_COMPUTE_UNUSED(info);
398 
399  auto vector_sum_col = tensors.get_const_tensor(TensorType::ACL_SRC_0);
400  auto vector_sum_row = tensors.get_const_tensor(TensorType::ACL_SRC_1);
401  auto mm_result = tensors.get_tensor(TensorType::ACL_DST);
402 
403  // Check if input is a 3D reinterpretation
404  const bool reinterpret_as_3d = vector_sum_row != nullptr
405  && mm_result->info()->num_dimensions() > 1
406  && mm_result->info()->tensor_shape().y() != vector_sum_row->info()->tensor_shape().x();
407 
408  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);
409 }
410 
412 {
413  return "CpuGemmLowpOffsetContributionKernel";
414 }
415 } // namespace kernels
416 } // namespace cpu
417 } // namespace arm_compute
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
Window calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
const Window & window() const
The maximum window the kernel can be executed on.
Definition: IKernel.cpp:28
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
Definition: Error.h:204
Store the tensor&#39;s metadata.
Definition: ITensorInfo.h:40
#define ARM_COMPUTE_ERROR_THROW_ON(status)
Definition: Error.h:455
Status class.
Definition: Error.h:52
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
Definition: Error.h:296
Copyright (c) 2017-2021 Arm Limited.
1 channel, 1 S32 per channel
const ITensor * get_const_tensor(int id) const
Get constant tensor of a given id.
Definition: ITensorPack.cpp:54
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
Definition: Window.h:43
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
Class to describe a number of elements in each dimension.
Definition: Steps.h:40
void configure(ITensorInfo *mm_result, ITensorInfo *vector_sum_col, ITensorInfo *vector_sum_row, int32_t k, int32_t a_offset, int32_t b_offset)
Initialise the kernel&#39;s input and output.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor&#39;s metadata.
static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, int32_t a_offset, int32_t b_offset)
Static function to check if given info will lead to a valid configuration.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
Definition: Validate.h:915
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
Definition: Window.h:45
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
ITensor * get_tensor(int id)
Get tensor of a given id from the pac.
Definition: ITensorPack.cpp:64
Information about executing thread and CPU.
Definition: CPPTypes.h:158
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
Definition: Window.h:47
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
Definition: Dimensions.h:143
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
Definition: Validate.h:788
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
Definition: Error.h:244
Tensor packing service.
Definition: ITensorPack.h:39
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
Definition: Validate.h:157
void execute_window_loop(const Window &w, L &&lambda_function, Ts &&... iterators)
Iterate through the passed window, automatically adjusting the iterators and calling the lambda_funct...
Definition: Helpers.inl:77
Describe a multidimensional execution window.
Definition: Window.h:39
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)
Definition: Validate.h:201