unlimitedocr: the DeepEncoder tower, R-SWA ring KV and the n-gram ban

DeepEncoder (arch/common/rad_block_sam.h): the checkpoint's SAM ViT-B/16,
neck and CLIP ViT-L/14 producing its 16x17+1 row layout, with a new
`sam_attn` op for the windowed, zero-padded SAM attention and its
decomposed relative-position bias (libref oracle, libavx kernel, libr4d
kernel and selftest). The plugin declares the tower when the container
records `radiance.encoder`, and splices its rows over the placeholders
after embed_lookup.

BOS: the chat path adds BOS when the rendered template does not already
start with it. This checkpoint's template is a bare concatenation and its
tokenizer states add_bos_token, so without it every position was off by
one and the det boxes came out wrong. A media run's placeholders shift
with it.

R-SWA: RAD_KV_RING, paged like a window group but with a per-sequence
floor armed at admission. adv_groups now computes the same clamped
seqused and modulo ring slots the builder stages; the mismatch read the
block table's -1 padding and faulted the card. Prefill stays causal;
prefix cache, checkpoints and speculation are off for the group.

Sampling: sample_ngram_ban in libref/libavx/libr4d and the host chain,
the request knobs, and the container's generation config (35 over 128).

Processor: the checkpoint's grid/mode selection, its tall-last tie order,
Pillow's bicubic pass order, and goldens generated from its own code.

Tools: --mm-max-patches auto-detection accepts a tower stated as per-width
tables under vision_config.image_size, and rad-convert records
radiance.encoder.

Verified: tower stages match the checkpoint's code (rows cos 0.9998), the
device sam_attn selftest is within tolerance, the R-SWA device repro
returns text, a GPU e2e reproduces the reference's first det block, and
the host suite is 88/89 (known webp decode baseline).
This commit is contained in:
Juup committed 2026-10-20 07:04:01 +00:00
1 parent 543cba7416
commit fad4ba7f1a
63 files changed
+6162 -169

No files matched your search

