|
14 | 14 | #include <cmath> |
15 | 15 | #include <cstring> |
16 | 16 |
|
| 17 | +#include <xtensa/config/core-isa.h> |
| 18 | + |
| 19 | +// HiFi4 cores run 4-bit through the fused kernel below. It requantizes with |
| 20 | +// nnlib's own HiFi4 macro, so its output is bit-identical to the nnlib path; |
| 21 | +// HiFi1 and HiFi5 requantize differently and keep the nnlib path. |
| 22 | +#if defined(XCHAL_HAVE_HIFI4) && XCHAL_HAVE_HIFI4 && \ |
| 23 | + !(defined(XCHAL_HAVE_HIFI5) && XCHAL_HAVE_HIFI5) && \ |
| 24 | + !(defined(XCHAL_HAVE_HIFI1) && XCHAL_HAVE_HIFI1) |
| 25 | +#define PACKED_FC_FUSED_4BIT 1 |
| 26 | +#include <xa_nnlib_common.h> |
| 27 | +#include <xa_nnlib_quant_macros.h> |
| 28 | +#else |
| 29 | +#define PACKED_FC_FUSED_4BIT 0 |
| 30 | +#endif |
| 31 | + |
17 | 32 | // Row-packed sub-byte fully-connected on HiFi. |
18 | 33 | // |
19 | 34 | // The weight arrives as [out_dim, packed_row_bytes] int8, where the bytes are |
|
38 | 53 | // GEMV, so it is memory bound on weight traffic and 6-bit moves 25% fewer |
39 | 54 | // bytes than 8-bit. |
40 | 55 | // |
| 56 | +// On HiFi4, 4-bit skips the tile: fused_split_4bit_gemv() decodes nibbles |
| 57 | +// straight into the multiply and requantizes with nnlib's macro. |
| 58 | +// |
41 | 59 | // There is no fatal path here. Which kernel serves this operator is decided at |
42 | 60 | // build time by operator_fallback.bzl, so there is no generic kernel linked to |
43 | 61 | // fall back to at runtime; aborting would take the model down. Anything the |
@@ -493,6 +511,158 @@ void packed_fully_connected_scalar( |
493 | 511 | } |
494 | 512 | } |
495 | 513 |
|
| 514 | +#if PACKED_FC_FUSED_4BIT |
| 515 | +// (x - zp) widened to int16 once per input vector, so every row reuses it. |
| 516 | +// Returns S = sum(x - zp) for the offset fold. |
| 517 | +inline int32_t widen_input( |
| 518 | + int16_t* __restrict__ x16, |
| 519 | + const int8_t* __restrict__ x, |
| 520 | + const int32_t n, |
| 521 | + const int32_t zp) { |
| 522 | + int32_t s = 0; |
| 523 | + for (int32_t k = 0; k < n; ++k) { |
| 524 | + const int32_t v = static_cast<int32_t>(x[k]) - zp; |
| 525 | + x16[k] = static_cast<int16_t>(v); |
| 526 | + s += v; |
| 527 | + } |
| 528 | + return s; |
| 529 | +} |
| 530 | + |
| 531 | +// Low 32 bits of two accumulators that hold sums scaled by 256, as nnlib's |
| 532 | +// 4-row kernel leaves them. |
| 533 | +inline __attribute__((always_inline)) ae_int32x2 |
| 534 | +unscale_pair(const ae_int64 a, const ae_int64 b) { |
| 535 | + return AE_SEL32_LL( |
| 536 | + AE_MOVINT32X2_FROMINT64(AE_SRAI64(a, 8)), |
| 537 | + AE_MOVINT32X2_FROMINT64(AE_SRAI64(b, 8))); |
| 538 | +} |
| 539 | + |
| 540 | +// Per-channel requantize of two rows, the same steps as |
| 541 | +// xa_nn_matmul_v2_per_chan_sym8sxasym8s_asym8s on HiFi4. |
| 542 | +inline __attribute__((always_inline)) ae_int32x2 requantize_pair( |
| 543 | + const ae_int32x2 acc, |
| 544 | + const int32_t mult0, |
| 545 | + const int32_t mult1, |
| 546 | + const int32_t shift0, |
| 547 | + const int32_t shift1, |
| 548 | + const int32_t out_zp) { |
| 549 | + const int32_t l0 = shift0 < 0 ? 0 : shift0; |
| 550 | + const int32_t r0 = shift0 > 0 ? 0 : -shift0; |
| 551 | + const int32_t l1 = shift1 < 0 ? 0 : shift1; |
| 552 | + const int32_t r1 = shift1 > 0 ? 0 : -shift1; |
| 553 | + ae_int32x2 y; |
| 554 | + MPY_BY_QUANT_MULT_PER_CHAN_X2_OUT32_SHIFT( |
| 555 | + y, acc, AE_MOVDA32X2(mult0, mult1), l0, l1, r0, r1); |
| 556 | + y = AE_ADD32S(y, AE_MOVDA32(out_zp)); |
| 557 | + y = AE_MAX32(y, AE_MOVDA32(-128)); |
| 558 | + return AE_MIN32(y, AE_MOVDA32(127)); |
| 559 | +} |
| 560 | + |
| 561 | +// One 4-bit split row times x16, accumulated scaled by 256. AE_L8X4F puts |
| 562 | +// each byte in the top half of a 16-bit lane, so masking with 0x0F00 gives |
| 563 | +// u[k] << 8 from the low nibble, and shifting right by 4 first gives |
| 564 | +// u[k + H] << 8 from the high one: the same scaling nnlib's int8 kernel |
| 565 | +// uses, so the weights never pass through scratch. |
| 566 | +inline __attribute__((always_inline)) void mac_split_4bit( |
| 567 | + ae_int64& acc, |
| 568 | + const ae_int16x4 w, |
| 569 | + const ae_int16x4 vlo, |
| 570 | + const ae_int16x4 vhi, |
| 571 | + const ae_int16x4 mask) { |
| 572 | + AE_MULAAAAQ16(acc, AE_AND16(w, mask), vlo); |
| 573 | + AE_MULAAAAQ16(acc, AE_AND16(AE_SRAI16(w, 4), mask), vhi); |
| 574 | +} |
| 575 | + |
| 576 | +// Fused 4-bit split GEMV, four output rows at a time: reads each packed row |
| 577 | +// once, multiplies, and requantizes, with no unpacked tile and no nnlib call. |
| 578 | +// Needs in_dim a multiple of 8 and 4-byte-aligned weights, for the 4-byte |
| 579 | +// weight loads and the 8-byte loads of x16. |
| 580 | +void fused_split_4bit_gemv( |
| 581 | + int8_t* __restrict__ out, |
| 582 | + const uint8_t* __restrict__ w, |
| 583 | + const int16_t* __restrict__ x16, |
| 584 | + const int32_t in_dim, |
| 585 | + const int32_t out_dim, |
| 586 | + const int32_t* __restrict__ bias, |
| 587 | + const int32_t offset_fold, |
| 588 | + const int32_t* __restrict__ mult, |
| 589 | + const int32_t* __restrict__ shift, |
| 590 | + const int32_t qstride, |
| 591 | + const int32_t out_zp) { |
| 592 | + const int32_t half = in_dim >> 1; |
| 593 | + const ae_int16x4 mask = AE_MOVDA16(0x0F00); |
| 594 | + int32_t r = 0; |
| 595 | + for (; r + 4 <= out_dim; r += 4) { |
| 596 | + const signed char* p0 = reinterpret_cast<const signed char*>(w + r * half); |
| 597 | + const signed char* p1 = p0 + half; |
| 598 | + const signed char* p2 = p1 + half; |
| 599 | + const signed char* p3 = p2 + half; |
| 600 | + const ae_int16x4* plo = reinterpret_cast<const ae_int16x4*>(x16); |
| 601 | + const ae_int16x4* phi = reinterpret_cast<const ae_int16x4*>(x16 + half); |
| 602 | + ae_int64 a0 = AE_ZERO64(); |
| 603 | + ae_int64 a1 = AE_ZERO64(); |
| 604 | + ae_int64 a2 = AE_ZERO64(); |
| 605 | + ae_int64 a3 = AE_ZERO64(); |
| 606 | + for (int32_t c = 0; c < half; c += 4) { |
| 607 | + ae_int16x4 vlo, vhi, w0, w1, w2, w3; |
| 608 | + AE_L16X4_IP(vlo, plo, 8); |
| 609 | + AE_L16X4_IP(vhi, phi, 8); |
| 610 | + AE_L8X4F_IP(w0, p0, 4); |
| 611 | + AE_L8X4F_IP(w1, p1, 4); |
| 612 | + AE_L8X4F_IP(w2, p2, 4); |
| 613 | + AE_L8X4F_IP(w3, p3, 4); |
| 614 | + mac_split_4bit(a0, w0, vlo, vhi, mask); |
| 615 | + mac_split_4bit(a1, w1, vlo, vhi, mask); |
| 616 | + mac_split_4bit(a2, w2, vlo, vhi, mask); |
| 617 | + mac_split_4bit(a3, w3, vlo, vhi, mask); |
| 618 | + } |
| 619 | + const ae_int32x2 acc01 = AE_ADD32( |
| 620 | + unscale_pair(a0, a1), |
| 621 | + AE_MOVDA32X2(bias[r] + offset_fold, bias[r + 1] + offset_fold)); |
| 622 | + const ae_int32x2 acc23 = AE_ADD32( |
| 623 | + unscale_pair(a2, a3), |
| 624 | + AE_MOVDA32X2(bias[r + 2] + offset_fold, bias[r + 3] + offset_fold)); |
| 625 | + const ae_int32x2 y01 = requantize_pair( |
| 626 | + acc01, |
| 627 | + mult[r * qstride], |
| 628 | + mult[(r + 1) * qstride], |
| 629 | + shift[r * qstride], |
| 630 | + shift[(r + 1) * qstride], |
| 631 | + out_zp); |
| 632 | + const ae_int32x2 y23 = requantize_pair( |
| 633 | + acc23, |
| 634 | + mult[(r + 2) * qstride], |
| 635 | + mult[(r + 3) * qstride], |
| 636 | + shift[(r + 2) * qstride], |
| 637 | + shift[(r + 3) * qstride], |
| 638 | + out_zp); |
| 639 | + out[r] = static_cast<int8_t>(AE_MOVAD32_H(y01)); |
| 640 | + out[r + 1] = static_cast<int8_t>(AE_MOVAD32_L(y01)); |
| 641 | + out[r + 2] = static_cast<int8_t>(AE_MOVAD32_H(y23)); |
| 642 | + out[r + 3] = static_cast<int8_t>(AE_MOVAD32_L(y23)); |
| 643 | + } |
| 644 | + for (; r < out_dim; ++r) { |
| 645 | + const signed char* p0 = reinterpret_cast<const signed char*>(w + r * half); |
| 646 | + const ae_int16x4* plo = reinterpret_cast<const ae_int16x4*>(x16); |
| 647 | + const ae_int16x4* phi = reinterpret_cast<const ae_int16x4*>(x16 + half); |
| 648 | + ae_int64 a0 = AE_ZERO64(); |
| 649 | + for (int32_t c = 0; c < half; c += 4) { |
| 650 | + ae_int16x4 vlo, vhi, w0; |
| 651 | + AE_L16X4_IP(vlo, plo, 8); |
| 652 | + AE_L16X4_IP(vhi, phi, 8); |
| 653 | + AE_L8X4F_IP(w0, p0, 4); |
| 654 | + mac_split_4bit(a0, w0, vlo, vhi, mask); |
| 655 | + } |
| 656 | + const ae_int32x2 acc = |
| 657 | + AE_ADD32(unscale_pair(a0, a0), AE_MOVDA32(bias[r] + offset_fold)); |
| 658 | + const int32_t m = mult[r * qstride]; |
| 659 | + const int32_t s = shift[r * qstride]; |
| 660 | + out[r] = static_cast<int8_t>( |
| 661 | + AE_MOVAD32_H(requantize_pair(acc, m, m, s, s, out_zp))); |
| 662 | + } |
| 663 | +} |
| 664 | +#endif |
| 665 | + |
496 | 666 | } // namespace |
497 | 667 |
|
498 | 668 | void quantized_fully_connected_packed_out( |
@@ -534,6 +704,39 @@ void quantized_fully_connected_packed_out( |
534 | 704 | const bool fast_path = out.scalar_type() == ScalarType::Char && |
535 | 705 | in.scalar_type() == ScalarType::Char && weight_zp == 0; |
536 | 706 |
|
| 707 | +#if PACKED_FC_FUSED_4BIT |
| 708 | + if (fast_path && weight_bits == 4 && (in_dim & 7) == 0 && |
| 709 | + row_bytes * 2 == in_dim && |
| 710 | + (reinterpret_cast<uintptr_t>(w_data) & 3) == 0) { |
| 711 | + int16_t* __restrict__ x16 = reinterpret_cast<int16_t*>( |
| 712 | + kernels::allocate_temp_memory(ctx, in_dim * sizeof(int16_t))); |
| 713 | + if (x16 != nullptr) { |
| 714 | + const int8_t* __restrict__ in_data = in.const_data_ptr<int8_t>(); |
| 715 | + int8_t* __restrict__ out_data = out.mutable_data_ptr<int8_t>(); |
| 716 | + const int32_t n_in = static_cast<int32_t>(in_dim); |
| 717 | + const int32_t n_out = static_cast<int32_t>(out_dim); |
| 718 | + for (int32_t i = 0; i < static_cast<int32_t>(leading_dims); ++i) { |
| 719 | + const int32_t s = widen_input( |
| 720 | + x16, in_data + i * n_in, n_in, static_cast<int32_t>(in_zero_point)); |
| 721 | + // Offset binary, B = 8: see the offset fold below. |
| 722 | + fused_split_4bit_gemv( |
| 723 | + out_data + i * n_out, |
| 724 | + w_data, |
| 725 | + x16, |
| 726 | + n_in, |
| 727 | + n_out, |
| 728 | + bias_data, |
| 729 | + -8 * s, |
| 730 | + mult_data, |
| 731 | + shift_data, |
| 732 | + static_cast<int32_t>(qparam_stride), |
| 733 | + static_cast<int32_t>(out_zero_point)); |
| 734 | + } |
| 735 | + return; |
| 736 | + } |
| 737 | + } |
| 738 | +#endif |
| 739 | + |
537 | 740 | const int64_t row_tile = rows_per_tile(in_dim); |
538 | 741 | int8_t* __restrict__ tile = nullptr; |
539 | 742 | int32_t* __restrict__ tile_mult = nullptr; |
|
0 commit comments