Skip to content

Commit b963cb9

Browse files
authored
Packed HiFi FC: fused 4-bit kernel on HiFi4 (#23910)
Summary: The HiFi `quantized_fully_connected_packed` kernel unpacks a tile of 4-bit rows into scratch and hands it to nnlib's per-channel int8 matmul. The weights therefore make a round trip through memory: unpacked, stored to scratch, loaded back. Each tile also pays nnlib's per-call argument checks, including one over every row's shift, and a separate bias-fold loop. On HiFi4 this adds a fused path for 4-bit that skips the tile and nnlib: - The kernel widens (x - zp) to int16 once per input vector, which every row then reuses. - AE_L8X4F loads 4 packed bytes into the top halves of four 16-bit lanes. Masking with 0x0F00 gives u[k] << 8 from the low nibbles; shifting right by 4 first gives u[k + in_dim/2] << 8 from the high ones. That is the same scaling nnlib's int8 kernel uses, so the values go straight into AE_MULAAAAQ16. - Four rows run at a time. The compiled loop does 32 weights in 7 cycles with a multiply in every bundle; the unpack plus nnlib multiply took 11. - The requantize uses nnlib's own HiFi4 macro (MPY_BY_QUANT_MULT_PER_CHAN_X2_OUT32_SHIFT, then out zero point and clamp), with the offset-binary fold added to the bias as before. Outputs are bit-identical to the nnlib path. The path needs in_dim a multiple of 8 and 4-byte-aligned weights, and only 2 * in_dim bytes of scratch. HiFi1 and HiFi5 requantize differently in nnlib and keep the existing path, as do 6-bit and every other fallback. Differential Revision: D125346691 Pull Request resolved: #23910
1 parent 141f543 commit b963cb9

2 files changed

Lines changed: 229 additions & 9 deletions

File tree

‎backends/cadence/hifi/operators/op_quantized_fully_connected_packed_out.cpp‎

Lines changed: 203 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
#include <cmath>
1515
#include <cstring>
1616

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+
1732
// Row-packed sub-byte fully-connected on HiFi.
1833
//
1934
// The weight arrives as [out_dim, packed_row_bytes] int8, where the bytes are
@@ -38,6 +53,9 @@
3853
// GEMV, so it is memory bound on weight traffic and 6-bit moves 25% fewer
3954
// bytes than 8-bit.
4055
//
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+
//
4159
// There is no fatal path here. Which kernel serves this operator is decided at
4260
// build time by operator_fallback.bzl, so there is no generic kernel linked to
4361
// fall back to at runtime; aborting would take the model down. Anything the
@@ -493,6 +511,158 @@ void packed_fully_connected_scalar(
493511
}
494512
}
495513

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+
496666
} // namespace
497667

498668
void quantized_fully_connected_packed_out(
@@ -534,6 +704,39 @@ void quantized_fully_connected_packed_out(
534704
const bool fast_path = out.scalar_type() == ScalarType::Char &&
535705
in.scalar_type() == ScalarType::Char && weight_zp == 0;
536706

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+
537740
const int64_t row_tile = rows_per_tile(in_dim);
538741
int8_t* __restrict__ tile = nullptr;
539742
int32_t* __restrict__ tile_mult = nullptr;

‎backends/cadence/hifi/operators/tests/test_op_quantized_fully_connected_packed_out.cpp‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,9 @@ alignas(16) uint8_t scratch_[32 * 1024];
199199

200200
class HiFiQuantizedFullyConnectedPackedTest : public OperatorTest {};
201201

202-
// in_dim = 32 takes the vector unpack; out_dim = 136 spans two row tiles
203-
// (128 + 8), so the tile loop and the per-channel qparam offsets are covered.
202+
// in_dim = 32 takes the vector unpack (on HiFi4, 4-bit takes the fused kernel
203+
// instead); out_dim = 136 spans two row tiles (128 + 8), so the tile loop and
204+
// the per-channel qparam offsets are covered.
204205
TEST_F(HiFiQuantizedFullyConnectedPackedTest, VectorUnpack) {
205206
for (int64_t bits : {4, 6}) {
206207
SCOPED_TRACE(bits);
@@ -210,14 +211,29 @@ TEST_F(HiFiQuantizedFullyConnectedPackedTest, VectorUnpack) {
210211
}
211212
}
212213

213-
// in_dim = 40 is not a multiple of 32, so the scalar unpack serves the nnlib
214-
// path.
214+
// in_dim = 44 is not a multiple of 8, so the scalar unpack serves the nnlib
215+
// path on every core (4-bit at a multiple of 8 takes the fused kernel on
216+
// HiFi4).
215217
TEST_F(HiFiQuantizedFullyConnectedPackedTest, ScalarUnpack) {
216218
for (int64_t bits : {4, 6}) {
217219
SCOPED_TRACE(bits);
218220
MemoryAllocator allocator(sizeof(scratch_), scratch_);
219221
KernelRuntimeContext ctx(nullptr, &allocator);
220-
run_and_check(ctx, Case{bits, 40, 24});
222+
run_and_check(ctx, Case{bits, 44, 24});
223+
}
224+
}
225+
226+
// The fused 4-bit kernel (HiFi4; other cores take the nnlib path): one
227+
// iteration per row (in_dim = 8), out_dim not a multiple of 4 so the one-row
228+
// tail runs, and a longer row.
229+
TEST_F(HiFiQuantizedFullyConnectedPackedTest, Fused4Bit) {
230+
for (const auto& [in_dim, out_dim] :
231+
{std::pair<int64_t, int64_t>{8, 4}, {40, 22}, {64, 9}}) {
232+
SCOPED_TRACE(testing::Message() << "in_dim " << in_dim);
233+
MemoryAllocator allocator(sizeof(scratch_), scratch_);
234+
KernelRuntimeContext ctx(nullptr, &allocator);
235+
run_and_check(ctx, Case{4, in_dim, out_dim});
236+
run_and_check(ctx, Case{4, in_dim, out_dim, /*scalar_qparams=*/true});
221237
}
222238
}
223239

@@ -267,15 +283,16 @@ TEST_F(
267283
}
268284
}
269285

270-
// Scratch for exactly one weight tile (128 rows x 32 for in_dim = 32) and
286+
// Scratch for exactly one weight tile (128 rows x 36 for in_dim = 36) and
271287
// nothing more: the bias tile allocation fails, so the offset-binary bias
272-
// cannot be folded and the unpack has to subtract it per value instead.
288+
// cannot be folded and the unpack has to subtract it per value instead. 36 is
289+
// not a multiple of 8, which keeps 4-bit off the fused kernel.
273290
TEST_F(HiFiQuantizedFullyConnectedPackedTest, NoBiasScratchSubtractsOffset) {
274291
for (int64_t bits : {4, 6}) {
275292
SCOPED_TRACE(bits);
276-
MemoryAllocator allocator(128 * 32, scratch_);
293+
MemoryAllocator allocator(128 * 36, scratch_);
277294
KernelRuntimeContext ctx(nullptr, &allocator);
278-
run_and_check(ctx, Case{bits, 32, 136});
295+
run_and_check(ctx, Case{bits, 36, 136});
279296
}
280297
}
281298

0 commit comments

Comments
 (0)