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 -3
View File
@@ -112,6 +112,9 @@ void BatchBuilder::carve(Set& s, char* h) {
pair(&s.h_dout, &s.d_dout, S);
pair(&s.h_rows, &s.d_rows, 4 * S);
pair(&s.h_first, &s.d_first, S * (int64_t)s.groups.size());
/* The advance's per-sequence runtime-window data (advance.h); 0 when there is none. */
pair(&s.h_rpos, &s.d_rpos, S);
pair(&s.h_tlen, &s.d_tlen, S);
s.h_grp = ::rad::carve<AdvGroup>(h, oh, (int64_t)s.groups.size());
s.d_grp = ::rad::carve<AdvGroup>(d, od, (int64_t)s.groups.size());
for (size_t gi = 0; gi < s.groups.size(); ++gi) {
@@ -690,13 +693,24 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set&
const StepEntry& e = plan.e[(size_t)i];
int64_t first = 0;
SeqWindow win;
bool has_win = false;
int64_t bt_len = 0; /* the sequence's block table, in blocks */
int64_t table_end = 0; /* the table's last position, for a runtime window's used */
if (L.paged) {
/* The manager's block table, by sequence id; Request::blocks is not the source
* of truth (see IKVManager). */
const std::vector<int32_t>& bt = kv.block_table(e.seq, (int32_t)gi);
first = kv.first_block_pos(e.seq, (int32_t)gi);
for (int32_t j = 0; j < e.n_tokens; ++j)
g.h_slot[t + j] = kvgeom_.slot((int)gi, bt, first, (int64_t)e.ctx_len + j);
has_win = kv.runtime_window(e.seq, (int32_t)gi, &win);
bt_len = (int64_t)bt.size();
table_end = bt_len * L.block_size;
for (int32_t j = 0; j < e.n_tokens; ++j) {
const int64_t pos = (int64_t)e.ctx_len + j;
g.h_slot[t + j] = has_win
? kvgeom_.slot_ring((int)gi, bt, first, pos, win)
: kvgeom_.slot((int)gi, bt, first, pos);
}
/* Restage relative to what the card holds (BtMirror). The same sequence in the
* same row keeps entries below the first changed one and below the end of both
* old and new tables, so a decode step stages nothing or one appended block. A
@@ -737,6 +751,11 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set&
s.h_convslot[(int64_t)gi * max_seqs_ + i] = st;
}
s.h_first[(int64_t)gi * max_seqs_ + i] = (int32_t)first;
/* The advance recomputes this row's used and slots after the build (advance_impl.h),
* so it needs the same per-sequence ring data this build staged: the row's floor and
* its table's length, 0 when the sequence has no runtime window. */
s.h_rpos[i] = has_win ? (int32_t)win.ring_pos : 0;
s.h_tlen[i] = has_win ? (int32_t)bt_len : 0;
t += e.n_tokens;
/* Tokens the sequence holds in this table, not min(length, window). `seqused` counts
@@ -745,8 +764,16 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set&
* unwritten slots at the end of the table, read as the newest context; a non-causal
* window group would attend to them. Do not clamp to `window` either: the held span
* may exceed it by under a block, and clamping would drop the newest tokens. Attending
* a few extra old positions is the cost of paging a window (vLLM does the same). */
* a few extra old positions is the cost of paging a window (vLLM does the same).
*
* A runtime window (R-SWA) has reused its ring's slots once the sequence is past the
* ring's span, so the held span is the table's, not the context's: every slot of the
* reference and of the ring holds live KV, and the ring's blocks are what a non-causal
* decode reads. Before the ring is full the shorter of the two is the written count. */
int64_t used = (int64_t)e.ctx_len + e.n_tokens - first;
if (has_win) {
used = std::min<int64_t>(used, table_end);
}
if (used < 0) used = 0;
g.h_used[i] = (int32_t)used;
if (L.paged) {
@@ -773,6 +800,23 @@ int BatchBuilder::build_groups(const StepPlan& plan, const IKVManager& kv, Set&
a.used_off = (const char*)g.d_used - d0;
a.bt_off = (const char*)g.d_bt - d0;
a.first_off = (const char*)(s.d_first + (int64_t)gi * max_seqs_) - d0;
/* The runtime window (KVManager::set_window): the advance runs after this build and
* overwrites `used` and the slots, so it must reproduce the ring's mapping rather
* than the linear one (advance_impl.h). The floor and table length are per sequence
* (s.h_rpos / s.h_tlen), the ring's block count is the group's W. */
a.ring = 0;
a.ring_blocks = 0;
a.rpos_off = (const char*)s.d_rpos - d0;
a.tlen_off = (const char*)s.d_tlen - d0;
for (int64_t i = 0; i < n_seq; ++i) {
const StepEntry& e = plan.e[(size_t)i];
SeqWindow w;
if (kv.runtime_window(e.seq, (int32_t)gi, &w)) {
a.ring = 1;
a.ring_blocks = (int32_t)w.ring_blocks;
break;
}
}
}
for (size_t r = 0; r < ranks_.size(); ++r) {