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

+31 -1
View File
@@ -12,6 +12,7 @@
#include <cfloat>
#include <cmath>
#include <numeric>
#include <unordered_set>
namespace rad {
@@ -526,8 +527,33 @@ int32_t host_argmax(const float* logits, int64_t n_vocab) {
return (int32_t)best;
}
/* ================================================================== the chain */
void host_ngram_ban(CandidateSet& s, const int32_t* history, int64_t n_history, int32_t n,
int32_t window) {
if (n <= 0 || !history || n_history < (int64_t)n) return;
const int64_t len = n_history;
const int64_t win = (window > 0 && window < len) ? window : len;
const int64_t start = len - win, end = len - n + 1;
if (end <= start) return;
const int64_t plen = (int64_t)n - 1, p0 = len - plen;
/* The set of tokens a continuation would complete an n-gram with, as the reference builds it:
* every n-gram in the window whose first n-1 tokens equal the current prefix contributes the
* token after them. */
std::unordered_set<int32_t> banned;
for (int64_t idx = start; idx < end; ++idx) {
bool match = true;
for (int64_t j = 0; j < plen; ++j) {
if (history[idx + j] != history[p0 + j]) { match = false; break; }
}
if (match) banned.insert(history[idx + plen]);
}
if (banned.empty()) return;
s.sorted = false; /* a -infinity candidate belongs after every finite one */
for (Candidate& c : s.c)
if (banned.count(c.id) != 0) c.logit = -INFINITY;
}
/* ================================================================== the chain */
int32_t host_sample_into(CandidateSet& s, const SamplingParams& sp, const HostRefInput& in,
ChainOrder order) {
/* Penalties first in both orders: they act on raw logits, the cuts on probabilities. */
@@ -547,6 +573,10 @@ int32_t host_sample_into(CandidateSet& s, const SamplingParams& sp, const HostRe
in.dry_breakers ? *in.dry_breakers : kNone);
}
/* The n-gram ban, after the penalties and before the greedy split: the device chain issues it
* there, so a greedy request sees the same masked distribution. Off at ngram_ban_n 0. */
host_ngram_ban(s, in.history, in.n_history, sp.ngram_ban_n, sp.ngram_window);
/* Greedy skips the chain: penalties -> mask -> argmax. Temperature 0 collapses every other
* logit to -inf, so a mask applied after it could leave nothing. */
if (sp.greedy()) {