From 93389679e8921b545d47c9f4bbc053d99d3c3512 Mon Sep 17 00:00:00 2001 From: keptn42 Date: Mon, 12 Oct 2026 01:53:48 +0000 Subject: [PATCH] kv: a checkpoint slot carries the window behind it A WINDOW group keeps only the blocks its window can still read, so no block list from the prefix cache describes it. A checkpoint slot now carries, after the linear state, window_blocks pages a WINDOW group (every layer, layer-major rows): the window a query at the checkpoint reads, blocks [max(0, P/B - (window_blocks - 1)), P/B) -- the floor recycle_window keeps. window_rows() names those pages from the leader's table, padded per group, so every rank copies its own shard with one vector (followers keep no sequence tables); it answers false when the table no longer holds them. seed_window() gives a sequence with an empty window table fresh blocks at that floor for a restore to fill. checkpoint_save/restore take the rows; a model with no window passes none. --- core/mem/kv.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++-- core/mem/kv.h | 33 +++++++++++-- tests/mem_test.cpp | 62 +++++++++++++++++++++++++ 3 files changed, 200 insertions(+), 8 deletions(-) diff --git a/core/mem/kv.cpp b/core/mem/kv.cpp index 1fba7b77..d43b23d9 100644 --- a/core/mem/kv.cpp +++ b/core/mem/kv.cpp @@ -271,7 +271,13 @@ int KVManager::plan_groups(const std::vector& groups, const Config& /* ---- fixed costs first: they are not negotiable and they are not per-token ---- */ checkpoint_bytes_ = 0; + window_ck_bytes_ = 0; for (const auto& p : plans_) if (p.stateful()) checkpoint_bytes_ += p.bytes_per_state; + /* AND THE WINDOW BEHIND A CHECKPOINT, after the linear state (see has_window_snapshot). */ + for (const auto& p : plans_) + if (p.kind == RAD_KV_WINDOW && !p.zero && p.n_layers > 0 && p.window_blocks > 0) + window_ck_bytes_ += p.window_blocks * p.bytes_per_block; + checkpoint_bytes_ += window_ck_bytes_; /* EVERY RESERVATION IS PADDED TO RAD_ALIGN_UNIT AND THE BUDGET HAS TO SAY SO. Summing the * fixed costs as raw bytes leaves the paged split free to spend the difference, and the plan @@ -1402,7 +1408,7 @@ void* KVManager::checkpoint_ptr(int32_t slot) const { * exactly one piece of code decides its order. Two functions that agreed today would be two * functions that could stop agreeing. */ int KVManager::checkpoint_copy(const int32_t* state_slots, int n_slots, int32_t ck_slot, - bool save, RadStream s) { + bool save, RadStream s, const std::vector* win) { /* A SLOT NOBODY HOLDS IS NOT ONE TO WRITE. Rank 0 hands the ids out, so a follower checks the * shared store rather than a list of its own. Each refusal says which check it is: a slot the * store gave back is a lifetime fault in whoever handed the id out, and nothing about the copy. */ @@ -1466,14 +1472,111 @@ int KVManager::checkpoint_copy(const int32_t* state_slots, int n_slots, int32_t } off += nbytes * p.n_layers; } + + /* THE WINDOW SECTION: window_blocks pages a WINDOW group, page j of the window in slot j, each + * page n_layers rows of one layer's block -- one pitched copy a page. `win` names the pages + * (window_rows); -1 is a page below position zero and is left alone either way. */ + if (window_ck_bytes_ > 0) { + if (!win) { + RAD_ERR("checkpoint %s slot %d: this model snapshots its window and no window rows " + "were named", dir, (int)ck_slot); + return RAD_E_INVAL; + } + size_t wi = 0; + for (size_t g = 0; g < plans_.size(); ++g) { + const KVGroupPlan& p = plans_[g]; + if (p.kind != RAD_KV_WINDOW || p.window_blocks <= 0) continue; + const size_t row0 = wi; + wi += (size_t)p.window_blocks; + if (p.zero || p.n_layers <= 0) continue; + if (wi > win->size() || !p.base) { + RAD_ERR("checkpoint %s slot %d: window group %d has %zu row(s) named for %lld", + dir, (int)ck_slot, (int)g, win->size(), (long long)wi); + return RAD_E_STATE; + } + const int64_t lb = p.layer_bytes_per_block(); + for (int64_t j = 0; j < p.window_blocks; ++j) { + const int32_t b = (*win)[row0 + (size_t)j]; + if (b < 0) continue; + if (b >= p.n_blocks) return RAD_E_STATE; + char* dev = (char*)p.base + (int64_t)b * lb; + char* host = ck + off + j * p.bytes_per_block; + const int cs = save ? rad_memcpy_2d_async(host, lb, dev, p.layer_stride(), lb, + p.n_layers, s) + : rad_memcpy_2d_async(dev, p.layer_stride(), host, lb, lb, + p.n_layers, s); + if (cs < 0) { + RAD_ERR("checkpoint %s slot %d: the device refused window page %lld of " + "group %d: %s", dir, (int)ck_slot, (long long)j, (int)g, + rad_strerror(cs)); + return cs; + } + } + off += p.window_blocks * p.bytes_per_block; + } + if (off > checkpoint_bytes_) return RAD_E_STATE; + } return RAD_OK; } -int KVManager::checkpoint_save(const int32_t* slots, int n, int32_t ck, RadStream s) { - return checkpoint_copy(slots, n, ck, /*save=*/true, s); +int KVManager::checkpoint_save(const int32_t* slots, int n, int32_t ck, RadStream s, + const std::vector* win) { + return checkpoint_copy(slots, n, ck, /*save=*/true, s, win); } -int KVManager::checkpoint_restore(const int32_t* slots, int n, int32_t ck, RadStream s) { - return checkpoint_copy(slots, n, ck, /*save=*/false, s); +int KVManager::checkpoint_restore(const int32_t* slots, int n, int32_t ck, RadStream s, + const std::vector* win) { + return checkpoint_copy(slots, n, ck, /*save=*/false, s, win); +} + +bool KVManager::window_rows(uint64_t id, int64_t pos, std::vector* out) const { + out->clear(); + const Seq* s = find(id); + if (!s || pos <= 0) return false; + for (size_t g = 0; g < plans_.size(); ++g) { + const KVGroupPlan& p = plans_[g]; + if (p.kind != RAD_KV_WINDOW || p.window_blocks <= 0) continue; + const size_t row0 = out->size(); + out->resize(row0 + (size_t)p.window_blocks, -1); + if (p.zero || p.block_size <= 0 || pos % p.block_size != 0) { + if (p.zero) continue; + return false; + } + const int64_t lo = window_lo(p, pos), hi = pos / p.block_size; + const int64_t first = s->first_block_pos[g] / p.block_size; + if (lo < first || hi - first > (int64_t)s->blocks[g].size()) return false; + for (int64_t j = lo; j < hi; ++j) + (*out)[row0 + (size_t)(j - lo)] = s->blocks[g][(size_t)(j - first)]; + } + return true; +} + +int KVManager::seed_window(uint64_t id, int64_t pos) { + Seq* s = find(id); + if (!s) return RAD_E_NOTFOUND; + /* Check every group before taking anything, as ensure() does. */ + for (size_t g = 0; g < plans_.size(); ++g) { + const KVGroupPlan& p = plans_[g]; + if (p.kind != RAD_KV_WINDOW || p.block_size <= 0) continue; + if (!s->blocks[g].empty()) return RAD_E_STATE; + if (pos % p.block_size != 0) return RAD_E_INVAL; + const int64_t need = pos / p.block_size - window_lo(p, pos); + if (need > available_blocks((int32_t)g)) return RAD_E_FULL; + RAD_TRY(grow_for((int32_t)g, need)); + } + for (size_t g = 0; g < plans_.size(); ++g) { + const KVGroupPlan& p = plans_[g]; + if (p.kind != RAD_KV_WINDOW || p.block_size <= 0) continue; + const int64_t lo = window_lo(p, pos), hi = pos / p.block_size; + s->first_block_pos[g] = lo * p.block_size; + for (int64_t j = lo; j < hi; ++j) { + int32_t b = -1; + grab_block((int32_t)g, &b); + s->blocks[g].push_back(b); + } + s->dirty_from[g] = 0; + } + if (s->n_tokens == 0) s->n_tokens = pos; + return RAD_OK; } /* HOW MANY COLUMNS A SEQUENCE'S state_index ROW HAS. One per physical copy would be the obvious diff --git a/core/mem/kv.h b/core/mem/kv.h index 91a4374b..abb615d9 100644 --- a/core/mem/kv.h +++ b/core/mem/kv.h @@ -485,8 +485,34 @@ public: * and the restore has to precede the step that consumes it, and both are ordered by the * stream rather than by waiting. One pitched copy a stateful group between the card and the * store, at the link's rate. RAD_E_INVAL on a bad slot or an unknown sequence. */ - int checkpoint_save(const int32_t* state_slots, int n_slots, int32_t ck_slot, RadStream s); - int checkpoint_restore(const int32_t* state_slots, int n_slots, int32_t ck_slot, RadStream s); + int checkpoint_save(const int32_t* state_slots, int n_slots, int32_t ck_slot, RadStream s, + const std::vector* win = nullptr); + int checkpoint_restore(const int32_t* state_slots, int n_slots, int32_t ck_slot, RadStream s, + const std::vector* win = nullptr); + + /* ---------------- window snapshots (the sliding-window half of a prefix hit) ---------------- + * A WINDOW group keeps only the blocks its window can still read, so no block list from the + * prefix cache describes it. What a later prompt that extends a cached prefix at position P + * needs from it is exactly the window behind P: blocks [lo, P/B) with + * lo = max(0, P/B - (window_blocks - 1)) -- the floor recycle_window keeps for a query at P. + * A checkpoint slot therefore carries, after the linear state, window_blocks pages a WINDOW + * group (every layer, layer-major rows), and a hit at a checkpoint restores them into fresh + * blocks seeded at that floor. + * + * has_window_snapshot(): this rank's slot carries a window section. window_rows() names, from + * the LEADER's table, the block ids [lo, P/B) of every non-zero WINDOW group in plans() order, + * each group padded with -1 to window_blocks -- one vector every rank addresses with its own + * pool, because followers keep no sequence tables. False when the sequence does not hold that + * range (it is gone, or its floor moved past lo). seed_window() gives a sequence with an empty + * WINDOW table the blocks [lo, P/B) a restore will fill, before the ensure that extends it. */ + bool has_window_snapshot() const { return window_ck_bytes_ > 0; } + bool window_rows(uint64_t seq, int64_t pos, std::vector* out) const; + int seed_window(uint64_t seq, int64_t pos); + /* The floor and end (block indices) of the window a query at `pos` reads in a group. */ + static int64_t window_lo(const KVGroupPlan& p, int64_t pos) { + const int64_t b = pos / p.block_size - (p.window_blocks - 1); + return b > 0 ? b : 0; + } /* Byte address of a state slot, for the slot-mapping builder. Null when the pool has no * host-visible base (a device pool), which is the normal case -- the offset is what the step @@ -509,7 +535,7 @@ private: /* One traversal serving both checkpoint directions; see checkpoint_save. */ int checkpoint_copy(const int32_t* state_slots, int n_slots, int32_t ck_slot, bool save, - RadStream s); + RadStream s, const std::vector* win); struct Seq { uint64_t id = 0; @@ -549,6 +575,7 @@ private: HostArena* ck_store_ = nullptr; /* set_checkpoint_store */ int64_t ck_rank_off_ = 0; int64_t checkpoint_bytes_ = 0; + int64_t window_ck_bytes_ = 0; /* the window section of checkpoint_bytes_ */ std::vector clears_; /* grabbed slots the engine must zero */ void* pool_base_ = nullptr; BundleComparison bundling_; diff --git a/tests/mem_test.cpp b/tests/mem_test.cpp index a60437db..73395bcb 100644 --- a/tests/mem_test.cpp +++ b/tests/mem_test.cpp @@ -943,6 +943,68 @@ TEST(kv_preemption_by_recompute_and_the_undeadlockable_request) { CHECK_OK(f.kv.ensure(newcomer, per)); } +/* THE WINDOW BEHIND A PREFIX HIT: a checkpoint slot carries window_blocks pages a WINDOW group, + * window_rows() names the pages a query at P reads from the table that wrote them, seed_window() + * gives a resumed sequence fresh pages at the same floor -- and a table whose floor moved past it + * answers false, which is the fallback (no snapshot, a full re-prefill). */ +TEST(kv_window_snapshot_rows_and_seed) { + std::vector g; + g.push_back(null_group()); + KVGroupInfo k; + k.name = "swa"; + k.decl.kind = RAD_KV_WINDOW; + k.decl.dtype = RAD_BF16; + k.decl.n_head_kv = 2; + k.decl.head_dim = 8; + k.decl.window = 64; + k.block_size = 16; + k.layers = { 0 }; + g.push_back(k); + Config cfg = small_config(); + Pools p; + CHECK_OK(p.configure(cfg, 0, &rad_malloc_allocator(), &rad_malloc_allocator())); + KVManager kv; + CHECK_OK(kv.configure(g, cfg, p.kv())); + CHECK(kv.has_window_snapshot()); + CHECK_EQ(kv.checkpoint_bytes(), 5 * kv.plan(G_FULL)->bytes_per_block); + + CHECK_OK(kv.add_sequence(1)); + CHECK_OK(kv.ensure(1, 400)); /* a prompt of 400 in one chunk */ + std::vector rows; + CHECK(kv.window_rows(1, 400, &rows)); + CHECK_EQ((long long)rows.size(), 5ll); /* padded to window_blocks */ + /* A query at 400 reads back to 337: blocks 21..24, the floor recycle_window keeps. */ + for (int j = 0; j < 4; ++j) CHECK_EQ(rows[(size_t)j], kv.block_table(1, G_FULL)[(size_t)(21 + j)]); + CHECK_EQ(rows[4], -1); + /* A short prefix: the window starts at zero and the rest is padding. */ + CHECK(kv.window_rows(1, 32, &rows)); + CHECK_EQ(rows[0], kv.block_table(1, G_FULL)[0]); + CHECK_EQ(rows[2], -1); + /* Not on a block: no rows. */ + CHECK(!kv.window_rows(1, 40, &rows)); + /* The floor moved past it (a decode step slid the window): the fallback. */ + CHECK_OK(kv.ensure(1, 401)); + CHECK(!kv.window_rows(1, 64, &rows)); + CHECK(kv.window_rows(1, 400, &rows)); + + /* A resumed sequence: seeded at the floor, extended from there, and its rows are its own. */ + CHECK_OK(kv.add_sequence(2)); + CHECK_OK(kv.seed_window(2, 400)); + CHECK_EQ(kv.first_block_pos(2, G_FULL), 336ll); + CHECK_EQ((long long)kv.block_table(2, G_FULL).size(), 4ll); + CHECK_EQ(kv.seq_tokens(2), 400ll); + CHECK_OK(kv.ensure(2, 432, 400)); + CHECK_EQ(kv.first_block_pos(2, G_FULL), 336ll); + CHECK_EQ((long long)kv.block_table(2, G_FULL).size(), 6ll); + std::vector rows2; + CHECK(kv.window_rows(2, 400, &rows2)); + for (int j = 0; j < 4; ++j) CHECK_EQ(rows2[(size_t)j], kv.block_table(2, G_FULL)[(size_t)j]); + /* A table already holding window pages is not seeded twice. */ + CHECK_EQ(kv.seed_window(2, 400), RAD_E_STATE); + kv.free_sequence(1); + kv.free_sequence(2); +} + TEST(kv_window_group_recycles_below_the_window) { std::vector g; g.push_back(null_group());