+249 -16
View File
@@ -76,6 +76,7 @@ static const char* kv_kind_name(int k) {
case RAD_KV_WINDOW: return "window";
case RAD_KV_LINEAR: return "linear";
case RAD_KV_CONV: return "conv";
case RAD_KV_RING: return "ring";
default: return "?";
}
}
@@ -126,7 +127,10 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
KVGroupPlan p;
p.index = (int32_t)i;
p.name = g.name.empty() ? fmt("kv%zu", i) : g.name;
p.kind = g.decl.kind;
/* R-SWA: RAD_KV_RING is a window group whose floor is per sequence (declare_runtime_window
* / SeqWindow), so it plans as a RAD_KV_WINDOW group with `runtime_win` set below. No
* other plan-path code has to know the kind. */
p.kind = g.decl.kind == RAD_KV_RING ? RAD_KV_WINDOW : g.decl.kind;
p.n_layers = (int64_t)g.layers.size();
if (p.n_layers <= 0) {
@@ -161,7 +165,9 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
}
if (p.kind == RAD_KV_WINDOW) {
if (g.decl.window <= 0) {
RAD_ERR("KV group '%s': RAD_KV_WINDOW with window 0", p.name.c_str());
RAD_ERR("KV group '%s': %s with window 0", p.name.c_str(),
g.decl.kind == RAD_KV_RING ? "RAD_KV_RING (the ring's W)"
: "RAD_KV_WINDOW");
return RAD_E_INVAL;
}
/* +1 because a window that is not block-aligned straddles one extra block. */
@@ -232,6 +238,28 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
}
bundling_ = compare_bundling(bundles);
/* R-SWA: a declared runtime window turns the group's plan over to per-sequence windows (see
* declare_runtime_window). Applied after every declaration above, so a fixed-window group's
* plan is exactly what it was. */
for (auto& p : plans_) apply_runtime_window(p);
/* A RAD_KV_RING group carries a runtime window by its kind, whether or not the caller had a
* manager to declare it on first (the engine's sizing probes declare it, but plan_groups must
* not depend on that). `window` is the ring's W. */
for (size_t i = 1; i < plans_.size(); ++i) {
KVGroupPlan& p = plans_[i];
if (groups[i].decl.kind != RAD_KV_RING || p.runtime_win) continue;
p.runtime_win = true;
p.runtime_tokens = groups[i].decl.window;
p.win_snap = false;
}
for (const auto& [g, w] : runtime_decl_) {
if (g < 0 || g >= (int32_t)plans_.size() || !plans_[(size_t)g].runtime_win) {
RAD_ERR("declare_runtime_window(%d): the group is not a RAD_KV_WINDOW or RAD_KV_RING "
"group in this program (a runtime window needs a windowed group)", (int)g);
return RAD_E_INVAL;
}
}
/* ---- fixed costs first: they are not per-token ---- */
checkpoint_bytes_ = 0;
window_ck_bytes_ = 0;
@@ -242,7 +270,7 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
for (size_t i = 1; i < plans_.size(); ++i) {
KVGroupPlan& p = plans_[i];
p.win_snap = !has_state && p.kind == RAD_KV_WINDOW && p.window_blocks > 0 &&
!groups[i].drafter;
!p.runtime_win && !groups[i].drafter;
if (p.win_snap && !p.zero && p.n_layers > 0)
window_ck_bytes_ += p.window_blocks * p.bytes_per_block;
}
@@ -256,26 +284,98 @@ int KVManager::plan_groups(const std::vector<KVGroupInfo>& groups, const Config&
if (p.stateful()) fixed += align_up(p.bytes_per_state * p.n_states, RAD_ALIGN_UNIT);
/* And one unit per paged group, whose size need not be a multiple of the alignment. */
for (const auto& p : plans_) if (p.paged()) fixed += RAD_ALIGN_UNIT;
/* A runtime window's ring is per sequence and on top of the context the split sizes: one
* ring a sequence, carved as a fixed cost. Its reference is the context itself, which the
* split already covers (the group is not capped at window_blocks). */
for (const auto& p : plans_)
if (p.runtime_win && !p.zero)
fixed += align_up(max_seqs_ * p.ring_blocks_max() * p.bytes_per_block, RAD_ALIGN_UNIT);
fixed_ = fixed;
ckpts_ = budget_ckpts;
return RAD_OK;
}
/* Windowed groups are capped at window_pool_cap, as in carve(). */
/* Windowed groups are capped at window_pool_cap, as in carve(). A runtime-window group is not:
* its blocks are the reference (one context a sequence, as a FULL group) plus the ring its fixed
* costs already carry. */
int64_t KVManager::ceiling(int64_t ctx) const {
if (ctx <= 0) return 0;
int64_t need = fixed_;
for (const auto& p : plans_) {
if (!p.paged() || p.block_size <= 0) continue;
int64_t blocks = max_seqs_ * ((ctx + p.block_size - 1) / p.block_size);
if (p.kind == RAD_KV_WINDOW && p.window_blocks > 0)
if (p.kind == RAD_KV_WINDOW && !p.runtime_win && p.window_blocks > 0)
blocks = std::min(blocks, window_pool_cap(p));
need += blocks * p.bytes_per_block;
}
return need;
}
/* Moves a WINDOW group's plan to per-sequence runtime windows (declare_runtime_window). Returns
* false for any other group, so plan_groups() can apply it to every plan unconditionally. */
bool KVManager::apply_runtime_window(KVGroupPlan& p) const {
auto it = runtime_decl_.find(p.index);
if (it == runtime_decl_.end() || p.kind != RAD_KV_WINDOW || p.runtime_win) return false;
p.runtime_win = true;
p.runtime_tokens = it->second;
/* The window a sequence may inherit (a fixed-window cap) is no longer the bound, and the
* declare-time window is not the sequence's own, so nothing here is snapshot-able. */
p.win_snap = false;
return true;
}
int KVManager::declare_runtime_window(int32_t g, int64_t window_tokens) {
if (g <= 0 || window_tokens <= 0) {
RAD_ERR("declare_runtime_window: group %d with a window of %lld (a runtime window needs a "
"real group and a positive W)", (int)g, (long long)window_tokens);
return RAD_E_INVAL;
}
const KVGroupPlan* p = plan(g);
if (p && p->kind != RAD_KV_WINDOW) {
RAD_ERR("group '%s' is not RAD_KV_WINDOW or RAD_KV_RING, so it cannot carry a runtime "
"window", p->name.c_str());
return RAD_E_INVAL;
}
if (p && p->runtime_win && p->runtime_tokens != window_tokens) {
/* A RAD_KV_RING group's W is its declaration's `window`, and one already planned for
* another W has its ring in the fixed costs. */
RAD_ERR("declare_runtime_window(%d): group '%s' already carries a runtime window of %lld "
"tokens, and its ring is sized for that", (int)g, p->name.c_str(),
(long long)p->runtime_tokens);
return RAD_E_INVAL;
}
if (configured_) {
RAD_ERR("declare_runtime_window(%d) after the carve: the group's blocks and fixed costs "
"are already sized for a different window", (int)g);
return RAD_E_STATE;
}
runtime_decl_[g] = window_tokens;
/* A plan built before the declaration (the engine's probes, or plan_groups() then declare)
* is updated in place; the next plan_groups() picks the declaration up on its own. */
if (!plans_.empty()) {
if (g >= (int32_t)plans_.size()) return RAD_E_INVAL;
KVGroupPlan& q = plans_[(size_t)g];
/* Whether the plan counted this group's window snapshot section (plan_groups' rule). */
const bool counted = q.win_snap && !q.zero && q.n_layers > 0;
if (!apply_runtime_window(q)) return RAD_OK; /* already applied */
/* The cost this declaration adds, against the plan as published: the ring is one a
* sequence, and the group gives up its window snapshot section. */
fixed_ += align_up(max_seqs_ * q.ring_blocks_max() * q.bytes_per_block, RAD_ALIGN_UNIT);
if (counted) {
const int64_t was = q.window_blocks * q.bytes_per_block;
window_ck_bytes_ = window_ck_bytes_ > was ? window_ck_bytes_ - was : 0;
checkpoint_bytes_ = checkpoint_bytes_ > was ? checkpoint_bytes_ - was : 0;
}
}
return RAD_OK;
}
int64_t KVManager::runtime_window_tokens(int32_t g) const {
const KVGroupPlan* p = plan(g);
return p && p->runtime_win ? p->runtime_tokens : 0;
}
/* The vectors parallel to plans_ are filled at different points of the carve, so a partial carve
* would leave plans_ passing bounds checks (reserve_block checks plans_, then reads free_usable_)
* over empty block state. On failure forget() empties both, so every entry point refuses
@@ -328,11 +428,11 @@ int64_t KVManager::paged_split(int64_t budget_bytes, std::vector<int64_t>* block
if (tokens_cap_ > 0 && tokens > tokens_cap_) tokens = tokens_cap_;
/* A windowed group cannot address past its pool cap; cap it and give the bytes back to
* the others. */
* the others. A runtime-window group's cap is the context plus its ring (a fixed cost). */
int32_t capped = -1;
for (int32_t i : open) {
const auto& p = plans_[i];
if (p.kind != RAD_KV_WINDOW) continue;
if (p.kind != RAD_KV_WINDOW || p.runtime_win) continue;
if (tokens / p.block_size > window_pool_cap(p)) { capped = i; break; }
}
if (capped < 0) break;
@@ -369,7 +469,8 @@ int64_t KVManager::pool_mib_for_tokens(int64_t tokens, int64_t max_mib) const {
int64_t KVManager::zero_blocks(const KVGroupPlan& p, int64_t tokens) const {
if (tokens == kUnboundedTokens) return 0;
const int64_t b = tokens / p.block_size;
if (p.kind == RAD_KV_WINDOW && b > window_pool_cap(p)) return window_pool_cap(p);
if (p.kind == RAD_KV_WINDOW && !p.runtime_win && b > window_pool_cap(p))
return window_pool_cap(p);
return b;
}
@@ -508,20 +609,31 @@ int KVManager::carve_into(Pool& kv_pool) {
configured_ = true;
report_ = bundling_.report();
/* A runtime-window group reads as "ring": its window is the sequence's own, not the plan's. */
auto kind_of = [](const KVGroupPlan& p) {
return p.runtime_win ? "ring" : kv_kind_name(p.kind);
};
for (const auto& p : plans_) {
if (p.paged())
report_ += fmt(" %-14s %-6s %3lld layers block %4lld tok page %8s "
"%7lld blocks = %9s (%lld tokens)\n",
p.name.c_str(), kv_kind_name(p.kind), (long long)p.n_layers,
p.name.c_str(), kind_of(p), (long long)p.n_layers,
(long long)p.block_size, humanb(p.bytes_per_block).c_str(),
(long long)p.n_blocks, humanb(p.pool_bytes).c_str(),
(long long)(p.n_blocks * p.block_size));
else
report_ += fmt(" %-14s %-6s %3lld layers state %8s %7lld slots = %9s\n",
p.name.c_str(), kv_kind_name(p.kind), (long long)p.n_layers,
p.name.c_str(), kind_of(p), (long long)p.n_layers,
humanb(p.bytes_per_state).c_str(),
(long long)p.n_states, humanb(p.pool_bytes).c_str());
}
for (const auto& p : plans_) {
if (!p.runtime_win) continue;
report_ += fmt(" %-14s runtime window W=%lld: a sequence keeps its reference and a ring "
"of at most %lld block(s), rewritten in place\n",
p.name.c_str(), (long long)p.runtime_tokens,
(long long)p.ring_blocks_max());
}
/* The pool's address range, so a GPU fault address can be placed in the carve. */
report_ += fmt(" pool at %p, %s; groups at +%lld..+%lld\n",
pool_base_, humanb(pool_ ? pool_->bytes() : 0).c_str(),
@@ -804,7 +916,17 @@ int64_t KVManager::blocks_for(int32_t g, int64_t n_tokens) const {
const KVGroupPlan* p = plan(g);
if (!p || !p->paged() || n_tokens <= 0) return 0;
int64_t n = ceil_div(n_tokens, p->block_size);
if (p->kind == RAD_KV_WINDOW) n = std::min(n, window_seq_cap(*p));
if (p->kind == RAD_KV_WINDOW) {
if (p->runtime_win) {
/* The reference's blocks up to n, plus one ring. Without a sequence the floor is
* unknown, so the whole context is the bound on the reference. */
const int64_t ring = p->ring_blocks_max();
const int64_t ctx = max_ctx_ > 0 ? max_ctx_ : n_tokens;
n = std::min(n, ceil_div(ctx, p->block_size)) + ring;
} else {
n = std::min(n, window_seq_cap(*p));
}
}
return n;
}
@@ -835,6 +957,7 @@ int KVManager::add_sequence(uint64_t id) {
s.id = id;
s.blocks.resize(plans_.size());
s.first_block_pos.assign(plans_.size(), 0);
s.win.resize(plans_.size());
s.state.assign(plans_.size(), -1);
s.dirty_from.assign(plans_.size(), 0);
seqs_.emplace(id, std::move(s));
@@ -852,6 +975,7 @@ int KVManager::preempt(uint64_t id) {
for (int32_t b : s->blocks[g]) drop_block((int32_t)g, b);
s->blocks[g].clear();
s->first_block_pos[g] = 0;
s->win[g] = SeqWindow{};
s->dirty_from[g] = 0;
/* The recurrent state goes too (see preempt in kv.h, spec §7.1). */
if (s->state[g] >= 0) { drop_state((int32_t)g, s->state[g]); s->state[g] = -1; }
@@ -869,6 +993,11 @@ int KVManager::rollback(uint64_t id, int64_t n_tokens) {
if (n_tokens < 0) return RAD_E_INVAL;
/* Nothing to free; RAD_OK so "every draft accepted" needs no special path in the caller. */
if (n_tokens >= s->n_tokens) return RAD_OK;
/* A runtime-windowed sequence unwinds nothing: the ring's slots are reused once it is full,
* so the slots a rejected verify wrote may already hold newer keys. Speculation stays off for
* these requests (the scheduler never drafts for them); this refuses rather than corrupt. */
for (const SeqWindow& w : s->win)
if (w.set) return RAD_E_UNSUPPORTED;
for (size_t g = 0; g < plans_.size(); ++g) {
const auto& p = plans_[g];
@@ -898,6 +1027,81 @@ int64_t KVManager::first_block_pos(uint64_t id, int32_t g) const {
return s->first_block_pos[(size_t)g];
}
/* ------------------------------------------------------------------ the runtime window */
int KVManager::set_window(uint64_t id, int64_t visible_lo, int64_t window_tokens) {
if (!configured_) return RAD_E_STATE;
Seq* s = find(id);
if (!s) return RAD_E_NOTFOUND;
if (visible_lo < 0 || window_tokens <= 0) return RAD_E_INVAL;
/* The reference is written linearly, so the floor cannot be behind KV the sequence already
* holds: rebasing would have to move blocks the ring has not been shaped for. */
if (visible_lo < s->n_tokens) {
RAD_ERR("sequence %llu: runtime window floor %lld is behind its %lld held tokens",
(unsigned long long)id, (long long)visible_lo, (long long)s->n_tokens);
return RAD_E_STATE;
}
bool any = false;
for (size_t g = 0; g < plans_.size(); ++g) {
const KVGroupPlan& p = plans_[g];
if (p.kind != RAD_KV_WINDOW) continue;
SeqWindow w;
w.set = true;
w.window = window_tokens;
w.visible_lo = visible_lo;
/* Whole blocks: the ring starts at the first boundary at or after the floor, and the
* tokens in [visible_lo, ring_pos) stay in the reference's last block (SeqWindow). The
* ring itself must span all of W: the frozen tokens are the oldest of the tail, so a
* smaller ring would lose the last W - span positions. */
w.ring_pos = ceil_div(visible_lo, p.block_size) * p.block_size;
w.ring_blocks = ceil_div(window_tokens, p.block_size);
if (w.ring_blocks < 1) w.ring_blocks = 1; /* the ring always has somewhere to write */
/* What the sequence can hold: the reference's blocks, then the whole ring. */
const int64_t need = w.ring_pos / p.block_size + w.ring_blocks;
if (p.n_blocks > 0 && need > p.n_blocks) return RAD_E_FULL;
s->win[g] = w;
any = true;
}
if (!any) return RAD_E_UNSUPPORTED;
return RAD_OK;
}
bool KVManager::runtime_window(uint64_t id, int32_t g, SeqWindow* out) const {
const Seq* s = find(id);
if (!s || g < 0 || g >= (int32_t)s->win.size() || !s->win[(size_t)g].set) return false;
if (out) *out = s->win[(size_t)g];
return true;
}
bool KVManager::has_runtime_window(uint64_t id) const {
const Seq* s = find(id);
if (!s) return false;
for (const SeqWindow& w : s->win) if (w.set) return true;
return false;
}
/* The block-table index of the block holding `pos`, or -1 past the table. A runtime window's
* reference maps as a fixed group does (first_block_pos is 0 there: the table starts at position
* 0); its ring maps modulo its blocks, which is what recycles the ring's slots in place. */
int64_t KVManager::table_index(const KVGroupPlan& p, const SeqWindow& w, int64_t first_block_pos,
int64_t pos, int64_t table_len) {
int64_t bi = (pos - first_block_pos) / p.block_size;
if (w.set && pos >= w.ring_pos) {
bi = w.ring_pos / p.block_size +
((pos - w.ring_pos) / p.block_size) % w.ring_blocks;
}
if (bi < 0 || bi >= table_len) return -1;
return bi;
}
/* What ensure() must have in the table for `n_tokens`: the reference grows linearly until the
* floor, then the ring fills up to its whole span and never grows again. */
int64_t KVManager::window_table_len(const KVGroupPlan& p, const SeqWindow& w, int64_t n_tokens) {
const int64_t ref = w.ring_pos / p.block_size;
if (n_tokens <= w.ring_pos) return ceil_div(n_tokens, p.block_size);
return ref + std::min(w.ring_blocks, ceil_div(n_tokens - w.ring_pos, p.block_size));
}
int64_t KVManager::seq_tokens(uint64_t id) const { const Seq* s = find(id); return s ? s->n_tokens : 0; }
int64_t KVManager::take_table_changes(uint64_t id, int32_t g) const {
@@ -1022,11 +1226,15 @@ int64_t KVManager::step_blocks(const KVGroupPlan& p) const {
/* Slides from the step's first query, not its last. The query at keep_from reads keys down to
* keep_from - window + 1, which lies in block keep_from / block - (window_blocks - 1) or later
* (window_blocks - 1 >= window / block). A target from the step's end is the same for a decode
* step but drops keys the earlier rows of a prefill chunk still need. */
* step but drops keys the earlier rows of a prefill chunk still need.
*
* A sequence with a runtime window is left alone. Its visible set is anchored at 0, so nothing in
* the table is below a sliding floor to recycle; the ring's blocks are rewritten in place and
* the table never moves (SeqWindow). */
void KVManager::recycle_window(Seq* s, int64_t keep_from) {
for (size_t g = 0; g < plans_.size(); ++g) {
const auto& p = plans_[g];
if (p.kind != RAD_KV_WINDOW) continue;
if (p.kind != RAD_KV_WINDOW || s->win[g].set) continue;
int64_t first = s->first_block_pos[g] / p.block_size;
int64_t target = std::max<int64_t>(first,
keep_from / p.block_size - (p.window_blocks - 1));
@@ -1065,7 +1273,9 @@ int KVManager::ensure(uint64_t id, int64_t n_tokens, int64_t keep_from) {
const auto& p = plans_[g];
if (p.paged()) {
int64_t first = s->first_block_pos[g] / p.block_size;
int64_t want = ceil_div(n_tokens, p.block_size) - first;
int64_t want = s->win[g].set
? window_table_len(p, s->win[g], n_tokens) - first
: ceil_div(n_tokens, p.block_size) - first;
need[g] = want - (int64_t)s->blocks[g].size();
if (need[g] < 0) need[g] = 0;
/* Copy-on-write: the partial block is the only one a write reaches; if it is shared,
@@ -1101,7 +1311,9 @@ int KVManager::ensure(uint64_t id, int64_t n_tokens, int64_t keep_from) {
}
}
}
int64_t want = ceil_div(n_tokens, p.block_size) - first;
int64_t want = s->win[g].set
? window_table_len(p, s->win[g], n_tokens) - first
: ceil_div(n_tokens, p.block_size) - first;
if ((int64_t)s->blocks[g].size() < want)
s->dirty_from[g] = std::min<int64_t>(s->dirty_from[g],
(int64_t)s->blocks[g].size());
@@ -1125,6 +1337,10 @@ int KVManager::adopt(uint64_t id, int32_t g, const std::vector<int32_t>& blocks,
if (!s) return RAD_E_NOTFOUND;
const KVGroupPlan* p = plan(g);
if (!p || !p->paged()) return RAD_E_INVAL;
/* A runtime window's table is its own reference plus ring, laid out against a floor no cache
* entry knows (set_window), so blocks adopted from a prefix hit would sit at the wrong
* positions. Prefix publish is skipped for such a sequence; this is the backstop. */
if (s->win[(size_t)g].set) return RAD_E_STATE;
/* Whole blocks only (see adopt in kv.h). */
if (n_tokens % p->block_size != 0) return RAD_E_INVAL;
if ((int64_t)blocks.size() != n_tokens / p->block_size) return RAD_E_INVAL;
@@ -1145,6 +1361,14 @@ int KVManager::fork(uint64_t parent, uint64_t child) {
Seq* pa = find(parent);
if (!pa) return RAD_E_NOTFOUND;
if (seqs_.count(child)) return RAD_E_DUPLICATE;
/* The window is runtime data: a child that shared the table without it would read the ring
* as if it were a reference. A forking model (speculative decoding) does not carry one. */
for (const SeqWindow& w : pa->win)
if (w.set) {
RAD_ERR("sequence %llu: fork refused, it carries a runtime window",
(unsigned long long)parent);
return RAD_E_UNSUPPORTED;
}
/* Pass 1: the child needs its own state slot in every stateful group before anything is
* shared (see fork in kv.h, spec §7.3). */
@@ -1159,6 +1383,7 @@ int KVManager::fork(uint64_t parent, uint64_t child) {
if (plans_[g].paged()) {
ch->blocks[g] = pa->blocks[g];
ch->first_block_pos[g] = pa->first_block_pos[g];
ch->win[g] = pa->win[g];
for (int32_t b : ch->blocks[g]) add_ref(g, b);
} else {
grab_state((int32_t)g, &ch->state[g]);
@@ -1188,7 +1413,10 @@ int KVManager::make_room(uint64_t id, int64_t n_tokens,
if (p.paged()) {
int64_t first = s->first_block_pos[g] / p.block_size;
int64_t want = ceil_div(n_tokens, p.block_size) - first;
if (p.kind == RAD_KV_WINDOW) want = std::min(want, window_seq_cap(p));
if (p.kind == RAD_KV_WINDOW) {
if (s->win[g].set) want = window_table_len(p, s->win[g], n_tokens) - first;
else want = std::min(want, window_seq_cap(p));
}
int64_t extra = want - (int64_t)s->blocks[g].size() + 1; /* +1 for a COW copy */
if (extra > available_blocks((int32_t)g)) return false;
} else if (p.stateful() && s->state[g] < 0 && free_states_[g].empty()) return false;
@@ -1368,6 +1596,9 @@ bool KVManager::window_rows(uint64_t id, int64_t pos, std::vector<int32_t>* out)
out->clear();
const Seq* s = find(id);
if (!s || pos <= 0) return false;
/* A runtime window is excluded from window snapshots (set_window): the window length is
* runtime data and the table is a reference plus a ring, not [lo, pos). */
for (const SeqWindow& w : s->win) if (w.set) return false;
for (size_t g = 0; g < plans_.size(); ++g) {
const KVGroupPlan& p = plans_[g];
if (!p.win_snap) continue;
@@ -1389,6 +1620,8 @@ bool KVManager::window_rows(uint64_t id, int64_t pos, std::vector<int32_t>* out)
int KVManager::seed_window(uint64_t id, int64_t pos) {
Seq* s = find(id);
if (!s) return RAD_E_NOTFOUND;
/* A sequence with a runtime window is never resumed from a window snapshot (set_window). */
for (const SeqWindow& w : s->win) if (w.set) return RAD_E_STATE;
/* Check every group before taking anything, as ensure() does. */
for (size_t g = 0; g < plans_.size(); ++g) {
const KVGroupPlan& p = plans_[g];