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

+47 -2
View File
@@ -44,6 +44,24 @@ struct IKVManager {
/* After a committed step, a window group frees what no later query can read
* (KVManager::trim_window). */
virtual void trim_window(uint64_t seq, int64_t keep_from) { (void)seq; (void)keep_from; }
/* R-SWA (KVManager::declare_runtime_window): the W this group arms a sequence with, 0 when it
* carries no runtime windows. The scheduler arms every sequence it admits with
* set_window(seq, prompt length, W); a manager with no runtime window refuses it and the
* scheduler never asks. */
virtual int64_t runtime_window_tokens(int32_t group) const { (void)group; return 0; }
virtual int set_window(uint64_t seq, int64_t visible_lo, int64_t window_tokens) {
(void)seq; (void)visible_lo; (void)window_tokens;
return RAD_E_UNSUPPORTED;
}
/* The sequence's runtime window in `group`, if it has one (KVManager::runtime_window). A
* sequence with one is excluded from prefix publish, checkpoint snapshots and speculation:
* the window is runtime data and the ring rewrites its slots in place. */
virtual bool runtime_window(uint64_t seq, int32_t group, SeqWindow* out) const {
(void)seq; (void)group; (void)out;
return false;
}
virtual bool has_runtime_window(uint64_t seq) const { (void)seq; return false; }
/* Window snapshot of a prefix hit (KVManager::has_window_snapshot / seed_window). The
* defaults mean no hit on a windowed trunk is ever resumed. */
virtual bool has_window_snapshot() const { return false; }
@@ -95,7 +113,11 @@ struct IKVManager {
/* Absolute token position that block_table(seq, group)[0] covers: zero for a full group,
* non-zero for a window group once blocks below the window are recycled. The step batch maps
* a slot as `(pos - first_block_pos) / block_size`. */
* a slot as `(pos - first_block_pos) / block_size`.
*
* A sequence with a runtime window (runtime_window) keeps first_block_pos 0 even once the ring
* recycles: its table is [reference][ring] from position 0 and the ring's positions map
* modulo ring_blocks (SeqWindow), so only the slot index recycles, never the table. */
virtual int64_t first_block_pos(uint64_t seq, int32_t group) const = 0;
/* Index of the first entry of block_table(seq, group) changed since the previous call;
@@ -213,7 +235,7 @@ struct KVGeom {
for (const KVGroupInfo& gi : groups) {
G e;
e.kind = gi.decl.kind;
e.paged = (e.kind == RAD_KV_FULL || e.kind == RAD_KV_WINDOW);
e.paged = (e.kind == RAD_KV_FULL || e.kind == RAD_KV_WINDOW || e.kind == RAD_KV_RING);
e.block_size = gi.block_size > 0 ? gi.block_size : 1;
e.window = gi.decl.window;
k.g.push_back(e);
@@ -229,6 +251,23 @@ struct KVGeom {
if (bi < 0 || bi >= (int64_t)bt.size()) return -1;
return (int32_t)(bt[(size_t)bi] * e.block_size + (pos % e.block_size));
}
/* The same slot for a runtime window (SeqWindow): the reference maps linearly (first_pos is
* 0 there), and a position at or past the ring's start lives in block
* ring_pos/block + ((pos - ring_pos)/block) % ring_blocks, which is what rewrites the ring's
* slots in place. ring_pos is block-aligned, so `pos % block` stays the in-block offset. */
int32_t slot_ring(int gi, const std::vector<int32_t>& bt, int64_t first_pos, int64_t pos,
const SeqWindow& w) const {
const G& e = g[(size_t)gi];
int64_t bi;
if (w.set && pos >= w.ring_pos) {
bi = w.ring_pos / e.block_size + ((pos - w.ring_pos) / e.block_size) % w.ring_blocks;
} else {
bi = (pos - first_pos) / e.block_size;
}
if (bi < 0 || bi >= (int64_t)bt.size()) return -1;
return (int32_t)(bt[(size_t)bi] * e.block_size + (pos % e.block_size));
}
};
/* ------------------------------------------------------------------ binding the concrete classes */
@@ -244,6 +283,12 @@ 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); }
int64_t runtime_window_tokens(int32_t g) const override { return m->runtime_window_tokens(g); }
int set_window(uint64_t s, int64_t lo, int64_t w) override { return m->set_window(s, lo, w); }
bool runtime_window(uint64_t s, int32_t g, SeqWindow* o) const override {
return m->runtime_window(s, g, o);
}
bool has_runtime_window(uint64_t s) const override { return m->has_runtime_window(s); }
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<int32_t>& b, int64_t n) override {