diff --git a/core/sched/mem_iface.h b/core/sched/mem_iface.h index af3e32df..94871128 100644 --- a/core/sched/mem_iface.h +++ b/core/sched/mem_iface.h @@ -104,6 +104,9 @@ struct IKVManager { /* Tokens one block covers, or 0 for a stateful group. */ virtual int64_t block_tokens(int32_t group) const = 0; + /* A checkpoint slot carries this group's window (KVGroupPlan::win_snap): a prefix hit, which + * resumes from such a slot, restores it whole. */ + virtual bool window_snapshot(int32_t group) const { (void)group; return false; } /* Speculative rejection rollback (spec ยง10). A verify writes KV for 1 + n_spec positions and * the sampler accepts a prefix; this shrinks the sequence to `n_tokens` and frees blocks past @@ -333,6 +336,10 @@ struct KVBinding final : IKVManager { const KVGroupPlan* p = m->plan(g); return p && p->paged() ? p->block_size : 0; } + bool window_snapshot(int32_t g) const override { + const KVGroupPlan* p = m->plan(g); + return p && p->win_snap; + } int rollback(uint64_t s, int64_t n) override { return m->rollback(s, n); } int64_t first_block_pos(uint64_t s, int32_t g) const override { return m->first_block_pos(s, g); diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index 17ee92da..fe6e4b4f 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -84,19 +84,28 @@ int Scheduler::init(Program& prog, const Config& cfg, IKVManager* kv, IPrefixCac "the prefix cache, checkpoints and speculation", rswa_windows_.size(), (long long)rswa_windows_[0].tokens); - /* A hit can leave a drafter's own cache short only where a paged group is not prefix-cached - * (scheduler.h, hit_holes_draft_). With the cache off cached_groups() is empty and this is - * true, which costs nothing: no hits are taken, and every first block pass is at ctx_len 0. */ + /* A hit can leave a drafter's own cache short only where a paged group is neither + * prefix-cached nor carried by the window snapshot a hit resumes from (scheduler.h, + * hit_holes_draft_). With the cache off cached_groups() is empty and this is true, which + * costs nothing: no hits are taken, and every first block pass is at ctx_len 0. */ { /* Paged as the cache counts it (KVGroupPlan::paged(), via block_tokens()), not kvgeom_'s * `paged` flag, which is also true for an empty placeholder group and would pin this * predicate true. */ - size_t n_paged = 0; - for (size_t i = 0; i < kvgeom_.g.size(); ++i) - if (kv->block_tokens((int32_t)i) > 0) ++n_paged; - hit_holes_draft_ = pc->cached_groups().size() < n_paged; - RAD_INFO("draft floor: %zu paged group(s), %zu prefix-cached -> a hit %s", - n_paged, pc->cached_groups().size(), + size_t n_paged = 0, n_snap = 0; + for (size_t i = 0; i < kvgeom_.g.size(); ++i) { + if (kv->block_tokens((int32_t)i) <= 0) continue; + ++n_paged; + const auto& cg = pc->cached_groups(); + /* Snapshots are taken only where the trunk has a window to snapshot too + * (ChunkGeometry::window_ckpt); without them no drafter window rides in one. */ + if (cfg.prefix_cache && geo_.window_ckpt && kv->window_snapshot((int32_t)i) && + std::find(cg.begin(), cg.end(), (int32_t)i) == cg.end()) + ++n_snap; + } + hit_holes_draft_ = pc->cached_groups().size() + n_snap < n_paged; + RAD_INFO("draft floor: %zu paged group(s), %zu prefix-cached, %zu in the window snapshot " + "-> a hit %s", n_paged, pc->cached_groups().size(), n_snap, hit_holes_draft_ ? "leaves a hole in a drafter's own KV" : "leaves no hole"); } /* The trunk's share: a paged group a trunk layer reads (a bound layer below the trunk's layer diff --git a/core/sched/scheduler.h b/core/sched/scheduler.h index 1bab24e7..421c13a3 100644 --- a/core/sched/scheduler.h +++ b/core/sched/scheduler.h @@ -471,11 +471,13 @@ private: IPrefixCache* pc_ = nullptr; /* Does a prefix-cache hit leave a hole in a block drafter's own KV? Only if some paged group - * is not prefix-cached. prefix.cpp skips windowed groups, so a windowed drafter has K and V - * only from the hit position and must wait for its window to slide past it. A full-attention - * drafter's group is cached like the trunk's and its floor is 0; treating it as windowed - * would stop drafting for good, since it has no window to slide. Read off - * IPrefixCache::cached_groups() so prefix.cpp's rule lives in one place. */ + * is neither prefix-cached nor restored from the window snapshot. prefix.cpp skips windowed + * groups, so a windowed drafter the snapshot does not carry has K and V only from the hit + * position and must wait for its window to slide past it. A full-attention drafter's group is + * cached like the trunk's and its floor is 0; treating it as windowed would stop drafting for + * good, since it has no window to slide. Read off IPrefixCache::cached_groups() and + * IKVManager::window_snapshot() so prefix.cpp's and the KV manager's rules live in one + * place. */ bool hit_holes_draft_ = true; /* Does a hit leave a hole in the trunk: a paged group with a trunk layer (bound below * meta.n_layers; drafter layers bind from `base_layer` up) that the cache does not index? diff --git a/tests/sched_fakes.h b/tests/sched_fakes.h index d7990160..f7d95d84 100644 --- a/tests/sched_fakes.h +++ b/tests/sched_fakes.h @@ -339,6 +339,17 @@ struct FakeKV : IKVManager { const KVGeom::G& gg = geom.g[(size_t)g]; return gg.paged ? gg.block_size : 0; } + /* Groups a test says the window snapshot carries (KVGroupPlan::win_snap): a checkpoint slot + * then holds their window, and a hit at a snapshot seeds them (nothing to copy here). */ + std::vector snap; + /* False: the snapshot is taken but its groups are not reported, as before + * IKVManager::window_snapshot existed. */ + bool snap_reported = true; + bool window_snapshot(int32_t g) const override { + return snap_reported && std::find(snap.begin(), snap.end(), g) != snap.end(); + } + bool has_window_snapshot() const override { return !snap.empty(); } + int seed_window(uint64_t, int64_t) override { return snap.empty() ? -1 : 0; } }; struct FakePC : IPrefixCache { diff --git a/tests/sched_test.cpp b/tests/sched_test.cpp index b5bf9f74..7b2d1b86 100644 --- a/tests/sched_test.cpp +++ b/tests/sched_test.cpp @@ -88,7 +88,7 @@ struct Fixture { ~Fixture() { sch.fini(); for (void* p : owned) std::free(p); } void build(int64_t blocks = 4096, int64_t states = 64, int64_t max_tok = 512, - int64_t max_ctx = 4096, bool windowed = false) { + int64_t max_ctx = 4096, bool windowed = false, bool linear = true) { g_gdn_ki = RadKernelInfo{}; g_gdn_ki.name = "gdn_fake"; g_gdn_ki.op = "gdn_chunked"; @@ -109,17 +109,21 @@ struct Fixture { attn.block_size = 16; prog.kv_groups.push_back(attn); - KVGroupInfo lin; - lin.name = "gdn"; - lin.decl.kind = RAD_KV_LINEAR; - lin.decl.state_dim[0] = 128; lin.decl.state_dim[1] = 128; - prog.kv_groups.push_back(lin); + /* `linear` false: a full-attention trunk, as Gemma 4's (with `windowed`, its sliding + * layers), and no GDN op or state buffers below. */ + if (linear) { + KVGroupInfo lin; + lin.name = "gdn"; + lin.decl.kind = RAD_KV_LINEAR; + lin.decl.state_dim[0] = 128; lin.decl.state_dim[1] = 128; + prog.kv_groups.push_back(lin); - KVGroupInfo conv; - conv.name = "conv"; - conv.decl.kind = RAD_KV_CONV; - conv.decl.conv_width = 4; - prog.kv_groups.push_back(conv); + KVGroupInfo conv; + conv.name = "conv"; + conv.decl.kind = RAD_KV_CONV; + conv.decl.conv_width = 4; + prog.kv_groups.push_back(conv); + } /* A windowed paged group, as sliding-window checkpoints pair with a full group. Not * prefix-cached: prefix.cpp indexes only groups whose blocks below the window stay put. @@ -138,22 +142,24 @@ struct Fixture { * stays unbound, and unbound is not trunk-owned */ } - OpInfo op; - op.op = "gdn_chunk_scan"; - op.schema = &g_gdn_schema; - Band b; - b.hi = 4096; - b.dom[RAD_DOMAIN_DEVICE].row = &g_gdn_row; - /* The architecture plugin's declared geometry, in arch/common/rad_block_gdn.h's spelling, - * the key its schema marks RAD_PROLE_SEQ_CHUNK. Both this and the kernel's constraint - * contribute. */ - b.dom[RAD_DOMAIN_DEVICE].geom.set_i("chunk", 64); - op.bands.push_back(b); - prog.ops.push_back(op); + if (linear) { + OpInfo op; + op.op = "gdn_chunk_scan"; + op.schema = &g_gdn_schema; + Band b; + b.hi = 4096; + b.dom[RAD_DOMAIN_DEVICE].row = &g_gdn_row; + /* The architecture plugin's declared geometry, in arch/common/rad_block_gdn.h's + * spelling, the key its schema marks RAD_PROLE_SEQ_CHUNK. Both this and the kernel's + * constraint contribute. */ + b.dom[RAD_DOMAIN_DEVICE].geom.set_i("chunk", 64); + op.bands.push_back(b); + prog.ops.push_back(op); - add_derived("seq_start", "seq_start_index", 257); - add_derived("chunk_bounds", "state_chunk_bounds", 4097); - add_derived("conv_idx", "conv_state_index", 256); + add_derived("seq_start", "seq_start_index", 257); + add_derived("chunk_bounds", "state_chunk_bounds", 4097); + add_derived("conv_idx", "conv_state_index", 256); + } cfg.max_tok = max_tok; cfg.max_seqs = 32; @@ -1575,6 +1581,49 @@ TEST(a_prefix_hit_with_no_checkpoint_near_the_cap_is_kept_whole) { CHECK_EQ(r.rows, 0); } +/* z-lab's DFlash on the Gemma 4 31B: a full-attention block drafter (no window), whose own group the + * prefix cache indexes like the trunk's full layers. A hit resumes the trunk's window from the + * window snapshot, so no paged group is left with a hole and the drafter drafts from the first + * decode. Counting the trunk's window as a hole, as before the snapshot was counted, put the + * drafter's floor at the hit, and a drafter with no window to slide never drafted again that turn. */ +TEST(a_full_attention_drafter_drafts_at_once_after_a_hit_the_window_snapshot_resumes) { + for (int counted = 0; counted < 2; ++counted) { + Fixture f; + f.build(/*blocks=*/4096, /*states=*/64, /*max_tok=*/512, /*max_ctx=*/8192, + /*windowed=*/true, /*linear=*/false); + f.cfg.n_spec = 7; + f.prog.drafter.kind = RAD_DRAFT_BLOCK; + f.prog.drafter.window = 0; + KVGroupInfo d; + d.name = "draft"; + d.decl.kind = RAD_KV_FULL; + d.block_size = 16; + f.prog.kv_groups.push_back(d); + f.kv.setup(f.prog.kv_groups, f.cfg.max_ctx, 4096, 64); + const int32_t swa = 1, draft = 2; + f.kv.snap = { swa }; + f.kv.snap_reported = counted != 0; + f.pc.groups = { 0, draft }; + f.pc.ckpt_lo = 2048; + CHECK_OK(f.start()); + CHECK(f.sch.geometry().window_ckpt); + f.pc.seed(2048); f.pc.lin = 2048; f.pc.src = 3; + const uint64_t a = f.submit(2200, 64); + Scheduler::BlockRow rows[4]; + int n_rows = -1; + for (int st = 0; st < 40; ++st) { + CHECK(f.sch.step() != nullptr); + if (st == 0) CHECK_EQ(f.sch.find(a)->n_computed, (int64_t)2048); + f.commit_plain(11); + const int n = f.sch.block_begin(8, 7, 0, 99, rows, 4); + f.sch.block_end(); + if (!f.sch.find(a)->output.empty()) { n_rows = n; break; } + } + CHECK_EQ(f.sch.metrics().draft_refills, (int64_t)0); + CHECK_EQ(n_rows, counted ? 1 : 0); + } +} + TEST(no_prefix_hit_gives_nothing_back) { const RefillRun r = refill_run(1100, 0); CHECK_EQ(r.m.draft_refills, (int64_t)0);