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

+80
View File
@@ -235,6 +235,86 @@ int ref_sample_dry(const RadArgs* a, RadStream) {
return RAD_OK;
}
/* ------------------------------------------------------------------ sample_ngram_ban */
/* The windowed no-repeat-ngram ban: Unlimited-OCR's `SlidingWindowNoRepeatNgramProcessor`
* (modeling_unlimitedocr.py:353-383), which SGLang ships as
* DeepseekOCRNoRepeatNGramLogitProcessor. With the row's history `s[0, hl)` (oldest first, `hl`
* committed tokens including the one just emitted), `n` and `w` from the `ngram` plane (an i32
* pair a row, {n-gram length, window}):
*
* search_start = max(0, hl - w) (w <= 0: the whole span)
* search_end = hl - n + 1
* prefix = s[hl - n + 1, hl) (empty at n == 1)
* for idx in [search_start, search_end):
* if s[idx, idx + n - 1) == prefix: ban s[idx + n - 1]
*
* A banned token's logit becomes -infinity, exactly as the reference writes it, so the argmax and
* every later stage see the same distribution. The n-gram that spans the window boundary is one
* of the searched ones, as in the reference: the window is applied to the START index, not to the
* n-gram's end.
*
* Ids are global and columns are global less `vocab_off`; a banned token on another rank's shard
* is that rank's to mask. Rows whose n is 0 (the default) are untouched, so a request without the
* ban leaves the logits bit for bit as they were. The scan is O(w * n) a row and compares
* integers, so every implementation (this one, libavx, libr4d) agrees by construction. */
int ref_sample_ngram_ban(const RadArgs* a, RadStream) {
if (!have(a, 4)) return RAD_E_INVAL;
const RadTensor *lg = &a->t[0], *pr = &a->t[1], *hist = &a->t[2], *ng = &a->t[3];
const int64_t n_vocab = p_int(a, "n_vocab", lg->rank >= 2 ? lg->shape[lg->rank - 1] : 0);
if (n_vocab <= 0) return RAD_E_SHAPE;
const int64_t M = numel(lg) / n_vocab;
if (M <= 0) return RAD_E_SHAPE;
Rows R;
RAD_REF_TRY(param_rows(pr, M, &R));
const int64_t hmax = numel(hist) / M;
if (hmax <= 0) return RAD_E_SHAPE;
/* One {n, window} pair a row. At least M pairs, so a plane shared by sparser row sets is not
* read past its end. */
if (numel(ng) < 2 * M) return RAD_E_SHAPE;
const int64_t voff = p_int(a, "vocab_off", 0);
REF_PAR_FOR(M * hmax)
for (int64_t m = 0; m < M; ++m) {
const RadSampleParams& sp = R.p[m];
const int64_t nb = rowoff(ng, m, 2), ns = laststride(ng);
const int64_t n = ld_int(ng->dtype, ng->data, nb);
const int64_t win_req = ld_int(ng->dtype, ng->data, nb + ns);
if (n <= 0) continue;
/* The whole staged span; the window below keeps its tail. last_n -1 is hist_window's
* "the whole span". */
int64_t ho = 0, hl = 0;
if (!hist_window(sp, -1, hmax, &ho, &hl)) continue;
if (hl < n) continue;
const int64_t win = win_req > 0 && win_req < hl ? win_req : hl;
const int64_t search_start = hl - win;
const int64_t search_end = hl - n + 1;
if (search_end <= search_start) continue;
const int64_t hb = rowoff(hist, m, hmax) + ho * laststride(hist);
const int64_t hs = laststride(hist);
const int64_t lb = rowoff(lg, m, n_vocab), ls = laststride(lg);
auto tok_at = [&](int64_t i) {
return ld_int(hist->dtype, hist->data, hb + i * hs);
};
/* n == 1 bans every token in the window: the prefix is empty, as in the reference. */
const int64_t plen = n - 1, p0 = hl - plen;
for (int64_t idx = search_start; idx < search_end; ++idx) {
bool match = true;
for (int64_t j = 0; j < plen; ++j) {
if (tok_at(idx + j) != tok_at(p0 + j)) { match = false; break; }
}
if (!match) continue;
const int64_t tok = tok_at(idx + plen);
const int64_t col = tok - voff;
if (tok < 0 || col < 0 || col >= n_vocab) continue;
stt(lg, lb + col * ls, -INFINITY);
}
}
return RAD_OK;
}
/* ------------------------------------------------------------------ sample_temp */
/* temp <= 0 leaves the row untouched; greedy decoding is `sample_argmax`. */
int ref_sample_temp(const RadArgs* a, RadStream) {