48 inline int32x4x4_t load_results_input(
const Iterator &mm_result_it, int32_t x)
53 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 0),
54 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 4),
55 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 8),
56 vld1q_s32(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x + 12)
61 inline int32x4x4_t load(
const int32_t *ptr, int32_t x)
66 vld1q_s32(ptr + x + 0),
67 vld1q_s32(ptr + x + 4),
68 vld1q_s32(ptr + x + 8),
69 vld1q_s32(ptr + x + 12)
74 inline int32x4x4_t add_s32(int32x4x4_t a, int32x4_t
b)
79 vaddq_s32(a.val[0],
b),
80 vaddq_s32(a.val[1],
b),
81 vaddq_s32(a.val[2],
b),
82 vaddq_s32(a.val[3],
b)
87 inline int32x4x4_t add_s32(int32x4x4_t a, int32x4x4_t
b)
92 vaddq_s32(a.val[0],
b.val[0]),
93 vaddq_s32(a.val[1],
b.val[1]),
94 vaddq_s32(a.val[2],
b.val[2]),
95 vaddq_s32(a.val[3],
b.val[3])
100 inline int32x4x4_t mul_s32(int32x4x4_t &a, int32_t mul_scalar)
105 vmulq_n_s32(a.val[0], mul_scalar),
106 vmulq_n_s32(a.val[1], mul_scalar),
107 vmulq_n_s32(a.val[2], mul_scalar),
108 vmulq_n_s32(a.val[3], mul_scalar)
113 inline int32x4x4_t mul_s32(int32x4x4_t &a,
const int32_t *multilpier)
118 vmulq_s32(a.val[0], vld1q_s32(multilpier)),
119 vmulq_s32(a.val[1], vld1q_s32(multilpier + 4)),
120 vmulq_s32(a.val[2], vld1q_s32(multilpier + 8)),
121 vmulq_s32(a.val[3], vld1q_s32(multilpier + 12))
126 inline int32x4x4_t get_a_offset(
const int32_t *vector_sum_col_ptr, int32_t a_offset, int32_t x)
128 int32x4x4_t a_offset_term_s32 = load(vector_sum_col_ptr, x);
130 a_offset_term_s32.val[0] = vmulq_n_s32(a_offset_term_s32.val[0], a_offset);
131 a_offset_term_s32.val[1] = vmulq_n_s32(a_offset_term_s32.val[1], a_offset);
132 a_offset_term_s32.val[2] = vmulq_n_s32(a_offset_term_s32.val[2], a_offset);
133 a_offset_term_s32.val[3] = vmulq_n_s32(a_offset_term_s32.val[3], a_offset);
134 return a_offset_term_s32;
137 inline int32x4_t get_b_offset(
const int32_t *vector_sum_row_ptr, int32_t b_offset)
139 int32x4_t b_offset_term_s32 = vld1q_dup_s32(vector_sum_row_ptr);
140 b_offset_term_s32 = vmulq_n_s32(b_offset_term_s32, b_offset);
141 return b_offset_term_s32;
144 inline int32x4x4_t get_k_offset(int32_t k_offset)
149 vdupq_n_s32(k_offset),
150 vdupq_n_s32(k_offset),
151 vdupq_n_s32(k_offset),
152 vdupq_n_s32(k_offset)
157 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)
159 const static int32x4_t zero_s32 = vdupq_n_s32(0);
162 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
163 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
164 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
165 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
168 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
169 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
170 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
171 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
174 const int16x8x2_t in_s16 =
177 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
178 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
183 uint8x16_t out_u8 = vcombine_u8(vqmovun_s16(in_s16.val[0]), vqmovun_s16(in_s16.val[1]));
187 out_u8 = vmaxq_u8(out_u8, min_u8);
188 out_u8 = vminq_u8(out_u8, max_u8);
194 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)
196 const static int32x4_t zero_s32 = vdupq_n_s32(0);
199 in_s32.val[0] = vshlq_s32(in_s32.val[0], result_shift_s32);
200 in_s32.val[1] = vshlq_s32(in_s32.val[1], result_shift_s32);
201 in_s32.val[2] = vshlq_s32(in_s32.val[2], result_shift_s32);
202 in_s32.val[3] = vshlq_s32(in_s32.val[3], result_shift_s32);
205 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
206 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
207 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
208 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
211 const int16x8x2_t in_s16 =
214 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
215 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
220 int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1]));
224 out_s8 = vmaxq_s8(out_s8, min_s8);
225 out_s8 = vminq_s8(out_s8, max_s8);
231 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)
233 const static int32x4_t zero_s32 = vdupq_n_s32(0);
236 in_s32.val[0] = vshlq_s32(in_s32.val[0], vnegq_s32(result_shift_s32.val[0]));
237 in_s32.val[1] = vshlq_s32(in_s32.val[1], vnegq_s32(result_shift_s32.val[1]));
238 in_s32.val[2] = vshlq_s32(in_s32.val[2], vnegq_s32(result_shift_s32.val[2]));
239 in_s32.val[3] = vshlq_s32(in_s32.val[3], vnegq_s32(result_shift_s32.val[3]));
242 in_s32.val[0] = vmaxq_s32(in_s32.val[0], zero_s32);
243 in_s32.val[1] = vmaxq_s32(in_s32.val[1], zero_s32);
244 in_s32.val[2] = vmaxq_s32(in_s32.val[2], zero_s32);
245 in_s32.val[3] = vmaxq_s32(in_s32.val[3], zero_s32);
248 const int16x8x2_t in_s16 =
251 vcombine_s16(vqmovn_s32(in_s32.val[0]), vqmovn_s32(in_s32.val[1])),
252 vcombine_s16(vqmovn_s32(in_s32.val[2]), vqmovn_s32(in_s32.val[3]))
257 int8x16_t out_s8 = vcombine_s8(vqmovn_s16(in_s16.val[0]), vqmovn_s16(in_s16.val[1]));
261 out_s8 = vmaxq_s8(out_s8, min_s8);
262 out_s8 = vminq_s8(out_s8, max_s8);
268 template <
typename T>
272 using vtype =
typename wrapper::traits::neon_bitvector_t<T, wrapper::traits::BitWidth::W128>;
275 inline Window get_win_vector_sum(
const Window &window)
277 Window win_vector_sum(window);
278 win_vector_sum.set(
Window::DimY, Window::Dimension(0, 0, 0));
279 win_vector_sum.set(
Window::DimZ, Window::Dimension(0, 0, 0));
280 return win_vector_sum;
283 inline Iterator get_vector_sum_col_it(
const Window &window,
const ITensor *vector_sum_col)
285 Iterator vector_sum_col_it(vector_sum_col, get_win_vector_sum(window));
286 return vector_sum_col_it;
289 inline Iterator get_vector_sum_row_it(
const Window &window,
const ITensor *vector_sum_row)
291 Window win_vector_sum_row = get_win_vector_sum(window);
292 win_vector_sum_row.set(
Window::DimX, Window::Dimension(0, 0, 0));
293 Iterator vector_sum_row_it(vector_sum_row, win_vector_sum_row);
294 return vector_sum_row_it;
297 inline Iterator get_bias_it(
const Window &window,
const ITensor *bias)
299 Window win_bias(window);
302 Iterator bias_it(bias, win_bias);
306 template <
typename VT>
307 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,
308 const int32x4_t result_offset_s32,
const int32x4_t result_shift_s32,
309 typename VT::vtype min_vec,
typename VT::vtype max_vec,
310 int32_t a_offset, int32_t b_offset, int32_t k_offset,
311 int32_t multiplier, int32_t shift, int32_t
offset, int32_t min_bound, int32_t max_bound,
312 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)
314 int32x4x4_t offset_term_s32 = { 0, 0, 0, 0 };
318 offset_term_s32 = add_s32(offset_term_s32, result_offset_s32);
320 if(has_a_offset && has_b_offset)
322 offset_term_s32 = add_s32(offset_term_s32, get_k_offset(k_offset));
326 offset_term_s32 = add_s32(offset_term_s32, get_b_offset(vector_sum_row_ptr, b_offset));
329 int x = window_start_x;
330 for(; x <= (window_end_x - window_step_x); x += window_step_x)
332 int32x4x4_t in_s32 = load_results_input(mm_result_it, x);
336 in_s32 = add_s32(in_s32, get_a_offset(vector_sum_col_ptr, a_offset, x));
340 in_s32 = add_s32(in_s32, load(bias_ptr, x));
342 if(!is_fixed_point || has_b_offset)
344 in_s32 = add_s32(in_s32, offset_term_s32);
348 in_s32 = mul_s32(in_s32, multiplier);
353 wrapper::vstore(reinterpret_cast<typename VT::stype *>(out_it.ptr() + x),
354 finalize_quantization(in_s32, multiplier, shift, result_offset_s32, min_vec, max_vec, is_bounded_relu));
358 wrapper::vstore(reinterpret_cast<typename VT::stype *>(out_it.ptr() + x),
359 finalize_quantization_floating_point(in_s32, result_shift_s32, min_vec, max_vec, is_bounded_relu));
363 for(; x < window_end_x; ++x)
365 int32_t in_value = *(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x) +
wrapper::vgetlane(offset_term_s32.val[0], 0);
369 in_value += (*(vector_sum_col_ptr + x) * a_offset);
373 in_value += *(bias_ptr + x);
380 static_cast<typename VT::stype>(min_bound),
381 static_cast<typename VT::stype>(max_bound), is_bounded_relu);
386 in_value = (in_value * multiplier) >> shift;
391 in_value = static_cast<typename VT::stype>(std::max<int32_t>(min_bound, std::min<int32_t>(max_bound, in_value)));
394 std::min<int32_t>(static_cast<int32_t>(std::numeric_limits<typename VT::stype>::max()), in_value)));
399 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,
400 const int32_t *result_multipliers,
const int32_t *result_shifts,
401 const int32x4_t result_offset, int8x16_t min_s8, int8x16_t max_s8,
402 int32_t a_offset, int32_t
offset, int32_t min_bound, int32_t max_bound,
403 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)
405 int32x4x4_t offset_term_s32 = { 0, 0, 0, 0 };
409 offset_term_s32 = add_s32(offset_term_s32, result_offset);
412 int x = window_start_x;
413 for(; x <= (window_end_x - window_step_x); x += window_step_x)
415 int32x4x4_t in_s32 = load_results_input(mm_result_it, x);
419 in_s32 = add_s32(in_s32, get_a_offset(vector_sum_col_ptr, a_offset, x));
423 in_s32 = add_s32(in_s32, load(bias_ptr, x));
427 in_s32 = add_s32(in_s32, offset_term_s32);
428 in_s32 = mul_s32(in_s32, result_multipliers + x);
433 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));
437 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));
441 for(; x < window_end_x; ++x)
443 int32_t in_value = *(reinterpret_cast<const int32_t *>(mm_result_it.ptr()) + x) +
wrapper::vgetlane(offset_term_s32.val[0], 0);
447 in_value += (*(vector_sum_col_ptr + x) * a_offset);
451 in_value += *(bias_ptr + x);
457 *(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);
462 in_value = (in_value * result_multipliers[x]) >> (-result_shifts[x]);
467 in_value = static_cast<int8_t>(std::max<int32_t>(min_bound, std::min<int32_t>(max_bound, in_value)));
469 *(out_it.ptr() + x) = static_cast<int8_t>(std::max<int32_t>(-128, std::min<int32_t>(127, in_value)));
474 template <
typename T>
475 void run_offset_contribution_output_stage(
const Window &window,
476 const ITensor *mm_result,
const ITensor *vector_sum_col,
const ITensor *vector_sum_row,
const ITensor *bias, ITensor *output,
477 int32_t a_offset, int32_t b_offset, int32_t k_offset,
bool slide_vector_sum_col,
478 GEMMLowpOutputStageInfo output_stage,
bool is_gemm3d,
bool is_bounded_relu,
bool is_fixed_point)
480 using ExactTagType =
typename wrapper::traits::neon_bitvector_tag_t<T, wrapper::traits::BitWidth::W128>;
481 using Typer = VectorTyper<T>;
483 const int height_input = is_gemm3d ? mm_result->info()->dimension(1) : 0;
484 const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1;
486 const int32_t multiplier = output_stage.gemmlowp_multiplier;
487 const int32_t shift = output_stage.gemmlowp_shift;
488 const int32_t
offset = output_stage.gemmlowp_offset;
489 const int32_t min_bound = output_stage.gemmlowp_min_bound;
490 const int32_t max_bound = output_stage.gemmlowp_max_bound;
492 const int32x4_t result_offset_s32 = vdupq_n_s32(
offset);
493 const int32x4_t result_shift_s32 = vdupq_n_s32(is_fixed_point ? shift : -shift);
494 const auto min_vec =
wrapper::vdup_n(static_cast<T>(min_bound), ExactTagType{});
495 const auto max_vec =
wrapper::vdup_n(static_cast<T>(max_bound), ExactTagType{});
497 const int window_step_x = 16;
498 const auto window_start_x = static_cast<int>(window.x().start());
499 const auto window_end_x = static_cast<int>(window.x().end());
504 Window collapsed_window = win.collapse_if_possible(win,
Window::DimZ);
506 Iterator mm_result_it(mm_result, win);
507 Iterator out_it(output, win);
509 if((a_offset != 0) && (b_offset != 0))
514 Iterator vector_sum_col_it = get_vector_sum_col_it(collapsed_window, vector_sum_col);
515 Iterator vector_sum_row_it = get_vector_sum_row_it(collapsed_window, vector_sum_row);
517 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
520 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
524 Iterator bias_it = get_bias_it(collapsed_window, bias);
527 const int batch_id =
id.z() / depth_input;
528 const auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
529 const auto vector_sum_row_ptr = reinterpret_cast<const int32_t *>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
530 +
id.y() + (
id.z() % depth_input) * height_input;
531 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr, vector_sum_row_ptr, reinterpret_cast<const int32_t *>(bias_it.ptr()),
534 result_offset_s32, result_shift_s32,
535 min_vec, max_vec, a_offset, b_offset, k_offset,
536 multiplier, shift,
offset, min_bound, max_bound,
537 window_step_x, window_start_x, window_end_x,
true,
true,
true, is_bounded_relu, is_fixed_point);
539 vector_sum_col_it, vector_sum_row_it, bias_it, mm_result_it, out_it);
545 const int batch_id =
id.z() / depth_input;
546 const auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
547 const auto vector_sum_row_ptr = reinterpret_cast<const int32_t *>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
548 +
id.y() + (
id.z() % depth_input) * height_input;
549 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr, vector_sum_row_ptr,
nullptr, mm_result_it, out_it,
550 result_offset_s32, result_shift_s32,
551 min_vec, max_vec, a_offset, b_offset, k_offset,
552 multiplier, shift,
offset, min_bound, max_bound,
553 window_step_x, window_start_x, window_end_x,
true,
true,
false, is_bounded_relu, is_fixed_point);
555 vector_sum_col_it, vector_sum_row_it, mm_result_it, out_it);
558 else if((a_offset == 0) && (b_offset != 0))
562 Iterator vector_sum_row_it = get_vector_sum_row_it(collapsed_window, vector_sum_row);
564 const size_t sum_row_stride_y = vector_sum_row->info()->strides_in_bytes().y();
568 Iterator bias_it = get_bias_it(collapsed_window, bias);
571 const int batch_id =
id.z() / depth_input;
572 const auto vector_sum_row_ptr = reinterpret_cast<const int32_t *>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
573 +
id.y() + (
id.z() % depth_input) * height_input;
574 run_offset_contribution_output_stage_window<Typer>(
nullptr, vector_sum_row_ptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it,
576 result_offset_s32, result_shift_s32,
577 min_vec, max_vec, a_offset, b_offset, k_offset,
578 multiplier, shift,
offset, min_bound, max_bound,
579 window_step_x, window_start_x, window_end_x,
false,
true,
true, is_bounded_relu, is_fixed_point);
581 vector_sum_row_it, bias_it, mm_result_it, out_it);
587 const int batch_id =
id.z() / depth_input;
588 const auto vector_sum_row_ptr = reinterpret_cast<const int32_t *>(vector_sum_row_it.ptr() + batch_id * sum_row_stride_y)
589 +
id.y() + (
id.z() % depth_input) * height_input;
590 run_offset_contribution_output_stage_window<Typer>(
nullptr, vector_sum_row_ptr,
nullptr, mm_result_it, out_it,
591 result_offset_s32, result_shift_s32,
592 min_vec, max_vec, a_offset, b_offset, k_offset,
593 multiplier, shift,
offset, min_bound, max_bound,
594 window_step_x, window_start_x, window_end_x,
false,
true,
false, is_bounded_relu, is_fixed_point);
596 vector_sum_row_it, mm_result_it, out_it);
599 else if((a_offset != 0) && (b_offset == 0))
603 Iterator vector_sum_col_it = get_vector_sum_col_it(collapsed_window, vector_sum_col);
606 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
610 Iterator bias_it = get_bias_it(collapsed_window, bias);
613 const int batch_id =
id.z() / depth_input;
614 const auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
615 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr,
nullptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it,
617 result_offset_s32, result_shift_s32,
618 min_vec, max_vec, a_offset, b_offset, k_offset,
619 multiplier, shift,
offset, min_bound, max_bound,
620 window_step_x, window_start_x, window_end_x,
true,
false,
true, is_bounded_relu, is_fixed_point);
622 vector_sum_col_it, bias_it, mm_result_it, out_it);
628 const int batch_id =
id.z() / depth_input;
629 const auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
630 run_offset_contribution_output_stage_window<Typer>(vector_sum_col_ptr,
nullptr,
nullptr, mm_result_it, out_it,
631 result_offset_s32, result_shift_s32,
632 min_vec, max_vec, a_offset, b_offset, k_offset,
633 multiplier, shift,
offset, min_bound, max_bound,
634 window_step_x, window_start_x, window_end_x,
true,
false,
false, is_bounded_relu, is_fixed_point);
636 vector_sum_col_it, mm_result_it, out_it);
643 Iterator bias_it = get_bias_it(collapsed_window, bias);
646 run_offset_contribution_output_stage_window<Typer>(
nullptr,
nullptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it, out_it,
647 result_offset_s32, result_shift_s32,
648 min_vec, max_vec, a_offset, b_offset, k_offset,
649 multiplier, shift,
offset, min_bound, max_bound,
650 window_step_x, window_start_x, window_end_x,
false,
false,
true, is_bounded_relu, is_fixed_point);
652 bias_it, mm_result_it, out_it);
658 run_offset_contribution_output_stage_window<Typer>(
nullptr,
nullptr,
nullptr, mm_result_it, out_it,
659 result_offset_s32, result_shift_s32,
660 min_vec, max_vec, a_offset, b_offset, k_offset,
661 multiplier, shift,
offset, min_bound, max_bound,
662 window_step_x, window_start_x, window_end_x,
false,
false,
false, is_bounded_relu, is_fixed_point);
664 mm_result_it, out_it);
670 void run_offset_contribution_output_stage_symm(
const Window &window,
671 const ITensor *mm_result,
const ITensor *vector_sum_col,
const ITensor *vector_sum_row,
const ITensor *bias, ITensor *output,
672 int32_t a_offset, int32_t b_offset, int32_t k_offset,
bool slide_vector_sum_col,
673 GEMMLowpOutputStageInfo output_stage,
bool is_gemm3d,
bool is_bounded_relu,
bool is_fixed_point)
677 const int depth_input = is_gemm3d ? mm_result->info()->dimension(2) : 1;
679 const int32_t
offset = output_stage.gemmlowp_offset;
680 const int32_t min_bound = output_stage.gemmlowp_min_bound;
681 const int32_t max_bound = output_stage.gemmlowp_max_bound;
683 const int32_t *result_multipliers = output_stage.gemmlowp_multipliers.data();
684 const int32_t *result_shifts = output_stage.gemmlowp_shifts.data();
685 const int32x4_t result_offset_s32 = vdupq_n_s32(
offset);
686 const int8x16_t min_s8 = vdupq_n_s8(static_cast<int8_t>(min_bound));
687 const int8x16_t max_s8 = vdupq_n_s8(static_cast<int8_t>(max_bound));
689 const int window_step_x = 16;
690 const auto window_start_x = static_cast<int>(window.x().start());
691 const auto window_end_x = static_cast<int>(window.x().end());
696 Window collapsed_window = win.collapse_if_possible(win,
Window::DimZ);
698 Iterator mm_result_it(mm_result, win);
699 Iterator out_it(output, win);
705 Iterator vector_sum_col_it = get_vector_sum_col_it(collapsed_window, vector_sum_col);
708 const int vector_sum_col_batch_offset = slide_vector_sum_col ? vector_sum_col->info()->strides_in_bytes().z() : 0;
712 Iterator bias_it = get_bias_it(collapsed_window, bias);
715 const int batch_id =
id.z() / depth_input;
716 const auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
717 run_offset_contribution_output_stage_window_symm(vector_sum_col_ptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it, out_it,
718 result_multipliers, result_shifts,
719 result_offset_s32, min_s8, max_s8,
720 a_offset,
offset, min_bound, max_bound,
721 window_step_x, window_start_x, window_end_x,
true,
true, is_bounded_relu, is_fixed_point);
723 vector_sum_col_it, bias_it, mm_result_it, out_it);
729 const int batch_id =
id.z() / depth_input;
730 const auto vector_sum_col_ptr = reinterpret_cast<const int32_t *>(vector_sum_col_it.ptr() + batch_id * vector_sum_col_batch_offset);
731 run_offset_contribution_output_stage_window_symm(vector_sum_col_ptr,
nullptr, mm_result_it, out_it,
732 result_multipliers, result_shifts,
733 result_offset_s32, min_s8, max_s8,
734 a_offset,
offset, min_bound, max_bound,
735 window_step_x, window_start_x, window_end_x,
true,
false, is_bounded_relu, is_fixed_point);
737 vector_sum_col_it, mm_result_it, out_it);
744 Iterator bias_it = get_bias_it(collapsed_window, bias);
747 run_offset_contribution_output_stage_window_symm(
nullptr, reinterpret_cast<const int32_t *>(bias_it.ptr()), mm_result_it, out_it,
748 result_multipliers, result_shifts,
749 result_offset_s32, min_s8, max_s8,
750 a_offset,
offset, min_bound, max_bound,
751 window_step_x, window_start_x, window_end_x,
false,
true, is_bounded_relu, is_fixed_point);
753 bias_it, mm_result_it, out_it);
759 run_offset_contribution_output_stage_window_symm(
nullptr,
nullptr, mm_result_it, out_it,
760 result_multipliers, result_shifts,
761 result_offset_s32, min_s8, max_s8,
762 a_offset,
offset, min_bound, max_bound,
763 window_step_x, window_start_x, window_end_x,
false,
false, is_bounded_relu, is_fixed_point);
765 mm_result_it, out_it);
771 Status
validate_arguments(
const ITensorInfo *mm_result,
const ITensorInfo *vector_sum_col,
const ITensorInfo *vector_sum_row,
const ITensorInfo *bias,
const ITensorInfo *output,
772 int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
802 const bool reinterpret_as_3d = mm_result->num_dimensions() > 1 && mm_result->tensor_shape().y() != vector_sum_row->tensor_shape().x();
805 ARM_COMPUTE_RETURN_ERROR_ON(reinterpret_as_3d && vector_sum_row->dimension(0) != (mm_result->dimension(1) * mm_result->dimension(2)));
811 const unsigned int output_batch_idx = reinterpret_as_3d ? 3 : 2;
813 TensorShape vector_sum_row_shape = vector_sum_row->tensor_shape();
818 "mm_result tensor must have the same number of batches of output tensor");
822 TensorShape vector_sum_col_shape = vector_sum_col->tensor_shape();
823 vector_sum_col_shape.collapse_from(1);
826 "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");
831 if(output->total_size() != 0)
840 std::pair<Status, Window> validate_and_configure_window(ITensorInfo *mm_result, ITensorInfo *output)
852 return std::make_pair(Status{}, win);
857 : _vector_sum_col(nullptr), _vector_sum_row(nullptr), _bias(nullptr), _mm_result(nullptr), _output(nullptr), _a_offset(0), _b_offset(0), _k_offset(0), _slide_vector_sum_col(true),
865 int32_t k, int32_t a_offset, int32_t b_offset,
872 vector_sum_col !=
nullptr ? vector_sum_col->
info() :
nullptr,
873 vector_sum_row !=
nullptr ? vector_sum_row->
info() :
nullptr,
874 bias !=
nullptr ? bias->
info() :
nullptr,
875 output->
info(), a_offset, b_offset, output_stage));
877 _vector_sum_col = vector_sum_col;
878 _vector_sum_row = vector_sum_row;
880 _mm_result = mm_result;
882 _a_offset = a_offset;
883 _b_offset = b_offset;
884 _k_offset = a_offset * b_offset * k;
885 _output_stage = output_stage;
897 auto win_config = validate_and_configure_window(mm_result->
info(), output->
info());
899 INEKernel::configure(win_config.second);
921 int32_t type_min_int = type_min.get<int32_t>();
922 int32_t type_max_int = type_max.get<int32_t>();
924 const bool reinterpret_as_3d = _vector_sum_row !=
nullptr 941 run_offset_contribution_output_stage_symm(
window, _mm_result, _vector_sum_col, _vector_sum_row, _bias, _output, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, _output_stage,
942 reinterpret_as_3d, is_bounded_relu, is_fixed_point);
948 run_offset_contribution_output_stage<int8_t>(
window, _mm_result, _vector_sum_col, _vector_sum_row, _bias, _output, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, _output_stage,
949 reinterpret_as_3d, is_bounded_relu, is_fixed_point);
953 run_offset_contribution_output_stage<uint8_t>(
window, _mm_result, _vector_sum_col, _vector_sum_row, _bias, _output, _a_offset, _b_offset, _k_offset, _slide_vector_sum_col, _output_stage,
954 reinterpret_as_3d, is_bounded_relu, is_fixed_point);
__global uchar * offset(const Image *img, int x, int y)
Get the pointer position of a Image.
virtual size_t num_dimensions() const =0
The number of dimensions of the tensor (rank)
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.
void run(const Window &window, const ThreadInfo &info) override
Execute the kernel on the passed window.
NEGEMMLowpOffsetContributionOutputStageKernel()
Constructor.
#define ARM_COMPUTE_RETURN_ON_ERROR(status)
Checks if a status contains an error and returns it.
virtual DataType data_type() const =0
Data type used for each element of the tensor.
Store the tensor's metadata.
#define ARM_COMPUTE_ERROR_THROW_ON(status)
int32_t gemmlowp_max_bound
GEMMLowp max value used to saturate down the output result before converting back to QASYMM8.
#define ARM_COMPUTE_RETURN_ERROR_ON(cond)
If the condition is true, an error is returned.
GEMMLowpOutputStageType type
GEMMLowp output stage type.
Interface for CPU tensor.
Copyright (c) 2017-2021 Arm Limited.
bool is_quantized_per_channel
GEMMLowp quantized per-channel flag.
1 channel, 1 S32 per channel
T x() const
Alias to access the size of the first dimension.
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
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.
virtual ITensorInfo * info() const =0
Interface to be implemented by the child class to return the tensor's metadata.
void configure(const ITensor *mm_result, const ITensor *vector_sum_col, const ITensor *vector_sum_row, const ITensor *bias, ITensor *output, int32_t k, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
Initialise the kernel's input and output.
Quantize using an integer multiplication.
#define ARM_COMPUTE_ERROR_ON_UNCONFIGURED_KERNEL(k)
static constexpr size_t DimY
Alias for dimension 1 also known as Y dimension.
ScaleKernelInfo info(interpolation_policy, default_border_mode, PixelValue(), sampling_policy, false)
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,...)
Status validate_arguments(const ITensorInfo *input, const ITensorInfo *bias, const ITensorInfo *output, const GEMMLowpOutputStageInfo *output_stage)
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.
T y() const
Alias to access the size of the second dimension.
quantized, asymmetric fixed-point 8-bit number signed
Includes all wrapper headers at once.
static Status validate(const ITensorInfo *mm_result, const ITensorInfo *vector_sum_col, const ITensorInfo *vector_sum_row, const ITensorInfo *bias, const ITensorInfo *output, int32_t a_offset, int32_t b_offset, GEMMLowpOutputStageInfo output_stage)
Static function to check if given info will lead to a valid configuration of NEGEMMLowpOffsetContribu...
int32_t gemmlowp_min_bound
GEMMLowp min value used to saturate down the output result before converting back to QASYMM8.
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.
wrapper::traits::neon_vector< T, 16 >::type finalize_quantization(int32x4x4_t &in_s32, int32x4_t result_shift_s32, typename wrapper::traits::neon_vector< T, 16 >::type min, typename wrapper::traits::neon_vector< T, 16 >::type max)
#define ARM_COMPUTE_ERROR_ON_INVALID_SUBWINDOW(f, s)