unlimitedocr: tiled/base e2e parity, per-segment tower layout, n-gram whitelist

The pass packer now keeps a view and its one-cell separator in one pass, and
RadBatch carries each segment's grid host-side (enc_seg_h/enc_seg_w) so the
DeepEncoder no longer reads device coordinates to pick a geometry. The tower
lays out a line of n tiles as kMap rows of n*kMap features plus one newline
cell, accepts an earlier chunk of a longer line, and emits the learned
view_seperator for an explicit 4x4 separator rather than any square segment.

The device sam_attn resamples a rel_pos table that is not 2*tile-1 rows on
read (the 127-row global tables serve a 40-grid block as 79), matching
libref/libavx. The n-gram ban gains the reference's optional whitelist
(5th operand, OAI keys, generation_config alias) with range/dtype checks.

Gates: tiled e2e stage cosines 0.9994-1.0, det/text 0.9901 with every
coordinate within 1 px; two-page 0.9917 within 2 px; device selftest
sam_attn (incl. 127->79) and ngram whitelist (1344 columns) 0 mismatches.
This commit is contained in:
Juup committed 2026-10-20 07:04:01 +00:00
1 parent fad4ba7f1a
commit 52637ec186
37 files changed
+401566 -778

No files matched your search

+20 -1
View File
@@ -256,10 +256,17 @@ int ref_sample_dry(const RadArgs* a, RadStream) {
* 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. */
* integers, so every implementation (this one, libavx, libr4d) agrees by construction.
*
* The optional `whitelist` bitmask is the reference's whitelist: a token whose column bit is set
* in the row's own plane row is removed from the banned set before the -inf writes
* (`banned.difference_update(self.whitelist)`, modeling_unlimitedocr.py:379), so it is never
* banned. Absent, nothing is exempt -- the default serving form, bit-identical to the ban
* without the operand. */
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 RadTensor* wl = a->n_t > 4 && a->t[4].data ? &a->t[4] : nullptr;
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;
@@ -272,6 +279,9 @@ int ref_sample_ngram_ban(const RadArgs* a, RadStream) {
* read past its end. */
if (numel(ng) < 2 * M) return RAD_E_SHAPE;
const int64_t voff = p_int(a, "vocab_off", 0);
const int64_t wwords = (n_vocab + 31) / 32;
if (wl && wl->dtype != RAD_I32 && wl->dtype != RAD_U32) return RAD_E_DTYPE;
if (wl && numel(wl) < M * wwords) return RAD_E_SHAPE;
REF_PAR_FOR(M * hmax)
for (int64_t m = 0; m < M; ++m) {
@@ -294,9 +304,17 @@ int ref_sample_ngram_ban(const RadArgs* a, RadStream) {
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);
const int64_t wb = wl ? rowoff(wl, m, wwords) : 0, ws = wl ? laststride(wl) : 0;
auto tok_at = [&](int64_t i) {
return ld_int(hist->dtype, hist->data, hb + i * hs);
};
/* The reference removes the whitelist from the banned set before it writes; a column
* whose bit is set in the row's whitelist words is therefore never written. */
auto exempt = [&](int64_t col) {
if (!wl) return false;
const uint32_t word = (uint32_t)ld_int(wl->dtype, wl->data, wb + (col / 32) * ws);
return ((word >> (uint32_t)(col % 32)) & 1u) != 0u;
};
/* 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;
@@ -309,6 +327,7 @@ int ref_sample_ngram_ban(const RadArgs* a, RadStream) {
const int64_t tok = tok_at(idx + plen);
const int64_t col = tok - voff;
if (tok < 0 || col < 0 || col >= n_vocab) continue;
if (exempt(col)) continue;
stt(lg, lb + col * ls, -INFINITY);
}
}