From 141212dd88ae05a6613384e6aaa9fdd4b6edd561 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Wed, 28 Oct 2026 12:06:53 +0100 Subject: [PATCH] gemma4_bf16: asymmetric int5 linears (w5a8i8 and w5a16 over a scale per 128) a4 at five bits: u5 codes, a u8 zero point (0..31) and an f16 scale per [1 x 128] (libquant `codes=u5 zero=u8 group=128 scale=f16`), ParoQuant int5's grid with no rotation. Read by w5a8i8 over quant_act_i8g's per-(row, 128) int8 activation above 64 rows and by w5a16 over the input cast to f16 up to 64, as a4 reads w4a8i8_asym and w4a16_asym; q|k|v stay one GEMM. The views, row parts and column cuts are a4's. arch_test: the 31B form on both asymmetric grids at one and two ranks. --- arch/gemma4_bf16/gemma4_bf16.cpp | 27 ++++++--- tests/arch_test.cpp | 99 ++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 9 deletions(-) diff --git a/arch/gemma4_bf16/gemma4_bf16.cpp b/arch/gemma4_bf16/gemma4_bf16.cpp index d1245fd1..0b7e9413 100644 --- a/arch/gemma4_bf16/gemma4_bf16.cpp +++ b/arch/gemma4_bf16/gemma4_bf16.cpp @@ -130,6 +130,10 @@ struct G4Linear { * per [1 x 128] (libquant `codes=u4 zero=u8 group=128 scale=f16`), gemm_nt_q at w4a8i8_asym * over the same per-(row, 128) int8 activation as i8g. */ bool a4 = false; + /* true with a4: the same grid at five bits, u5 codes and a zero point 0..31 (libquant + * `codes=u5 zero=u8 group=128 scale=f16`), ParoQuant int5's grid with no rotation. Read by + * w5a8i8 and w5a16 where a4 reads w4a8i8_asym and w4a16_asym, over the same activations. */ + bool w5 = false; bool grouped() const { return i8g || a4; } /* a4 at decode heights, as LinearFP8's w4 (rad_fp8.h): up to kA4Split rows the bf16 input is * cast to f16 (`a16`) for w4a16_asym's skinny kernel; w4a8i8_asym has only a prefill kernel, @@ -224,9 +228,10 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char const RadEncPlane* sp = rad_enc_plane(&enc, "scale"); const RadEncPlane* zp = rad_enc_plane(&enc, "zero"); a4 = known && rad_enc_is(&enc, "affine") && enc.n_planes == 3 && !enc.transform[0] && - cdt == RAD_U4 && enc.plane[0].block[1] == 1 && sp && sp->dtype == RAD_F16 && - sp->block[0] == 1 && sp->block[1] == RAD_FP8_BLOCK && zp && zp->dtype == RAD_U8 && - zp->block[0] == 1 && zp->block[1] == RAD_FP8_BLOCK; + (cdt == RAD_U4 || cdt == RAD_U5) && enc.plane[0].block[1] == 1 && sp && + sp->dtype == RAD_F16 && sp->block[0] == 1 && sp->block[1] == RAD_FP8_BLOCK && zp && + zp->dtype == RAD_U8 && zp->block[0] == 1 && zp->block[1] == RAD_FP8_BLOCK; + w5 = a4 && cdt == RAD_U5; } if (i8g || a4) { if (N % 16 || K % RAD_FP8_BLOCK) { @@ -236,7 +241,8 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char return RAD_E_INVAL; } if (a4) { - w = decl_view(b, wn, nullptr, "codes", RAD_U4, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); + w = decl_view(b, wn, nullptr, "codes", w5 ? RAD_U5 : RAD_U4, { N, K }, RAD_ACCESS_PER_TOKEN, + shard, grp, parts, np); ws = decl_view(b, nm.f("%s.scale", base), wn, "scale,zero", RAD_F16, { N, K }, RAD_ACCESS_PER_TOKEN, shard, grp, parts, np); } else { @@ -264,7 +270,7 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char op = rw(b, RAD_OP(b, "gemm_nt_q", RAD_PARAMS(RAD_RANGE("M", 1, g.max_tok), RAD_INT("N", N), RAD_INT("K", K), RAD_INT("group", RAD_FP8_BLOCK), - RAD_STR("dtype", a4 ? "w4a8i8_asym" : "i8a8")), + RAD_STR("dtype", w5 ? "w5a8i8" : a4 ? "w4a8i8_asym" : "i8a8")), RAD_WEIGHTS(w, ws)), { q8, s8 }, { y }); if (!op) return RAD_E_INVAL; @@ -279,7 +285,8 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char { a }, { a16 }); op_dec = rw(b, RAD_OP(b, "gemm_nt_q", RAD_PARAMS(RAD_RANGE("M", 1, a4_split), RAD_INT("N", N), RAD_INT("K", K), - RAD_INT("group", RAD_FP8_BLOCK), RAD_STR("dtype", "w4a16_asym")), + RAD_INT("group", RAD_FP8_BLOCK), + RAD_STR("dtype", w5 ? "w5a16" : "w4a16_asym")), RAD_WEIGHTS(w, ws)), { a16 }, { y }); if (!op_a16 || !op_dec) return RAD_E_INVAL; @@ -328,7 +335,7 @@ inline int G4Linear::declare(RadBuilder* b, Names& nm, const Geom& g, const char char en[256]; rad_enc_format(&enc, en, sizeof en); fprintf(stderr, "radiance: '%s' is %s, and this model's linears are plain bf16, w4a8, w8a8, " - "int8 a 128 (i8a8) or asymmetric int4 a 128 -- none matches\n", wn, en); + "int8 a 128 (i8a8) or asymmetric int4 or int5 a 128 -- none matches\n", wn, en); return RAD_E_DTYPE; } @@ -514,11 +521,13 @@ int Gemma4Attn::declare(RadBuilder* b, Names& nm, const Geom& geom, int l, const if (cls.has_v) RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src, v_src })); else RAD_ARCH_TRY(map_concat(b, qkv_wn, { q_src, k_src })); { - /* one q|k|v GEMM wherever the weight is on a quantised grid: w4a8 / w8a8, i8a8 or a4 */ + /* one q|k|v GEMM wherever the weight is on a quantised grid: w4a8 / w8a8, i8a8, a4 or + * its five-bit form */ RadEncoding e{}; fused = weight_enc(b, qkv_wn, &e) && (g4_grid_w4(e) || linear_is_w8(e) || (rad_enc_is(&e, "affine") && e.n_planes == 3 && - e.plane[0].dtype == RAD_U4)); + (e.plane[0].dtype == RAD_U4 || + e.plane[0].dtype == RAD_U5))); } if (fused) { if (!qkv_slot) return RAD_E_INVAL; diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index a28486a0..f338ed96 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -4851,6 +4851,105 @@ TEST(the_gemma4_e4b_form_is_refused_where_it_cannot_run) { } } +/* The 31B's linears on the asymmetric grids, u4 (a4) or u5 codes with a u8 zero point and an f16 + * scale per [1 x 128], at one and two ranks: each is two GEMMs over the codes and the joined + * (scale, zero) view, up to 64 rows the input cast to f16 for w4a16_asym / w5a16, above that + * quant_act_i8g's int8 codes for w4a8i8_asym / w5a8i8. q|k|v stay one GEMM; o_proj and down_proj + * are cut along K, in whole scale groups. A decode step issues only the f16 band, a step of 200 + * rows only the int8 one. */ +TEST(the_gemma4_31b_form_reads_asymmetric_int4_and_int5_linears) { + static const int32_t tall_out[] = { 99, 199 }; + for (const uint32_t cdt : { (uint32_t)RAD_U4, (uint32_t)RAD_U5 }) { + const bool five = cdt == RAD_U5; + const std::string dec = five ? "w5a16" : "w4a16_asym", pre = five ? "w5a8i8" : "w4a8i8_asym"; + RadEncoding e = rad_enc_affine(cdt, RAD_F16, 1, 128); + rad_enc_add_plane(&e, "zero", RAD_U8, 1, 128); + for (int world : { 1, 2 }) { + for (int rank = 0; rank < world; ++rank) { + RadBuilder b; + b.encs = { { "attn_qkv.", e }, { "attn_o.", e }, { "ffn_gate_up.", e }, + { "ffn_down.", e } }; + RadModelMeta meta = gemma4_meta(); + RadBuildCtx ctx = build_ctx(rank, world); + CHECK_OK(gemma4_bf16::declare(&b, &meta, &ctx)); + CHECK(!gemma4_bf16::g_model[rank].fuse); + int n_dec = 0, n_pre = 0, other = 0; + for (const RecOp& o : b.ops) { + if (o.op != "gemm_nt_q") continue; + const std::string dt = pstr(&o, "dtype"); + const RecParam* m = param(&o, "M"); + REQUIRE(m != nullptr); + if (dt == dec) { + ++n_dec; + CHECK_EQ(m->ihi, 64LL); + } else if (dt == pre) { + ++n_pre; + CHECK_EQ(m->ihi, (long long)ctx.max_tok); + } else { + ++other; + } + CHECK_EQ(pint(&o, "group"), 128); + CHECK(param(&o, "pb1") == nullptr); + } + /* twelve layers of q|k|v, o, gate_up and down */ + CHECK_EQ(n_dec, 48); + CHECK_EQ(n_pre, 48); + CHECK_EQ(other, 0); + CHECK_EQ(count_op(b, "quant_act_i8g"), 48); + int codes = 0, joined = 0; + for (const RecWeight& w : b.weights) { + if (!w.d.planes) continue; + if (std::string(w.d.planes) == "codes") { + ++codes; + CHECK_EQ(w.d.dtype, cdt); + } else if (std::string(w.d.planes) == "scale,zero") { + ++joined; + CHECK_EQ(w.d.dtype, (uint32_t)RAD_F16); + } + } + CHECK_EQ(codes, 48); + CHECK_EQ(joined, 48); + /* o_proj of a sliding (0) and a global (5) layer, and down_proj: this rank's K */ + const std::initializer_list> cut = { + { "blk.0.attn_o", 32 * 256 }, { "blk.5.attn_o", 32 * 512 }, + { "blk.0.ffn_down", 21504 } }; + for (const auto& [base, k] : cut) + for (const char* v : { ".weight", ".scale" }) { + const RecWeight* w = find_w(b, base + v); + REQUIRE(w != nullptr); + CHECK_EQ(w->d.shard, (int)RAD_SHARD_COL); + CHECK_EQ(w->d.shape[0], (int64_t)5376); + CHECK_EQ(w->d.shape[1], k / world); + CHECK_EQ(w->d.shape[1] % 128, (int64_t)0); + } + + RadBatch tall = prefill_batch(); + tall.n_tok = 200; tall.max_q_len = 100; tall.max_ctx_len = 100; + tall.out_ids = tall_out; + for (const RadBatch& batch : { decode_batch(), tall }) { + RadCtx c; c.b = &b; c.rank = rank; c.world = world; c.batch = &batch; + gemma4_bf16::step(&c, &batch); + CHECK_LINT(b, &c); + CHECK(c.step_fail.empty()); + const std::string want = batch.n_tok <= 64 ? dec : pre; + int gemms = 0, casts = 0; + for (const auto& i : c.issues) { + const RecOp* o = b.op_of(i.op); + if (!o) continue; + if (o->op == "gemm_nt_q") { + ++gemms; + CHECK_EQ(pstr(o, "dtype"), want); + } + if (o->op == "cast" && pstr(o, "to") == "f16") ++casts; + } + CHECK_EQ(gemms, 48); + CHECK_EQ(casts, batch.n_tok <= 64 ? 48 : 0); + } + } + } + } +} + TEST(gemma4_attention_shapes_for_both_classes) { RadBuilder b; RadModelMeta meta = gemma4_meta();