kv: a window group keeps the keys a prefill chunk still reads

ensure() recycled a RAD_KV_WINDOW group against the end of the step, so a
prefill chunk lost the keys its first rows read (a 2185-token prompt kept
no window KV for positions 0..1151). Recycle below the step's first query
(keep_from = n_computed), size the caps for one extra chunk, and trim the
window when a prefill chunk is committed. The old test pinned the bug.

Found while debugging #75 (Gemma 4).
This commit is contained in:
keptn42 authored and sfjes committed 2026-10-10 08:08:43 -07:00
1 parent 77c266ce8f
commit 921b02b60b
6 files changed
+145 -29

No files matched your search

+6 -2
View File
@@ -185,8 +185,12 @@ void BatchBuilder::size_set(Set& s, const std::vector<KVGroupInfo>& groups) {
s.groups[gi].block_size = e.block_size;
int64_t mb = 1;
if (e.paged) {
int64_t span = (e.kind == RAD_KV_WINDOW && e.window > 0 && e.window < max_ctx_)
? e.window : max_ctx_;
/* A WINDOW row holds the window behind the step's first query AND the step's own
* tokens (KVManager::ensure keeps both), so it is sized for a whole chunk past the
* window -- plus the speculative lookahead, which a verify row carries on top. */
const int64_t wspan = e.window + max_tok_ + 2 * ((int64_t)std::max(cfg_.n_spec, 0) + 1);
int64_t span = (e.kind == RAD_KV_WINDOW && e.window > 0 && wspan < max_ctx_)
? wspan : max_ctx_;
mb = (span + e.block_size - 1) / e.block_size + 1;
}
s.groups[gi].max_blocks = mb;
+12
View File
@@ -46,6 +46,16 @@ struct IKVManager {
* free list is empty, which is a scheduling signal and not an error: the caller preempts a
* victim and retries. */
virtual int ensure(uint64_t seq, int64_t n_tokens) = 0;
/* The same, for a step whose first query sits at `keep_from` (the request's n_computed): a
* WINDOW group then keeps every block that query can still read, however long the step is.
* See KVManager::ensure. The default is for a manager with no windowed group. */
virtual int ensure_from(uint64_t seq, int64_t n_tokens, int64_t keep_from) {
(void)keep_from;
return ensure(seq, n_tokens);
}
/* 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; }
/* 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
@@ -282,6 +292,8 @@ struct KVBinding final : IKVManager {
bool has_sequence(uint64_t s) const override { return m->has_sequence(s); }
void free_sequence(uint64_t s) override { m->free_sequence(s); }
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); }
int adopt(uint64_t s, int32_t g, const std::vector<int32_t>& b, int64_t n) override {
return m->adopt(s, g, b, n);
}
+12 -6
View File
@@ -1048,7 +1048,7 @@ int Scheduler::admit_one(SchedReq& s, int64_t budget, int64_t reserve) {
/* THE CACHE GIVES WAY TO A NEW REQUEST as it does to a running one: with nothing running,
* no other path would ever take blocks back from it, and the request would wait for good. */
int rounds = 0;
while (kv_->ensure(r.id, r.n_computed + want) < 0) {
while (kv_->ensure_from(r.id, r.n_computed + want, r.n_computed) < 0) {
if (++rounds > 256 || !cfg_.prefix_cache || pc_->evict(256) <= 0) {
rc = RAD_E_NOMEM;
break;
@@ -1238,7 +1238,7 @@ const RadBatch* Scheduler::step() {
static const int kMaxEvictRounds = 256;
int evict_rounds = 0;
bool failed = false;
while (kv_->ensure(r.id, r.n_computed + want) < 0) {
while (kv_->ensure_from(r.id, r.n_computed + want, r.n_computed) < 0) {
/* THE CACHE GIVES ITS BLOCKS BACK BEFORE A RUNNING REQUEST DOES. Everything the prefix
* cache holds is a saving on work that has already been done once; everything a
* running request holds is work in flight. Evicting first is therefore strictly the
@@ -1450,7 +1450,7 @@ const RadBatch* Scheduler::step() {
int64_t ckpt = -1;
const int64_t grown = e.ckpt_slot < 0 && limit > had
? plan_chunk(r.n_computed, s.seq_len(), limit, &ckpt) : had;
if (grown > had && kv_->ensure(r.id, r.n_computed + grown) >= 0) {
if (grown > had && kv_->ensure_from(r.id, r.n_computed + grown, r.n_computed) >= 0) {
e.n_tokens = (int32_t)grown;
e.produces_token = (r.n_computed + grown >= s.seq_len());
if (ckpt > 0) {
@@ -1690,7 +1690,7 @@ const RadBatch* Scheduler::step_next_locked() {
const int32_t d = s.draft_next;
const int64_t want = 1 + d;
if (ctx_limit_ > 0 && ctx_up + want > ctx_limit_) return nullptr;
if (kv_->ensure(r.id, ctx_up + want) < 0) return nullptr;
if (kv_->ensure_from(r.id, ctx_up + want, r.n_computed) < 0) return nullptr;
StepEntry n;
n.req = &r;
@@ -1831,6 +1831,10 @@ int Scheduler::commit(const StepResult& res) {
if (e.is_prefill) {
r.n_computed += e.n_tokens;
/* The chunk is committed: a window group gives its blocks below the window back now,
* not at this sequence's next ensure(), which may be steps away while others
* prefill. */
kv_->trim_window(r.id, r.n_computed);
step_prefill_tok_ += e.n_tokens;
server::sink_prompt_progress(r.sink, r.n_computed, (int32_t)r.prompt.size(),
r.n_cached);
@@ -2167,7 +2171,8 @@ int Scheduler::serial_begin(int* rounds, SerialRow* out, int max) {
* unwritten in the head's layer for good. The same goes for a row too near the end of the
* context for its rounds, whose last positions do not exist. */
if (can_draft && ctx_limit_ > 0 && head_max + *rounds > ctx_limit_) can_draft = false;
if (can_draft && kv_->ensure(r.id, head_max + *rounds) < 0) can_draft = false;
if (can_draft && kv_->ensure_from(r.id, head_max + *rounds, r.n_computed) < 0)
can_draft = false;
/* THE HISTORY THIS STEP OWES, in two parts. Before the verified positions, the positions
* the step computed whose next token the host holds -- a prefill chunk's prompt -- from
@@ -2418,7 +2423,8 @@ int Scheduler::block_begin(int block, int depth, int window, int32_t mask, Block
/* The query block writes positions n_computed .. n_computed + block - 1, and none of them
* may be at or past the end of the context. */
const bool ok_ctx = ctx_limit_ <= 0 || r.n_computed + block <= ctx_limit_;
const bool ok_kv = ok_room && ok_ctx && kv_->ensure(r.id, r.n_computed + block) >= 0;
const bool ok_kv = ok_room && ok_ctx &&
kv_->ensure_from(r.id, r.n_computed + block, r.n_computed) >= 0;
static const bool dbg = [] { const char* v = std::getenv("RADIANCE_DEBUG_DRAFT");
return v && *v && *v != '0'; }();
if (dbg) {