From b27f352259ddce30228192441e2f0a50e38672f9 Mon Sep 17 00:00:00 2001 From: Jintak Han Date: Mon, 16 Nov 2026 17:13:01 +0900 Subject: [PATCH] Adapt gemma4_bf16's SWA implementation to gemma4 --- arch/gemma4/gemma4.cpp | 41 +++++++++++++++++++++++++++++++---------- tests/arch_test.cpp | 17 ++++++++++------- tests/sched_test.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 17 deletions(-) diff --git a/arch/gemma4/gemma4.cpp b/arch/gemma4/gemma4.cpp index 1c920939..d8c0a681 100644 --- a/arch/gemma4/gemma4.cpp +++ b/arch/gemma4/gemma4.cpp @@ -569,24 +569,45 @@ static int declare(RadBuilder* b, const RadModelMeta* meta, const RadBuildCtx* c /* ---- the KV groups, one per geometry: the sliding layers' (256-wide heads) and the full * layers' (512). * - * The sliding layers' group is RAD_KV_FULL, not RAD_KV_WINDOW; the window lives on the - * attention op (`window` 1024), which reads only keys inside it. A window group is not - * prefix-cached (core/mem/prefix.cpp), so a prefix hit on a model with one reuses nothing and - * re-prefills (core/sched). As FULL the group is cached with the others and the drafter's KV - * has no hole. The only loss is recycling below the window, which gains nothing here: the - * planner gives every paged group the same block count, so the full group bounds the tokens a - * card holds anyway. */ + * The sliding layers' group is RAD_KV_WINDOW, sized to the attention window, which is what + * the kernel reads anyway: attn_paged takes `window` 1024 on those layers and never looks at + * a key older than it. As a window group the pool is capped at the window plus one block per + * sequence (core/mem/kv.cpp), so 25 sliding layers cost ~1k tokens of cache each instead of + * the whole context -- at 32k and fp8 KV, 0.4 GiB a sequence where FULL costs 3.5. The full + * layers' group stays RAD_KV_FULL: it is the only one whose keys outlive a window. + * + + * A windowed group is not prefix-cached (core/mem/prefix.cpp): its rows below the window are + * recycled, and adopt() would overfill a table capped at window_blocks. What that costs on + * a prefix hit is only the windowed rows; the full layers' rows still come back from the + * store. The window itself resumes from the deepest retained checkpoint at or below the + * matched end — that checkpoint's slot carries the window behind it — and only the gap between + * that checkpoint and the match end is recomputed. Only where no retained checkpoint sits at + * or below the match do the windowed layers start their cache where the hit ended + * (the re-prefill case); that is the regime gemma3 runs with a DFlash drafter on the + * same core, and it declares its sliding group exactly this way. + * + + * The drafter does not read the trunk's cache. DFlash keeps its own windowed draft KV (2048), + * rebuilt each context pass from the trunk's residual taps; its rows live inside that window + * like every other draft query's, and it has no stake in the trunk's windowed rows outliving + * the window. A windowed trunk group therefore costs the drafter nothing. */ + { + const uint32_t kvdt = rad_kv_cache_dtype(ctx, RAD_BF16); RadKVGroupDecl d{}; - d.kind = RAD_KV_FULL; - d.dtype = rad_kv_cache_dtype(ctx, RAD_BF16); + d.kind = RAD_KV_WINDOW; + d.window = m.ks.window; /* the same 1024 the attention op reads */ + d.dtype = kvdt; d.n_head_kv = m.ks.nkv; d.head_dim = m.ks.hd; m.ks.kv = rad_decl_kv_group(b, nm.f("kv_swa"), &d); + d.kind = RAD_KV_FULL; + d.window = 0; d.n_head_kv = m.kf.nkv; d.head_dim = m.kf.hd; m.kf.kv = rad_decl_kv_group(b, nm.f("kv_full"), &d); - g.kv_dtype = rad_dtype_name(d.dtype); + g.kv_dtype = rad_dtype_name(kvdt); } /* Gemma4ForConditionalGeneration keeps the text model under model.language_model; diff --git a/tests/arch_test.cpp b/tests/arch_test.cpp index 3332acb8..5f464a6b 100644 --- a/tests/arch_test.cpp +++ b/tests/arch_test.cpp @@ -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); diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index 64faf60a..95f995d8 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -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();