Adapt gemma4_bf16's SWA implementation to gemma4

This commit is contained in:
Jintak Han authored and StillDeadcode committed 2026-11-16 17:53:54 +01:00
1 parent c9bce68d45
commit b27f352259
3 files changed
+82 -17

No files matched your search

+10 -7
View File
@@ -5734,17 +5734,20 @@ TEST(a_gemma4_moe_trunk_declares_both_arms_and_the_joins) {
/* The sliding group is RAD_KV_FULL with the window on the attention op, since a window group is
* not prefix-cached (arch/gemma4/gemma4.cpp, the KV groups). */
TEST(gemma4_moe_kv_groups_are_both_full_and_the_window_rides_the_op) {
TEST(gemma4_moe_kv_groups_are_window_and_full_and_the_window_rides_the_op) {
RadBuilder b;
RadModelMeta meta = gemma4_moe_meta();
RadBuildCtx ctx = build_ctx();
CHECK_OK(gemma4::declare(&b, &meta, &ctx));
/* The sliding group is a window group: the pool caps at what attn_paged_qrow can read. */
REQUIRE((int)b.kv_groups.size() == 2);
for (const auto& g : b.kv_groups) {
CHECK_EQ((int)g.d.kind, RAD_KV_FULL);
CHECK_EQ(g.d.window, 0);
}
CHECK_EQ(b.kv_groups[0].name, std::string("kv_swa"));
CHECK_EQ((int)b.kv_groups[0].d.kind, RAD_KV_WINDOW);
CHECK_EQ(b.kv_groups[0].d.window, 1024);
CHECK_EQ(b.kv_groups[1].name, std::string("kv_full"));
CHECK_EQ((int)b.kv_groups[1].d.kind, RAD_KV_FULL);
CHECK_EQ(b.kv_groups[1].d.window, 0);
const RecOp* sl = find_op(b, "attn_paged_qrow", 0);
REQUIRE(sl != nullptr);
CHECK_EQ(pint(sl, "head_dim"), 256);
@@ -11361,10 +11364,10 @@ struct ArchCases {
nullptr, 0, {}, 0x7be4844610b039d8ull);
add("gemma4", [this, sc](RadBuilder* b) {
auto c = sc(1, 0); return gemma4::declare(b, &g4m, &c); },
gemma4::step, 0, {}, 0x0f227f5853b6488cull);
gemma4::step, 0, {}, 0xfe8442fba92e5d6dull);
add("gemma4 + DFlash", [this, sc](RadBuilder* b) {
auto c = sc(1, 15); return gemma4::declare(b, &g4df, &c); },
gemma4::step, 15, {}, 0x37bffcc9bd749b88ull);
gemma4::step, 15, {}, 0x5d420566673e5a19ull);
add("embgemma2", [this](RadBuilder* b) {
auto c = eg2_ctx(); return embgemma2::declare(b, eg2.meta(), &c); },
embgemma2::step, 0, {}, 0xac72c705bc97d2b3ull);
+41
View File
@@ -378,6 +378,47 @@ TEST(chunk_geometry_snapshots_a_trunk_window_and_not_a_drafters) {
}
}
/* Gemma 4 exact group layout: a non-drafter RAD_KV_WINDOW trunk group (sliding layers) beside a
* non-drafter RAD_KV_FULL trunk group (full layers) and a drafter own RAD_KV_FULL group. The
* window group is what a prefix hit resumes from (scheduler.cpp trunk share clamp): it must
* still enable window checkpoints and a non zero interval even with a drafter riding a separate
* full group. A drafter own group is RAD_KV_FULL here so it never feeds has window; only the
* trunk window group does. */
TEST(gemma4_layout_window_group_enables_prefix_cache_checkpoints) {
Program prog;
prog.meta.n_layers = 30;
prog.kv_groups.emplace_back(); /* index 0 sentinel */
KVGroupInfo swa;
swa.name = "kv_swa";
swa.decl.kind = RAD_KV_WINDOW;
swa.decl.window = 1024;
swa.block_size = 16;
swa.layers = { 0 }; /* trunk sliding layer < n_layers, not a drafter group */
prog.kv_groups.push_back(swa);
KVGroupInfo full;
full.name = "kv_full";
full.decl.kind = RAD_KV_FULL;
full.block_size = 16;
full.layers = { 1 }; /* trunk full layer < n_layers */
prog.kv_groups.push_back(full);
KVGroupInfo draft;
draft.name = "kv_draft";
draft.decl.kind = RAD_KV_FULL;
draft.block_size = 16;
draft.layers = { 30 }; /* drafter own group, bound at n_layers and above */
draft.drafter = true;
prog.kv_groups.push_back(draft);
Config cfg;
cfg.max_tok = 512;
cfg.checkpoint_interval = 2048; /* the default (rad_core.h) */
cfg.prefix_cache = true;
ChunkGeometry g;
CHECK_OK(chunk_geometry_resolve(prog, cfg, &g));
CHECK(g.checkpoint_interval > 0);
CHECK(g.window_ckpt);
CHECK(window_snapshots(prog, cfg));
}
TEST(chunk_geometry_refuses_a_budget_it_cannot_serve) {
Fixture f;
f.build();