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];
+114 -3
View File
@@ -11,6 +11,10 @@
* RAD_KV_LINEAR per-sequence, fixed size: the GDN recurrent state
* RAD_KV_CONV per-sequence, fixed size: a rolling window of conv_width-1 + n_spec entries
*
* A RAD_KV_RING group is a RAD_KV_WINDOW whose floor is per sequence and set at runtime (R-SWA,
* see SeqWindow); it is planned as a WINDOW group with `runtime_win` set, so no kind of its own
* reaches the plan.
*
* The two stateful kinds are also allocated, mapped and freed here, so one allocator decides what
* a sequence owns. With two, a freed sequence's state slot could still be handed to the next
* request by the other.
@@ -50,6 +54,45 @@ struct BundleComparison {
};
BundleComparison compare_bundling(const std::vector<int64_t>& bundles);
/* ------------------------------------------------------------------ the runtime window */
/* A sequence's per-sequence window on a RAD_KV_WINDOW group, set at runtime (R-SWA, the
* "reference + ring" attention of Unlimited-OCR: a full causal prefill over the prompt and image
* tokens, then a ring of W decode slots right after it, rewritten in place forever).
*
* A fixed sliding window (`KVGroupPlan::window_blocks`, gemma4's) keeps the last W tokens: the
* table starts where the window begins and blocks leave from its front. A runtime window instead
* retains the whole prefill as the reference and appends into a ring after it:
*
* visible = [0, visible_lo) U [visible_lo, visible_lo + W)
*
* `visible_lo` is the sequence's own prefill length (image tokens included), so the visible set is
* per sequence. The reference is never recycled and first_block_pos stays 0 (the table still
* starts at position 0); only the ring's slot mapping recycles, so RoPE positions keep increasing
* with the token's own position and a decode query sees the whole reference plus the ring.
*
* Blocks are whole, so the ring can start only on a block boundary: it starts at
* `ring_pos = ceil(visible_lo / block) * block` with `ceil(W / block)` blocks, and the tokens in
* [visible_lo, ring_pos) stay in the reference's last block (they are the oldest of the tail and
* are never rewritten). The visible tail is therefore W .. W + block - 1 tokens when W is a block
* multiple -- with block_size 128 = W the 128-256 token granularity of the plan, exact even when
* the floor is not aligned -- and up to one block more otherwise, always a superset of the
* reference implementation's last W. The ring's blocks hold the newest positions in place (no
* block is allocated past them, so the sequence's bytes are flat once the floor is set):
*
* block index = ring_pos / block + ((pos - ring_pos) / block) % ring_blocks
*
* A sequence with a runtime window is excluded from prefix-cache reuse, window snapshots, forking
* and speculative rollback: all three assume the declare-time window, and the runtime length is
* not in any cache key (see KVManager::set_window). */
struct SeqWindow {
bool set = false;
int64_t window = 0; /* W as set */
int64_t visible_lo = 0; /* the retained reference ends here */
int64_t ring_pos = 0; /* block-aligned first ring token position */
int64_t ring_blocks = 0; /* whole blocks the ring holds */
int64_t ring_span(int64_t block) const { return ring_blocks * block; }
};
/* ------------------------------------------------------------------ per-group plan */
struct KVGroupPlan {
int32_t index = -1; /* position in Program::kv_groups */
@@ -62,6 +105,18 @@ struct KVGroupPlan {
int64_t bytes_per_state = 0; /* LINEAR/CONV: per sequence, every layer */
int64_t conv_slots = 0; /* CONV: conv_width - 1 + n_spec */
int64_t window_blocks = 0; /* WINDOW: the per-sequence block cap */
/* WINDOW: sequences may be given a runtime window (declare_runtime_window). Such a group is
* sized for the whole context plus one ring per sequence instead of being capped at
* window_blocks, its window is not snapshotted, and a sequence armed with one is bounded by
* its own reference + ring rather than by window_blocks. */
bool runtime_win = false;
int64_t runtime_tokens = 0; /* the W set_window arms a sequence with */
/* The most ring blocks one sequence needs: ceil(W / block), the reference's fraction d of
* the ring's first block being frozen in it (see SeqWindow). */
int64_t ring_blocks_max() const {
return runtime_tokens > 0 && block_size > 0
? (runtime_tokens + block_size - 1) / block_size : 0;
}
/* WINDOW: a checkpoint slot carries this group's window (has_window_snapshot). Set only for a
* trunk group of a model with no linear state. A drafter's window is recomputed by draft
* refill, and a model with linear state re-prefills a hit on a windowed trunk, so neither
@@ -202,9 +257,46 @@ public:
/* Recycles a committed chunk's window blocks. ensure() keeps a chunk's blocks beside the
* window and only the next ensure() recycles them, so a prefill waiting for its next chunk
* would hold a whole chunk of blocks; several such prefills can exhaust the pool. The
* scheduler calls this when it advances n_computed, passing the new n_computed. */
* scheduler calls this when it advances n_computed, passing the new n_computed.
*
* A sequence with a runtime window retains its reference and rewrites its ring in place, so
* nothing below the floor may be recycled and this is a no-op for it. */
void trim_window(uint64_t seq, int64_t keep_from);
/* ---------------- the runtime window (R-SWA; see SeqWindow) ---------------- */
/* Mark a WINDOW group as carrying per-sequence runtime windows of `window_tokens`. Call before
* plan_groups()/configure(): it changes the plan (the group is sized for the whole context
* plus one ring a sequence, its window is not snapshotted, and the ring joins the fixed
* costs), so a group declared after the plan is refused. A fixed-window group is untouched.
*
* A group that is not marked still accepts set_window() -- the caller then sizes the pool
* itself, e.g. by declaring the group's window as the whole context -- and each sequence's
* capacity is checked at set_window()/ensure() against the blocks actually carved. */
int declare_runtime_window(int32_t group, int64_t window_tokens);
/* Give a sequence a runtime window with `visible_lo` (its prefill length, image tokens
* included) as the retained reference's end and a ring of `window_tokens` after it. Every
* paged WINDOW group of the sequence gets the same floor. Call it before the sequence's KV
* grows past `visible_lo` -- at admission, where the scheduler knows the prompt length -- so
* the reference is written linearly and no checkpoint can be reserved for it (a snapshot
* would describe a window the sequence no longer has).
*
* RAD_E_UNSUPPORTED no windowed group;
* RAD_E_INVAL a negative floor or a window of 0;
* RAD_E_STATE the sequence already holds tokens past the floor;
* RAD_E_FULL the reference plus the ring exceeds what this group carved. */
int set_window(uint64_t seq, int64_t visible_lo, int64_t window_tokens);
/* The sequence's window in `group`, or false when it has none (a fixed-window group keeps
* KVGroupPlan::window_blocks and first_block_pos). The step batch maps a ring position with
* SeqWindow's rule; every other position maps as (pos - first_block_pos) / block_size. */
bool runtime_window(uint64_t seq, int32_t group, SeqWindow* out) const;
/* Whether any group has a runtime window set for the sequence. Prefix publish, checkpoint
* reservation and snapshots are skipped for it (see set_window). */
bool has_runtime_window(uint64_t seq) const;
/* The W declare_runtime_window gave the group, 0 if it carries no runtime windows. */
int64_t runtime_window_tokens(int32_t group) const;
/* 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
* corrupt the cache entry. Bumps each block's refcount. */
@@ -359,7 +451,11 @@ public:
/* The absolute token position block_table(seq, group)[0] covers: zero for a FULL group, the
* recycled prefix for a WINDOW group. The slot mapping is (pos - first_block_pos) /
* block_size; without the offset a sliding-window sequence writes to the wrong slot once it
* passes its window. */
* passes its window.
*
* A sequence with a runtime window (set_window) keeps 0 for the group's whole life: its table
* is [reference][ring] from position 0 and only the ring's slot index recycles (SeqWindow),
* so the mapping above holds for the reference and SeqWindow's rule for the ring. */
int64_t first_block_pos(uint64_t seq, int32_t group) const;
/* The first table entry changed since the last call; entries below it are unchanged, and
@@ -457,6 +553,9 @@ private:
* for a windowed group. 0 for kUnboundedTokens, which is not a count. */
int64_t zero_blocks(const KVGroupPlan& p, int64_t tokens) const;
void forget();
/* Turns a WINDOW group into a runtime-window one (declare_runtime_window). Returns false for a
* group that is not windowed. */
bool apply_runtime_window(KVGroupPlan& p) const;
/* 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,
@@ -467,6 +566,7 @@ private:
int64_t n_tokens = 0;
std::vector<std::vector<int32_t>> blocks; /* per group */
std::vector<int64_t> first_block_pos; /* WINDOW: token pos of blocks[0] */
std::vector<SeqWindow> win; /* per group: the runtime window, if any */
std::vector<int32_t> state; /* per group, -1 when not stateful */
/* Per group, the lowest table entry written since the batch builder last staged this
* sequence. An append or truncation lowers it to where it touched; a shift lowers it to
@@ -482,8 +582,16 @@ private:
void drop_state(int32_t g, int32_t s);
Seq* find(uint64_t seq);
const Seq* find(uint64_t seq) const;
/* Recycle a WINDOW group's blocks no query at or past `keep_from` can read. */
/* Recycle a WINDOW group's blocks no query at or past `keep_from` can read. A runtime window
* (SeqWindow) is not trimmed: its reference must stay and its ring rewrites slots in place. */
void recycle_window(Seq* s, int64_t keep_from);
/* The block-table index the block holding `pos` has, or -1 past the table: the fixed mapping
* (pos - first_block_pos) / block_size, or the ring's for a runtime window. */
static int64_t table_index(const KVGroupPlan& p, const SeqWindow& w, int64_t first_block_pos,
int64_t pos, int64_t table_len);
/* The table length a runtime-windowed sequence needs to hold `n_tokens`: the reference's
* blocks, then the ring's up to its whole span. */
static int64_t window_table_len(const KVGroupPlan& p, const SeqWindow& w, int64_t n_tokens);
std::vector<KVGroupPlan> plans_;
std::vector<std::vector<int32_t>> free_blocks_; /* per group */
@@ -511,6 +619,9 @@ private:
int64_t fixed_ = 0; /* the non-negotiable floor: states, conv slots, padding */
int64_t ckpts_ = 0; /* --checkpoint-slots, kept for the warning on a model with none */
int64_t tokens_cap_ = 0; /* cap_paged_tokens(): the least any rank can carve; 0 is none */
/* declare_runtime_window(): group -> W. Survives forget(), which plan_groups() calls at its
* start: it is a declaration about the program, not about a plan. */
std::unordered_map<int32_t, int64_t> runtime_decl_;
/* ---- the loan (see the public block) ----
*