diff --git a/core/engine.cpp b/core/engine.cpp index 8d13055d..a5096555 100644 --- a/core/engine.cpp +++ b/core/engine.cpp @@ -667,13 +667,21 @@ int Engine::step_loop() { for (const StepEntry& e : plan.e) { if (e.ckpt_restore < 0) continue; if (!state_slots_of(e.seq, &ckpt_slots_)) continue; + /* The window behind the hit, in the blocks admit_one seeded at its floor. */ + if (ranks_[0]->kv.has_window_snapshot() && + !ranks_[0]->kv.window_rows(e.seq, e.ctx_len, &ckpt_win_)) { + RAD_ERR("sequence %llu: no window rows at %d to restore into", + (unsigned long long)e.seq, (int)e.ctx_len); + return RAD_E_STATE; + } for (size_t ri = 0; ri < ranks_.size(); ++ri) { auto& rk = ranks_[ri]; /* This rank's device, for its own stream and its own state slots. */ if (ranks_.size() > 1) (void)rad_dev_set(rk->device); const int s = rk->kv.checkpoint_restore(ckpt_slots_.data(), (int)ckpt_slots_.size(), - e.ckpt_restore, rk->ctx.stream()); + e.ckpt_restore, rk->ctx.stream(), + &ckpt_win_); if (s < 0) { RAD_ERR("could not restore sequence %llu from checkpoint slot %d on " "rank %d: %s", (unsigned long long)e.seq, @@ -772,11 +780,23 @@ int Engine::step_loop() { for (const StepEntry& e : plan.e) { if (e.ckpt_slot < 0 || e.ckpt_tok < 0) continue; if (!state_slots_of(e.seq, &ckpt_slots_)) continue; + /* The window behind the checkpoint position. The chunk that ends there + * still holds it (its ensure kept the window behind the chunk's first + * query), so a miss is a broken invariant -- and skipping would leave the + * cache a slot it commits as valid with somebody else's pages in it. */ + if (ranks_[0]->kv.has_window_snapshot() && + !ranks_[0]->kv.window_rows(e.seq, e.ctx_len + e.ckpt_tok + 1, + &ckpt_win_)) { + RAD_ERR("sequence %llu: the window behind checkpoint %d is not held", + (unsigned long long)e.seq, (int)(e.ctx_len + e.ckpt_tok + 1)); + return RAD_E_STATE; + } for (auto& rk : ranks_) { if (ranks_.size() > 1) (void)rad_dev_set(rk->device); const int s = rk->kv.checkpoint_save(ckpt_slots_.data(), (int)ckpt_slots_.size(), - e.ckpt_slot, rk->ctx.stream()); + e.ckpt_slot, rk->ctx.stream(), + &ckpt_win_); if (s < 0) { RAD_ERR("rank %d could not snapshot sequence %llu into checkpoint slot " "%d: %s", rk->index, (unsigned long long)e.seq, @@ -1807,7 +1827,8 @@ bool Engine::state_slots_of(uint64_t seq, std::vector* out) const { (*out)[(size_t)g] = s; any = true; } - return any; + /* A window snapshot is a snapshot with no state slots (KVManager::has_window_snapshot). */ + return any || (kv.has_window_snapshot() && kv.has_sequence(seq)); } /* ------------------------------------------------------------------ the HTTP frontend */ diff --git a/core/engine.h b/core/engine.h index ea820186..1080f2f0 100644 --- a/core/engine.h +++ b/core/engine.h @@ -226,6 +226,7 @@ private: /* Scratch for the checkpoint copies: the state slots of one sequence, reused so the step path * does not allocate. See state_slots_of. */ std::vector ckpt_slots_; + std::vector ckpt_win_; /* KVManager::window_rows, same reuse */ bool state_slots_of(uint64_t seq, std::vector* out) const; int load_vocab(); diff --git a/core/mem/prefix.cpp b/core/mem/prefix.cpp index a2a9c07e..142bee1a 100644 --- a/core/mem/prefix.cpp +++ b/core/mem/prefix.cpp @@ -158,6 +158,9 @@ int PrefixCache::configure(const Config& cfg, KVManager* kv, int64_t checkpoint_ } interval_ = checkpoint_interval; + /* A WINDOW SNAPSHOT is state in the same sense: a hit is reusable at a checkpoint only, where + * the slot carries the window behind it (KVManager::has_window_snapshot). */ + if (kv->has_window_snapshot() && interval_ > 0) has_linear_ = true; if (has_linear_) { if (interval_ <= 0) { RAD_ERR("checkpoint_interval is %lld but the model has linear-attention state. A " diff --git a/core/sched/geometry.cpp b/core/sched/geometry.cpp index e686c19b..7ceeeb66 100644 --- a/core/sched/geometry.cpp +++ b/core/sched/geometry.cpp @@ -47,12 +47,13 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry * the same block size -- libr4d's is 16 for all of them -- but a hybrid model with a * sliding-window group served by a different kernel is exactly the case ยง7.2 is written for, * and lcm is the answer that is right for both. */ - bool has_linear = false; + bool has_linear = false, has_window = false; for (const KVGroupInfo& kg : prog.kv_groups) { if (kg.decl.kind == RAD_KV_FULL || kg.decl.kind == RAD_KV_WINDOW) { if (kg.block_size > 0) { g.attn_block = lcm64(g.attn_block, kg.block_size); note(g.why, "attn_block", kg.block_size, kg.name.c_str()); + if (kg.decl.kind == RAD_KV_WINDOW && !kg.layers.empty()) has_window = true; } } else { has_linear = true; @@ -124,10 +125,11 @@ int chunk_geometry_resolve(const Program& prog, const Config& cfg, ChunkGeometry * * No linear state means no checkpoints: the interval is a property of a state kind that this * model does not have, and pretending otherwise would split chunks for nothing. */ - if (has_linear && cfg.checkpoint_interval > 0) + if ((has_linear || (has_window && cfg.prefix_cache)) && cfg.checkpoint_interval > 0) g.checkpoint_interval = align_up(cfg.checkpoint_interval, g.quantum); else g.checkpoint_interval = 0; + g.window_ckpt = !has_linear && g.checkpoint_interval > 0; /* One full quantum has to fit in one step. Below that, every chunk is short, no chunk ever * ends on a boundary, and the checkpoint lands wherever the step happened to end -- which is diff --git a/core/sched/geometry.h b/core/sched/geometry.h index 77596dfc..c11ba385 100644 --- a/core/sched/geometry.h +++ b/core/sched/geometry.h @@ -48,6 +48,12 @@ struct ChunkGeometry { /* Every contributor by name, for the startup line and for the refusal. Built once. */ std::string why; + /* NO LINEAR STATE, A SLIDING WINDOW: the checkpoint snapshots the window behind a prompt's + * tail (KVManager::has_window_snapshot) and nothing else. Chunks are not split at the + * interval -- a window snapshot in the middle of a prompt buys a later hit nothing its tail + * does not -- and the interval is only the shortest prompt worth a snapshot. */ + bool window_ckpt = false; + bool checkpoints() const { return checkpoint_interval > 0; } }; diff --git a/core/sched/mem_iface.h b/core/sched/mem_iface.h index 841afbad..f6bf781b 100644 --- a/core/sched/mem_iface.h +++ b/core/sched/mem_iface.h @@ -56,6 +56,10 @@ struct IKVManager { /* A committed step: a WINDOW group gives back what no later query can read. A no-op for a * manager with no windowed group. See KVManager::trim_window. */ virtual void trim_window(uint64_t seq, int64_t keep_from) { (void)seq; (void)keep_from; } + /* The window snapshot of a prefix hit (KVManager::has_window_snapshot / seed_window). The + * defaults are a manager with none: no hit on a windowed trunk is ever resumed. */ + virtual bool has_window_snapshot() const { return false; } + virtual int seed_window(uint64_t seq, int64_t pos) { (void)seq; (void)pos; return -1; } /* Adopt blocks a prefix-cache hit found. `n_tokens` must be a whole multiple of the group's * block size -- a partial block is never shared, because the next token written into it would @@ -294,6 +298,8 @@ struct KVBinding final : IKVManager { int ensure(uint64_t s, int64_t n) override { return m->ensure(s, n); } int ensure_from(uint64_t s, int64_t n, int64_t k) override { return m->ensure(s, n, k); } void trim_window(uint64_t s, int64_t k) override { m->trim_window(s, k); } + bool has_window_snapshot() const override { return m->has_window_snapshot(); } + int seed_window(uint64_t s, int64_t p) override { return m->seed_window(s, p); } int adopt(uint64_t s, int32_t g, const std::vector& b, int64_t n) override { return m->adopt(s, g, b, n); } diff --git a/core/sched/scheduler.cpp b/core/sched/scheduler.cpp index a5bdebbd..1eff1ee4 100644 --- a/core/sched/scheduler.cpp +++ b/core/sched/scheduler.cpp @@ -741,6 +741,7 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg if (geo_.checkpoints()) { const int64_t I = geo_.checkpoint_interval; int64_t next = ((n_computed / I) + 1) * I; /* strictly after n_computed */ + if (geo_.window_ckpt) next = INT64_MAX; /* a window snapshot: the tail only */ if (n_computed + want > next) want = next - n_computed; if (n_computed + want == next) { if (checkpoint_pos) *checkpoint_pos = next; @@ -786,6 +787,7 @@ int64_t Scheduler::plan_chunk(int64_t n_computed, int64_t n_target, int64_t budg bool Scheduler::last_checkpoint(int64_t pos, int64_t target) const { if (!geo_.checkpoints()) return false; const int64_t I = geo_.checkpoint_interval; + if (geo_.window_ckpt) { const int64_t t = tail_checkpoint(target); return t > 0 && t <= pos; } if ((pos / I + 1) * I <= target) return false; return tail_checkpoint(target) <= pos; } @@ -797,6 +799,15 @@ int64_t Scheduler::tail_checkpoint(int64_t target) const { const int64_t I = geo_.checkpoint_interval, Q = geo_.quantum; if (I <= 0 || Q <= 0) return -1; const int64_t tail = target / Q * Q; + /* A WINDOW SNAPSHOT is taken at the tail of any prompt of an interval or more. */ + /* A block short of the end: the next turn re-renders the last few tokens (a generation + * prompt it replaces with the reply), and a snapshot past where the two renderings part is + * one no hit can reach -- measured: every prompt whose length left fewer than ~8 tokens in + * its last block missed. */ + if (geo_.window_ckpt) { + const int64_t t = (target - std::max(Q, 16)) / Q * Q; + return t >= I ? t : -1; + } return tail > I && tail - tail / I * I >= std::max(Q, I / 4) ? tail : -1; } @@ -872,7 +883,7 @@ int64_t Scheduler::busy_cap(int64_t n_computed, int64_t n_target, int64_t budget cap = std::max(cap, floor); const int64_t remaining = n_target - n_computed; int64_t span = remaining; /* where this run of chunks must end */ - if (geo_.checkpoints()) { + if (geo_.checkpoints() && !geo_.window_ckpt) { const int64_t I = geo_.checkpoint_interval; span = std::min(span, ((n_computed / I) + 1) * I - n_computed); } @@ -986,6 +997,11 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { * snapshot to resume from -- that is every recurrent layer continuing from whatever the * slot last held, and it reads as fluent text rather than as a failure. */ if (reuse > 0 && has_linear_state_ && h.checkpoint_slot < 0) reuse = 0; + /* A WINDOW SNAPSHOT COVERS THE TRUNK'S WINDOWED GROUPS -- at the checkpoint and nowhere + * else. The lookup clamped the hit to it (PrefixCache: a window counts as state there), + * so a hit with a slot is exactly at a snapshot; one without is no hit. */ + const bool win_hit = geo_.window_ckpt && kv_->has_window_snapshot() && reuse > 0 && + h.checkpoint_slot >= 0 && reuse == h.n_linear_tokens; /* THE WINDOWED-GROUP CLAMP โ€” a prefix hit on a model whose OWN (trunk) KV includes a * paged group the cache does not index. prefix.cpp skips RAD_KV_WINDOW groups โ€” their @@ -1003,7 +1019,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { * scheduler.h) is repaired by draft refill, which CAPS the hit so the recompute covers * the drafter's window while the trunk reuses the rest; zeroing here would delete that * design. The trunk's hole, which no tail-recompute can fix, is this clamp's own. */ - if (reuse > 0 && hit_holes_trunk_) { + if (reuse > 0 && hit_holes_trunk_ && !win_hit) { reuse = 0; RAD_DEBUG("admit %llu: prefix hit %lld zeroed โ€” a trunk paged group is not " "prefix-cached (windowed); re-prefilling", (unsigned long long)r.id, @@ -1029,11 +1045,18 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) { break; } } + /* The windowed groups start at the floor of the window behind the hit, in fresh + * blocks the entry's restore fills from the snapshot. */ + if (reuse > 0 && win_hit && kv_->seed_window(r.id, reuse) < 0) { + kv_->preempt(r.id); + reuse = 0; + } } r.n_computed = reuse; r.n_cached = reuse; - s.ckpt_src = (reuse > 0 && reuse == h.n_linear_tokens) ? h.checkpoint_slot : -1; + s.ckpt_src = (reuse > 0 && reuse == h.n_linear_tokens && + (has_linear_state_ || win_hit)) ? h.checkpoint_slot : -1; s.hit_attn = h.n_attn_tokens; }