49 inline int32x4x4_t load_results_input(
const Iterator &mm_result_it, int32_t x)
54 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 0),
55 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 4),
56 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 8),
57 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 12)
62 inline int32x4x4_t load(
const int32_t *ptr, int32_t x)
67 vld1q_s32(ptr + x + 0),
68 vld1q_s32(ptr + x + 4),
69 vld1q_s32(ptr + x + 8),
70 vld1q_s32(ptr + x + 12)
75 inline int32x4x4_t add_s32(int32x4x4_t a, int32x4_t
b)
80 vaddq_s32(a.val[0], b),
81 vaddq_s32(a.val[1], b),
82 vaddq_s32(a.val[2], b),
83 vaddq_s32(a.val[3], b)
88 inline int32x4x4_t add_s32(int32x4x4_t a, int32x4x4_t
b)
93 vaddq_s32(a.val[0], b.val[0]),
94 vaddq_s32(a.val[1], b.val[1]),
95 vaddq_s32(a.val[2], b.val[2]),
96 vaddq_s32(a.val[3], b.val[3])
101 inline int32x4x4_t mul_s32(int32x4x4_t &a, int32_t mul_scalar)
106 vmulq_n_s32(a.val[0], mul_scalar),
107 vmulq_n_s32(a.val[1], mul_scalar),
108 vmulq_n_s32(a.val[2], mul_scalar),
109 vmulq_n_s32(a.val[3], mul_scalar)
114 inline int32x4x4_t mul_s32(int32x4x4_t &a,
const int32_t *multilpier)
119 vmulq_s32(a.val[0], vld1q_s32(multilpier)),
120 vmulq_s32(a.val[1], vld1q_s32(multilpier + 4)),
121 vmulq_s32(a.val[2], vld1q_s32(multilpier + 8)),
122 vmulq_s32(a.val[3], vld1q_s32(multilpier + 12))
127 inline int32x4x4_t get_a_offset(
const int32_t *vector_sum_col_ptr, int32_t a_offset, int32_t x)
129 int32x4x4_t a_offset_term_s32 = load(vector_sum_col_ptr, x);
131 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
132 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
133 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
134 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
135 return a_offset_term_s32;
138 inline int32x4_t get_b_offset(
const int32_t *vector_sum_row_ptr, int32_t b_offset)
140 int32x4_t b_offset_term_s32 = vld1q_dup_s32(vector_sum_row_ptr);
141 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, b_offset);
142 return b_offset_term_s32;
145 inline int32x4x4_t get_k_offset(int32_t k_offset)
150 vdupq_n_s32(k_offset),
151 vdupq_n_s32(k_offset),
152 vdupq_n_s32(k_offset),
153 vdupq_n_s32(k_offset)
158 inline uint8x16_t finalize_quantization_floating_point(int32x4x4_t &in_s32, int32x4_t result_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8,
bool is_bounded_relu)
160 const static int32x4_t zero_s32 = vdupq_n_s32(0);
163 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
164 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
165 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
166 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
169 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
170 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
171 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
172 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
175 const int16x8x2_t in_s16 =
178 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
179 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
184 uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1]));
188 out_u8 = vmaxq_u8(out_u8, min_u8);
189 out_u8 = vminq_u8(out_u8, max_u8);
195 inline int8x16_t finalize_quantization_floating_point(int32x4x4_t &in_s32, int32x4_t result_shift_s32, int8x16_t min_s8, int8x16_t max_s8,
bool is_bounded_relu)
197 const static int32x4_t zero_s32 = vdupq_n_s32(0);
200 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
201 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
202 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
203 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
206 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
207 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
208 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
209 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
212 const int16x8x2_t in_s16 =
215 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
216 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
221 int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1]));
225 out_s8 = vmaxq_s8(out_s8, min_s8);
226 out_s8 = vminq_s8(out_s8, max_s8);
232 inline int8x16_t finalize_quantization_floating_point(int32x4x4_t &in_s32, int32x4x4_t result_shift_s32, int8x16_t min_s8, int8x16_t max_s8,
bool is_bounded_relu)
234 const static int32x4_t zero_s32 = vdupq_n_s32(0);
237 in_s32.val[0] = vshlq_s32(in_s32.val[0], vnegq_s32(result_shift_s32.val[0]));
238 in_s32.val[1] = vshlq_s32(in_s32.val[1], vnegq_s32(result_shift_s32.val[1]));
239 in_s32.val[2] = vshlq_s32(in_s32.val[2], vnegq_s32(result_shift_s32.val[2]));
240 in_s32.val[3] = vshlq_s32(in_s32.val[3], vnegq_s32(result_shift_s32.val[3]));
243 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
244 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
245 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
246 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
249 const int16x8x2_t in_s16 =
252 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
253 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
258 int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1]));
262 out_s8 = vmaxq_s8(out_s8, min_s8);
263 out_s8 = vminq_s8(out_s8, max_s8);
269 template <
typename T>
273 using vtype =
typename wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128>;
276 inline Window get_win_vector_sum(
const Window &window)
278 Window win_vector_sum(window);
279 win_vector_sum.set(
Window::DimY, Window::Dimension(0, 0, 0));
280 win_vector_sum.set(
Window::DimZ, Window::Dimension(0, 0, 0));
281 return win_vector_sum;
284 inline Iterator get_vector_sum_col_it(
const Window &window,
const ITensor *vector_sum_col)
286 Iterator vector_sum_col_it(vector_sum_col, get_win_vector_sum(window));
287 return vector_sum_col_it;
290 inline Iterator get_vector_sum_row_it(
const Window &window,
const ITensor *vector_sum_row)
292 Window win_vector_sum_row = get_win_vector_sum(window);
293 win_vector_sum_row.set(
Window::DimX, Window::Dimension(0, 0, 0));
294 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
295 return vector_sum_row_it;
298 inline Iterator get_bias_it(
const Window &window,
const ITensor *bias)
300 Window win_bias(window);
303 Iterator bias_it(bias, win_bias);
307 template <
typename VT>
308 inline void run_offset_contribution_output_stage_window(
const int32_t *vector_sum_col_ptr,
const int32_t *vector_sum_row_ptr,
const int32_t *bias_ptr, Iterator mm_result_it, Iterator out_it,
309 const int32x4_t result_offset_s32,
const int32x4_t result_shift_s32,
310 typename VT::vtype min_vec,
typename VT::vtype max_vec,
311 int32_t a_offset, int32_t b_offset, int32_t k_offset,
312 int32_t multiplier, int32_t shift, int32_t
offset, int32_t min_bound, int32_t max_bound,
313 int window_step_x,
int window_start_x,
int window_end_x,
bool has_a_offset,
bool has_b_offset,
bool has_bias,
bool is_bounded_relu,
bool is_fixed_point)
315 int32x4x4_t offset_term_s32 = { 0, 0, 0, 0 };
319 offset_term_s32 = add_s32(offset_term_s32, result_offset_s32);
321 if(has_a_offset && has_b_offset)
323 offset_term_s32 = add_s32(offset_term_s32, get_k_offset(k_offset));
327 offset_term_s32 = add_s32(offset_term_s32, get_b_offset(vector_sum_row_ptr, b_offset));
330 int x = window_start_x;
331 for(; x <= (window_end_x - window_step_x); x += window_step_x)
333 int32x4x4_t in_s32 = load_results_input(mm_result_it, x);
337 in_s32 = add_s32(in_s32, get_a_offset(vector_sum_col_ptr, a_offset, x));
341 in_s32 = add_s32(in_s32, load(bias_ptr, x));
343 if(!is_fixed_point || has_b_offset)
345 in_s32 = add_s32(in_s32, offset_term_s32);
349 in_s32 = mul_s32(in_s32, multiplier);
354 wrapper::vstore(reinterpret_cast<typename VT::stype *>(out_it.ptr() + x),
355 finalize_quantization(in_s32, multiplier, shift, result_offset_s32, min_vec, max_vec, is_bounded_relu));
359 wrapper::vstore(reinterpret_cast<typename VT::stype *>(out_it.ptr() + x),
360 finalize_quantization_floating_point(in_s32, result_shift_s32, min_vec, max_vec, is_bounded_relu));
364 for(; x < window_end_x; ++x)
366 int32_t in_value = *(
reinterpret_cast<const int32_t *
>(mm_result_it.ptr()) + x) +
wrapper::vgetlane(offset_term_s32.val[0], 0);
370 in_value += (*(vector_sum_col_ptr + x) * a_offset);
374 in_value += *(bias_ptr + x);
380 *
reinterpret_cast<typename VT::stype *
>(out_it.ptr() + x) =
finalize_quantization(in_value, multiplier, shift, offset,
381 static_cast<typename VT::stype>(min_bound),
382 static_cast<typename VT::stype
>(max_bound), is_bounded_relu);
387 in_value = (in_value * multiplier) >> shift;
392 in_value =
static_cast<typename VT::stype
>(std::max<int32_t>(min_bound, std::min<int32_t>(max_bound, in_value)));
395 std::min<int32_t>(static_cast<int32_t>(std::numeric_limits<typename VT::stype>::max()), in_value)));
400 inline void run_offset_contribution_output_stage_window_symm(
const int32_t *vector_sum_col_ptr,
const int32_t *bias_ptr, Iterator mm_result_it, Iterator out_it,
401 const int32_t *result_multipliers,
const int32_t *result_shifts,
402 const int32x4_t result_offset, int8x16_t min_s8, int8x16_t max_s8,
403 int32_t a_offset, int32_t offset, int32_t min_bound, int32_t max_bound,
404 int window_step_x,
int window_start_x,
int window_end_x,
bool has_a_offset,
bool has_bias,
bool is_bounded_relu,
bool is_fixed_point)
406 int32x4x4_t offset_term_s32 = { 0, 0, 0, 0 };
410 offset_term_s32 = add_s32(offset_term_s32, result_offset);
413 int x = window_start_x;
414 for(; x <= (window_end_x - window_step_x); x += window_step_x)
416 int32x4x4_t in_s32 = load_results_input(mm_result_it, x);
420 in_s32 = add_s32(in_s32, get_a_offset(vector_sum_col_ptr, a_offset, x));
424 in_s32 = add_s32(in_s32, load(bias_ptr, x));
428 in_s32 = add_s32(in_s32, offset_term_s32);
429 in_s32 = mul_s32(in_s32, result_multipliers + x);
434 vst1q_s8(reinterpret_cast<int8_t *>(out_it.ptr() + x),
finalize_quantization_symm(in_s32, load(result_multipliers, x), load(result_shifts, x), result_offset, min_s8, max_s8, is_bounded_relu));
438 vst1q_s8(reinterpret_cast<int8_t *>(out_it.ptr() + x), finalize_quantization_floating_point(in_s32, load(result_shifts, x), min_s8, max_s8, is_bounded_relu));
442 for(; x < window_end_x; ++x)
444 int32_t in_value = *(
reinterpret_cast<const int32_t *
>(mm_result_it.ptr()) + x) +
wrapper::vgetlane(offset_term_s32.val[0], 0);
448 in_value += (*(vector_sum_col_ptr + x) * a_offset);
452 in_value += *(bias_ptr + x);
458 *(out_it.ptr() + x) =
finalize_quantization(in_value, result_multipliers[x], result_shifts[x], offset, static_cast<int8_t>(min_bound),
static_cast<int8_t
>(max_bound), is_bounded_relu);
463 in_value = (in_value * result_multipliers[x]) >> (-result_shifts[x]);
468 in_value =
static_cast<int8_t
>(std::max<int32_t>(min_bound, std::min<int32_t>(max_bound, in_value)));
470 *(out_it.ptr() + x) = static_cast<int8_t>(std::max<int32_t>(-128, std::min<int32_t>(127, in_value)));
475 template <
typename T>
476 void run_offset_contribution_output_stage(
const Window &window,
477 const ITensor *mm_result,
const ITensor *vector_sum_col,
const ITensor *vector_sum_row,
const ITensor *bias, ITensor *output,
478 int32_t a_offset, int32_t b_offset, int32_t k_offset,
bool slide_vector_sum_col,
479 GEMMLowpOutputStageInfo output_stage,
bool is_gemm3d,
bool is_bounded_relu,
bool is_fixed_point)
481 using ExactTagType =
typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
482 using Typer = VectorTyper<T>;
484 const int height_input = is_gemm3d ? mm_result->info()->dimension(1) : 0;
485 const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1;
487 const int32_t multiplier = output_stage.gemmlowp_multiplier;
488 const int32_t shift = output_stage.gemmlowp_shift;
489 const int32_t offset = output_stage.gemmlowp_offset;
490 const int32_t min_bound = output_stage.gemmlowp_min_bound;
491 const int32_t max_bound = output_stage.gemmlowp_max_bound;
493 const int32x4_t result_offset_s32 = vdupq_n_s32(offset);
494 const int32x4_t result_shift_s32 = vdupq_n_s32(is_fixed_point ? shift : -shift);
495 const auto min_vec =
wrapper::vdup_n(static_cast<T>(min_bound), ExactTagType{});
496 const auto max_vec =
wrapper::vdup_n(static_cast<T>(max_bound), ExactTagType{});
498 const int window_step_x = 16;
499 const auto window_start_x =
static_cast<int>(window.x().start());
500 const auto window_end_x =
static_cast<int>(window.x().end());
505 Window collapsed_window = win.collapse_if_possible(win,
Window::DimZ);
507 Iterator mm_result_it(mm_result, win);
508 Iterator out_it(output, win);
510 if((a_offset != 0) && (b_offset != 0))
515 Iterator vector_sum_col_it = get_vector_sum_col_it(collapsed_window, vector_sum_col);
516 Iterator vector_sum_row_it = get_vector_sum_row_it(collapsed_window, vector_sum_row);
518 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
521 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
525 Iterator bias_it = get_bias_it(collapsed_window, bias);
528 const int batch_id =
id.z() / depth_input;
529 const auto vector_sum_col_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
530 const auto vector_sum_row_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
531 +
id.y() + (
id.z() % depth_input) * height_input;
532 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr, vector_sum_row_ptr,
reinterpret_cast<const int32_t *
>(bias_it.ptr()),
535 result_offset_s32, result_shift_s32,
536 min_vec, max_vec, a_offset, b_offset, k_offset,
537 multiplier, shift, offset, min_bound, max_bound,
538 window_step_x, window_start_x, window_end_x,
true,
true,
true, is_bounded_relu, is_fixed_point);
540 vector_sum_col_it, vector_sum_row_it, bias_it, mm_result_it, out_it);
546 const int batch_id =
id.z() / depth_input;
547 const auto vector_sum_col_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
548 const auto vector_sum_row_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
549 +
id.y() + (
id.z() % depth_input) * height_input;
550 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr, vector_sum_row_ptr,
nullptr, mm_result_it, out_it,
551 result_offset_s32, result_shift_s32,
552 min_vec, max_vec, a_offset, b_offset, k_offset,
553 multiplier, shift,
offset, min_bound, max_bound,
554 window_step_x, window_start_x, window_end_x,
true,
true,
false, is_bounded_relu, is_fixed_point);
556 vector_sum_col_it, vector_sum_row_it, mm_result_it, out_it);
559 else if((a_offset == 0) && (b_offset != 0))
563 Iterator vector_sum_row_it = get_vector_sum_row_it(collapsed_window, vector_sum_row);
565 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
569 Iterator bias_it = get_bias_it(collapsed_window, bias);
572 const int batch_id =
id.z() / depth_input;
573 const auto vector_sum_row_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
574 +
id.y() + (
id.z() % depth_input) * height_input;
575 run_offset_contribution_output_stage_window<Typer>(
nullptr, vector_sum_row_ptr,
reinterpret_cast<const int32_t *
>(bias_it.ptr()), mm_result_it,
577 result_offset_s32, result_shift_s32,
578 min_vec, max_vec, a_offset, b_offset, k_offset,
579 multiplier, shift, offset, min_bound, max_bound,
580 window_step_x, window_start_x, window_end_x,
false,
true,
true, is_bounded_relu, is_fixed_point);
582 vector_sum_row_it, bias_it, mm_result_it, out_it);
588 const int batch_id =
id.z() / depth_input;
589 const auto vector_sum_row_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
590 +
id.y() + (
id.z() % depth_input) * height_input;
591 run_offset_contribution_output_stage_window<Typer>(
nullptr, vector_sum_row_ptr,
nullptr, mm_result_it, out_it,
592 result_offset_s32, result_shift_s32,
593 min_vec, max_vec, a_offset, b_offset, k_offset,
594 multiplier, shift,
offset, min_bound, max_bound,
595 window_step_x, window_start_x, window_end_x,
false,
true,
false, is_bounded_relu, is_fixed_point);
597 vector_sum_row_it, mm_result_it, out_it);
600 else if((a_offset != 0) && (b_offset == 0))
604 Iterator vector_sum_col_it = get_vector_sum_col_it(collapsed_window, vector_sum_col);
607 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
611 Iterator bias_it = get_bias_it(collapsed_window, bias);
614 const int batch_id =
id.z() / depth_input;
615 const auto vector_sum_col_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
616 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr,
nullptr,
reinterpret_cast<const int32_t *
>(bias_it.ptr()), mm_result_it,
618 result_offset_s32, result_shift_s32,
619 min_vec, max_vec, a_offset, b_offset, k_offset,
620 multiplier, shift, offset, min_bound, max_bound,
621 window_step_x, window_start_x, window_end_x,
true,
false,
true, is_bounded_relu, is_fixed_point);
623 vector_sum_col_it, bias_it, mm_result_it, out_it);
629 const int batch_id =
id.z() / depth_input;
630 const auto vector_sum_col_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
631 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr,
nullptr,
nullptr, mm_result_it, out_it,
632 result_offset_s32, result_shift_s32,
633 min_vec, max_vec, a_offset, b_offset, k_offset,
634 multiplier, shift,
offset, min_bound, max_bound,
635 window_step_x, window_start_x, window_end_x,
true,
false,
false, is_bounded_relu, is_fixed_point);
637 vector_sum_col_it, mm_result_it, out_it);
644 Iterator bias_it = get_bias_it(collapsed_window, bias);
647 run_offset_contribution_output_stage_window<Typer>(
nullptr,
nullptr,
reinterpret_cast<const int32_t *
>(bias_it.ptr()), mm_result_it, out_it,
648 result_offset_s32, result_shift_s32,
649 min_vec, max_vec, a_offset, b_offset, k_offset,
650 multiplier, shift, offset, min_bound, max_bound,
651 window_step_x, window_start_x, window_end_x,
false,
false,
true, is_bounded_relu, is_fixed_point);
653 bias_it, mm_result_it, out_it);
659 run_offset_contribution_output_stage_window<Typer>(
nullptr,
nullptr,
nullptr, mm_result_it, out_it,
660 result_offset_s32, result_shift_s32,
661 min_vec, max_vec, a_offset, b_offset, k_offset,
662 multiplier, shift,
offset, min_bound, max_bound,
663 window_step_x, window_start_x, window_end_x,
false,
false,
false, is_bounded_relu, is_fixed_point);
665 mm_result_it, out_it);
671 void run_offset_contribution_output_stage_symm(
const Window &window,
672 const ITensor *mm_result,
const ITensor *vector_sum_col,
const ITensor *vector_sum_row,
const ITensor *bias, ITensor *output,
673 int32_t a_offset, int32_t b_offset, int32_t k_offset,
bool slide_vector_sum_col,
674 GEMMLowpOutputStageInfo output_stage,
bool is_gemm3d,
bool is_bounded_relu,
bool is_fixed_point)
678 const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1;
680 const int32_t offset = output_stage.gemmlowp_offset;
681 const int32_t min_bound = output_stage.gemmlowp_min_bound;
682 const int32_t max_bound = output_stage.gemmlowp_max_bound;
684 const int32_t *result_multipliers = output_stage.gemmlowp_multipliers.data();
685 const int32_t *result_shifts = output_stage.gemmlowp_shifts.data();
686 const int32x4_t result_offset_s32 = vdupq_n_s32(offset);
687 const int8x16_t min_s8 = vdupq_n_s8(static_cast<int8_t>(min_bound));
688 const int8x16_t max_s8 = vdupq_n_s8(static_cast<int8_t>(max_bound));
690 const int window_step_x = 16;
691 const auto window_start_x =
static_cast<int>(window.x().start());
692 const auto window_end_x =
static_cast<int>(window.x().end());
697 Window collapsed_window = win.collapse_if_possible(win,
Window::DimZ);
699 Iterator mm_result_it(mm_result, win);
700 Iterator out_it(output, win);
706 Iterator vector_sum_col_it = get_vector_sum_col_it(collapsed_window, vector_sum_col);
709 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
713 Iterator bias_it = get_bias_it(collapsed_window, bias);
716 const int batch_id =
id.z() / depth_input;
717 const auto vector_sum_col_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
718 run_offset_contribution_output_stage_window_symm(vector_sum_col_ptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it, out_it,
719 result_multipliers, result_shifts,
720 result_offset_s32, min_s8, max_s8,
721 a_offset, offset, min_bound, max_bound,
722 window_step_x, window_start_x, window_end_x,
true,
true, is_bounded_relu, is_fixed_point);
724 vector_sum_col_it, bias_it, mm_result_it, out_it);
730 const int batch_id =
id.z() / depth_input;
731 const auto vector_sum_col_ptr =
reinterpret_cast<const int32_t *
>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
732 run_offset_contribution_output_stage_window_symm(vector_sum_col_ptr,
nullptr, mm_result_it, out_it,
733 result_multipliers, result_shifts,
734 result_offset_s32, min_s8, max_s8,
735 a_offset, offset, min_bound, max_bound,
736 window_step_x, window_start_x, window_end_x,
true,
false, is_bounded_relu, is_fixed_point);
738 vector_sum_col_it, mm_result_it, out_it);
745 Iterator bias_it = get_bias_it(collapsed_window, bias);
748 run_offset_contribution_output_stage_window_symm(
nullptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it, out_it,
749 result_multipliers, result_shifts,
750 result_offset_s32, min_s8, max_s8,
751 a_offset, offset, min_bound, max_bound,
752 window_step_x, window_start_x, window_end_x,
false,
true, is_bounded_relu, is_fixed_point);
754 bias_it, mm_result_it, out_it);
760 run_offset_contribution_output_stage_window_symm(
nullptr,
nullptr, mm_result_it, out_it,
761 result_multipliers, result_shifts,
762 result_offset_s32, min_s8, max_s8,
763 a_offset, offset, min_bound, max_bound,
764 window_step_x, window_start_x, window_end_x,
false,
false, is_bounded_relu, is_fixed_point);
766 mm_result_it, out_it);
772 Status validate_arguments(
const ITensorInfo *mm_result,
const ITensorInfo *vector_sum_col,
const ITensorInfo *vector_sum_row,
const ITensorInfo *bias,
const ITensorInfo *output,
773 int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
803 const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x();
806 ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2)));
810 if(output_shape.num_dimensions() > 1)
812 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
814 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
816 output_shape.collapse_from(output_batch_idx);
819 "mm_result tensor must have the same number of batches of output tensor");
823 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
824 vector_sum_col_shape.collapse_from(1);
827 "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");
832 if(output->total_size() != 0)
844 int32_t k, int32_t a_offset, int32_t b_offset,
850 ARM_COMPUTE_ERROR_THROW_ON(validate_arguments(mm_result, vector_sum_col, vector_sum_row, bias, dst, a_offset, b_offset, output_stage));
852 _a_offset = a_offset;
853 _b_offset = b_offset;
854 _k_offset = a_offset * b_offset * k;
855 _output_stage = output_stage;
875 ICpuKernel::configure(win);
883 ARM_COMPUTE_RETURN_ON_ERROR(validate_arguments(mm_result, vector_sum_col, vector_sum_row, bias, output, a_offset, b_offset, output_stage));
901 std::tie(type_min, type_max) =
get_min_max(
dst->info()->data_type());
902 int32_t type_min_int = type_min.get<int32_t>();
903 int32_t type_max_int = type_max.get<int32_t>();
905 const bool reinterpret_as_3d = vector_sum_row !=
nullptr 906 && mm_result->info()->num_dimensions() > 1
907 && mm_result->info()->tensor_shape().y() != vector_sum_row->info()->tensor_shape().x();
909 const bool is_bounded_relu = !(_output_stage.gemmlowp_min_bound <= type_min_int && _output_stage.gemmlowp_max_bound >= type_max_int);
918 const bool is_symm = _output_stage.is_quantized_per_channel;
922 run_offset_contribution_output_stage_symm(window, mm_result, vector_sum_col, vector_sum_row, bias,
dst, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, _output_stage,
923 reinterpret_as_3d, is_bounded_relu, is_fixed_point);
929 run_offset_contribution_output_stage<int8_t>(window, mm_result, vector_sum_col, vector_sum_row, bias,
dst, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, _output_stage,
930 reinterpret_as_3d, is_bounded_relu, is_fixed_point);
934 run_offset_contribution_output_stage<uint8_t>(window, mm_result, vector_sum_col, vector_sum_row, bias,
dst, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, _output_stage,
935 reinterpret_as_3d, is_bounded_relu, is_fixed_point);
942 return "CpuGemmLowpOffsetContributionOutputStageKernel";
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
Class describing the value of a pixel for any image format.
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.
Quantize using a fixed point multiplication.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
const char * name() const override
Name of the kernel.
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
void configure(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, ITensorInfo *dst, int32_t k, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
Initialise the kernel inputs and output.
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.
uint8_t vgetlane(const uint8x8_t vector, const unsigned int lane)
static constexpr size_t DimX
Alias for dimension 0 also known as X dimension.
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
void collapse_from(size_t start)
Collapse dimensions starting from a given point.
virtual const TensorShape & tensor_shape() const =0
Size for each dimension of the tensor.
quantized, asymmetric fixed-point 8-bit number unsigned
Class to describe a number of elements in each dimension.
bool auto_init_if_empty(ITensorInfo &info, const TensorShape &shape, int num_channels, DataType data_type, QuantizationInfo quantization_info=QuantizationInfo())
Auto initialize the tensor info (shape, number of channels and data type) if the current assignment i...
virtual std::unique_ptr< T > clone() const =0
Provide a clone of the current object of class T.
GEMMLowp output stage info.
Quantize using an integer multiplication.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, const ITensorInfo *dst, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
Static function to check if given info will lead to a valid configuration.
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
void run_op(ITensorPack &tensors, const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
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.
uint8x16_t finalize_quantization(int32x4x4_t &in_s32, int result_fixedpoint_multiplier, int32_t result_shift, int32x4_t result_offset_after_shift_s32, uint8x16_t min_u8, uint8x16_t max_u8, bool is_bounded_relu)
Performs final quantization step on 16 elements.
Information about executing thread and CPU.
#define ARM_COMPUTE_RETURN_ERROR_ON_MISMATCHING_SHAPES(...)
static constexpr size_t DimZ
Alias for dimension 2 also known as Z dimension.
unsigned int num_dimensions() const
Returns the effective dimensionality of the tensor.
#define ARM_COMPUTE_RETURN_ERROR_ON_DATA_TYPE_CHANNEL_NOT_IN(t, c,...)
void vstore(uint8_t *ptr, uint8x8_t val)
#define ARM_COMPUTE_RETURN_ERROR_ON_MSG(cond, msg)
If the condition is true, an error is returned.
#define ARM_COMPUTE_ERROR_ON_NULLPTR(...)
uint8x8_t vdup_n(uint8_t value, traits::vector_64_tag)
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...
int8x16_t finalize_quantization_symm(int32x4x4_t &in_s32, const int32x4x4_t &result_fixedpoint_multiplier, const int32x4x4_t &result_shift, const int32x4_t &result_offset_after_shift_s32, const int8x16_t &min_s8, const int8x16_t &max_s8, const bool is_bounded_relu)
Performs final quantization step on 16 elements for symmetric quantization.
quantized, asymmetric fixed-point 8-bit number signed
Includes all wrapper headers at once.
std::tuple< PixelValue, PixelValue > get_min_max(DataType dt)
Compute the mininum and maximum values a data type can take.
Describe a multidimensional execution window.
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)